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(ISD::SHL, DL, TotalVT, Hi,
227 DAG.getConstant(Lo.getValueSizeInBits(), DL,
229 TotalVT, DAG.getDataLayout())));
230 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
231 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
232 }
233 } else if (PartVT.isFloatingPoint()) {
234 // FP split into multiple FP parts (for ppcf128)
235 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
236 "Unexpected split");
237 SDValue Lo, Hi;
238 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
239 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
240 if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
241 std::swap(Lo, Hi);
242 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
243 } else {
244 // FP split into integer parts (soft fp)
245 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
246 !PartVT.isVector() && "Unexpected split");
247 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
248 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V,
249 InChain, CC);
250 }
251 }
252
253 // There is now one part, held in Val. Correct it to match ValueVT.
254 // PartEVT is the type of the register class that holds the value.
255 // ValueVT is the type of the inline asm operation.
256 EVT PartEVT = Val.getValueType();
257
258 if (PartEVT == ValueVT)
259 return Val;
260
261 if (PartEVT.isInteger() && ValueVT.isFloatingPoint() &&
262 ValueVT.bitsLT(PartEVT)) {
263 // For an FP value in an integer part, we need to truncate to the right
264 // width first.
265 PartEVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
266 Val = DAG.getNode(ISD::TRUNCATE, DL, PartEVT, Val);
267 }
268
269 // Handle types that have the same size.
270 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
271 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
272
273 // Handle types with different sizes.
274 if (PartEVT.isInteger() && ValueVT.isInteger()) {
275 if (ValueVT.bitsLT(PartEVT)) {
276 // For a truncate, see if we have any information to
277 // indicate whether the truncated bits will always be
278 // zero or sign-extension.
279 if (AssertOp)
280 Val = DAG.getNode(*AssertOp, DL, PartEVT, Val,
281 DAG.getValueType(ValueVT));
282 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
283 }
284 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
285 }
286
287 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
288 // FP_ROUND's are always exact here.
289 if (ValueVT.bitsLT(Val.getValueType())) {
290
291 SDValue NoChange =
293
295 llvm::Attribute::StrictFP)) {
296 return DAG.getNode(ISD::STRICT_FP_ROUND, DL,
297 DAG.getVTList(ValueVT, MVT::Other), InChain, Val,
298 NoChange);
299 }
300
301 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, NoChange);
302 }
303
304 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
305 }
306
307 // Handle MMX to a narrower integer type by bitcasting MMX to integer and
308 // then truncating.
309 if (PartEVT == MVT::x86mmx && ValueVT.isInteger() &&
310 ValueVT.bitsLT(PartEVT)) {
311 Val = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Val);
312 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
313 }
314
315 report_fatal_error("Unknown mismatch in getCopyFromParts!");
316}
317
319 const Twine &ErrMsg) {
320 const Instruction *I = dyn_cast_or_null<Instruction>(V);
321 if (!I)
322 return Ctx.emitError(ErrMsg);
323
324 if (const CallInst *CI = dyn_cast<CallInst>(I))
325 if (CI->isInlineAsm()) {
327 *CI, ErrMsg + ", possible invalid constraint for vector type"));
328 }
329
330 return Ctx.emitError(I, ErrMsg);
331}
332
333/// getCopyFromPartsVector - Create a value that contains the specified legal
334/// parts combined into the value they represent. If the parts combine to a
335/// type larger than ValueVT then AssertOp can be used to specify whether the
336/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
337/// ValueVT (ISD::AssertSext).
339 const SDValue *Parts, unsigned NumParts,
340 MVT PartVT, EVT ValueVT, const Value *V,
341 SDValue InChain,
342 std::optional<CallingConv::ID> CallConv) {
343 assert(ValueVT.isVector() && "Not a vector value");
344 assert(NumParts > 0 && "No parts to assemble!");
345 const bool IsABIRegCopy = CallConv.has_value();
346
347 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
348 SDValue Val = Parts[0];
349
350 // Handle a multi-element vector.
351 if (NumParts > 1) {
352 EVT IntermediateVT;
353 MVT RegisterVT;
354 unsigned NumIntermediates;
355 unsigned NumRegs;
356
357 if (IsABIRegCopy) {
359 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT,
360 NumIntermediates, RegisterVT);
361 } else {
362 NumRegs =
363 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
364 NumIntermediates, RegisterVT);
365 }
366
367 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
368 NumParts = NumRegs; // Silence a compiler warning.
369 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
370 assert(RegisterVT.getSizeInBits() ==
371 Parts[0].getSimpleValueType().getSizeInBits() &&
372 "Part type sizes don't match!");
373
374 // Assemble the parts into intermediate operands.
375 SmallVector<SDValue, 8> Ops(NumIntermediates);
376 if (NumIntermediates == NumParts) {
377 // If the register was not expanded, truncate or copy the value,
378 // as appropriate.
379 for (unsigned i = 0; i != NumParts; ++i)
380 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, PartVT, IntermediateVT,
381 V, InChain, CallConv);
382 } else if (NumParts > 0) {
383 // If the intermediate type was expanded, build the intermediate
384 // operands from the parts.
385 assert(NumParts % NumIntermediates == 0 &&
386 "Must expand into a divisible number of parts!");
387 unsigned Factor = NumParts / NumIntermediates;
388 for (unsigned i = 0; i != NumIntermediates; ++i)
389 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, PartVT,
390 IntermediateVT, V, InChain, CallConv);
391 }
392
393 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
394 // intermediate operands.
395 EVT BuiltVectorTy =
396 IntermediateVT.isVector()
398 *DAG.getContext(), IntermediateVT.getScalarType(),
399 IntermediateVT.getVectorElementCount() * NumParts)
401 IntermediateVT.getScalarType(),
402 NumIntermediates);
403 Val = DAG.getNode(IntermediateVT.isVector() ? ISD::CONCAT_VECTORS
405 DL, BuiltVectorTy, Ops);
406 }
407
408 // There is now one part, held in Val. Correct it to match ValueVT.
409 EVT PartEVT = Val.getValueType();
410
411 if (PartEVT == ValueVT)
412 return Val;
413
414 if (PartEVT.isVector()) {
415 // Vector/Vector bitcast.
416 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
417 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
418
419 // If the parts vector has more elements than the value vector, then we
420 // have a vector widening case (e.g. <2 x float> -> <4 x float>).
421 // Extract the elements we want.
422 if (PartEVT.getVectorElementCount() != ValueVT.getVectorElementCount()) {
425 (PartEVT.getVectorElementCount().isScalable() ==
426 ValueVT.getVectorElementCount().isScalable()) &&
427 "Cannot narrow, it would be a lossy transformation");
428 PartEVT =
430 ValueVT.getVectorElementCount());
431 Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, PartEVT, Val,
432 DAG.getVectorIdxConstant(0, DL));
433 if (PartEVT == ValueVT)
434 return Val;
435 if (PartEVT.isInteger() && ValueVT.isFloatingPoint())
436 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
437
438 // Vector/Vector bitcast (e.g. <2 x bfloat> -> <2 x half>).
439 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
440 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
441 }
442
443 // Promoted vector extract
444 return DAG.getAnyExtOrTrunc(Val, DL, ValueVT);
445 }
446
447 // Trivial bitcast if the types are the same size and the destination
448 // vector type is legal.
449 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
450 TLI.isTypeLegal(ValueVT))
451 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
452
453 if (ValueVT.getVectorNumElements() != 1) {
454 // Certain ABIs require that vectors are passed as integers. For vectors
455 // are the same size, this is an obvious bitcast.
456 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) {
457 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
458 } else if (ValueVT.bitsLT(PartEVT)) {
459 const uint64_t ValueSize = ValueVT.getFixedSizeInBits();
460 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
461 // Drop the extra bits.
462 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
463 return DAG.getBitcast(ValueVT, Val);
464 }
465
467 *DAG.getContext(), V, "non-trivial scalar-to-vector conversion");
468 return DAG.getUNDEF(ValueVT);
469 }
470
471 // Handle cases such as i8 -> <1 x i1>
472 EVT ValueSVT = ValueVT.getVectorElementType();
473 if (ValueVT.getVectorNumElements() == 1 && ValueSVT != PartEVT) {
474 unsigned ValueSize = ValueSVT.getSizeInBits();
475 if (ValueSize == PartEVT.getSizeInBits()) {
476 Val = DAG.getNode(ISD::BITCAST, DL, ValueSVT, Val);
477 } else if (ValueSVT.isFloatingPoint() && PartEVT.isInteger()) {
478 // It's possible a scalar floating point type gets softened to integer and
479 // then promoted to a larger integer. If PartEVT is the larger integer
480 // we need to truncate it and then bitcast to the FP type.
481 assert(ValueSVT.bitsLT(PartEVT) && "Unexpected types");
482 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
483 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
484 Val = DAG.getBitcast(ValueSVT, Val);
485 } else {
486 Val = ValueVT.isFloatingPoint()
487 ? DAG.getFPExtendOrRound(Val, DL, ValueSVT)
488 : DAG.getAnyExtOrTrunc(Val, DL, ValueSVT);
489 }
490 }
491
492 return DAG.getBuildVector(ValueVT, DL, Val);
493}
494
495static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl,
496 SDValue Val, SDValue *Parts, unsigned NumParts,
497 MVT PartVT, const Value *V,
498 std::optional<CallingConv::ID> CallConv);
499
500/// getCopyToParts - Create a series of nodes that contain the specified value
501/// split into legal parts. If the parts contain more bits than Val, then, for
502/// integers, ExtendKind can be used to specify how to generate the extra bits.
503static void
505 unsigned NumParts, MVT PartVT, const Value *V,
506 std::optional<CallingConv::ID> CallConv = std::nullopt,
507 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
508 // Let the target split the parts if it wants to
509 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
510 if (TLI.splitValueIntoRegisterParts(DAG, DL, Val, Parts, NumParts, PartVT,
511 CallConv))
512 return;
513 EVT ValueVT = Val.getValueType();
514
515 // Handle the vector case separately.
516 if (ValueVT.isVector())
517 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V,
518 CallConv);
519
520 unsigned OrigNumParts = NumParts;
522 "Copying to an illegal type!");
523
524 if (NumParts == 0)
525 return;
526
527 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
528 EVT PartEVT = PartVT;
529 if (PartEVT == ValueVT) {
530 assert(NumParts == 1 && "No-op copy with multiple parts!");
531 Parts[0] = Val;
532 return;
533 }
534
535 unsigned PartBits = PartVT.getSizeInBits();
536 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
537 // If the parts cover more bits than the value has, promote the value.
538 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
539 assert(NumParts == 1 && "Do not know what to promote to!");
540 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
541 } else {
542 if (ValueVT.isFloatingPoint()) {
543 // FP values need to be bitcast, then extended if they are being put
544 // into a larger container.
545 ValueVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
546 Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
547 }
548 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
549 ValueVT.isInteger() &&
550 "Unknown mismatch!");
551 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
552 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
553 if (PartVT == MVT::x86mmx)
554 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
555 }
556 } else if (PartBits == ValueVT.getSizeInBits()) {
557 // Different types of the same size.
558 assert(NumParts == 1 && PartEVT != ValueVT);
559 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
560 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
561 // If the parts cover less bits than value has, truncate the value.
562 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
563 ValueVT.isInteger() &&
564 "Unknown mismatch!");
565 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
566 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
567 if (PartVT == MVT::x86mmx)
568 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
569 }
570
571 // The value may have changed - recompute ValueVT.
572 ValueVT = Val.getValueType();
573 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
574 "Failed to tile the value with PartVT!");
575
576 if (NumParts == 1) {
577 if (PartEVT != ValueVT) {
579 "scalar-to-vector conversion failed");
580 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
581 }
582
583 Parts[0] = Val;
584 return;
585 }
586
587 // Expand the value into multiple parts.
588 if (NumParts & (NumParts - 1)) {
589 // The number of parts is not a power of 2. Split off and copy the tail.
590 assert(PartVT.isInteger() && ValueVT.isInteger() &&
591 "Do not know what to expand to!");
592 unsigned RoundParts = llvm::bit_floor(NumParts);
593 unsigned RoundBits = RoundParts * PartBits;
594 unsigned OddParts = NumParts - RoundParts;
595 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
596 DAG.getShiftAmountConstant(RoundBits, ValueVT, DL));
597
598 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V,
599 CallConv);
600
601 if (DAG.getDataLayout().isBigEndian())
602 // The odd parts were reversed by getCopyToParts - unreverse them.
603 std::reverse(Parts + RoundParts, Parts + NumParts);
604
605 NumParts = RoundParts;
606 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
607 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
608 }
609
610 // The number of parts is a power of 2. Repeatedly bisect the value using
611 // EXTRACT_ELEMENT.
612 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
614 ValueVT.getSizeInBits()),
615 Val);
616
617 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
618 for (unsigned i = 0; i < NumParts; i += StepSize) {
619 unsigned ThisBits = StepSize * PartBits / 2;
620 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
621 SDValue &Part0 = Parts[i];
622 SDValue &Part1 = Parts[i+StepSize/2];
623
624 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
625 ThisVT, Part0, DAG.getIntPtrConstant(1, DL));
626 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
627 ThisVT, Part0, DAG.getIntPtrConstant(0, DL));
628
629 if (ThisBits == PartBits && ThisVT != PartVT) {
630 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
631 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
632 }
633 }
634 }
635
636 if (DAG.getDataLayout().isBigEndian())
637 std::reverse(Parts, Parts + OrigNumParts);
638}
639
641 const SDLoc &DL, EVT PartVT) {
642 if (!PartVT.isVector())
643 return SDValue();
644
645 EVT ValueVT = Val.getValueType();
646 EVT PartEVT = PartVT.getVectorElementType();
647 EVT ValueEVT = ValueVT.getVectorElementType();
648 ElementCount PartNumElts = PartVT.getVectorElementCount();
649 ElementCount ValueNumElts = ValueVT.getVectorElementCount();
650
651 // We only support widening vectors with equivalent element types and
652 // fixed/scalable properties. If a target needs to widen a fixed-length type
653 // to a scalable one, it should be possible to use INSERT_SUBVECTOR below.
654 if (ElementCount::isKnownLE(PartNumElts, ValueNumElts) ||
655 PartNumElts.isScalable() != ValueNumElts.isScalable())
656 return SDValue();
657
658 // Have a try for bf16 because some targets share its ABI with fp16.
659 if (ValueEVT == MVT::bf16 && PartEVT == MVT::f16) {
661 "Cannot widen to illegal type");
662 Val = DAG.getNode(ISD::BITCAST, DL,
663 ValueVT.changeVectorElementType(MVT::f16), Val);
664 } else if (PartEVT != ValueEVT) {
665 return SDValue();
666 }
667
668 // Widening a scalable vector to another scalable vector is done by inserting
669 // the vector into a larger undef one.
670 if (PartNumElts.isScalable())
671 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT),
672 Val, DAG.getVectorIdxConstant(0, DL));
673
674 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
675 // undef elements.
677 DAG.ExtractVectorElements(Val, Ops);
678 SDValue EltUndef = DAG.getUNDEF(PartEVT);
679 Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef);
680
681 // FIXME: Use CONCAT for 2x -> 4x.
682 return DAG.getBuildVector(PartVT, DL, Ops);
683}
684
685/// getCopyToPartsVector - Create a series of nodes that contain the specified
686/// value split into legal parts.
687static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL,
688 SDValue Val, SDValue *Parts, unsigned NumParts,
689 MVT PartVT, const Value *V,
690 std::optional<CallingConv::ID> CallConv) {
691 EVT ValueVT = Val.getValueType();
692 assert(ValueVT.isVector() && "Not a vector");
693 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
694 const bool IsABIRegCopy = CallConv.has_value();
695
696 if (NumParts == 1) {
697 EVT PartEVT = PartVT;
698 if (PartEVT == ValueVT) {
699 // Nothing to do.
700 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
701 // Bitconvert vector->vector case.
702 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
703 } else if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, PartVT)) {
704 Val = Widened;
705 } else if (PartVT.isVector() &&
707 ValueVT.getVectorElementType()) &&
708 PartEVT.getVectorElementCount() ==
709 ValueVT.getVectorElementCount()) {
710
711 // Promoted vector extract
712 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
713 } else if (PartEVT.isVector() &&
714 PartEVT.getVectorElementType() !=
715 ValueVT.getVectorElementType() &&
716 TLI.getTypeAction(*DAG.getContext(), ValueVT) ==
717 TargetLowering::TypeWidenVector) {
718 // Combination of widening and promotion.
719 EVT WidenVT =
721 PartVT.getVectorElementCount());
722 SDValue Widened = widenVectorToPartType(DAG, Val, DL, WidenVT);
723 Val = DAG.getAnyExtOrTrunc(Widened, DL, PartVT);
724 } else {
725 // Don't extract an integer from a float vector. This can happen if the
726 // FP type gets softened to integer and then promoted. The promotion
727 // prevents it from being picked up by the earlier bitcast case.
728 if (ValueVT.getVectorElementCount().isScalar() &&
729 (!ValueVT.isFloatingPoint() || !PartVT.isInteger())) {
730 // If we reach this condition and PartVT is FP, this means that
731 // ValueVT is also FP and both have a different size, otherwise we
732 // would have bitcasted them. Producing an EXTRACT_VECTOR_ELT here
733 // would be invalid since that would mean the smaller FP type has to
734 // be extended to the larger one.
735 if (PartVT.isFloatingPoint()) {
736 Val = DAG.getBitcast(ValueVT.getScalarType(), Val);
737 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
738 } else
739 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val,
740 DAG.getVectorIdxConstant(0, DL));
741 } else {
742 uint64_t ValueSize = ValueVT.getFixedSizeInBits();
743 assert(PartVT.getFixedSizeInBits() > ValueSize &&
744 "lossy conversion of vector to scalar type");
745 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
746 Val = DAG.getBitcast(IntermediateType, Val);
747 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
748 }
749 }
750
751 assert(Val.getValueType() == PartVT && "Unexpected vector part value type");
752 Parts[0] = Val;
753 return;
754 }
755
756 // Handle a multi-element vector.
757 EVT IntermediateVT;
758 MVT RegisterVT;
759 unsigned NumIntermediates;
760 unsigned NumRegs;
761 if (IsABIRegCopy) {
763 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT, NumIntermediates,
764 RegisterVT);
765 } else {
766 NumRegs =
767 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
768 NumIntermediates, RegisterVT);
769 }
770
771 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
772 NumParts = NumRegs; // Silence a compiler warning.
773 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
774
775 assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() &&
776 "Mixing scalable and fixed vectors when copying in parts");
777
778 std::optional<ElementCount> DestEltCnt;
779
780 if (IntermediateVT.isVector())
781 DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates;
782 else
783 DestEltCnt = ElementCount::getFixed(NumIntermediates);
784
785 EVT BuiltVectorTy = EVT::getVectorVT(
786 *DAG.getContext(), IntermediateVT.getScalarType(), *DestEltCnt);
787
788 if (ValueVT == BuiltVectorTy) {
789 // Nothing to do.
790 } else if (ValueVT.getSizeInBits() == BuiltVectorTy.getSizeInBits()) {
791 // Bitconvert vector->vector case.
792 Val = DAG.getNode(ISD::BITCAST, DL, BuiltVectorTy, Val);
793 } else {
794 if (BuiltVectorTy.getVectorElementType().bitsGT(
795 ValueVT.getVectorElementType())) {
796 // Integer promotion.
797 ValueVT = EVT::getVectorVT(*DAG.getContext(),
798 BuiltVectorTy.getVectorElementType(),
799 ValueVT.getVectorElementCount());
800 Val = DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
801 }
802
803 if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy)) {
804 Val = Widened;
805 }
806 }
807
808 assert(Val.getValueType() == BuiltVectorTy && "Unexpected vector value type");
809
810 // Split the vector into intermediate operands.
811 SmallVector<SDValue, 8> Ops(NumIntermediates);
812 for (unsigned i = 0; i != NumIntermediates; ++i) {
813 if (IntermediateVT.isVector()) {
814 // This does something sensible for scalable vectors - see the
815 // definition of EXTRACT_SUBVECTOR for further details.
816 unsigned IntermediateNumElts = IntermediateVT.getVectorMinNumElements();
817 Ops[i] =
818 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val,
819 DAG.getVectorIdxConstant(i * IntermediateNumElts, DL));
820 } else {
821 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val,
822 DAG.getVectorIdxConstant(i, DL));
823 }
824 }
825
826 // Split the intermediate operands into legal parts.
827 if (NumParts == NumIntermediates) {
828 // If the register was not expanded, promote or copy the value,
829 // as appropriate.
830 for (unsigned i = 0; i != NumParts; ++i)
831 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V, CallConv);
832 } else if (NumParts > 0) {
833 // If the intermediate type was expanded, split each the value into
834 // legal parts.
835 assert(NumIntermediates != 0 && "division by zero");
836 assert(NumParts % NumIntermediates == 0 &&
837 "Must expand into a divisible number of parts!");
838 unsigned Factor = NumParts / NumIntermediates;
839 for (unsigned i = 0; i != NumIntermediates; ++i)
840 getCopyToParts(DAG, DL, Ops[i], &Parts[i * Factor], Factor, PartVT, V,
841 CallConv);
842 }
843}
844
846 ArrayRef<uint32_t> AllowedBundles) {
847 if (I.hasOperandBundlesOtherThan(AllowedBundles)) {
848 ListSeparator LS;
849 std::string Error;
851 for (unsigned i = 0, e = I.getNumOperandBundles(); i != e; ++i) {
852 OperandBundleUse U = I.getOperandBundleAt(i);
853 if (!is_contained(AllowedBundles, U.getTagID()))
854 OS << LS << U.getTagName();
855 }
857 Twine("cannot lower ", Name)
858 .concat(Twine(" with arbitrary operand bundles: ", Error)));
859 }
860}
861
863 EVT valuevt, std::optional<CallingConv::ID> CC)
864 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs),
865 RegCount(1, regs.size()), CallConv(CC) {}
866
868 const DataLayout &DL, Register Reg, Type *Ty,
869 std::optional<CallingConv::ID> CC) {
870 ComputeValueVTs(TLI, DL, Ty, ValueVTs);
871
872 CallConv = CC;
873
874 for (EVT ValueVT : ValueVTs) {
875 unsigned NumRegs =
877 ? TLI.getNumRegistersForCallingConv(Context, *CC, ValueVT)
878 : TLI.getNumRegisters(Context, ValueVT);
879 MVT RegisterVT =
881 ? TLI.getRegisterTypeForCallingConv(Context, *CC, ValueVT)
882 : TLI.getRegisterType(Context, ValueVT);
883 for (unsigned i = 0; i != NumRegs; ++i)
884 Regs.push_back(Reg + i);
885 RegVTs.push_back(RegisterVT);
886 RegCount.push_back(NumRegs);
887 Reg = Reg.id() + NumRegs;
888 }
889}
890
892 FunctionLoweringInfo &FuncInfo,
893 const SDLoc &dl, SDValue &Chain,
894 SDValue *Glue, const Value *V) const {
895 // A Value with type {} or [0 x %t] needs no registers.
896 if (ValueVTs.empty())
897 return SDValue();
898
899 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
900
901 // Assemble the legal parts into the final values.
902 SmallVector<SDValue, 4> Values(ValueVTs.size());
904 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
905 // Copy the legal parts from the registers.
906 EVT ValueVT = ValueVTs[Value];
907 unsigned NumRegs = RegCount[Value];
908 MVT RegisterVT = isABIMangled()
910 *DAG.getContext(), *CallConv, RegVTs[Value])
911 : RegVTs[Value];
912
913 Parts.resize(NumRegs);
914 for (unsigned i = 0; i != NumRegs; ++i) {
915 SDValue P;
916 if (!Glue) {
917 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
918 } else {
919 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Glue);
920 *Glue = P.getValue(2);
921 }
922
923 Chain = P.getValue(1);
924 Parts[i] = P;
925
926 // If the source register was virtual and if we know something about it,
927 // add an assert node.
928 if (!Regs[Part + i].isVirtual() || !RegisterVT.isInteger())
929 continue;
930
932 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
933 if (!LOI)
934 continue;
935
936 unsigned RegSize = RegisterVT.getScalarSizeInBits();
937 unsigned NumSignBits = LOI->NumSignBits;
938 unsigned NumZeroBits = LOI->Known.countMinLeadingZeros();
939
940 if (NumZeroBits == RegSize) {
941 // The current value is a zero.
942 // Explicitly express that as it would be easier for
943 // optimizations to kick in.
944 Parts[i] = DAG.getConstant(0, dl, RegisterVT);
945 continue;
946 }
947
948 // FIXME: We capture more information than the dag can represent. For
949 // now, just use the tightest assertzext/assertsext possible.
950 bool isSExt;
951 EVT FromVT(MVT::Other);
952 if (NumZeroBits) {
953 FromVT = EVT::getIntegerVT(*DAG.getContext(), RegSize - NumZeroBits);
954 isSExt = false;
955 } else if (NumSignBits > 1) {
956 FromVT =
957 EVT::getIntegerVT(*DAG.getContext(), RegSize - NumSignBits + 1);
958 isSExt = true;
959 } else {
960 continue;
961 }
962 // Add an assertion node.
963 assert(FromVT != MVT::Other);
964 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
965 RegisterVT, P, DAG.getValueType(FromVT));
966 }
967
968 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), NumRegs,
969 RegisterVT, ValueVT, V, Chain, CallConv);
970 Part += NumRegs;
971 Parts.clear();
972 }
973
974 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values);
975}
976
978 const SDLoc &dl, SDValue &Chain, SDValue *Glue,
979 const Value *V,
980 ISD::NodeType PreferredExtendType) const {
981 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
982 ISD::NodeType ExtendKind = PreferredExtendType;
983
984 // Get the list of the values's legal parts.
985 unsigned NumRegs = Regs.size();
986 SmallVector<SDValue, 8> Parts(NumRegs);
987 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
988 unsigned NumParts = RegCount[Value];
989
990 MVT RegisterVT = isABIMangled()
992 *DAG.getContext(), *CallConv, RegVTs[Value])
993 : RegVTs[Value];
994
995 if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT))
996 ExtendKind = ISD::ZERO_EXTEND;
997
998 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
999 NumParts, RegisterVT, V, CallConv, ExtendKind);
1000 Part += NumParts;
1001 }
1002
1003 // Copy the parts into the registers.
1004 SmallVector<SDValue, 8> Chains(NumRegs);
1005 for (unsigned i = 0; i != NumRegs; ++i) {
1006 SDValue Part;
1007 if (!Glue) {
1008 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
1009 } else {
1010 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue);
1011 *Glue = Part.getValue(1);
1012 }
1013
1014 Chains[i] = Part.getValue(0);
1015 }
1016
1017 if (NumRegs == 1 || Glue)
1018 // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is
1019 // flagged to it. That is the CopyToReg nodes and the user are considered
1020 // a single scheduling unit. If we create a TokenFactor and return it as
1021 // chain, then the TokenFactor is both a predecessor (operand) of the
1022 // user as well as a successor (the TF operands are flagged to the user).
1023 // c1, f1 = CopyToReg
1024 // c2, f2 = CopyToReg
1025 // c3 = TokenFactor c1, c2
1026 // ...
1027 // = op c3, ..., f2
1028 Chain = Chains[NumRegs-1];
1029 else
1030 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1031}
1032
1034 unsigned MatchingIdx, const SDLoc &dl,
1035 SelectionDAG &DAG,
1036 std::vector<SDValue> &Ops) const {
1037 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1038
1039 InlineAsm::Flag Flag(Code, Regs.size());
1040 if (HasMatching)
1041 Flag.setMatchingOp(MatchingIdx);
1042 else if (!Regs.empty() && Regs.front().isVirtual()) {
1043 // Put the register class of the virtual registers in the flag word. That
1044 // way, later passes can recompute register class constraints for inline
1045 // assembly as well as normal instructions.
1046 // Don't do this for tied operands that can use the regclass information
1047 // from the def.
1049 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
1050 Flag.setRegClass(RC->getID());
1051 }
1052
1053 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
1054 Ops.push_back(Res);
1055
1056 if (Code == InlineAsm::Kind::Clobber) {
1057 // Clobbers should always have a 1:1 mapping with registers, and may
1058 // reference registers that have illegal (e.g. vector) types. Hence, we
1059 // shouldn't try to apply any sort of splitting logic to them.
1060 assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
1061 "No 1:1 mapping from clobbers to regs?");
1063 (void)SP;
1064 for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
1065 Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
1066 assert(
1067 (Regs[I] != SP ||
1069 "If we clobbered the stack pointer, MFI should know about it.");
1070 }
1071 return;
1072 }
1073
1074 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1075 MVT RegisterVT = RegVTs[Value];
1076 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1077 RegisterVT);
1078 for (unsigned i = 0; i != NumRegs; ++i) {
1079 assert(Reg < Regs.size() && "Mismatch in # registers expected");
1080 Register TheReg = Regs[Reg++];
1081 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1082 }
1083 }
1084}
1085
1089 unsigned I = 0;
1090 for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1091 unsigned RegCount = std::get<0>(CountAndVT);
1092 MVT RegisterVT = std::get<1>(CountAndVT);
1093 TypeSize RegisterSize = RegisterVT.getSizeInBits();
1094 for (unsigned E = I + RegCount; I != E; ++I)
1095 OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1096 }
1097 return OutVec;
1098}
1099
1101 AssumptionCache *ac,
1102 const TargetLibraryInfo *li) {
1103 BatchAA = aa;
1104 AC = ac;
1105 GFI = gfi;
1106 LibInfo = li;
1108 LPadToCallSiteMap.clear();
1110 AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
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;
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
1163 return updateRoot(PendingLoads);
1164}
1165
1167 // Chain up all pending constrained intrinsics together with all
1168 // pending loads, by simply appending them to PendingLoads and
1169 // then calling getMemoryRoot().
1170 PendingLoads.reserve(PendingLoads.size() +
1171 PendingConstrainedFP.size() +
1172 PendingConstrainedFPStrict.size());
1173 PendingLoads.append(PendingConstrainedFP.begin(),
1174 PendingConstrainedFP.end());
1175 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1176 PendingConstrainedFPStrict.end());
1177 PendingConstrainedFP.clear();
1178 PendingConstrainedFPStrict.clear();
1179 return getMemoryRoot();
1180}
1181
1183 // We need to emit pending fpexcept.strict constrained intrinsics,
1184 // so append them to the PendingExports list.
1185 PendingExports.append(PendingConstrainedFPStrict.begin(),
1186 PendingConstrainedFPStrict.end());
1187 PendingConstrainedFPStrict.clear();
1188 return updateRoot(PendingExports);
1189}
1190
1192 DILocalVariable *Variable,
1194 DebugLoc DL) {
1195 assert(Variable && "Missing variable");
1196
1197 // Check if address has undef value.
1198 if (!Address || isa<UndefValue>(Address) ||
1199 (Address->use_empty() && !isa<Argument>(Address))) {
1200 LLVM_DEBUG(
1201 dbgs()
1202 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1203 return;
1204 }
1205
1206 bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1207
1208 SDValue &N = NodeMap[Address];
1209 if (!N.getNode() && isa<Argument>(Address))
1210 // Check unused arguments map.
1211 N = UnusedArgNodeMap[Address];
1212 SDDbgValue *SDV;
1213 if (N.getNode()) {
1214 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1215 Address = BCI->getOperand(0);
1216 // Parameters are handled specially.
1217 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1218 if (IsParameter && FINode) {
1219 // Byval parameter. We have a frame index at this point.
1220 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1221 /*IsIndirect*/ true, DL, SDNodeOrder);
1222 } else if (isa<Argument>(Address)) {
1223 // Address is an argument, so try to emit its dbg value using
1224 // virtual register info from the FuncInfo.ValueMap.
1225 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1226 FuncArgumentDbgValueKind::Declare, N);
1227 return;
1228 } else {
1229 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1230 true, DL, SDNodeOrder);
1231 }
1232 DAG.AddDbgValue(SDV, IsParameter);
1233 } else {
1234 // If Address is an argument then try to emit its dbg value using
1235 // virtual register info from the FuncInfo.ValueMap.
1236 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1237 FuncArgumentDbgValueKind::Declare, N)) {
1238 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1239 << " (could not emit func-arg dbg_value)\n");
1240 }
1241 }
1242}
1243
1245 // Add SDDbgValue nodes for any var locs here. Do so before updating
1246 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1247 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1248 // Add SDDbgValue nodes for any var locs here. Do so before updating
1249 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1250 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1251 It != End; ++It) {
1252 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1253 dropDanglingDebugInfo(Var, It->Expr);
1254 if (It->Values.isKillLocation(It->Expr)) {
1255 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1256 continue;
1257 }
1258 SmallVector<Value *> Values(It->Values.location_ops());
1259 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1260 It->Values.hasArgList())) {
1261 SmallVector<Value *, 4> Vals(It->Values.location_ops());
1263 FnVarLocs->getDILocalVariable(It->VariableID),
1264 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1265 }
1266 }
1267 }
1268
1269 // We must skip DbgVariableRecords if they've already been processed above as
1270 // we have just emitted the debug values resulting from assignment tracking
1271 // analysis, making any existing DbgVariableRecords redundant (and probably
1272 // less correct). We still need to process DbgLabelRecords. This does sink
1273 // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1274 // be important as it does so deterministcally and ordering between
1275 // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1276 // printing).
1277 bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1278 // Is there is any debug-info attached to this instruction, in the form of
1279 // DbgRecord non-instruction debug-info records.
1280 for (DbgRecord &DR : I.getDbgRecordRange()) {
1281 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1282 assert(DLR->getLabel() && "Missing label");
1283 SDDbgLabel *SDV =
1284 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1285 DAG.AddDbgLabel(SDV);
1286 continue;
1287 }
1288
1289 if (SkipDbgVariableRecords)
1290 continue;
1291 DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
1292 DILocalVariable *Variable = DVR.getVariable();
1295
1297 if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1298 continue;
1299 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1300 << "\n");
1302 DVR.getDebugLoc());
1303 continue;
1304 }
1305
1306 // A DbgVariableRecord with no locations is a kill location.
1308 if (Values.empty()) {
1310 SDNodeOrder);
1311 continue;
1312 }
1313
1314 // A DbgVariableRecord with an undef or absent location is also a kill
1315 // location.
1316 if (llvm::any_of(Values,
1317 [](Value *V) { return !V || isa<UndefValue>(V); })) {
1319 SDNodeOrder);
1320 continue;
1321 }
1322
1323 bool IsVariadic = DVR.hasArgList();
1324 if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1325 SDNodeOrder, IsVariadic)) {
1326 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1327 DVR.getDebugLoc(), SDNodeOrder);
1328 }
1329 }
1330}
1331
1333 visitDbgInfo(I);
1334
1335 // Set up outgoing PHI node register values before emitting the terminator.
1336 if (I.isTerminator()) {
1337 HandlePHINodesInSuccessorBlocks(I.getParent());
1338 }
1339
1340 ++SDNodeOrder;
1341 CurInst = &I;
1342
1343 // Set inserted listener only if required.
1344 bool NodeInserted = false;
1345 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1346 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1347 MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1348 if (PCSectionsMD || MMRA) {
1349 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1350 DAG, [&](SDNode *) { NodeInserted = true; });
1351 }
1352
1353 visit(I.getOpcode(), I);
1354
1355 if (!I.isTerminator() && !HasTailCall &&
1356 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1358
1359 // Handle metadata.
1360 if (PCSectionsMD || MMRA) {
1361 auto It = NodeMap.find(&I);
1362 if (It != NodeMap.end()) {
1363 if (PCSectionsMD)
1364 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1365 if (MMRA)
1366 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1367 } else if (NodeInserted) {
1368 // This should not happen; if it does, don't let it go unnoticed so we can
1369 // fix it. Relevant visit*() function is probably missing a setValue().
1370 errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1371 << I.getModule()->getName() << "]\n";
1372 LLVM_DEBUG(I.dump());
1373 assert(false);
1374 }
1375 }
1376
1377 CurInst = nullptr;
1378}
1379
1380void SelectionDAGBuilder::visitPHI(const PHINode &) {
1381 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1382}
1383
1384void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1385 // Note: this doesn't use InstVisitor, because it has to work with
1386 // ConstantExpr's in addition to instructions.
1387 switch (Opcode) {
1388 default: llvm_unreachable("Unknown instruction type encountered!");
1389 // Build the switch statement using the Instruction.def file.
1390#define HANDLE_INST(NUM, OPCODE, CLASS) \
1391 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1392#include "llvm/IR/Instruction.def"
1393 }
1394}
1395
1397 DILocalVariable *Variable,
1398 DebugLoc DL, unsigned Order,
1401 // For variadic dbg_values we will now insert poison.
1402 // FIXME: We can potentially recover these!
1404 for (const Value *V : Values) {
1405 auto *Poison = PoisonValue::get(V->getType());
1407 }
1408 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1409 /*IsIndirect=*/false, DL, Order,
1410 /*IsVariadic=*/true);
1411 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1412 return true;
1413}
1414
1416 DILocalVariable *Var,
1417 DIExpression *Expr,
1418 bool IsVariadic, DebugLoc DL,
1419 unsigned Order) {
1420 if (IsVariadic) {
1421 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1422 return;
1423 }
1424 // TODO: Dangling debug info will eventually either be resolved or produce
1425 // a poison DBG_VALUE. However in the resolution case, a gap may appear
1426 // between the original dbg.value location and its resolved DBG_VALUE,
1427 // which we should ideally fill with an extra poison DBG_VALUE.
1428 assert(Values.size() == 1);
1429 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1430}
1431
1433 const DIExpression *Expr) {
1434 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1435 DIVariable *DanglingVariable = DDI.getVariable();
1436 DIExpression *DanglingExpr = DDI.getExpression();
1437 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1438 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1439 << printDDI(nullptr, DDI) << "\n");
1440 return true;
1441 }
1442 return false;
1443 };
1444
1445 for (auto &DDIMI : DanglingDebugInfoMap) {
1446 DanglingDebugInfoVector &DDIV = DDIMI.second;
1447
1448 // If debug info is to be dropped, run it through final checks to see
1449 // whether it can be salvaged.
1450 for (auto &DDI : DDIV)
1451 if (isMatchingDbgValue(DDI))
1452 salvageUnresolvedDbgValue(DDIMI.first, DDI);
1453
1454 erase_if(DDIV, isMatchingDbgValue);
1455 }
1456}
1457
1458// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1459// generate the debug data structures now that we've seen its definition.
1461 SDValue Val) {
1462 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1463 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1464 return;
1465
1466 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1467 for (auto &DDI : DDIV) {
1468 DebugLoc DL = DDI.getDebugLoc();
1469 unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1470 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1471 DILocalVariable *Variable = DDI.getVariable();
1472 DIExpression *Expr = DDI.getExpression();
1474 "Expected inlined-at fields to agree");
1475 SDDbgValue *SDV;
1476 if (Val.getNode()) {
1477 // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1478 // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1479 // we couldn't resolve it directly when examining the DbgValue intrinsic
1480 // in the first place we should not be more successful here). Unless we
1481 // have some test case that prove this to be correct we should avoid
1482 // calling EmitFuncArgumentDbgValue here.
1483 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1484 FuncArgumentDbgValueKind::Value, Val)) {
1485 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1486 << printDDI(V, DDI) << "\n");
1487 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump());
1488 // Increase the SDNodeOrder for the DbgValue here to make sure it is
1489 // inserted after the definition of Val when emitting the instructions
1490 // after ISel. An alternative could be to teach
1491 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1492 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1493 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1494 << ValSDNodeOrder << "\n");
1495 SDV = getDbgValue(Val, Variable, Expr, DL,
1496 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1497 DAG.AddDbgValue(SDV, false);
1498 } else
1499 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1500 << printDDI(V, DDI)
1501 << " in EmitFuncArgumentDbgValue\n");
1502 } else {
1503 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1504 << "\n");
1505 auto Poison = PoisonValue::get(V->getType());
1506 auto SDV =
1507 DAG.getConstantDbgValue(Variable, Expr, Poison, DL, DbgSDNodeOrder);
1508 DAG.AddDbgValue(SDV, false);
1509 }
1510 }
1511 DDIV.clear();
1512}
1513
1515 DanglingDebugInfo &DDI) {
1516 // TODO: For the variadic implementation, instead of only checking the fail
1517 // state of `handleDebugValue`, we need know specifically which values were
1518 // invalid, so that we attempt to salvage only those values when processing
1519 // a DIArgList.
1520 const Value *OrigV = V;
1521 DILocalVariable *Var = DDI.getVariable();
1522 DIExpression *Expr = DDI.getExpression();
1523 DebugLoc DL = DDI.getDebugLoc();
1524 unsigned SDOrder = DDI.getSDNodeOrder();
1525
1526 // Currently we consider only dbg.value intrinsics -- we tell the salvager
1527 // that DW_OP_stack_value is desired.
1528 bool StackValue = true;
1529
1530 // Can this Value can be encoded without any further work?
1531 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1532 return;
1533
1534 // Attempt to salvage back through as many instructions as possible. Bail if
1535 // a non-instruction is seen, such as a constant expression or global
1536 // variable. FIXME: Further work could recover those too.
1537 while (isa<Instruction>(V)) {
1538 const Instruction &VAsInst = *cast<const Instruction>(V);
1539 // Temporary "0", awaiting real implementation.
1541 SmallVector<Value *, 4> AdditionalValues;
1542 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1543 Expr->getNumLocationOperands(), Ops,
1544 AdditionalValues);
1545 // If we cannot salvage any further, and haven't yet found a suitable debug
1546 // expression, bail out.
1547 if (!V)
1548 break;
1549
1550 // TODO: If AdditionalValues isn't empty, then the salvage can only be
1551 // represented with a DBG_VALUE_LIST, so we give up. When we have support
1552 // here for variadic dbg_values, remove that condition.
1553 if (!AdditionalValues.empty())
1554 break;
1555
1556 // New value and expr now represent this debuginfo.
1557 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1558
1559 // Some kind of simplification occurred: check whether the operand of the
1560 // salvaged debug expression can be encoded in this DAG.
1561 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1562 LLVM_DEBUG(
1563 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n"
1564 << *OrigV << "\nBy stripping back to:\n " << *V << "\n");
1565 return;
1566 }
1567 }
1568
1569 // This was the final opportunity to salvage this debug information, and it
1570 // couldn't be done. Place a poison DBG_VALUE at this location to terminate
1571 // any earlier variable location.
1572 assert(OrigV && "V shouldn't be null");
1573 auto *Poison = PoisonValue::get(OrigV->getType());
1574 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Poison, DL, SDNodeOrder);
1575 DAG.AddDbgValue(SDV, false);
1576 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n "
1577 << printDDI(OrigV, DDI) << "\n");
1578}
1579
1581 DIExpression *Expr,
1582 DebugLoc DbgLoc,
1583 unsigned Order) {
1587 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1588 /*IsVariadic*/ false);
1589}
1590
1592 DILocalVariable *Var,
1593 DIExpression *Expr, DebugLoc DbgLoc,
1594 unsigned Order, bool IsVariadic) {
1595 if (Values.empty())
1596 return true;
1597
1598 // Filter EntryValue locations out early.
1599 if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1600 return true;
1601
1602 SmallVector<SDDbgOperand> LocationOps;
1603 SmallVector<SDNode *> Dependencies;
1604 for (const Value *V : Values) {
1605 // Constant value.
1606 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V) ||
1607 isa<ConstantPointerNull>(V)) {
1608 LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1609 continue;
1610 }
1611
1612 // Look through IntToPtr constants.
1613 if (auto *CE = dyn_cast<ConstantExpr>(V))
1614 if (CE->getOpcode() == Instruction::IntToPtr) {
1615 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1616 continue;
1617 }
1618
1619 // If the Value is a frame index, we can create a FrameIndex debug value
1620 // without relying on the DAG at all.
1621 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1622 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1623 if (SI != FuncInfo.StaticAllocaMap.end()) {
1624 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1625 continue;
1626 }
1627 }
1628
1629 // Do not use getValue() in here; we don't want to generate code at
1630 // this point if it hasn't been done yet.
1631 SDValue N = NodeMap[V];
1632 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1633 N = UnusedArgNodeMap[V];
1634
1635 if (N.getNode()) {
1636 // Only emit func arg dbg value for non-variadic dbg.values for now.
1637 if (!IsVariadic &&
1638 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1639 FuncArgumentDbgValueKind::Value, N))
1640 return true;
1641 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1642 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1643 // describe stack slot locations.
1644 //
1645 // Consider "int x = 0; int *px = &x;". There are two kinds of
1646 // interesting debug values here after optimization:
1647 //
1648 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
1649 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1650 //
1651 // Both describe the direct values of their associated variables.
1652 Dependencies.push_back(N.getNode());
1653 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1654 continue;
1655 }
1656 LocationOps.emplace_back(
1657 SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1658 continue;
1659 }
1660
1662 // Special rules apply for the first dbg.values of parameter variables in a
1663 // function. Identify them by the fact they reference Argument Values, that
1664 // they're parameters, and they are parameters of the current function. We
1665 // need to let them dangle until they get an SDNode.
1666 bool IsParamOfFunc =
1667 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1668 if (IsParamOfFunc)
1669 return false;
1670
1671 // The value is not used in this block yet (or it would have an SDNode).
1672 // We still want the value to appear for the user if possible -- if it has
1673 // an associated VReg, we can refer to that instead.
1674 auto VMI = FuncInfo.ValueMap.find(V);
1675 if (VMI != FuncInfo.ValueMap.end()) {
1676 Register Reg = VMI->second;
1677 // If this is a PHI node, it may be split up into several MI PHI nodes
1678 // (in FunctionLoweringInfo::set).
1679 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1680 V->getType(), std::nullopt);
1681 if (RFV.occupiesMultipleRegs()) {
1682 // FIXME: We could potentially support variadic dbg_values here.
1683 if (IsVariadic)
1684 return false;
1685 unsigned Offset = 0;
1686 unsigned BitsToDescribe = 0;
1687 if (auto VarSize = Var->getSizeInBits())
1688 BitsToDescribe = *VarSize;
1689 if (auto Fragment = Expr->getFragmentInfo())
1690 BitsToDescribe = Fragment->SizeInBits;
1691 for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1692 // Bail out if all bits are described already.
1693 if (Offset >= BitsToDescribe)
1694 break;
1695 // TODO: handle scalable vectors.
1696 unsigned RegisterSize = RegAndSize.second;
1697 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1698 ? BitsToDescribe - Offset
1699 : RegisterSize;
1700 auto FragmentExpr = DIExpression::createFragmentExpression(
1701 Expr, Offset, FragmentSize);
1702 if (!FragmentExpr)
1703 continue;
1705 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1706 DAG.AddDbgValue(SDV, false);
1707 Offset += RegisterSize;
1708 }
1709 return true;
1710 }
1711 // We can use simple vreg locations for variadic dbg_values as well.
1712 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1713 continue;
1714 }
1715 // We failed to create a SDDbgOperand for V.
1716 return false;
1717 }
1718
1719 // We have created a SDDbgOperand for each Value in Values.
1720 assert(!LocationOps.empty());
1721 SDDbgValue *SDV =
1722 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1723 /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1724 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1725 return true;
1726}
1727
1729 // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1730 for (auto &Pair : DanglingDebugInfoMap)
1731 for (auto &DDI : Pair.second)
1732 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1734}
1735
1736/// getCopyFromRegs - If there was virtual register allocated for the value V
1737/// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1740 SDValue Result;
1741
1742 if (It != FuncInfo.ValueMap.end()) {
1743 Register InReg = It->second;
1744
1746 DAG.getDataLayout(), InReg, Ty,
1747 std::nullopt); // This is not an ABI copy.
1748 SDValue Chain = DAG.getEntryNode();
1749 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1750 V);
1751 resolveDanglingDebugInfo(V, Result);
1752 }
1753
1754 return Result;
1755}
1756
1757/// getValue - Return an SDValue for the given Value.
1759 // If we already have an SDValue for this value, use it. It's important
1760 // to do this first, so that we don't create a CopyFromReg if we already
1761 // have a regular SDValue.
1762 SDValue &N = NodeMap[V];
1763 if (N.getNode()) return N;
1764
1765 // If there's a virtual register allocated and initialized for this
1766 // value, use it.
1767 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1768 return copyFromReg;
1769
1770 // Otherwise create a new SDValue and remember it.
1771 SDValue Val = getValueImpl(V);
1772 NodeMap[V] = Val;
1774 return Val;
1775}
1776
1777/// getNonRegisterValue - Return an SDValue for the given Value, but
1778/// don't look in FuncInfo.ValueMap for a virtual register.
1780 // If we already have an SDValue for this value, use it.
1781 SDValue &N = NodeMap[V];
1782 if (N.getNode()) {
1783 if (isIntOrFPConstant(N)) {
1784 // Remove the debug location from the node as the node is about to be used
1785 // in a location which may differ from the original debug location. This
1786 // is relevant to Constant and ConstantFP nodes because they can appear
1787 // as constant expressions inside PHI nodes.
1788 N->setDebugLoc(DebugLoc());
1789 }
1790 return N;
1791 }
1792
1793 // Otherwise create a new SDValue and remember it.
1794 SDValue Val = getValueImpl(V);
1795 NodeMap[V] = Val;
1797 return Val;
1798}
1799
1800/// getValueImpl - Helper function for getValue and getNonRegisterValue.
1801/// Create an SDValue for the given value.
1804
1805 if (const Constant *C = dyn_cast<Constant>(V)) {
1806 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1807
1808 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1809 SDLoc DL = getCurSDLoc();
1810
1811 // DAG.getConstant() may attempt to legalise the vector constant which can
1812 // significantly change the combines applied to the DAG. To reduce the
1813 // divergence when enabling ConstantInt based vectors we try to construct
1814 // the DAG in the same way as shufflevector based splats. TODO: The
1815 // divergence sometimes leads to better optimisations. Ideally we should
1816 // prevent DAG.getConstant() from legalising too early but there are some
1817 // degradations preventing this.
1818 if (VT.isScalableVector())
1819 return DAG.getNode(
1820 ISD::SPLAT_VECTOR, DL, VT,
1821 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1822 if (VT.isFixedLengthVector())
1823 return DAG.getSplatBuildVector(
1824 VT, DL,
1825 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1826 return DAG.getConstant(*CI, DL, VT);
1827 }
1828
1829 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1830 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1831
1832 if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1834 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1835 getValue(CPA->getAddrDiscriminator()),
1836 getValue(CPA->getDiscriminator()));
1837 }
1838
1839 if (isa<ConstantPointerNull>(C))
1840 return DAG.getConstant(0, getCurSDLoc(), VT);
1841
1842 if (match(C, m_VScale()))
1843 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1844
1845 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1846 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1847
1848 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1849 return isa<PoisonValue>(C) ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
1850
1851 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1852 visit(CE->getOpcode(), *CE);
1853 SDValue N1 = NodeMap[V];
1854 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1855 return N1;
1856 }
1857
1858 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1859 SmallVector<SDValue, 4> Constants;
1860 for (const Use &U : C->operands()) {
1861 SDNode *Val = getValue(U).getNode();
1862 // If the operand is an empty aggregate, there are no values.
1863 if (!Val) continue;
1864 // Add each leaf value from the operand to the Constants list
1865 // to form a flattened list of all the values.
1866 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1867 Constants.push_back(SDValue(Val, i));
1868 }
1869
1870 return DAG.getMergeValues(Constants, getCurSDLoc());
1871 }
1872
1873 if (const ConstantDataSequential *CDS =
1874 dyn_cast<ConstantDataSequential>(C)) {
1876 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1877 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1878 // Add each leaf value from the operand to the Constants list
1879 // to form a flattened list of all the values.
1880 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1881 Ops.push_back(SDValue(Val, i));
1882 }
1883
1884 if (isa<ArrayType>(CDS->getType()))
1885 return DAG.getMergeValues(Ops, getCurSDLoc());
1886 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1887 }
1888
1889 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1890 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1891 "Unknown struct or array constant!");
1892
1893 SmallVector<EVT, 4> ValueVTs;
1894 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1895 unsigned NumElts = ValueVTs.size();
1896 if (NumElts == 0)
1897 return SDValue(); // empty struct
1898 SmallVector<SDValue, 4> Constants(NumElts);
1899 for (unsigned i = 0; i != NumElts; ++i) {
1900 EVT EltVT = ValueVTs[i];
1901 if (isa<UndefValue>(C))
1902 Constants[i] = DAG.getUNDEF(EltVT);
1903 else if (EltVT.isFloatingPoint())
1904 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1905 else
1906 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1907 }
1908
1909 return DAG.getMergeValues(Constants, getCurSDLoc());
1910 }
1911
1912 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1913 return DAG.getBlockAddress(BA, VT);
1914
1915 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1916 return getValue(Equiv->getGlobalValue());
1917
1918 if (const auto *NC = dyn_cast<NoCFIValue>(C))
1919 return getValue(NC->getGlobalValue());
1920
1921 if (VT == MVT::aarch64svcount) {
1922 assert(C->isNullValue() && "Can only zero this target type!");
1923 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1924 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1925 }
1926
1927 if (VT.isRISCVVectorTuple()) {
1928 assert(C->isNullValue() && "Can only zero this target type!");
1929 return DAG.getNode(
1931 DAG.getNode(
1933 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1934 VT.getSizeInBits().getKnownMinValue() / 8, true),
1936 }
1937
1938 VectorType *VecTy = cast<VectorType>(V->getType());
1939
1940 // Now that we know the number and type of the elements, get that number of
1941 // elements into the Ops array based on what kind of constant it is.
1942 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1944 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1945 for (unsigned i = 0; i != NumElements; ++i)
1946 Ops.push_back(getValue(CV->getOperand(i)));
1947
1948 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1949 }
1950
1951 if (isa<ConstantAggregateZero>(C)) {
1952 EVT EltVT =
1954
1955 SDValue Op;
1956 if (EltVT.isFloatingPoint())
1957 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1958 else
1959 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1960
1961 return DAG.getSplat(VT, getCurSDLoc(), Op);
1962 }
1963
1964 llvm_unreachable("Unknown vector constant");
1965 }
1966
1967 // If this is a static alloca, generate it as the frameindex instead of
1968 // computation.
1969 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1971 FuncInfo.StaticAllocaMap.find(AI);
1972 if (SI != FuncInfo.StaticAllocaMap.end())
1973 return DAG.getFrameIndex(
1974 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
1975 }
1976
1977 // If this is an instruction which fast-isel has deferred, select it now.
1978 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
1980
1981 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
1982 Inst->getType(), std::nullopt);
1983 SDValue Chain = DAG.getEntryNode();
1984 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
1985 }
1986
1987 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
1988 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
1989
1990 if (const auto *BB = dyn_cast<BasicBlock>(V))
1991 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
1992
1993 llvm_unreachable("Can't get register for value!");
1994}
1995
1996void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
1998 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
1999 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
2000 bool IsSEH = isAsynchronousEHPersonality(Pers);
2001 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2002 if (IsSEH) {
2003 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2004 CatchPadMBB->setIsEHContTarget(true);
2006 } else
2007 CatchPadMBB->setIsEHScopeEntry();
2008 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2009 if (IsMSVCCXX || IsCoreCLR)
2010 CatchPadMBB->setIsEHFuncletEntry();
2011}
2012
2013void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2014 // Update machine-CFG edge.
2015 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2016 FuncInfo.MBB->addSuccessor(TargetMBB);
2017
2019 bool IsSEH = isAsynchronousEHPersonality(Pers);
2020 if (IsSEH) {
2021 // If this is not a fall-through branch or optimizations are switched off,
2022 // emit the branch.
2023 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2025 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2026 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2027 return;
2028 }
2029
2030 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2031 TargetMBB->setIsEHContTarget(true);
2033
2034 // Figure out the funclet membership for the catchret's successor.
2035 // This will be used by the FuncletLayout pass to determine how to order the
2036 // BB's.
2037 // A 'catchret' returns to the outer scope's color.
2038 Value *ParentPad = I.getCatchSwitchParentPad();
2039 const BasicBlock *SuccessorColor;
2040 if (isa<ConstantTokenNone>(ParentPad))
2041 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2042 else
2043 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2044 assert(SuccessorColor && "No parent funclet for catchret!");
2045 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2046 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2047
2048 // Create the terminator node.
2050 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2051 DAG.getBasicBlock(SuccessorColorMBB));
2052 DAG.setRoot(Ret);
2053}
2054
2055void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2056 // Don't emit any special code for the cleanuppad instruction. It just marks
2057 // the start of an EH scope/funclet.
2060 if (Pers != EHPersonality::Wasm_CXX) {
2063 }
2064}
2065
2066/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2067/// many places it could ultimately go. In the IR, we have a single unwind
2068/// destination, but in the machine CFG, we enumerate all the possible blocks.
2069/// This function skips over imaginary basic blocks that hold catchswitch
2070/// instructions, and finds all the "real" machine
2071/// basic block destinations. As those destinations may not be successors of
2072/// EHPadBB, here we also calculate the edge probability to those destinations.
2073/// The passed-in Prob is the edge probability to EHPadBB.
2075 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2076 BranchProbability Prob,
2077 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2078 &UnwindDests) {
2079 EHPersonality Personality =
2081 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2082 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2083 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2084 bool IsSEH = isAsynchronousEHPersonality(Personality);
2085
2086 while (EHPadBB) {
2088 BasicBlock *NewEHPadBB = nullptr;
2089 if (isa<LandingPadInst>(Pad)) {
2090 // Stop on landingpads. They are not funclets.
2091 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2092 break;
2093 } else if (isa<CleanupPadInst>(Pad)) {
2094 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2095 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2096 // which always catches an exception.
2097 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2098 UnwindDests.back().first->setIsEHScopeEntry();
2099 // In Wasm, EH scopes are not funclets
2100 if (!IsWasmCXX)
2101 UnwindDests.back().first->setIsEHFuncletEntry();
2102 break;
2103 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2104 // Add the catchpad handlers to the possible destinations.
2105 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2106 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2107 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2108 if (IsMSVCCXX || IsCoreCLR)
2109 UnwindDests.back().first->setIsEHFuncletEntry();
2110 if (!IsSEH)
2111 UnwindDests.back().first->setIsEHScopeEntry();
2112 }
2113 NewEHPadBB = CatchSwitch->getUnwindDest();
2114 } else {
2115 continue;
2116 }
2117
2118 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2119 if (BPI && NewEHPadBB)
2120 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2121 EHPadBB = NewEHPadBB;
2122 }
2123}
2124
2125void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2126 // Update successor info.
2128 auto UnwindDest = I.getUnwindDest();
2130 BranchProbability UnwindDestProb =
2131 (BPI && UnwindDest)
2132 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2134 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2135 for (auto &UnwindDest : UnwindDests) {
2136 UnwindDest.first->setIsEHPad();
2137 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2138 }
2140
2141 // Create the terminator node.
2142 MachineBasicBlock *CleanupPadMBB =
2143 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2145 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2146 DAG.setRoot(Ret);
2147}
2148
2149void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2150 report_fatal_error("visitCatchSwitch not yet implemented!");
2151}
2152
2153void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2155 auto &DL = DAG.getDataLayout();
2156 SDValue Chain = getControlRoot();
2159
2160 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2161 // lower
2162 //
2163 // %val = call <ty> @llvm.experimental.deoptimize()
2164 // ret <ty> %val
2165 //
2166 // differently.
2167 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2169 return;
2170 }
2171
2172 if (!FuncInfo.CanLowerReturn) {
2173 Register DemoteReg = FuncInfo.DemoteRegister;
2174
2175 // Emit a store of the return value through the virtual register.
2176 // Leave Outs empty so that LowerReturn won't try to load return
2177 // registers the usual way.
2178 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2179 SDValue RetPtr =
2180 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2181 SDValue RetOp = getValue(I.getOperand(0));
2182
2183 SmallVector<EVT, 4> ValueVTs, MemVTs;
2185 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2186 &Offsets, 0);
2187 unsigned NumValues = ValueVTs.size();
2188
2189 SmallVector<SDValue, 4> Chains(NumValues);
2190 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2191 for (unsigned i = 0; i != NumValues; ++i) {
2192 // An aggregate return value cannot wrap around the address space, so
2193 // offsets to its parts don't wrap either.
2195 TypeSize::getFixed(Offsets[i]));
2196
2197 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2198 if (MemVTs[i] != ValueVTs[i])
2199 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2200 Chains[i] = DAG.getStore(
2201 Chain, getCurSDLoc(), Val,
2202 // FIXME: better loc info would be nice.
2204 commonAlignment(BaseAlign, Offsets[i]));
2205 }
2206
2208 MVT::Other, Chains);
2209 } else if (I.getNumOperands() != 0) {
2211 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2212 unsigned NumValues = Types.size();
2213 if (NumValues) {
2214 SDValue RetOp = getValue(I.getOperand(0));
2215
2216 const Function *F = I.getParent()->getParent();
2217
2218 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2219 I.getOperand(0)->getType(), F->getCallingConv(),
2220 /*IsVarArg*/ false, DL);
2221
2222 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2223 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2224 ExtendKind = ISD::SIGN_EXTEND;
2225 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2226 ExtendKind = ISD::ZERO_EXTEND;
2227
2228 LLVMContext &Context = F->getContext();
2229 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2230
2231 for (unsigned j = 0; j != NumValues; ++j) {
2232 EVT VT = TLI.getValueType(DL, Types[j]);
2233
2234 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2235 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2236
2237 CallingConv::ID CC = F->getCallingConv();
2238
2239 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2240 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2241 SmallVector<SDValue, 4> Parts(NumParts);
2243 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2244 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2245
2246 // 'inreg' on function refers to return value
2248 if (RetInReg)
2249 Flags.setInReg();
2250
2251 if (I.getOperand(0)->getType()->isPointerTy()) {
2252 Flags.setPointer();
2253 Flags.setPointerAddrSpace(
2254 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2255 }
2256
2257 if (NeedsRegBlock) {
2258 Flags.setInConsecutiveRegs();
2259 if (j == NumValues - 1)
2260 Flags.setInConsecutiveRegsLast();
2261 }
2262
2263 // Propagate extension type if any
2264 if (ExtendKind == ISD::SIGN_EXTEND)
2265 Flags.setSExt();
2266 else if (ExtendKind == ISD::ZERO_EXTEND)
2267 Flags.setZExt();
2268 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2269 Flags.setNoExt();
2270
2271 for (unsigned i = 0; i < NumParts; ++i) {
2272 Outs.push_back(ISD::OutputArg(Flags,
2273 Parts[i].getValueType().getSimpleVT(),
2274 VT, Types[j], 0, 0));
2275 OutVals.push_back(Parts[i]);
2276 }
2277 }
2278 }
2279 }
2280
2281 // Push in swifterror virtual register as the last element of Outs. This makes
2282 // sure swifterror virtual register will be returned in the swifterror
2283 // physical register.
2284 const Function *F = I.getParent()->getParent();
2285 if (TLI.supportSwiftError() &&
2286 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2287 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2289 Flags.setSwiftError();
2290 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2291 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2293 /*origidx=*/1, /*partOffs=*/0));
2294 // Create SDNode for the swifterror virtual register.
2295 OutVals.push_back(
2298 EVT(TLI.getPointerTy(DL))));
2299 }
2300
2301 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2302 CallingConv::ID CallConv =
2305 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2306
2307 // Verify that the target's LowerReturn behaved as expected.
2308 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2309 "LowerReturn didn't return a valid chain!");
2310
2311 // Update the DAG with the new chain value resulting from return lowering.
2312 DAG.setRoot(Chain);
2313}
2314
2315/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2316/// created for it, emit nodes to copy the value into the virtual
2317/// registers.
2319 // Skip empty types
2320 if (V->getType()->isEmptyTy())
2321 return;
2322
2324 if (VMI != FuncInfo.ValueMap.end()) {
2325 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2326 "Unused value assigned virtual registers!");
2327 CopyValueToVirtualRegister(V, VMI->second);
2328 }
2329}
2330
2331/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2332/// the current basic block, add it to ValueMap now so that we'll get a
2333/// CopyTo/FromReg.
2335 // No need to export constants.
2336 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2337
2338 // Already exported?
2339 if (FuncInfo.isExportedInst(V)) return;
2340
2343}
2344
2346 const BasicBlock *FromBB) {
2347 // The operands of the setcc have to be in this block. We don't know
2348 // how to export them from some other block.
2349 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2350 // Can export from current BB.
2351 if (VI->getParent() == FromBB)
2352 return true;
2353
2354 // Is already exported, noop.
2355 return FuncInfo.isExportedInst(V);
2356 }
2357
2358 // If this is an argument, we can export it if the BB is the entry block or
2359 // if it is already exported.
2360 if (isa<Argument>(V)) {
2361 if (FromBB->isEntryBlock())
2362 return true;
2363
2364 // Otherwise, can only export this if it is already exported.
2365 return FuncInfo.isExportedInst(V);
2366 }
2367
2368 // Otherwise, constants can always be exported.
2369 return true;
2370}
2371
2372/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2374SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2375 const MachineBasicBlock *Dst) const {
2377 const BasicBlock *SrcBB = Src->getBasicBlock();
2378 const BasicBlock *DstBB = Dst->getBasicBlock();
2379 if (!BPI) {
2380 // If BPI is not available, set the default probability as 1 / N, where N is
2381 // the number of successors.
2382 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2383 return BranchProbability(1, SuccSize);
2384 }
2385 return BPI->getEdgeProbability(SrcBB, DstBB);
2386}
2387
2388void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2389 MachineBasicBlock *Dst,
2390 BranchProbability Prob) {
2391 if (!FuncInfo.BPI)
2392 Src->addSuccessorWithoutProb(Dst);
2393 else {
2394 if (Prob.isUnknown())
2395 Prob = getEdgeProbability(Src, Dst);
2396 Src->addSuccessor(Dst, Prob);
2397 }
2398}
2399
2400static bool InBlock(const Value *V, const BasicBlock *BB) {
2401 if (const Instruction *I = dyn_cast<Instruction>(V))
2402 return I->getParent() == BB;
2403 return true;
2404}
2405
2406/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2407/// This function emits a branch and is used at the leaves of an OR or an
2408/// AND operator tree.
2409void
2412 MachineBasicBlock *FBB,
2413 MachineBasicBlock *CurBB,
2414 MachineBasicBlock *SwitchBB,
2415 BranchProbability TProb,
2416 BranchProbability FProb,
2417 bool InvertCond) {
2418 const BasicBlock *BB = CurBB->getBasicBlock();
2419
2420 // If the leaf of the tree is a comparison, merge the condition into
2421 // the caseblock.
2422 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2423 // The operands of the cmp have to be in this block. We don't know
2424 // how to export them from some other block. If this is the first block
2425 // of the sequence, no exporting is needed.
2426 if (CurBB == SwitchBB ||
2427 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2428 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2429 ISD::CondCode Condition;
2430 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2431 ICmpInst::Predicate Pred =
2432 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2433 Condition = getICmpCondCode(Pred);
2434 } else {
2435 const FCmpInst *FC = cast<FCmpInst>(Cond);
2436 FCmpInst::Predicate Pred =
2437 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2438 Condition = getFCmpCondCode(Pred);
2439 if (TM.Options.NoNaNsFPMath)
2440 Condition = getFCmpCodeWithoutNaN(Condition);
2441 }
2442
2443 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2444 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2445 SL->SwitchCases.push_back(CB);
2446 return;
2447 }
2448 }
2449
2450 // Create a CaseBlock record representing this branch.
2451 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2453 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2454 SL->SwitchCases.push_back(CB);
2455}
2456
2457// Collect dependencies on V recursively. This is used for the cost analysis in
2458// `shouldKeepJumpConditionsTogether`.
2462 unsigned Depth = 0) {
2463 // Return false if we have an incomplete count.
2465 return false;
2466
2467 auto *I = dyn_cast<Instruction>(V);
2468 if (I == nullptr)
2469 return true;
2470
2471 if (Necessary != nullptr) {
2472 // This instruction is necessary for the other side of the condition so
2473 // don't count it.
2474 if (Necessary->contains(I))
2475 return true;
2476 }
2477
2478 // Already added this dep.
2479 if (!Deps->try_emplace(I, false).second)
2480 return true;
2481
2482 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2483 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2484 Depth + 1))
2485 return false;
2486 return true;
2487}
2488
2490 const FunctionLoweringInfo &FuncInfo, const BranchInst &I,
2491 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2493 if (I.getNumSuccessors() != 2)
2494 return false;
2495
2496 if (!I.isConditional())
2497 return false;
2498
2499 if (Params.BaseCost < 0)
2500 return false;
2501
2502 // Baseline cost.
2503 InstructionCost CostThresh = Params.BaseCost;
2504
2505 BranchProbabilityInfo *BPI = nullptr;
2506 if (Params.LikelyBias || Params.UnlikelyBias)
2507 BPI = FuncInfo.BPI;
2508 if (BPI != nullptr) {
2509 // See if we are either likely to get an early out or compute both lhs/rhs
2510 // of the condition.
2511 BasicBlock *IfFalse = I.getSuccessor(0);
2512 BasicBlock *IfTrue = I.getSuccessor(1);
2513
2514 std::optional<bool> Likely;
2515 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2516 Likely = true;
2517 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2518 Likely = false;
2519
2520 if (Likely) {
2521 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2522 // Its likely we will have to compute both lhs and rhs of condition
2523 CostThresh += Params.LikelyBias;
2524 else {
2525 if (Params.UnlikelyBias < 0)
2526 return false;
2527 // Its likely we will get an early out.
2528 CostThresh -= Params.UnlikelyBias;
2529 }
2530 }
2531 }
2532
2533 if (CostThresh <= 0)
2534 return false;
2535
2536 // Collect "all" instructions that lhs condition is dependent on.
2537 // Use map for stable iteration (to avoid non-determanism of iteration of
2538 // SmallPtrSet). The `bool` value is just a dummy.
2540 collectInstructionDeps(&LhsDeps, Lhs);
2541 // Collect "all" instructions that rhs condition is dependent on AND are
2542 // dependencies of lhs. This gives us an estimate on which instructions we
2543 // stand to save by splitting the condition.
2544 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2545 return false;
2546 // Add the compare instruction itself unless its a dependency on the LHS.
2547 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2548 if (!LhsDeps.contains(RhsI))
2549 RhsDeps.try_emplace(RhsI, false);
2550
2551 const auto &TLI = DAG.getTargetLoweringInfo();
2552 const auto &TTI =
2553 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
2554
2555 InstructionCost CostOfIncluding = 0;
2556 // See if this instruction will need to computed independently of whether RHS
2557 // is.
2558 Value *BrCond = I.getCondition();
2559 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2560 for (const auto *U : Ins->users()) {
2561 // If user is independent of RHS calculation we don't need to count it.
2562 if (auto *UIns = dyn_cast<Instruction>(U))
2563 if (UIns != BrCond && !RhsDeps.contains(UIns))
2564 return false;
2565 }
2566 return true;
2567 };
2568
2569 // Prune instructions from RHS Deps that are dependencies of unrelated
2570 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2571 // arbitrary and just meant to cap the how much time we spend in the pruning
2572 // loop. Its highly unlikely to come into affect.
2573 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2574 // Stop after a certain point. No incorrectness from including too many
2575 // instructions.
2576 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2577 const Instruction *ToDrop = nullptr;
2578 for (const auto &InsPair : RhsDeps) {
2579 if (!ShouldCountInsn(InsPair.first)) {
2580 ToDrop = InsPair.first;
2581 break;
2582 }
2583 }
2584 if (ToDrop == nullptr)
2585 break;
2586 RhsDeps.erase(ToDrop);
2587 }
2588
2589 for (const auto &InsPair : RhsDeps) {
2590 // Finally accumulate latency that we can only attribute to computing the
2591 // RHS condition. Use latency because we are essentially trying to calculate
2592 // the cost of the dependency chain.
2593 // Possible TODO: We could try to estimate ILP and make this more precise.
2594 CostOfIncluding +=
2596
2597 if (CostOfIncluding > CostThresh)
2598 return false;
2599 }
2600 return true;
2601}
2602
2605 MachineBasicBlock *FBB,
2606 MachineBasicBlock *CurBB,
2607 MachineBasicBlock *SwitchBB,
2609 BranchProbability TProb,
2610 BranchProbability FProb,
2611 bool InvertCond) {
2612 // Skip over not part of the tree and remember to invert op and operands at
2613 // next level.
2614 Value *NotCond;
2615 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2616 InBlock(NotCond, CurBB->getBasicBlock())) {
2617 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2618 !InvertCond);
2619 return;
2620 }
2621
2622 const Instruction *BOp = dyn_cast<Instruction>(Cond);
2623 const Value *BOpOp0, *BOpOp1;
2624 // Compute the effective opcode for Cond, taking into account whether it needs
2625 // to be inverted, e.g.
2626 // and (not (or A, B)), C
2627 // gets lowered as
2628 // and (and (not A, not B), C)
2630 if (BOp) {
2631 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2632 ? Instruction::And
2633 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2634 ? Instruction::Or
2636 if (InvertCond) {
2637 if (BOpc == Instruction::And)
2638 BOpc = Instruction::Or;
2639 else if (BOpc == Instruction::Or)
2640 BOpc = Instruction::And;
2641 }
2642 }
2643
2644 // If this node is not part of the or/and tree, emit it as a branch.
2645 // Note that all nodes in the tree should have same opcode.
2646 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2647 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2648 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2649 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2650 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2651 TProb, FProb, InvertCond);
2652 return;
2653 }
2654
2655 // Create TmpBB after CurBB.
2656 MachineFunction::iterator BBI(CurBB);
2659 CurBB->getParent()->insert(++BBI, TmpBB);
2660
2661 if (Opc == Instruction::Or) {
2662 // Codegen X | Y as:
2663 // BB1:
2664 // jmp_if_X TBB
2665 // jmp TmpBB
2666 // TmpBB:
2667 // jmp_if_Y TBB
2668 // jmp FBB
2669 //
2670
2671 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2672 // The requirement is that
2673 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2674 // = TrueProb for original BB.
2675 // Assuming the original probabilities are A and B, one choice is to set
2676 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2677 // A/(1+B) and 2B/(1+B). This choice assumes that
2678 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2679 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2680 // TmpBB, but the math is more complicated.
2681
2682 auto NewTrueProb = TProb / 2;
2683 auto NewFalseProb = TProb / 2 + FProb;
2684 // Emit the LHS condition.
2685 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2686 NewFalseProb, InvertCond);
2687
2688 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2689 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2690 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
2691 // Emit the RHS condition into TmpBB.
2692 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2693 Probs[1], InvertCond);
2694 } else {
2695 assert(Opc == Instruction::And && "Unknown merge op!");
2696 // Codegen X & Y as:
2697 // BB1:
2698 // jmp_if_X TmpBB
2699 // jmp FBB
2700 // TmpBB:
2701 // jmp_if_Y TBB
2702 // jmp FBB
2703 //
2704 // This requires creation of TmpBB after CurBB.
2705
2706 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2707 // The requirement is that
2708 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2709 // = FalseProb for original BB.
2710 // Assuming the original probabilities are A and B, one choice is to set
2711 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2712 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2713 // TrueProb for BB1 * FalseProb for TmpBB.
2714
2715 auto NewTrueProb = TProb + FProb / 2;
2716 auto NewFalseProb = FProb / 2;
2717 // Emit the LHS condition.
2718 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2719 NewFalseProb, InvertCond);
2720
2721 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2722 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2723 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
2724 // Emit the RHS condition into TmpBB.
2725 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2726 Probs[1], InvertCond);
2727 }
2728}
2729
2730/// If the set of cases should be emitted as a series of branches, return true.
2731/// If we should emit this as a bunch of and/or'd together conditions, return
2732/// false.
2733bool
2734SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2735 if (Cases.size() != 2) return true;
2736
2737 // If this is two comparisons of the same values or'd or and'd together, they
2738 // will get folded into a single comparison, so don't emit two blocks.
2739 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2740 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2741 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2742 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2743 return false;
2744 }
2745
2746 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2747 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2748 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2749 Cases[0].CC == Cases[1].CC &&
2750 isa<Constant>(Cases[0].CmpRHS) &&
2751 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2752 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2753 return false;
2754 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2755 return false;
2756 }
2757
2758 return true;
2759}
2760
2761void SelectionDAGBuilder::visitBr(const BranchInst &I) {
2763
2764 // Update machine-CFG edges.
2765 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2766
2767 if (I.isUnconditional()) {
2768 // Update machine-CFG edges.
2769 BrMBB->addSuccessor(Succ0MBB);
2770
2771 // If this is not a fall-through branch or optimizations are switched off,
2772 // emit the branch.
2773 if (Succ0MBB != NextBlock(BrMBB) ||
2775 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2776 getControlRoot(), DAG.getBasicBlock(Succ0MBB));
2777 setValue(&I, Br);
2778 DAG.setRoot(Br);
2779 }
2780
2781 return;
2782 }
2783
2784 // If this condition is one of the special cases we handle, do special stuff
2785 // now.
2786 const Value *CondVal = I.getCondition();
2787 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2788
2789 // If this is a series of conditions that are or'd or and'd together, emit
2790 // this as a sequence of branches instead of setcc's with and/or operations.
2791 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2792 // unpredictable branches, and vector extracts because those jumps are likely
2793 // expensive for any target), this should improve performance.
2794 // For example, instead of something like:
2795 // cmp A, B
2796 // C = seteq
2797 // cmp D, E
2798 // F = setle
2799 // or C, F
2800 // jnz foo
2801 // Emit:
2802 // cmp A, B
2803 // je foo
2804 // cmp D, E
2805 // jle foo
2806 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2807 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2808 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2809 BOp->hasOneUse() && !IsUnpredictable) {
2810 Value *Vec;
2811 const Value *BOp0, *BOp1;
2813 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2814 Opcode = Instruction::And;
2815 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2816 Opcode = Instruction::Or;
2817
2818 if (Opcode &&
2819 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2820 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2822 FuncInfo, I, Opcode, BOp0, BOp1,
2824 Opcode, BOp0, BOp1))) {
2825 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2826 getEdgeProbability(BrMBB, Succ0MBB),
2827 getEdgeProbability(BrMBB, Succ1MBB),
2828 /*InvertCond=*/false);
2829 // If the compares in later blocks need to use values not currently
2830 // exported from this block, export them now. This block should always
2831 // be the first entry.
2832 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2833
2834 // Allow some cases to be rejected.
2835 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2836 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2837 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2838 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2839 }
2840
2841 // Emit the branch for this block.
2842 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2843 SL->SwitchCases.erase(SL->SwitchCases.begin());
2844 return;
2845 }
2846
2847 // Okay, we decided not to do this, remove any inserted MBB's and clear
2848 // SwitchCases.
2849 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2850 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2851
2852 SL->SwitchCases.clear();
2853 }
2854 }
2855
2856 // Create a CaseBlock record representing this branch.
2858 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2860 IsUnpredictable);
2861
2862 // Use visitSwitchCase to actually insert the fast branch sequence for this
2863 // cond branch.
2864 visitSwitchCase(CB, BrMBB);
2865}
2866
2867/// visitSwitchCase - Emits the necessary code to represent a single node in
2868/// the binary search tree resulting from lowering a switch instruction.
2870 MachineBasicBlock *SwitchBB) {
2871 SDValue Cond;
2872 SDValue CondLHS = getValue(CB.CmpLHS);
2873 SDLoc dl = CB.DL;
2874
2875 if (CB.CC == ISD::SETTRUE) {
2876 // Branch or fall through to TrueBB.
2877 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2878 SwitchBB->normalizeSuccProbs();
2879 if (CB.TrueBB != NextBlock(SwitchBB)) {
2880 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2881 DAG.getBasicBlock(CB.TrueBB)));
2882 }
2883 return;
2884 }
2885
2886 auto &TLI = DAG.getTargetLoweringInfo();
2887 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2888
2889 // Build the setcc now.
2890 if (!CB.CmpMHS) {
2891 // Fold "(X == true)" to X and "(X == false)" to !X to
2892 // handle common cases produced by branch lowering.
2893 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2894 CB.CC == ISD::SETEQ)
2895 Cond = CondLHS;
2896 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2897 CB.CC == ISD::SETEQ) {
2898 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2899 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2900 } else {
2901 SDValue CondRHS = getValue(CB.CmpRHS);
2902
2903 // If a pointer's DAG type is larger than its memory type then the DAG
2904 // values are zero-extended. This breaks signed comparisons so truncate
2905 // back to the underlying type before doing the compare.
2906 if (CondLHS.getValueType() != MemVT) {
2907 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2908 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2909 }
2910 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2911 }
2912 } else {
2913 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2914
2915 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2916 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2917
2918 SDValue CmpOp = getValue(CB.CmpMHS);
2919 EVT VT = CmpOp.getValueType();
2920
2921 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2922 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2923 ISD::SETLE);
2924 } else {
2925 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2926 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2927 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2928 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2929 }
2930 }
2931
2932 // Update successor info
2933 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2934 // TrueBB and FalseBB are always different unless the incoming IR is
2935 // degenerate. This only happens when running llc on weird IR.
2936 if (CB.TrueBB != CB.FalseBB)
2937 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2938 SwitchBB->normalizeSuccProbs();
2939
2940 // If the lhs block is the next block, invert the condition so that we can
2941 // fall through to the lhs instead of the rhs block.
2942 if (CB.TrueBB == NextBlock(SwitchBB)) {
2943 std::swap(CB.TrueBB, CB.FalseBB);
2944 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2945 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2946 }
2947
2948 SDNodeFlags Flags;
2949 Flags.setUnpredictable(CB.IsUnpredictable);
2950 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2951 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2952
2953 setValue(CurInst, BrCond);
2954
2955 // Insert the false branch. Do this even if it's a fall through branch,
2956 // this makes it easier to do DAG optimizations which require inverting
2957 // the branch condition.
2958 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2960
2961 DAG.setRoot(BrCond);
2962}
2963
2964/// visitJumpTable - Emit JumpTable node in the current MBB
2966 // Emit the code for the jump table
2967 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
2968 assert(JT.Reg && "Should lower JT Header first!");
2970 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
2971 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
2972 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
2973 Index.getValue(1), Table, Index);
2974 DAG.setRoot(BrJumpTable);
2975}
2976
2977/// visitJumpTableHeader - This function emits necessary code to produce index
2978/// in the JumpTable from switch case.
2980 JumpTableHeader &JTH,
2981 MachineBasicBlock *SwitchBB) {
2982 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
2983 const SDLoc &dl = *JT.SL;
2984
2985 // Subtract the lowest switch case value from the value being switched on.
2986 SDValue SwitchOp = getValue(JTH.SValue);
2987 EVT VT = SwitchOp.getValueType();
2988 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
2989 DAG.getConstant(JTH.First, dl, VT));
2990
2991 // The SDNode we just created, which holds the value being switched on minus
2992 // the smallest case value, needs to be copied to a virtual register so it
2993 // can be used as an index into the jump table in a subsequent basic block.
2994 // This value may be smaller or larger than the target's pointer type, and
2995 // therefore require extension or truncating.
2997 SwitchOp =
2999
3000 Register JumpTableReg =
3002 SDValue CopyTo =
3003 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3004 JT.Reg = JumpTableReg;
3005
3006 if (!JTH.FallthroughUnreachable) {
3007 // Emit the range check for the jump table, and branch to the default block
3008 // for the switch statement if the value being switched on exceeds the
3009 // largest case in the switch.
3010 SDValue CMP = DAG.getSetCC(
3012 Sub.getValueType()),
3013 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3014
3015 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3016 MVT::Other, CopyTo, CMP,
3017 DAG.getBasicBlock(JT.Default));
3018
3019 // Avoid emitting unnecessary branches to the next block.
3020 if (JT.MBB != NextBlock(SwitchBB))
3021 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3022 DAG.getBasicBlock(JT.MBB));
3023
3024 DAG.setRoot(BrCond);
3025 } else {
3026 // Avoid emitting unnecessary branches to the next block.
3027 if (JT.MBB != NextBlock(SwitchBB))
3028 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3029 DAG.getBasicBlock(JT.MBB)));
3030 else
3031 DAG.setRoot(CopyTo);
3032 }
3033}
3034
3035/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3036/// variable if there exists one.
3038 SDValue &Chain) {
3039 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3040 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3041 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3045 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3046 if (Global) {
3047 MachinePointerInfo MPInfo(Global);
3051 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3052 DAG.setNodeMemRefs(Node, {MemRef});
3053 }
3054 if (PtrTy != PtrMemTy)
3055 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3056 return SDValue(Node, 0);
3057}
3058
3059/// Codegen a new tail for a stack protector check ParentMBB which has had its
3060/// tail spliced into a stack protector check success bb.
3061///
3062/// For a high level explanation of how this fits into the stack protector
3063/// generation see the comment on the declaration of class
3064/// StackProtectorDescriptor.
3066 MachineBasicBlock *ParentBB) {
3067
3068 // First create the loads to the guard/stack slot for the comparison.
3070 auto &DL = DAG.getDataLayout();
3071 EVT PtrTy = TLI.getFrameIndexTy(DL);
3072 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3073
3074 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3075 int FI = MFI.getStackProtectorIndex();
3076
3077 SDValue Guard;
3078 SDLoc dl = getCurSDLoc();
3079 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3080 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3081 Align Align = DL.getPrefTypeAlign(
3082 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3083
3084 // Generate code to load the content of the guard slot.
3085 SDValue GuardVal = DAG.getLoad(
3086 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3089
3090 if (TLI.useStackGuardXorFP())
3091 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3092
3093 // If we're using function-based instrumentation, call the guard check
3094 // function
3096 // Get the guard check function from the target and verify it exists since
3097 // we're using function-based instrumentation
3098 const Function *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3099 assert(GuardCheckFn && "Guard check function is null");
3100
3101 // The target provides a guard check function to validate the guard value.
3102 // Generate a call to that function with the content of the guard slot as
3103 // argument.
3104 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3105 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3106
3108 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3109 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3110 Entry.IsInReg = true;
3111 Args.push_back(Entry);
3112
3116 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3117 getValue(GuardCheckFn), std::move(Args));
3118
3119 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3120 DAG.setRoot(Result.second);
3121 return;
3122 }
3123
3124 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3125 // Otherwise, emit a volatile load to retrieve the stack guard value.
3126 SDValue Chain = DAG.getEntryNode();
3127 if (TLI.useLoadStackGuardNode(M)) {
3128 Guard = getLoadStackGuard(DAG, dl, Chain);
3129 } else {
3130 const Value *IRGuard = TLI.getSDagStackGuard(M);
3131 SDValue GuardPtr = getValue(IRGuard);
3132
3133 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3134 MachinePointerInfo(IRGuard, 0), Align,
3136 }
3137
3138 // Perform the comparison via a getsetcc.
3139 SDValue Cmp = DAG.getSetCC(
3140 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3141 Guard, GuardVal, ISD::SETNE);
3142
3143 // If the guard/stackslot do not equal, branch to failure MBB.
3144 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3145 MVT::Other, GuardVal.getOperand(0),
3146 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3147 // Otherwise branch to success MBB.
3148 SDValue Br = DAG.getNode(ISD::BR, dl,
3149 MVT::Other, BrCond,
3151
3152 DAG.setRoot(Br);
3153}
3154
3155/// Codegen the failure basic block for a stack protector check.
3156///
3157/// A failure stack protector machine basic block consists simply of a call to
3158/// __stack_chk_fail().
3159///
3160/// For a high level explanation of how this fits into the stack protector
3161/// generation see the comment on the declaration of class
3162/// StackProtectorDescriptor.
3165
3167 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3168 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3169 SDValue Chain;
3170
3171 // For -Oz builds with a guard check function, we use function-based
3172 // instrumentation. Otherwise, if we have a guard check function, we call it
3173 // in the failure block.
3174 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3175 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3176 // First create the loads to the guard/stack slot for the comparison.
3177 auto &DL = DAG.getDataLayout();
3178 EVT PtrTy = TLI.getFrameIndexTy(DL);
3179 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3180
3181 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3182 int FI = MFI.getStackProtectorIndex();
3183
3184 SDLoc dl = getCurSDLoc();
3185 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3186 Align Align = DL.getPrefTypeAlign(
3187 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3188
3189 // Generate code to load the content of the guard slot.
3190 SDValue GuardVal = DAG.getLoad(
3191 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3194
3195 if (TLI.useStackGuardXorFP())
3196 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3197
3198 // The target provides a guard check function to validate the guard value.
3199 // Generate a call to that function with the content of the guard slot as
3200 // argument.
3201 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3202 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3203
3205 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3206 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3207 Entry.IsInReg = true;
3208 Args.push_back(Entry);
3209
3213 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3214 getValue(GuardCheckFn), std::move(Args));
3215
3216 Chain = TLI.LowerCallTo(CLI).second;
3217 } else {
3219 CallOptions.setDiscardResult(true);
3220 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3221 {}, CallOptions, getCurSDLoc())
3222 .second;
3223 }
3224
3225 // Emit a trap instruction if we are required to do so.
3226 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3227 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3228 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3229
3230 DAG.setRoot(Chain);
3231}
3232
3233/// visitBitTestHeader - This function emits necessary code to produce value
3234/// suitable for "bit tests"
3236 MachineBasicBlock *SwitchBB) {
3237 SDLoc dl = getCurSDLoc();
3238
3239 // Subtract the minimum value.
3240 SDValue SwitchOp = getValue(B.SValue);
3241 EVT VT = SwitchOp.getValueType();
3242 SDValue RangeSub =
3243 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3244
3245 // Determine the type of the test operands.
3247 bool UsePtrType = false;
3248 if (!TLI.isTypeLegal(VT)) {
3249 UsePtrType = true;
3250 } else {
3251 for (const BitTestCase &Case : B.Cases)
3252 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3253 // Switch table case range are encoded into series of masks.
3254 // Just use pointer type, it's guaranteed to fit.
3255 UsePtrType = true;
3256 break;
3257 }
3258 }
3259 SDValue Sub = RangeSub;
3260 if (UsePtrType) {
3261 VT = TLI.getPointerTy(DAG.getDataLayout());
3262 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3263 }
3264
3265 B.RegVT = VT.getSimpleVT();
3266 B.Reg = FuncInfo.CreateReg(B.RegVT);
3267 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3268
3269 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3270
3271 if (!B.FallthroughUnreachable)
3272 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3273 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3274 SwitchBB->normalizeSuccProbs();
3275
3276 SDValue Root = CopyTo;
3277 if (!B.FallthroughUnreachable) {
3278 // Conditional branch to the default block.
3279 SDValue RangeCmp = DAG.getSetCC(dl,
3281 RangeSub.getValueType()),
3282 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3283 ISD::SETUGT);
3284
3285 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3286 DAG.getBasicBlock(B.Default));
3287 }
3288
3289 // Avoid emitting unnecessary branches to the next block.
3290 if (MBB != NextBlock(SwitchBB))
3291 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3292
3293 DAG.setRoot(Root);
3294}
3295
3296/// visitBitTestCase - this function produces one "bit test"
3298 MachineBasicBlock *NextMBB,
3299 BranchProbability BranchProbToNext,
3300 Register Reg, BitTestCase &B,
3301 MachineBasicBlock *SwitchBB) {
3302 SDLoc dl = getCurSDLoc();
3303 MVT VT = BB.RegVT;
3304 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3305 SDValue Cmp;
3306 unsigned PopCount = llvm::popcount(B.Mask);
3308 if (PopCount == 1) {
3309 // Testing for a single bit; just compare the shift count with what it
3310 // would need to be to shift a 1 bit in that position.
3311 Cmp = DAG.getSetCC(
3313 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3314 ISD::SETEQ);
3315 } else if (PopCount == BB.Range) {
3316 // There is only one zero bit in the range, test for it directly.
3317 Cmp = DAG.getSetCC(
3319 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3320 } else {
3321 // Make desired shift
3322 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3323 DAG.getConstant(1, dl, VT), ShiftOp);
3324
3325 // Emit bit tests and jumps
3326 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3327 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3328 Cmp = DAG.getSetCC(
3330 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3331 }
3332
3333 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3334 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3335 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3336 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3337 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3338 // one as they are relative probabilities (and thus work more like weights),
3339 // and hence we need to normalize them to let the sum of them become one.
3340 SwitchBB->normalizeSuccProbs();
3341
3342 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3343 MVT::Other, getControlRoot(),
3344 Cmp, DAG.getBasicBlock(B.TargetBB));
3345
3346 // Avoid emitting unnecessary branches to the next block.
3347 if (NextMBB != NextBlock(SwitchBB))
3348 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3349 DAG.getBasicBlock(NextMBB));
3350
3351 DAG.setRoot(BrAnd);
3352}
3353
3354void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3355 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3356
3357 // Retrieve successors. Look through artificial IR level blocks like
3358 // catchswitch for successors.
3359 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3360 const BasicBlock *EHPadBB = I.getSuccessor(1);
3361 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3362
3363 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3364 // have to do anything here to lower funclet bundles.
3365 failForInvalidBundles(I, "invokes",
3371
3372 const Value *Callee(I.getCalledOperand());
3373 const Function *Fn = dyn_cast<Function>(Callee);
3374 if (isa<InlineAsm>(Callee))
3375 visitInlineAsm(I, EHPadBB);
3376 else if (Fn && Fn->isIntrinsic()) {
3377 switch (Fn->getIntrinsicID()) {
3378 default:
3379 llvm_unreachable("Cannot invoke this intrinsic");
3380 case Intrinsic::donothing:
3381 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3382 case Intrinsic::seh_try_begin:
3383 case Intrinsic::seh_scope_begin:
3384 case Intrinsic::seh_try_end:
3385 case Intrinsic::seh_scope_end:
3386 if (EHPadMBB)
3387 // a block referenced by EH table
3388 // so dtor-funclet not removed by opts
3389 EHPadMBB->setMachineBlockAddressTaken();
3390 break;
3391 case Intrinsic::experimental_patchpoint_void:
3392 case Intrinsic::experimental_patchpoint:
3393 visitPatchpoint(I, EHPadBB);
3394 break;
3395 case Intrinsic::experimental_gc_statepoint:
3396 LowerStatepoint(cast<GCStatepointInst>(I), EHPadBB);
3397 break;
3398 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3399 // but these intrinsics are special because they can be invoked, so we
3400 // manually lower it to a DAG node here.
3401 case Intrinsic::wasm_throw: {
3403 std::array<SDValue, 4> Ops = {
3404 getControlRoot(), // inchain for the terminator node
3405 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3407 getValue(I.getArgOperand(0)), // tag
3408 getValue(I.getArgOperand(1)) // thrown value
3409 };
3410 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3412 break;
3413 }
3414 case Intrinsic::wasm_rethrow: {
3416 std::array<SDValue, 2> Ops = {
3417 getControlRoot(), // inchain for the terminator node
3418 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3420 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3422 break;
3423 }
3424 }
3425 } else if (I.hasDeoptState()) {
3426 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3427 // Eventually we will support lowering the @llvm.experimental.deoptimize
3428 // intrinsic, and right now there are no plans to support other intrinsics
3429 // with deopt state.
3430 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3431 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3432 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), EHPadBB);
3433 } else {
3434 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3435 }
3436
3437 // If the value of the invoke is used outside of its defining block, make it
3438 // available as a virtual register.
3439 // We already took care of the exported value for the statepoint instruction
3440 // during call to the LowerStatepoint.
3441 if (!isa<GCStatepointInst>(I)) {
3443 }
3444
3447 BranchProbability EHPadBBProb =
3448 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3450 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3451
3452 // Update successor info.
3453 addSuccessorWithProb(InvokeMBB, Return);
3454 for (auto &UnwindDest : UnwindDests) {
3455 UnwindDest.first->setIsEHPad();
3456 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3457 }
3458 InvokeMBB->normalizeSuccProbs();
3459
3460 // Drop into normal successor.
3462 DAG.getBasicBlock(Return)));
3463}
3464
3465void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3466 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3467
3468 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3469 // have to do anything here to lower funclet bundles.
3470 failForInvalidBundles(I, "callbrs",
3472
3473 assert(I.isInlineAsm() && "Only know how to handle inlineasm callbr");
3474 visitInlineAsm(I);
3476
3477 // Retrieve successors.
3479 Dests.insert(I.getDefaultDest());
3480 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3481
3482 // Update successor info.
3483 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3484 for (unsigned i = 0, e = I.getNumIndirectDests(); i < e; ++i) {
3485 BasicBlock *Dest = I.getIndirectDest(i);
3487 Target->setIsInlineAsmBrIndirectTarget();
3488 // If we introduce a type of asm goto statement that is permitted to use an
3489 // indirect call instruction to jump to its labels, then we should add a
3490 // call to Target->setMachineBlockAddressTaken() here, to mark the target
3491 // block as requiring a BTI.
3492
3493 Target->setLabelMustBeEmitted();
3494 // Don't add duplicate machine successors.
3495 if (Dests.insert(Dest).second)
3496 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3497 }
3498 CallBrMBB->normalizeSuccProbs();
3499
3500 // Drop into default successor.
3502 MVT::Other, getControlRoot(),
3503 DAG.getBasicBlock(Return)));
3504}
3505
3506void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3507 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3508}
3509
3510void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3512 "Call to landingpad not in landing pad!");
3513
3514 // If there aren't registers to copy the values into (e.g., during SjLj
3515 // exceptions), then don't bother to create these DAG nodes.
3517 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3518 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3519 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3520 return;
3521
3522 // If landingpad's return type is token type, we don't create DAG nodes
3523 // for its exception pointer and selector value. The extraction of exception
3524 // pointer or selector value from token type landingpads is not currently
3525 // supported.
3526 if (LP.getType()->isTokenTy())
3527 return;
3528
3529 SmallVector<EVT, 2> ValueVTs;
3530 SDLoc dl = getCurSDLoc();
3531 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3532 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3533
3534 // Get the two live-in registers as SDValues. The physregs have already been
3535 // copied into virtual registers.
3536 SDValue Ops[2];
3538 Ops[0] = DAG.getZExtOrTrunc(
3542 dl, ValueVTs[0]);
3543 } else {
3544 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3545 }
3546 Ops[1] = DAG.getZExtOrTrunc(
3550 dl, ValueVTs[1]);
3551
3552 // Merge into one.
3554 DAG.getVTList(ValueVTs), Ops);
3555 setValue(&LP, Res);
3556}
3557
3560 // Update JTCases.
3561 for (JumpTableBlock &JTB : SL->JTCases)
3562 if (JTB.first.HeaderBB == First)
3563 JTB.first.HeaderBB = Last;
3564
3565 // Update BitTestCases.
3566 for (BitTestBlock &BTB : SL->BitTestCases)
3567 if (BTB.Parent == First)
3568 BTB.Parent = Last;
3569}
3570
3571void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3572 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3573
3574 // Update machine-CFG edges with unique successors.
3576 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3577 BasicBlock *BB = I.getSuccessor(i);
3578 bool Inserted = Done.insert(BB).second;
3579 if (!Inserted)
3580 continue;
3581
3582 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3583 addSuccessorWithProb(IndirectBrMBB, Succ);
3584 }
3585 IndirectBrMBB->normalizeSuccProbs();
3586
3588 MVT::Other, getControlRoot(),
3589 getValue(I.getAddress())));
3590}
3591
3592void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3593 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3595 return;
3596
3597 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3598}
3599
3600void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3602 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3603 Flags.copyFMF(*FPOp);
3604
3605 SDValue Op = getValue(I.getOperand(0));
3606 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3607 Op, Flags);
3608 setValue(&I, UnNodeValue);
3609}
3610
3611void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3613 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3614 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3615 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3616 }
3617 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3618 Flags.setExact(ExactOp->isExact());
3619 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3620 Flags.setDisjoint(DisjointOp->isDisjoint());
3621 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3622 Flags.copyFMF(*FPOp);
3623
3624 SDValue Op1 = getValue(I.getOperand(0));
3625 SDValue Op2 = getValue(I.getOperand(1));
3626 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3627 Op1, Op2, Flags);
3628 setValue(&I, BinNodeValue);
3629}
3630
3631void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3632 SDValue Op1 = getValue(I.getOperand(0));
3633 SDValue Op2 = getValue(I.getOperand(1));
3634
3636 Op1.getValueType(), DAG.getDataLayout());
3637
3638 // Coerce the shift amount to the right type if we can. This exposes the
3639 // truncate or zext to optimization early.
3640 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3642 "Unexpected shift type");
3643 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3644 }
3645
3646 bool nuw = false;
3647 bool nsw = false;
3648 bool exact = false;
3649
3650 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3651
3652 if (const OverflowingBinaryOperator *OFBinOp =
3653 dyn_cast<const OverflowingBinaryOperator>(&I)) {
3654 nuw = OFBinOp->hasNoUnsignedWrap();
3655 nsw = OFBinOp->hasNoSignedWrap();
3656 }
3657 if (const PossiblyExactOperator *ExactOp =
3658 dyn_cast<const PossiblyExactOperator>(&I))
3659 exact = ExactOp->isExact();
3660 }
3662 Flags.setExact(exact);
3663 Flags.setNoSignedWrap(nsw);
3664 Flags.setNoUnsignedWrap(nuw);
3665 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3666 Flags);
3667 setValue(&I, Res);
3668}
3669
3670void SelectionDAGBuilder::visitSDiv(const User &I) {
3671 SDValue Op1 = getValue(I.getOperand(0));
3672 SDValue Op2 = getValue(I.getOperand(1));
3673
3675 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3676 cast<PossiblyExactOperator>(&I)->isExact());
3678 Op2, Flags));
3679}
3680
3681void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3682 ICmpInst::Predicate predicate = I.getPredicate();
3683 SDValue Op1 = getValue(I.getOperand(0));
3684 SDValue Op2 = getValue(I.getOperand(1));
3685 ISD::CondCode Opcode = getICmpCondCode(predicate);
3686
3687 auto &TLI = DAG.getTargetLoweringInfo();
3688 EVT MemVT =
3689 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3690
3691 // If a pointer's DAG type is larger than its memory type then the DAG values
3692 // are zero-extended. This breaks signed comparisons so truncate back to the
3693 // underlying type before doing the compare.
3694 if (Op1.getValueType() != MemVT) {
3695 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3696 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3697 }
3698
3700 Flags.setSameSign(I.hasSameSign());
3701 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3702
3704 I.getType());
3705 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
3706}
3707
3708void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3709 FCmpInst::Predicate predicate = I.getPredicate();
3710 SDValue Op1 = getValue(I.getOperand(0));
3711 SDValue Op2 = getValue(I.getOperand(1));
3712
3713 ISD::CondCode Condition = getFCmpCondCode(predicate);
3714 auto *FPMO = cast<FPMathOperator>(&I);
3715 if (FPMO->hasNoNaNs() || TM.Options.NoNaNsFPMath)
3716 Condition = getFCmpCodeWithoutNaN(Condition);
3717
3719 Flags.copyFMF(*FPMO);
3720 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3721
3723 I.getType());
3724 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
3725}
3726
3727// Check if the condition of the select has one use or two users that are both
3728// selects with the same condition.
3729static bool hasOnlySelectUsers(const Value *Cond) {
3730 return llvm::all_of(Cond->users(), [](const Value *V) {
3731 return isa<SelectInst>(V);
3732 });
3733}
3734
3735void SelectionDAGBuilder::visitSelect(const User &I) {
3736 SmallVector<EVT, 4> ValueVTs;
3738 ValueVTs);
3739 unsigned NumValues = ValueVTs.size();
3740 if (NumValues == 0) return;
3741
3742 SmallVector<SDValue, 4> Values(NumValues);
3743 SDValue Cond = getValue(I.getOperand(0));
3744 SDValue LHSVal = getValue(I.getOperand(1));
3745 SDValue RHSVal = getValue(I.getOperand(2));
3746 SmallVector<SDValue, 1> BaseOps(1, Cond);
3748 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3749
3750 bool IsUnaryAbs = false;
3751 bool Negate = false;
3752
3754 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3755 Flags.copyFMF(*FPOp);
3756
3757 Flags.setUnpredictable(
3758 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3759
3760 // Min/max matching is only viable if all output VTs are the same.
3761 if (all_equal(ValueVTs)) {
3762 EVT VT = ValueVTs[0];
3763 LLVMContext &Ctx = *DAG.getContext();
3764 auto &TLI = DAG.getTargetLoweringInfo();
3765
3766 // We care about the legality of the operation after it has been type
3767 // legalized.
3768 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3769 VT = TLI.getTypeToTransformTo(Ctx, VT);
3770
3771 // If the vselect is legal, assume we want to leave this as a vector setcc +
3772 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3773 // min/max is legal on the scalar type.
3774 bool UseScalarMinMax = VT.isVector() &&
3776
3777 // ValueTracking's select pattern matching does not account for -0.0,
3778 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3779 // -0.0 is less than +0.0.
3780 const Value *LHS, *RHS;
3781 auto SPR = matchSelectPattern(&I, LHS, RHS);
3783 switch (SPR.Flavor) {
3784 case SPF_UMAX: Opc = ISD::UMAX; break;
3785 case SPF_UMIN: Opc = ISD::UMIN; break;
3786 case SPF_SMAX: Opc = ISD::SMAX; break;
3787 case SPF_SMIN: Opc = ISD::SMIN; break;
3788 case SPF_FMINNUM:
3789 switch (SPR.NaNBehavior) {
3790 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3791 case SPNB_RETURNS_NAN: break;
3792 case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break;
3793 case SPNB_RETURNS_ANY:
3795 (UseScalarMinMax &&
3797 Opc = ISD::FMINNUM;
3798 break;
3799 }
3800 break;
3801 case SPF_FMAXNUM:
3802 switch (SPR.NaNBehavior) {
3803 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3804 case SPNB_RETURNS_NAN: break;
3805 case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break;
3806 case SPNB_RETURNS_ANY:
3808 (UseScalarMinMax &&
3810 Opc = ISD::FMAXNUM;
3811 break;
3812 }
3813 break;
3814 case SPF_NABS:
3815 Negate = true;
3816 [[fallthrough]];
3817 case SPF_ABS:
3818 IsUnaryAbs = true;
3819 Opc = ISD::ABS;
3820 break;
3821 default: break;
3822 }
3823
3824 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3825 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3826 (UseScalarMinMax &&
3828 // If the underlying comparison instruction is used by any other
3829 // instruction, the consumed instructions won't be destroyed, so it is
3830 // not profitable to convert to a min/max.
3831 hasOnlySelectUsers(cast<SelectInst>(I).getCondition())) {
3832 OpCode = Opc;
3833 LHSVal = getValue(LHS);
3834 RHSVal = getValue(RHS);
3835 BaseOps.clear();
3836 }
3837
3838 if (IsUnaryAbs) {
3839 OpCode = Opc;
3840 LHSVal = getValue(LHS);
3841 BaseOps.clear();
3842 }
3843 }
3844
3845 if (IsUnaryAbs) {
3846 for (unsigned i = 0; i != NumValues; ++i) {
3847 SDLoc dl = getCurSDLoc();
3848 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3849 Values[i] =
3850 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3851 if (Negate)
3852 Values[i] = DAG.getNegative(Values[i], dl, VT);
3853 }
3854 } else {
3855 for (unsigned i = 0; i != NumValues; ++i) {
3856 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3857 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3858 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3859 Values[i] = DAG.getNode(
3860 OpCode, getCurSDLoc(),
3861 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3862 }
3863 }
3864
3866 DAG.getVTList(ValueVTs), Values));
3867}
3868
3869void SelectionDAGBuilder::visitTrunc(const User &I) {
3870 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3871 SDValue N = getValue(I.getOperand(0));
3873 I.getType());
3875 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3876 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
3877 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
3878 }
3879
3880 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
3881}
3882
3883void SelectionDAGBuilder::visitZExt(const User &I) {
3884 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3885 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3886 SDValue N = getValue(I.getOperand(0));
3887 auto &TLI = DAG.getTargetLoweringInfo();
3888 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3889
3891 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3892 Flags.setNonNeg(PNI->hasNonNeg());
3893
3894 // Eagerly use nonneg information to canonicalize towards sign_extend if
3895 // that is the target's preference.
3896 // TODO: Let the target do this later.
3897 if (Flags.hasNonNeg() &&
3898 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3900 return;
3901 }
3902
3903 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3904}
3905
3906void SelectionDAGBuilder::visitSExt(const User &I) {
3907 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3908 // SExt also can't be a cast to bool for same reason. So, nothing much to do
3909 SDValue N = getValue(I.getOperand(0));
3911 I.getType());
3913}
3914
3915void SelectionDAGBuilder::visitFPTrunc(const User &I) {
3916 // FPTrunc is never a no-op cast, no need to check
3917 SDValue N = getValue(I.getOperand(0));
3918 SDLoc dl = getCurSDLoc();
3920 if (auto *TruncInst = dyn_cast<FPMathOperator>(&I))
3921 Flags.copyFMF(*TruncInst);
3923 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3924 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
3926 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
3927 Flags));
3928}
3929
3930void SelectionDAGBuilder::visitFPExt(const User &I) {
3931 // FPExt is never a no-op cast, no need to check
3932 SDValue N = getValue(I.getOperand(0));
3934 I.getType());
3936}
3937
3938void SelectionDAGBuilder::visitFPToUI(const User &I) {
3939 // FPToUI is never a no-op cast, no need to check
3940 SDValue N = getValue(I.getOperand(0));
3942 I.getType());
3944}
3945
3946void SelectionDAGBuilder::visitFPToSI(const User &I) {
3947 // FPToSI is never a no-op cast, no need to check
3948 SDValue N = getValue(I.getOperand(0));
3950 I.getType());
3952}
3953
3954void SelectionDAGBuilder::visitUIToFP(const User &I) {
3955 // UIToFP is never a no-op cast, no need to check
3956 SDValue N = getValue(I.getOperand(0));
3958 I.getType());
3960 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3961 Flags.setNonNeg(PNI->hasNonNeg());
3962
3963 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
3964}
3965
3966void SelectionDAGBuilder::visitSIToFP(const User &I) {
3967 // SIToFP is never a no-op cast, no need to check
3968 SDValue N = getValue(I.getOperand(0));
3970 I.getType());
3972}
3973
3974void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
3975 // FIXME: this is not correct for pointers with addr width != pointer width
3976 visitPtrToInt(I);
3977}
3978
3979void SelectionDAGBuilder::visitPtrToInt(const User &I) {
3980 // What to do depends on the size of the integer and the size of the pointer.
3981 // We can either truncate, zero extend, or no-op, accordingly.
3982 SDValue N = getValue(I.getOperand(0));
3983 auto &TLI = DAG.getTargetLoweringInfo();
3985 I.getType());
3986 EVT PtrMemVT =
3987 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3988 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
3989 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
3990 setValue(&I, N);
3991}
3992
3993void SelectionDAGBuilder::visitIntToPtr(const User &I) {
3994 // What to do depends on the size of the integer and the size of the pointer.
3995 // We can either truncate, zero extend, or no-op, accordingly.
3996 SDValue N = getValue(I.getOperand(0));
3997 auto &TLI = DAG.getTargetLoweringInfo();
3998 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3999 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4000 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4001 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4002 setValue(&I, N);
4003}
4004
4005void SelectionDAGBuilder::visitBitCast(const User &I) {
4006 SDValue N = getValue(I.getOperand(0));
4007 SDLoc dl = getCurSDLoc();
4009 I.getType());
4010
4011 // BitCast assures us that source and destination are the same size so this is
4012 // either a BITCAST or a no-op.
4013 if (DestVT != N.getValueType())
4015 DestVT, N)); // convert types.
4016 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4017 // might fold any kind of constant expression to an integer constant and that
4018 // is not what we are looking for. Only recognize a bitcast of a genuine
4019 // constant integer as an opaque constant.
4020 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4021 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4022 /*isOpaque*/true));
4023 else
4024 setValue(&I, N); // noop cast.
4025}
4026
4027void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4029 const Value *SV = I.getOperand(0);
4030 SDValue N = getValue(SV);
4031 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4032
4033 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4034 unsigned DestAS = I.getType()->getPointerAddressSpace();
4035
4036 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4037 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4038
4039 setValue(&I, N);
4040}
4041
4042void SelectionDAGBuilder::visitInsertElement(const User &I) {
4044 SDValue InVec = getValue(I.getOperand(0));
4045 SDValue InVal = getValue(I.getOperand(1));
4046 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4049 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4050 InVec, InVal, InIdx));
4051}
4052
4053void SelectionDAGBuilder::visitExtractElement(const User &I) {
4055 SDValue InVec = getValue(I.getOperand(0));
4056 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4059 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4060 InVec, InIdx));
4061}
4062
4063void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4064 SDValue Src1 = getValue(I.getOperand(0));
4065 SDValue Src2 = getValue(I.getOperand(1));
4067 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4068 Mask = SVI->getShuffleMask();
4069 else
4070 Mask = cast<ConstantExpr>(I).getShuffleMask();
4071 SDLoc DL = getCurSDLoc();
4073 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4074 EVT SrcVT = Src1.getValueType();
4075
4076 if (all_of(Mask, [](int Elem) { return Elem == 0; }) &&
4077 VT.isScalableVector()) {
4078 // Canonical splat form of first element of first input vector.
4079 SDValue FirstElt =
4082 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4083 return;
4084 }
4085
4086 // For now, we only handle splats for scalable vectors.
4087 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4088 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4089 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4090
4091 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4092 unsigned MaskNumElts = Mask.size();
4093
4094 if (SrcNumElts == MaskNumElts) {
4095 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4096 return;
4097 }
4098
4099 // Normalize the shuffle vector since mask and vector length don't match.
4100 if (SrcNumElts < MaskNumElts) {
4101 // Mask is longer than the source vectors. We can use concatenate vector to
4102 // make the mask and vectors lengths match.
4103
4104 if (MaskNumElts % SrcNumElts == 0) {
4105 // Mask length is a multiple of the source vector length.
4106 // Check if the shuffle is some kind of concatenation of the input
4107 // vectors.
4108 unsigned NumConcat = MaskNumElts / SrcNumElts;
4109 bool IsConcat = true;
4110 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4111 for (unsigned i = 0; i != MaskNumElts; ++i) {
4112 int Idx = Mask[i];
4113 if (Idx < 0)
4114 continue;
4115 // Ensure the indices in each SrcVT sized piece are sequential and that
4116 // the same source is used for the whole piece.
4117 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4118 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4119 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4120 IsConcat = false;
4121 break;
4122 }
4123 // Remember which source this index came from.
4124 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4125 }
4126
4127 // The shuffle is concatenating multiple vectors together. Just emit
4128 // a CONCAT_VECTORS operation.
4129 if (IsConcat) {
4130 SmallVector<SDValue, 8> ConcatOps;
4131 for (auto Src : ConcatSrcs) {
4132 if (Src < 0)
4133 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4134 else if (Src == 0)
4135 ConcatOps.push_back(Src1);
4136 else
4137 ConcatOps.push_back(Src2);
4138 }
4139 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4140 return;
4141 }
4142 }
4143
4144 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4145 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4146 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4147 PaddedMaskNumElts);
4148
4149 // Pad both vectors with undefs to make them the same length as the mask.
4150 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4151
4152 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4153 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4154 MOps1[0] = Src1;
4155 MOps2[0] = Src2;
4156
4157 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4158 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4159
4160 // Readjust mask for new input vector length.
4161 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4162 for (unsigned i = 0; i != MaskNumElts; ++i) {
4163 int Idx = Mask[i];
4164 if (Idx >= (int)SrcNumElts)
4165 Idx -= SrcNumElts - PaddedMaskNumElts;
4166 MappedOps[i] = Idx;
4167 }
4168
4169 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4170
4171 // If the concatenated vector was padded, extract a subvector with the
4172 // correct number of elements.
4173 if (MaskNumElts != PaddedMaskNumElts)
4176
4177 setValue(&I, Result);
4178 return;
4179 }
4180
4181 assert(SrcNumElts > MaskNumElts);
4182
4183 // Analyze the access pattern of the vector to see if we can extract
4184 // two subvectors and do the shuffle.
4185 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4186 bool CanExtract = true;
4187 for (int Idx : Mask) {
4188 unsigned Input = 0;
4189 if (Idx < 0)
4190 continue;
4191
4192 if (Idx >= (int)SrcNumElts) {
4193 Input = 1;
4194 Idx -= SrcNumElts;
4195 }
4196
4197 // If all the indices come from the same MaskNumElts sized portion of
4198 // the sources we can use extract. Also make sure the extract wouldn't
4199 // extract past the end of the source.
4200 int NewStartIdx = alignDown(Idx, MaskNumElts);
4201 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4202 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4203 CanExtract = false;
4204 // Make sure we always update StartIdx as we use it to track if all
4205 // elements are undef.
4206 StartIdx[Input] = NewStartIdx;
4207 }
4208
4209 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4210 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4211 return;
4212 }
4213 if (CanExtract) {
4214 // Extract appropriate subvector and generate a vector shuffle
4215 for (unsigned Input = 0; Input < 2; ++Input) {
4216 SDValue &Src = Input == 0 ? Src1 : Src2;
4217 if (StartIdx[Input] < 0)
4218 Src = DAG.getUNDEF(VT);
4219 else {
4220 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4221 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4222 }
4223 }
4224
4225 // Calculate new mask.
4226 SmallVector<int, 8> MappedOps(Mask);
4227 for (int &Idx : MappedOps) {
4228 if (Idx >= (int)SrcNumElts)
4229 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4230 else if (Idx >= 0)
4231 Idx -= StartIdx[0];
4232 }
4233
4234 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4235 return;
4236 }
4237
4238 // We can't use either concat vectors or extract subvectors so fall back to
4239 // replacing the shuffle with extract and build vector.
4240 // to insert and build vector.
4241 EVT EltVT = VT.getVectorElementType();
4243 for (int Idx : Mask) {
4244 SDValue Res;
4245
4246 if (Idx < 0) {
4247 Res = DAG.getUNDEF(EltVT);
4248 } else {
4249 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4250 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4251
4252 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4254 }
4255
4256 Ops.push_back(Res);
4257 }
4258
4259 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4260}
4261
4262void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4263 ArrayRef<unsigned> Indices = I.getIndices();
4264 const Value *Op0 = I.getOperand(0);
4265 const Value *Op1 = I.getOperand(1);
4266 Type *AggTy = I.getType();
4267 Type *ValTy = Op1->getType();
4268 bool IntoUndef = isa<UndefValue>(Op0);
4269 bool FromUndef = isa<UndefValue>(Op1);
4270
4271 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4272
4274 SmallVector<EVT, 4> AggValueVTs;
4275 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4276 SmallVector<EVT, 4> ValValueVTs;
4277 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4278
4279 unsigned NumAggValues = AggValueVTs.size();
4280 unsigned NumValValues = ValValueVTs.size();
4281 SmallVector<SDValue, 4> Values(NumAggValues);
4282
4283 // Ignore an insertvalue that produces an empty object
4284 if (!NumAggValues) {
4285 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4286 return;
4287 }
4288
4289 SDValue Agg = getValue(Op0);
4290 unsigned i = 0;
4291 // Copy the beginning value(s) from the original aggregate.
4292 for (; i != LinearIndex; ++i)
4293 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4294 SDValue(Agg.getNode(), Agg.getResNo() + i);
4295 // Copy values from the inserted value(s).
4296 if (NumValValues) {
4297 SDValue Val = getValue(Op1);
4298 for (; i != LinearIndex + NumValValues; ++i)
4299 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4300 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4301 }
4302 // Copy remaining value(s) from the original aggregate.
4303 for (; i != NumAggValues; ++i)
4304 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4305 SDValue(Agg.getNode(), Agg.getResNo() + i);
4306
4308 DAG.getVTList(AggValueVTs), Values));
4309}
4310
4311void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4312 ArrayRef<unsigned> Indices = I.getIndices();
4313 const Value *Op0 = I.getOperand(0);
4314 Type *AggTy = Op0->getType();
4315 Type *ValTy = I.getType();
4316 bool OutOfUndef = isa<UndefValue>(Op0);
4317
4318 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4319
4321 SmallVector<EVT, 4> ValValueVTs;
4322 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4323
4324 unsigned NumValValues = ValValueVTs.size();
4325
4326 // Ignore a extractvalue that produces an empty object
4327 if (!NumValValues) {
4328 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4329 return;
4330 }
4331
4332 SmallVector<SDValue, 4> Values(NumValValues);
4333
4334 SDValue Agg = getValue(Op0);
4335 // Copy out the selected value(s).
4336 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4337 Values[i - LinearIndex] =
4338 OutOfUndef ?
4339 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4340 SDValue(Agg.getNode(), Agg.getResNo() + i);
4341
4343 DAG.getVTList(ValValueVTs), Values));
4344}
4345
4346void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4347 Value *Op0 = I.getOperand(0);
4348 // Note that the pointer operand may be a vector of pointers. Take the scalar
4349 // element which holds a pointer.
4350 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4351 SDValue N = getValue(Op0);
4352 SDLoc dl = getCurSDLoc();
4353 auto &TLI = DAG.getTargetLoweringInfo();
4354 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4355
4356 // For a vector GEP, keep the prefix scalar as long as possible, then
4357 // convert any scalars encountered after the first vector operand to vectors.
4358 bool IsVectorGEP = I.getType()->isVectorTy();
4359 ElementCount VectorElementCount =
4360 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4362
4363 for (gep_type_iterator GTI = gep_type_begin(&I), E = gep_type_end(&I);
4364 GTI != E; ++GTI) {
4365 const Value *Idx = GTI.getOperand();
4366 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4367 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4368 if (Field) {
4369 // N = N + Offset
4372
4373 // In an inbounds GEP with an offset that is nonnegative even when
4374 // interpreted as signed, assume there is no unsigned overflow.
4376 if (NW.hasNoUnsignedWrap() ||
4377 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4379
4381 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4382 }
4383 } else {
4384 // IdxSize is the width of the arithmetic according to IR semantics.
4385 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4386 // (and fix up the result later).
4387 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4388 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4389 TypeSize ElementSize =
4390 GTI.getSequentialElementStride(DAG.getDataLayout());
4391 // We intentionally mask away the high bits here; ElementSize may not
4392 // fit in IdxTy.
4393 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4394 /*isSigned=*/false, /*implicitTrunc=*/true);
4395 bool ElementScalable = ElementSize.isScalable();
4396
4397 // If this is a scalar constant or a splat vector of constants,
4398 // handle it quickly.
4399 const auto *C = dyn_cast<Constant>(Idx);
4400 if (C && isa<VectorType>(C->getType()))
4401 C = C->getSplatValue();
4402
4403 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4404 if (CI && CI->isZero())
4405 continue;
4406 if (CI && !ElementScalable) {
4407 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4409 SDValue OffsVal;
4410 if (N.getValueType().isVector())
4411 OffsVal = DAG.getConstant(
4412 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4413 else
4414 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4415
4416 // In an inbounds GEP with an offset that is nonnegative even when
4417 // interpreted as signed, assume there is no unsigned overflow.
4419 if (NW.hasNoUnsignedWrap() ||
4420 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4421 Flags.setNoUnsignedWrap(true);
4422
4423 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4424
4425 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4426 continue;
4427 }
4428
4429 // N = N + Idx * ElementMul;
4430 SDValue IdxN = getValue(Idx);
4431
4432 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4433 if (N.getValueType().isVector()) {
4435 VectorElementCount);
4436 IdxN = DAG.getSplat(VT, dl, IdxN);
4437 } else {
4438 EVT VT =
4439 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4440 N = DAG.getSplat(VT, dl, N);
4441 }
4442 }
4443
4444 // If the index is smaller or larger than intptr_t, truncate or extend
4445 // it.
4446 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4447
4448 SDNodeFlags ScaleFlags;
4449 // The multiplication of an index by the type size does not wrap the
4450 // pointer index type in a signed sense (mul nsw).
4452
4453 // The multiplication of an index by the type size does not wrap the
4454 // pointer index type in an unsigned sense (mul nuw).
4455 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4456
4457 if (ElementScalable) {
4458 EVT VScaleTy = N.getValueType().getScalarType();
4459 SDValue VScale = DAG.getNode(
4460 ISD::VSCALE, dl, VScaleTy,
4461 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4462 if (N.getValueType().isVector())
4463 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4464 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4465 ScaleFlags);
4466 } else {
4467 // If this is a multiply by a power of two, turn it into a shl
4468 // immediately. This is a very common case.
4469 if (ElementMul != 1) {
4470 if (ElementMul.isPowerOf2()) {
4471 unsigned Amt = ElementMul.logBase2();
4472 IdxN = DAG.getNode(ISD::SHL, dl, N.getValueType(), IdxN,
4473 DAG.getConstant(Amt, dl, IdxN.getValueType()),
4474 ScaleFlags);
4475 } else {
4476 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4477 IdxN.getValueType());
4478 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4479 ScaleFlags);
4480 }
4481 }
4482 }
4483
4484 // The successive addition of the current address, truncated to the
4485 // pointer index type and interpreted as an unsigned number, and each
4486 // offset, also interpreted as an unsigned number, does not wrap the
4487 // pointer index type (add nuw).
4488 SDNodeFlags AddFlags;
4489 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4490
4491 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4492 }
4493 }
4494
4495 if (IsVectorGEP && !N.getValueType().isVector()) {
4496 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4497 N = DAG.getSplat(VT, dl, N);
4498 }
4499
4500 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4501 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4502 if (IsVectorGEP) {
4503 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4504 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4505 }
4506
4507 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4508 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4509
4510 setValue(&I, N);
4511}
4512
4513void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4514 // If this is a fixed sized alloca in the entry block of the function,
4515 // allocate it statically on the stack.
4516 if (FuncInfo.StaticAllocaMap.count(&I))
4517 return; // getValue will auto-populate this.
4518
4519 SDLoc dl = getCurSDLoc();
4520 Type *Ty = I.getAllocatedType();
4522 auto &DL = DAG.getDataLayout();
4523 TypeSize TySize = DL.getTypeAllocSize(Ty);
4524 MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign());
4525
4526 SDValue AllocSize = getValue(I.getArraySize());
4527
4528 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4529 if (AllocSize.getValueType() != IntPtr)
4530 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4531
4532 if (TySize.isScalable())
4533 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4534 DAG.getVScale(dl, IntPtr,
4535 APInt(IntPtr.getScalarSizeInBits(),
4536 TySize.getKnownMinValue())));
4537 else {
4538 SDValue TySizeValue =
4540 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4541 DAG.getZExtOrTrunc(TySizeValue, dl, IntPtr));
4542 }
4543
4544 // Handle alignment. If the requested alignment is less than or equal to
4545 // the stack alignment, ignore it. If the size is greater than or equal to
4546 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4548 if (*Alignment <= StackAlign)
4549 Alignment = std::nullopt;
4550
4551 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4552 // Round the size of the allocation up to the stack alignment size
4553 // by add SA-1 to the size. This doesn't overflow because we're computing
4554 // an address inside an alloca.
4555 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4556 DAG.getConstant(StackAlignMask, dl, IntPtr),
4558
4559 // Mask out the low bits for alignment purposes.
4560 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4561 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4562
4563 SDValue Ops[] = {
4564 getRoot(), AllocSize,
4565 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4566 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4567 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4568 setValue(&I, DSA);
4569 DAG.setRoot(DSA.getValue(1));
4570
4572}
4573
4574static const MDNode *getRangeMetadata(const Instruction &I) {
4575 return I.getMetadata(LLVMContext::MD_range);
4576}
4577
4578static std::optional<ConstantRange> getRange(const Instruction &I) {
4579 if (const auto *CB = dyn_cast<CallBase>(&I))
4580 if (std::optional<ConstantRange> CR = CB->getRange())
4581 return CR;
4582 if (const MDNode *Range = getRangeMetadata(I))
4584 return std::nullopt;
4585}
4586
4587void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4588 if (I.isAtomic())
4589 return visitAtomicLoad(I);
4590
4592 const Value *SV = I.getOperand(0);
4593 if (TLI.supportSwiftError()) {
4594 // Swifterror values can come from either a function parameter with
4595 // swifterror attribute or an alloca with swifterror attribute.
4596 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4597 if (Arg->hasSwiftErrorAttr())
4598 return visitLoadFromSwiftError(I);
4599 }
4600
4601 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4602 if (Alloca->isSwiftError())
4603 return visitLoadFromSwiftError(I);
4604 }
4605 }
4606
4607 SDValue Ptr = getValue(SV);
4608
4609 Type *Ty = I.getType();
4610 SmallVector<EVT, 4> ValueVTs, MemVTs;
4612 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4613 unsigned NumValues = ValueVTs.size();
4614 if (NumValues == 0)
4615 return;
4616
4617 Align Alignment = I.getAlign();
4618 AAMDNodes AAInfo = I.getAAMetadata();
4619 const MDNode *Ranges = getRangeMetadata(I);
4620 bool isVolatile = I.isVolatile();
4621 MachineMemOperand::Flags MMOFlags =
4623
4624 SDValue Root;
4625 bool ConstantMemory = false;
4626 if (isVolatile)
4627 // Serialize volatile loads with other side effects.
4628 Root = getRoot();
4629 else if (NumValues > MaxParallelChains)
4630 Root = getMemoryRoot();
4631 else if (BatchAA &&
4633 SV,
4635 AAInfo))) {
4636 // Do not serialize (non-volatile) loads of constant memory with anything.
4637 Root = DAG.getEntryNode();
4638 ConstantMemory = true;
4640 } else {
4641 // Do not serialize non-volatile loads against each other.
4642 Root = DAG.getRoot();
4643 }
4644
4645 SDLoc dl = getCurSDLoc();
4646
4647 if (isVolatile)
4648 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4649
4650 SmallVector<SDValue, 4> Values(NumValues);
4651 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4652
4653 unsigned ChainI = 0;
4654 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4655 // Serializing loads here may result in excessive register pressure, and
4656 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4657 // could recover a bit by hoisting nodes upward in the chain by recognizing
4658 // they are side-effect free or do not alias. The optimizer should really
4659 // avoid this case by converting large object/array copies to llvm.memcpy
4660 // (MaxParallelChains should always remain as failsafe).
4661 if (ChainI == MaxParallelChains) {
4662 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4663 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4664 ArrayRef(Chains.data(), ChainI));
4665 Root = Chain;
4666 ChainI = 0;
4667 }
4668
4669 // TODO: MachinePointerInfo only supports a fixed length offset.
4670 MachinePointerInfo PtrInfo =
4671 !Offsets[i].isScalable() || Offsets[i].isZero()
4672 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4674
4675 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4676 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4677 MMOFlags, AAInfo, Ranges);
4678 Chains[ChainI] = L.getValue(1);
4679
4680 if (MemVTs[i] != ValueVTs[i])
4681 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4682
4683 Values[i] = L;
4684 }
4685
4686 if (!ConstantMemory) {
4687 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4688 ArrayRef(Chains.data(), ChainI));
4689 if (isVolatile)
4690 DAG.setRoot(Chain);
4691 else
4692 PendingLoads.push_back(Chain);
4693 }
4694
4696 DAG.getVTList(ValueVTs), Values));
4697}
4698
4699void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4701 "call visitStoreToSwiftError when backend supports swifterror");
4702
4703 SmallVector<EVT, 4> ValueVTs;
4705 const Value *SrcV = I.getOperand(0);
4707 SrcV->getType(), ValueVTs, &Offsets, 0);
4708 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4709 "expect a single EVT for swifterror");
4710
4711 SDValue Src = getValue(SrcV);
4712 // Create a virtual register, then update the virtual register.
4713 Register VReg =
4714 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4715 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4716 // Chain can be getRoot or getControlRoot.
4717 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4718 SDValue(Src.getNode(), Src.getResNo()));
4719 DAG.setRoot(CopyNode);
4720}
4721
4722void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4724 "call visitLoadFromSwiftError when backend supports swifterror");
4725
4726 assert(!I.isVolatile() &&
4727 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4728 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4729 "Support volatile, non temporal, invariant for load_from_swift_error");
4730
4731 const Value *SV = I.getOperand(0);
4732 Type *Ty = I.getType();
4733 assert(
4734 (!BatchAA ||
4737 I.getAAMetadata()))) &&
4738 "load_from_swift_error should not be constant memory");
4739
4740 SmallVector<EVT, 4> ValueVTs;
4743 ValueVTs, &Offsets, 0);
4744 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4745 "expect a single EVT for swifterror");
4746
4747 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4749 getRoot(), getCurSDLoc(),
4750 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4751
4752 setValue(&I, L);
4753}
4754
4755void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4756 if (I.isAtomic())
4757 return visitAtomicStore(I);
4758
4759 const Value *SrcV = I.getOperand(0);
4760 const Value *PtrV = I.getOperand(1);
4761
4763 if (TLI.supportSwiftError()) {
4764 // Swifterror values can come from either a function parameter with
4765 // swifterror attribute or an alloca with swifterror attribute.
4766 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4767 if (Arg->hasSwiftErrorAttr())
4768 return visitStoreToSwiftError(I);
4769 }
4770
4771 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4772 if (Alloca->isSwiftError())
4773 return visitStoreToSwiftError(I);
4774 }
4775 }
4776
4777 SmallVector<EVT, 4> ValueVTs, MemVTs;
4780 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4781 unsigned NumValues = ValueVTs.size();
4782 if (NumValues == 0)
4783 return;
4784
4785 // Get the lowered operands. Note that we do this after
4786 // checking if NumResults is zero, because with zero results
4787 // the operands won't have values in the map.
4788 SDValue Src = getValue(SrcV);
4789 SDValue Ptr = getValue(PtrV);
4790
4791 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4792 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4793 SDLoc dl = getCurSDLoc();
4794 Align Alignment = I.getAlign();
4795 AAMDNodes AAInfo = I.getAAMetadata();
4796
4797 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4798
4799 unsigned ChainI = 0;
4800 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4801 // See visitLoad comments.
4802 if (ChainI == MaxParallelChains) {
4803 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4804 ArrayRef(Chains.data(), ChainI));
4805 Root = Chain;
4806 ChainI = 0;
4807 }
4808
4809 // TODO: MachinePointerInfo only supports a fixed length offset.
4810 MachinePointerInfo PtrInfo =
4811 !Offsets[i].isScalable() || Offsets[i].isZero()
4812 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4814
4815 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4816 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4817 if (MemVTs[i] != ValueVTs[i])
4818 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4819 SDValue St =
4820 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4821 Chains[ChainI] = St;
4822 }
4823
4824 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4825 ArrayRef(Chains.data(), ChainI));
4826 setValue(&I, StoreNode);
4827 DAG.setRoot(StoreNode);
4828}
4829
4830void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4831 bool IsCompressing) {
4832 SDLoc sdl = getCurSDLoc();
4833
4834 auto getMaskedStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4835 Align &Alignment) {
4836 // llvm.masked.store.*(Src0, Ptr, alignment, Mask)
4837 Src0 = I.getArgOperand(0);
4838 Ptr = I.getArgOperand(1);
4839 Alignment = cast<ConstantInt>(I.getArgOperand(2))->getAlignValue();
4840 Mask = I.getArgOperand(3);
4841 };
4842 auto getCompressingStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4843 Align &Alignment) {
4844 // llvm.masked.compressstore.*(Src0, Ptr, Mask)
4845 Src0 = I.getArgOperand(0);
4846 Ptr = I.getArgOperand(1);
4847 Mask = I.getArgOperand(2);
4848 Alignment = I.getParamAlign(1).valueOrOne();
4849 };
4850
4851 Value *PtrOperand, *MaskOperand, *Src0Operand;
4852 Align Alignment;
4853 if (IsCompressing)
4854 getCompressingStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4855 else
4856 getMaskedStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4857
4858 SDValue Ptr = getValue(PtrOperand);
4859 SDValue Src0 = getValue(Src0Operand);
4860 SDValue Mask = getValue(MaskOperand);
4861 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4862
4863 EVT VT = Src0.getValueType();
4864
4865 auto MMOFlags = MachineMemOperand::MOStore;
4866 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4868
4870 MachinePointerInfo(PtrOperand), MMOFlags,
4871 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4872
4873 const auto &TLI = DAG.getTargetLoweringInfo();
4874 const auto &TTI =
4875 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
4876 SDValue StoreNode =
4877 !IsCompressing && TTI.hasConditionalLoadStoreForType(
4878 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4879 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4880 Mask)
4881 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4882 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4883 IsCompressing);
4884 DAG.setRoot(StoreNode);
4885 setValue(&I, StoreNode);
4886}
4887
4888// Get a uniform base for the Gather/Scatter intrinsic.
4889// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4890// We try to represent it as a base pointer + vector of indices.
4891// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4892// The first operand of the GEP may be a single pointer or a vector of pointers
4893// Example:
4894// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4895// or
4896// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4897// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4898//
4899// When the first GEP operand is a single pointer - it is the uniform base we
4900// are looking for. If first operand of the GEP is a splat vector - we
4901// extract the splat value and use it as a uniform base.
4902// In all other cases the function returns 'false'.
4903static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4904 SDValue &Scale, SelectionDAGBuilder *SDB,
4905 const BasicBlock *CurBB, uint64_t ElemSize) {
4906 SelectionDAG& DAG = SDB->DAG;
4907 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4908 const DataLayout &DL = DAG.getDataLayout();
4909
4910 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4911
4912 // Handle splat constant pointer.
4913 if (auto *C = dyn_cast<Constant>(Ptr)) {
4914 C = C->getSplatValue();
4915 if (!C)
4916 return false;
4917
4918 Base = SDB->getValue(C);
4919
4920 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
4921 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
4922 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
4923 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4924 return true;
4925 }
4926
4927 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
4928 if (!GEP || GEP->getParent() != CurBB)
4929 return false;
4930
4931 if (GEP->getNumOperands() != 2)
4932 return false;
4933
4934 const Value *BasePtr = GEP->getPointerOperand();
4935 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
4936
4937 // Make sure the base is scalar and the index is a vector.
4938 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
4939 return false;
4940
4941 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
4942 if (ScaleVal.isScalable())
4943 return false;
4944
4945 // Target may not support the required addressing mode.
4946 if (ScaleVal != 1 &&
4947 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
4948 return false;
4949
4950 Base = SDB->getValue(BasePtr);
4951 Index = SDB->getValue(IndexVal);
4952
4953 Scale =
4954 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4955 return true;
4956}
4957
4958void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
4959 SDLoc sdl = getCurSDLoc();
4960
4961 // llvm.masked.scatter.*(Src0, Ptrs, alignment, Mask)
4962 const Value *Ptr = I.getArgOperand(1);
4963 SDValue Src0 = getValue(I.getArgOperand(0));
4964 SDValue Mask = getValue(I.getArgOperand(3));
4965 EVT VT = Src0.getValueType();
4966 Align Alignment = cast<ConstantInt>(I.getArgOperand(2))
4967 ->getMaybeAlignValue()
4968 .value_or(DAG.getEVTAlign(VT.getScalarType()));
4970
4971 SDValue Base;
4972 SDValue Index;
4973 SDValue Scale;
4974 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
4975 I.getParent(), VT.getScalarStoreSize());
4976
4977 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
4980 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4981 if (!UniformBase) {
4983 Index = getValue(Ptr);
4984 Scale =
4986 }
4987
4988 EVT IdxVT = Index.getValueType();
4989 EVT EltTy = IdxVT.getVectorElementType();
4990 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
4991 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
4992 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
4993 }
4994
4995 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
4996 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
4997 Ops, MMO, ISD::SIGNED_SCALED, false);
4998 DAG.setRoot(Scatter);
4999 setValue(&I, Scatter);
5000}
5001
5002void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5003 SDLoc sdl = getCurSDLoc();
5004
5005 auto getMaskedLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
5006 Align &Alignment) {
5007 // @llvm.masked.load.*(Ptr, alignment, Mask, Src0)
5008 Ptr = I.getArgOperand(0);
5009 Alignment = cast<ConstantInt>(I.getArgOperand(1))->getAlignValue();
5010 Mask = I.getArgOperand(2);
5011 Src0 = I.getArgOperand(3);
5012 };
5013 auto getExpandingLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
5014 Align &Alignment) {
5015 // @llvm.masked.expandload.*(Ptr, Mask, Src0)
5016 Ptr = I.getArgOperand(0);
5017 Alignment = I.getParamAlign(0).valueOrOne();
5018 Mask = I.getArgOperand(1);
5019 Src0 = I.getArgOperand(2);
5020 };
5021
5022 Value *PtrOperand, *MaskOperand, *Src0Operand;
5023 Align Alignment;
5024 if (IsExpanding)
5025 getExpandingLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
5026 else
5027 getMaskedLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
5028
5029 SDValue Ptr = getValue(PtrOperand);
5030 SDValue Src0 = getValue(Src0Operand);
5031 SDValue Mask = getValue(MaskOperand);
5032 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5033
5034 EVT VT = Src0.getValueType();
5035 AAMDNodes AAInfo = I.getAAMetadata();
5036 const MDNode *Ranges = getRangeMetadata(I);
5037
5038 // Do not serialize masked loads of constant memory with anything.
5039 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5040 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5041
5042 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5043
5044 auto MMOFlags = MachineMemOperand::MOLoad;
5045 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5047
5049 MachinePointerInfo(PtrOperand), MMOFlags,
5050 LocationSize::beforeOrAfterPointer(), Alignment, AAInfo, Ranges);
5051
5052 const auto &TLI = DAG.getTargetLoweringInfo();
5053 const auto &TTI =
5054 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
5055 // The Load/Res may point to different values and both of them are output
5056 // variables.
5057 SDValue Load;
5058 SDValue Res;
5059 if (!IsExpanding && TTI.hasConditionalLoadStoreForType(Src0Operand->getType(),
5060 /*IsStore=*/false))
5061 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5062 else
5063 Res = Load =
5064 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5065 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5066 if (AddToChain)
5067 PendingLoads.push_back(Load.getValue(1));
5068 setValue(&I, Res);
5069}
5070
5071void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5072 SDLoc sdl = getCurSDLoc();
5073
5074 // @llvm.masked.gather.*(Ptrs, alignment, Mask, Src0)
5075 const Value *Ptr = I.getArgOperand(0);
5076 SDValue Src0 = getValue(I.getArgOperand(3));
5077 SDValue Mask = getValue(I.getArgOperand(2));
5078
5080 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5081 Align Alignment = cast<ConstantInt>(I.getArgOperand(1))
5082 ->getMaybeAlignValue()
5083 .value_or(DAG.getEVTAlign(VT.getScalarType()));
5084
5085 const MDNode *Ranges = getRangeMetadata(I);
5086
5087 SDValue Root = DAG.getRoot();
5088 SDValue Base;
5089 SDValue Index;
5090 SDValue Scale;
5091 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5092 I.getParent(), VT.getScalarStoreSize());
5093 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5096 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5097 Ranges);
5098
5099 if (!UniformBase) {
5101 Index = getValue(Ptr);
5102 Scale =
5104 }
5105
5106 EVT IdxVT = Index.getValueType();
5107 EVT EltTy = IdxVT.getVectorElementType();
5108 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5109 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5110 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5111 }
5112
5113 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5114 SDValue Gather =
5115 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5117
5118 PendingLoads.push_back(Gather.getValue(1));
5119 setValue(&I, Gather);
5120}
5121
5122void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5123 SDLoc dl = getCurSDLoc();
5124 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5125 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5126 SyncScope::ID SSID = I.getSyncScopeID();
5127
5128 SDValue InChain = getRoot();
5129
5130 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5131 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5132
5135
5138 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5139 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5140 FailureOrdering);
5141
5143 dl, MemVT, VTs, InChain,
5144 getValue(I.getPointerOperand()),
5145 getValue(I.getCompareOperand()),
5146 getValue(I.getNewValOperand()), MMO);
5147
5148 SDValue OutChain = L.getValue(2);
5149
5150 setValue(&I, L);
5151 DAG.setRoot(OutChain);
5152}
5153
5154void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5155 SDLoc dl = getCurSDLoc();
5157 switch (I.getOperation()) {
5158 default: llvm_unreachable("Unknown atomicrmw operation");
5176 break;
5179 break;
5182 break;
5185 break;
5188 break;
5191 break;
5192 }
5193 AtomicOrdering Ordering = I.getOrdering();
5194 SyncScope::ID SSID = I.getSyncScopeID();
5195
5196 SDValue InChain = getRoot();
5197
5198 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5201
5204 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5205 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
5206
5207 SDValue L =
5208 DAG.getAtomic(NT, dl, MemVT, InChain,
5209 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5210 MMO);
5211
5212 SDValue OutChain = L.getValue(1);
5213
5214 setValue(&I, L);
5215 DAG.setRoot(OutChain);
5216}
5217
5218void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5219 SDLoc dl = getCurSDLoc();
5221 SDValue Ops[3];
5222 Ops[0] = getRoot();
5223 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5225 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5227 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5228 setValue(&I, N);
5229 DAG.setRoot(N);
5230}
5231
5232void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5233 SDLoc dl = getCurSDLoc();
5234 AtomicOrdering Order = I.getOrdering();
5235 SyncScope::ID SSID = I.getSyncScopeID();
5236
5237 SDValue InChain = getRoot();
5238
5240 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5241 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5242
5243 if (!TLI.supportsUnalignedAtomics() &&
5244 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5245 report_fatal_error("Cannot generate unaligned atomic load");
5246
5248
5249 const MDNode *Ranges = getRangeMetadata(I);
5251 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5252 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5253
5254 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5255
5256 SDValue Ptr = getValue(I.getPointerOperand());
5257 SDValue L =
5258 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5259
5260 SDValue OutChain = L.getValue(1);
5261 if (MemVT != VT)
5262 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5263
5264 setValue(&I, L);
5265 DAG.setRoot(OutChain);
5266}
5267
5268void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5269 SDLoc dl = getCurSDLoc();
5270
5271 AtomicOrdering Ordering = I.getOrdering();
5272 SyncScope::ID SSID = I.getSyncScopeID();
5273
5274 SDValue InChain = getRoot();
5275
5277 EVT MemVT =
5278 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5279
5280 if (!TLI.supportsUnalignedAtomics() &&
5281 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5282 report_fatal_error("Cannot generate unaligned atomic store");
5283
5285
5288 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5289 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5290
5291 SDValue Val = getValue(I.getValueOperand());
5292 if (Val.getValueType() != MemVT)
5293 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5294 SDValue Ptr = getValue(I.getPointerOperand());
5295
5296 SDValue OutChain =
5297 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5298
5299 setValue(&I, OutChain);
5300 DAG.setRoot(OutChain);
5301}
5302
5303/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5304/// node.
5305void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5306 unsigned Intrinsic) {
5307 // Ignore the callsite's attributes. A specific call site may be marked with
5308 // readnone, but the lowering code will expect the chain based on the
5309 // definition.
5310 const Function *F = I.getCalledFunction();
5311 bool HasChain = !F->doesNotAccessMemory();
5312 bool OnlyLoad =
5313 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5314
5315 // Build the operand list.
5317 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5318 if (OnlyLoad) {
5319 // We don't need to serialize loads against other loads.
5320 Ops.push_back(DAG.getRoot());
5321 } else {
5322 Ops.push_back(getRoot());
5323 }
5324 }
5325
5326 // Info is set by getTgtMemIntrinsic
5329 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I,
5331 Intrinsic);
5332
5333 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5334 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
5336 Ops.push_back(DAG.getTargetConstant(Intrinsic, getCurSDLoc(),
5338
5339 // Add all operands of the call to the operand list.
5340 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5341 const Value *Arg = I.getArgOperand(i);
5342 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5343 Ops.push_back(getValue(Arg));
5344 continue;
5345 }
5346
5347 // Use TargetConstant instead of a regular constant for immarg.
5348 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5349 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5350 assert(CI->getBitWidth() <= 64 &&
5351 "large intrinsic immediates not handled");
5352 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5353 } else {
5354 Ops.push_back(
5355 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5356 }
5357 }
5358
5359 SmallVector<EVT, 4> ValueVTs;
5360 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5361
5362 if (HasChain)
5363 ValueVTs.push_back(MVT::Other);
5364
5365 SDVTList VTs = DAG.getVTList(ValueVTs);
5366
5367 // Propagate fast-math-flags from IR to node(s).
5369 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5370 Flags.copyFMF(*FPMO);
5371 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5372
5373 // Create the node.
5375
5376 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5377 auto *Token = Bundle->Inputs[0].get();
5378 SDValue ConvControlToken = getValue(Token);
5379 assert(Ops.back().getValueType() != MVT::Glue &&
5380 "Did not expected another glue node here.");
5381 ConvControlToken =
5382 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5383 Ops.push_back(ConvControlToken);
5384 }
5385
5386 // In some cases, custom collection of operands from CallInst I may be needed.
5388 if (IsTgtIntrinsic) {
5389 // This is target intrinsic that touches memory
5390 //
5391 // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic
5392 // didn't yield anything useful.
5394 if (Info.ptrVal)
5395 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5396 else if (Info.fallbackAddressSpace)
5397 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5398 EVT MemVT = Info.memVT;
5400 if (Size.hasValue() && !Size.getValue())
5402 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5404 MPI, Info.flags, Size, Alignment, I.getAAMetadata(), /*Ranges=*/nullptr,
5405 Info.ssid, Info.order, Info.failureOrder);
5406 Result =
5407 DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops, MemVT, MMO);
5408 } else if (!HasChain) {
5410 } else if (!I.getType()->isVoidTy()) {
5412 } else {
5414 }
5415
5416 if (HasChain) {
5417 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
5418 if (OnlyLoad)
5419 PendingLoads.push_back(Chain);
5420 else
5421 DAG.setRoot(Chain);
5422 }
5423
5424 if (!I.getType()->isVoidTy()) {
5425 if (!isa<VectorType>(I.getType()))
5426 Result = lowerRangeToAssertZExt(DAG, I, Result);
5427
5428 MaybeAlign Alignment = I.getRetAlign();
5429
5430 // Insert `assertalign` node if there's an alignment.
5431 if (InsertAssertAlign && Alignment) {
5432 Result =
5433 DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5434 }
5435 }
5436
5437 setValue(&I, Result);
5438}
5439
5440/// GetSignificand - Get the significand and build it into a floating-point
5441/// number with exponent of 1:
5442///
5443/// Op = (Op & 0x007fffff) | 0x3f800000;
5444///
5445/// where Op is the hexadecimal representation of floating point value.
5447 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5448 DAG.getConstant(0x007fffff, dl, MVT::i32));
5449 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5450 DAG.getConstant(0x3f800000, dl, MVT::i32));
5451 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5452}
5453
5454/// GetExponent - Get the exponent:
5455///
5456/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5457///
5458/// where Op is the hexadecimal representation of floating point value.
5460 const TargetLowering &TLI, const SDLoc &dl) {
5461 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5462 DAG.getConstant(0x7f800000, dl, MVT::i32));
5463 SDValue t1 = DAG.getNode(
5464 ISD::SRL, dl, MVT::i32, t0,
5465 DAG.getConstant(23, dl,
5466 TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
5467 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5468 DAG.getConstant(127, dl, MVT::i32));
5469 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5470}
5471
5472/// getF32Constant - Get 32-bit floating point constant.
5474 const SDLoc &dl) {
5475 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5476 MVT::f32);
5477}
5478
5480 SelectionDAG &DAG) {
5481 // TODO: What fast-math-flags should be set on the floating-point nodes?
5482
5483 // IntegerPartOfX = ((int32_t)(t0);
5484 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5485
5486 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5487 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5488 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5489
5490 // IntegerPartOfX <<= 23;
5491 IntegerPartOfX =
5492 DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5493 DAG.getConstant(23, dl,
5495 MVT::i32, DAG.getDataLayout())));
5496
5497 SDValue TwoToFractionalPartOfX;
5498 if (LimitFloatPrecision <= 6) {
5499 // For floating-point precision of 6:
5500 //
5501 // TwoToFractionalPartOfX =
5502 // 0.997535578f +
5503 // (0.735607626f + 0.252464424f * x) * x;
5504 //
5505 // error 0.0144103317, which is 6 bits
5506 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5507 getF32Constant(DAG, 0x3e814304, dl));
5508 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5509 getF32Constant(DAG, 0x3f3c50c8, dl));
5510 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5511 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5512 getF32Constant(DAG, 0x3f7f5e7e, dl));
5513 } else if (LimitFloatPrecision <= 12) {
5514 // For floating-point precision of 12:
5515 //
5516 // TwoToFractionalPartOfX =
5517 // 0.999892986f +
5518 // (0.696457318f +
5519 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5520 //
5521 // error 0.000107046256, which is 13 to 14 bits
5522 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5523 getF32Constant(DAG, 0x3da235e3, dl));
5524 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5525 getF32Constant(DAG, 0x3e65b8f3, dl));
5526 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5527 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5528 getF32Constant(DAG, 0x3f324b07, dl));
5529 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5530 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5531 getF32Constant(DAG, 0x3f7ff8fd, dl));
5532 } else { // LimitFloatPrecision <= 18
5533 // For floating-point precision of 18:
5534 //
5535 // TwoToFractionalPartOfX =
5536 // 0.999999982f +
5537 // (0.693148872f +
5538 // (0.240227044f +
5539 // (0.554906021e-1f +
5540 // (0.961591928e-2f +
5541 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5542 // error 2.47208000*10^(-7), which is better than 18 bits
5543 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5544 getF32Constant(DAG, 0x3924b03e, dl));
5545 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5546 getF32Constant(DAG, 0x3ab24b87, dl));
5547 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5548 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5549 getF32Constant(DAG, 0x3c1d8c17, dl));
5550 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5551 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5552 getF32Constant(DAG, 0x3d634a1d, dl));
5553 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5554 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5555 getF32Constant(DAG, 0x3e75fe14, dl));
5556 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5557 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5558 getF32Constant(DAG, 0x3f317234, dl));
5559 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5560 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5561 getF32Constant(DAG, 0x3f800000, dl));
5562 }
5563
5564 // Add the exponent into the result in integer domain.
5565 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5566 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5567 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5568}
5569
5570/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5571/// limited-precision mode.
5573 const TargetLowering &TLI, SDNodeFlags Flags) {
5574 if (Op.getValueType() == MVT::f32 &&
5576
5577 // Put the exponent in the right bit position for later addition to the
5578 // final result:
5579 //
5580 // t0 = Op * log2(e)
5581
5582 // TODO: What fast-math-flags should be set here?
5583 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5584 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5585 return getLimitedPrecisionExp2(t0, dl, DAG);
5586 }
5587
5588 // No special expansion.
5589 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5590}
5591
5592/// expandLog - Lower a log intrinsic. Handles the special sequences for
5593/// limited-precision mode.
5595 const TargetLowering &TLI, SDNodeFlags Flags) {
5596 // TODO: What fast-math-flags should be set on the floating-point nodes?
5597
5598 if (Op.getValueType() == MVT::f32 &&
5600 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5601
5602 // Scale the exponent by log(2).
5603 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5604 SDValue LogOfExponent =
5605 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5606 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5607
5608 // Get the significand and build it into a floating-point number with
5609 // exponent of 1.
5610 SDValue X = GetSignificand(DAG, Op1, dl);
5611
5612 SDValue LogOfMantissa;
5613 if (LimitFloatPrecision <= 6) {
5614 // For floating-point precision of 6:
5615 //
5616 // LogofMantissa =
5617 // -1.1609546f +
5618 // (1.4034025f - 0.23903021f * x) * x;
5619 //
5620 // error 0.0034276066, which is better than 8 bits
5621 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5622 getF32Constant(DAG, 0xbe74c456, dl));
5623 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5624 getF32Constant(DAG, 0x3fb3a2b1, dl));
5625 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5626 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5627 getF32Constant(DAG, 0x3f949a29, dl));
5628 } else if (LimitFloatPrecision <= 12) {
5629 // For floating-point precision of 12:
5630 //
5631 // LogOfMantissa =
5632 // -1.7417939f +
5633 // (2.8212026f +
5634 // (-1.4699568f +
5635 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5636 //
5637 // error 0.000061011436, which is 14 bits
5638 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5639 getF32Constant(DAG, 0xbd67b6d6, dl));
5640 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5641 getF32Constant(DAG, 0x3ee4f4b8, dl));
5642 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5643 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5644 getF32Constant(DAG, 0x3fbc278b, dl));
5645 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5646 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5647 getF32Constant(DAG, 0x40348e95, dl));
5648 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5649 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5650 getF32Constant(DAG, 0x3fdef31a, dl));
5651 } else { // LimitFloatPrecision <= 18
5652 // For floating-point precision of 18:
5653 //
5654 // LogOfMantissa =
5655 // -2.1072184f +
5656 // (4.2372794f +
5657 // (-3.7029485f +
5658 // (2.2781945f +
5659 // (-0.87823314f +
5660 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5661 //
5662 // error 0.0000023660568, which is better than 18 bits
5663 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5664 getF32Constant(DAG, 0xbc91e5ac, dl));
5665 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5666 getF32Constant(DAG, 0x3e4350aa, dl));
5667 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5668 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5669 getF32Constant(DAG, 0x3f60d3e3, dl));
5670 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5671 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5672 getF32Constant(DAG, 0x4011cdf0, dl));
5673 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5674 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5675 getF32Constant(DAG, 0x406cfd1c, dl));
5676 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5677 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5678 getF32Constant(DAG, 0x408797cb, dl));
5679 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5680 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5681 getF32Constant(DAG, 0x4006dcab, dl));
5682 }
5683
5684 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5685 }
5686
5687 // No special expansion.
5688 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5689}
5690
5691/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5692/// limited-precision mode.
5694 const TargetLowering &TLI, SDNodeFlags Flags) {
5695 // TODO: What fast-math-flags should be set on the floating-point nodes?
5696
5697 if (Op.getValueType() == MVT::f32 &&
5699 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5700
5701 // Get the exponent.
5702 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5703
5704 // Get the significand and build it into a floating-point number with
5705 // exponent of 1.
5706 SDValue X = GetSignificand(DAG, Op1, dl);
5707
5708 // Different possible minimax approximations of significand in
5709 // floating-point for various degrees of accuracy over [1,2].
5710 SDValue Log2ofMantissa;
5711 if (LimitFloatPrecision <= 6) {
5712 // For floating-point precision of 6:
5713 //
5714 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5715 //
5716 // error 0.0049451742, which is more than 7 bits
5717 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5718 getF32Constant(DAG, 0xbeb08fe0, dl));
5719 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5720 getF32Constant(DAG, 0x40019463, dl));
5721 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5722 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5723 getF32Constant(DAG, 0x3fd6633d, dl));
5724 } else if (LimitFloatPrecision <= 12) {
5725 // For floating-point precision of 12:
5726 //
5727 // Log2ofMantissa =
5728 // -2.51285454f +
5729 // (4.07009056f +
5730 // (-2.12067489f +
5731 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5732 //
5733 // error 0.0000876136000, which is better than 13 bits
5734 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5735 getF32Constant(DAG, 0xbda7262e, dl));
5736 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5737 getF32Constant(DAG, 0x3f25280b, dl));
5738 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5739 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5740 getF32Constant(DAG, 0x4007b923, dl));
5741 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5742 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5743 getF32Constant(DAG, 0x40823e2f, dl));
5744 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5745 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5746 getF32Constant(DAG, 0x4020d29c, dl));
5747 } else { // LimitFloatPrecision <= 18
5748 // For floating-point precision of 18:
5749 //
5750 // Log2ofMantissa =
5751 // -3.0400495f +
5752 // (6.1129976f +
5753 // (-5.3420409f +
5754 // (3.2865683f +
5755 // (-1.2669343f +
5756 // (0.27515199f -
5757 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5758 //
5759 // error 0.0000018516, which is better than 18 bits
5760 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5761 getF32Constant(DAG, 0xbcd2769e, dl));
5762 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5763 getF32Constant(DAG, 0x3e8ce0b9, dl));
5764 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5765 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5766 getF32Constant(DAG, 0x3fa22ae7, dl));
5767 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5768 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5769 getF32Constant(DAG, 0x40525723, dl));
5770 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5771 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5772 getF32Constant(DAG, 0x40aaf200, dl));
5773 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5774 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5775 getF32Constant(DAG, 0x40c39dad, dl));
5776 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5777 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5778 getF32Constant(DAG, 0x4042902c, dl));
5779 }
5780
5781 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5782 }
5783
5784 // No special expansion.
5785 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5786}
5787
5788/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5789/// limited-precision mode.
5791 const TargetLowering &TLI, SDNodeFlags Flags) {
5792 // TODO: What fast-math-flags should be set on the floating-point nodes?
5793
5794 if (Op.getValueType() == MVT::f32 &&
5796 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5797
5798 // Scale the exponent by log10(2) [0.30102999f].
5799 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5800 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5801 getF32Constant(DAG, 0x3e9a209a, dl));
5802
5803 // Get the significand and build it into a floating-point number with
5804 // exponent of 1.
5805 SDValue X = GetSignificand(DAG, Op1, dl);
5806
5807 SDValue Log10ofMantissa;
5808 if (LimitFloatPrecision <= 6) {
5809 // For floating-point precision of 6:
5810 //
5811 // Log10ofMantissa =
5812 // -0.50419619f +
5813 // (0.60948995f - 0.10380950f * x) * x;
5814 //
5815 // error 0.0014886165, which is 6 bits
5816 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5817 getF32Constant(DAG, 0xbdd49a13, dl));
5818 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5819 getF32Constant(DAG, 0x3f1c0789, dl));
5820 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5821 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5822 getF32Constant(DAG, 0x3f011300, dl));
5823 } else if (LimitFloatPrecision <= 12) {
5824 // For floating-point precision of 12:
5825 //
5826 // Log10ofMantissa =
5827 // -0.64831180f +
5828 // (0.91751397f +
5829 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5830 //
5831 // error 0.00019228036, which is better than 12 bits
5832 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5833 getF32Constant(DAG, 0x3d431f31, dl));
5834 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5835 getF32Constant(DAG, 0x3ea21fb2, dl));
5836 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5837 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5838 getF32Constant(DAG, 0x3f6ae232, dl));
5839 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5840 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5841 getF32Constant(DAG, 0x3f25f7c3, dl));
5842 } else { // LimitFloatPrecision <= 18
5843 // For floating-point precision of 18:
5844 //
5845 // Log10ofMantissa =
5846 // -0.84299375f +
5847 // (1.5327582f +
5848 // (-1.0688956f +
5849 // (0.49102474f +
5850 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5851 //
5852 // error 0.0000037995730, which is better than 18 bits
5853 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5854 getF32Constant(DAG, 0x3c5d51ce, dl));
5855 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5856 getF32Constant(DAG, 0x3e00685a, dl));
5857 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5858 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5859 getF32Constant(DAG, 0x3efb6798, dl));
5860 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5861 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5862 getF32Constant(DAG, 0x3f88d192, dl));
5863 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5864 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5865 getF32Constant(DAG, 0x3fc4316c, dl));
5866 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5867 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5868 getF32Constant(DAG, 0x3f57ce70, dl));
5869 }
5870
5871 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5872 }
5873
5874 // No special expansion.
5875 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5876}
5877
5878/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5879/// limited-precision mode.
5881 const TargetLowering &TLI, SDNodeFlags Flags) {
5882 if (Op.getValueType() == MVT::f32 &&
5884 return getLimitedPrecisionExp2(Op, dl, DAG);
5885
5886 // No special expansion.
5887 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
5888}
5889
5890/// visitPow - Lower a pow intrinsic. Handles the special sequences for
5891/// limited-precision mode with x == 10.0f.
5892static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS,
5893 SelectionDAG &DAG, const TargetLowering &TLI,
5894 SDNodeFlags Flags) {
5895 bool IsExp10 = false;
5896 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
5898 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
5899 APFloat Ten(10.0f);
5900 IsExp10 = LHSC->isExactlyValue(Ten);
5901 }
5902 }
5903
5904 // TODO: What fast-math-flags should be set on the FMUL node?
5905 if (IsExp10) {
5906 // Put the exponent in the right bit position for later addition to the
5907 // final result:
5908 //
5909 // #define LOG2OF10 3.3219281f
5910 // t0 = Op * LOG2OF10;
5911 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
5912 getF32Constant(DAG, 0x40549a78, dl));
5913 return getLimitedPrecisionExp2(t0, dl, DAG);
5914 }
5915
5916 // No special expansion.
5917 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
5918}
5919
5920/// ExpandPowI - Expand a llvm.powi intrinsic.
5921static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS,
5922 SelectionDAG &DAG) {
5923 // If RHS is a constant, we can expand this out to a multiplication tree if
5924 // it's beneficial on the target, otherwise we end up lowering to a call to
5925 // __powidf2 (for example).
5926 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
5927 unsigned Val = RHSC->getSExtValue();
5928
5929 // powi(x, 0) -> 1.0
5930 if (Val == 0)
5931 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
5932
5934 Val, DAG.shouldOptForSize())) {
5935 // Get the exponent as a positive value.
5936 if ((int)Val < 0)
5937 Val = -Val;
5938 // We use the simple binary decomposition method to generate the multiply
5939 // sequence. There are more optimal ways to do this (for example,
5940 // powi(x,15) generates one more multiply than it should), but this has
5941 // the benefit of being both really simple and much better than a libcall.
5942 SDValue Res; // Logically starts equal to 1.0
5943 SDValue CurSquare = LHS;
5944 // TODO: Intrinsics should have fast-math-flags that propagate to these
5945 // nodes.
5946 while (Val) {
5947 if (Val & 1) {
5948 if (Res.getNode())
5949 Res =
5950 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
5951 else
5952 Res = CurSquare; // 1.0*CurSquare.
5953 }
5954
5955 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
5956 CurSquare, CurSquare);
5957 Val >>= 1;
5958 }
5959
5960 // If the original was negative, invert the result, producing 1/(x*x*x).
5961 if (RHSC->getSExtValue() < 0)
5962 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
5963 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
5964 return Res;
5965 }
5966 }
5967
5968 // Otherwise, expand to a libcall.
5969 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
5970}
5971
5972static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
5973 SDValue LHS, SDValue RHS, SDValue Scale,
5974 SelectionDAG &DAG, const TargetLowering &TLI) {
5975 EVT VT = LHS.getValueType();
5976 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
5977 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
5978 LLVMContext &Ctx = *DAG.getContext();
5979
5980 // If the type is legal but the operation isn't, this node might survive all
5981 // the way to operation legalization. If we end up there and we do not have
5982 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
5983 // node.
5984
5985 // Coax the legalizer into expanding the node during type legalization instead
5986 // by bumping the size by one bit. This will force it to Promote, enabling the
5987 // early expansion and avoiding the need to expand later.
5988
5989 // We don't have to do this if Scale is 0; that can always be expanded, unless
5990 // it's a saturating signed operation. Those can experience true integer
5991 // division overflow, a case which we must avoid.
5992
5993 // FIXME: We wouldn't have to do this (or any of the early
5994 // expansion/promotion) if it was possible to expand a libcall of an
5995 // illegal type during operation legalization. But it's not, so things
5996 // get a bit hacky.
5997 unsigned ScaleInt = Scale->getAsZExtVal();
5998 if ((ScaleInt > 0 || (Saturating && Signed)) &&
5999 (TLI.isTypeLegal(VT) ||
6000 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6002 Opcode, VT, ScaleInt);
6003 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6004 EVT PromVT;
6005 if (VT.isScalarInteger())
6006 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6007 else if (VT.isVector()) {
6008 PromVT = VT.getVectorElementType();
6009 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6010 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6011 } else
6012 llvm_unreachable("Wrong VT for DIVFIX?");
6013 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6014 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6015 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6016 // For saturating operations, we need to shift up the LHS to get the
6017 // proper saturation width, and then shift down again afterwards.
6018 if (Saturating)
6019 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6020 DAG.getConstant(1, DL, ShiftTy));
6021 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6022 if (Saturating)
6023 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6024 DAG.getConstant(1, DL, ShiftTy));
6025 return DAG.getZExtOrTrunc(Res, DL, VT);
6026 }
6027 }
6028
6029 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6030}
6031
6032// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6033// bitcasted, or split argument. Returns a list of <Register, size in bits>
6034static void
6035getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6036 const SDValue &N) {
6037 switch (N.getOpcode()) {
6038 case ISD::CopyFromReg: {
6039 SDValue Op = N.getOperand(1);
6040 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6041 Op.getValueType().getSizeInBits());
6042 return;
6043 }
6044 case ISD::BITCAST:
6045 case ISD::AssertZext:
6046 case ISD::AssertSext:
6047 case ISD::TRUNCATE:
6048 getUnderlyingArgRegs(Regs, N.getOperand(0));
6049 return;
6050 case ISD::BUILD_PAIR:
6051 case ISD::BUILD_VECTOR:
6053 for (SDValue Op : N->op_values())
6054 getUnderlyingArgRegs(Regs, Op);
6055 return;
6056 default:
6057 return;
6058 }
6059}
6060
6061/// If the DbgValueInst is a dbg_value of a function argument, create the
6062/// corresponding DBG_VALUE machine instruction for it now. At the end of
6063/// instruction selection, they will be inserted to the entry BB.
6064/// We don't currently support this for variadic dbg_values, as they shouldn't
6065/// appear for function arguments or in the prologue.
6066bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6067 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6068 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6069 const Argument *Arg = dyn_cast<Argument>(V);
6070 if (!Arg)
6071 return false;
6072
6075
6076 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6077 // we've been asked to pursue.
6078 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6079 bool Indirect) {
6080 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6081 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6082 // pointing at the VReg, which will be patched up later.
6083 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6085 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6086 /* isKill */ false, /* isDead */ false,
6087 /* isUndef */ false, /* isEarlyClobber */ false,
6088 /* SubReg */ 0, /* isDebug */ true)});
6089
6090 auto *NewDIExpr = FragExpr;
6091 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6092 // the DIExpression.
6093 if (Indirect)
6094 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6096 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6097 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6098 } else {
6099 // Create a completely standard DBG_VALUE.
6100 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6101 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6102 }
6103 };
6104
6105 if (Kind == FuncArgumentDbgValueKind::Value) {
6106 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6107 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6108 // the entry block.
6109 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6110 if (!IsInEntryBlock)
6111 return false;
6112
6113 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6114 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6115 // variable that also is a param.
6116 //
6117 // Although, if we are at the top of the entry block already, we can still
6118 // emit using ArgDbgValue. This might catch some situations when the
6119 // dbg.value refers to an argument that isn't used in the entry block, so
6120 // any CopyToReg node would be optimized out and the only way to express
6121 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6122 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6123 // we should only emit as ArgDbgValue if the Variable is an argument to the
6124 // current function, and the dbg.value intrinsic is found in the entry
6125 // block.
6126 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6127 !DL->getInlinedAt();
6128 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6129 if (!IsInPrologue && !VariableIsFunctionInputArg)
6130 return false;
6131
6132 // Here we assume that a function argument on IR level only can be used to
6133 // describe one input parameter on source level. If we for example have
6134 // source code like this
6135 //
6136 // struct A { long x, y; };
6137 // void foo(struct A a, long b) {
6138 // ...
6139 // b = a.x;
6140 // ...
6141 // }
6142 //
6143 // and IR like this
6144 //
6145 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6146 // entry:
6147 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6148 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6149 // call void @llvm.dbg.value(metadata i32 %b, "b",
6150 // ...
6151 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6152 // ...
6153 //
6154 // then the last dbg.value is describing a parameter "b" using a value that
6155 // is an argument. But since we already has used %a1 to describe a parameter
6156 // we should not handle that last dbg.value here (that would result in an
6157 // incorrect hoisting of the DBG_VALUE to the function entry).
6158 // Notice that we allow one dbg.value per IR level argument, to accommodate
6159 // for the situation with fragments above.
6160 // If there is no node for the value being handled, we return true to skip
6161 // the normal generation of debug info, as it would kill existing debug
6162 // info for the parameter in case of duplicates.
6163 if (VariableIsFunctionInputArg) {
6164 unsigned ArgNo = Arg->getArgNo();
6165 if (ArgNo >= FuncInfo.DescribedArgs.size())
6166 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6167 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6168 return !NodeMap[V].getNode();
6169 FuncInfo.DescribedArgs.set(ArgNo);
6170 }
6171 }
6172
6173 bool IsIndirect = false;
6174 std::optional<MachineOperand> Op;
6175 // Some arguments' frame index is recorded during argument lowering.
6176 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6177 if (FI != std::numeric_limits<int>::max())
6179
6181 if (!Op && N.getNode()) {
6182 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6183 Register Reg;
6184 if (ArgRegsAndSizes.size() == 1)
6185 Reg = ArgRegsAndSizes.front().first;
6186
6187 if (Reg && Reg.isVirtual()) {
6189 Register PR = RegInfo.getLiveInPhysReg(Reg);
6190 if (PR)
6191 Reg = PR;
6192 }
6193 if (Reg) {
6194 Op = MachineOperand::CreateReg(Reg, false);
6195 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6196 }
6197 }
6198
6199 if (!Op && N.getNode()) {
6200 // Check if frame index is available.
6201 SDValue LCandidate = peekThroughBitcasts(N);
6202 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6203 if (FrameIndexSDNode *FINode =
6204 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6205 Op = MachineOperand::CreateFI(FINode->getIndex());
6206 }
6207
6208 if (!Op) {
6209 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6210 auto splitMultiRegDbgValue = [&](ArrayRef<std::pair<Register, TypeSize>>
6211 SplitRegs) {
6212 unsigned Offset = 0;
6213 for (const auto &RegAndSize : SplitRegs) {
6214 // If the expression is already a fragment, the current register
6215 // offset+size might extend beyond the fragment. In this case, only
6216 // the register bits that are inside the fragment are relevant.
6217 int RegFragmentSizeInBits = RegAndSize.second;
6218 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6219 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6220 // The register is entirely outside the expression fragment,
6221 // so is irrelevant for debug info.
6222 if (Offset >= ExprFragmentSizeInBits)
6223 break;
6224 // The register is partially outside the expression fragment, only
6225 // the low bits within the fragment are relevant for debug info.
6226 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6227 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6228 }
6229 }
6230
6231 auto FragmentExpr = DIExpression::createFragmentExpression(
6232 Expr, Offset, RegFragmentSizeInBits);
6233 Offset += RegAndSize.second;
6234 // If a valid fragment expression cannot be created, the variable's
6235 // correct value cannot be determined and so it is set as poison.
6236 if (!FragmentExpr) {
6238 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6239 DAG.AddDbgValue(SDV, false);
6240 continue;
6241 }
6242 MachineInstr *NewMI =
6243 MakeVRegDbgValue(RegAndSize.first, *FragmentExpr,
6244 Kind != FuncArgumentDbgValueKind::Value);
6245 FuncInfo.ArgDbgValues.push_back(NewMI);
6246 }
6247 };
6248
6249 // Check if ValueMap has reg number.
6251 VMI = FuncInfo.ValueMap.find(V);
6252 if (VMI != FuncInfo.ValueMap.end()) {
6253 const auto &TLI = DAG.getTargetLoweringInfo();
6254 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6255 V->getType(), std::nullopt);
6256 if (RFV.occupiesMultipleRegs()) {
6257 splitMultiRegDbgValue(RFV.getRegsAndSizes());
6258 return true;
6259 }
6260
6261 Op = MachineOperand::CreateReg(VMI->second, false);
6262 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6263 } else if (ArgRegsAndSizes.size() > 1) {
6264 // This was split due to the calling convention, and no virtual register
6265 // mapping exists for the value.
6266 splitMultiRegDbgValue(ArgRegsAndSizes);
6267 return true;
6268 }
6269 }
6270
6271 if (!Op)
6272 return false;
6273
6275 "Expected inlined-at fields to agree");
6276 MachineInstr *NewMI = nullptr;
6277
6278 if (Op->isReg())
6279 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6280 else
6281 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6282 Variable, Expr);
6283
6284 // Otherwise, use ArgDbgValues.
6285 FuncInfo.ArgDbgValues.push_back(NewMI);
6286 return true;
6287}
6288
6289/// Return the appropriate SDDbgValue based on N.
6290SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6291 DILocalVariable *Variable,
6292 DIExpression *Expr,
6293 const DebugLoc &dl,
6294 unsigned DbgSDNodeOrder) {
6295 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6296 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6297 // stack slot locations.
6298 //
6299 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6300 // debug values here after optimization:
6301 //
6302 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6303 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6304 //
6305 // Both describe the direct values of their associated variables.
6306 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6307 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6308 }
6309 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6310 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6311}
6312
6313static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6314 switch (Intrinsic) {
6315 case Intrinsic::smul_fix:
6316 return ISD::SMULFIX;
6317 case Intrinsic::umul_fix:
6318 return ISD::UMULFIX;
6319 case Intrinsic::smul_fix_sat:
6320 return ISD::SMULFIXSAT;
6321 case Intrinsic::umul_fix_sat:
6322 return ISD::UMULFIXSAT;
6323 case Intrinsic::sdiv_fix:
6324 return ISD::SDIVFIX;
6325 case Intrinsic::udiv_fix:
6326 return ISD::UDIVFIX;
6327 case Intrinsic::sdiv_fix_sat:
6328 return ISD::SDIVFIXSAT;
6329 case Intrinsic::udiv_fix_sat:
6330 return ISD::UDIVFIXSAT;
6331 default:
6332 llvm_unreachable("Unhandled fixed point intrinsic");
6333 }
6334}
6335
6336/// Given a @llvm.call.preallocated.setup, return the corresponding
6337/// preallocated call.
6338static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6339 assert(cast<CallBase>(PreallocatedSetup)
6341 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6342 "expected call_preallocated_setup Value");
6343 for (const auto *U : PreallocatedSetup->users()) {
6344 auto *UseCall = cast<CallBase>(U);
6345 const Function *Fn = UseCall->getCalledFunction();
6346 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6347 return UseCall;
6348 }
6349 }
6350 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6351}
6352
6353/// If DI is a debug value with an EntryValue expression, lower it using the
6354/// corresponding physical register of the associated Argument value
6355/// (guaranteed to exist by the verifier).
6356bool SelectionDAGBuilder::visitEntryValueDbgValue(
6357 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6358 DIExpression *Expr, DebugLoc DbgLoc) {
6359 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6360 return false;
6361
6362 // These properties are guaranteed by the verifier.
6363 const Argument *Arg = cast<Argument>(Values[0]);
6364 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6365
6366 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6367 if (ArgIt == FuncInfo.ValueMap.end()) {
6368 LLVM_DEBUG(
6369 dbgs() << "Dropping dbg.value: expression is entry_value but "
6370 "couldn't find an associated register for the Argument\n");
6371 return true;
6372 }
6373 Register ArgVReg = ArgIt->getSecond();
6374
6375 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6376 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6378 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6379 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6380 return true;
6381 }
6382 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6383 "couldn't find a physical register\n");
6384 return true;
6385}
6386
6387/// Lower the call to the specified intrinsic function.
6388void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6389 unsigned Intrinsic) {
6390 SDLoc sdl = getCurSDLoc();
6391 switch (Intrinsic) {
6392 case Intrinsic::experimental_convergence_anchor:
6393 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6394 break;
6395 case Intrinsic::experimental_convergence_entry:
6396 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6397 break;
6398 case Intrinsic::experimental_convergence_loop: {
6399 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6400 auto *Token = Bundle->Inputs[0].get();
6401 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6402 getValue(Token)));
6403 break;
6404 }
6405 }
6406}
6407
6408void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6409 unsigned IntrinsicID) {
6410 // For now, we're only lowering an 'add' histogram.
6411 // We can add others later, e.g. saturating adds, min/max.
6412 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6413 "Tried to lower unsupported histogram type");
6414 SDLoc sdl = getCurSDLoc();
6415 Value *Ptr = I.getOperand(0);
6416 SDValue Inc = getValue(I.getOperand(1));
6417 SDValue Mask = getValue(I.getOperand(2));
6418
6420 DataLayout TargetDL = DAG.getDataLayout();
6421 EVT VT = Inc.getValueType();
6422 Align Alignment = DAG.getEVTAlign(VT);
6423
6424 const MDNode *Ranges = getRangeMetadata(I);
6425
6426 SDValue Root = DAG.getRoot();
6427 SDValue Base;
6428 SDValue Index;
6429 SDValue Scale;
6430 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6431 I.getParent(), VT.getScalarStoreSize());
6432
6433 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6434
6438 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6439
6440 if (!UniformBase) {
6442 Index = getValue(Ptr);
6443 Scale =
6445 }
6446
6447 EVT IdxVT = Index.getValueType();
6448 EVT EltTy = IdxVT.getVectorElementType();
6449 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6450 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
6451 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6452 }
6453
6454 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6455
6456 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6457 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6458 Ops, MMO, ISD::SIGNED_SCALED);
6459
6460 setValue(&I, Histogram);
6461 DAG.setRoot(Histogram);
6462}
6463
6464void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6465 unsigned Intrinsic) {
6466 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6467 "Tried lowering invalid vector extract last");
6468 SDLoc sdl = getCurSDLoc();
6469 const DataLayout &Layout = DAG.getDataLayout();
6470 SDValue Data = getValue(I.getOperand(0));
6471 SDValue Mask = getValue(I.getOperand(1));
6472
6474 EVT ResVT = TLI.getValueType(Layout, I.getType());
6475
6476 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6477 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6479
6480 Value *Default = I.getOperand(2);
6481 if (!isa<PoisonValue>(Default) && !isa<UndefValue>(Default)) {
6482 SDValue PassThru = getValue(Default);
6483 EVT BoolVT = Mask.getValueType().getScalarType();
6484 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6485 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6486 }
6487
6488 setValue(&I, Result);
6489}
6490
6491/// Lower the call to the specified intrinsic function.
6492void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6493 unsigned Intrinsic) {
6495 SDLoc sdl = getCurSDLoc();
6496 DebugLoc dl = getCurDebugLoc();
6497 SDValue Res;
6498
6500 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6501 Flags.copyFMF(*FPOp);
6502
6503 switch (Intrinsic) {
6504 default:
6505 // By default, turn this into a target intrinsic node.
6506 visitTargetIntrinsic(I, Intrinsic);
6507 return;
6508 case Intrinsic::vscale: {
6509 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6510 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6511 return;
6512 }
6513 case Intrinsic::vastart: visitVAStart(I); return;
6514 case Intrinsic::vaend: visitVAEnd(I); return;
6515 case Intrinsic::vacopy: visitVACopy(I); return;
6516 case Intrinsic::returnaddress:
6518 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6519 getValue(I.getArgOperand(0))));
6520 return;
6521 case Intrinsic::addressofreturnaddress:
6522 setValue(&I,
6524 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6525 return;
6526 case Intrinsic::sponentry:
6527 setValue(&I,
6529 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6530 return;
6531 case Intrinsic::frameaddress:
6534 getValue(I.getArgOperand(0))));
6535 return;
6536 case Intrinsic::read_volatile_register:
6537 case Intrinsic::read_register: {
6538 Value *Reg = I.getArgOperand(0);
6539 SDValue Chain = getRoot();
6541 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6542 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6543 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6544 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6545 setValue(&I, Res);
6546 DAG.setRoot(Res.getValue(1));
6547 return;
6548 }
6549 case Intrinsic::write_register: {
6550 Value *Reg = I.getArgOperand(0);
6551 Value *RegValue = I.getArgOperand(1);
6552 SDValue Chain = getRoot();
6554 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6555 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6556 RegName, getValue(RegValue)));
6557 return;
6558 }
6559 case Intrinsic::memcpy:
6560 case Intrinsic::memcpy_inline: {
6561 const auto &MCI = cast<MemCpyInst>(I);
6562 SDValue Dst = getValue(I.getArgOperand(0));
6563 SDValue Src = getValue(I.getArgOperand(1));
6564 SDValue Size = getValue(I.getArgOperand(2));
6565 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6566 "memcpy_inline needs constant size");
6567 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6568 Align DstAlign = MCI.getDestAlign().valueOrOne();
6569 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6570 Align Alignment = std::min(DstAlign, SrcAlign);
6571 bool isVol = MCI.isVolatile();
6572 // FIXME: Support passing different dest/src alignments to the memcpy DAG
6573 // node.
6574 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6575 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol,
6576 MCI.isForceInlined(), &I, std::nullopt,
6577 MachinePointerInfo(I.getArgOperand(0)),
6578 MachinePointerInfo(I.getArgOperand(1)),
6579 I.getAAMetadata(), BatchAA);
6580 updateDAGForMaybeTailCall(MC);
6581 return;
6582 }
6583 case Intrinsic::memset:
6584 case Intrinsic::memset_inline: {
6585 const auto &MSII = cast<MemSetInst>(I);
6586 SDValue Dst = getValue(I.getArgOperand(0));
6587 SDValue Value = getValue(I.getArgOperand(1));
6588 SDValue Size = getValue(I.getArgOperand(2));
6589 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6590 "memset_inline needs constant size");
6591 // @llvm.memset defines 0 and 1 to both mean no alignment.
6592 Align DstAlign = MSII.getDestAlign().valueOrOne();
6593 bool isVol = MSII.isVolatile();
6594 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6595 SDValue MC = DAG.getMemset(
6596 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6597 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6598 updateDAGForMaybeTailCall(MC);
6599 return;
6600 }
6601 case Intrinsic::memmove: {
6602 const auto &MMI = cast<MemMoveInst>(I);
6603 SDValue Op1 = getValue(I.getArgOperand(0));
6604 SDValue Op2 = getValue(I.getArgOperand(1));
6605 SDValue Op3 = getValue(I.getArgOperand(2));
6606 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6607 Align DstAlign = MMI.getDestAlign().valueOrOne();
6608 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6609 Align Alignment = std::min(DstAlign, SrcAlign);
6610 bool isVol = MMI.isVolatile();
6611 // FIXME: Support passing different dest/src alignments to the memmove DAG
6612 // node.
6613 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6614 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, &I,
6615 /* OverrideTailCall */ std::nullopt,
6616 MachinePointerInfo(I.getArgOperand(0)),
6617 MachinePointerInfo(I.getArgOperand(1)),
6618 I.getAAMetadata(), BatchAA);
6619 updateDAGForMaybeTailCall(MM);
6620 return;
6621 }
6622 case Intrinsic::memcpy_element_unordered_atomic: {
6623 auto &MI = cast<AnyMemCpyInst>(I);
6624 SDValue Dst = getValue(MI.getRawDest());
6625 SDValue Src = getValue(MI.getRawSource());
6626 SDValue Length = getValue(MI.getLength());
6627
6628 Type *LengthTy = MI.getLength()->getType();
6629 unsigned ElemSz = MI.getElementSizeInBytes();
6630 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6631 SDValue MC =
6632 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6633 isTC, MachinePointerInfo(MI.getRawDest()),
6634 MachinePointerInfo(MI.getRawSource()));
6635 updateDAGForMaybeTailCall(MC);
6636 return;
6637 }
6638 case Intrinsic::memmove_element_unordered_atomic: {
6639 auto &MI = cast<AnyMemMoveInst>(I);
6640 SDValue Dst = getValue(MI.getRawDest());
6641 SDValue Src = getValue(MI.getRawSource());
6642 SDValue Length = getValue(MI.getLength());
6643
6644 Type *LengthTy = MI.getLength()->getType();
6645 unsigned ElemSz = MI.getElementSizeInBytes();
6646 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6647 SDValue MC =
6648 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6649 isTC, MachinePointerInfo(MI.getRawDest()),
6650 MachinePointerInfo(MI.getRawSource()));
6651 updateDAGForMaybeTailCall(MC);
6652 return;
6653 }
6654 case Intrinsic::memset_element_unordered_atomic: {
6655 auto &MI = cast<AnyMemSetInst>(I);
6656 SDValue Dst = getValue(MI.getRawDest());
6657 SDValue Val = getValue(MI.getValue());
6658 SDValue Length = getValue(MI.getLength());
6659
6660 Type *LengthTy = MI.getLength()->getType();
6661 unsigned ElemSz = MI.getElementSizeInBytes();
6662 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6663 SDValue MC =
6664 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6665 isTC, MachinePointerInfo(MI.getRawDest()));
6666 updateDAGForMaybeTailCall(MC);
6667 return;
6668 }
6669 case Intrinsic::call_preallocated_setup: {
6670 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6671 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6672 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6673 getRoot(), SrcValue);
6674 setValue(&I, Res);
6675 DAG.setRoot(Res);
6676 return;
6677 }
6678 case Intrinsic::call_preallocated_arg: {
6679 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6680 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6681 SDValue Ops[3];
6682 Ops[0] = getRoot();
6683 Ops[1] = SrcValue;
6684 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6685 MVT::i32); // arg index
6686 SDValue Res = DAG.getNode(
6688 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6689 setValue(&I, Res);
6690 DAG.setRoot(Res.getValue(1));
6691 return;
6692 }
6693
6694 case Intrinsic::eh_typeid_for: {
6695 // Find the type id for the given typeinfo.
6696 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6697 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6698 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6699 setValue(&I, Res);
6700 return;
6701 }
6702
6703 case Intrinsic::eh_return_i32:
6704 case Intrinsic::eh_return_i64:
6707 MVT::Other,
6709 getValue(I.getArgOperand(0)),
6710 getValue(I.getArgOperand(1))));
6711 return;
6712 case Intrinsic::eh_unwind_init:
6714 return;
6715 case Intrinsic::eh_dwarf_cfa:
6718 getValue(I.getArgOperand(0))));
6719 return;
6720 case Intrinsic::eh_sjlj_callsite: {
6721 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6722 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6723
6725 return;
6726 }
6727 case Intrinsic::eh_sjlj_functioncontext: {
6728 // Get and store the index of the function context.
6730 AllocaInst *FnCtx =
6731 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6732 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6734 return;
6735 }
6736 case Intrinsic::eh_sjlj_setjmp: {
6737 SDValue Ops[2];
6738 Ops[0] = getRoot();
6739 Ops[1] = getValue(I.getArgOperand(0));
6741 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6742 setValue(&I, Op.getValue(0));
6743 DAG.setRoot(Op.getValue(1));
6744 return;
6745 }
6746 case Intrinsic::eh_sjlj_longjmp:
6747 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6748 getRoot(), getValue(I.getArgOperand(0))));
6749 return;
6750 case Intrinsic::eh_sjlj_setup_dispatch:
6752 getRoot()));
6753 return;
6754 case Intrinsic::masked_gather:
6755 visitMaskedGather(I);
6756 return;
6757 case Intrinsic::masked_load:
6758 visitMaskedLoad(I);
6759 return;
6760 case Intrinsic::masked_scatter:
6761 visitMaskedScatter(I);
6762 return;
6763 case Intrinsic::masked_store:
6764 visitMaskedStore(I);
6765 return;
6766 case Intrinsic::masked_expandload:
6767 visitMaskedLoad(I, true /* IsExpanding */);
6768 return;
6769 case Intrinsic::masked_compressstore:
6770 visitMaskedStore(I, true /* IsCompressing */);
6771 return;
6772 case Intrinsic::powi:
6773 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6774 getValue(I.getArgOperand(1)), DAG));
6775 return;
6776 case Intrinsic::log:
6777 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6778 return;
6779 case Intrinsic::log2:
6780 setValue(&I,
6781 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6782 return;
6783 case Intrinsic::log10:
6784 setValue(&I,
6785 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6786 return;
6787 case Intrinsic::exp:
6788 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6789 return;
6790 case Intrinsic::exp2:
6791 setValue(&I,
6792 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6793 return;
6794 case Intrinsic::pow:
6795 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6796 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6797 return;
6798 case Intrinsic::sqrt:
6799 case Intrinsic::fabs:
6800 case Intrinsic::sin:
6801 case Intrinsic::cos:
6802 case Intrinsic::tan:
6803 case Intrinsic::asin:
6804 case Intrinsic::acos:
6805 case Intrinsic::atan:
6806 case Intrinsic::sinh:
6807 case Intrinsic::cosh:
6808 case Intrinsic::tanh:
6809 case Intrinsic::exp10:
6810 case Intrinsic::floor:
6811 case Intrinsic::ceil:
6812 case Intrinsic::trunc:
6813 case Intrinsic::rint:
6814 case Intrinsic::nearbyint:
6815 case Intrinsic::round:
6816 case Intrinsic::roundeven:
6817 case Intrinsic::canonicalize: {
6818 unsigned Opcode;
6819 // clang-format off
6820 switch (Intrinsic) {
6821 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6822 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6823 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6824 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6825 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6826 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6827 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6828 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6829 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6830 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6831 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6832 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6833 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6834 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6835 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6836 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6837 case Intrinsic::rint: Opcode = ISD::FRINT; break;
6838 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6839 case Intrinsic::round: Opcode = ISD::FROUND; break;
6840 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6841 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6842 }
6843 // clang-format on
6844
6845 setValue(&I, DAG.getNode(Opcode, sdl,
6846 getValue(I.getArgOperand(0)).getValueType(),
6847 getValue(I.getArgOperand(0)), Flags));
6848 return;
6849 }
6850 case Intrinsic::atan2:
6852 getValue(I.getArgOperand(0)).getValueType(),
6853 getValue(I.getArgOperand(0)),
6854 getValue(I.getArgOperand(1)), Flags));
6855 return;
6856 case Intrinsic::lround:
6857 case Intrinsic::llround:
6858 case Intrinsic::lrint:
6859 case Intrinsic::llrint: {
6860 unsigned Opcode;
6861 // clang-format off
6862 switch (Intrinsic) {
6863 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6864 case Intrinsic::lround: Opcode = ISD::LROUND; break;
6865 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6866 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
6867 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
6868 }
6869 // clang-format on
6870
6871 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6872 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6873 getValue(I.getArgOperand(0))));
6874 return;
6875 }
6876 case Intrinsic::minnum:
6878 getValue(I.getArgOperand(0)).getValueType(),
6879 getValue(I.getArgOperand(0)),
6880 getValue(I.getArgOperand(1)), Flags));
6881 return;
6882 case Intrinsic::maxnum:
6884 getValue(I.getArgOperand(0)).getValueType(),
6885 getValue(I.getArgOperand(0)),
6886 getValue(I.getArgOperand(1)), Flags));
6887 return;
6888 case Intrinsic::minimum:
6890 getValue(I.getArgOperand(0)).getValueType(),
6891 getValue(I.getArgOperand(0)),
6892 getValue(I.getArgOperand(1)), Flags));
6893 return;
6894 case Intrinsic::maximum:
6896 getValue(I.getArgOperand(0)).getValueType(),
6897 getValue(I.getArgOperand(0)),
6898 getValue(I.getArgOperand(1)), Flags));
6899 return;
6900 case Intrinsic::minimumnum:
6902 getValue(I.getArgOperand(0)).getValueType(),
6903 getValue(I.getArgOperand(0)),
6904 getValue(I.getArgOperand(1)), Flags));
6905 return;
6906 case Intrinsic::maximumnum:
6908 getValue(I.getArgOperand(0)).getValueType(),
6909 getValue(I.getArgOperand(0)),
6910 getValue(I.getArgOperand(1)), Flags));
6911 return;
6912 case Intrinsic::copysign:
6914 getValue(I.getArgOperand(0)).getValueType(),
6915 getValue(I.getArgOperand(0)),
6916 getValue(I.getArgOperand(1)), Flags));
6917 return;
6918 case Intrinsic::ldexp:
6920 getValue(I.getArgOperand(0)).getValueType(),
6921 getValue(I.getArgOperand(0)),
6922 getValue(I.getArgOperand(1)), Flags));
6923 return;
6924 case Intrinsic::modf:
6925 case Intrinsic::sincos:
6926 case Intrinsic::sincospi:
6927 case Intrinsic::frexp: {
6928 unsigned Opcode;
6929 switch (Intrinsic) {
6930 default:
6931 llvm_unreachable("unexpected intrinsic");
6932 case Intrinsic::sincos:
6933 Opcode = ISD::FSINCOS;
6934 break;
6935 case Intrinsic::sincospi:
6936 Opcode = ISD::FSINCOSPI;
6937 break;
6938 case Intrinsic::modf:
6939 Opcode = ISD::FMODF;
6940 break;
6941 case Intrinsic::frexp:
6942 Opcode = ISD::FFREXP;
6943 break;
6944 }
6945 SmallVector<EVT, 2> ValueVTs;
6946 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
6947 SDVTList VTs = DAG.getVTList(ValueVTs);
6948 setValue(
6949 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
6950 return;
6951 }
6952 case Intrinsic::arithmetic_fence: {
6954 getValue(I.getArgOperand(0)).getValueType(),
6955 getValue(I.getArgOperand(0)), Flags));
6956 return;
6957 }
6958 case Intrinsic::fma:
6960 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
6961 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
6962 getValue(I.getArgOperand(2)), Flags));
6963 return;
6964#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
6965 case Intrinsic::INTRINSIC:
6966#include "llvm/IR/ConstrainedOps.def"
6967 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
6968 return;
6969#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
6970#include "llvm/IR/VPIntrinsics.def"
6971 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
6972 return;
6973 case Intrinsic::fptrunc_round: {
6974 // Get the last argument, the metadata and convert it to an integer in the
6975 // call
6976 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
6977 std::optional<RoundingMode> RoundMode =
6978 convertStrToRoundingMode(cast<MDString>(MD)->getString());
6979
6980 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6981
6982 // Propagate fast-math-flags from IR to node(s).
6984 Flags.copyFMF(*cast<FPMathOperator>(&I));
6985 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
6986
6988 Result = DAG.getNode(
6989 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
6990 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
6991 setValue(&I, Result);
6992
6993 return;
6994 }
6995 case Intrinsic::fmuladd: {
6996 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6999 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7000 getValue(I.getArgOperand(0)).getValueType(),
7001 getValue(I.getArgOperand(0)),
7002 getValue(I.getArgOperand(1)),
7003 getValue(I.getArgOperand(2)), Flags));
7004 } else {
7005 // TODO: Intrinsic calls should have fast-math-flags.
7007 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7008 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7010 getValue(I.getArgOperand(0)).getValueType(),
7011 Mul, getValue(I.getArgOperand(2)), Flags);
7012 setValue(&I, Add);
7013 }
7014 return;
7015 }
7016 case Intrinsic::convert_to_fp16:
7017 setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16,
7018 DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16,
7019 getValue(I.getArgOperand(0)),
7020 DAG.getTargetConstant(0, sdl,
7021 MVT::i32))));
7022 return;
7023 case Intrinsic::convert_from_fp16:
7025 TLI.getValueType(DAG.getDataLayout(), I.getType()),
7026 DAG.getNode(ISD::BITCAST, sdl, MVT::f16,
7027 getValue(I.getArgOperand(0)))));
7028 return;
7029 case Intrinsic::fptosi_sat: {
7030 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7032 getValue(I.getArgOperand(0)),
7034 return;
7035 }
7036 case Intrinsic::fptoui_sat: {
7037 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7039 getValue(I.getArgOperand(0)),
7041 return;
7042 }
7043 case Intrinsic::set_rounding:
7044 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7045 {getRoot(), getValue(I.getArgOperand(0))});
7046 setValue(&I, Res);
7047 DAG.setRoot(Res.getValue(0));
7048 return;
7049 case Intrinsic::is_fpclass: {
7050 const DataLayout DLayout = DAG.getDataLayout();
7051 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7052 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7053 FPClassTest Test = static_cast<FPClassTest>(
7054 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7056 const Function &F = MF.getFunction();
7057 SDValue Op = getValue(I.getArgOperand(0));
7059 Flags.setNoFPExcept(
7060 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7061 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7062 // expansion can use illegal types. Making expansion early allows
7063 // legalizing these types prior to selection.
7064 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7065 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7066 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7067 setValue(&I, Result);
7068 return;
7069 }
7070
7071 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7072 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7073 setValue(&I, V);
7074 return;
7075 }
7076 case Intrinsic::get_fpenv: {
7077 const DataLayout DLayout = DAG.getDataLayout();
7078 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7079 Align TempAlign = DAG.getEVTAlign(EnvVT);
7080 SDValue Chain = getRoot();
7081 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7082 // and temporary storage in stack.
7083 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7084 Res = DAG.getNode(
7085 ISD::GET_FPENV, sdl,
7086 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7087 MVT::Other),
7088 Chain);
7089 } else {
7090 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7091 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7092 auto MPI =
7096 TempAlign);
7097 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7098 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7099 }
7100 setValue(&I, Res);
7101 DAG.setRoot(Res.getValue(1));
7102 return;
7103 }
7104 case Intrinsic::set_fpenv: {
7105 const DataLayout DLayout = DAG.getDataLayout();
7106 SDValue Env = getValue(I.getArgOperand(0));
7107 EVT EnvVT = Env.getValueType();
7108 Align TempAlign = DAG.getEVTAlign(EnvVT);
7109 SDValue Chain = getRoot();
7110 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7111 // environment from memory.
7112 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7113 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7114 } else {
7115 // Allocate space in stack, copy environment bits into it and use this
7116 // memory in SET_FPENV_MEM.
7117 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7118 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7119 auto MPI =
7121 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7125 TempAlign);
7126 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7127 }
7128 DAG.setRoot(Chain);
7129 return;
7130 }
7131 case Intrinsic::reset_fpenv:
7132 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7133 return;
7134 case Intrinsic::get_fpmode:
7135 Res = DAG.getNode(
7136 ISD::GET_FPMODE, sdl,
7137 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7138 MVT::Other),
7139 DAG.getRoot());
7140 setValue(&I, Res);
7141 DAG.setRoot(Res.getValue(1));
7142 return;
7143 case Intrinsic::set_fpmode:
7144 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7145 getValue(I.getArgOperand(0)));
7146 DAG.setRoot(Res);
7147 return;
7148 case Intrinsic::reset_fpmode: {
7149 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7150 DAG.setRoot(Res);
7151 return;
7152 }
7153 case Intrinsic::pcmarker: {
7154 SDValue Tmp = getValue(I.getArgOperand(0));
7155 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7156 return;
7157 }
7158 case Intrinsic::readcyclecounter: {
7159 SDValue Op = getRoot();
7161 DAG.getVTList(MVT::i64, MVT::Other), Op);
7162 setValue(&I, Res);
7163 DAG.setRoot(Res.getValue(1));
7164 return;
7165 }
7166 case Intrinsic::readsteadycounter: {
7167 SDValue Op = getRoot();
7169 DAG.getVTList(MVT::i64, MVT::Other), Op);
7170 setValue(&I, Res);
7171 DAG.setRoot(Res.getValue(1));
7172 return;
7173 }
7174 case Intrinsic::bitreverse:
7176 getValue(I.getArgOperand(0)).getValueType(),
7177 getValue(I.getArgOperand(0))));
7178 return;
7179 case Intrinsic::bswap:
7181 getValue(I.getArgOperand(0)).getValueType(),
7182 getValue(I.getArgOperand(0))));
7183 return;
7184 case Intrinsic::cttz: {
7185 SDValue Arg = getValue(I.getArgOperand(0));
7186 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7187 EVT Ty = Arg.getValueType();
7189 sdl, Ty, Arg));
7190 return;
7191 }
7192 case Intrinsic::ctlz: {
7193 SDValue Arg = getValue(I.getArgOperand(0));
7194 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7195 EVT Ty = Arg.getValueType();
7197 sdl, Ty, Arg));
7198 return;
7199 }
7200 case Intrinsic::ctpop: {
7201 SDValue Arg = getValue(I.getArgOperand(0));
7202 EVT Ty = Arg.getValueType();
7203 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7204 return;
7205 }
7206 case Intrinsic::fshl:
7207 case Intrinsic::fshr: {
7208 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7209 SDValue X = getValue(I.getArgOperand(0));
7210 SDValue Y = getValue(I.getArgOperand(1));
7211 SDValue Z = getValue(I.getArgOperand(2));
7212 EVT VT = X.getValueType();
7213
7214 if (X == Y) {
7215 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7216 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7217 } else {
7218 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7219 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7220 }
7221 return;
7222 }
7223 case Intrinsic::sadd_sat: {
7224 SDValue Op1 = getValue(I.getArgOperand(0));
7225 SDValue Op2 = getValue(I.getArgOperand(1));
7226 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7227 return;
7228 }
7229 case Intrinsic::uadd_sat: {
7230 SDValue Op1 = getValue(I.getArgOperand(0));
7231 SDValue Op2 = getValue(I.getArgOperand(1));
7232 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7233 return;
7234 }
7235 case Intrinsic::ssub_sat: {
7236 SDValue Op1 = getValue(I.getArgOperand(0));
7237 SDValue Op2 = getValue(I.getArgOperand(1));
7238 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7239 return;
7240 }
7241 case Intrinsic::usub_sat: {
7242 SDValue Op1 = getValue(I.getArgOperand(0));
7243 SDValue Op2 = getValue(I.getArgOperand(1));
7244 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7245 return;
7246 }
7247 case Intrinsic::sshl_sat: {
7248 SDValue Op1 = getValue(I.getArgOperand(0));
7249 SDValue Op2 = getValue(I.getArgOperand(1));
7250 setValue(&I, DAG.getNode(ISD::SSHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7251 return;
7252 }
7253 case Intrinsic::ushl_sat: {
7254 SDValue Op1 = getValue(I.getArgOperand(0));
7255 SDValue Op2 = getValue(I.getArgOperand(1));
7256 setValue(&I, DAG.getNode(ISD::USHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7257 return;
7258 }
7259 case Intrinsic::smul_fix:
7260 case Intrinsic::umul_fix:
7261 case Intrinsic::smul_fix_sat:
7262 case Intrinsic::umul_fix_sat: {
7263 SDValue Op1 = getValue(I.getArgOperand(0));
7264 SDValue Op2 = getValue(I.getArgOperand(1));
7265 SDValue Op3 = getValue(I.getArgOperand(2));
7267 Op1.getValueType(), Op1, Op2, Op3));
7268 return;
7269 }
7270 case Intrinsic::sdiv_fix:
7271 case Intrinsic::udiv_fix:
7272 case Intrinsic::sdiv_fix_sat:
7273 case Intrinsic::udiv_fix_sat: {
7274 SDValue Op1 = getValue(I.getArgOperand(0));
7275 SDValue Op2 = getValue(I.getArgOperand(1));
7276 SDValue Op3 = getValue(I.getArgOperand(2));
7278 Op1, Op2, Op3, DAG, TLI));
7279 return;
7280 }
7281 case Intrinsic::smax: {
7282 SDValue Op1 = getValue(I.getArgOperand(0));
7283 SDValue Op2 = getValue(I.getArgOperand(1));
7284 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7285 return;
7286 }
7287 case Intrinsic::smin: {
7288 SDValue Op1 = getValue(I.getArgOperand(0));
7289 SDValue Op2 = getValue(I.getArgOperand(1));
7290 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7291 return;
7292 }
7293 case Intrinsic::umax: {
7294 SDValue Op1 = getValue(I.getArgOperand(0));
7295 SDValue Op2 = getValue(I.getArgOperand(1));
7296 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7297 return;
7298 }
7299 case Intrinsic::umin: {
7300 SDValue Op1 = getValue(I.getArgOperand(0));
7301 SDValue Op2 = getValue(I.getArgOperand(1));
7302 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7303 return;
7304 }
7305 case Intrinsic::abs: {
7306 // TODO: Preserve "int min is poison" arg in SDAG?
7307 SDValue Op1 = getValue(I.getArgOperand(0));
7308 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7309 return;
7310 }
7311 case Intrinsic::scmp: {
7312 SDValue Op1 = getValue(I.getArgOperand(0));
7313 SDValue Op2 = getValue(I.getArgOperand(1));
7314 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7315 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7316 break;
7317 }
7318 case Intrinsic::ucmp: {
7319 SDValue Op1 = getValue(I.getArgOperand(0));
7320 SDValue Op2 = getValue(I.getArgOperand(1));
7321 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7322 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7323 break;
7324 }
7325 case Intrinsic::stacksave: {
7326 SDValue Op = getRoot();
7327 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7328 Res = DAG.getNode(ISD::STACKSAVE, sdl, DAG.getVTList(VT, MVT::Other), Op);
7329 setValue(&I, Res);
7330 DAG.setRoot(Res.getValue(1));
7331 return;
7332 }
7333 case Intrinsic::stackrestore:
7334 Res = getValue(I.getArgOperand(0));
7335 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7336 return;
7337 case Intrinsic::get_dynamic_area_offset: {
7338 SDValue Op = getRoot();
7339 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7341 Op);
7342 DAG.setRoot(Op);
7343 setValue(&I, Res);
7344 return;
7345 }
7346 case Intrinsic::stackguard: {
7348 const Module &M = *MF.getFunction().getParent();
7349 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7350 SDValue Chain = getRoot();
7351 if (TLI.useLoadStackGuardNode(M)) {
7352 Res = getLoadStackGuard(DAG, sdl, Chain);
7353 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7354 } else {
7355 const Value *Global = TLI.getSDagStackGuard(M);
7357 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7360 }
7361 if (TLI.useStackGuardXorFP())
7362 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7363 DAG.setRoot(Chain);
7364 setValue(&I, Res);
7365 return;
7366 }
7367 case Intrinsic::stackprotector: {
7368 // Emit code into the DAG to store the stack guard onto the stack.
7370 MachineFrameInfo &MFI = MF.getFrameInfo();
7371 const Module &M = *MF.getFunction().getParent();
7372 SDValue Src, Chain = getRoot();
7373
7374 if (TLI.useLoadStackGuardNode(M))
7375 Src = getLoadStackGuard(DAG, sdl, Chain);
7376 else
7377 Src = getValue(I.getArgOperand(0)); // The guard's value.
7378
7379 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7380
7381 int FI = FuncInfo.StaticAllocaMap[Slot];
7382 MFI.setStackProtectorIndex(FI);
7383 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7384
7385 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7386
7387 // Store the stack protector onto the stack.
7388 Res = DAG.getStore(
7389 Chain, sdl, Src, FIN,
7392 setValue(&I, Res);
7393 DAG.setRoot(Res);
7394 return;
7395 }
7396 case Intrinsic::objectsize:
7397 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7398
7399 case Intrinsic::is_constant:
7400 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7401
7402 case Intrinsic::annotation:
7403 case Intrinsic::ptr_annotation:
7404 case Intrinsic::launder_invariant_group:
7405 case Intrinsic::strip_invariant_group:
7406 // Drop the intrinsic, but forward the value
7407 setValue(&I, getValue(I.getOperand(0)));
7408 return;
7409
7410 case Intrinsic::type_test:
7411 case Intrinsic::public_type_test:
7412 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7413 return;
7414
7415 case Intrinsic::assume:
7416 case Intrinsic::experimental_noalias_scope_decl:
7417 case Intrinsic::var_annotation:
7418 case Intrinsic::sideeffect:
7419 // Discard annotate attributes, noalias scope declarations, assumptions, and
7420 // artificial side-effects.
7421 return;
7422
7423 case Intrinsic::codeview_annotation: {
7424 // Emit a label associated with this metadata.
7426 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7427 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7428 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7429 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7430 DAG.setRoot(Res);
7431 return;
7432 }
7433
7434 case Intrinsic::init_trampoline: {
7435 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7436
7437 SDValue Ops[6];
7438 Ops[0] = getRoot();
7439 Ops[1] = getValue(I.getArgOperand(0));
7440 Ops[2] = getValue(I.getArgOperand(1));
7441 Ops[3] = getValue(I.getArgOperand(2));
7442 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7443 Ops[5] = DAG.getSrcValue(F);
7444
7445 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7446
7447 DAG.setRoot(Res);
7448 return;
7449 }
7450 case Intrinsic::adjust_trampoline:
7453 getValue(I.getArgOperand(0))));
7454 return;
7455 case Intrinsic::gcroot: {
7457 "only valid in functions with gc specified, enforced by Verifier");
7458 assert(GFI && "implied by previous");
7459 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7460 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7461
7462 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7463 GFI->addStackRoot(FI->getIndex(), TypeMap);
7464 return;
7465 }
7466 case Intrinsic::gcread:
7467 case Intrinsic::gcwrite:
7468 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7469 case Intrinsic::get_rounding:
7470 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7471 setValue(&I, Res);
7472 DAG.setRoot(Res.getValue(1));
7473 return;
7474
7475 case Intrinsic::expect:
7476 case Intrinsic::expect_with_probability:
7477 // Just replace __builtin_expect(exp, c) and
7478 // __builtin_expect_with_probability(exp, c, p) with EXP.
7479 setValue(&I, getValue(I.getArgOperand(0)));
7480 return;
7481
7482 case Intrinsic::ubsantrap:
7483 case Intrinsic::debugtrap:
7484 case Intrinsic::trap: {
7485 StringRef TrapFuncName =
7486 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7487 if (TrapFuncName.empty()) {
7488 switch (Intrinsic) {
7489 case Intrinsic::trap:
7490 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7491 break;
7492 case Intrinsic::debugtrap:
7493 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7494 break;
7495 case Intrinsic::ubsantrap:
7497 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7499 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7500 MVT::i32)));
7501 break;
7502 default: llvm_unreachable("unknown trap intrinsic");
7503 }
7505 I.hasFnAttr(Attribute::NoMerge));
7506 return;
7507 }
7509 if (Intrinsic == Intrinsic::ubsantrap) {
7510 Value *Arg = I.getArgOperand(0);
7511 Args.emplace_back(Arg, getValue(Arg));
7512 }
7513
7515 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7516 CallingConv::C, I.getType(),
7517 DAG.getExternalSymbol(TrapFuncName.data(),
7519 std::move(Args));
7520 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7521 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7522 DAG.setRoot(Result.second);
7523 return;
7524 }
7525
7526 case Intrinsic::allow_runtime_check:
7527 case Intrinsic::allow_ubsan_check:
7528 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7529 return;
7530
7531 case Intrinsic::uadd_with_overflow:
7532 case Intrinsic::sadd_with_overflow:
7533 case Intrinsic::usub_with_overflow:
7534 case Intrinsic::ssub_with_overflow:
7535 case Intrinsic::umul_with_overflow:
7536 case Intrinsic::smul_with_overflow: {
7538 switch (Intrinsic) {
7539 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7540 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7541 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7542 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7543 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7544 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7545 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7546 }
7547 SDValue Op1 = getValue(I.getArgOperand(0));
7548 SDValue Op2 = getValue(I.getArgOperand(1));
7549
7550 EVT ResultVT = Op1.getValueType();
7551 EVT OverflowVT = MVT::i1;
7552 if (ResultVT.isVector())
7553 OverflowVT = EVT::getVectorVT(
7554 *Context, OverflowVT, ResultVT.getVectorElementCount());
7555
7556 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7557 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7558 return;
7559 }
7560 case Intrinsic::prefetch: {
7561 SDValue Ops[5];
7562 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7564 Ops[0] = DAG.getRoot();
7565 Ops[1] = getValue(I.getArgOperand(0));
7566 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7567 MVT::i32);
7568 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7569 MVT::i32);
7570 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7571 MVT::i32);
7573 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7574 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7575 /* align */ std::nullopt, Flags);
7576
7577 // Chain the prefetch in parallel with any pending loads, to stay out of
7578 // the way of later optimizations.
7579 PendingLoads.push_back(Result);
7580 Result = getRoot();
7581 DAG.setRoot(Result);
7582 return;
7583 }
7584 case Intrinsic::lifetime_start:
7585 case Intrinsic::lifetime_end: {
7586 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7587 // Stack coloring is not enabled in O0, discard region information.
7589 return;
7590
7591 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7592 if (!LifetimeObject)
7593 return;
7594
7595 // First check that the Alloca is static, otherwise it won't have a
7596 // valid frame index.
7597 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7598 if (SI == FuncInfo.StaticAllocaMap.end())
7599 return;
7600
7601 const int FrameIndex = SI->second;
7602 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7603 DAG.setRoot(Res);
7604 return;
7605 }
7606 case Intrinsic::pseudoprobe: {
7607 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7608 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7609 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7610 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7611 DAG.setRoot(Res);
7612 return;
7613 }
7614 case Intrinsic::invariant_start:
7615 // Discard region information.
7616 setValue(&I,
7617 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7618 return;
7619 case Intrinsic::invariant_end:
7620 // Discard region information.
7621 return;
7622 case Intrinsic::clear_cache: {
7623 SDValue InputChain = DAG.getRoot();
7624 SDValue StartVal = getValue(I.getArgOperand(0));
7625 SDValue EndVal = getValue(I.getArgOperand(1));
7626 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7627 {InputChain, StartVal, EndVal});
7628 setValue(&I, Res);
7629 DAG.setRoot(Res);
7630 return;
7631 }
7632 case Intrinsic::donothing:
7633 case Intrinsic::seh_try_begin:
7634 case Intrinsic::seh_scope_begin:
7635 case Intrinsic::seh_try_end:
7636 case Intrinsic::seh_scope_end:
7637 // ignore
7638 return;
7639 case Intrinsic::experimental_stackmap:
7640 visitStackmap(I);
7641 return;
7642 case Intrinsic::experimental_patchpoint_void:
7643 case Intrinsic::experimental_patchpoint:
7644 visitPatchpoint(I);
7645 return;
7646 case Intrinsic::experimental_gc_statepoint:
7647 LowerStatepoint(cast<GCStatepointInst>(I));
7648 return;
7649 case Intrinsic::experimental_gc_result:
7650 visitGCResult(cast<GCResultInst>(I));
7651 return;
7652 case Intrinsic::experimental_gc_relocate:
7653 visitGCRelocate(cast<GCRelocateInst>(I));
7654 return;
7655 case Intrinsic::instrprof_cover:
7656 llvm_unreachable("instrprof failed to lower a cover");
7657 case Intrinsic::instrprof_increment:
7658 llvm_unreachable("instrprof failed to lower an increment");
7659 case Intrinsic::instrprof_timestamp:
7660 llvm_unreachable("instrprof failed to lower a timestamp");
7661 case Intrinsic::instrprof_value_profile:
7662 llvm_unreachable("instrprof failed to lower a value profiling call");
7663 case Intrinsic::instrprof_mcdc_parameters:
7664 llvm_unreachable("instrprof failed to lower mcdc parameters");
7665 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7666 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7667 case Intrinsic::localescape: {
7670
7671 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7672 // is the same on all targets.
7673 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7674 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7675 if (isa<ConstantPointerNull>(Arg))
7676 continue; // Skip null pointers. They represent a hole in index space.
7677 AllocaInst *Slot = cast<AllocaInst>(Arg);
7678 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7679 "can only escape static allocas");
7680 int FI = FuncInfo.StaticAllocaMap[Slot];
7681 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7684 TII->get(TargetOpcode::LOCAL_ESCAPE))
7685 .addSym(FrameAllocSym)
7686 .addFrameIndex(FI);
7687 }
7688
7689 return;
7690 }
7691
7692 case Intrinsic::localrecover: {
7693 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7695
7696 // Get the symbol that defines the frame offset.
7697 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7698 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7699 unsigned IdxVal =
7700 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7701 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7703
7704 Value *FP = I.getArgOperand(1);
7705 SDValue FPVal = getValue(FP);
7706 EVT PtrVT = FPVal.getValueType();
7707
7708 // Create a MCSymbol for the label to avoid any target lowering
7709 // that would make this PC relative.
7710 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7711 SDValue OffsetVal =
7712 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7713
7714 // Add the offset to the FP.
7715 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7716 setValue(&I, Add);
7717
7718 return;
7719 }
7720
7721 case Intrinsic::fake_use: {
7722 Value *V = I.getArgOperand(0);
7723 SDValue Ops[2];
7724 // For Values not declared or previously used in this basic block, the
7725 // NodeMap will not have an entry, and `getValue` will assert if V has no
7726 // valid register value.
7727 auto FakeUseValue = [&]() -> SDValue {
7728 SDValue &N = NodeMap[V];
7729 if (N.getNode())
7730 return N;
7731
7732 // If there's a virtual register allocated and initialized for this
7733 // value, use it.
7734 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7735 return copyFromReg;
7736 // FIXME: Do we want to preserve constants? It seems pointless.
7737 if (isa<Constant>(V))
7738 return getValue(V);
7739 return SDValue();
7740 }();
7741 if (!FakeUseValue || FakeUseValue.isUndef())
7742 return;
7743 Ops[0] = getRoot();
7744 Ops[1] = FakeUseValue;
7745 // Also, do not translate a fake use with an undef operand, or any other
7746 // empty SDValues.
7747 if (!Ops[1] || Ops[1].isUndef())
7748 return;
7749 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7750 return;
7751 }
7752
7753 case Intrinsic::eh_exceptionpointer:
7754 case Intrinsic::eh_exceptioncode: {
7755 // Get the exception pointer vreg, copy from it, and resize it to fit.
7756 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7757 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7758 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7760 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7761 if (Intrinsic == Intrinsic::eh_exceptioncode)
7762 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7763 setValue(&I, N);
7764 return;
7765 }
7766 case Intrinsic::xray_customevent: {
7767 // Here we want to make sure that the intrinsic behaves as if it has a
7768 // specific calling convention.
7769 const auto &Triple = DAG.getTarget().getTargetTriple();
7770 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7771 return;
7772
7774
7775 // We want to say that we always want the arguments in registers.
7776 SDValue LogEntryVal = getValue(I.getArgOperand(0));
7777 SDValue StrSizeVal = getValue(I.getArgOperand(1));
7778 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7779 SDValue Chain = getRoot();
7780 Ops.push_back(LogEntryVal);
7781 Ops.push_back(StrSizeVal);
7782 Ops.push_back(Chain);
7783
7784 // We need to enforce the calling convention for the callsite, so that
7785 // argument ordering is enforced correctly, and that register allocation can
7786 // see that some registers may be assumed clobbered and have to preserve
7787 // them across calls to the intrinsic.
7788 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7789 sdl, NodeTys, Ops);
7790 SDValue patchableNode = SDValue(MN, 0);
7791 DAG.setRoot(patchableNode);
7792 setValue(&I, patchableNode);
7793 return;
7794 }
7795 case Intrinsic::xray_typedevent: {
7796 // Here we want to make sure that the intrinsic behaves as if it has a
7797 // specific calling convention.
7798 const auto &Triple = DAG.getTarget().getTargetTriple();
7799 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7800 return;
7801
7803
7804 // We want to say that we always want the arguments in registers.
7805 // It's unclear to me how manipulating the selection DAG here forces callers
7806 // to provide arguments in registers instead of on the stack.
7807 SDValue LogTypeId = getValue(I.getArgOperand(0));
7808 SDValue LogEntryVal = getValue(I.getArgOperand(1));
7809 SDValue StrSizeVal = getValue(I.getArgOperand(2));
7810 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7811 SDValue Chain = getRoot();
7812 Ops.push_back(LogTypeId);
7813 Ops.push_back(LogEntryVal);
7814 Ops.push_back(StrSizeVal);
7815 Ops.push_back(Chain);
7816
7817 // We need to enforce the calling convention for the callsite, so that
7818 // argument ordering is enforced correctly, and that register allocation can
7819 // see that some registers may be assumed clobbered and have to preserve
7820 // them across calls to the intrinsic.
7822 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
7823 SDValue patchableNode = SDValue(MN, 0);
7824 DAG.setRoot(patchableNode);
7825 setValue(&I, patchableNode);
7826 return;
7827 }
7828 case Intrinsic::experimental_deoptimize:
7830 return;
7831 case Intrinsic::stepvector:
7832 visitStepVector(I);
7833 return;
7834 case Intrinsic::vector_reduce_fadd:
7835 case Intrinsic::vector_reduce_fmul:
7836 case Intrinsic::vector_reduce_add:
7837 case Intrinsic::vector_reduce_mul:
7838 case Intrinsic::vector_reduce_and:
7839 case Intrinsic::vector_reduce_or:
7840 case Intrinsic::vector_reduce_xor:
7841 case Intrinsic::vector_reduce_smax:
7842 case Intrinsic::vector_reduce_smin:
7843 case Intrinsic::vector_reduce_umax:
7844 case Intrinsic::vector_reduce_umin:
7845 case Intrinsic::vector_reduce_fmax:
7846 case Intrinsic::vector_reduce_fmin:
7847 case Intrinsic::vector_reduce_fmaximum:
7848 case Intrinsic::vector_reduce_fminimum:
7849 visitVectorReduce(I, Intrinsic);
7850 return;
7851
7852 case Intrinsic::icall_branch_funnel: {
7854 Ops.push_back(getValue(I.getArgOperand(0)));
7855
7856 int64_t Offset;
7857 auto *Base = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset(
7858 I.getArgOperand(1), Offset, DAG.getDataLayout()));
7859 if (!Base)
7861 "llvm.icall.branch.funnel operand must be a GlobalValue");
7862 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
7863
7864 struct BranchFunnelTarget {
7865 int64_t Offset;
7867 };
7869
7870 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
7871 auto *ElemBase = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset(
7872 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
7873 if (ElemBase != Base)
7874 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
7875 "to the same GlobalValue");
7876
7877 SDValue Val = getValue(I.getArgOperand(Op + 1));
7878 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
7879 if (!GA)
7881 "llvm.icall.branch.funnel operand must be a GlobalValue");
7883 GA->getGlobal(), sdl, Val.getValueType(),
7884 GA->getOffset())});
7885 }
7886 llvm::sort(Targets,
7887 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
7888 return T1.Offset < T2.Offset;
7889 });
7890
7891 for (auto &T : Targets) {
7892 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
7893 Ops.push_back(T.Target);
7894 }
7895
7896 Ops.push_back(DAG.getRoot()); // Chain
7897 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
7898 MVT::Other, Ops),
7899 0);
7900 DAG.setRoot(N);
7901 setValue(&I, N);
7902 HasTailCall = true;
7903 return;
7904 }
7905
7906 case Intrinsic::wasm_landingpad_index:
7907 // Information this intrinsic contained has been transferred to
7908 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
7909 // delete it now.
7910 return;
7911
7912 case Intrinsic::aarch64_settag:
7913 case Intrinsic::aarch64_settag_zero: {
7915 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
7917 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
7918 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
7919 ZeroMemory);
7920 DAG.setRoot(Val);
7921 setValue(&I, Val);
7922 return;
7923 }
7924 case Intrinsic::amdgcn_cs_chain: {
7925 // At this point we don't care if it's amdgpu_cs_chain or
7926 // amdgpu_cs_chain_preserve.
7928
7929 Type *RetTy = I.getType();
7930 assert(RetTy->isVoidTy() && "Should not return");
7931
7932 SDValue Callee = getValue(I.getOperand(0));
7933
7934 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
7935 // We'll also tack the value of the EXEC mask at the end.
7937 Args.reserve(3);
7938
7939 for (unsigned Idx : {2, 3, 1}) {
7940 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
7941 I.getOperand(Idx)->getType());
7942 Arg.setAttributes(&I, Idx);
7943 Args.push_back(Arg);
7944 }
7945
7946 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
7947 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
7948 Args[2].IsInReg = true; // EXEC should be inreg
7949
7950 // Forward the flags and any additional arguments.
7951 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
7952 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
7953 I.getOperand(Idx)->getType());
7954 Arg.setAttributes(&I, Idx);
7955 Args.push_back(Arg);
7956 }
7957
7959 CLI.setDebugLoc(getCurSDLoc())
7960 .setChain(getRoot())
7961 .setCallee(CC, RetTy, Callee, std::move(Args))
7962 .setNoReturn(true)
7963 .setTailCall(true)
7964 .setConvergent(I.isConvergent());
7965 CLI.CB = &I;
7966 std::pair<SDValue, SDValue> Result =
7967 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
7968 (void)Result;
7969 assert(!Result.first.getNode() && !Result.second.getNode() &&
7970 "Should've lowered as tail call");
7971
7972 HasTailCall = true;
7973 return;
7974 }
7975 case Intrinsic::amdgcn_call_whole_wave: {
7977
7978 // The first argument is the callee. Skip it when assembling the call args.
7979 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
7980 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
7981 I.getArgOperand(Idx)->getType());
7982 Arg.setAttributes(&I, Idx);
7983 Args.push_back(Arg);
7984 }
7985
7986 SDValue ConvControlToken;
7987 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
7988 auto *Token = Bundle->Inputs[0].get();
7989 ConvControlToken = getValue(Token);
7990 }
7991
7993 CLI.setDebugLoc(getCurSDLoc())
7994 .setChain(getRoot())
7995 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
7996 getValue(I.getArgOperand(0)), std::move(Args))
7997 .setTailCall(false)
7998 .setIsPreallocated(
7999 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8000 .setConvergent(I.isConvergent())
8001 .setConvergenceControlToken(ConvControlToken);
8002 CLI.CB = &I;
8003
8004 std::pair<SDValue, SDValue> Result =
8005 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8006
8007 if (Result.first.getNode())
8008 setValue(&I, Result.first);
8009 return;
8010 }
8011 case Intrinsic::ptrmask: {
8012 SDValue Ptr = getValue(I.getOperand(0));
8013 SDValue Mask = getValue(I.getOperand(1));
8014
8015 // On arm64_32, pointers are 32 bits when stored in memory, but
8016 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8017 // match the index type, but the pointer is 64 bits, so the mask must be
8018 // zero-extended up to 64 bits to match the pointer.
8019 EVT PtrVT =
8020 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8021 EVT MemVT =
8022 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8023 assert(PtrVT == Ptr.getValueType());
8024 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8025 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8026 // 128-bit, so we have to pad the mask with ones for unused bits.
8027 auto HighOnes = DAG.getNode(
8028 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8029 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8030 PtrVT, sdl));
8031 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8032 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8033 } else if (Mask.getValueType() != PtrVT)
8034 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8035
8036 assert(Mask.getValueType() == PtrVT);
8037 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8038 return;
8039 }
8040 case Intrinsic::threadlocal_address: {
8041 setValue(&I, getValue(I.getOperand(0)));
8042 return;
8043 }
8044 case Intrinsic::get_active_lane_mask: {
8045 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8046 SDValue Index = getValue(I.getOperand(0));
8047 SDValue TripCount = getValue(I.getOperand(1));
8048 EVT ElementVT = Index.getValueType();
8049
8050 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8051 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8052 TripCount));
8053 return;
8054 }
8055
8056 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8057 CCVT.getVectorElementCount());
8058
8059 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8060 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8061 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8062 SDValue VectorInduction = DAG.getNode(
8063 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8064 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8065 VectorTripCount, ISD::CondCode::SETULT);
8066 setValue(&I, SetCC);
8067 return;
8068 }
8069 case Intrinsic::experimental_get_vector_length: {
8070 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8071 "Expected positive VF");
8072 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8073 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8074
8075 SDValue Count = getValue(I.getOperand(0));
8076 EVT CountVT = Count.getValueType();
8077
8078 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8079 visitTargetIntrinsic(I, Intrinsic);
8080 return;
8081 }
8082
8083 // Expand to a umin between the trip count and the maximum elements the type
8084 // can hold.
8085 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8086
8087 // Extend the trip count to at least the result VT.
8088 if (CountVT.bitsLT(VT)) {
8089 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8090 CountVT = VT;
8091 }
8092
8093 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8094 ElementCount::get(VF, IsScalable));
8095
8096 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8097 // Clip to the result type if needed.
8098 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8099
8100 setValue(&I, Trunc);
8101 return;
8102 }
8103 case Intrinsic::experimental_vector_partial_reduce_add: {
8104 if (!TLI.shouldExpandPartialReductionIntrinsic(cast<IntrinsicInst>(&I))) {
8105 visitTargetIntrinsic(I, Intrinsic);
8106 return;
8107 }
8108 SDValue Acc = getValue(I.getOperand(0));
8109 SDValue Input = getValue(I.getOperand(1));
8110 setValue(&I,
8112 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8113 return;
8114 }
8115 case Intrinsic::experimental_cttz_elts: {
8116 auto DL = getCurSDLoc();
8117 SDValue Op = getValue(I.getOperand(0));
8118 EVT OpVT = Op.getValueType();
8119
8120 if (!TLI.shouldExpandCttzElements(OpVT)) {
8121 visitTargetIntrinsic(I, Intrinsic);
8122 return;
8123 }
8124
8125 if (OpVT.getScalarType() != MVT::i1) {
8126 // Compare the input vector elements to zero & use to count trailing zeros
8127 SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8128 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8129 OpVT.getVectorElementCount());
8130 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8131 }
8132
8133 // If the zero-is-poison flag is set, we can assume the upper limit
8134 // of the result is VF-1.
8135 bool ZeroIsPoison =
8136 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8137 ConstantRange VScaleRange(1, true); // Dummy value.
8138 if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8139 VScaleRange = getVScaleRange(I.getCaller(), 64);
8140 unsigned EltWidth = TLI.getBitWidthForCttzElements(
8141 I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8142
8143 MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8144
8145 // Create the new vector type & get the vector length
8146 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8147 OpVT.getVectorElementCount());
8148
8149 SDValue VL =
8150 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8151
8152 SDValue StepVec = DAG.getStepVector(DL, NewVT);
8153 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8154 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8156 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8158 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8159
8160 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8162
8163 setValue(&I, Ret);
8164 return;
8165 }
8166 case Intrinsic::vector_insert: {
8167 SDValue Vec = getValue(I.getOperand(0));
8168 SDValue SubVec = getValue(I.getOperand(1));
8169 SDValue Index = getValue(I.getOperand(2));
8170
8171 // The intrinsic's index type is i64, but the SDNode requires an index type
8172 // suitable for the target. Convert the index as required.
8173 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8174 if (Index.getValueType() != VectorIdxTy)
8175 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8176
8177 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8178 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8179 Index));
8180 return;
8181 }
8182 case Intrinsic::vector_extract: {
8183 SDValue Vec = getValue(I.getOperand(0));
8184 SDValue Index = getValue(I.getOperand(1));
8185 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8186
8187 // The intrinsic's index type is i64, but the SDNode requires an index type
8188 // suitable for the target. Convert the index as required.
8189 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8190 if (Index.getValueType() != VectorIdxTy)
8191 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8192
8193 setValue(&I,
8194 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8195 return;
8196 }
8197 case Intrinsic::experimental_vector_match: {
8198 SDValue Op1 = getValue(I.getOperand(0));
8199 SDValue Op2 = getValue(I.getOperand(1));
8200 SDValue Mask = getValue(I.getOperand(2));
8201 EVT Op1VT = Op1.getValueType();
8202 EVT Op2VT = Op2.getValueType();
8203 EVT ResVT = Mask.getValueType();
8204 unsigned SearchSize = Op2VT.getVectorNumElements();
8205
8206 // If the target has native support for this vector match operation, lower
8207 // the intrinsic untouched; otherwise, expand it below.
8208 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8209 visitTargetIntrinsic(I, Intrinsic);
8210 return;
8211 }
8212
8213 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8214
8215 for (unsigned i = 0; i < SearchSize; ++i) {
8217 Op2VT.getVectorElementType(), Op2,
8218 DAG.getVectorIdxConstant(i, sdl));
8219 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8220 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8221 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8222 }
8223
8224 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8225 return;
8226 }
8227 case Intrinsic::vector_reverse:
8228 visitVectorReverse(I);
8229 return;
8230 case Intrinsic::vector_splice:
8231 visitVectorSplice(I);
8232 return;
8233 case Intrinsic::callbr_landingpad:
8234 visitCallBrLandingPad(I);
8235 return;
8236 case Intrinsic::vector_interleave2:
8237 visitVectorInterleave(I, 2);
8238 return;
8239 case Intrinsic::vector_interleave3:
8240 visitVectorInterleave(I, 3);
8241 return;
8242 case Intrinsic::vector_interleave4:
8243 visitVectorInterleave(I, 4);
8244 return;
8245 case Intrinsic::vector_interleave5:
8246 visitVectorInterleave(I, 5);
8247 return;
8248 case Intrinsic::vector_interleave6:
8249 visitVectorInterleave(I, 6);
8250 return;
8251 case Intrinsic::vector_interleave7:
8252 visitVectorInterleave(I, 7);
8253 return;
8254 case Intrinsic::vector_interleave8:
8255 visitVectorInterleave(I, 8);
8256 return;
8257 case Intrinsic::vector_deinterleave2:
8258 visitVectorDeinterleave(I, 2);
8259 return;
8260 case Intrinsic::vector_deinterleave3:
8261 visitVectorDeinterleave(I, 3);
8262 return;
8263 case Intrinsic::vector_deinterleave4:
8264 visitVectorDeinterleave(I, 4);
8265 return;
8266 case Intrinsic::vector_deinterleave5:
8267 visitVectorDeinterleave(I, 5);
8268 return;
8269 case Intrinsic::vector_deinterleave6:
8270 visitVectorDeinterleave(I, 6);
8271 return;
8272 case Intrinsic::vector_deinterleave7:
8273 visitVectorDeinterleave(I, 7);
8274 return;
8275 case Intrinsic::vector_deinterleave8:
8276 visitVectorDeinterleave(I, 8);
8277 return;
8278 case Intrinsic::experimental_vector_compress:
8280 getValue(I.getArgOperand(0)).getValueType(),
8281 getValue(I.getArgOperand(0)),
8282 getValue(I.getArgOperand(1)),
8283 getValue(I.getArgOperand(2)), Flags));
8284 return;
8285 case Intrinsic::experimental_convergence_anchor:
8286 case Intrinsic::experimental_convergence_entry:
8287 case Intrinsic::experimental_convergence_loop:
8288 visitConvergenceControl(I, Intrinsic);
8289 return;
8290 case Intrinsic::experimental_vector_histogram_add: {
8291 visitVectorHistogram(I, Intrinsic);
8292 return;
8293 }
8294 case Intrinsic::experimental_vector_extract_last_active: {
8295 visitVectorExtractLastActive(I, Intrinsic);
8296 return;
8297 }
8298 }
8299}
8300
8301void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8302 const ConstrainedFPIntrinsic &FPI) {
8303 SDLoc sdl = getCurSDLoc();
8304
8305 // We do not need to serialize constrained FP intrinsics against
8306 // each other or against (nonvolatile) loads, so they can be
8307 // chained like loads.
8308 SDValue Chain = DAG.getRoot();
8310 Opers.push_back(Chain);
8311 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8312 Opers.push_back(getValue(FPI.getArgOperand(I)));
8313
8314 auto pushOutChain = [this](SDValue Result, fp::ExceptionBehavior EB) {
8315 assert(Result.getNode()->getNumValues() == 2);
8316
8317 // Push node to the appropriate list so that future instructions can be
8318 // chained up correctly.
8319 SDValue OutChain = Result.getValue(1);
8320 switch (EB) {
8322 // The only reason why ebIgnore nodes still need to be chained is that
8323 // they might depend on the current rounding mode, and therefore must
8324 // not be moved across instruction that may change that mode.
8325 [[fallthrough]];
8327 // These must not be moved across calls or instructions that may change
8328 // floating-point exception masks.
8329 PendingConstrainedFP.push_back(OutChain);
8330 break;
8332 // These must not be moved across calls or instructions that may change
8333 // floating-point exception masks or read floating-point exception flags.
8334 // In addition, they cannot be optimized out even if unused.
8335 PendingConstrainedFPStrict.push_back(OutChain);
8336 break;
8337 }
8338 };
8339
8341 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8342 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8344
8347 Flags.setNoFPExcept(true);
8348
8349 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8350 Flags.copyFMF(*FPOp);
8351
8352 unsigned Opcode;
8353 switch (FPI.getIntrinsicID()) {
8354 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8355#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8356 case Intrinsic::INTRINSIC: \
8357 Opcode = ISD::STRICT_##DAGN; \
8358 break;
8359#include "llvm/IR/ConstrainedOps.def"
8360 case Intrinsic::experimental_constrained_fmuladd: {
8361 Opcode = ISD::STRICT_FMA;
8362 // Break fmuladd into fmul and fadd.
8365 Opers.pop_back();
8366 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8367 pushOutChain(Mul, EB);
8368 Opcode = ISD::STRICT_FADD;
8369 Opers.clear();
8370 Opers.push_back(Mul.getValue(1));
8371 Opers.push_back(Mul.getValue(0));
8372 Opers.push_back(getValue(FPI.getArgOperand(2)));
8373 }
8374 break;
8375 }
8376 }
8377
8378 // A few strict DAG nodes carry additional operands that are not
8379 // set up by the default code above.
8380 switch (Opcode) {
8381 default: break;
8383 Opers.push_back(
8385 break;
8386 case ISD::STRICT_FSETCC:
8387 case ISD::STRICT_FSETCCS: {
8388 auto *FPCmp = dyn_cast<ConstrainedFPCmpIntrinsic>(&FPI);
8389 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8390 if (TM.Options.NoNaNsFPMath)
8391 Condition = getFCmpCodeWithoutNaN(Condition);
8392 Opers.push_back(DAG.getCondCode(Condition));
8393 break;
8394 }
8395 }
8396
8397 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8398 pushOutChain(Result, EB);
8399
8400 SDValue FPResult = Result.getValue(0);
8401 setValue(&FPI, FPResult);
8402}
8403
8404static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8405 std::optional<unsigned> ResOPC;
8406 switch (VPIntrin.getIntrinsicID()) {
8407 case Intrinsic::vp_ctlz: {
8408 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8409 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8410 break;
8411 }
8412 case Intrinsic::vp_cttz: {
8413 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8414 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8415 break;
8416 }
8417 case Intrinsic::vp_cttz_elts: {
8418 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8419 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8420 break;
8421 }
8422#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8423 case Intrinsic::VPID: \
8424 ResOPC = ISD::VPSD; \
8425 break;
8426#include "llvm/IR/VPIntrinsics.def"
8427 }
8428
8429 if (!ResOPC)
8431 "Inconsistency: no SDNode available for this VPIntrinsic!");
8432
8433 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8434 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8435 if (VPIntrin.getFastMathFlags().allowReassoc())
8436 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8437 : ISD::VP_REDUCE_FMUL;
8438 }
8439
8440 return *ResOPC;
8441}
8442
8443void SelectionDAGBuilder::visitVPLoad(
8444 const VPIntrinsic &VPIntrin, EVT VT,
8445 const SmallVectorImpl<SDValue> &OpValues) {
8446 SDLoc DL = getCurSDLoc();
8447 Value *PtrOperand = VPIntrin.getArgOperand(0);
8448 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8449 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8450 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8451 SDValue LD;
8452 // Do not serialize variable-length loads of constant memory with
8453 // anything.
8454 if (!Alignment)
8455 Alignment = DAG.getEVTAlign(VT);
8456 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8457 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8458 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8460 MachineMemOperand::Flags MMOFlags =
8461 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8463 MachinePointerInfo(PtrOperand), MMOFlags,
8464 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8465 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8466 MMO, false /*IsExpanding */);
8467 if (AddToChain)
8468 PendingLoads.push_back(LD.getValue(1));
8469 setValue(&VPIntrin, LD);
8470}
8471
8472void SelectionDAGBuilder::visitVPLoadFF(
8473 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8474 const SmallVectorImpl<SDValue> &OpValues) {
8475 assert(OpValues.size() == 3 && "Unexpected number of operands");
8476 SDLoc DL = getCurSDLoc();
8477 Value *PtrOperand = VPIntrin.getArgOperand(0);
8478 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8479 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8480 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8481 SDValue LD;
8482 // Do not serialize variable-length loads of constant memory with
8483 // anything.
8484 if (!Alignment)
8485 Alignment = DAG.getEVTAlign(VT);
8486 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8487 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8488 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8491 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8492 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8493 MMO);
8494 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8495 if (AddToChain)
8496 PendingLoads.push_back(LD.getValue(2));
8497 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8498}
8499
8500void SelectionDAGBuilder::visitVPGather(
8501 const VPIntrinsic &VPIntrin, EVT VT,
8502 const SmallVectorImpl<SDValue> &OpValues) {
8503 SDLoc DL = getCurSDLoc();
8505 Value *PtrOperand = VPIntrin.getArgOperand(0);
8506 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8507 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8508 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8509 SDValue LD;
8510 if (!Alignment)
8511 Alignment = DAG.getEVTAlign(VT.getScalarType());
8512 unsigned AS =
8513 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8514 MachineMemOperand::Flags MMOFlags =
8515 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8518 *Alignment, AAInfo, Ranges);
8519 SDValue Base, Index, Scale;
8520 bool UniformBase =
8521 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8522 VT.getScalarStoreSize());
8523 if (!UniformBase) {
8525 Index = getValue(PtrOperand);
8527 }
8528 EVT IdxVT = Index.getValueType();
8529 EVT EltTy = IdxVT.getVectorElementType();
8530 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8531 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8532 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8533 }
8534 LD = DAG.getGatherVP(
8535 DAG.getVTList(VT, MVT::Other), VT, DL,
8536 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8538 PendingLoads.push_back(LD.getValue(1));
8539 setValue(&VPIntrin, LD);
8540}
8541
8542void SelectionDAGBuilder::visitVPStore(
8543 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8544 SDLoc DL = getCurSDLoc();
8545 Value *PtrOperand = VPIntrin.getArgOperand(1);
8546 EVT VT = OpValues[0].getValueType();
8547 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8548 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8549 SDValue ST;
8550 if (!Alignment)
8551 Alignment = DAG.getEVTAlign(VT);
8552 SDValue Ptr = OpValues[1];
8553 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8555 MachineMemOperand::Flags MMOFlags =
8556 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8558 MachinePointerInfo(PtrOperand), MMOFlags,
8559 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8560 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8561 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8562 /* IsTruncating */ false, /*IsCompressing*/ false);
8563 DAG.setRoot(ST);
8564 setValue(&VPIntrin, ST);
8565}
8566
8567void SelectionDAGBuilder::visitVPScatter(
8568 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8569 SDLoc DL = getCurSDLoc();
8571 Value *PtrOperand = VPIntrin.getArgOperand(1);
8572 EVT VT = OpValues[0].getValueType();
8573 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8574 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8575 SDValue ST;
8576 if (!Alignment)
8577 Alignment = DAG.getEVTAlign(VT.getScalarType());
8578 unsigned AS =
8579 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8580 MachineMemOperand::Flags MMOFlags =
8581 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8584 *Alignment, AAInfo);
8585 SDValue Base, Index, Scale;
8586 bool UniformBase =
8587 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8588 VT.getScalarStoreSize());
8589 if (!UniformBase) {
8591 Index = getValue(PtrOperand);
8593 }
8594 EVT IdxVT = Index.getValueType();
8595 EVT EltTy = IdxVT.getVectorElementType();
8596 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8597 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8598 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8599 }
8600 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8601 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8602 OpValues[2], OpValues[3]},
8603 MMO, ISD::SIGNED_SCALED);
8604 DAG.setRoot(ST);
8605 setValue(&VPIntrin, ST);
8606}
8607
8608void SelectionDAGBuilder::visitVPStridedLoad(
8609 const VPIntrinsic &VPIntrin, EVT VT,
8610 const SmallVectorImpl<SDValue> &OpValues) {
8611 SDLoc DL = getCurSDLoc();
8612 Value *PtrOperand = VPIntrin.getArgOperand(0);
8613 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8614 if (!Alignment)
8615 Alignment = DAG.getEVTAlign(VT.getScalarType());
8616 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8617 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8618 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8619 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8620 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8621 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8623 MachineMemOperand::Flags MMOFlags =
8624 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8627 *Alignment, AAInfo, Ranges);
8628
8629 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8630 OpValues[2], OpValues[3], MMO,
8631 false /*IsExpanding*/);
8632
8633 if (AddToChain)
8634 PendingLoads.push_back(LD.getValue(1));
8635 setValue(&VPIntrin, LD);
8636}
8637
8638void SelectionDAGBuilder::visitVPStridedStore(
8639 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8640 SDLoc DL = getCurSDLoc();
8641 Value *PtrOperand = VPIntrin.getArgOperand(1);
8642 EVT VT = OpValues[0].getValueType();
8643 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8644 if (!Alignment)
8645 Alignment = DAG.getEVTAlign(VT.getScalarType());
8646 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8647 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8649 MachineMemOperand::Flags MMOFlags =
8650 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8653 *Alignment, AAInfo);
8654
8656 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8657 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8658 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8659 /*IsCompressing*/ false);
8660
8661 DAG.setRoot(ST);
8662 setValue(&VPIntrin, ST);
8663}
8664
8665void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8667 SDLoc DL = getCurSDLoc();
8668
8669 ISD::CondCode Condition;
8671 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8672 if (IsFP) {
8673 // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
8674 // flags, but calls that don't return floating-point types can't be
8675 // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
8676 Condition = getFCmpCondCode(CondCode);
8677 if (TM.Options.NoNaNsFPMath)
8678 Condition = getFCmpCodeWithoutNaN(Condition);
8679 } else {
8680 Condition = getICmpCondCode(CondCode);
8681 }
8682
8683 SDValue Op1 = getValue(VPIntrin.getOperand(0));
8684 SDValue Op2 = getValue(VPIntrin.getOperand(1));
8685 // #2 is the condition code
8686 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8687 SDValue EVL = getValue(VPIntrin.getOperand(4));
8688 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8689 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8690 "Unexpected target EVL type");
8691 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8692
8694 VPIntrin.getType());
8695 setValue(&VPIntrin,
8696 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8697}
8698
8699void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8700 const VPIntrinsic &VPIntrin) {
8701 SDLoc DL = getCurSDLoc();
8702 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8703
8704 auto IID = VPIntrin.getIntrinsicID();
8705
8706 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8707 return visitVPCmp(*CmpI);
8708
8709 SmallVector<EVT, 4> ValueVTs;
8711 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8712 SDVTList VTs = DAG.getVTList(ValueVTs);
8713
8714 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8715
8716 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8717 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8718 "Unexpected target EVL type");
8719
8720 // Request operands.
8721 SmallVector<SDValue, 7> OpValues;
8722 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8723 auto Op = getValue(VPIntrin.getArgOperand(I));
8724 if (I == EVLParamPos)
8725 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8726 OpValues.push_back(Op);
8727 }
8728
8729 switch (Opcode) {
8730 default: {
8731 SDNodeFlags SDFlags;
8732 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8733 SDFlags.copyFMF(*FPMO);
8734 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8735 setValue(&VPIntrin, Result);
8736 break;
8737 }
8738 case ISD::VP_LOAD:
8739 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8740 break;
8741 case ISD::VP_LOAD_FF:
8742 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8743 break;
8744 case ISD::VP_GATHER:
8745 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8746 break;
8747 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8748 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8749 break;
8750 case ISD::VP_STORE:
8751 visitVPStore(VPIntrin, OpValues);
8752 break;
8753 case ISD::VP_SCATTER:
8754 visitVPScatter(VPIntrin, OpValues);
8755 break;
8756 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8757 visitVPStridedStore(VPIntrin, OpValues);
8758 break;
8759 case ISD::VP_FMULADD: {
8760 assert(OpValues.size() == 5 && "Unexpected number of operands");
8761 SDNodeFlags SDFlags;
8762 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8763 SDFlags.copyFMF(*FPMO);
8766 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8767 } else {
8769 ISD::VP_FMUL, DL, VTs,
8770 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8771 SDValue Add =
8772 DAG.getNode(ISD::VP_FADD, DL, VTs,
8773 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8774 setValue(&VPIntrin, Add);
8775 }
8776 break;
8777 }
8778 case ISD::VP_IS_FPCLASS: {
8779 const DataLayout DLayout = DAG.getDataLayout();
8780 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8781 auto Constant = OpValues[1]->getAsZExtVal();
8783 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8784 {OpValues[0], Check, OpValues[2], OpValues[3]});
8785 setValue(&VPIntrin, V);
8786 return;
8787 }
8788 case ISD::VP_INTTOPTR: {
8789 SDValue N = OpValues[0];
8790 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8791 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8792 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8793 OpValues[2]);
8794 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8795 OpValues[2]);
8796 setValue(&VPIntrin, N);
8797 break;
8798 }
8799 case ISD::VP_PTRTOINT: {
8800 SDValue N = OpValues[0];
8802 VPIntrin.getType());
8803 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
8804 VPIntrin.getOperand(0)->getType());
8805 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8806 OpValues[2]);
8807 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8808 OpValues[2]);
8809 setValue(&VPIntrin, N);
8810 break;
8811 }
8812 case ISD::VP_ABS:
8813 case ISD::VP_CTLZ:
8814 case ISD::VP_CTLZ_ZERO_UNDEF:
8815 case ISD::VP_CTTZ:
8816 case ISD::VP_CTTZ_ZERO_UNDEF:
8817 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
8818 case ISD::VP_CTTZ_ELTS: {
8819 SDValue Result =
8820 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
8821 setValue(&VPIntrin, Result);
8822 break;
8823 }
8824 }
8825}
8826
8827SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
8828 const BasicBlock *EHPadBB,
8829 MCSymbol *&BeginLabel) {
8831
8832 // Insert a label before the invoke call to mark the try range. This can be
8833 // used to detect deletion of the invoke via the MachineModuleInfo.
8834 BeginLabel = MF.getContext().createTempSymbol();
8835
8836 // For SjLj, keep track of which landing pads go with which invokes
8837 // so as to maintain the ordering of pads in the LSDA.
8838 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
8839 if (CallSiteIndex) {
8840 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
8841 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
8842
8843 // Now that the call site is handled, stop tracking it.
8845 }
8846
8847 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
8848}
8849
8850SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
8851 const BasicBlock *EHPadBB,
8852 MCSymbol *BeginLabel) {
8853 assert(BeginLabel && "BeginLabel should've been set");
8854
8856
8857 // Insert a label at the end of the invoke call to mark the try range. This
8858 // can be used to detect deletion of the invoke via the MachineModuleInfo.
8859 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
8860 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
8861
8862 // Inform MachineModuleInfo of range.
8864 // There is a platform (e.g. wasm) that uses funclet style IR but does not
8865 // actually use outlined funclets and their LSDA info style.
8866 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
8867 assert(II && "II should've been set");
8868 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
8869 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
8870 } else if (!isScopedEHPersonality(Pers)) {
8871 assert(EHPadBB);
8872 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
8873 }
8874
8875 return Chain;
8876}
8877
8878std::pair<SDValue, SDValue>
8880 const BasicBlock *EHPadBB) {
8881 MCSymbol *BeginLabel = nullptr;
8882
8883 if (EHPadBB) {
8884 // Both PendingLoads and PendingExports must be flushed here;
8885 // this call might not return.
8886 (void)getRoot();
8887 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
8888 CLI.setChain(getRoot());
8889 }
8890
8892 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
8893
8894 assert((CLI.IsTailCall || Result.second.getNode()) &&
8895 "Non-null chain expected with non-tail call!");
8896 assert((Result.second.getNode() || !Result.first.getNode()) &&
8897 "Null value expected with tail call!");
8898
8899 if (!Result.second.getNode()) {
8900 // As a special case, a null chain means that a tail call has been emitted
8901 // and the DAG root is already updated.
8902 HasTailCall = true;
8903
8904 // Since there's no actual continuation from this block, nothing can be
8905 // relying on us setting vregs for them.
8906 PendingExports.clear();
8907 } else {
8908 DAG.setRoot(Result.second);
8909 }
8910
8911 if (EHPadBB) {
8912 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
8913 BeginLabel));
8914 Result.second = getRoot();
8915 }
8916
8917 return Result;
8918}
8919
8921 bool isTailCall, bool isMustTailCall,
8922 const BasicBlock *EHPadBB,
8923 const TargetLowering::PtrAuthInfo *PAI) {
8924 auto &DL = DAG.getDataLayout();
8925 FunctionType *FTy = CB.getFunctionType();
8926 Type *RetTy = CB.getType();
8927
8929 Args.reserve(CB.arg_size());
8930
8931 const Value *SwiftErrorVal = nullptr;
8933
8934 if (isTailCall) {
8935 // Avoid emitting tail calls in functions with the disable-tail-calls
8936 // attribute.
8937 auto *Caller = CB.getParent()->getParent();
8938 if (Caller->getFnAttribute("disable-tail-calls").getValueAsString() ==
8939 "true" && !isMustTailCall)
8940 isTailCall = false;
8941
8942 // We can't tail call inside a function with a swifterror argument. Lowering
8943 // does not support this yet. It would have to move into the swifterror
8944 // register before the call.
8945 if (TLI.supportSwiftError() &&
8946 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
8947 isTailCall = false;
8948 }
8949
8950 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
8951 const Value *V = *I;
8952
8953 // Skip empty types
8954 if (V->getType()->isEmptyTy())
8955 continue;
8956
8957 SDValue ArgNode = getValue(V);
8958 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
8959 Entry.setAttributes(&CB, I - CB.arg_begin());
8960
8961 // Use swifterror virtual register as input to the call.
8962 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
8963 SwiftErrorVal = V;
8964 // We find the virtual register for the actual swifterror argument.
8965 // Instead of using the Value, we use the virtual register instead.
8966 Entry.Node =
8968 EVT(TLI.getPointerTy(DL)));
8969 }
8970
8971 Args.push_back(Entry);
8972
8973 // If we have an explicit sret argument that is an Instruction, (i.e., it
8974 // might point to function-local memory), we can't meaningfully tail-call.
8975 if (Entry.IsSRet && isa<Instruction>(V))
8976 isTailCall = false;
8977 }
8978
8979 // If call site has a cfguardtarget operand bundle, create and add an
8980 // additional ArgListEntry.
8981 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
8982 Value *V = Bundle->Inputs[0];
8984 Entry.IsCFGuardTarget = true;
8985 Args.push_back(Entry);
8986 }
8987
8988 // Check if target-independent constraints permit a tail call here.
8989 // Target-dependent constraints are checked within TLI->LowerCallTo.
8990 if (isTailCall && !isInTailCallPosition(CB, DAG.getTarget()))
8991 isTailCall = false;
8992
8993 // Disable tail calls if there is an swifterror argument. Targets have not
8994 // been updated to support tail calls.
8995 if (TLI.supportSwiftError() && SwiftErrorVal)
8996 isTailCall = false;
8997
8998 ConstantInt *CFIType = nullptr;
8999 if (CB.isIndirectCall()) {
9000 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9001 if (!TLI.supportKCFIBundles())
9003 "Target doesn't support calls with kcfi operand bundles.");
9004 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9005 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9006 }
9007 }
9008
9009 SDValue ConvControlToken;
9010 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9011 auto *Token = Bundle->Inputs[0].get();
9012 ConvControlToken = getValue(Token);
9013 }
9014
9017 .setChain(getRoot())
9018 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9019 .setTailCall(isTailCall)
9023 .setCFIType(CFIType)
9024 .setConvergenceControlToken(ConvControlToken);
9025
9026 // Set the pointer authentication info if we have it.
9027 if (PAI) {
9028 if (!TLI.supportPtrAuthBundles())
9030 "This target doesn't support calls with ptrauth operand bundles.");
9031 CLI.setPtrAuth(*PAI);
9032 }
9033
9034 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9035
9036 if (Result.first.getNode()) {
9037 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9038 setValue(&CB, Result.first);
9039 }
9040
9041 // The last element of CLI.InVals has the SDValue for swifterror return.
9042 // Here we copy it to a virtual register and update SwiftErrorMap for
9043 // book-keeping.
9044 if (SwiftErrorVal && TLI.supportSwiftError()) {
9045 // Get the last element of InVals.
9046 SDValue Src = CLI.InVals.back();
9047 Register VReg =
9048 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9049 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9050 DAG.setRoot(CopyNode);
9051 }
9052}
9053
9054static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9055 SelectionDAGBuilder &Builder) {
9056 // Check to see if this load can be trivially constant folded, e.g. if the
9057 // input is from a string literal.
9058 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9059 // Cast pointer to the type we really want to load.
9060 Type *LoadTy =
9061 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9062 if (LoadVT.isVector())
9063 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9064 if (const Constant *LoadCst =
9065 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9066 LoadTy, Builder.DAG.getDataLayout()))
9067 return Builder.getValue(LoadCst);
9068 }
9069
9070 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9071 // still constant memory, the input chain can be the entry node.
9072 SDValue Root;
9073 bool ConstantMemory = false;
9074
9075 // Do not serialize (non-volatile) loads of constant memory with anything.
9076 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9077 Root = Builder.DAG.getEntryNode();
9078 ConstantMemory = true;
9079 } else {
9080 // Do not serialize non-volatile loads against each other.
9081 Root = Builder.DAG.getRoot();
9082 }
9083
9084 SDValue Ptr = Builder.getValue(PtrVal);
9085 SDValue LoadVal =
9086 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9087 MachinePointerInfo(PtrVal), Align(1));
9088
9089 if (!ConstantMemory)
9090 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9091 return LoadVal;
9092}
9093
9094/// Record the value for an instruction that produces an integer result,
9095/// converting the type where necessary.
9096void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9097 SDValue Value,
9098 bool IsSigned) {
9100 I.getType(), true);
9101 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9102 setValue(&I, Value);
9103}
9104
9105/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9106/// true and lower it. Otherwise return false, and it will be lowered like a
9107/// normal call.
9108/// The caller already checked that \p I calls the appropriate LibFunc with a
9109/// correct prototype.
9110bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9111 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9112 const Value *Size = I.getArgOperand(2);
9113 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9114 if (CSize && CSize->getZExtValue() == 0) {
9116 I.getType(), true);
9117 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9118 return true;
9119 }
9120
9122 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9123 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9124 getValue(Size), &I);
9125 if (Res.first.getNode()) {
9126 processIntegerCallValue(I, Res.first, true);
9127 PendingLoads.push_back(Res.second);
9128 return true;
9129 }
9130
9131 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9132 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9133 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9134 return false;
9135
9136 // If the target has a fast compare for the given size, it will return a
9137 // preferred load type for that size. Require that the load VT is legal and
9138 // that the target supports unaligned loads of that type. Otherwise, return
9139 // INVALID.
9140 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9142 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9143 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9144 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9145 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9146 // TODO: Check alignment of src and dest ptrs.
9147 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9148 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9149 if (!TLI.isTypeLegal(LVT) ||
9150 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9151 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9153 }
9154
9155 return LVT;
9156 };
9157
9158 // This turns into unaligned loads. We only do this if the target natively
9159 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9160 // we'll only produce a small number of byte loads.
9161 MVT LoadVT;
9162 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9163 switch (NumBitsToCompare) {
9164 default:
9165 return false;
9166 case 16:
9167 LoadVT = MVT::i16;
9168 break;
9169 case 32:
9170 LoadVT = MVT::i32;
9171 break;
9172 case 64:
9173 case 128:
9174 case 256:
9175 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9176 break;
9177 }
9178
9179 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9180 return false;
9181
9182 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9183 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9184
9185 // Bitcast to a wide integer type if the loads are vectors.
9186 if (LoadVT.isVector()) {
9187 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9188 LoadL = DAG.getBitcast(CmpVT, LoadL);
9189 LoadR = DAG.getBitcast(CmpVT, LoadR);
9190 }
9191
9192 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9193 processIntegerCallValue(I, Cmp, false);
9194 return true;
9195}
9196
9197/// See if we can lower a memchr call into an optimized form. If so, return
9198/// true and lower it. Otherwise return false, and it will be lowered like a
9199/// normal call.
9200/// The caller already checked that \p I calls the appropriate LibFunc with a
9201/// correct prototype.
9202bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9203 const Value *Src = I.getArgOperand(0);
9204 const Value *Char = I.getArgOperand(1);
9205 const Value *Length = I.getArgOperand(2);
9206
9208 std::pair<SDValue, SDValue> Res =
9210 getValue(Src), getValue(Char), getValue(Length),
9211 MachinePointerInfo(Src));
9212 if (Res.first.getNode()) {
9213 setValue(&I, Res.first);
9214 PendingLoads.push_back(Res.second);
9215 return true;
9216 }
9217
9218 return false;
9219}
9220
9221/// See if we can lower a mempcpy call into an optimized form. If so, return
9222/// true and lower it. Otherwise return false, and it will be lowered like a
9223/// normal call.
9224/// The caller already checked that \p I calls the appropriate LibFunc with a
9225/// correct prototype.
9226bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9227 SDValue Dst = getValue(I.getArgOperand(0));
9228 SDValue Src = getValue(I.getArgOperand(1));
9229 SDValue Size = getValue(I.getArgOperand(2));
9230
9231 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9232 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9233 // DAG::getMemcpy needs Alignment to be defined.
9234 Align Alignment = std::min(DstAlign, SrcAlign);
9235
9236 SDLoc sdl = getCurSDLoc();
9237
9238 // In the mempcpy context we need to pass in a false value for isTailCall
9239 // because the return pointer needs to be adjusted by the size of
9240 // the copied memory.
9241 SDValue Root = getMemoryRoot();
9242 SDValue MC = DAG.getMemcpy(
9243 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9244 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9245 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9246 assert(MC.getNode() != nullptr &&
9247 "** memcpy should not be lowered as TailCall in mempcpy context **");
9248 DAG.setRoot(MC);
9249
9250 // Check if Size needs to be truncated or extended.
9251 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9252
9253 // Adjust return pointer to point just past the last dst byte.
9254 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9255 setValue(&I, DstPlusSize);
9256 return true;
9257}
9258
9259/// See if we can lower a strcpy call into an optimized form. If so, return
9260/// true and lower it, otherwise return false and it will be lowered like a
9261/// normal call.
9262/// The caller already checked that \p I calls the appropriate LibFunc with a
9263/// correct prototype.
9264bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9265 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9266
9268 std::pair<SDValue, SDValue> Res =
9270 getValue(Arg0), getValue(Arg1),
9271 MachinePointerInfo(Arg0),
9272 MachinePointerInfo(Arg1), isStpcpy);
9273 if (Res.first.getNode()) {
9274 setValue(&I, Res.first);
9275 DAG.setRoot(Res.second);
9276 return true;
9277 }
9278
9279 return false;
9280}
9281
9282/// See if we can lower a strcmp call into an optimized form. If so, return
9283/// true and lower it, otherwise return false and it will be lowered like a
9284/// normal call.
9285/// The caller already checked that \p I calls the appropriate LibFunc with a
9286/// correct prototype.
9287bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9288 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9289
9291 std::pair<SDValue, SDValue> Res =
9293 getValue(Arg0), getValue(Arg1),
9294 MachinePointerInfo(Arg0),
9295 MachinePointerInfo(Arg1));
9296 if (Res.first.getNode()) {
9297 processIntegerCallValue(I, Res.first, true);
9298 PendingLoads.push_back(Res.second);
9299 return true;
9300 }
9301
9302 return false;
9303}
9304
9305/// See if we can lower a strlen call into an optimized form. If so, return
9306/// true and lower it, otherwise return false and it will be lowered like a
9307/// normal call.
9308/// The caller already checked that \p I calls the appropriate LibFunc with a
9309/// correct prototype.
9310bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9311 const Value *Arg0 = I.getArgOperand(0);
9312
9314 std::pair<SDValue, SDValue> Res =
9316 getValue(Arg0), MachinePointerInfo(Arg0));
9317 if (Res.first.getNode()) {
9318 processIntegerCallValue(I, Res.first, false);
9319 PendingLoads.push_back(Res.second);
9320 return true;
9321 }
9322
9323 return false;
9324}
9325
9326/// See if we can lower a strnlen call into an optimized form. If so, return
9327/// true and lower it, otherwise return false and it will be lowered like a
9328/// normal call.
9329/// The caller already checked that \p I calls the appropriate LibFunc with a
9330/// correct prototype.
9331bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9332 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9333
9335 std::pair<SDValue, SDValue> Res =
9337 getValue(Arg0), getValue(Arg1),
9338 MachinePointerInfo(Arg0));
9339 if (Res.first.getNode()) {
9340 processIntegerCallValue(I, Res.first, false);
9341 PendingLoads.push_back(Res.second);
9342 return true;
9343 }
9344
9345 return false;
9346}
9347
9348/// See if we can lower a unary floating-point operation into an SDNode with
9349/// the specified Opcode. If so, return true and lower it, otherwise return
9350/// false and it will be lowered like a normal call.
9351/// The caller already checked that \p I calls the appropriate LibFunc with a
9352/// correct prototype.
9353bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9354 unsigned Opcode) {
9355 // We already checked this call's prototype; verify it doesn't modify errno.
9356 if (!I.onlyReadsMemory())
9357 return false;
9358
9360 Flags.copyFMF(cast<FPMathOperator>(I));
9361
9362 SDValue Tmp = getValue(I.getArgOperand(0));
9363 setValue(&I,
9364 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9365 return true;
9366}
9367
9368/// See if we can lower a binary floating-point operation into an SDNode with
9369/// the specified Opcode. If so, return true and lower it. Otherwise return
9370/// false, and it will be lowered like a normal call.
9371/// The caller already checked that \p I calls the appropriate LibFunc with a
9372/// correct prototype.
9373bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9374 unsigned Opcode) {
9375 // We already checked this call's prototype; verify it doesn't modify errno.
9376 if (!I.onlyReadsMemory())
9377 return false;
9378
9380 Flags.copyFMF(cast<FPMathOperator>(I));
9381
9382 SDValue Tmp0 = getValue(I.getArgOperand(0));
9383 SDValue Tmp1 = getValue(I.getArgOperand(1));
9384 EVT VT = Tmp0.getValueType();
9385 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9386 return true;
9387}
9388
9389void SelectionDAGBuilder::visitCall(const CallInst &I) {
9390 // Handle inline assembly differently.
9391 if (I.isInlineAsm()) {
9392 visitInlineAsm(I);
9393 return;
9394 }
9395
9397
9398 if (Function *F = I.getCalledFunction()) {
9399 if (F->isDeclaration()) {
9400 // Is this an LLVM intrinsic?
9401 if (unsigned IID = F->getIntrinsicID()) {
9402 visitIntrinsicCall(I, IID);
9403 return;
9404 }
9405 }
9406
9407 // Check for well-known libc/libm calls. If the function is internal, it
9408 // can't be a library call. Don't do the check if marked as nobuiltin for
9409 // some reason or the call site requires strict floating point semantics.
9410 LibFunc Func;
9411 if (!I.isNoBuiltin() && !I.isStrictFP() && !F->hasLocalLinkage() &&
9412 F->hasName() && LibInfo->getLibFunc(*F, Func) &&
9414 switch (Func) {
9415 default: break;
9416 case LibFunc_bcmp:
9417 if (visitMemCmpBCmpCall(I))
9418 return;
9419 break;
9420 case LibFunc_copysign:
9421 case LibFunc_copysignf:
9422 case LibFunc_copysignl:
9423 // We already checked this call's prototype; verify it doesn't modify
9424 // errno.
9425 if (I.onlyReadsMemory()) {
9426 SDValue LHS = getValue(I.getArgOperand(0));
9427 SDValue RHS = getValue(I.getArgOperand(1));
9429 LHS.getValueType(), LHS, RHS));
9430 return;
9431 }
9432 break;
9433 case LibFunc_fabs:
9434 case LibFunc_fabsf:
9435 case LibFunc_fabsl:
9436 if (visitUnaryFloatCall(I, ISD::FABS))
9437 return;
9438 break;
9439 case LibFunc_fmin:
9440 case LibFunc_fminf:
9441 case LibFunc_fminl:
9442 if (visitBinaryFloatCall(I, ISD::FMINNUM))
9443 return;
9444 break;
9445 case LibFunc_fmax:
9446 case LibFunc_fmaxf:
9447 case LibFunc_fmaxl:
9448 if (visitBinaryFloatCall(I, ISD::FMAXNUM))
9449 return;
9450 break;
9451 case LibFunc_fminimum_num:
9452 case LibFunc_fminimum_numf:
9453 case LibFunc_fminimum_numl:
9454 if (visitBinaryFloatCall(I, ISD::FMINIMUMNUM))
9455 return;
9456 break;
9457 case LibFunc_fmaximum_num:
9458 case LibFunc_fmaximum_numf:
9459 case LibFunc_fmaximum_numl:
9460 if (visitBinaryFloatCall(I, ISD::FMAXIMUMNUM))
9461 return;
9462 break;
9463 case LibFunc_sin:
9464 case LibFunc_sinf:
9465 case LibFunc_sinl:
9466 if (visitUnaryFloatCall(I, ISD::FSIN))
9467 return;
9468 break;
9469 case LibFunc_cos:
9470 case LibFunc_cosf:
9471 case LibFunc_cosl:
9472 if (visitUnaryFloatCall(I, ISD::FCOS))
9473 return;
9474 break;
9475 case LibFunc_tan:
9476 case LibFunc_tanf:
9477 case LibFunc_tanl:
9478 if (visitUnaryFloatCall(I, ISD::FTAN))
9479 return;
9480 break;
9481 case LibFunc_asin:
9482 case LibFunc_asinf:
9483 case LibFunc_asinl:
9484 if (visitUnaryFloatCall(I, ISD::FASIN))
9485 return;
9486 break;
9487 case LibFunc_acos:
9488 case LibFunc_acosf:
9489 case LibFunc_acosl:
9490 if (visitUnaryFloatCall(I, ISD::FACOS))
9491 return;
9492 break;
9493 case LibFunc_atan:
9494 case LibFunc_atanf:
9495 case LibFunc_atanl:
9496 if (visitUnaryFloatCall(I, ISD::FATAN))
9497 return;
9498 break;
9499 case LibFunc_atan2:
9500 case LibFunc_atan2f:
9501 case LibFunc_atan2l:
9502 if (visitBinaryFloatCall(I, ISD::FATAN2))
9503 return;
9504 break;
9505 case LibFunc_sinh:
9506 case LibFunc_sinhf:
9507 case LibFunc_sinhl:
9508 if (visitUnaryFloatCall(I, ISD::FSINH))
9509 return;
9510 break;
9511 case LibFunc_cosh:
9512 case LibFunc_coshf:
9513 case LibFunc_coshl:
9514 if (visitUnaryFloatCall(I, ISD::FCOSH))
9515 return;
9516 break;
9517 case LibFunc_tanh:
9518 case LibFunc_tanhf:
9519 case LibFunc_tanhl:
9520 if (visitUnaryFloatCall(I, ISD::FTANH))
9521 return;
9522 break;
9523 case LibFunc_sqrt:
9524 case LibFunc_sqrtf:
9525 case LibFunc_sqrtl:
9526 case LibFunc_sqrt_finite:
9527 case LibFunc_sqrtf_finite:
9528 case LibFunc_sqrtl_finite:
9529 if (visitUnaryFloatCall(I, ISD::FSQRT))
9530 return;
9531 break;
9532 case LibFunc_floor:
9533 case LibFunc_floorf:
9534 case LibFunc_floorl:
9535 if (visitUnaryFloatCall(I, ISD::FFLOOR))
9536 return;
9537 break;
9538 case LibFunc_nearbyint:
9539 case LibFunc_nearbyintf:
9540 case LibFunc_nearbyintl:
9541 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
9542 return;
9543 break;
9544 case LibFunc_ceil:
9545 case LibFunc_ceilf:
9546 case LibFunc_ceill:
9547 if (visitUnaryFloatCall(I, ISD::FCEIL))
9548 return;
9549 break;
9550 case LibFunc_rint:
9551 case LibFunc_rintf:
9552 case LibFunc_rintl:
9553 if (visitUnaryFloatCall(I, ISD::FRINT))
9554 return;
9555 break;
9556 case LibFunc_round:
9557 case LibFunc_roundf:
9558 case LibFunc_roundl:
9559 if (visitUnaryFloatCall(I, ISD::FROUND))
9560 return;
9561 break;
9562 case LibFunc_trunc:
9563 case LibFunc_truncf:
9564 case LibFunc_truncl:
9565 if (visitUnaryFloatCall(I, ISD::FTRUNC))
9566 return;
9567 break;
9568 case LibFunc_log2:
9569 case LibFunc_log2f:
9570 case LibFunc_log2l:
9571 if (visitUnaryFloatCall(I, ISD::FLOG2))
9572 return;
9573 break;
9574 case LibFunc_exp2:
9575 case LibFunc_exp2f:
9576 case LibFunc_exp2l:
9577 if (visitUnaryFloatCall(I, ISD::FEXP2))
9578 return;
9579 break;
9580 case LibFunc_exp10:
9581 case LibFunc_exp10f:
9582 case LibFunc_exp10l:
9583 if (visitUnaryFloatCall(I, ISD::FEXP10))
9584 return;
9585 break;
9586 case LibFunc_ldexp:
9587 case LibFunc_ldexpf:
9588 case LibFunc_ldexpl:
9589 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9590 return;
9591 break;
9592 case LibFunc_memcmp:
9593 if (visitMemCmpBCmpCall(I))
9594 return;
9595 break;
9596 case LibFunc_mempcpy:
9597 if (visitMemPCpyCall(I))
9598 return;
9599 break;
9600 case LibFunc_memchr:
9601 if (visitMemChrCall(I))
9602 return;
9603 break;
9604 case LibFunc_strcpy:
9605 if (visitStrCpyCall(I, false))
9606 return;
9607 break;
9608 case LibFunc_stpcpy:
9609 if (visitStrCpyCall(I, true))
9610 return;
9611 break;
9612 case LibFunc_strcmp:
9613 if (visitStrCmpCall(I))
9614 return;
9615 break;
9616 case LibFunc_strlen:
9617 if (visitStrLenCall(I))
9618 return;
9619 break;
9620 case LibFunc_strnlen:
9621 if (visitStrNLenCall(I))
9622 return;
9623 break;
9624 }
9625 }
9626 }
9627
9628 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9629 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9630 return;
9631 }
9632
9633 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9634 // have to do anything here to lower funclet bundles.
9635 // CFGuardTarget bundles are lowered in LowerCallTo.
9637 I, "calls",
9642
9643 SDValue Callee = getValue(I.getCalledOperand());
9644
9645 if (I.hasDeoptState())
9646 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9647 else
9648 // Check if we can potentially perform a tail call. More detailed checking
9649 // is be done within LowerCallTo, after more information about the call is
9650 // known.
9651 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9652}
9653
9655 const CallBase &CB, const BasicBlock *EHPadBB) {
9656 auto PAB = CB.getOperandBundle("ptrauth");
9657 const Value *CalleeV = CB.getCalledOperand();
9658
9659 // Gather the call ptrauth data from the operand bundle:
9660 // [ i32 <key>, i64 <discriminator> ]
9661 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9662 const Value *Discriminator = PAB->Inputs[1];
9663
9664 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9665 assert(Discriminator->getType()->isIntegerTy(64) &&
9666 "Invalid ptrauth discriminator");
9667
9668 // Look through ptrauth constants to find the raw callee.
9669 // Do a direct unauthenticated call if we found it and everything matches.
9670 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9671 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9672 DAG.getDataLayout()))
9673 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9674 CB.isMustTailCall(), EHPadBB);
9675
9676 // Functions should never be ptrauth-called directly.
9677 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9678
9679 // Otherwise, do an authenticated indirect call.
9680 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9681 getValue(Discriminator)};
9682
9683 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9684 EHPadBB, &PAI);
9685}
9686
9687namespace {
9688
9689/// AsmOperandInfo - This contains information for each constraint that we are
9690/// lowering.
9691class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9692public:
9693 /// CallOperand - If this is the result output operand or a clobber
9694 /// this is null, otherwise it is the incoming operand to the CallInst.
9695 /// This gets modified as the asm is processed.
9696 SDValue CallOperand;
9697
9698 /// AssignedRegs - If this is a register or register class operand, this
9699 /// contains the set of register corresponding to the operand.
9700 RegsForValue AssignedRegs;
9701
9702 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9703 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9704 }
9705
9706 /// Whether or not this operand accesses memory
9707 bool hasMemory(const TargetLowering &TLI) const {
9708 // Indirect operand accesses access memory.
9709 if (isIndirect)
9710 return true;
9711
9712 for (const auto &Code : Codes)
9714 return true;
9715
9716 return false;
9717 }
9718};
9719
9720
9721} // end anonymous namespace
9722
9723/// Make sure that the output operand \p OpInfo and its corresponding input
9724/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9725/// out).
9726static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9727 SDISelAsmOperandInfo &MatchingOpInfo,
9728 SelectionDAG &DAG) {
9729 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9730 return;
9731
9733 const auto &TLI = DAG.getTargetLoweringInfo();
9734
9735 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9736 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9737 OpInfo.ConstraintVT);
9738 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9739 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9740 MatchingOpInfo.ConstraintVT);
9741 const bool OutOpIsIntOrFP =
9742 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9743 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9744 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9745 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9746 // FIXME: error out in a more elegant fashion
9747 report_fatal_error("Unsupported asm: input constraint"
9748 " with a matching output constraint of"
9749 " incompatible type!");
9750 }
9751 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9752}
9753
9754/// Get a direct memory input to behave well as an indirect operand.
9755/// This may introduce stores, hence the need for a \p Chain.
9756/// \return The (possibly updated) chain.
9757static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9758 SDISelAsmOperandInfo &OpInfo,
9759 SelectionDAG &DAG) {
9760 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9761
9762 // If we don't have an indirect input, put it in the constpool if we can,
9763 // otherwise spill it to a stack slot.
9764 // TODO: This isn't quite right. We need to handle these according to
9765 // the addressing mode that the constraint wants. Also, this may take
9766 // an additional register for the computation and we don't want that
9767 // either.
9768
9769 // If the operand is a float, integer, or vector constant, spill to a
9770 // constant pool entry to get its address.
9771 const Value *OpVal = OpInfo.CallOperandVal;
9772 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9773 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
9774 OpInfo.CallOperand = DAG.getConstantPool(
9775 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9776 return Chain;
9777 }
9778
9779 // Otherwise, create a stack slot and emit a store to it before the asm.
9780 Type *Ty = OpVal->getType();
9781 auto &DL = DAG.getDataLayout();
9782 TypeSize TySize = DL.getTypeAllocSize(Ty);
9785 int StackID = 0;
9786 if (TySize.isScalable())
9787 StackID = TFI->getStackIDForScalableVectors();
9788 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9789 DL.getPrefTypeAlign(Ty), false,
9790 nullptr, StackID);
9791 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9792 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9794 TLI.getMemValueType(DL, Ty));
9795 OpInfo.CallOperand = StackSlot;
9796
9797 return Chain;
9798}
9799
9800/// GetRegistersForValue - Assign registers (virtual or physical) for the
9801/// specified operand. We prefer to assign virtual registers, to allow the
9802/// register allocator to handle the assignment process. However, if the asm
9803/// uses features that we can't model on machineinstrs, we have SDISel do the
9804/// allocation. This produces generally horrible, but correct, code.
9805///
9806/// OpInfo describes the operand
9807/// RefOpInfo describes the matching operand if any, the operand otherwise
9808static std::optional<unsigned>
9810 SDISelAsmOperandInfo &OpInfo,
9811 SDISelAsmOperandInfo &RefOpInfo) {
9812 LLVMContext &Context = *DAG.getContext();
9813 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9814
9818
9819 // No work to do for memory/address operands.
9820 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9821 OpInfo.ConstraintType == TargetLowering::C_Address)
9822 return std::nullopt;
9823
9824 // If this is a constraint for a single physreg, or a constraint for a
9825 // register class, find it.
9826 unsigned AssignedReg;
9827 const TargetRegisterClass *RC;
9828 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
9829 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
9830 // RC is unset only on failure. Return immediately.
9831 if (!RC)
9832 return std::nullopt;
9833
9834 // Get the actual register value type. This is important, because the user
9835 // may have asked for (e.g.) the AX register in i32 type. We need to
9836 // remember that AX is actually i16 to get the right extension.
9837 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
9838
9839 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
9840 // If this is an FP operand in an integer register (or visa versa), or more
9841 // generally if the operand value disagrees with the register class we plan
9842 // to stick it in, fix the operand type.
9843 //
9844 // If this is an input value, the bitcast to the new type is done now.
9845 // Bitcast for output value is done at the end of visitInlineAsm().
9846 if ((OpInfo.Type == InlineAsm::isOutput ||
9847 OpInfo.Type == InlineAsm::isInput) &&
9848 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
9849 // Try to convert to the first EVT that the reg class contains. If the
9850 // types are identical size, use a bitcast to convert (e.g. two differing
9851 // vector types). Note: output bitcast is done at the end of
9852 // visitInlineAsm().
9853 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
9854 // Exclude indirect inputs while they are unsupported because the code
9855 // to perform the load is missing and thus OpInfo.CallOperand still
9856 // refers to the input address rather than the pointed-to value.
9857 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
9858 OpInfo.CallOperand =
9859 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
9860 OpInfo.ConstraintVT = RegVT;
9861 // If the operand is an FP value and we want it in integer registers,
9862 // use the corresponding integer type. This turns an f64 value into
9863 // i64, which can be passed with two i32 values on a 32-bit machine.
9864 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
9865 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
9866 if (OpInfo.Type == InlineAsm::isInput)
9867 OpInfo.CallOperand =
9868 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
9869 OpInfo.ConstraintVT = VT;
9870 }
9871 }
9872 }
9873
9874 // No need to allocate a matching input constraint since the constraint it's
9875 // matching to has already been allocated.
9876 if (OpInfo.isMatchingInputConstraint())
9877 return std::nullopt;
9878
9879 EVT ValueVT = OpInfo.ConstraintVT;
9880 if (OpInfo.ConstraintVT == MVT::Other)
9881 ValueVT = RegVT;
9882
9883 // Initialize NumRegs.
9884 unsigned NumRegs = 1;
9885 if (OpInfo.ConstraintVT != MVT::Other)
9886 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
9887
9888 // If this is a constraint for a specific physical register, like {r17},
9889 // assign it now.
9890
9891 // If this associated to a specific register, initialize iterator to correct
9892 // place. If virtual, make sure we have enough registers
9893
9894 // Initialize iterator if necessary
9897
9898 // Do not check for single registers.
9899 if (AssignedReg) {
9900 I = std::find(I, RC->end(), AssignedReg);
9901 if (I == RC->end()) {
9902 // RC does not contain the selected register, which indicates a
9903 // mismatch between the register and the required type/bitwidth.
9904 return {AssignedReg};
9905 }
9906 }
9907
9908 for (; NumRegs; --NumRegs, ++I) {
9909 assert(I != RC->end() && "Ran out of registers to allocate!");
9910 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
9911 Regs.push_back(R);
9912 }
9913
9914 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
9915 return std::nullopt;
9916}
9917
9918static unsigned
9920 const std::vector<SDValue> &AsmNodeOperands) {
9921 // Scan until we find the definition we already emitted of this operand.
9922 unsigned CurOp = InlineAsm::Op_FirstOperand;
9923 for (; OperandNo; --OperandNo) {
9924 // Advance to the next operand.
9925 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
9926 const InlineAsm::Flag F(OpFlag);
9927 assert(
9928 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
9929 "Skipped past definitions?");
9930 CurOp += F.getNumOperandRegisters() + 1;
9931 }
9932 return CurOp;
9933}
9934
9935namespace {
9936
9937class ExtraFlags {
9938 unsigned Flags = 0;
9939
9940public:
9941 explicit ExtraFlags(const CallBase &Call) {
9942 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
9943 if (IA->hasSideEffects())
9945 if (IA->isAlignStack())
9947 if (Call.isConvergent())
9949 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
9950 }
9951
9952 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
9953 // Ideally, we would only check against memory constraints. However, the
9954 // meaning of an Other constraint can be target-specific and we can't easily
9955 // reason about it. Therefore, be conservative and set MayLoad/MayStore
9956 // for Other constraints as well.
9959 if (OpInfo.Type == InlineAsm::isInput)
9961 else if (OpInfo.Type == InlineAsm::isOutput)
9963 else if (OpInfo.Type == InlineAsm::isClobber)
9965 }
9966 }
9967
9968 unsigned get() const { return Flags; }
9969};
9970
9971} // end anonymous namespace
9972
9973static bool isFunction(SDValue Op) {
9974 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
9975 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
9976 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
9977
9978 // In normal "call dllimport func" instruction (non-inlineasm) it force
9979 // indirect access by specifing call opcode. And usually specially print
9980 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
9981 // not do in this way now. (In fact, this is similar with "Data Access"
9982 // action). So here we ignore dllimport function.
9983 if (Fn && !Fn->hasDLLImportStorageClass())
9984 return true;
9985 }
9986 }
9987 return false;
9988}
9989
9990/// visitInlineAsm - Handle a call to an InlineAsm object.
9991void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
9992 const BasicBlock *EHPadBB) {
9993 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
9994
9995 /// ConstraintOperands - Information about all of the constraints.
9996 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
9997
10001
10002 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
10003 // AsmDialect, MayLoad, MayStore).
10004 bool HasSideEffect = IA->hasSideEffects();
10005 ExtraFlags ExtraInfo(Call);
10006
10007 for (auto &T : TargetConstraints) {
10008 ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10009 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
10010
10011 if (OpInfo.CallOperandVal)
10012 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
10013
10014 if (!HasSideEffect)
10015 HasSideEffect = OpInfo.hasMemory(TLI);
10016
10017 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10018 // FIXME: Could we compute this on OpInfo rather than T?
10019
10020 // Compute the constraint code and ConstraintType to use.
10022
10023 if (T.ConstraintType == TargetLowering::C_Immediate &&
10024 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
10025 // We've delayed emitting a diagnostic like the "n" constraint because
10026 // inlining could cause an integer showing up.
10027 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
10028 "' expects an integer constant "
10029 "expression");
10030
10031 ExtraInfo.update(T);
10032 }
10033
10034 // We won't need to flush pending loads if this asm doesn't touch
10035 // memory and is nonvolatile.
10036 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
10037
10038 bool EmitEHLabels = isa<InvokeInst>(Call);
10039 if (EmitEHLabels) {
10040 assert(EHPadBB && "InvokeInst must have an EHPadBB");
10041 }
10042 bool IsCallBr = isa<CallBrInst>(Call);
10043
10044 if (IsCallBr || EmitEHLabels) {
10045 // If this is a callbr or invoke we need to flush pending exports since
10046 // inlineasm_br and invoke are terminators.
10047 // We need to do this before nodes are glued to the inlineasm_br node.
10048 Chain = getControlRoot();
10049 }
10050
10051 MCSymbol *BeginLabel = nullptr;
10052 if (EmitEHLabels) {
10053 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
10054 }
10055
10056 int OpNo = -1;
10057 SmallVector<StringRef> AsmStrs;
10058 IA->collectAsmStrs(AsmStrs);
10059
10060 // Second pass over the constraints: compute which constraint option to use.
10061 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10062 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10063 OpNo++;
10064
10065 // If this is an output operand with a matching input operand, look up the
10066 // matching input. If their types mismatch, e.g. one is an integer, the
10067 // other is floating point, or their sizes are different, flag it as an
10068 // error.
10069 if (OpInfo.hasMatchingInput()) {
10070 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
10071 patchMatchingInput(OpInfo, Input, DAG);
10072 }
10073
10074 // Compute the constraint code and ConstraintType to use.
10075 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10076
10077 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10078 OpInfo.Type == InlineAsm::isClobber) ||
10079 OpInfo.ConstraintType == TargetLowering::C_Address)
10080 continue;
10081
10082 // In Linux PIC model, there are 4 cases about value/label addressing:
10083 //
10084 // 1: Function call or Label jmp inside the module.
10085 // 2: Data access (such as global variable, static variable) inside module.
10086 // 3: Function call or Label jmp outside the module.
10087 // 4: Data access (such as global variable) outside the module.
10088 //
10089 // Due to current llvm inline asm architecture designed to not "recognize"
10090 // the asm code, there are quite troubles for us to treat mem addressing
10091 // differently for same value/adress used in different instuctions.
10092 // For example, in pic model, call a func may in plt way or direclty
10093 // pc-related, but lea/mov a function adress may use got.
10094 //
10095 // Here we try to "recognize" function call for the case 1 and case 3 in
10096 // inline asm. And try to adjust the constraint for them.
10097 //
10098 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10099 // label, so here we don't handle jmp function label now, but we need to
10100 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10101 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10102 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10104 OpInfo.isIndirect = false;
10105 OpInfo.ConstraintType = TargetLowering::C_Address;
10106 }
10107
10108 // If this is a memory input, and if the operand is not indirect, do what we
10109 // need to provide an address for the memory input.
10110 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10111 !OpInfo.isIndirect) {
10112 assert((OpInfo.isMultipleAlternative ||
10113 (OpInfo.Type == InlineAsm::isInput)) &&
10114 "Can only indirectify direct input operands!");
10115
10116 // Memory operands really want the address of the value.
10117 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
10118
10119 // There is no longer a Value* corresponding to this operand.
10120 OpInfo.CallOperandVal = nullptr;
10121
10122 // It is now an indirect operand.
10123 OpInfo.isIndirect = true;
10124 }
10125
10126 }
10127
10128 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10129 std::vector<SDValue> AsmNodeOperands;
10130 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10131 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10132 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10133
10134 // If we have a !srcloc metadata node associated with it, we want to attach
10135 // this to the ultimately generated inline asm machineinstr. To do this, we
10136 // pass in the third operand as this (potentially null) inline asm MDNode.
10137 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10138 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10139
10140 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10141 // bits as operand 3.
10142 AsmNodeOperands.push_back(DAG.getTargetConstant(
10143 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10144
10145 // Third pass: Loop over operands to prepare DAG-level operands.. As part of
10146 // this, assign virtual and physical registers for inputs and otput.
10147 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10148 // Assign Registers.
10149 SDISelAsmOperandInfo &RefOpInfo =
10150 OpInfo.isMatchingInputConstraint()
10151 ? ConstraintOperands[OpInfo.getMatchedOperand()]
10152 : OpInfo;
10153 const auto RegError =
10154 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
10155 if (RegError) {
10158 const char *RegName = TRI.getName(*RegError);
10159 emitInlineAsmError(Call, "register '" + Twine(RegName) +
10160 "' allocated for constraint '" +
10161 Twine(OpInfo.ConstraintCode) +
10162 "' does not match required type");
10163 return;
10164 }
10165
10166 auto DetectWriteToReservedRegister = [&]() {
10169 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10170 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10171 const char *RegName = TRI.getName(Reg);
10172 emitInlineAsmError(Call, "write to reserved register '" +
10173 Twine(RegName) + "'");
10174 return true;
10175 }
10176 }
10177 return false;
10178 };
10179 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10180 (OpInfo.Type == InlineAsm::isInput &&
10181 !OpInfo.isMatchingInputConstraint())) &&
10182 "Only address as input operand is allowed.");
10183
10184 switch (OpInfo.Type) {
10186 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10187 const InlineAsm::ConstraintCode ConstraintID =
10188 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10190 "Failed to convert memory constraint code to constraint id.");
10191
10192 // Add information to the INLINEASM node to know about this output.
10194 OpFlags.setMemConstraint(ConstraintID);
10195 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
10196 MVT::i32));
10197 AsmNodeOperands.push_back(OpInfo.CallOperand);
10198 } else {
10199 // Otherwise, this outputs to a register (directly for C_Register /
10200 // C_RegisterClass, and a target-defined fashion for
10201 // C_Immediate/C_Other). Find a register that we can use.
10202 if (OpInfo.AssignedRegs.Regs.empty()) {
10203 emitInlineAsmError(
10204 Call, "couldn't allocate output register for constraint '" +
10205 Twine(OpInfo.ConstraintCode) + "'");
10206 return;
10207 }
10208
10209 if (DetectWriteToReservedRegister())
10210 return;
10211
10212 // Add information to the INLINEASM node to know that this register is
10213 // set.
10214 OpInfo.AssignedRegs.AddInlineAsmOperands(
10215 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10217 false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
10218 }
10219 break;
10220
10221 case InlineAsm::isInput:
10222 case InlineAsm::isLabel: {
10223 SDValue InOperandVal = OpInfo.CallOperand;
10224
10225 if (OpInfo.isMatchingInputConstraint()) {
10226 // If this is required to match an output register we have already set,
10227 // just use its register.
10228 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10229 AsmNodeOperands);
10230 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
10231 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10232 if (OpInfo.isIndirect) {
10233 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10234 emitInlineAsmError(Call, "inline asm not supported yet: "
10235 "don't know how to handle tied "
10236 "indirect register inputs");
10237 return;
10238 }
10239
10244 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
10245 Register TiedReg = R->getReg();
10246 MVT RegVT = R->getSimpleValueType(0);
10247 const TargetRegisterClass *RC =
10248 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10249 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10250 : TRI.getMinimalPhysRegClass(TiedReg);
10251 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
10252 Regs.push_back(MRI.createVirtualRegister(RC));
10253
10254 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10255
10256 SDLoc dl = getCurSDLoc();
10257 // Use the produced MatchedRegs object to
10258 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
10259 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
10260 OpInfo.getMatchedOperand(), dl, DAG,
10261 AsmNodeOperands);
10262 break;
10263 }
10264
10265 assert(Flag.isMemKind() && "Unknown matching constraint!");
10266 assert(Flag.getNumOperandRegisters() == 1 &&
10267 "Unexpected number of operands");
10268 // Add information to the INLINEASM node to know about this input.
10269 // See InlineAsm.h isUseOperandTiedToDef.
10270 Flag.clearMemConstraint();
10271 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10272 AsmNodeOperands.push_back(DAG.getTargetConstant(
10273 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10274 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
10275 break;
10276 }
10277
10278 // Treat indirect 'X' constraint as memory.
10279 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10280 OpInfo.isIndirect)
10281 OpInfo.ConstraintType = TargetLowering::C_Memory;
10282
10283 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10284 OpInfo.ConstraintType == TargetLowering::C_Other) {
10285 std::vector<SDValue> Ops;
10286 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10287 Ops, DAG);
10288 if (Ops.empty()) {
10289 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10290 if (isa<ConstantSDNode>(InOperandVal)) {
10291 emitInlineAsmError(Call, "value out of range for constraint '" +
10292 Twine(OpInfo.ConstraintCode) + "'");
10293 return;
10294 }
10295
10296 emitInlineAsmError(Call,
10297 "invalid operand for inline asm constraint '" +
10298 Twine(OpInfo.ConstraintCode) + "'");
10299 return;
10300 }
10301
10302 // Add information to the INLINEASM node to know about this input.
10303 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10304 AsmNodeOperands.push_back(DAG.getTargetConstant(
10305 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10306 llvm::append_range(AsmNodeOperands, Ops);
10307 break;
10308 }
10309
10310 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10311 assert((OpInfo.isIndirect ||
10312 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10313 "Operand must be indirect to be a mem!");
10314 assert(InOperandVal.getValueType() ==
10316 "Memory operands expect pointer values");
10317
10318 const InlineAsm::ConstraintCode ConstraintID =
10319 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10321 "Failed to convert memory constraint code to constraint id.");
10322
10323 // Add information to the INLINEASM node to know about this input.
10325 ResOpType.setMemConstraint(ConstraintID);
10326 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10327 getCurSDLoc(),
10328 MVT::i32));
10329 AsmNodeOperands.push_back(InOperandVal);
10330 break;
10331 }
10332
10333 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10334 const InlineAsm::ConstraintCode ConstraintID =
10335 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10337 "Failed to convert memory constraint code to constraint id.");
10338
10340
10341 SDValue AsmOp = InOperandVal;
10342 if (isFunction(InOperandVal)) {
10343 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10344 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10345 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10346 InOperandVal.getValueType(),
10347 GA->getOffset());
10348 }
10349
10350 // Add information to the INLINEASM node to know about this input.
10351 ResOpType.setMemConstraint(ConstraintID);
10352
10353 AsmNodeOperands.push_back(
10354 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10355
10356 AsmNodeOperands.push_back(AsmOp);
10357 break;
10358 }
10359
10360 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10361 OpInfo.ConstraintType != TargetLowering::C_Register) {
10362 emitInlineAsmError(Call, "unknown asm constraint '" +
10363 Twine(OpInfo.ConstraintCode) + "'");
10364 return;
10365 }
10366
10367 // TODO: Support this.
10368 if (OpInfo.isIndirect) {
10369 emitInlineAsmError(
10370 Call, "Don't know how to handle indirect register inputs yet "
10371 "for constraint '" +
10372 Twine(OpInfo.ConstraintCode) + "'");
10373 return;
10374 }
10375
10376 // Copy the input into the appropriate registers.
10377 if (OpInfo.AssignedRegs.Regs.empty()) {
10378 emitInlineAsmError(Call,
10379 "couldn't allocate input reg for constraint '" +
10380 Twine(OpInfo.ConstraintCode) + "'");
10381 return;
10382 }
10383
10384 if (DetectWriteToReservedRegister())
10385 return;
10386
10387 SDLoc dl = getCurSDLoc();
10388
10389 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10390 &Call);
10391
10392 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10393 0, dl, DAG, AsmNodeOperands);
10394 break;
10395 }
10397 // Add the clobbered value to the operand list, so that the register
10398 // allocator is aware that the physreg got clobbered.
10399 if (!OpInfo.AssignedRegs.Regs.empty())
10400 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::Clobber,
10401 false, 0, getCurSDLoc(), DAG,
10402 AsmNodeOperands);
10403 break;
10404 }
10405 }
10406
10407 // Finish up input operands. Set the input chain and add the flag last.
10408 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10409 if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10410
10411 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10412 Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10413 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10414 Glue = Chain.getValue(1);
10415
10416 // Do additional work to generate outputs.
10417
10418 SmallVector<EVT, 1> ResultVTs;
10419 SmallVector<SDValue, 1> ResultValues;
10420 SmallVector<SDValue, 8> OutChains;
10421
10422 llvm::Type *CallResultType = Call.getType();
10423 ArrayRef<Type *> ResultTypes;
10424 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10425 ResultTypes = StructResult->elements();
10426 else if (!CallResultType->isVoidTy())
10427 ResultTypes = ArrayRef(CallResultType);
10428
10429 auto CurResultType = ResultTypes.begin();
10430 auto handleRegAssign = [&](SDValue V) {
10431 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10432 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10433 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10434 ++CurResultType;
10435 // If the type of the inline asm call site return value is different but has
10436 // same size as the type of the asm output bitcast it. One example of this
10437 // is for vectors with different width / number of elements. This can
10438 // happen for register classes that can contain multiple different value
10439 // types. The preg or vreg allocated may not have the same VT as was
10440 // expected.
10441 //
10442 // This can also happen for a return value that disagrees with the register
10443 // class it is put in, eg. a double in a general-purpose register on a
10444 // 32-bit machine.
10445 if (ResultVT != V.getValueType() &&
10446 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10447 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10448 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10449 V.getValueType().isInteger()) {
10450 // If a result value was tied to an input value, the computed result
10451 // may have a wider width than the expected result. Extract the
10452 // relevant portion.
10453 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10454 }
10455 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10456 ResultVTs.push_back(ResultVT);
10457 ResultValues.push_back(V);
10458 };
10459
10460 // Deal with output operands.
10461 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10462 if (OpInfo.Type == InlineAsm::isOutput) {
10463 SDValue Val;
10464 // Skip trivial output operands.
10465 if (OpInfo.AssignedRegs.Regs.empty())
10466 continue;
10467
10468 switch (OpInfo.ConstraintType) {
10471 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10472 Chain, &Glue, &Call);
10473 break;
10476 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10477 OpInfo, DAG);
10478 break;
10480 break; // Already handled.
10482 break; // Silence warning.
10484 assert(false && "Unexpected unknown constraint");
10485 }
10486
10487 // Indirect output manifest as stores. Record output chains.
10488 if (OpInfo.isIndirect) {
10489 const Value *Ptr = OpInfo.CallOperandVal;
10490 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10491 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10493 OutChains.push_back(Store);
10494 } else {
10495 // generate CopyFromRegs to associated registers.
10496 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10497 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10498 for (const SDValue &V : Val->op_values())
10499 handleRegAssign(V);
10500 } else
10501 handleRegAssign(Val);
10502 }
10503 }
10504 }
10505
10506 // Set results.
10507 if (!ResultValues.empty()) {
10508 assert(CurResultType == ResultTypes.end() &&
10509 "Mismatch in number of ResultTypes");
10510 assert(ResultValues.size() == ResultTypes.size() &&
10511 "Mismatch in number of output operands in asm result");
10512
10514 DAG.getVTList(ResultVTs), ResultValues);
10515 setValue(&Call, V);
10516 }
10517
10518 // Collect store chains.
10519 if (!OutChains.empty())
10520 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10521
10522 if (EmitEHLabels) {
10523 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10524 }
10525
10526 // Only Update Root if inline assembly has a memory effect.
10527 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10528 EmitEHLabels)
10529 DAG.setRoot(Chain);
10530}
10531
10532void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10533 const Twine &Message) {
10534 LLVMContext &Ctx = *DAG.getContext();
10535 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10536
10537 // Make sure we leave the DAG in a valid state
10539 SmallVector<EVT, 1> ValueVTs;
10540 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10541
10542 if (ValueVTs.empty())
10543 return;
10544
10546 for (const EVT &VT : ValueVTs)
10547 Ops.push_back(DAG.getUNDEF(VT));
10548
10549 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10550}
10551
10552void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10554 MVT::Other, getRoot(),
10555 getValue(I.getArgOperand(0)),
10556 DAG.getSrcValue(I.getArgOperand(0))));
10557}
10558
10559void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10561 const DataLayout &DL = DAG.getDataLayout();
10563 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10564 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10565 DL.getABITypeAlign(I.getType()).value());
10566 DAG.setRoot(V.getValue(1));
10567
10568 if (I.getType()->isPointerTy())
10570 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10571 setValue(&I, V);
10572}
10573
10574void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10576 MVT::Other, getRoot(),
10577 getValue(I.getArgOperand(0)),
10578 DAG.getSrcValue(I.getArgOperand(0))));
10579}
10580
10581void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10583 MVT::Other, getRoot(),
10584 getValue(I.getArgOperand(0)),
10585 getValue(I.getArgOperand(1)),
10586 DAG.getSrcValue(I.getArgOperand(0)),
10587 DAG.getSrcValue(I.getArgOperand(1))));
10588}
10589
10591 const Instruction &I,
10592 SDValue Op) {
10593 std::optional<ConstantRange> CR = getRange(I);
10594
10595 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10596 return Op;
10597
10598 APInt Lo = CR->getUnsignedMin();
10599 if (!Lo.isMinValue())
10600 return Op;
10601
10602 APInt Hi = CR->getUnsignedMax();
10603 unsigned Bits = std::max(Hi.getActiveBits(),
10604 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10605
10606 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10607
10608 SDLoc SL = getCurSDLoc();
10609
10610 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10611 DAG.getValueType(SmallVT));
10612 unsigned NumVals = Op.getNode()->getNumValues();
10613 if (NumVals == 1)
10614 return ZExt;
10615
10617
10618 Ops.push_back(ZExt);
10619 for (unsigned I = 1; I != NumVals; ++I)
10620 Ops.push_back(Op.getValue(I));
10621
10622 return DAG.getMergeValues(Ops, SL);
10623}
10624
10625/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10626/// the call being lowered.
10627///
10628/// This is a helper for lowering intrinsics that follow a target calling
10629/// convention or require stack pointer adjustment. Only a subset of the
10630/// intrinsic's operands need to participate in the calling convention.
10633 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10634 AttributeSet RetAttrs, bool IsPatchPoint) {
10636 Args.reserve(NumArgs);
10637
10638 // Populate the argument list.
10639 // Attributes for args start at offset 1, after the return attribute.
10640 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10641 ArgI != ArgE; ++ArgI) {
10642 const Value *V = Call->getOperand(ArgI);
10643
10644 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10645
10646 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10647 Entry.setAttributes(Call, ArgI);
10648 Args.push_back(Entry);
10649 }
10650
10652 .setChain(getRoot())
10653 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10654 RetAttrs)
10655 .setDiscardResult(Call->use_empty())
10656 .setIsPatchPoint(IsPatchPoint)
10658 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10659}
10660
10661/// Add a stack map intrinsic call's live variable operands to a stackmap
10662/// or patchpoint target node's operand list.
10663///
10664/// Constants are converted to TargetConstants purely as an optimization to
10665/// avoid constant materialization and register allocation.
10666///
10667/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10668/// generate addess computation nodes, and so FinalizeISel can convert the
10669/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10670/// address materialization and register allocation, but may also be required
10671/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10672/// alloca in the entry block, then the runtime may assume that the alloca's
10673/// StackMap location can be read immediately after compilation and that the
10674/// location is valid at any point during execution (this is similar to the
10675/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10676/// only available in a register, then the runtime would need to trap when
10677/// execution reaches the StackMap in order to read the alloca's location.
10678static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10679 const SDLoc &DL, SmallVectorImpl<SDValue> &Ops,
10680 SelectionDAGBuilder &Builder) {
10681 SelectionDAG &DAG = Builder.DAG;
10682 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10683 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10684
10685 // Things on the stack are pointer-typed, meaning that they are already
10686 // legal and can be emitted directly to target nodes.
10687 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
10688 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10689 } else {
10690 // Otherwise emit a target independent node to be legalised.
10691 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10692 }
10693 }
10694}
10695
10696/// Lower llvm.experimental.stackmap.
10697void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10698 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10699 // [live variables...])
10700
10701 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10702
10703 SDValue Chain, InGlue, Callee;
10705
10706 SDLoc DL = getCurSDLoc();
10708
10709 // The stackmap intrinsic only records the live variables (the arguments
10710 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10711 // intrinsic, this won't be lowered to a function call. This means we don't
10712 // have to worry about calling conventions and target specific lowering code.
10713 // Instead we perform the call lowering right here.
10714 //
10715 // chain, flag = CALLSEQ_START(chain, 0, 0)
10716 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10717 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10718 //
10719 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10720 InGlue = Chain.getValue(1);
10721
10722 // Add the STACKMAP operands, starting with DAG house-keeping.
10723 Ops.push_back(Chain);
10724 Ops.push_back(InGlue);
10725
10726 // Add the <id>, <numShadowBytes> operands.
10727 //
10728 // These do not require legalisation, and can be emitted directly to target
10729 // constant nodes.
10731 assert(ID.getValueType() == MVT::i64);
10732 SDValue IDConst =
10733 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10734 Ops.push_back(IDConst);
10735
10736 SDValue Shad = getValue(CI.getArgOperand(1));
10737 assert(Shad.getValueType() == MVT::i32);
10738 SDValue ShadConst =
10740 Ops.push_back(ShadConst);
10741
10742 // Add the live variables.
10743 addStackMapLiveVars(CI, 2, DL, Ops, *this);
10744
10745 // Create the STACKMAP node.
10746 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10747 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10748 InGlue = Chain.getValue(1);
10749
10750 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10751
10752 // Stackmaps don't generate values, so nothing goes into the NodeMap.
10753
10754 // Set the root to the target-lowered call chain.
10755 DAG.setRoot(Chain);
10756
10757 // Inform the Frame Information that we have a stackmap in this function.
10759}
10760
10761/// Lower llvm.experimental.patchpoint directly to its target opcode.
10762void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10763 const BasicBlock *EHPadBB) {
10764 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10765 // i32 <numBytes>,
10766 // i8* <target>,
10767 // i32 <numArgs>,
10768 // [Args...],
10769 // [live variables...])
10770
10772 bool IsAnyRegCC = CC == CallingConv::AnyReg;
10773 bool HasDef = !CB.getType()->isVoidTy();
10774 SDLoc dl = getCurSDLoc();
10776
10777 // Handle immediate and symbolic callees.
10778 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10779 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10780 /*isTarget=*/true);
10781 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
10782 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
10783 SDLoc(SymbolicCallee),
10784 SymbolicCallee->getValueType(0));
10785
10786 // Get the real number of arguments participating in the call <numArgs>
10788 unsigned NumArgs = NArgVal->getAsZExtVal();
10789
10790 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
10791 // Intrinsics include all meta-operands up to but not including CC.
10792 unsigned NumMetaOpers = PatchPointOpers::CCPos;
10793 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
10794 "Not enough arguments provided to the patchpoint intrinsic");
10795
10796 // For AnyRegCC the arguments are lowered later on manually.
10797 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
10798 Type *ReturnTy =
10799 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
10800
10802 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
10803 ReturnTy, CB.getAttributes().getRetAttrs(), true);
10804 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
10805
10806 SDNode *CallEnd = Result.second.getNode();
10807 if (CallEnd->getOpcode() == ISD::EH_LABEL)
10808 CallEnd = CallEnd->getOperand(0).getNode();
10809 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
10810 CallEnd = CallEnd->getOperand(0).getNode();
10811
10812 /// Get a call instruction from the call sequence chain.
10813 /// Tail calls are not allowed.
10814 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
10815 "Expected a callseq node.");
10816 SDNode *Call = CallEnd->getOperand(0).getNode();
10817 bool HasGlue = Call->getGluedNode();
10818
10819 // Replace the target specific call node with the patchable intrinsic.
10821
10822 // Push the chain.
10823 Ops.push_back(*(Call->op_begin()));
10824
10825 // Optionally, push the glue (if any).
10826 if (HasGlue)
10827 Ops.push_back(*(Call->op_end() - 1));
10828
10829 // Push the register mask info.
10830 if (HasGlue)
10831 Ops.push_back(*(Call->op_end() - 2));
10832 else
10833 Ops.push_back(*(Call->op_end() - 1));
10834
10835 // Add the <id> and <numBytes> constants.
10837 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
10839 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
10840
10841 // Add the callee.
10842 Ops.push_back(Callee);
10843
10844 // Adjust <numArgs> to account for any arguments that have been passed on the
10845 // stack instead.
10846 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
10847 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
10848 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
10849 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
10850
10851 // Add the calling convention
10852 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
10853
10854 // Add the arguments we omitted previously. The register allocator should
10855 // place these in any free register.
10856 if (IsAnyRegCC)
10857 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
10858 Ops.push_back(getValue(CB.getArgOperand(i)));
10859
10860 // Push the arguments from the call instruction.
10861 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
10862 Ops.append(Call->op_begin() + 2, e);
10863
10864 // Push live variables for the stack map.
10865 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
10866
10867 SDVTList NodeTys;
10868 if (IsAnyRegCC && HasDef) {
10869 // Create the return types based on the intrinsic definition
10871 SmallVector<EVT, 3> ValueVTs;
10872 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
10873 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
10874
10875 // There is always a chain and a glue type at the end
10876 ValueVTs.push_back(MVT::Other);
10877 ValueVTs.push_back(MVT::Glue);
10878 NodeTys = DAG.getVTList(ValueVTs);
10879 } else
10880 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10881
10882 // Replace the target specific call node with a PATCHPOINT node.
10883 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
10884
10885 // Update the NodeMap.
10886 if (HasDef) {
10887 if (IsAnyRegCC)
10888 setValue(&CB, SDValue(PPV.getNode(), 0));
10889 else
10890 setValue(&CB, Result.first);
10891 }
10892
10893 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
10894 // call sequence. Furthermore the location of the chain and glue can change
10895 // when the AnyReg calling convention is used and the intrinsic returns a
10896 // value.
10897 if (IsAnyRegCC && HasDef) {
10898 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
10899 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
10901 } else
10902 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
10903 DAG.DeleteNode(Call);
10904
10905 // Inform the Frame Information that we have a patchpoint in this function.
10907}
10908
10909void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
10910 unsigned Intrinsic) {
10912 SDValue Op1 = getValue(I.getArgOperand(0));
10913 SDValue Op2;
10914 if (I.arg_size() > 1)
10915 Op2 = getValue(I.getArgOperand(1));
10916 SDLoc dl = getCurSDLoc();
10917 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
10918 SDValue Res;
10919 SDNodeFlags SDFlags;
10920 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
10921 SDFlags.copyFMF(*FPMO);
10922
10923 switch (Intrinsic) {
10924 case Intrinsic::vector_reduce_fadd:
10925 if (SDFlags.hasAllowReassociation())
10926 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
10927 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
10928 SDFlags);
10929 else
10930 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
10931 break;
10932 case Intrinsic::vector_reduce_fmul:
10933 if (SDFlags.hasAllowReassociation())
10934 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
10935 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
10936 SDFlags);
10937 else
10938 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
10939 break;
10940 case Intrinsic::vector_reduce_add:
10941 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
10942 break;
10943 case Intrinsic::vector_reduce_mul:
10944 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
10945 break;
10946 case Intrinsic::vector_reduce_and:
10947 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
10948 break;
10949 case Intrinsic::vector_reduce_or:
10950 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
10951 break;
10952 case Intrinsic::vector_reduce_xor:
10953 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
10954 break;
10955 case Intrinsic::vector_reduce_smax:
10956 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
10957 break;
10958 case Intrinsic::vector_reduce_smin:
10959 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
10960 break;
10961 case Intrinsic::vector_reduce_umax:
10962 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
10963 break;
10964 case Intrinsic::vector_reduce_umin:
10965 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
10966 break;
10967 case Intrinsic::vector_reduce_fmax:
10968 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
10969 break;
10970 case Intrinsic::vector_reduce_fmin:
10971 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
10972 break;
10973 case Intrinsic::vector_reduce_fmaximum:
10974 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
10975 break;
10976 case Intrinsic::vector_reduce_fminimum:
10977 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
10978 break;
10979 default:
10980 llvm_unreachable("Unhandled vector reduce intrinsic");
10981 }
10982 setValue(&I, Res);
10983}
10984
10985/// Returns an AttributeList representing the attributes applied to the return
10986/// value of the given call.
10989 if (CLI.RetSExt)
10990 Attrs.push_back(Attribute::SExt);
10991 if (CLI.RetZExt)
10992 Attrs.push_back(Attribute::ZExt);
10993 if (CLI.IsInReg)
10994 Attrs.push_back(Attribute::InReg);
10995
10997 Attrs);
10998}
10999
11000/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11001/// implementation, which just calls LowerCall.
11002/// FIXME: When all targets are
11003/// migrated to using LowerCall, this hook should be integrated into SDISel.
11004std::pair<SDValue, SDValue>
11007
11008 // Handle the incoming return values from the call.
11009 CLI.Ins.clear();
11010 SmallVector<Type *, 4> RetOrigTys;
11012 auto &DL = CLI.DAG.getDataLayout();
11013 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11014
11015 SmallVector<EVT, 4> RetVTs;
11016 if (CLI.RetTy != CLI.OrigRetTy) {
11017 assert(RetOrigTys.size() == 1 &&
11018 "Only supported for non-aggregate returns");
11019 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11020 } else {
11021 for (Type *Ty : RetOrigTys)
11022 RetVTs.push_back(getValueType(DL, Ty));
11023 }
11024
11025 if (CLI.IsPostTypeLegalization) {
11026 // If we are lowering a libcall after legalization, split the return type.
11027 SmallVector<Type *, 4> OldRetOrigTys;
11028 SmallVector<EVT, 4> OldRetVTs;
11029 SmallVector<TypeSize, 4> OldOffsets;
11030 RetOrigTys.swap(OldRetOrigTys);
11031 RetVTs.swap(OldRetVTs);
11032 Offsets.swap(OldOffsets);
11033
11034 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11035 EVT RetVT = OldRetVTs[i];
11036 uint64_t Offset = OldOffsets[i];
11037 MVT RegisterVT = getRegisterType(Context, RetVT);
11038 unsigned NumRegs = getNumRegisters(Context, RetVT);
11039 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11040 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11041 RetVTs.append(NumRegs, RegisterVT);
11042 for (unsigned j = 0; j != NumRegs; ++j)
11043 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11044 }
11045 }
11046
11048 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11049
11050 bool CanLowerReturn =
11052 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11053
11054 SDValue DemoteStackSlot;
11055 int DemoteStackIdx = -100;
11056 if (!CanLowerReturn) {
11057 // FIXME: equivalent assert?
11058 // assert(!CS.hasInAllocaArgument() &&
11059 // "sret demotion is incompatible with inalloca");
11060 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11061 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11063 DemoteStackIdx =
11064 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11065 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11066
11067 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11068 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11069 Entry.IsSRet = true;
11070 Entry.Alignment = Alignment;
11071 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11072 CLI.NumFixedArgs += 1;
11073 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11075
11076 // sret demotion isn't compatible with tail-calls, since the sret argument
11077 // points into the callers stack frame.
11078 CLI.IsTailCall = false;
11079 } else {
11080 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11081 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11082 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11083 ISD::ArgFlagsTy Flags;
11084 if (NeedsRegBlock) {
11085 Flags.setInConsecutiveRegs();
11086 if (I == RetVTs.size() - 1)
11087 Flags.setInConsecutiveRegsLast();
11088 }
11089 EVT VT = RetVTs[I];
11090 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11091 unsigned NumRegs =
11093 for (unsigned i = 0; i != NumRegs; ++i) {
11094 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11096 if (CLI.RetTy->isPointerTy()) {
11097 Ret.Flags.setPointer();
11098 Ret.Flags.setPointerAddrSpace(
11099 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11100 }
11101 if (CLI.RetSExt)
11102 Ret.Flags.setSExt();
11103 if (CLI.RetZExt)
11104 Ret.Flags.setZExt();
11105 if (CLI.IsInReg)
11106 Ret.Flags.setInReg();
11107 CLI.Ins.push_back(Ret);
11108 }
11109 }
11110 }
11111
11112 // We push in swifterror return as the last element of CLI.Ins.
11113 ArgListTy &Args = CLI.getArgs();
11114 if (supportSwiftError()) {
11115 for (const ArgListEntry &Arg : Args) {
11116 if (Arg.IsSwiftError) {
11117 ISD::ArgFlagsTy Flags;
11118 Flags.setSwiftError();
11121 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11122 CLI.Ins.push_back(Ret);
11123 }
11124 }
11125 }
11126
11127 // Handle all of the outgoing arguments.
11128 CLI.Outs.clear();
11129 CLI.OutVals.clear();
11130 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11131 SmallVector<Type *, 4> OrigArgTys;
11132 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11133 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11134 Type *FinalType = Args[i].Ty;
11135 if (Args[i].IsByVal)
11136 FinalType = Args[i].IndirectType;
11137 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11138 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11139 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11140 ++Value) {
11141 Type *OrigArgTy = OrigArgTys[Value];
11142 Type *ArgTy = OrigArgTy;
11143 if (Args[i].Ty != Args[i].OrigTy) {
11144 assert(Value == 0 && "Only supported for non-aggregate arguments");
11145 ArgTy = Args[i].Ty;
11146 }
11147
11148 EVT VT = getValueType(DL, ArgTy);
11149 SDValue Op = SDValue(Args[i].Node.getNode(),
11150 Args[i].Node.getResNo() + Value);
11151 ISD::ArgFlagsTy Flags;
11152
11153 // Certain targets (such as MIPS), may have a different ABI alignment
11154 // for a type depending on the context. Give the target a chance to
11155 // specify the alignment it wants.
11156 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11157 Flags.setOrigAlign(OriginalAlignment);
11158
11159 if (i >= CLI.NumFixedArgs)
11160 Flags.setVarArg();
11161 if (ArgTy->isPointerTy()) {
11162 Flags.setPointer();
11163 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11164 }
11165 if (Args[i].IsZExt)
11166 Flags.setZExt();
11167 if (Args[i].IsSExt)
11168 Flags.setSExt();
11169 if (Args[i].IsNoExt)
11170 Flags.setNoExt();
11171 if (Args[i].IsInReg) {
11172 // If we are using vectorcall calling convention, a structure that is
11173 // passed InReg - is surely an HVA
11175 isa<StructType>(FinalType)) {
11176 // The first value of a structure is marked
11177 if (0 == Value)
11178 Flags.setHvaStart();
11179 Flags.setHva();
11180 }
11181 // Set InReg Flag
11182 Flags.setInReg();
11183 }
11184 if (Args[i].IsSRet)
11185 Flags.setSRet();
11186 if (Args[i].IsSwiftSelf)
11187 Flags.setSwiftSelf();
11188 if (Args[i].IsSwiftAsync)
11189 Flags.setSwiftAsync();
11190 if (Args[i].IsSwiftError)
11191 Flags.setSwiftError();
11192 if (Args[i].IsCFGuardTarget)
11193 Flags.setCFGuardTarget();
11194 if (Args[i].IsByVal)
11195 Flags.setByVal();
11196 if (Args[i].IsByRef)
11197 Flags.setByRef();
11198 if (Args[i].IsPreallocated) {
11199 Flags.setPreallocated();
11200 // Set the byval flag for CCAssignFn callbacks that don't know about
11201 // preallocated. This way we can know how many bytes we should've
11202 // allocated and how many bytes a callee cleanup function will pop. If
11203 // we port preallocated to more targets, we'll have to add custom
11204 // preallocated handling in the various CC lowering callbacks.
11205 Flags.setByVal();
11206 }
11207 if (Args[i].IsInAlloca) {
11208 Flags.setInAlloca();
11209 // Set the byval flag for CCAssignFn callbacks that don't know about
11210 // inalloca. This way we can know how many bytes we should've allocated
11211 // and how many bytes a callee cleanup function will pop. If we port
11212 // inalloca to more targets, we'll have to add custom inalloca handling
11213 // in the various CC lowering callbacks.
11214 Flags.setByVal();
11215 }
11216 Align MemAlign;
11217 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11218 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11219 Flags.setByValSize(FrameSize);
11220
11221 // info is not there but there are cases it cannot get right.
11222 if (auto MA = Args[i].Alignment)
11223 MemAlign = *MA;
11224 else
11225 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11226 } else if (auto MA = Args[i].Alignment) {
11227 MemAlign = *MA;
11228 } else {
11229 MemAlign = OriginalAlignment;
11230 }
11231 Flags.setMemAlign(MemAlign);
11232 if (Args[i].IsNest)
11233 Flags.setNest();
11234 if (NeedsRegBlock)
11235 Flags.setInConsecutiveRegs();
11236
11238 unsigned NumParts =
11240 SmallVector<SDValue, 4> Parts(NumParts);
11241 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11242
11243 if (Args[i].IsSExt)
11244 ExtendKind = ISD::SIGN_EXTEND;
11245 else if (Args[i].IsZExt)
11246 ExtendKind = ISD::ZERO_EXTEND;
11247
11248 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11249 // for now.
11250 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11252 assert((CLI.RetTy == Args[i].Ty ||
11253 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11255 Args[i].Ty->getPointerAddressSpace())) &&
11256 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11257 // Before passing 'returned' to the target lowering code, ensure that
11258 // either the register MVT and the actual EVT are the same size or that
11259 // the return value and argument are extended in the same way; in these
11260 // cases it's safe to pass the argument register value unchanged as the
11261 // return register value (although it's at the target's option whether
11262 // to do so)
11263 // TODO: allow code generation to take advantage of partially preserved
11264 // registers rather than clobbering the entire register when the
11265 // parameter extension method is not compatible with the return
11266 // extension method
11267 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11268 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11269 CLI.RetZExt == Args[i].IsZExt))
11270 Flags.setReturned();
11271 }
11272
11273 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11274 CLI.CallConv, ExtendKind);
11275
11276 for (unsigned j = 0; j != NumParts; ++j) {
11277 // if it isn't first piece, alignment must be 1
11278 // For scalable vectors the scalable part is currently handled
11279 // by individual targets, so we just use the known minimum size here.
11280 ISD::OutputArg MyFlags(
11281 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11282 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11283 if (NumParts > 1 && j == 0)
11284 MyFlags.Flags.setSplit();
11285 else if (j != 0) {
11286 MyFlags.Flags.setOrigAlign(Align(1));
11287 if (j == NumParts - 1)
11288 MyFlags.Flags.setSplitEnd();
11289 }
11290
11291 CLI.Outs.push_back(MyFlags);
11292 CLI.OutVals.push_back(Parts[j]);
11293 }
11294
11295 if (NeedsRegBlock && Value == NumValues - 1)
11296 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11297 }
11298 }
11299
11301 CLI.Chain = LowerCall(CLI, InVals);
11302
11303 // Update CLI.InVals to use outside of this function.
11304 CLI.InVals = InVals;
11305
11306 // Verify that the target's LowerCall behaved as expected.
11307 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11308 "LowerCall didn't return a valid chain!");
11309 assert((!CLI.IsTailCall || InVals.empty()) &&
11310 "LowerCall emitted a return value for a tail call!");
11311 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11312 "LowerCall didn't emit the correct number of values!");
11313
11314 // For a tail call, the return value is merely live-out and there aren't
11315 // any nodes in the DAG representing it. Return a special value to
11316 // indicate that a tail call has been emitted and no more Instructions
11317 // should be processed in the current block.
11318 if (CLI.IsTailCall) {
11319 CLI.DAG.setRoot(CLI.Chain);
11320 return std::make_pair(SDValue(), SDValue());
11321 }
11322
11323#ifndef NDEBUG
11324 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11325 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11326 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11327 "LowerCall emitted a value with the wrong type!");
11328 }
11329#endif
11330
11331 SmallVector<SDValue, 4> ReturnValues;
11332 if (!CanLowerReturn) {
11333 // The instruction result is the result of loading from the
11334 // hidden sret parameter.
11335 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11336
11337 unsigned NumValues = RetVTs.size();
11338 ReturnValues.resize(NumValues);
11339 SmallVector<SDValue, 4> Chains(NumValues);
11340
11341 // An aggregate return value cannot wrap around the address space, so
11342 // offsets to its parts don't wrap either.
11344 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11345 for (unsigned i = 0; i < NumValues; ++i) {
11347 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11349 SDValue L = CLI.DAG.getLoad(
11350 RetVTs[i], CLI.DL, CLI.Chain, Add,
11352 DemoteStackIdx, Offsets[i]),
11353 HiddenSRetAlign);
11354 ReturnValues[i] = L;
11355 Chains[i] = L.getValue(1);
11356 }
11357
11358 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11359 } else {
11360 // Collect the legal value parts into potentially illegal values
11361 // that correspond to the original function's return values.
11362 std::optional<ISD::NodeType> AssertOp;
11363 if (CLI.RetSExt)
11364 AssertOp = ISD::AssertSext;
11365 else if (CLI.RetZExt)
11366 AssertOp = ISD::AssertZext;
11367 unsigned CurReg = 0;
11368 for (EVT VT : RetVTs) {
11369 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11370 unsigned NumRegs =
11372
11373 ReturnValues.push_back(getCopyFromParts(
11374 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11375 CLI.Chain, CLI.CallConv, AssertOp));
11376 CurReg += NumRegs;
11377 }
11378
11379 // For a function returning void, there is no return value. We can't create
11380 // such a node, so we just return a null return value in that case. In
11381 // that case, nothing will actually look at the value.
11382 if (ReturnValues.empty())
11383 return std::make_pair(SDValue(), CLI.Chain);
11384 }
11385
11386 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11387 CLI.DAG.getVTList(RetVTs), ReturnValues);
11388 return std::make_pair(Res, CLI.Chain);
11389}
11390
11391/// Places new result values for the node in Results (their number
11392/// and types must exactly match those of the original return values of
11393/// the node), or leaves Results empty, which indicates that the node is not
11394/// to be custom lowered after all.
11397 SelectionDAG &DAG) const {
11398 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11399
11400 if (!Res.getNode())
11401 return;
11402
11403 // If the original node has one result, take the return value from
11404 // LowerOperation as is. It might not be result number 0.
11405 if (N->getNumValues() == 1) {
11406 Results.push_back(Res);
11407 return;
11408 }
11409
11410 // If the original node has multiple results, then the return node should
11411 // have the same number of results.
11412 assert((N->getNumValues() == Res->getNumValues()) &&
11413 "Lowering returned the wrong number of results!");
11414
11415 // Places new result values base on N result number.
11416 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11417 Results.push_back(Res.getValue(I));
11418}
11419
11421 llvm_unreachable("LowerOperation not implemented for this target!");
11422}
11423
11425 Register Reg,
11426 ISD::NodeType ExtendType) {
11428 assert((Op.getOpcode() != ISD::CopyFromReg ||
11429 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11430 "Copy from a reg to the same reg!");
11431 assert(!Reg.isPhysical() && "Is a physreg");
11432
11434 // If this is an InlineAsm we have to match the registers required, not the
11435 // notional registers required by the type.
11436
11437 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11438 std::nullopt); // This is not an ABI copy.
11439 SDValue Chain = DAG.getEntryNode();
11440
11441 if (ExtendType == ISD::ANY_EXTEND) {
11442 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11443 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11444 ExtendType = PreferredExtendIt->second;
11445 }
11446 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11447 PendingExports.push_back(Chain);
11448}
11449
11451
11452/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11453/// entry block, return true. This includes arguments used by switches, since
11454/// the switch may expand into multiple basic blocks.
11455static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11456 // With FastISel active, we may be splitting blocks, so force creation
11457 // of virtual registers for all non-dead arguments.
11458 if (FastISel)
11459 return A->use_empty();
11460
11461 const BasicBlock &Entry = A->getParent()->front();
11462 for (const User *U : A->users())
11463 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11464 return false; // Use not in entry block.
11465
11466 return true;
11467}
11468
11470 DenseMap<const Argument *,
11471 std::pair<const AllocaInst *, const StoreInst *>>;
11472
11473/// Scan the entry block of the function in FuncInfo for arguments that look
11474/// like copies into a local alloca. Record any copied arguments in
11475/// ArgCopyElisionCandidates.
11476static void
11478 FunctionLoweringInfo *FuncInfo,
11479 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11480 // Record the state of every static alloca used in the entry block. Argument
11481 // allocas are all used in the entry block, so we need approximately as many
11482 // entries as we have arguments.
11483 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11485 unsigned NumArgs = FuncInfo->Fn->arg_size();
11486 StaticAllocas.reserve(NumArgs * 2);
11487
11488 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11489 if (!V)
11490 return nullptr;
11491 V = V->stripPointerCasts();
11492 const auto *AI = dyn_cast<AllocaInst>(V);
11493 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11494 return nullptr;
11495 auto Iter = StaticAllocas.insert({AI, Unknown});
11496 return &Iter.first->second;
11497 };
11498
11499 // Look for stores of arguments to static allocas. Look through bitcasts and
11500 // GEPs to handle type coercions, as long as the alloca is fully initialized
11501 // by the store. Any non-store use of an alloca escapes it and any subsequent
11502 // unanalyzed store might write it.
11503 // FIXME: Handle structs initialized with multiple stores.
11504 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11505 // Look for stores, and handle non-store uses conservatively.
11506 const auto *SI = dyn_cast<StoreInst>(&I);
11507 if (!SI) {
11508 // We will look through cast uses, so ignore them completely.
11509 if (I.isCast())
11510 continue;
11511 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11512 // to allocas.
11513 if (I.isDebugOrPseudoInst())
11514 continue;
11515 // This is an unknown instruction. Assume it escapes or writes to all
11516 // static alloca operands.
11517 for (const Use &U : I.operands()) {
11518 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11519 *Info = StaticAllocaInfo::Clobbered;
11520 }
11521 continue;
11522 }
11523
11524 // If the stored value is a static alloca, mark it as escaped.
11525 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11526 *Info = StaticAllocaInfo::Clobbered;
11527
11528 // Check if the destination is a static alloca.
11529 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11530 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11531 if (!Info)
11532 continue;
11533 const AllocaInst *AI = cast<AllocaInst>(Dst);
11534
11535 // Skip allocas that have been initialized or clobbered.
11536 if (*Info != StaticAllocaInfo::Unknown)
11537 continue;
11538
11539 // Check if the stored value is an argument, and that this store fully
11540 // initializes the alloca.
11541 // If the argument type has padding bits we can't directly forward a pointer
11542 // as the upper bits may contain garbage.
11543 // Don't elide copies from the same argument twice.
11544 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11545 const auto *Arg = dyn_cast<Argument>(Val);
11546 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11547 Arg->getType()->isEmptyTy() ||
11548 DL.getTypeStoreSize(Arg->getType()) !=
11549 DL.getTypeAllocSize(AI->getAllocatedType()) ||
11550 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11551 ArgCopyElisionCandidates.count(Arg)) {
11552 *Info = StaticAllocaInfo::Clobbered;
11553 continue;
11554 }
11555
11556 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11557 << '\n');
11558
11559 // Mark this alloca and store for argument copy elision.
11560 *Info = StaticAllocaInfo::Elidable;
11561 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11562
11563 // Stop scanning if we've seen all arguments. This will happen early in -O0
11564 // builds, which is useful, because -O0 builds have large entry blocks and
11565 // many allocas.
11566 if (ArgCopyElisionCandidates.size() == NumArgs)
11567 break;
11568 }
11569}
11570
11571/// Try to elide argument copies from memory into a local alloca. Succeeds if
11572/// ArgVal is a load from a suitable fixed stack object.
11575 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11576 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11577 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11578 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11579 // Check if this is a load from a fixed stack object.
11580 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11581 if (!LNode)
11582 return;
11583 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11584 if (!FINode)
11585 return;
11586
11587 // Check that the fixed stack object is the right size and alignment.
11588 // Look at the alignment that the user wrote on the alloca instead of looking
11589 // at the stack object.
11590 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11591 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11592 const AllocaInst *AI = ArgCopyIter->second.first;
11593 int FixedIndex = FINode->getIndex();
11594 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11595 int OldIndex = AllocaIndex;
11596 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11597 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11598 LLVM_DEBUG(
11599 dbgs() << " argument copy elision failed due to bad fixed stack "
11600 "object size\n");
11601 return;
11602 }
11603 Align RequiredAlignment = AI->getAlign();
11604 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11605 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11606 "greater than stack argument alignment ("
11607 << DebugStr(RequiredAlignment) << " vs "
11608 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11609 return;
11610 }
11611
11612 // Perform the elision. Delete the old stack object and replace its only use
11613 // in the variable info map. Mark the stack object as mutable and aliased.
11614 LLVM_DEBUG({
11615 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11616 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11617 << '\n';
11618 });
11619 MFI.RemoveStackObject(OldIndex);
11620 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11621 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11622 AllocaIndex = FixedIndex;
11623 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11624 for (SDValue ArgVal : ArgVals)
11625 Chains.push_back(ArgVal.getValue(1));
11626
11627 // Avoid emitting code for the store implementing the copy.
11628 const StoreInst *SI = ArgCopyIter->second.second;
11629 ElidedArgCopyInstrs.insert(SI);
11630
11631 // Check for uses of the argument again so that we can avoid exporting ArgVal
11632 // if it is't used by anything other than the store.
11633 for (const Value *U : Arg.users()) {
11634 if (U != SI) {
11635 ArgHasUses = true;
11636 break;
11637 }
11638 }
11639}
11640
11641void SelectionDAGISel::LowerArguments(const Function &F) {
11642 SelectionDAG &DAG = SDB->DAG;
11643 SDLoc dl = SDB->getCurSDLoc();
11644 const DataLayout &DL = DAG.getDataLayout();
11646
11647 // In Naked functions we aren't going to save any registers.
11648 if (F.hasFnAttribute(Attribute::Naked))
11649 return;
11650
11651 if (!FuncInfo->CanLowerReturn) {
11652 // Put in an sret pointer parameter before all the other parameters.
11653 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11654
11656 Flags.setSRet();
11657 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11658 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11660 Ins.push_back(RetArg);
11661 }
11662
11663 // Look for stores of arguments to static allocas. Mark such arguments with a
11664 // flag to ask the target to give us the memory location of that argument if
11665 // available.
11666 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11668 ArgCopyElisionCandidates);
11669
11670 // Set up the incoming argument description vector.
11671 for (const Argument &Arg : F.args()) {
11672 unsigned ArgNo = Arg.getArgNo();
11674 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11675 bool isArgValueUsed = !Arg.use_empty();
11676 unsigned PartBase = 0;
11677 Type *FinalType = Arg.getType();
11678 if (Arg.hasAttribute(Attribute::ByVal))
11679 FinalType = Arg.getParamByValType();
11680 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11681 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11682 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11683 ++Value) {
11684 Type *ArgTy = Types[Value];
11685 EVT VT = TLI->getValueType(DL, ArgTy);
11687
11688 if (ArgTy->isPointerTy()) {
11689 Flags.setPointer();
11690 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11691 }
11692 if (Arg.hasAttribute(Attribute::ZExt))
11693 Flags.setZExt();
11694 if (Arg.hasAttribute(Attribute::SExt))
11695 Flags.setSExt();
11696 if (Arg.hasAttribute(Attribute::InReg)) {
11697 // If we are using vectorcall calling convention, a structure that is
11698 // passed InReg - is surely an HVA
11699 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11700 isa<StructType>(Arg.getType())) {
11701 // The first value of a structure is marked
11702 if (0 == Value)
11703 Flags.setHvaStart();
11704 Flags.setHva();
11705 }
11706 // Set InReg Flag
11707 Flags.setInReg();
11708 }
11709 if (Arg.hasAttribute(Attribute::StructRet))
11710 Flags.setSRet();
11711 if (Arg.hasAttribute(Attribute::SwiftSelf))
11712 Flags.setSwiftSelf();
11713 if (Arg.hasAttribute(Attribute::SwiftAsync))
11714 Flags.setSwiftAsync();
11715 if (Arg.hasAttribute(Attribute::SwiftError))
11716 Flags.setSwiftError();
11717 if (Arg.hasAttribute(Attribute::ByVal))
11718 Flags.setByVal();
11719 if (Arg.hasAttribute(Attribute::ByRef))
11720 Flags.setByRef();
11721 if (Arg.hasAttribute(Attribute::InAlloca)) {
11722 Flags.setInAlloca();
11723 // Set the byval flag for CCAssignFn callbacks that don't know about
11724 // inalloca. This way we can know how many bytes we should've allocated
11725 // and how many bytes a callee cleanup function will pop. If we port
11726 // inalloca to more targets, we'll have to add custom inalloca handling
11727 // in the various CC lowering callbacks.
11728 Flags.setByVal();
11729 }
11730 if (Arg.hasAttribute(Attribute::Preallocated)) {
11731 Flags.setPreallocated();
11732 // Set the byval flag for CCAssignFn callbacks that don't know about
11733 // preallocated. This way we can know how many bytes we should've
11734 // allocated and how many bytes a callee cleanup function will pop. If
11735 // we port preallocated to more targets, we'll have to add custom
11736 // preallocated handling in the various CC lowering callbacks.
11737 Flags.setByVal();
11738 }
11739
11740 // Certain targets (such as MIPS), may have a different ABI alignment
11741 // for a type depending on the context. Give the target a chance to
11742 // specify the alignment it wants.
11743 const Align OriginalAlignment(
11745 Flags.setOrigAlign(OriginalAlignment);
11746
11747 Align MemAlign;
11748 Type *ArgMemTy = nullptr;
11749 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11750 Flags.isByRef()) {
11751 if (!ArgMemTy)
11752 ArgMemTy = Arg.getPointeeInMemoryValueType();
11753
11754 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11755
11756 // For in-memory arguments, size and alignment should be passed from FE.
11757 // BE will guess if this info is not there but there are cases it cannot
11758 // get right.
11759 if (auto ParamAlign = Arg.getParamStackAlign())
11760 MemAlign = *ParamAlign;
11761 else if ((ParamAlign = Arg.getParamAlign()))
11762 MemAlign = *ParamAlign;
11763 else
11764 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
11765 if (Flags.isByRef())
11766 Flags.setByRefSize(MemSize);
11767 else
11768 Flags.setByValSize(MemSize);
11769 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11770 MemAlign = *ParamAlign;
11771 } else {
11772 MemAlign = OriginalAlignment;
11773 }
11774 Flags.setMemAlign(MemAlign);
11775
11776 if (Arg.hasAttribute(Attribute::Nest))
11777 Flags.setNest();
11778 if (NeedsRegBlock)
11779 Flags.setInConsecutiveRegs();
11780 if (ArgCopyElisionCandidates.count(&Arg))
11781 Flags.setCopyElisionCandidate();
11782 if (Arg.hasAttribute(Attribute::Returned))
11783 Flags.setReturned();
11784
11786 *CurDAG->getContext(), F.getCallingConv(), VT);
11787 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
11788 *CurDAG->getContext(), F.getCallingConv(), VT);
11789 for (unsigned i = 0; i != NumRegs; ++i) {
11790 // For scalable vectors, use the minimum size; individual targets
11791 // are responsible for handling scalable vector arguments and
11792 // return values.
11793 ISD::InputArg MyFlags(
11794 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
11795 PartBase + i * RegisterVT.getStoreSize().getKnownMinValue());
11796 if (NumRegs > 1 && i == 0)
11797 MyFlags.Flags.setSplit();
11798 // if it isn't first piece, alignment must be 1
11799 else if (i > 0) {
11800 MyFlags.Flags.setOrigAlign(Align(1));
11801 if (i == NumRegs - 1)
11802 MyFlags.Flags.setSplitEnd();
11803 }
11804 Ins.push_back(MyFlags);
11805 }
11806 if (NeedsRegBlock && Value == NumValues - 1)
11807 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
11808 PartBase += VT.getStoreSize().getKnownMinValue();
11809 }
11810 }
11811
11812 // Call the target to set up the argument values.
11814 SDValue NewRoot = TLI->LowerFormalArguments(
11815 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
11816
11817 // Verify that the target's LowerFormalArguments behaved as expected.
11818 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
11819 "LowerFormalArguments didn't return a valid chain!");
11820 assert(InVals.size() == Ins.size() &&
11821 "LowerFormalArguments didn't emit the correct number of values!");
11822 LLVM_DEBUG({
11823 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
11824 assert(InVals[i].getNode() &&
11825 "LowerFormalArguments emitted a null value!");
11826 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
11827 "LowerFormalArguments emitted a value with the wrong type!");
11828 }
11829 });
11830
11831 // Update the DAG with the new chain value resulting from argument lowering.
11832 DAG.setRoot(NewRoot);
11833
11834 // Set up the argument values.
11835 unsigned i = 0;
11836 if (!FuncInfo->CanLowerReturn) {
11837 // Create a virtual register for the sret pointer, and put in a copy
11838 // from the sret argument into it.
11839 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11840 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
11841 std::optional<ISD::NodeType> AssertOp;
11842 SDValue ArgValue =
11843 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
11844 F.getCallingConv(), AssertOp);
11845
11846 MachineFunction& MF = SDB->DAG.getMachineFunction();
11848 Register SRetReg =
11849 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
11850 FuncInfo->DemoteRegister = SRetReg;
11851 NewRoot =
11852 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
11853 DAG.setRoot(NewRoot);
11854
11855 // i indexes lowered arguments. Bump it past the hidden sret argument.
11856 ++i;
11857 }
11858
11860 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
11861 for (const Argument &Arg : F.args()) {
11862 SmallVector<SDValue, 4> ArgValues;
11863 SmallVector<EVT, 4> ValueVTs;
11864 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
11865 unsigned NumValues = ValueVTs.size();
11866 if (NumValues == 0)
11867 continue;
11868
11869 bool ArgHasUses = !Arg.use_empty();
11870
11871 // Elide the copying store if the target loaded this argument from a
11872 // suitable fixed stack object.
11873 if (Ins[i].Flags.isCopyElisionCandidate()) {
11874 unsigned NumParts = 0;
11875 for (EVT VT : ValueVTs)
11877 F.getCallingConv(), VT);
11878
11879 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
11880 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
11881 ArrayRef(&InVals[i], NumParts), ArgHasUses);
11882 }
11883
11884 // If this argument is unused then remember its value. It is used to generate
11885 // debugging information.
11886 bool isSwiftErrorArg =
11888 Arg.hasAttribute(Attribute::SwiftError);
11889 if (!ArgHasUses && !isSwiftErrorArg) {
11890 SDB->setUnusedArgValue(&Arg, InVals[i]);
11891
11892 // Also remember any frame index for use in FastISel.
11893 if (FrameIndexSDNode *FI =
11894 dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
11895 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11896 }
11897
11898 for (unsigned Val = 0; Val != NumValues; ++Val) {
11899 EVT VT = ValueVTs[Val];
11901 F.getCallingConv(), VT);
11902 unsigned NumParts = TLI->getNumRegistersForCallingConv(
11903 *CurDAG->getContext(), F.getCallingConv(), VT);
11904
11905 // Even an apparent 'unused' swifterror argument needs to be returned. So
11906 // we do generate a copy for it that can be used on return from the
11907 // function.
11908 if (ArgHasUses || isSwiftErrorArg) {
11909 std::optional<ISD::NodeType> AssertOp;
11910 if (Arg.hasAttribute(Attribute::SExt))
11911 AssertOp = ISD::AssertSext;
11912 else if (Arg.hasAttribute(Attribute::ZExt))
11913 AssertOp = ISD::AssertZext;
11914
11915 SDValue OutVal =
11916 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
11917 NewRoot, F.getCallingConv(), AssertOp);
11918
11919 FPClassTest NoFPClass = Arg.getNoFPClass();
11920 if (NoFPClass != fcNone) {
11921 SDValue SDNoFPClass = DAG.getTargetConstant(
11922 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
11923 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
11924 OutVal, SDNoFPClass);
11925 }
11926 ArgValues.push_back(OutVal);
11927 }
11928
11929 i += NumParts;
11930 }
11931
11932 // We don't need to do anything else for unused arguments.
11933 if (ArgValues.empty())
11934 continue;
11935
11936 // Note down frame index.
11937 if (FrameIndexSDNode *FI =
11938 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
11939 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11940
11941 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
11942 SDB->getCurSDLoc());
11943
11944 SDB->setValue(&Arg, Res);
11946 // We want to associate the argument with the frame index, among
11947 // involved operands, that correspond to the lowest address. The
11948 // getCopyFromParts function, called earlier, is swapping the order of
11949 // the operands to BUILD_PAIR depending on endianness. The result of
11950 // that swapping is that the least significant bits of the argument will
11951 // be in the first operand of the BUILD_PAIR node, and the most
11952 // significant bits will be in the second operand.
11953 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
11954 if (LoadSDNode *LNode =
11955 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
11956 if (FrameIndexSDNode *FI =
11957 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
11958 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11959 }
11960
11961 // Analyses past this point are naive and don't expect an assertion.
11962 if (Res.getOpcode() == ISD::AssertZext)
11963 Res = Res.getOperand(0);
11964
11965 // Update the SwiftErrorVRegDefMap.
11966 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
11967 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
11968 if (Reg.isVirtual())
11969 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
11970 Reg);
11971 }
11972
11973 // If this argument is live outside of the entry block, insert a copy from
11974 // wherever we got it to the vreg that other BB's will reference it as.
11975 if (Res.getOpcode() == ISD::CopyFromReg) {
11976 // If we can, though, try to skip creating an unnecessary vreg.
11977 // FIXME: This isn't very clean... it would be nice to make this more
11978 // general.
11979 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
11980 if (Reg.isVirtual()) {
11981 FuncInfo->ValueMap[&Arg] = Reg;
11982 continue;
11983 }
11984 }
11986 FuncInfo->InitializeRegForValue(&Arg);
11987 SDB->CopyToExportRegsIfNeeded(&Arg);
11988 }
11989 }
11990
11991 if (!Chains.empty()) {
11992 Chains.push_back(NewRoot);
11993 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
11994 }
11995
11996 DAG.setRoot(NewRoot);
11997
11998 assert(i == InVals.size() && "Argument register count mismatch!");
11999
12000 // If any argument copy elisions occurred and we have debug info, update the
12001 // stale frame indices used in the dbg.declare variable info table.
12002 if (!ArgCopyElisionFrameIndexMap.empty()) {
12005 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12006 if (I != ArgCopyElisionFrameIndexMap.end())
12007 VI.updateStackSlot(I->second);
12008 }
12009 }
12010
12011 // Finally, if the target has anything special to do, allow it to do so.
12013}
12014
12015/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12016/// ensure constants are generated when needed. Remember the virtual registers
12017/// that need to be added to the Machine PHI nodes as input. We cannot just
12018/// directly add them, because expansion might result in multiple MBB's for one
12019/// BB. As such, the start of the BB might correspond to a different MBB than
12020/// the end.
12021void
12022SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12024
12026
12027 // Check PHI nodes in successors that expect a value to be available from this
12028 // block.
12029 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12030 if (!isa<PHINode>(SuccBB->begin())) continue;
12031 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12032
12033 // If this terminator has multiple identical successors (common for
12034 // switches), only handle each succ once.
12035 if (!SuccsHandled.insert(SuccMBB).second)
12036 continue;
12037
12039
12040 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12041 // nodes and Machine PHI nodes, but the incoming operands have not been
12042 // emitted yet.
12043 for (const PHINode &PN : SuccBB->phis()) {
12044 // Ignore dead phi's.
12045 if (PN.use_empty())
12046 continue;
12047
12048 // Skip empty types
12049 if (PN.getType()->isEmptyTy())
12050 continue;
12051
12052 Register Reg;
12053 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12054
12055 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12056 Register &RegOut = ConstantsOut[C];
12057 if (!RegOut) {
12058 RegOut = FuncInfo.CreateRegs(&PN);
12059 // We need to zero/sign extend ConstantInt phi operands to match
12060 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12061 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12062 if (auto *CI = dyn_cast<ConstantInt>(C))
12063 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12065 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12066 }
12067 Reg = RegOut;
12068 } else {
12070 FuncInfo.ValueMap.find(PHIOp);
12071 if (I != FuncInfo.ValueMap.end())
12072 Reg = I->second;
12073 else {
12074 assert(isa<AllocaInst>(PHIOp) &&
12075 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12076 "Didn't codegen value into a register!??");
12077 Reg = FuncInfo.CreateRegs(&PN);
12078 CopyValueToVirtualRegister(PHIOp, Reg);
12079 }
12080 }
12081
12082 // Remember that this register needs to added to the machine PHI node as
12083 // the input for this MBB.
12084 SmallVector<EVT, 4> ValueVTs;
12085 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12086 for (EVT VT : ValueVTs) {
12087 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12088 for (unsigned i = 0; i != NumRegisters; ++i)
12089 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12090 Reg += NumRegisters;
12091 }
12092 }
12093 }
12094
12095 ConstantsOut.clear();
12096}
12097
12098MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12100 if (++I == FuncInfo.MF->end())
12101 return nullptr;
12102 return &*I;
12103}
12104
12105/// During lowering new call nodes can be created (such as memset, etc.).
12106/// Those will become new roots of the current DAG, but complications arise
12107/// when they are tail calls. In such cases, the call lowering will update
12108/// the root, but the builder still needs to know that a tail call has been
12109/// lowered in order to avoid generating an additional return.
12110void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12111 // If the node is null, we do have a tail call.
12112 if (MaybeTC.getNode() != nullptr)
12113 DAG.setRoot(MaybeTC);
12114 else
12115 HasTailCall = true;
12116}
12117
12118void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12119 MachineBasicBlock *SwitchMBB,
12120 MachineBasicBlock *DefaultMBB) {
12121 MachineFunction *CurMF = FuncInfo.MF;
12122 MachineBasicBlock *NextMBB = nullptr;
12124 if (++BBI != FuncInfo.MF->end())
12125 NextMBB = &*BBI;
12126
12127 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12128
12130
12131 if (Size == 2 && W.MBB == SwitchMBB) {
12132 // If any two of the cases has the same destination, and if one value
12133 // is the same as the other, but has one bit unset that the other has set,
12134 // use bit manipulation to do two compares at once. For example:
12135 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12136 // TODO: This could be extended to merge any 2 cases in switches with 3
12137 // cases.
12138 // TODO: Handle cases where W.CaseBB != SwitchBB.
12139 CaseCluster &Small = *W.FirstCluster;
12140 CaseCluster &Big = *W.LastCluster;
12141
12142 if (Small.Low == Small.High && Big.Low == Big.High &&
12143 Small.MBB == Big.MBB) {
12144 const APInt &SmallValue = Small.Low->getValue();
12145 const APInt &BigValue = Big.Low->getValue();
12146
12147 // Check that there is only one bit different.
12148 APInt CommonBit = BigValue ^ SmallValue;
12149 if (CommonBit.isPowerOf2()) {
12150 SDValue CondLHS = getValue(Cond);
12151 EVT VT = CondLHS.getValueType();
12152 SDLoc DL = getCurSDLoc();
12153
12154 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12155 DAG.getConstant(CommonBit, DL, VT));
12157 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12158 ISD::SETEQ);
12159
12160 // Update successor info.
12161 // Both Small and Big will jump to Small.BB, so we sum up the
12162 // probabilities.
12163 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12164 if (BPI)
12165 addSuccessorWithProb(
12166 SwitchMBB, DefaultMBB,
12167 // The default destination is the first successor in IR.
12168 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12169 else
12170 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12171
12172 // Insert the true branch.
12173 SDValue BrCond =
12174 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12175 DAG.getBasicBlock(Small.MBB));
12176 // Insert the false branch.
12177 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12178 DAG.getBasicBlock(DefaultMBB));
12179
12180 DAG.setRoot(BrCond);
12181 return;
12182 }
12183 }
12184 }
12185
12186 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12187 // Here, we order cases by probability so the most likely case will be
12188 // checked first. However, two clusters can have the same probability in
12189 // which case their relative ordering is non-deterministic. So we use Low
12190 // as a tie-breaker as clusters are guaranteed to never overlap.
12191 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12192 [](const CaseCluster &a, const CaseCluster &b) {
12193 return a.Prob != b.Prob ?
12194 a.Prob > b.Prob :
12195 a.Low->getValue().slt(b.Low->getValue());
12196 });
12197
12198 // Rearrange the case blocks so that the last one falls through if possible
12199 // without changing the order of probabilities.
12200 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12201 --I;
12202 if (I->Prob > W.LastCluster->Prob)
12203 break;
12204 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12205 std::swap(*I, *W.LastCluster);
12206 break;
12207 }
12208 }
12209 }
12210
12211 // Compute total probability.
12212 BranchProbability DefaultProb = W.DefaultProb;
12213 BranchProbability UnhandledProbs = DefaultProb;
12214 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12215 UnhandledProbs += I->Prob;
12216
12217 MachineBasicBlock *CurMBB = W.MBB;
12218 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12219 bool FallthroughUnreachable = false;
12220 MachineBasicBlock *Fallthrough;
12221 if (I == W.LastCluster) {
12222 // For the last cluster, fall through to the default destination.
12223 Fallthrough = DefaultMBB;
12224 FallthroughUnreachable = isa<UnreachableInst>(
12225 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12226 } else {
12227 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12228 CurMF->insert(BBI, Fallthrough);
12229 // Put Cond in a virtual register to make it available from the new blocks.
12231 }
12232 UnhandledProbs -= I->Prob;
12233
12234 switch (I->Kind) {
12235 case CC_JumpTable: {
12236 // FIXME: Optimize away range check based on pivot comparisons.
12237 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12238 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12239
12240 // The jump block hasn't been inserted yet; insert it here.
12241 MachineBasicBlock *JumpMBB = JT->MBB;
12242 CurMF->insert(BBI, JumpMBB);
12243
12244 auto JumpProb = I->Prob;
12245 auto FallthroughProb = UnhandledProbs;
12246
12247 // If the default statement is a target of the jump table, we evenly
12248 // distribute the default probability to successors of CurMBB. Also
12249 // update the probability on the edge from JumpMBB to Fallthrough.
12250 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12251 SE = JumpMBB->succ_end();
12252 SI != SE; ++SI) {
12253 if (*SI == DefaultMBB) {
12254 JumpProb += DefaultProb / 2;
12255 FallthroughProb -= DefaultProb / 2;
12256 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12257 JumpMBB->normalizeSuccProbs();
12258 break;
12259 }
12260 }
12261
12262 // If the default clause is unreachable, propagate that knowledge into
12263 // JTH->FallthroughUnreachable which will use it to suppress the range
12264 // check.
12265 //
12266 // However, don't do this if we're doing branch target enforcement,
12267 // because a table branch _without_ a range check can be a tempting JOP
12268 // gadget - out-of-bounds inputs that are impossible in correct
12269 // execution become possible again if an attacker can influence the
12270 // control flow. So if an attacker doesn't already have a BTI bypass
12271 // available, we don't want them to be able to get one out of this
12272 // table branch.
12273 if (FallthroughUnreachable) {
12274 Function &CurFunc = CurMF->getFunction();
12275 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12276 JTH->FallthroughUnreachable = true;
12277 }
12278
12279 if (!JTH->FallthroughUnreachable)
12280 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12281 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12282 CurMBB->normalizeSuccProbs();
12283
12284 // The jump table header will be inserted in our current block, do the
12285 // range check, and fall through to our fallthrough block.
12286 JTH->HeaderBB = CurMBB;
12287 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12288
12289 // If we're in the right place, emit the jump table header right now.
12290 if (CurMBB == SwitchMBB) {
12291 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12292 JTH->Emitted = true;
12293 }
12294 break;
12295 }
12296 case CC_BitTests: {
12297 // FIXME: Optimize away range check based on pivot comparisons.
12298 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12299
12300 // The bit test blocks haven't been inserted yet; insert them here.
12301 for (BitTestCase &BTC : BTB->Cases)
12302 CurMF->insert(BBI, BTC.ThisBB);
12303
12304 // Fill in fields of the BitTestBlock.
12305 BTB->Parent = CurMBB;
12306 BTB->Default = Fallthrough;
12307
12308 BTB->DefaultProb = UnhandledProbs;
12309 // If the cases in bit test don't form a contiguous range, we evenly
12310 // distribute the probability on the edge to Fallthrough to two
12311 // successors of CurMBB.
12312 if (!BTB->ContiguousRange) {
12313 BTB->Prob += DefaultProb / 2;
12314 BTB->DefaultProb -= DefaultProb / 2;
12315 }
12316
12317 if (FallthroughUnreachable)
12318 BTB->FallthroughUnreachable = true;
12319
12320 // If we're in the right place, emit the bit test header right now.
12321 if (CurMBB == SwitchMBB) {
12322 visitBitTestHeader(*BTB, SwitchMBB);
12323 BTB->Emitted = true;
12324 }
12325 break;
12326 }
12327 case CC_Range: {
12328 const Value *RHS, *LHS, *MHS;
12329 ISD::CondCode CC;
12330 if (I->Low == I->High) {
12331 // Check Cond == I->Low.
12332 CC = ISD::SETEQ;
12333 LHS = Cond;
12334 RHS=I->Low;
12335 MHS = nullptr;
12336 } else {
12337 // Check I->Low <= Cond <= I->High.
12338 CC = ISD::SETLE;
12339 LHS = I->Low;
12340 MHS = Cond;
12341 RHS = I->High;
12342 }
12343
12344 // If Fallthrough is unreachable, fold away the comparison.
12345 if (FallthroughUnreachable)
12346 CC = ISD::SETTRUE;
12347
12348 // The false probability is the sum of all unhandled cases.
12349 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12350 getCurSDLoc(), I->Prob, UnhandledProbs);
12351
12352 if (CurMBB == SwitchMBB)
12353 visitSwitchCase(CB, SwitchMBB);
12354 else
12355 SL->SwitchCases.push_back(CB);
12356
12357 break;
12358 }
12359 }
12360 CurMBB = Fallthrough;
12361 }
12362}
12363
12364void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12365 const SwitchWorkListItem &W,
12366 Value *Cond,
12367 MachineBasicBlock *SwitchMBB) {
12368 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12369 "Clusters not sorted?");
12370 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12371
12372 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12373 SL->computeSplitWorkItemInfo(W);
12374
12375 // Use the first element on the right as pivot since we will make less-than
12376 // comparisons against it.
12377 CaseClusterIt PivotCluster = FirstRight;
12378 assert(PivotCluster > W.FirstCluster);
12379 assert(PivotCluster <= W.LastCluster);
12380
12381 CaseClusterIt FirstLeft = W.FirstCluster;
12382 CaseClusterIt LastRight = W.LastCluster;
12383
12384 const ConstantInt *Pivot = PivotCluster->Low;
12385
12386 // New blocks will be inserted immediately after the current one.
12388 ++BBI;
12389
12390 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12391 // we can branch to its destination directly if it's squeezed exactly in
12392 // between the known lower bound and Pivot - 1.
12393 MachineBasicBlock *LeftMBB;
12394 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12395 FirstLeft->Low == W.GE &&
12396 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12397 LeftMBB = FirstLeft->MBB;
12398 } else {
12399 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12400 FuncInfo.MF->insert(BBI, LeftMBB);
12401 WorkList.push_back(
12402 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12403 // Put Cond in a virtual register to make it available from the new blocks.
12405 }
12406
12407 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12408 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12409 // directly if RHS.High equals the current upper bound.
12410 MachineBasicBlock *RightMBB;
12411 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12412 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12413 RightMBB = FirstRight->MBB;
12414 } else {
12415 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12416 FuncInfo.MF->insert(BBI, RightMBB);
12417 WorkList.push_back(
12418 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12419 // Put Cond in a virtual register to make it available from the new blocks.
12421 }
12422
12423 // Create the CaseBlock record that will be used to lower the branch.
12424 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12425 getCurSDLoc(), LeftProb, RightProb);
12426
12427 if (W.MBB == SwitchMBB)
12428 visitSwitchCase(CB, SwitchMBB);
12429 else
12430 SL->SwitchCases.push_back(CB);
12431}
12432
12433// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12434// from the swith statement.
12436 BranchProbability PeeledCaseProb) {
12437 if (PeeledCaseProb == BranchProbability::getOne())
12439 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12440
12441 uint32_t Numerator = CaseProb.getNumerator();
12442 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12443 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12444}
12445
12446// Try to peel the top probability case if it exceeds the threshold.
12447// Return current MachineBasicBlock for the switch statement if the peeling
12448// does not occur.
12449// If the peeling is performed, return the newly created MachineBasicBlock
12450// for the peeled switch statement. Also update Clusters to remove the peeled
12451// case. PeeledCaseProb is the BranchProbability for the peeled case.
12452MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12453 const SwitchInst &SI, CaseClusterVector &Clusters,
12454 BranchProbability &PeeledCaseProb) {
12455 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12456 // Don't perform if there is only one cluster or optimizing for size.
12457 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12459 SwitchMBB->getParent()->getFunction().hasMinSize())
12460 return SwitchMBB;
12461
12463 unsigned PeeledCaseIndex = 0;
12464 bool SwitchPeeled = false;
12465 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12466 CaseCluster &CC = Clusters[Index];
12467 if (CC.Prob < TopCaseProb)
12468 continue;
12469 TopCaseProb = CC.Prob;
12470 PeeledCaseIndex = Index;
12471 SwitchPeeled = true;
12472 }
12473 if (!SwitchPeeled)
12474 return SwitchMBB;
12475
12476 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12477 << TopCaseProb << "\n");
12478
12479 // Record the MBB for the peeled switch statement.
12480 MachineFunction::iterator BBI(SwitchMBB);
12481 ++BBI;
12482 MachineBasicBlock *PeeledSwitchMBB =
12484 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12485
12486 ExportFromCurrentBlock(SI.getCondition());
12487 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12488 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12489 nullptr, nullptr, TopCaseProb.getCompl()};
12490 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12491
12492 Clusters.erase(PeeledCaseIt);
12493 for (CaseCluster &CC : Clusters) {
12494 LLVM_DEBUG(
12495 dbgs() << "Scale the probablity for one cluster, before scaling: "
12496 << CC.Prob << "\n");
12497 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12498 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12499 }
12500 PeeledCaseProb = TopCaseProb;
12501 return PeeledSwitchMBB;
12502}
12503
12504void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12505 // Extract cases from the switch.
12507 CaseClusterVector Clusters;
12508 Clusters.reserve(SI.getNumCases());
12509 for (auto I : SI.cases()) {
12510 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12511 const ConstantInt *CaseVal = I.getCaseValue();
12512 BranchProbability Prob =
12513 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12514 : BranchProbability(1, SI.getNumCases() + 1);
12515 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12516 }
12517
12518 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12519
12520 // Cluster adjacent cases with the same destination. We do this at all
12521 // optimization levels because it's cheap to do and will make codegen faster
12522 // if there are many clusters.
12523 sortAndRangeify(Clusters);
12524
12525 // The branch probablity of the peeled case.
12527 MachineBasicBlock *PeeledSwitchMBB =
12528 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12529
12530 // If there is only the default destination, jump there directly.
12531 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12532 if (Clusters.empty()) {
12533 assert(PeeledSwitchMBB == SwitchMBB);
12534 SwitchMBB->addSuccessor(DefaultMBB);
12535 if (DefaultMBB != NextBlock(SwitchMBB)) {
12536 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12537 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12538 }
12539 return;
12540 }
12541
12542 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12543 DAG.getBFI());
12544 SL->findBitTestClusters(Clusters, &SI);
12545
12546 LLVM_DEBUG({
12547 dbgs() << "Case clusters: ";
12548 for (const CaseCluster &C : Clusters) {
12549 if (C.Kind == CC_JumpTable)
12550 dbgs() << "JT:";
12551 if (C.Kind == CC_BitTests)
12552 dbgs() << "BT:";
12553
12554 C.Low->getValue().print(dbgs(), true);
12555 if (C.Low != C.High) {
12556 dbgs() << '-';
12557 C.High->getValue().print(dbgs(), true);
12558 }
12559 dbgs() << ' ';
12560 }
12561 dbgs() << '\n';
12562 });
12563
12564 assert(!Clusters.empty());
12565 SwitchWorkList WorkList;
12566 CaseClusterIt First = Clusters.begin();
12567 CaseClusterIt Last = Clusters.end() - 1;
12568 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12569 // Scale the branchprobability for DefaultMBB if the peel occurs and
12570 // DefaultMBB is not replaced.
12571 if (PeeledCaseProb != BranchProbability::getZero() &&
12572 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12573 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12574 WorkList.push_back(
12575 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12576
12577 while (!WorkList.empty()) {
12578 SwitchWorkListItem W = WorkList.pop_back_val();
12579 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12580
12581 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12582 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12583 // For optimized builds, lower large range as a balanced binary tree.
12584 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12585 continue;
12586 }
12587
12588 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12589 }
12590}
12591
12592void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12594 auto DL = getCurSDLoc();
12595 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12596 setValue(&I, DAG.getStepVector(DL, ResultVT));
12597}
12598
12599void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12601 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12602
12603 SDLoc DL = getCurSDLoc();
12604 SDValue V = getValue(I.getOperand(0));
12605 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12606
12607 if (VT.isScalableVector()) {
12609 return;
12610 }
12611
12612 // Use VECTOR_SHUFFLE for the fixed-length vector
12613 // to maintain existing behavior.
12615 unsigned NumElts = VT.getVectorMinNumElements();
12616 for (unsigned i = 0; i != NumElts; ++i)
12617 Mask.push_back(NumElts - 1 - i);
12618
12619 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12620}
12621
12622void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12623 unsigned Factor) {
12624 auto DL = getCurSDLoc();
12625 SDValue InVec = getValue(I.getOperand(0));
12626
12627 SmallVector<EVT, 4> ValueVTs;
12629 ValueVTs);
12630
12631 EVT OutVT = ValueVTs[0];
12632 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12633
12634 SmallVector<SDValue, 4> SubVecs(Factor);
12635 for (unsigned i = 0; i != Factor; ++i) {
12636 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12637 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12638 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12639 }
12640
12641 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12642 // from existing legalisation and combines.
12643 if (OutVT.isFixedLengthVector() && Factor == 2) {
12644 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12645 createStrideMask(0, 2, OutNumElts));
12646 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12647 createStrideMask(1, 2, OutNumElts));
12648 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12649 setValue(&I, Res);
12650 return;
12651 }
12652
12654 DAG.getVTList(ValueVTs), SubVecs);
12655 setValue(&I, Res);
12656}
12657
12658void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12659 unsigned Factor) {
12660 auto DL = getCurSDLoc();
12662 EVT InVT = getValue(I.getOperand(0)).getValueType();
12663 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12664
12665 SmallVector<SDValue, 8> InVecs(Factor);
12666 for (unsigned i = 0; i < Factor; ++i) {
12667 InVecs[i] = getValue(I.getOperand(i));
12668 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12669 "Expected VTs to be the same");
12670 }
12671
12672 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12673 // from existing legalisation and combines.
12674 if (OutVT.isFixedLengthVector() && Factor == 2) {
12675 unsigned NumElts = InVT.getVectorMinNumElements();
12676 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12677 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12678 createInterleaveMask(NumElts, 2)));
12679 return;
12680 }
12681
12682 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12683 SDValue Res =
12684 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12685
12687 for (unsigned i = 0; i < Factor; ++i)
12688 Results[i] = Res.getValue(i);
12689
12690 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12691 setValue(&I, Res);
12692}
12693
12694void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12695 SmallVector<EVT, 4> ValueVTs;
12697 ValueVTs);
12698 unsigned NumValues = ValueVTs.size();
12699 if (NumValues == 0) return;
12700
12701 SmallVector<SDValue, 4> Values(NumValues);
12702 SDValue Op = getValue(I.getOperand(0));
12703
12704 for (unsigned i = 0; i != NumValues; ++i)
12705 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12706 SDValue(Op.getNode(), Op.getResNo() + i));
12707
12709 DAG.getVTList(ValueVTs), Values));
12710}
12711
12712void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12714 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12715
12716 SDLoc DL = getCurSDLoc();
12717 SDValue V1 = getValue(I.getOperand(0));
12718 SDValue V2 = getValue(I.getOperand(1));
12719 int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue();
12720
12721 // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
12722 if (VT.isScalableVector()) {
12723 setValue(
12724 &I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2,
12726 Imm, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
12727 return;
12728 }
12729
12730 unsigned NumElts = VT.getVectorNumElements();
12731
12732 uint64_t Idx = (NumElts + Imm) % NumElts;
12733
12734 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12736 for (unsigned i = 0; i < NumElts; ++i)
12737 Mask.push_back(Idx + i);
12738 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12739}
12740
12741// Consider the following MIR after SelectionDAG, which produces output in
12742// phyregs in the first case or virtregs in the second case.
12743//
12744// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12745// %5:gr32 = COPY $ebx
12746// %6:gr32 = COPY $edx
12747// %1:gr32 = COPY %6:gr32
12748// %0:gr32 = COPY %5:gr32
12749//
12750// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12751// %1:gr32 = COPY %6:gr32
12752// %0:gr32 = COPY %5:gr32
12753//
12754// Given %0, we'd like to return $ebx in the first case and %5 in the second.
12755// Given %1, we'd like to return $edx in the first case and %6 in the second.
12756//
12757// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12758// to a single virtreg (such as %0). The remaining outputs monotonically
12759// increase in virtreg number from there. If a callbr has no outputs, then it
12760// should not have a corresponding callbr landingpad; in fact, the callbr
12761// landingpad would not even be able to refer to such a callbr.
12763 MachineInstr *MI = MRI.def_begin(Reg)->getParent();
12764 // There is definitely at least one copy.
12765 assert(MI->getOpcode() == TargetOpcode::COPY &&
12766 "start of copy chain MUST be COPY");
12767 Reg = MI->getOperand(1).getReg();
12768
12769 // If the copied register in the first copy must be virtual.
12770 assert(Reg.isVirtual() && "expected COPY of virtual register");
12771 MI = MRI.def_begin(Reg)->getParent();
12772
12773 // There may be an optional second copy.
12774 if (MI->getOpcode() == TargetOpcode::COPY) {
12775 assert(Reg.isVirtual() && "expected COPY of virtual register");
12776 Reg = MI->getOperand(1).getReg();
12777 assert(Reg.isPhysical() && "expected COPY of physical register");
12778 } else {
12779 // The start of the chain must be an INLINEASM_BR.
12780 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12781 "end of copy chain MUST be INLINEASM_BR");
12782 }
12783
12784 return Reg;
12785}
12786
12787// We must do this walk rather than the simpler
12788// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
12789// otherwise we will end up with copies of virtregs only valid along direct
12790// edges.
12791void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
12792 SmallVector<EVT, 8> ResultVTs;
12793 SmallVector<SDValue, 8> ResultValues;
12794 const auto *CBR =
12795 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
12796
12800
12801 Register InitialDef = FuncInfo.ValueMap[CBR];
12802 SDValue Chain = DAG.getRoot();
12803
12804 // Re-parse the asm constraints string.
12805 TargetLowering::AsmOperandInfoVector TargetConstraints =
12806 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
12807 for (auto &T : TargetConstraints) {
12808 SDISelAsmOperandInfo OpInfo(T);
12809 if (OpInfo.Type != InlineAsm::isOutput)
12810 continue;
12811
12812 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
12813 // individual constraint.
12814 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
12815
12816 switch (OpInfo.ConstraintType) {
12819 // Fill in OpInfo.AssignedRegs.Regs.
12820 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
12821
12822 // getRegistersForValue may produce 1 to many registers based on whether
12823 // the OpInfo.ConstraintVT is legal on the target or not.
12824 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
12825 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
12826 if (OriginalDef.isPhysical())
12827 FuncInfo.MBB->addLiveIn(OriginalDef);
12828 // Update the assigned registers to use the original defs.
12829 Reg = OriginalDef;
12830 }
12831
12832 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
12833 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
12834 ResultValues.push_back(V);
12835 ResultVTs.push_back(OpInfo.ConstraintVT);
12836 break;
12837 }
12839 SDValue Flag;
12840 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
12841 OpInfo, DAG);
12842 ++InitialDef;
12843 ResultValues.push_back(V);
12844 ResultVTs.push_back(OpInfo.ConstraintVT);
12845 break;
12846 }
12847 default:
12848 break;
12849 }
12850 }
12852 DAG.getVTList(ResultVTs), ResultValues);
12853 setValue(&I, V);
12854}
unsigned const MachineRegisterInfo * MRI
@ Poison
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.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
return RetTy
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
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,...)
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,...
#define RegName(no)
lazy value info
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static bool isUndef(const MachineInstr &MI)
Register const TargetRegisterInfo * TRI
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.
#define T1
static unsigned 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
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#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.
raw_pwrite_stream & OS
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.
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 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:119
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
Value * RHS
Value * LHS
support::ulittle16_t & Lo
Definition: aarch32.cpp:205
support::ulittle16_t & Hi
Definition: aarch32.cpp:204
Class for arbitrary precision integers.
Definition: APInt.h:78
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1041
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:440
an instruction to allocate memory on the stack
Definition: Instructions.h:64
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instructions.h:128
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:121
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:41
iterator end() const
Definition: ArrayRef.h:136
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
iterator begin() const
Definition: ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:142
A cache of @llvm.assume calls within a function.
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:506
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:709
@ Add
*p = old + v
Definition: Instructions.h:725
@ FAdd
*p = old + v
Definition: Instructions.h:746
@ USubCond
Subtract only if no unsigned overflow.
Definition: Instructions.h:777
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
Definition: Instructions.h:765
@ Min
*p = old <signed v ? old : v
Definition: Instructions.h:739
@ Or
*p = old | v
Definition: Instructions.h:733
@ Sub
*p = old - v
Definition: Instructions.h:727
@ And
*p = old & v
Definition: Instructions.h:729
@ Xor
*p = old ^ v
Definition: Instructions.h:735
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
Definition: Instructions.h:781
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
Definition: Instructions.h:761
@ FSub
*p = old - v
Definition: Instructions.h:749
@ UIncWrap
Increment one up to a maximum value.
Definition: Instructions.h:769
@ Max
*p = old >signed v ? old : v
Definition: Instructions.h:737
@ UMin
*p = old <unsigned v ? old : v
Definition: Instructions.h:743
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
Definition: Instructions.h:757
@ UMax
*p = old >unsigned v ? old : v
Definition: Instructions.h:741
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
Definition: Instructions.h:753
@ UDecWrap
Decrement one until a minimum value or zero.
Definition: Instructions.h:773
@ Nand
*p = ~(old & v)
Definition: Instructions.h:731
static LLVM_ABI AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeList with the specified parameters in it.
LLVM_ABI AttributeSet getRetAttrs() const
The attributes for the ret value are returned.
LLVM_ABI bool hasFnAttr(Attribute::AttrKind Kind) const
Return true if the attribute exists for the function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:337
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.
Definition: BasicBlock.cpp:549
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,...
Definition: BasicBlock.cpp:354
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:213
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
const Instruction & back() const
Definition: BasicBlock.h:484
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
This class represents a no-op cast from one type to another.
bool test(unsigned Idx) const
Definition: BitVector.h:461
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition: BitVector.h:341
BitVector & set()
Definition: BitVector.h:351
size_type size() const
size - Returns the number of bits in this bitvector.
Definition: BitVector.h:159
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...
Definition: InstrTypes.h:1116
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:2083
CallingConv::ID getCallingConv() const
Definition: InstrTypes.h:1406
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1267
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.
Definition: InstrTypes.h:2059
Value * getCalledOperand() const
Definition: InstrTypes.h:1340
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1292
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
Definition: InstrTypes.h:1273
bool isConvergent() const
Determine if the invoke is convergent.
Definition: InstrTypes.h:1967
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1205
unsigned arg_size() const
Definition: InstrTypes.h:1290
AttributeList getAttributes() const
Return the attributes for this call.
Definition: InstrTypes.h:1424
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:666
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:678
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)
Definition: Constants.cpp:868
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)
Definition: Constants.cpp:875
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
This class represents a range of values.
Definition: ConstantRange.h:47
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.
bool isValidLocationForIntrinsic(const DILocation *DL) const
Check that a location is valid for this variable.
Debug location.
Base class for variables.
LLVM_ABI std::optional< uint64_t > getSizeInBits() const
Determines the size of the variable's type.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:708
bool isBigEndian() const
Definition: DataLayout.h:199
unsigned getIndexSizeInBits(unsigned AS) const
The size in bits of indices used for address calculation in getelementptr and for addresses in the gi...
Definition: DataLayout.h:398
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
Definition: DataLayout.h:468
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:846
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....
DIExpression * getExpression() const
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) 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:69
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:177
unsigned size() const
Definition: DenseMap.h:120
bool empty() const
Definition: DenseMap.h:119
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:173
iterator end()
Definition: DenseMap.h:87
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:230
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:124
Diagnostic information for inline asm reporting.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition: TypeSize.h:312
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition: TypeSize.h:318
constexpr bool isScalar() const
Exactly one element.
Definition: TypeSize.h:323
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.
Definition: Instructions.h:429
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:803
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
Register CreateRegs(const Value *V)
SmallPtrSet< const DbgVariableRecord *, 8 > PreprocessedDVRDeclares
Collection of dbg_declare instructions handled after argument lowering and before ISel proper.
MachineBasicBlock * getMBB(const BasicBlock *BB) const
Register DemoteRegister
DemoteRegister - if CanLowerReturn is false, DemoteRegister is a vreg allocated to hold a pointer to ...
BitVector DescribedArgs
Bitvector with a bit set if corresponding argument is described in ArgDbgValues.
DenseMap< const AllocaInst *, int > StaticAllocaMap
StaticAllocaMap - Keep track of frame indices for fixed sized allocas in the entry block.
int getArgumentFrameIndex(const Argument *A)
getArgumentFrameIndex - Get frame index for the byval argument.
bool isExportedInst(const Value *V) const
isExportedInst - Return true if the specified value is an instruction exported from its block.
const LiveOutInfo * GetLiveOutRegInfo(Register Reg)
GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the register is a PHI destinat...
Register InitializeRegForValue(const Value *V)
DenseMap< const Value *, Register > ValueMap
ValueMap - Since we emit code for the function a basic block at a time, we must remember which virtua...
Register ExceptionPointerVirtReg
If the current MBB is a landing pad, the exception pointer and exception selector registers are copie...
MachineBasicBlock::iterator InsertPt
MBB - The current insert position inside the current block.
MachineBasicBlock * MBB
MBB - The current block.
SmallVector< MachineInstr *, 8 > ArgDbgValues
ArgDbgValues - A list of DBG_VALUE instructions created during isel for function arguments that are i...
std::vector< std::pair< MachineInstr *, Register > > PHINodesToUpdate
PHINodesToUpdate - A list of phi instructions whose operand list will be updated after processing the...
unsigned getCurrentCallSite()
Get the call site currently being processed, if any. Return zero if none.
void setCurrentCallSite(unsigned Site)
Set the call site currently being processed.
MachineRegisterInfo * RegInfo
Register CreateReg(MVT VT, bool isDivergent=false)
CreateReg - Allocate a single virtual register for the given type.
bool CanLowerReturn
CanLowerReturn - true iff the function's return value can be lowered to registers.
DenseMap< const Value *, ISD::NodeType > PreferredExtendType
Record the preferred extend type (ISD::SIGN_EXTEND or ISD::ZERO_EXTEND) for a value.
Register getCatchPadExceptionPointerVReg(const Value *CPI, const TargetRegisterClass *RC)
Class to represent function types.
Definition: DerivedTypes.h:105
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:144
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:137
Type * getReturnType() const
Definition: DerivedTypes.h:126
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:739
bool hasGC() const
hasGC/getGC/setGC/clearGC - The name of the garbage collection algorithm to use during code generatio...
Definition: Function.h:344
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.
Definition: Function.cpp:1036
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 isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.h:227
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:727
Garbage collection metadata for a single function.
Definition: GCMetadata.h:80
void addStackRoot(int Num, const Constant *Metadata)
addStackRoot - Registers a root that lives on the stack.
Definition: GCMetadata.h:120
Represents flags for the getelementptr instruction/expression.
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:949
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
Definition: GlobalValue.h:569
bool hasDLLImportStorageClass() const
Definition: GlobalValue.h:280
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:663
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.
Definition: Instruction.h:428
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.
Definition: Metadata.cpp:1789
@ MIN_INT_BITS
Minimum number of bits that can be specified.
Definition: DerivedTypes.h:53
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:56
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
LLVM_ABI void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
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...
An instruction for reading from memory.
Definition: Instructions.h:180
This class is used to represent ISD::LOAD nodes.
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.
Definition: MCContext.cpp:386
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.
Definition: MCContext.cpp:247
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:1077
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.
bool isEHPad() const
Returns true if the block is a landing pad.
void setIsCleanupFuncletEntry(bool V=true)
Indicates if this is the entry block of a cleanup funclet.
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
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
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.
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.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
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.
void setHasPatchPoint(bool s=true)
void setHasStackMap(bool s=true)
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 setStackProtectorIndex(int I)
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)
Description of the location of a variable whose Address is valid and unchanging during function execu...
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
void setCallsUnwindInit(bool b)
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.
void setCallsEHReturn(bool b)
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.
unsigned getTypeIDFor(const GlobalValue *TI)
Return the type id for the specified typeinfo. This is function wide.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
auto getInStackSlotVariableDbgInfo()
Returns the collection of variables for which we have debug info and that have been assigned a stack ...
void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD)
Record annotations associated with a particular label.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & front() const
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)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void erase(iterator MBBI)
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.
Definition: MachineInstr.h:72
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,...
ArrayRef< std::pair< MCRegister, Register > > liveins() const
An SDNode that represents everything that will be needed to construct a MachineInstr.
bool contains(const KeyT &Key) const
Definition: MapVector.h:137
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition: MapVector.h:107
Representation for a specific memory location.
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:182
Root of the metadata hierarchy.
Definition: Metadata.h:63
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:78
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:720
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.
Definition: Constants.cpp:1885
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition: Operator.h:154
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:74
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:78
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.
Represents a use of a SDNode.
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
void dump() 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....
DenseMap< MachineBasicBlock *, SmallVector< unsigned, 4 > > LPadToCallSiteMap
Map a landing pad to the call site indexes.
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"
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.
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 init(GCFunctionInfo *gfi, BatchAAResults *BatchAA, AssumptionCache *AC, const TargetLibraryInfo *li)
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
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
MachineFunction * MF
std::unique_ptr< SwiftErrorValueTracking > SwiftError
virtual void emitFunctionEntryCode()
std::unique_ptr< SelectionDAGBuilder > SDB
Targets can subclass this to parameterize the SelectionDAG lowering and instruction selection process...
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 > 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 std::pair< SDValue, SDValue > EmitTargetCodeForStrlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, MachinePointerInfo SrcPtrInfo) const
virtual SDValue EmitTargetCodeForSetTag(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Addr, SDValue Size, MachinePointerInfo DstPtrInfo, bool ZeroData) const
Help to insert SDNodeFlags automatically in transforming.
Definition: SelectionDAG.h:372
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:229
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned TargetFlags=0)
Definition: SelectionDAG.h:758
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 ...
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
Definition: SelectionDAG.h:578
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
const TargetSubtargetInfo & getSubtarget() const
Definition: SelectionDAG.h:500
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
Definition: SelectionDAG.h:813
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.
BlockFrequencyInfo * getBFI() const
Definition: SelectionDAG.h:514
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
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 getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm, bool ConstantFold=true)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
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 getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
void addMMRAMetadata(const SDNode *Node, MDNode *MMRA)
Set MMRAMetadata to be associated with Node.
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 getElementCount(const SDLoc &DL, EVT VT, ElementCount EC, bool ConstantFold=true)
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
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 SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
void addNoMergeSiteInfo(const SDNode *Node, bool NoMerge)
Set NoMergeSiteInfo to be associated with Node if NoMerge is true.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:504
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:459
LLVM_ABI SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
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 getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2, SDValue InGlue, const SDLoc &DL)
Return a new CALLSEQ_END node, which always must have a glue result (to ensure it's not CSE'd).
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
Definition: SelectionDAG.h:868
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
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)
Definition: SelectionDAG.h:839
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
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
Definition: SelectionDAG.h:498
ProfileSummaryInfo * getPSI() const
Definition: SelectionDAG.h:513
SDValue getTargetFrameIndex(int FI, EVT VT)
Definition: SelectionDAG.h:763
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
Definition: SelectionDAG.h:506
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
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 getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
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 getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
Definition: SelectionDAG.h:902
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize, const SDLoc &DL)
Return a new CALLSEQ_START node, that starts new call frame, in which InSize bytes are set up inside ...
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:499
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 getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
SDValue getTargetConstantFP(double Val, const SDLoc &DL, EVT VT)
Definition: SelectionDAG.h:743
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
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...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
Definition: SelectionDAG.h:707
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:493
LLVM_ABI SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
Definition: SelectionDAG.h:885
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
const FunctionVarLocs * getFunctionVarLocs() const
Returns the result of the AssignmentTrackingAnalysis pass if it's available, otherwise return nullptr...
Definition: SelectionDAG.h:510
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...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
Definition: SelectionDAG.h:511
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
Definition: SelectionDAG.h:587
void addPCSections(const SDNode *Node, MDNode *MD)
Set PCSections to be associated with Node.
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
SDValue getSetCCVP(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Mask, SDValue EVL)
Helper function to make it easier to build VP_SETCCs if you just have an ISD::CondCode instead of an ...
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
Definition: SelectionDAG.h:581
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
Definition: SelectionDAG.h:918
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:380
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
void swap(SmallVectorImpl &RHS)
Definition: SmallVector.h:969
void resize(size_type N)
Definition: SmallVector.h:639
void push_back(const T &Elt)
Definition: SmallVector.h:414
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:287
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
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
void clear()
Clear the memory usage of this object.
An instruction for storing to memory.
Definition: Instructions.h:296
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:151
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:148
TypeSize getElementOffset(unsigned Idx) const
Definition: DataLayout.h:657
Class to represent struct types.
Definition: DerivedTypes.h:218
Register getOrCreateVRegUseAt(const Instruction *, const MachineBasicBlock *, const Value *)
Get or create the swifterror value virtual register for a use of a swifterror by an instruction.
Register getOrCreateVRegDefAt(const Instruction *, const MachineBasicBlock *, const Value *)
Get or create the swifterror value virtual register for a def of a swifterror by an instruction.
const Value * getFunctionArg() const
Get the (unique) function argument that was marked swifterror, or nullptr if this function has no swi...
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.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
TargetInstrInfo - Interface to description of machine instruction set.
Provides information about what library functions are available for the current target.
bool hasOptimizedCodeGen(LibFunc F) const
Tests if the function is both available and a candidate for optimized code generation.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
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 CondMergingParams getJumpConditionMergingParams(Instruction::BinaryOps, const Value *, const Value *) const
const TargetMachine & getTargetMachine() const
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.
bool isJumpExpensive() const
Return true if Flow Control is an expensive operation that should be avoided.
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...
virtual 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 shouldExpandPartialReductionIntrinsic(const IntrinsicInst *I) const
Return true if the @llvm.experimental.vector.partial.reduce.
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 SDValue LowerFormalArguments(SDValue, CallingConv::ID, bool, const SmallVectorImpl< ISD::InputArg > &, const SDLoc &, SelectionDAG &, SmallVectorImpl< SDValue > &) const
This hook must be implemented to lower the incoming (formal) arguments, described by the Ins array,...
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 SDValue LowerReturn(SDValue, CallingConv::ID, bool, const SmallVectorImpl< ISD::OutputArg > &, const SmallVectorImpl< SDValue > &, const SDLoc &, SelectionDAG &) const
This hook must be implemented to lower outgoing return values, described by the Outs array,...
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.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
virtual TargetTransformInfo getTargetTransformInfo(const Function &F) const
Return a TargetTransformInfo for a given function.
TargetOptions Options
CodeModel::Model getCodeModel() const
Returns the code model.
unsigned NoNaNsFPMath
NoNaNsFPMath - This flag is enabled when the -enable-no-nans-fp-math flag is specified on the command...
unsigned EnableFastISel
EnableFastISel - This flag enables fast-path instruction selection which trades away generated code q...
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.
FPOpFusion::FPOpFusionMode AllowFPOpFusion
AllowFPOpFusion - This flag is set by the -fp-contract=xxx option.
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 TargetInstrInfo * getInstrInfo() 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.
LLVM_ABI bool hasConditionalLoadStoreForType(Type *Ty, bool IsStore) const
LLVM_ABI InstructionCost getInstructionCost(const User *U, ArrayRef< const Value * > Operands, TargetCostKind CostKind) const
Estimate the cost of a given IR user when lowered.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition: Triple.h:408
bool isAArch64() const
Tests whether the target is AArch64 (little and big endian).
Definition: Triple.h:995
This class represents a truncation of integer types.
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:346
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
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.
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:267
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
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
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
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:352
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
Value * getOperand(unsigned i) const
Definition: User.h:232
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
bool use_empty() const
Definition: Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1098
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.
Definition: DerivedTypes.h:430
Type * getElementType() const
Definition: DerivedTypes.h:463
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:203
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:172
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:169
const ParentTy * getParent() const
Definition: ilist_node.h:34
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: Lint.cpp:82
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
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.
Definition: BitmaskEnum.h:126
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.
Definition: CallingConv.h:245
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
Definition: CallingConv.h:163
@ 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
@ STACKRESTORE
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain.
Definition: ISDOpcodes.h:1236
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
Definition: ISDOpcodes.h:1232
@ CTLZ_ZERO_UNDEF
Definition: ISDOpcodes.h:774
@ CONVERGENCECTRL_ANCHOR
Definition: ISDOpcodes.h:1535
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:504
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1386
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition: ISDOpcodes.h:45
@ SET_FPENV
Sets the current floating-point environment.
Definition: ISDOpcodes.h:1108
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1458
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1491
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition: ISDOpcodes.h:163
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1379
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:587
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:765
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition: ISDOpcodes.h:387
@ VAEND
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE.
Definition: ISDOpcodes.h:1265
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1381
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1351
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1382
@ RESET_FPENV
Set floating-point environment to default state.
Definition: ISDOpcodes.h:1112
@ 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
@ SET_FPMODE
Sets the current dynamic floating-point control modes.
Definition: ISDOpcodes.h:1131
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:835
@ ATOMIC_LOAD_USUB_COND
Definition: ISDOpcodes.h:1392
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:511
@ VECTOR_FIND_LAST_ACTIVE
Definition: ISDOpcodes.h:1550
@ FMODF
FMODF - Decomposes the operand into integral and fractional parts, each having the same type and sign...
Definition: ISDOpcodes.h:1098
@ FATAN2
FATAN2 - atan2, inspired by libm.
Definition: ISDOpcodes.h:1020
@ FSINCOSPI
FSINCOSPI - Compute both the sine and cosine times pi more accurately than FSINCOS(pi*x),...
Definition: ISDOpcodes.h:1094
@ 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
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
Definition: ISDOpcodes.h:1364
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:862
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:571
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1476
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:410
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1480
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:738
@ ATOMIC_FENCE
OUTCHAIN = ATOMIC_FENCE(INCHAIN, ordering, scope) This corresponds to the fence instruction.
Definition: ISDOpcodes.h:1343
@ RESET_FPMODE
Sets default dynamic floating-point control modes.
Definition: ISDOpcodes.h:1135
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1490
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:505
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition: ISDOpcodes.h:508
@ FAKE_USE
FAKE_USE represents a use of the operand but does not do anything.
Definition: ISDOpcodes.h:1424
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1377
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:975
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:249
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1378
@ INIT_TRAMPOLINE
INIT_TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
Definition: ISDOpcodes.h:1309
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:1018
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition: ISDOpcodes.h:400
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1384
@ FrameIndex
Definition: ISDOpcodes.h:90
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1212
@ ATOMIC_LOAD_USUB_SAT
Definition: ISDOpcodes.h:1393
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition: ISDOpcodes.h:151
@ ANNOTATION_LABEL
ANNOTATION_LABEL - Represents a mid basic block label used by annotations.
Definition: ISDOpcodes.h:1218
@ SET_ROUNDING
Set rounding mode.
Definition: ISDOpcodes.h:957
@ CONVERGENCECTRL_GLUE
Definition: ISDOpcodes.h:1541
@ PARTIAL_REDUCE_UMLA
Definition: ISDOpcodes.h:1511
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:826
@ PREALLOCATED_SETUP
PREALLOCATED_SETUP - This has 2 operands: an input chain and a SRCVALUE with the preallocated call Va...
Definition: ISDOpcodes.h:1270
@ READSTEADYCOUNTER
READSTEADYCOUNTER - This corresponds to the readfixedcounter intrinsic.
Definition: ISDOpcodes.h:1298
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition: ISDOpcodes.h:117
@ CONVERGENCECTRL_ENTRY
Definition: ISDOpcodes.h:1536
@ BR
Control flow instructions. These all have token chains.
Definition: ISDOpcodes.h:1157
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1473
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:773
@ WRITE_REGISTER
Definition: ISDOpcodes.h:135
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1331
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1477
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
Definition: ISDOpcodes.h:1090
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1385
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:347
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1380
@ PREALLOCATED_ARG
PREALLOCATED_ARG - This has 3 operands: an input chain, a SRCVALUE with the preallocated call Value,...
Definition: ISDOpcodes.h:1273
@ BRIND
BRIND - Indirect branch.
Definition: ISDOpcodes.h:1162
@ BR_JT
BR_JT - Jumptable branch.
Definition: ISDOpcodes.h:1166
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition: ISDOpcodes.h:622
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:528
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition: ISDOpcodes.h:535
@ 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:778
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1492
@ 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:663
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
Definition: ISDOpcodes.h:1261
@ ATOMIC_LOAD_FMIN
Definition: ISDOpcodes.h:1387
@ GET_ACTIVE_LANE_MASK
Definition: ISDOpcodes.h:1559
@ 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
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
Definition: ISDOpcodes.h:1335
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1485
@ 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:952
@ CLEANUPRET
CLEANUPRET - Represents a return from a cleanup block funclet.
Definition: ISDOpcodes.h:1227
@ ATOMIC_LOAD_FMAXIMUM
Definition: ISDOpcodes.h:1388
@ GET_FPMODE
Reads the current dynamic floating-point control modes.
Definition: ISDOpcodes.h:1126
@ GET_FPENV
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1103
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:756
@ 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
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1375
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:601
@ 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:563
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:832
@ DEBUGTRAP
DEBUGTRAP - Trap intended to get the attention of a debugger.
Definition: ISDOpcodes.h:1321
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:928
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1448
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1383
@ LOCAL_RECOVER
LOCAL_RECOVER - Represents the llvm.localrecover intrinsic.
Definition: ISDOpcodes.h:130
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
Definition: ISDOpcodes.h:1059
@ UBSANTRAP
UBSANTRAP - Trap with an immediate describing the kind of sanitizer failure.
Definition: ISDOpcodes.h:1325
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:351
@ ATOMIC_LOAD_FMINIMUM
Definition: ISDOpcodes.h:1389
@ DYNAMIC_STACKALLOC
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary.
Definition: ISDOpcodes.h:1151
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:718
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition: ISDOpcodes.h:627
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition: ISDOpcodes.h:406
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:960
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:787
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1493
@ PCMARKER
PCMARKER - This corresponds to the pcmarker intrinsic.
Definition: ISDOpcodes.h:1284
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
Definition: ISDOpcodes.h:1207
@ 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
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1391
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1373
@ 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
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1081
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1374
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:908
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
Definition: ISDOpcodes.h:1292
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:730
@ TRAP
TRAP - Trapping instruction.
Definition: ISDOpcodes.h:1318
@ 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:726
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1474
@ 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:552
@ 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:648
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1372
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:1025
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:941
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition: ISDOpcodes.h:690
@ SPONENTRY
SPONENTRY - Represents the llvm.sponentry intrinsic.
Definition: ISDOpcodes.h:122
@ CONVERGENCECTRL_LOOP
Definition: ISDOpcodes.h:1537
@ INLINEASM
INLINEASM - Represents an inline asm block.
Definition: ISDOpcodes.h:1204
@ 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:927
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1481
@ 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:838
@ BRCOND
BRCOND - Conditional branch.
Definition: ISDOpcodes.h:1180
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1459
@ CATCHRET
CATCHRET - Represents a return from a catch block funclet.
Definition: ISDOpcodes.h:1223
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
Definition: ISDOpcodes.h:1390
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:521
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:360
@ AssertZext
Definition: ISDOpcodes.h:63
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition: ISDOpcodes.h:611
@ GET_DYNAMIC_AREA_OFFSET
GET_DYNAMIC_AREA_OFFSET - get offset from native SP to the address of the most recent dynamic alloca.
Definition: ISDOpcodes.h:1439
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
Definition: ISDOpcodes.h:1086
@ ADJUST_TRAMPOLINE
ADJUST_TRAMPOLINE - This corresponds to the adjust_trampoline intrinsic.
Definition: ISDOpcodes.h:1315
@ 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:543
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
Definition: ISDOpcodes.h:1685
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:149
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)
Definition: PatternMatch.h:49
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:962
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.
Definition: PatternMatch.h:92
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Offsets
Offsets in bytes from the start of the input buffer.
Definition: SIInstrInfo.h:1695
std::vector< CaseCluster > CaseClusterVector
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
CaseClusterVector::iterator CaseClusterIt
std::pair< JumpTableHeader, JumpTable > JumpTableBlock
@ 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.
Reg
All possible values of the reg field in the ModR/M byte.
@ ReallyHidden
Definition: CommandLine.h:139
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:464
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:148
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:66
constexpr double e
Definition: MathExtras.h:47
constexpr float ln2f
Definition: MathExtras.h:64
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
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:349
@ Offset
Definition: DWP.cpp:477
@ Length
Definition: DWP.cpp:477
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:1744
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition: bit.h:307
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:1702
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)
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
@ 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:260
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.
Definition: SCCPSolver.cpp:93
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2155
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:252
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:551
gep_type_iterator gep_type_end(const User *GEP)
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:1197
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:157
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
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:1751
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_SMIN
@ 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:883
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1669
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
auto succ_size(const MachineBasicBlock *BB)
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition: STLExtras.h:322
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.
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.
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
Definition: DebugInfo.cpp:2259
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()).
@ Mul
Product 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:155
@ 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
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs, 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
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
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:24
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:2139
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:1916
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
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:2127
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:280
@ Default
The result values are uniform if and only if all operands are uniform.
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:858
#define N
#define NC
Definition: regutils.h:42
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:760
static LLVM_ABI const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:266
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
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:390
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:397
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:279
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:295
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:345
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:368
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:354
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:380
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:311
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:376
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:318
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:287
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:323
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:331
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:241
Matching combinators.
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.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141
A lightweight accessor for an operand bundle meant to be passed around by value.
Definition: InstrTypes.h:1011
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.
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:249
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.
A cluster of case labels.
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
SmallVector< SDValue, 32 > OutVals
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)