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
294 if (DAG.getMachineFunction().getFunction().getAttributes().hasFnAttr(
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) {
321 if (!I)
322 return Ctx.emitError(ErrMsg);
323
324 if (const CallInst *CI = dyn_cast<CallInst>(I))
325 if (CI->isInlineAsm()) {
326 return Ctx.diagnose(DiagnosticInfoInlineAsm(
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) ==
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
845static void failForInvalidBundles(const CallBase &I, StringRef Name,
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;
1107 Context = DAG.getContext();
1108 LPadToCallSiteMap.clear();
1109 SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1110 AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
1111 *DAG.getMachineFunction().getFunction().getParent());
1112}
1113
1115 NodeMap.clear();
1116 UnusedArgNodeMap.clear();
1117 PendingLoads.clear();
1118 PendingExports.clear();
1119 PendingConstrainedFP.clear();
1120 PendingConstrainedFPStrict.clear();
1121 CurInst = nullptr;
1122 HasTailCall = false;
1123 SDNodeOrder = LowestSDNodeOrder;
1124 StatepointLowering.clear();
1125}
1126
1128 DanglingDebugInfoMap.clear();
1129}
1130
1131// Update DAG root to include dependencies on Pending chains.
1132SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1133 SDValue Root = DAG.getRoot();
1134
1135 if (Pending.empty())
1136 return Root;
1137
1138 // Add current root to PendingChains, unless we already indirectly
1139 // depend on it.
1140 if (Root.getOpcode() != ISD::EntryToken) {
1141 unsigned i = 0, e = Pending.size();
1142 for (; i != e; ++i) {
1143 assert(Pending[i].getNode()->getNumOperands() > 1);
1144 if (Pending[i].getNode()->getOperand(0) == Root)
1145 break; // Don't add the root if we already indirectly depend on it.
1146 }
1147
1148 if (i == e)
1149 Pending.push_back(Root);
1150 }
1151
1152 if (Pending.size() == 1)
1153 Root = Pending[0];
1154 else
1155 Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1156
1157 DAG.setRoot(Root);
1158 Pending.clear();
1159 return Root;
1160}
1161
1165
1167 // 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;
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.
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
1661 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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;
1704 SDDbgValue *SDV = DAG.getVRegDbgValue(
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
1745 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
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.
1803 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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)) {
1833 return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1834 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1835 getValue(CPA->getAddrDiscriminator()),
1836 getValue(CPA->getDiscriminator()));
1837 }
1838
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
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 =
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()) {
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(
1930 ISD::BITCAST, getCurSDLoc(), VT,
1931 DAG.getNode(
1933 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1934 VT.getSizeInBits().getKnownMinValue() / 8, true),
1935 DAG.getConstant(0, getCurSDLoc(), MVT::getIntegerVT(8))));
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
1952 EVT EltVT =
1953 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
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)) {
1979 Register InReg = FuncInfo.InitializeRegForValue(Inst);
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
2018 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
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) ||
2024 TM.getOptLevel() == CodeGenOptLevel::None)
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);
2032 DAG.getMachineFunction().setHasEHContTarget(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.
2049 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
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.
2058 FuncInfo.MBB->setIsEHScopeEntry();
2059 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2060 if (Pers != EHPersonality::Wasm_CXX) {
2061 FuncInfo.MBB->setIsEHFuncletEntry();
2062 FuncInfo.MBB->setIsCleanupFuncletEntry();
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();
2129 BranchProbabilityInfo *BPI = FuncInfo.BPI;
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 }
2139 FuncInfo.MBB->normalizeSuccProbs();
2140
2141 // Create the terminator node.
2142 MachineBasicBlock *CleanupPadMBB =
2143 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2144 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
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) {
2154 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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;
2184 SmallVector<uint64_t, 4> Offsets;
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.
2194 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
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.
2203 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2204 commonAlignment(BaseAlign, Offsets[i]));
2205 }
2206
2207 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
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
2247 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
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");
2288 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2289 Flags.setSwiftError();
2290 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2291 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2292 PointerType::getUnqual(*DAG.getContext()),
2293 /*origidx=*/1, /*partOffs=*/0));
2294 // Create SDNode for the swifterror virtual register.
2295 OutVals.push_back(
2296 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2297 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2298 EVT(TLI.getPointerTy(DL))));
2299 }
2300
2301 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2302 CallingConv::ID CallConv =
2303 DAG.getMachineFunction().getFunction().getCallingConv();
2304 Chain = DAG.getTargetLoweringInfo().LowerReturn(
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
2341 Register Reg = FuncInfo.InitializeRegForValue(V);
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;
2452 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
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
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 +=
2595 TTI.getInstructionCost(InsPair.first, TargetTransformInfo::TCK_Latency);
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
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);
2657 MachineFunction &MF = DAG.getMachineFunction();
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};
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};
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,
2823 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
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.
2857 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
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;
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,
2959 DAG.getBasicBlock(CB.FalseBB));
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!");
2969 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
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.
2996 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2997 SwitchOp =
2998 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
2999
3000 Register JumpTableReg =
3001 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
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(
3011 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
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.
3069 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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,
3087 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
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
3115 .setChain(DAG.getEntryNode())
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,
3150 DAG.getBasicBlock(SPD.getSuccessMBB()));
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
3166 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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,
3192 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
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
3212 .setChain(DAG.getEntryNode())
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.
3246 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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,
3280 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
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);
3307 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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(
3312 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
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(
3318 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
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(
3329 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
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:
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
3411 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3412 break;
3413 }
3414 case Intrinsic::wasm_rethrow: {
3415 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3416 std::array<SDValue, 2> Ops = {
3417 getControlRoot(), // inchain for the terminator node
3418 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3419 TLI.getPointerTy(DAG.getDataLayout()))};
3420 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3421 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
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)) {
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
3446 BranchProbabilityInfo *BPI = FuncInfo.BPI;
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.
3461 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
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.
3478 SmallPtrSet<BasicBlock *, 8> Dests;
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);
3486 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
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.
3501 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
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) {
3511 assert(FuncInfo.MBB->isEHPad() &&
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.
3516 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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];
3537 if (FuncInfo.ExceptionPointerVirtReg) {
3538 Ops[0] = DAG.getZExtOrTrunc(
3539 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3540 FuncInfo.ExceptionPointerVirtReg,
3541 TLI.getPointerTy(DAG.getDataLayout())),
3542 dl, ValueVTs[0]);
3543 } else {
3544 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3545 }
3546 Ops[1] = DAG.getZExtOrTrunc(
3547 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3548 FuncInfo.ExceptionSelectorVirtReg,
3549 TLI.getPointerTy(DAG.getDataLayout())),
3550 dl, ValueVTs[1]);
3551
3552 // Merge into one.
3553 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
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
3587 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
3588 MVT::Other, getControlRoot(),
3589 getValue(I.getAddress())));
3590}
3591
3592void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3593 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3594 DAG.getTarget().Options.NoTrapAfterNoreturn))
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) {
3601 SDNodeFlags Flags;
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) {
3612 SDNodeFlags Flags;
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
3635 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
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 =
3654 nuw = OFBinOp->hasNoUnsignedWrap();
3655 nsw = OFBinOp->hasNoSignedWrap();
3656 }
3657 if (const PossiblyExactOperator *ExactOp =
3659 exact = ExactOp->isExact();
3660 }
3661 SDNodeFlags Flags;
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
3674 SDNodeFlags Flags;
3675 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3676 cast<PossiblyExactOperator>(&I)->isExact());
3677 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
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
3699 SDNodeFlags Flags;
3700 Flags.setSameSign(I.hasSameSign());
3701 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3702
3703 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
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
3718 SDNodeFlags Flags;
3719 Flags.copyFMF(*FPMO);
3720 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3721
3722 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
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;
3737 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
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
3753 SDNodeFlags Flags;
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:
3794 if (TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT) ||
3795 (UseScalarMinMax &&
3796 TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT.getScalarType())))
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:
3807 if (TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT) ||
3808 (UseScalarMinMax &&
3809 TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT.getScalarType())))
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));
3872 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3873 I.getType());
3874 SDNodeFlags Flags;
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
3890 SDNodeFlags Flags;
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)) {
3899 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
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));
3910 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3911 I.getType());
3912 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
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();
3919 SDNodeFlags Flags;
3920 if (auto *TruncInst = dyn_cast<FPMathOperator>(&I))
3921 Flags.copyFMF(*TruncInst);
3922 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3923 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3924 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
3925 DAG.getTargetConstant(
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));
3933 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3934 I.getType());
3935 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
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));
3941 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3942 I.getType());
3943 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
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));
3949 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3950 I.getType());
3951 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
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));
3957 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3958 I.getType());
3959 SDNodeFlags Flags;
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));
3969 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3970 I.getType());
3971 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
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();
3984 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
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();
4008 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
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())
4014 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
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) {
4028 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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) {
4043 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4044 SDValue InVec = getValue(I.getOperand(0));
4045 SDValue InVal = getValue(I.getOperand(1));
4046 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4047 TLI.getVectorIdxTy(DAG.getDataLayout()));
4049 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4050 InVec, InVal, InIdx));
4051}
4052
4053void SelectionDAGBuilder::visitExtractElement(const User &I) {
4054 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4055 SDValue InVec = getValue(I.getOperand(0));
4056 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4057 TLI.getVectorIdxTy(DAG.getDataLayout()));
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));
4066 ArrayRef<int> Mask;
4067 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4068 Mask = SVI->getShuffleMask();
4069 else
4070 Mask = cast<ConstantExpr>(I).getShuffleMask();
4071 SDLoc DL = getCurSDLoc();
4072 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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 =
4080 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4081 DAG.getVectorIdxConstant(0, DL));
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)
4174 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4175 DAG.getVectorIdxConstant(0, DL));
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,
4253 DAG.getVectorIdxConstant(Idx, DL));
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
4273 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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
4320 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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
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
4370 uint64_t Offset =
4371 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4372
4373 // In an inbounds GEP with an offset that is nonnegative even when
4374 // interpreted as signed, assume there is no unsigned overflow.
4375 SDNodeFlags Flags;
4376 if (NW.hasNoUnsignedWrap() ||
4377 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4379
4380 N = DAG.getMemBasePlusOffset(
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);
4408 LLVMContext &Context = *DAG.getContext();
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.
4418 SDNodeFlags Flags;
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()) {
4434 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
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();
4521 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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 =
4539 DAG.getConstant(TySize.getFixedValue(), dl, MVT::getIntegerVT(64));
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.
4547 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
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
4571 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
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
4591 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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 =
4622 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
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 &&
4632 BatchAA->pointsToConstantMemory(MemoryLocation(
4633 SV,
4634 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
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())
4673 : MachinePointerInfo();
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
4695 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4696 DAG.getVTList(ValueVTs), Values));
4697}
4698
4699void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4700 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4701 "call visitStoreToSwiftError when backend supports swifterror");
4702
4703 SmallVector<EVT, 4> ValueVTs;
4704 SmallVector<uint64_t, 4> Offsets;
4705 const Value *SrcV = I.getOperand(0);
4706 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
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) {
4723 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
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 ||
4735 !BatchAA->pointsToConstantMemory(MemoryLocation(
4736 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4737 I.getAAMetadata()))) &&
4738 "load_from_swift_error should not be constant memory");
4739
4740 SmallVector<EVT, 4> ValueVTs;
4741 SmallVector<uint64_t, 4> Offsets;
4742 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
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
4748 SDValue L = DAG.getCopyFromReg(
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
4762 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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;
4779 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
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())
4813 : MachinePointerInfo();
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
4869 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
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
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()));
4969 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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();
4978 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4979 MachinePointerInfo(AS), MachineMemOperand::MOStore,
4980 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4981 if (!UniformBase) {
4982 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4983 Index = getValue(Ptr);
4984 Scale =
4985 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
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
5048 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
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
5079 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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();
5094 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5095 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5096 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5097 Ranges);
5098
5099 if (!UniformBase) {
5100 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5101 Index = getValue(Ptr);
5102 Scale =
5103 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
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
5133 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5134 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5135
5136 MachineFunction &MF = DAG.getMachineFunction();
5137 MachineMemOperand *MMO = MF.getMachineMemOperand(
5138 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5139 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5140 FailureOrdering);
5141
5142 SDValue L = DAG.getAtomicCmpSwap(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS,
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");
5159 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
5160 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
5161 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
5162 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
5163 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
5164 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
5165 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
5166 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
5167 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
5168 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
5169 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
5170 case AtomicRMWInst::FAdd: NT = ISD::ATOMIC_LOAD_FADD; break;
5171 case AtomicRMWInst::FSub: NT = ISD::ATOMIC_LOAD_FSUB; break;
5172 case AtomicRMWInst::FMax: NT = ISD::ATOMIC_LOAD_FMAX; break;
5173 case AtomicRMWInst::FMin: NT = ISD::ATOMIC_LOAD_FMIN; break;
5175 NT = ISD::ATOMIC_LOAD_FMAXIMUM;
5176 break;
5178 NT = ISD::ATOMIC_LOAD_FMINIMUM;
5179 break;
5181 NT = ISD::ATOMIC_LOAD_UINC_WRAP;
5182 break;
5184 NT = ISD::ATOMIC_LOAD_UDEC_WRAP;
5185 break;
5187 NT = ISD::ATOMIC_LOAD_USUB_COND;
5188 break;
5190 NT = ISD::ATOMIC_LOAD_USUB_SAT;
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();
5199 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5200 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5201
5202 MachineFunction &MF = DAG.getMachineFunction();
5203 MachineMemOperand *MMO = MF.getMachineMemOperand(
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();
5220 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5221 SDValue Ops[3];
5222 Ops[0] = getRoot();
5223 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5224 TLI.getFenceOperandTy(DAG.getDataLayout()));
5225 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5226 TLI.getFenceOperandTy(DAG.getDataLayout()));
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
5239 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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
5247 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5248
5249 const MDNode *Ranges = getRangeMetadata(I);
5250 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
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
5276 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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
5284 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5285
5286 MachineFunction &MF = DAG.getMachineFunction();
5287 MachineMemOperand *MMO = MF.getMachineMemOperand(
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
5327 TargetLowering::IntrinsicInfo Info;
5328 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5329 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I,
5330 DAG.getMachineFunction(),
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(),
5337 TLI.getPointerTy(DAG.getDataLayout())));
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).
5368 SDNodeFlags Flags;
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.
5393 MachinePointerInfo MPI;
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;
5399 LocationSize Size = LocationSize::precise(Info.size);
5400 if (Size.hasValue() && !Size.getValue())
5402 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5403 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
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) {
5409 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5410 } else if (!I.getType()->isVoidTy()) {
5411 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5412 } else {
5413 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
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.
5893 SelectionDAG &DAG, const TargetLowering &TLI,
5894 SDNodeFlags Flags) {
5895 bool IsExp10 = false;
5896 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
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.
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).
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
6073 MachineFunction &MF = DAG.getMachineFunction();
6074 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
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()) {
6188 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6189 Register PR = RegInfo.getLiveInPhysReg(Reg);
6190 if (PR)
6191 Reg = PR;
6192 }
6193 if (Reg) {
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) {
6237 SDDbgValue *SDV = DAG.getConstantDbgValue(
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) {
6377 SDDbgValue *SDV = DAG.getVRegDbgValue(
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
6419 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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
6435 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6436 MachinePointerInfo(AS),
6438 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6439
6440 if (!UniformBase) {
6441 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6442 Index = getValue(Ptr);
6443 Scale =
6444 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
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
6473 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
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);
6478 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6479
6480 Value *Default = I.getOperand(2);
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) {
6494 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6495 SDLoc sdl = getCurSDLoc();
6496 DebugLoc dl = getCurDebugLoc();
6497 SDValue Res;
6498
6499 SDNodeFlags Flags;
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:
6517 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6518 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6519 getValue(I.getArgOperand(0))));
6520 return;
6521 case Intrinsic::addressofreturnaddress:
6522 setValue(&I,
6523 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6524 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6525 return;
6526 case Intrinsic::sponentry:
6527 setValue(&I,
6528 DAG.getNode(ISD::SPONENTRY, sdl,
6529 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6530 return;
6531 case Intrinsic::frameaddress:
6532 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6533 TLI.getFrameIndexTy(DAG.getDataLayout()),
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(
6687 ISD::PREALLOCATED_ARG, sdl,
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:
6705 DAG.getMachineFunction().setCallsEHReturn(true);
6706 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6707 MVT::Other,
6709 getValue(I.getArgOperand(0)),
6710 getValue(I.getArgOperand(1))));
6711 return;
6712 case Intrinsic::eh_unwind_init:
6713 DAG.getMachineFunction().setCallsUnwindInit(true);
6714 return;
6715 case Intrinsic::eh_dwarf_cfa:
6716 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6717 TLI.getPointerTy(DAG.getDataLayout()),
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
6724 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6725 return;
6726 }
6727 case Intrinsic::eh_sjlj_functioncontext: {
6728 // Get and store the index of the function context.
6729 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
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));
6740 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
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:
6751 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
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:
6851 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
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:
6877 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6878 getValue(I.getArgOperand(0)).getValueType(),
6879 getValue(I.getArgOperand(0)),
6880 getValue(I.getArgOperand(1)), Flags));
6881 return;
6882 case Intrinsic::maxnum:
6883 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6884 getValue(I.getArgOperand(0)).getValueType(),
6885 getValue(I.getArgOperand(0)),
6886 getValue(I.getArgOperand(1)), Flags));
6887 return;
6888 case Intrinsic::minimum:
6889 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
6890 getValue(I.getArgOperand(0)).getValueType(),
6891 getValue(I.getArgOperand(0)),
6892 getValue(I.getArgOperand(1)), Flags));
6893 return;
6894 case Intrinsic::maximum:
6895 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
6896 getValue(I.getArgOperand(0)).getValueType(),
6897 getValue(I.getArgOperand(0)),
6898 getValue(I.getArgOperand(1)), Flags));
6899 return;
6900 case Intrinsic::minimumnum:
6901 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
6902 getValue(I.getArgOperand(0)).getValueType(),
6903 getValue(I.getArgOperand(0)),
6904 getValue(I.getArgOperand(1)), Flags));
6905 return;
6906 case Intrinsic::maximumnum:
6907 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
6908 getValue(I.getArgOperand(0)).getValueType(),
6909 getValue(I.getArgOperand(0)),
6910 getValue(I.getArgOperand(1)), Flags));
6911 return;
6912 case Intrinsic::copysign:
6913 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
6914 getValue(I.getArgOperand(0)).getValueType(),
6915 getValue(I.getArgOperand(0)),
6916 getValue(I.getArgOperand(1)), Flags));
6917 return;
6918 case Intrinsic::ldexp:
6919 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
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: {
6953 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
6954 getValue(I.getArgOperand(0)).getValueType(),
6955 getValue(I.getArgOperand(0)), Flags));
6956 return;
6957 }
6958 case Intrinsic::fma:
6959 setValue(&I, DAG.getNode(
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).
6983 SDNodeFlags Flags;
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());
6997 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
6998 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
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.
7006 SDValue Mul = DAG.getNode(
7007 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7008 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7009 SDValue Add = DAG.getNode(ISD::FADD, sdl,
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:
7024 setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl,
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());
7031 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7032 getValue(I.getArgOperand(0)),
7033 DAG.getValueType(VT.getScalarType())));
7034 return;
7035 }
7036 case Intrinsic::fptoui_sat: {
7037 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7038 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7039 getValue(I.getArgOperand(0)),
7040 DAG.getValueType(VT.getScalarType())));
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());
7055 MachineFunction &MF = DAG.getMachineFunction();
7056 const Function &F = MF.getFunction();
7057 SDValue Op = getValue(I.getArgOperand(0));
7058 SDNodeFlags Flags;
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 =
7093 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7094 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
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 =
7120 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7121 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7123 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
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();
7160 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
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();
7168 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
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:
7175 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7176 getValue(I.getArgOperand(0)).getValueType(),
7177 getValue(I.getArgOperand(0))));
7178 return;
7179 case Intrinsic::bswap:
7180 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
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();
7188 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
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();
7196 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
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));
7266 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
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());
7340 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7341 Op);
7342 DAG.setRoot(Op);
7343 setValue(&I, Res);
7344 return;
7345 }
7346 case Intrinsic::stackguard: {
7347 MachineFunction &MF = DAG.getMachineFunction();
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);
7356 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7357 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7358 MachinePointerInfo(Global, 0), Align,
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.
7369 MachineFunction &MF = DAG.getMachineFunction();
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,
7390 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7391 MaybeAlign(), MachineMemOperand::MOVolatile);
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.
7425 MachineFunction &MF = DAG.getMachineFunction();
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:
7451 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7452 TLI.getPointerTy(DAG.getDataLayout()),
7453 getValue(I.getArgOperand(0))));
7454 return;
7455 case Intrinsic::gcroot: {
7456 assert(DAG.getMachineFunction().getFunction().hasGC() &&
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:
7496 DAG.setRoot(DAG.getNode(
7497 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7498 DAG.getTargetConstant(
7499 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7500 MVT::i32)));
7501 break;
7502 default: llvm_unreachable("unknown trap intrinsic");
7503 }
7504 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
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
7514 TargetLowering::CallLoweringInfo CLI(DAG);
7515 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7516 CallingConv::C, I.getType(),
7517 DAG.getExternalSymbol(TrapFuncName.data(),
7518 TLI.getPointerTy(DAG.getDataLayout())),
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);
7572 SDValue Result = DAG.getMemIntrinsicNode(
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.
7588 if (TM.getOptLevel() == CodeGenOptLevel::None)
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:
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: {
7668 MachineFunction &MF = DAG.getMachineFunction();
7669 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
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(
7683 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
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)
7694 MachineFunction &MF = DAG.getMachineFunction();
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);
7759 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
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.
7821 MachineSDNode *MN = DAG.getMachineNode(
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;
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) {
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");
7882 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
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: {
7914 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
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
7958 TargetLowering::CallLoweringInfo CLI(DAG);
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 bool isTailCall = I.isTailCall();
7978
7979 // The first argument is the callee. Skip it when assembling the call args.
7980 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
7981 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
7982 I.getArgOperand(Idx)->getType());
7983 Arg.setAttributes(&I, Idx);
7984
7985 // If we have an explicit sret argument that is an Instruction, (i.e., it
7986 // might point to function-local memory), we can't meaningfully tail-call.
7987 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
7988 isTailCall = false;
7989
7990 Args.push_back(Arg);
7991 }
7992
7993 SDValue ConvControlToken;
7994 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
7995 auto *Token = Bundle->Inputs[0].get();
7996 ConvControlToken = getValue(Token);
7997 }
7998
7999 TargetLowering::CallLoweringInfo CLI(DAG);
8000 CLI.setDebugLoc(getCurSDLoc())
8001 .setChain(getRoot())
8002 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8003 getValue(I.getArgOperand(0)), std::move(Args))
8004 .setTailCall(isTailCall && canTailCall(I))
8005 .setIsPreallocated(
8006 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8007 .setConvergent(I.isConvergent())
8008 .setConvergenceControlToken(ConvControlToken);
8009 CLI.CB = &I;
8010
8011 std::pair<SDValue, SDValue> Result =
8012 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8013
8014 if (Result.first.getNode())
8015 setValue(&I, Result.first);
8016 return;
8017 }
8018 case Intrinsic::ptrmask: {
8019 SDValue Ptr = getValue(I.getOperand(0));
8020 SDValue Mask = getValue(I.getOperand(1));
8021
8022 // On arm64_32, pointers are 32 bits when stored in memory, but
8023 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8024 // match the index type, but the pointer is 64 bits, so the mask must be
8025 // zero-extended up to 64 bits to match the pointer.
8026 EVT PtrVT =
8027 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8028 EVT MemVT =
8029 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8030 assert(PtrVT == Ptr.getValueType());
8031 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8032 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8033 // 128-bit, so we have to pad the mask with ones for unused bits.
8034 auto HighOnes = DAG.getNode(
8035 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8036 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8037 PtrVT, sdl));
8038 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8039 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8040 } else if (Mask.getValueType() != PtrVT)
8041 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8042
8043 assert(Mask.getValueType() == PtrVT);
8044 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8045 return;
8046 }
8047 case Intrinsic::threadlocal_address: {
8048 setValue(&I, getValue(I.getOperand(0)));
8049 return;
8050 }
8051 case Intrinsic::get_active_lane_mask: {
8052 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8053 SDValue Index = getValue(I.getOperand(0));
8054 SDValue TripCount = getValue(I.getOperand(1));
8055 EVT ElementVT = Index.getValueType();
8056
8057 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8058 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8059 TripCount));
8060 return;
8061 }
8062
8063 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8064 CCVT.getVectorElementCount());
8065
8066 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8067 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8068 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8069 SDValue VectorInduction = DAG.getNode(
8070 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8071 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8072 VectorTripCount, ISD::CondCode::SETULT);
8073 setValue(&I, SetCC);
8074 return;
8075 }
8076 case Intrinsic::experimental_get_vector_length: {
8077 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8078 "Expected positive VF");
8079 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8080 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8081
8082 SDValue Count = getValue(I.getOperand(0));
8083 EVT CountVT = Count.getValueType();
8084
8085 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8086 visitTargetIntrinsic(I, Intrinsic);
8087 return;
8088 }
8089
8090 // Expand to a umin between the trip count and the maximum elements the type
8091 // can hold.
8092 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8093
8094 // Extend the trip count to at least the result VT.
8095 if (CountVT.bitsLT(VT)) {
8096 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8097 CountVT = VT;
8098 }
8099
8100 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8101 ElementCount::get(VF, IsScalable));
8102
8103 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8104 // Clip to the result type if needed.
8105 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8106
8107 setValue(&I, Trunc);
8108 return;
8109 }
8110 case Intrinsic::experimental_vector_partial_reduce_add: {
8112 visitTargetIntrinsic(I, Intrinsic);
8113 return;
8114 }
8115 SDValue Acc = getValue(I.getOperand(0));
8116 SDValue Input = getValue(I.getOperand(1));
8117 setValue(&I,
8118 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8119 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8120 return;
8121 }
8122 case Intrinsic::experimental_cttz_elts: {
8123 auto DL = getCurSDLoc();
8124 SDValue Op = getValue(I.getOperand(0));
8125 EVT OpVT = Op.getValueType();
8126
8127 if (!TLI.shouldExpandCttzElements(OpVT)) {
8128 visitTargetIntrinsic(I, Intrinsic);
8129 return;
8130 }
8131
8132 if (OpVT.getScalarType() != MVT::i1) {
8133 // Compare the input vector elements to zero & use to count trailing zeros
8134 SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8135 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8136 OpVT.getVectorElementCount());
8137 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8138 }
8139
8140 // If the zero-is-poison flag is set, we can assume the upper limit
8141 // of the result is VF-1.
8142 bool ZeroIsPoison =
8143 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8144 ConstantRange VScaleRange(1, true); // Dummy value.
8145 if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8146 VScaleRange = getVScaleRange(I.getCaller(), 64);
8147 unsigned EltWidth = TLI.getBitWidthForCttzElements(
8148 I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8149
8150 MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8151
8152 // Create the new vector type & get the vector length
8153 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8154 OpVT.getVectorElementCount());
8155
8156 SDValue VL =
8157 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8158
8159 SDValue StepVec = DAG.getStepVector(DL, NewVT);
8160 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8161 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8162 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8163 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8164 SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8165 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8166
8167 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8168 SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
8169
8170 setValue(&I, Ret);
8171 return;
8172 }
8173 case Intrinsic::vector_insert: {
8174 SDValue Vec = getValue(I.getOperand(0));
8175 SDValue SubVec = getValue(I.getOperand(1));
8176 SDValue Index = getValue(I.getOperand(2));
8177
8178 // The intrinsic's index type is i64, but the SDNode requires an index type
8179 // suitable for the target. Convert the index as required.
8180 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8181 if (Index.getValueType() != VectorIdxTy)
8182 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8183
8184 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8185 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8186 Index));
8187 return;
8188 }
8189 case Intrinsic::vector_extract: {
8190 SDValue Vec = getValue(I.getOperand(0));
8191 SDValue Index = getValue(I.getOperand(1));
8192 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8193
8194 // The intrinsic's index type is i64, but the SDNode requires an index type
8195 // suitable for the target. Convert the index as required.
8196 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8197 if (Index.getValueType() != VectorIdxTy)
8198 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8199
8200 setValue(&I,
8201 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8202 return;
8203 }
8204 case Intrinsic::experimental_vector_match: {
8205 SDValue Op1 = getValue(I.getOperand(0));
8206 SDValue Op2 = getValue(I.getOperand(1));
8207 SDValue Mask = getValue(I.getOperand(2));
8208 EVT Op1VT = Op1.getValueType();
8209 EVT Op2VT = Op2.getValueType();
8210 EVT ResVT = Mask.getValueType();
8211 unsigned SearchSize = Op2VT.getVectorNumElements();
8212
8213 // If the target has native support for this vector match operation, lower
8214 // the intrinsic untouched; otherwise, expand it below.
8215 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8216 visitTargetIntrinsic(I, Intrinsic);
8217 return;
8218 }
8219
8220 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8221
8222 for (unsigned i = 0; i < SearchSize; ++i) {
8223 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8224 Op2VT.getVectorElementType(), Op2,
8225 DAG.getVectorIdxConstant(i, sdl));
8226 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8227 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8228 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8229 }
8230
8231 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8232 return;
8233 }
8234 case Intrinsic::vector_reverse:
8235 visitVectorReverse(I);
8236 return;
8237 case Intrinsic::vector_splice:
8238 visitVectorSplice(I);
8239 return;
8240 case Intrinsic::callbr_landingpad:
8241 visitCallBrLandingPad(I);
8242 return;
8243 case Intrinsic::vector_interleave2:
8244 visitVectorInterleave(I, 2);
8245 return;
8246 case Intrinsic::vector_interleave3:
8247 visitVectorInterleave(I, 3);
8248 return;
8249 case Intrinsic::vector_interleave4:
8250 visitVectorInterleave(I, 4);
8251 return;
8252 case Intrinsic::vector_interleave5:
8253 visitVectorInterleave(I, 5);
8254 return;
8255 case Intrinsic::vector_interleave6:
8256 visitVectorInterleave(I, 6);
8257 return;
8258 case Intrinsic::vector_interleave7:
8259 visitVectorInterleave(I, 7);
8260 return;
8261 case Intrinsic::vector_interleave8:
8262 visitVectorInterleave(I, 8);
8263 return;
8264 case Intrinsic::vector_deinterleave2:
8265 visitVectorDeinterleave(I, 2);
8266 return;
8267 case Intrinsic::vector_deinterleave3:
8268 visitVectorDeinterleave(I, 3);
8269 return;
8270 case Intrinsic::vector_deinterleave4:
8271 visitVectorDeinterleave(I, 4);
8272 return;
8273 case Intrinsic::vector_deinterleave5:
8274 visitVectorDeinterleave(I, 5);
8275 return;
8276 case Intrinsic::vector_deinterleave6:
8277 visitVectorDeinterleave(I, 6);
8278 return;
8279 case Intrinsic::vector_deinterleave7:
8280 visitVectorDeinterleave(I, 7);
8281 return;
8282 case Intrinsic::vector_deinterleave8:
8283 visitVectorDeinterleave(I, 8);
8284 return;
8285 case Intrinsic::experimental_vector_compress:
8286 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8287 getValue(I.getArgOperand(0)).getValueType(),
8288 getValue(I.getArgOperand(0)),
8289 getValue(I.getArgOperand(1)),
8290 getValue(I.getArgOperand(2)), Flags));
8291 return;
8292 case Intrinsic::experimental_convergence_anchor:
8293 case Intrinsic::experimental_convergence_entry:
8294 case Intrinsic::experimental_convergence_loop:
8295 visitConvergenceControl(I, Intrinsic);
8296 return;
8297 case Intrinsic::experimental_vector_histogram_add: {
8298 visitVectorHistogram(I, Intrinsic);
8299 return;
8300 }
8301 case Intrinsic::experimental_vector_extract_last_active: {
8302 visitVectorExtractLastActive(I, Intrinsic);
8303 return;
8304 }
8305 case Intrinsic::loop_dependence_war_mask:
8306 setValue(&I,
8308 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8309 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8310 return;
8311 case Intrinsic::loop_dependence_raw_mask:
8312 setValue(&I,
8314 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8315 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8316 return;
8317 }
8318}
8319
8320void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8321 const ConstrainedFPIntrinsic &FPI) {
8322 SDLoc sdl = getCurSDLoc();
8323
8324 // We do not need to serialize constrained FP intrinsics against
8325 // each other or against (nonvolatile) loads, so they can be
8326 // chained like loads.
8327 SDValue Chain = DAG.getRoot();
8329 Opers.push_back(Chain);
8330 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8331 Opers.push_back(getValue(FPI.getArgOperand(I)));
8332
8333 auto pushOutChain = [this](SDValue Result, fp::ExceptionBehavior EB) {
8334 assert(Result.getNode()->getNumValues() == 2);
8335
8336 // Push node to the appropriate list so that future instructions can be
8337 // chained up correctly.
8338 SDValue OutChain = Result.getValue(1);
8339 switch (EB) {
8341 // The only reason why ebIgnore nodes still need to be chained is that
8342 // they might depend on the current rounding mode, and therefore must
8343 // not be moved across instruction that may change that mode.
8344 [[fallthrough]];
8346 // These must not be moved across calls or instructions that may change
8347 // floating-point exception masks.
8348 PendingConstrainedFP.push_back(OutChain);
8349 break;
8351 // These must not be moved across calls or instructions that may change
8352 // floating-point exception masks or read floating-point exception flags.
8353 // In addition, they cannot be optimized out even if unused.
8354 PendingConstrainedFPStrict.push_back(OutChain);
8355 break;
8356 }
8357 };
8358
8359 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8360 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8361 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8363
8364 SDNodeFlags Flags;
8366 Flags.setNoFPExcept(true);
8367
8368 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8369 Flags.copyFMF(*FPOp);
8370
8371 unsigned Opcode;
8372 switch (FPI.getIntrinsicID()) {
8373 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8374#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8375 case Intrinsic::INTRINSIC: \
8376 Opcode = ISD::STRICT_##DAGN; \
8377 break;
8378#include "llvm/IR/ConstrainedOps.def"
8379 case Intrinsic::experimental_constrained_fmuladd: {
8380 Opcode = ISD::STRICT_FMA;
8381 // Break fmuladd into fmul and fadd.
8382 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8383 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8384 Opers.pop_back();
8385 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8386 pushOutChain(Mul, EB);
8387 Opcode = ISD::STRICT_FADD;
8388 Opers.clear();
8389 Opers.push_back(Mul.getValue(1));
8390 Opers.push_back(Mul.getValue(0));
8391 Opers.push_back(getValue(FPI.getArgOperand(2)));
8392 }
8393 break;
8394 }
8395 }
8396
8397 // A few strict DAG nodes carry additional operands that are not
8398 // set up by the default code above.
8399 switch (Opcode) {
8400 default: break;
8402 Opers.push_back(
8403 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8404 break;
8405 case ISD::STRICT_FSETCC:
8406 case ISD::STRICT_FSETCCS: {
8408 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8409 if (TM.Options.NoNaNsFPMath)
8410 Condition = getFCmpCodeWithoutNaN(Condition);
8411 Opers.push_back(DAG.getCondCode(Condition));
8412 break;
8413 }
8414 }
8415
8416 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8417 pushOutChain(Result, EB);
8418
8419 SDValue FPResult = Result.getValue(0);
8420 setValue(&FPI, FPResult);
8421}
8422
8423static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8424 std::optional<unsigned> ResOPC;
8425 switch (VPIntrin.getIntrinsicID()) {
8426 case Intrinsic::vp_ctlz: {
8427 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8428 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8429 break;
8430 }
8431 case Intrinsic::vp_cttz: {
8432 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8433 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8434 break;
8435 }
8436 case Intrinsic::vp_cttz_elts: {
8437 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8438 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8439 break;
8440 }
8441#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8442 case Intrinsic::VPID: \
8443 ResOPC = ISD::VPSD; \
8444 break;
8445#include "llvm/IR/VPIntrinsics.def"
8446 }
8447
8448 if (!ResOPC)
8450 "Inconsistency: no SDNode available for this VPIntrinsic!");
8451
8452 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8453 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8454 if (VPIntrin.getFastMathFlags().allowReassoc())
8455 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8456 : ISD::VP_REDUCE_FMUL;
8457 }
8458
8459 return *ResOPC;
8460}
8461
8462void SelectionDAGBuilder::visitVPLoad(
8463 const VPIntrinsic &VPIntrin, EVT VT,
8464 const SmallVectorImpl<SDValue> &OpValues) {
8465 SDLoc DL = getCurSDLoc();
8466 Value *PtrOperand = VPIntrin.getArgOperand(0);
8467 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8468 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8469 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8470 SDValue LD;
8471 // Do not serialize variable-length loads of constant memory with
8472 // anything.
8473 if (!Alignment)
8474 Alignment = DAG.getEVTAlign(VT);
8475 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8476 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8477 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8478 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8479 MachineMemOperand::Flags MMOFlags =
8480 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8481 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8482 MachinePointerInfo(PtrOperand), MMOFlags,
8483 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8484 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8485 MMO, false /*IsExpanding */);
8486 if (AddToChain)
8487 PendingLoads.push_back(LD.getValue(1));
8488 setValue(&VPIntrin, LD);
8489}
8490
8491void SelectionDAGBuilder::visitVPLoadFF(
8492 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8493 const SmallVectorImpl<SDValue> &OpValues) {
8494 assert(OpValues.size() == 3 && "Unexpected number of operands");
8495 SDLoc DL = getCurSDLoc();
8496 Value *PtrOperand = VPIntrin.getArgOperand(0);
8497 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8498 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8499 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8500 SDValue LD;
8501 // Do not serialize variable-length loads of constant memory with
8502 // anything.
8503 if (!Alignment)
8504 Alignment = DAG.getEVTAlign(VT);
8505 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8506 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8507 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8508 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8509 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8510 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8511 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8512 MMO);
8513 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8514 if (AddToChain)
8515 PendingLoads.push_back(LD.getValue(2));
8516 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8517}
8518
8519void SelectionDAGBuilder::visitVPGather(
8520 const VPIntrinsic &VPIntrin, EVT VT,
8521 const SmallVectorImpl<SDValue> &OpValues) {
8522 SDLoc DL = getCurSDLoc();
8523 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8524 Value *PtrOperand = VPIntrin.getArgOperand(0);
8525 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8526 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8527 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8528 SDValue LD;
8529 if (!Alignment)
8530 Alignment = DAG.getEVTAlign(VT.getScalarType());
8531 unsigned AS =
8532 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8533 MachineMemOperand::Flags MMOFlags =
8534 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8535 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8536 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8537 *Alignment, AAInfo, Ranges);
8538 SDValue Base, Index, Scale;
8539 bool UniformBase =
8540 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8541 VT.getScalarStoreSize());
8542 if (!UniformBase) {
8543 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8544 Index = getValue(PtrOperand);
8545 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8546 }
8547 EVT IdxVT = Index.getValueType();
8548 EVT EltTy = IdxVT.getVectorElementType();
8549 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8550 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8551 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8552 }
8553 LD = DAG.getGatherVP(
8554 DAG.getVTList(VT, MVT::Other), VT, DL,
8555 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8557 PendingLoads.push_back(LD.getValue(1));
8558 setValue(&VPIntrin, LD);
8559}
8560
8561void SelectionDAGBuilder::visitVPStore(
8562 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8563 SDLoc DL = getCurSDLoc();
8564 Value *PtrOperand = VPIntrin.getArgOperand(1);
8565 EVT VT = OpValues[0].getValueType();
8566 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8567 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8568 SDValue ST;
8569 if (!Alignment)
8570 Alignment = DAG.getEVTAlign(VT);
8571 SDValue Ptr = OpValues[1];
8572 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8573 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8574 MachineMemOperand::Flags MMOFlags =
8575 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8576 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8577 MachinePointerInfo(PtrOperand), MMOFlags,
8578 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8579 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8580 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8581 /* IsTruncating */ false, /*IsCompressing*/ false);
8582 DAG.setRoot(ST);
8583 setValue(&VPIntrin, ST);
8584}
8585
8586void SelectionDAGBuilder::visitVPScatter(
8587 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8588 SDLoc DL = getCurSDLoc();
8589 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8590 Value *PtrOperand = VPIntrin.getArgOperand(1);
8591 EVT VT = OpValues[0].getValueType();
8592 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8593 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8594 SDValue ST;
8595 if (!Alignment)
8596 Alignment = DAG.getEVTAlign(VT.getScalarType());
8597 unsigned AS =
8598 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8599 MachineMemOperand::Flags MMOFlags =
8600 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8601 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8602 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8603 *Alignment, AAInfo);
8604 SDValue Base, Index, Scale;
8605 bool UniformBase =
8606 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8607 VT.getScalarStoreSize());
8608 if (!UniformBase) {
8609 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8610 Index = getValue(PtrOperand);
8611 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8612 }
8613 EVT IdxVT = Index.getValueType();
8614 EVT EltTy = IdxVT.getVectorElementType();
8615 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8616 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8617 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8618 }
8619 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8620 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8621 OpValues[2], OpValues[3]},
8622 MMO, ISD::SIGNED_SCALED);
8623 DAG.setRoot(ST);
8624 setValue(&VPIntrin, ST);
8625}
8626
8627void SelectionDAGBuilder::visitVPStridedLoad(
8628 const VPIntrinsic &VPIntrin, EVT VT,
8629 const SmallVectorImpl<SDValue> &OpValues) {
8630 SDLoc DL = getCurSDLoc();
8631 Value *PtrOperand = VPIntrin.getArgOperand(0);
8632 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8633 if (!Alignment)
8634 Alignment = DAG.getEVTAlign(VT.getScalarType());
8635 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8636 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8637 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8638 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8639 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8640 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8641 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8642 MachineMemOperand::Flags MMOFlags =
8643 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8644 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8645 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8646 *Alignment, AAInfo, Ranges);
8647
8648 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8649 OpValues[2], OpValues[3], MMO,
8650 false /*IsExpanding*/);
8651
8652 if (AddToChain)
8653 PendingLoads.push_back(LD.getValue(1));
8654 setValue(&VPIntrin, LD);
8655}
8656
8657void SelectionDAGBuilder::visitVPStridedStore(
8658 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8659 SDLoc DL = getCurSDLoc();
8660 Value *PtrOperand = VPIntrin.getArgOperand(1);
8661 EVT VT = OpValues[0].getValueType();
8662 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8663 if (!Alignment)
8664 Alignment = DAG.getEVTAlign(VT.getScalarType());
8665 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8666 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8667 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8668 MachineMemOperand::Flags MMOFlags =
8669 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8670 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8671 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8672 *Alignment, AAInfo);
8673
8674 SDValue ST = DAG.getStridedStoreVP(
8675 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8676 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8677 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8678 /*IsCompressing*/ false);
8679
8680 DAG.setRoot(ST);
8681 setValue(&VPIntrin, ST);
8682}
8683
8684void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8685 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8686 SDLoc DL = getCurSDLoc();
8687
8688 ISD::CondCode Condition;
8690 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8691 if (IsFP) {
8692 // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
8693 // flags, but calls that don't return floating-point types can't be
8694 // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
8695 Condition = getFCmpCondCode(CondCode);
8696 if (TM.Options.NoNaNsFPMath)
8697 Condition = getFCmpCodeWithoutNaN(Condition);
8698 } else {
8699 Condition = getICmpCondCode(CondCode);
8700 }
8701
8702 SDValue Op1 = getValue(VPIntrin.getOperand(0));
8703 SDValue Op2 = getValue(VPIntrin.getOperand(1));
8704 // #2 is the condition code
8705 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8706 SDValue EVL = getValue(VPIntrin.getOperand(4));
8707 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8708 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8709 "Unexpected target EVL type");
8710 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8711
8712 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8713 VPIntrin.getType());
8714 setValue(&VPIntrin,
8715 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8716}
8717
8718void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8719 const VPIntrinsic &VPIntrin) {
8720 SDLoc DL = getCurSDLoc();
8721 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8722
8723 auto IID = VPIntrin.getIntrinsicID();
8724
8725 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8726 return visitVPCmp(*CmpI);
8727
8728 SmallVector<EVT, 4> ValueVTs;
8729 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8730 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8731 SDVTList VTs = DAG.getVTList(ValueVTs);
8732
8733 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8734
8735 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8736 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8737 "Unexpected target EVL type");
8738
8739 // Request operands.
8740 SmallVector<SDValue, 7> OpValues;
8741 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8742 auto Op = getValue(VPIntrin.getArgOperand(I));
8743 if (I == EVLParamPos)
8744 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8745 OpValues.push_back(Op);
8746 }
8747
8748 switch (Opcode) {
8749 default: {
8750 SDNodeFlags SDFlags;
8751 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8752 SDFlags.copyFMF(*FPMO);
8753 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8754 setValue(&VPIntrin, Result);
8755 break;
8756 }
8757 case ISD::VP_LOAD:
8758 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8759 break;
8760 case ISD::VP_LOAD_FF:
8761 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8762 break;
8763 case ISD::VP_GATHER:
8764 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8765 break;
8766 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8767 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8768 break;
8769 case ISD::VP_STORE:
8770 visitVPStore(VPIntrin, OpValues);
8771 break;
8772 case ISD::VP_SCATTER:
8773 visitVPScatter(VPIntrin, OpValues);
8774 break;
8775 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8776 visitVPStridedStore(VPIntrin, OpValues);
8777 break;
8778 case ISD::VP_FMULADD: {
8779 assert(OpValues.size() == 5 && "Unexpected number of operands");
8780 SDNodeFlags SDFlags;
8781 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8782 SDFlags.copyFMF(*FPMO);
8783 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8784 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8785 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8786 } else {
8787 SDValue Mul = DAG.getNode(
8788 ISD::VP_FMUL, DL, VTs,
8789 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8790 SDValue Add =
8791 DAG.getNode(ISD::VP_FADD, DL, VTs,
8792 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8793 setValue(&VPIntrin, Add);
8794 }
8795 break;
8796 }
8797 case ISD::VP_IS_FPCLASS: {
8798 const DataLayout DLayout = DAG.getDataLayout();
8799 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8800 auto Constant = OpValues[1]->getAsZExtVal();
8801 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8802 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8803 {OpValues[0], Check, OpValues[2], OpValues[3]});
8804 setValue(&VPIntrin, V);
8805 return;
8806 }
8807 case ISD::VP_INTTOPTR: {
8808 SDValue N = OpValues[0];
8809 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8810 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8811 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8812 OpValues[2]);
8813 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8814 OpValues[2]);
8815 setValue(&VPIntrin, N);
8816 break;
8817 }
8818 case ISD::VP_PTRTOINT: {
8819 SDValue N = OpValues[0];
8820 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8821 VPIntrin.getType());
8822 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
8823 VPIntrin.getOperand(0)->getType());
8824 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8825 OpValues[2]);
8826 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8827 OpValues[2]);
8828 setValue(&VPIntrin, N);
8829 break;
8830 }
8831 case ISD::VP_ABS:
8832 case ISD::VP_CTLZ:
8833 case ISD::VP_CTLZ_ZERO_UNDEF:
8834 case ISD::VP_CTTZ:
8835 case ISD::VP_CTTZ_ZERO_UNDEF:
8836 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
8837 case ISD::VP_CTTZ_ELTS: {
8838 SDValue Result =
8839 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
8840 setValue(&VPIntrin, Result);
8841 break;
8842 }
8843 }
8844}
8845
8846SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
8847 const BasicBlock *EHPadBB,
8848 MCSymbol *&BeginLabel) {
8849 MachineFunction &MF = DAG.getMachineFunction();
8850
8851 // Insert a label before the invoke call to mark the try range. This can be
8852 // used to detect deletion of the invoke via the MachineModuleInfo.
8853 BeginLabel = MF.getContext().createTempSymbol();
8854
8855 // For SjLj, keep track of which landing pads go with which invokes
8856 // so as to maintain the ordering of pads in the LSDA.
8857 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
8858 if (CallSiteIndex) {
8859 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
8860 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
8861
8862 // Now that the call site is handled, stop tracking it.
8863 FuncInfo.setCurrentCallSite(0);
8864 }
8865
8866 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
8867}
8868
8869SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
8870 const BasicBlock *EHPadBB,
8871 MCSymbol *BeginLabel) {
8872 assert(BeginLabel && "BeginLabel should've been set");
8873
8874 MachineFunction &MF = DAG.getMachineFunction();
8875
8876 // Insert a label at the end of the invoke call to mark the try range. This
8877 // can be used to detect deletion of the invoke via the MachineModuleInfo.
8878 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
8879 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
8880
8881 // Inform MachineModuleInfo of range.
8882 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
8883 // There is a platform (e.g. wasm) that uses funclet style IR but does not
8884 // actually use outlined funclets and their LSDA info style.
8885 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
8886 assert(II && "II should've been set");
8887 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
8888 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
8889 } else if (!isScopedEHPersonality(Pers)) {
8890 assert(EHPadBB);
8891 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
8892 }
8893
8894 return Chain;
8895}
8896
8897std::pair<SDValue, SDValue>
8899 const BasicBlock *EHPadBB) {
8900 MCSymbol *BeginLabel = nullptr;
8901
8902 if (EHPadBB) {
8903 // Both PendingLoads and PendingExports must be flushed here;
8904 // this call might not return.
8905 (void)getRoot();
8906 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
8907 CLI.setChain(getRoot());
8908 }
8909
8910 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8911 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
8912
8913 assert((CLI.IsTailCall || Result.second.getNode()) &&
8914 "Non-null chain expected with non-tail call!");
8915 assert((Result.second.getNode() || !Result.first.getNode()) &&
8916 "Null value expected with tail call!");
8917
8918 if (!Result.second.getNode()) {
8919 // As a special case, a null chain means that a tail call has been emitted
8920 // and the DAG root is already updated.
8921 HasTailCall = true;
8922
8923 // Since there's no actual continuation from this block, nothing can be
8924 // relying on us setting vregs for them.
8925 PendingExports.clear();
8926 } else {
8927 DAG.setRoot(Result.second);
8928 }
8929
8930 if (EHPadBB) {
8931 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
8932 BeginLabel));
8933 Result.second = getRoot();
8934 }
8935
8936 return Result;
8937}
8938
8940 bool isMustTailCall = CB.isMustTailCall();
8941
8942 // Avoid emitting tail calls in functions with the disable-tail-calls
8943 // attribute.
8944 const Function *Caller = CB.getParent()->getParent();
8945 if (Caller->getFnAttribute("disable-tail-calls").getValueAsString() ==
8946 "true" &&
8947 !isMustTailCall)
8948 return false;
8949
8950 // We can't tail call inside a function with a swifterror argument. Lowering
8951 // does not support this yet. It would have to move into the swifterror
8952 // register before the call.
8953 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
8954 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
8955 return false;
8956
8957 // Check if target-independent constraints permit a tail call here.
8958 // Target-dependent constraints are checked within TLI->LowerCallTo.
8959 return isInTailCallPosition(CB, DAG.getTarget());
8960}
8961
8963 bool isTailCall, bool isMustTailCall,
8964 const BasicBlock *EHPadBB,
8965 const TargetLowering::PtrAuthInfo *PAI) {
8966 auto &DL = DAG.getDataLayout();
8967 FunctionType *FTy = CB.getFunctionType();
8968 Type *RetTy = CB.getType();
8969
8971 Args.reserve(CB.arg_size());
8972
8973 const Value *SwiftErrorVal = nullptr;
8974 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8975
8976 if (isTailCall)
8977 isTailCall = canTailCall(CB);
8978
8979 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
8980 const Value *V = *I;
8981
8982 // Skip empty types
8983 if (V->getType()->isEmptyTy())
8984 continue;
8985
8986 SDValue ArgNode = getValue(V);
8987 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
8988 Entry.setAttributes(&CB, I - CB.arg_begin());
8989
8990 // Use swifterror virtual register as input to the call.
8991 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
8992 SwiftErrorVal = V;
8993 // We find the virtual register for the actual swifterror argument.
8994 // Instead of using the Value, we use the virtual register instead.
8995 Entry.Node =
8996 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
8997 EVT(TLI.getPointerTy(DL)));
8998 }
8999
9000 Args.push_back(Entry);
9001
9002 // If we have an explicit sret argument that is an Instruction, (i.e., it
9003 // might point to function-local memory), we can't meaningfully tail-call.
9004 if (Entry.IsSRet && isa<Instruction>(V))
9005 isTailCall = false;
9006 }
9007
9008 // If call site has a cfguardtarget operand bundle, create and add an
9009 // additional ArgListEntry.
9010 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9011 Value *V = Bundle->Inputs[0];
9013 Entry.IsCFGuardTarget = true;
9014 Args.push_back(Entry);
9015 }
9016
9017 // Disable tail calls if there is an swifterror argument. Targets have not
9018 // been updated to support tail calls.
9019 if (TLI.supportSwiftError() && SwiftErrorVal)
9020 isTailCall = false;
9021
9022 ConstantInt *CFIType = nullptr;
9023 if (CB.isIndirectCall()) {
9024 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9025 if (!TLI.supportKCFIBundles())
9027 "Target doesn't support calls with kcfi operand bundles.");
9028 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9029 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9030 }
9031 }
9032
9033 SDValue ConvControlToken;
9034 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9035 auto *Token = Bundle->Inputs[0].get();
9036 ConvControlToken = getValue(Token);
9037 }
9038
9041 .setChain(getRoot())
9042 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9043 .setTailCall(isTailCall)
9047 .setCFIType(CFIType)
9048 .setConvergenceControlToken(ConvControlToken);
9049
9050 // Set the pointer authentication info if we have it.
9051 if (PAI) {
9052 if (!TLI.supportPtrAuthBundles())
9054 "This target doesn't support calls with ptrauth operand bundles.");
9055 CLI.setPtrAuth(*PAI);
9056 }
9057
9058 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9059
9060 if (Result.first.getNode()) {
9061 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9062 setValue(&CB, Result.first);
9063 }
9064
9065 // The last element of CLI.InVals has the SDValue for swifterror return.
9066 // Here we copy it to a virtual register and update SwiftErrorMap for
9067 // book-keeping.
9068 if (SwiftErrorVal && TLI.supportSwiftError()) {
9069 // Get the last element of InVals.
9070 SDValue Src = CLI.InVals.back();
9071 Register VReg =
9072 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9073 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9074 DAG.setRoot(CopyNode);
9075 }
9076}
9077
9078static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9079 SelectionDAGBuilder &Builder) {
9080 // Check to see if this load can be trivially constant folded, e.g. if the
9081 // input is from a string literal.
9082 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9083 // Cast pointer to the type we really want to load.
9084 Type *LoadTy =
9085 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9086 if (LoadVT.isVector())
9087 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9088 if (const Constant *LoadCst =
9089 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9090 LoadTy, Builder.DAG.getDataLayout()))
9091 return Builder.getValue(LoadCst);
9092 }
9093
9094 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9095 // still constant memory, the input chain can be the entry node.
9096 SDValue Root;
9097 bool ConstantMemory = false;
9098
9099 // Do not serialize (non-volatile) loads of constant memory with anything.
9100 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9101 Root = Builder.DAG.getEntryNode();
9102 ConstantMemory = true;
9103 } else {
9104 // Do not serialize non-volatile loads against each other.
9105 Root = Builder.DAG.getRoot();
9106 }
9107
9108 SDValue Ptr = Builder.getValue(PtrVal);
9109 SDValue LoadVal =
9110 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9111 MachinePointerInfo(PtrVal), Align(1));
9112
9113 if (!ConstantMemory)
9114 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9115 return LoadVal;
9116}
9117
9118/// Record the value for an instruction that produces an integer result,
9119/// converting the type where necessary.
9120void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9121 SDValue Value,
9122 bool IsSigned) {
9123 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9124 I.getType(), true);
9125 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9126 setValue(&I, Value);
9127}
9128
9129/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9130/// true and lower it. Otherwise return false, and it will be lowered like a
9131/// normal call.
9132/// The caller already checked that \p I calls the appropriate LibFunc with a
9133/// correct prototype.
9134bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9135 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9136 const Value *Size = I.getArgOperand(2);
9137 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9138 if (CSize && CSize->getZExtValue() == 0) {
9139 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9140 I.getType(), true);
9141 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9142 return true;
9143 }
9144
9145 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9146 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9147 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9148 getValue(Size), &I);
9149 if (Res.first.getNode()) {
9150 processIntegerCallValue(I, Res.first, true);
9151 PendingLoads.push_back(Res.second);
9152 return true;
9153 }
9154
9155 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9156 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9157 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9158 return false;
9159
9160 // If the target has a fast compare for the given size, it will return a
9161 // preferred load type for that size. Require that the load VT is legal and
9162 // that the target supports unaligned loads of that type. Otherwise, return
9163 // INVALID.
9164 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9165 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9166 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9167 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9168 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9169 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9170 // TODO: Check alignment of src and dest ptrs.
9171 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9172 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9173 if (!TLI.isTypeLegal(LVT) ||
9174 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9175 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9177 }
9178
9179 return LVT;
9180 };
9181
9182 // This turns into unaligned loads. We only do this if the target natively
9183 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9184 // we'll only produce a small number of byte loads.
9185 MVT LoadVT;
9186 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9187 switch (NumBitsToCompare) {
9188 default:
9189 return false;
9190 case 16:
9191 LoadVT = MVT::i16;
9192 break;
9193 case 32:
9194 LoadVT = MVT::i32;
9195 break;
9196 case 64:
9197 case 128:
9198 case 256:
9199 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9200 break;
9201 }
9202
9203 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9204 return false;
9205
9206 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9207 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9208
9209 // Bitcast to a wide integer type if the loads are vectors.
9210 if (LoadVT.isVector()) {
9211 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9212 LoadL = DAG.getBitcast(CmpVT, LoadL);
9213 LoadR = DAG.getBitcast(CmpVT, LoadR);
9214 }
9215
9216 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9217 processIntegerCallValue(I, Cmp, false);
9218 return true;
9219}
9220
9221/// See if we can lower a memchr 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::visitMemChrCall(const CallInst &I) {
9227 const Value *Src = I.getArgOperand(0);
9228 const Value *Char = I.getArgOperand(1);
9229 const Value *Length = I.getArgOperand(2);
9230
9231 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9232 std::pair<SDValue, SDValue> Res =
9233 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9234 getValue(Src), getValue(Char), getValue(Length),
9235 MachinePointerInfo(Src));
9236 if (Res.first.getNode()) {
9237 setValue(&I, Res.first);
9238 PendingLoads.push_back(Res.second);
9239 return true;
9240 }
9241
9242 return false;
9243}
9244
9245/// See if we can lower a mempcpy call into an optimized form. If so, return
9246/// true and lower it. Otherwise return false, and it will be lowered like a
9247/// normal call.
9248/// The caller already checked that \p I calls the appropriate LibFunc with a
9249/// correct prototype.
9250bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9251 SDValue Dst = getValue(I.getArgOperand(0));
9252 SDValue Src = getValue(I.getArgOperand(1));
9253 SDValue Size = getValue(I.getArgOperand(2));
9254
9255 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9256 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9257 // DAG::getMemcpy needs Alignment to be defined.
9258 Align Alignment = std::min(DstAlign, SrcAlign);
9259
9260 SDLoc sdl = getCurSDLoc();
9261
9262 // In the mempcpy context we need to pass in a false value for isTailCall
9263 // because the return pointer needs to be adjusted by the size of
9264 // the copied memory.
9265 SDValue Root = getMemoryRoot();
9266 SDValue MC = DAG.getMemcpy(
9267 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9268 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9269 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9270 assert(MC.getNode() != nullptr &&
9271 "** memcpy should not be lowered as TailCall in mempcpy context **");
9272 DAG.setRoot(MC);
9273
9274 // Check if Size needs to be truncated or extended.
9275 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9276
9277 // Adjust return pointer to point just past the last dst byte.
9278 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9279 setValue(&I, DstPlusSize);
9280 return true;
9281}
9282
9283/// See if we can lower a strcpy call into an optimized form. If so, return
9284/// true and lower it, otherwise return false and it will be lowered like a
9285/// normal call.
9286/// The caller already checked that \p I calls the appropriate LibFunc with a
9287/// correct prototype.
9288bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9289 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9290
9291 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9292 std::pair<SDValue, SDValue> Res =
9294 getValue(Arg0), getValue(Arg1),
9295 MachinePointerInfo(Arg0),
9296 MachinePointerInfo(Arg1), isStpcpy);
9297 if (Res.first.getNode()) {
9298 setValue(&I, Res.first);
9299 DAG.setRoot(Res.second);
9300 return true;
9301 }
9302
9303 return false;
9304}
9305
9306/// See if we can lower a strcmp call into an optimized form. If so, return
9307/// true and lower it, otherwise return false and it will be lowered like a
9308/// normal call.
9309/// The caller already checked that \p I calls the appropriate LibFunc with a
9310/// correct prototype.
9311bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9312 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9313
9314 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9315 std::pair<SDValue, SDValue> Res =
9316 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
9317 getValue(Arg0), getValue(Arg1),
9318 MachinePointerInfo(Arg0),
9319 MachinePointerInfo(Arg1));
9320 if (Res.first.getNode()) {
9321 processIntegerCallValue(I, Res.first, true);
9322 PendingLoads.push_back(Res.second);
9323 return true;
9324 }
9325
9326 return false;
9327}
9328
9329/// See if we can lower a strlen call into an optimized form. If so, return
9330/// true and lower it, otherwise return false and it will be lowered like a
9331/// normal call.
9332/// The caller already checked that \p I calls the appropriate LibFunc with a
9333/// correct prototype.
9334bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9335 const Value *Arg0 = I.getArgOperand(0);
9336
9337 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9338 std::pair<SDValue, SDValue> Res =
9339 TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(),
9340 getValue(Arg0), MachinePointerInfo(Arg0));
9341 if (Res.first.getNode()) {
9342 processIntegerCallValue(I, Res.first, false);
9343 PendingLoads.push_back(Res.second);
9344 return true;
9345 }
9346
9347 return false;
9348}
9349
9350/// See if we can lower a strnlen call into an optimized form. If so, return
9351/// true and lower it, otherwise return false and it will be lowered like a
9352/// normal call.
9353/// The caller already checked that \p I calls the appropriate LibFunc with a
9354/// correct prototype.
9355bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9356 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9357
9358 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9359 std::pair<SDValue, SDValue> Res =
9360 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9361 getValue(Arg0), getValue(Arg1),
9362 MachinePointerInfo(Arg0));
9363 if (Res.first.getNode()) {
9364 processIntegerCallValue(I, Res.first, false);
9365 PendingLoads.push_back(Res.second);
9366 return true;
9367 }
9368
9369 return false;
9370}
9371
9372/// See if we can lower a unary floating-point operation into an SDNode with
9373/// the specified Opcode. If so, return true and lower it, otherwise return
9374/// false and it will be lowered like a normal call.
9375/// The caller already checked that \p I calls the appropriate LibFunc with a
9376/// correct prototype.
9377bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9378 unsigned Opcode) {
9379 // We already checked this call's prototype; verify it doesn't modify errno.
9380 if (!I.onlyReadsMemory())
9381 return false;
9382
9383 SDNodeFlags Flags;
9384 Flags.copyFMF(cast<FPMathOperator>(I));
9385
9386 SDValue Tmp = getValue(I.getArgOperand(0));
9387 setValue(&I,
9388 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9389 return true;
9390}
9391
9392/// See if we can lower a binary floating-point operation into an SDNode with
9393/// the specified Opcode. If so, return true and lower it. Otherwise return
9394/// false, and it will be lowered like a normal call.
9395/// The caller already checked that \p I calls the appropriate LibFunc with a
9396/// correct prototype.
9397bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9398 unsigned Opcode) {
9399 // We already checked this call's prototype; verify it doesn't modify errno.
9400 if (!I.onlyReadsMemory())
9401 return false;
9402
9403 SDNodeFlags Flags;
9404 Flags.copyFMF(cast<FPMathOperator>(I));
9405
9406 SDValue Tmp0 = getValue(I.getArgOperand(0));
9407 SDValue Tmp1 = getValue(I.getArgOperand(1));
9408 EVT VT = Tmp0.getValueType();
9409 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9410 return true;
9411}
9412
9413void SelectionDAGBuilder::visitCall(const CallInst &I) {
9414 // Handle inline assembly differently.
9415 if (I.isInlineAsm()) {
9416 visitInlineAsm(I);
9417 return;
9418 }
9419
9421
9422 if (Function *F = I.getCalledFunction()) {
9423 if (F->isDeclaration()) {
9424 // Is this an LLVM intrinsic?
9425 if (unsigned IID = F->getIntrinsicID()) {
9426 visitIntrinsicCall(I, IID);
9427 return;
9428 }
9429 }
9430
9431 // Check for well-known libc/libm calls. If the function is internal, it
9432 // can't be a library call. Don't do the check if marked as nobuiltin for
9433 // some reason or the call site requires strict floating point semantics.
9434 LibFunc Func;
9435 if (!I.isNoBuiltin() && !I.isStrictFP() && !F->hasLocalLinkage() &&
9436 F->hasName() && LibInfo->getLibFunc(*F, Func) &&
9437 LibInfo->hasOptimizedCodeGen(Func)) {
9438 switch (Func) {
9439 default: break;
9440 case LibFunc_bcmp:
9441 if (visitMemCmpBCmpCall(I))
9442 return;
9443 break;
9444 case LibFunc_copysign:
9445 case LibFunc_copysignf:
9446 case LibFunc_copysignl:
9447 // We already checked this call's prototype; verify it doesn't modify
9448 // errno.
9449 if (I.onlyReadsMemory()) {
9450 SDValue LHS = getValue(I.getArgOperand(0));
9451 SDValue RHS = getValue(I.getArgOperand(1));
9453 LHS.getValueType(), LHS, RHS));
9454 return;
9455 }
9456 break;
9457 case LibFunc_fabs:
9458 case LibFunc_fabsf:
9459 case LibFunc_fabsl:
9460 if (visitUnaryFloatCall(I, ISD::FABS))
9461 return;
9462 break;
9463 case LibFunc_fmin:
9464 case LibFunc_fminf:
9465 case LibFunc_fminl:
9466 if (visitBinaryFloatCall(I, ISD::FMINNUM))
9467 return;
9468 break;
9469 case LibFunc_fmax:
9470 case LibFunc_fmaxf:
9471 case LibFunc_fmaxl:
9472 if (visitBinaryFloatCall(I, ISD::FMAXNUM))
9473 return;
9474 break;
9475 case LibFunc_fminimum_num:
9476 case LibFunc_fminimum_numf:
9477 case LibFunc_fminimum_numl:
9478 if (visitBinaryFloatCall(I, ISD::FMINIMUMNUM))
9479 return;
9480 break;
9481 case LibFunc_fmaximum_num:
9482 case LibFunc_fmaximum_numf:
9483 case LibFunc_fmaximum_numl:
9484 if (visitBinaryFloatCall(I, ISD::FMAXIMUMNUM))
9485 return;
9486 break;
9487 case LibFunc_sin:
9488 case LibFunc_sinf:
9489 case LibFunc_sinl:
9490 if (visitUnaryFloatCall(I, ISD::FSIN))
9491 return;
9492 break;
9493 case LibFunc_cos:
9494 case LibFunc_cosf:
9495 case LibFunc_cosl:
9496 if (visitUnaryFloatCall(I, ISD::FCOS))
9497 return;
9498 break;
9499 case LibFunc_tan:
9500 case LibFunc_tanf:
9501 case LibFunc_tanl:
9502 if (visitUnaryFloatCall(I, ISD::FTAN))
9503 return;
9504 break;
9505 case LibFunc_asin:
9506 case LibFunc_asinf:
9507 case LibFunc_asinl:
9508 if (visitUnaryFloatCall(I, ISD::FASIN))
9509 return;
9510 break;
9511 case LibFunc_acos:
9512 case LibFunc_acosf:
9513 case LibFunc_acosl:
9514 if (visitUnaryFloatCall(I, ISD::FACOS))
9515 return;
9516 break;
9517 case LibFunc_atan:
9518 case LibFunc_atanf:
9519 case LibFunc_atanl:
9520 if (visitUnaryFloatCall(I, ISD::FATAN))
9521 return;
9522 break;
9523 case LibFunc_atan2:
9524 case LibFunc_atan2f:
9525 case LibFunc_atan2l:
9526 if (visitBinaryFloatCall(I, ISD::FATAN2))
9527 return;
9528 break;
9529 case LibFunc_sinh:
9530 case LibFunc_sinhf:
9531 case LibFunc_sinhl:
9532 if (visitUnaryFloatCall(I, ISD::FSINH))
9533 return;
9534 break;
9535 case LibFunc_cosh:
9536 case LibFunc_coshf:
9537 case LibFunc_coshl:
9538 if (visitUnaryFloatCall(I, ISD::FCOSH))
9539 return;
9540 break;
9541 case LibFunc_tanh:
9542 case LibFunc_tanhf:
9543 case LibFunc_tanhl:
9544 if (visitUnaryFloatCall(I, ISD::FTANH))
9545 return;
9546 break;
9547 case LibFunc_sqrt:
9548 case LibFunc_sqrtf:
9549 case LibFunc_sqrtl:
9550 case LibFunc_sqrt_finite:
9551 case LibFunc_sqrtf_finite:
9552 case LibFunc_sqrtl_finite:
9553 if (visitUnaryFloatCall(I, ISD::FSQRT))
9554 return;
9555 break;
9556 case LibFunc_floor:
9557 case LibFunc_floorf:
9558 case LibFunc_floorl:
9559 if (visitUnaryFloatCall(I, ISD::FFLOOR))
9560 return;
9561 break;
9562 case LibFunc_nearbyint:
9563 case LibFunc_nearbyintf:
9564 case LibFunc_nearbyintl:
9565 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
9566 return;
9567 break;
9568 case LibFunc_ceil:
9569 case LibFunc_ceilf:
9570 case LibFunc_ceill:
9571 if (visitUnaryFloatCall(I, ISD::FCEIL))
9572 return;
9573 break;
9574 case LibFunc_rint:
9575 case LibFunc_rintf:
9576 case LibFunc_rintl:
9577 if (visitUnaryFloatCall(I, ISD::FRINT))
9578 return;
9579 break;
9580 case LibFunc_round:
9581 case LibFunc_roundf:
9582 case LibFunc_roundl:
9583 if (visitUnaryFloatCall(I, ISD::FROUND))
9584 return;
9585 break;
9586 case LibFunc_trunc:
9587 case LibFunc_truncf:
9588 case LibFunc_truncl:
9589 if (visitUnaryFloatCall(I, ISD::FTRUNC))
9590 return;
9591 break;
9592 case LibFunc_log2:
9593 case LibFunc_log2f:
9594 case LibFunc_log2l:
9595 if (visitUnaryFloatCall(I, ISD::FLOG2))
9596 return;
9597 break;
9598 case LibFunc_exp2:
9599 case LibFunc_exp2f:
9600 case LibFunc_exp2l:
9601 if (visitUnaryFloatCall(I, ISD::FEXP2))
9602 return;
9603 break;
9604 case LibFunc_exp10:
9605 case LibFunc_exp10f:
9606 case LibFunc_exp10l:
9607 if (visitUnaryFloatCall(I, ISD::FEXP10))
9608 return;
9609 break;
9610 case LibFunc_ldexp:
9611 case LibFunc_ldexpf:
9612 case LibFunc_ldexpl:
9613 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9614 return;
9615 break;
9616 case LibFunc_memcmp:
9617 if (visitMemCmpBCmpCall(I))
9618 return;
9619 break;
9620 case LibFunc_mempcpy:
9621 if (visitMemPCpyCall(I))
9622 return;
9623 break;
9624 case LibFunc_memchr:
9625 if (visitMemChrCall(I))
9626 return;
9627 break;
9628 case LibFunc_strcpy:
9629 if (visitStrCpyCall(I, false))
9630 return;
9631 break;
9632 case LibFunc_stpcpy:
9633 if (visitStrCpyCall(I, true))
9634 return;
9635 break;
9636 case LibFunc_strcmp:
9637 if (visitStrCmpCall(I))
9638 return;
9639 break;
9640 case LibFunc_strlen:
9641 if (visitStrLenCall(I))
9642 return;
9643 break;
9644 case LibFunc_strnlen:
9645 if (visitStrNLenCall(I))
9646 return;
9647 break;
9648 }
9649 }
9650 }
9651
9652 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9653 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9654 return;
9655 }
9656
9657 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9658 // have to do anything here to lower funclet bundles.
9659 // CFGuardTarget bundles are lowered in LowerCallTo.
9661 I, "calls",
9666
9667 SDValue Callee = getValue(I.getCalledOperand());
9668
9669 if (I.hasDeoptState())
9670 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9671 else
9672 // Check if we can potentially perform a tail call. More detailed checking
9673 // is be done within LowerCallTo, after more information about the call is
9674 // known.
9675 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9676}
9677
9679 const CallBase &CB, const BasicBlock *EHPadBB) {
9680 auto PAB = CB.getOperandBundle("ptrauth");
9681 const Value *CalleeV = CB.getCalledOperand();
9682
9683 // Gather the call ptrauth data from the operand bundle:
9684 // [ i32 <key>, i64 <discriminator> ]
9685 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9686 const Value *Discriminator = PAB->Inputs[1];
9687
9688 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9689 assert(Discriminator->getType()->isIntegerTy(64) &&
9690 "Invalid ptrauth discriminator");
9691
9692 // Look through ptrauth constants to find the raw callee.
9693 // Do a direct unauthenticated call if we found it and everything matches.
9694 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9695 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9696 DAG.getDataLayout()))
9697 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9698 CB.isMustTailCall(), EHPadBB);
9699
9700 // Functions should never be ptrauth-called directly.
9701 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9702
9703 // Otherwise, do an authenticated indirect call.
9704 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9705 getValue(Discriminator)};
9706
9707 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9708 EHPadBB, &PAI);
9709}
9710
9711namespace {
9712
9713/// AsmOperandInfo - This contains information for each constraint that we are
9714/// lowering.
9715class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9716public:
9717 /// CallOperand - If this is the result output operand or a clobber
9718 /// this is null, otherwise it is the incoming operand to the CallInst.
9719 /// This gets modified as the asm is processed.
9720 SDValue CallOperand;
9721
9722 /// AssignedRegs - If this is a register or register class operand, this
9723 /// contains the set of register corresponding to the operand.
9724 RegsForValue AssignedRegs;
9725
9726 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9727 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9728 }
9729
9730 /// Whether or not this operand accesses memory
9731 bool hasMemory(const TargetLowering &TLI) const {
9732 // Indirect operand accesses access memory.
9733 if (isIndirect)
9734 return true;
9735
9736 for (const auto &Code : Codes)
9738 return true;
9739
9740 return false;
9741 }
9742};
9743
9744
9745} // end anonymous namespace
9746
9747/// Make sure that the output operand \p OpInfo and its corresponding input
9748/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9749/// out).
9750static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9751 SDISelAsmOperandInfo &MatchingOpInfo,
9752 SelectionDAG &DAG) {
9753 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9754 return;
9755
9757 const auto &TLI = DAG.getTargetLoweringInfo();
9758
9759 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9760 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9761 OpInfo.ConstraintVT);
9762 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9763 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9764 MatchingOpInfo.ConstraintVT);
9765 const bool OutOpIsIntOrFP =
9766 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9767 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9768 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9769 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9770 // FIXME: error out in a more elegant fashion
9771 report_fatal_error("Unsupported asm: input constraint"
9772 " with a matching output constraint of"
9773 " incompatible type!");
9774 }
9775 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9776}
9777
9778/// Get a direct memory input to behave well as an indirect operand.
9779/// This may introduce stores, hence the need for a \p Chain.
9780/// \return The (possibly updated) chain.
9781static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9782 SDISelAsmOperandInfo &OpInfo,
9783 SelectionDAG &DAG) {
9784 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9785
9786 // If we don't have an indirect input, put it in the constpool if we can,
9787 // otherwise spill it to a stack slot.
9788 // TODO: This isn't quite right. We need to handle these according to
9789 // the addressing mode that the constraint wants. Also, this may take
9790 // an additional register for the computation and we don't want that
9791 // either.
9792
9793 // If the operand is a float, integer, or vector constant, spill to a
9794 // constant pool entry to get its address.
9795 const Value *OpVal = OpInfo.CallOperandVal;
9796 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9798 OpInfo.CallOperand = DAG.getConstantPool(
9799 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9800 return Chain;
9801 }
9802
9803 // Otherwise, create a stack slot and emit a store to it before the asm.
9804 Type *Ty = OpVal->getType();
9805 auto &DL = DAG.getDataLayout();
9806 TypeSize TySize = DL.getTypeAllocSize(Ty);
9809 int StackID = 0;
9810 if (TySize.isScalable())
9811 StackID = TFI->getStackIDForScalableVectors();
9812 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9813 DL.getPrefTypeAlign(Ty), false,
9814 nullptr, StackID);
9815 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9816 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9818 TLI.getMemValueType(DL, Ty));
9819 OpInfo.CallOperand = StackSlot;
9820
9821 return Chain;
9822}
9823
9824/// GetRegistersForValue - Assign registers (virtual or physical) for the
9825/// specified operand. We prefer to assign virtual registers, to allow the
9826/// register allocator to handle the assignment process. However, if the asm
9827/// uses features that we can't model on machineinstrs, we have SDISel do the
9828/// allocation. This produces generally horrible, but correct, code.
9829///
9830/// OpInfo describes the operand
9831/// RefOpInfo describes the matching operand if any, the operand otherwise
9832static std::optional<unsigned>
9834 SDISelAsmOperandInfo &OpInfo,
9835 SDISelAsmOperandInfo &RefOpInfo) {
9836 LLVMContext &Context = *DAG.getContext();
9837 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9838
9842
9843 // No work to do for memory/address operands.
9844 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9845 OpInfo.ConstraintType == TargetLowering::C_Address)
9846 return std::nullopt;
9847
9848 // If this is a constraint for a single physreg, or a constraint for a
9849 // register class, find it.
9850 unsigned AssignedReg;
9851 const TargetRegisterClass *RC;
9852 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
9853 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
9854 // RC is unset only on failure. Return immediately.
9855 if (!RC)
9856 return std::nullopt;
9857
9858 // Get the actual register value type. This is important, because the user
9859 // may have asked for (e.g.) the AX register in i32 type. We need to
9860 // remember that AX is actually i16 to get the right extension.
9861 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
9862
9863 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
9864 // If this is an FP operand in an integer register (or visa versa), or more
9865 // generally if the operand value disagrees with the register class we plan
9866 // to stick it in, fix the operand type.
9867 //
9868 // If this is an input value, the bitcast to the new type is done now.
9869 // Bitcast for output value is done at the end of visitInlineAsm().
9870 if ((OpInfo.Type == InlineAsm::isOutput ||
9871 OpInfo.Type == InlineAsm::isInput) &&
9872 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
9873 // Try to convert to the first EVT that the reg class contains. If the
9874 // types are identical size, use a bitcast to convert (e.g. two differing
9875 // vector types). Note: output bitcast is done at the end of
9876 // visitInlineAsm().
9877 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
9878 // Exclude indirect inputs while they are unsupported because the code
9879 // to perform the load is missing and thus OpInfo.CallOperand still
9880 // refers to the input address rather than the pointed-to value.
9881 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
9882 OpInfo.CallOperand =
9883 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
9884 OpInfo.ConstraintVT = RegVT;
9885 // If the operand is an FP value and we want it in integer registers,
9886 // use the corresponding integer type. This turns an f64 value into
9887 // i64, which can be passed with two i32 values on a 32-bit machine.
9888 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
9889 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
9890 if (OpInfo.Type == InlineAsm::isInput)
9891 OpInfo.CallOperand =
9892 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
9893 OpInfo.ConstraintVT = VT;
9894 }
9895 }
9896 }
9897
9898 // No need to allocate a matching input constraint since the constraint it's
9899 // matching to has already been allocated.
9900 if (OpInfo.isMatchingInputConstraint())
9901 return std::nullopt;
9902
9903 EVT ValueVT = OpInfo.ConstraintVT;
9904 if (OpInfo.ConstraintVT == MVT::Other)
9905 ValueVT = RegVT;
9906
9907 // Initialize NumRegs.
9908 unsigned NumRegs = 1;
9909 if (OpInfo.ConstraintVT != MVT::Other)
9910 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
9911
9912 // If this is a constraint for a specific physical register, like {r17},
9913 // assign it now.
9914
9915 // If this associated to a specific register, initialize iterator to correct
9916 // place. If virtual, make sure we have enough registers
9917
9918 // Initialize iterator if necessary
9921
9922 // Do not check for single registers.
9923 if (AssignedReg) {
9924 I = std::find(I, RC->end(), AssignedReg);
9925 if (I == RC->end()) {
9926 // RC does not contain the selected register, which indicates a
9927 // mismatch between the register and the required type/bitwidth.
9928 return {AssignedReg};
9929 }
9930 }
9931
9932 for (; NumRegs; --NumRegs, ++I) {
9933 assert(I != RC->end() && "Ran out of registers to allocate!");
9934 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
9935 Regs.push_back(R);
9936 }
9937
9938 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
9939 return std::nullopt;
9940}
9941
9942static unsigned
9944 const std::vector<SDValue> &AsmNodeOperands) {
9945 // Scan until we find the definition we already emitted of this operand.
9946 unsigned CurOp = InlineAsm::Op_FirstOperand;
9947 for (; OperandNo; --OperandNo) {
9948 // Advance to the next operand.
9949 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
9950 const InlineAsm::Flag F(OpFlag);
9951 assert(
9952 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
9953 "Skipped past definitions?");
9954 CurOp += F.getNumOperandRegisters() + 1;
9955 }
9956 return CurOp;
9957}
9958
9959namespace {
9960
9961class ExtraFlags {
9962 unsigned Flags = 0;
9963
9964public:
9965 explicit ExtraFlags(const CallBase &Call) {
9966 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
9967 if (IA->hasSideEffects())
9969 if (IA->isAlignStack())
9971 if (Call.isConvergent())
9973 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
9974 }
9975
9976 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
9977 // Ideally, we would only check against memory constraints. However, the
9978 // meaning of an Other constraint can be target-specific and we can't easily
9979 // reason about it. Therefore, be conservative and set MayLoad/MayStore
9980 // for Other constraints as well.
9983 if (OpInfo.Type == InlineAsm::isInput)
9985 else if (OpInfo.Type == InlineAsm::isOutput)
9987 else if (OpInfo.Type == InlineAsm::isClobber)
9989 }
9990 }
9991
9992 unsigned get() const { return Flags; }
9993};
9994
9995} // end anonymous namespace
9996
9997static bool isFunction(SDValue Op) {
9998 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
9999 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10000 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10001
10002 // In normal "call dllimport func" instruction (non-inlineasm) it force
10003 // indirect access by specifing call opcode. And usually specially print
10004 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10005 // not do in this way now. (In fact, this is similar with "Data Access"
10006 // action). So here we ignore dllimport function.
10007 if (Fn && !Fn->hasDLLImportStorageClass())
10008 return true;
10009 }
10010 }
10011 return false;
10012}
10013
10014/// visitInlineAsm - Handle a call to an InlineAsm object.
10015void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10016 const BasicBlock *EHPadBB) {
10017 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10018
10019 /// ConstraintOperands - Information about all of the constraints.
10020 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10021
10022 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10024 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10025
10026 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
10027 // AsmDialect, MayLoad, MayStore).
10028 bool HasSideEffect = IA->hasSideEffects();
10029 ExtraFlags ExtraInfo(Call);
10030
10031 for (auto &T : TargetConstraints) {
10032 ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10033 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
10034
10035 if (OpInfo.CallOperandVal)
10036 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
10037
10038 if (!HasSideEffect)
10039 HasSideEffect = OpInfo.hasMemory(TLI);
10040
10041 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10042 // FIXME: Could we compute this on OpInfo rather than T?
10043
10044 // Compute the constraint code and ConstraintType to use.
10046
10047 if (T.ConstraintType == TargetLowering::C_Immediate &&
10048 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
10049 // We've delayed emitting a diagnostic like the "n" constraint because
10050 // inlining could cause an integer showing up.
10051 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
10052 "' expects an integer constant "
10053 "expression");
10054
10055 ExtraInfo.update(T);
10056 }
10057
10058 // We won't need to flush pending loads if this asm doesn't touch
10059 // memory and is nonvolatile.
10060 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
10061
10062 bool EmitEHLabels = isa<InvokeInst>(Call);
10063 if (EmitEHLabels) {
10064 assert(EHPadBB && "InvokeInst must have an EHPadBB");
10065 }
10066 bool IsCallBr = isa<CallBrInst>(Call);
10067
10068 if (IsCallBr || EmitEHLabels) {
10069 // If this is a callbr or invoke we need to flush pending exports since
10070 // inlineasm_br and invoke are terminators.
10071 // We need to do this before nodes are glued to the inlineasm_br node.
10072 Chain = getControlRoot();
10073 }
10074
10075 MCSymbol *BeginLabel = nullptr;
10076 if (EmitEHLabels) {
10077 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
10078 }
10079
10080 int OpNo = -1;
10081 SmallVector<StringRef> AsmStrs;
10082 IA->collectAsmStrs(AsmStrs);
10083
10084 // Second pass over the constraints: compute which constraint option to use.
10085 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10086 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10087 OpNo++;
10088
10089 // If this is an output operand with a matching input operand, look up the
10090 // matching input. If their types mismatch, e.g. one is an integer, the
10091 // other is floating point, or their sizes are different, flag it as an
10092 // error.
10093 if (OpInfo.hasMatchingInput()) {
10094 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
10095 patchMatchingInput(OpInfo, Input, DAG);
10096 }
10097
10098 // Compute the constraint code and ConstraintType to use.
10099 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10100
10101 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10102 OpInfo.Type == InlineAsm::isClobber) ||
10103 OpInfo.ConstraintType == TargetLowering::C_Address)
10104 continue;
10105
10106 // In Linux PIC model, there are 4 cases about value/label addressing:
10107 //
10108 // 1: Function call or Label jmp inside the module.
10109 // 2: Data access (such as global variable, static variable) inside module.
10110 // 3: Function call or Label jmp outside the module.
10111 // 4: Data access (such as global variable) outside the module.
10112 //
10113 // Due to current llvm inline asm architecture designed to not "recognize"
10114 // the asm code, there are quite troubles for us to treat mem addressing
10115 // differently for same value/adress used in different instuctions.
10116 // For example, in pic model, call a func may in plt way or direclty
10117 // pc-related, but lea/mov a function adress may use got.
10118 //
10119 // Here we try to "recognize" function call for the case 1 and case 3 in
10120 // inline asm. And try to adjust the constraint for them.
10121 //
10122 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10123 // label, so here we don't handle jmp function label now, but we need to
10124 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10125 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10126 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10127 TM.getCodeModel() != CodeModel::Large) {
10128 OpInfo.isIndirect = false;
10129 OpInfo.ConstraintType = TargetLowering::C_Address;
10130 }
10131
10132 // If this is a memory input, and if the operand is not indirect, do what we
10133 // need to provide an address for the memory input.
10134 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10135 !OpInfo.isIndirect) {
10136 assert((OpInfo.isMultipleAlternative ||
10137 (OpInfo.Type == InlineAsm::isInput)) &&
10138 "Can only indirectify direct input operands!");
10139
10140 // Memory operands really want the address of the value.
10141 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
10142
10143 // There is no longer a Value* corresponding to this operand.
10144 OpInfo.CallOperandVal = nullptr;
10145
10146 // It is now an indirect operand.
10147 OpInfo.isIndirect = true;
10148 }
10149
10150 }
10151
10152 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10153 std::vector<SDValue> AsmNodeOperands;
10154 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10155 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10156 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10157
10158 // If we have a !srcloc metadata node associated with it, we want to attach
10159 // this to the ultimately generated inline asm machineinstr. To do this, we
10160 // pass in the third operand as this (potentially null) inline asm MDNode.
10161 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10162 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10163
10164 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10165 // bits as operand 3.
10166 AsmNodeOperands.push_back(DAG.getTargetConstant(
10167 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10168
10169 // Third pass: Loop over operands to prepare DAG-level operands.. As part of
10170 // this, assign virtual and physical registers for inputs and otput.
10171 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10172 // Assign Registers.
10173 SDISelAsmOperandInfo &RefOpInfo =
10174 OpInfo.isMatchingInputConstraint()
10175 ? ConstraintOperands[OpInfo.getMatchedOperand()]
10176 : OpInfo;
10177 const auto RegError =
10178 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
10179 if (RegError) {
10180 const MachineFunction &MF = DAG.getMachineFunction();
10181 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10182 const char *RegName = TRI.getName(*RegError);
10183 emitInlineAsmError(Call, "register '" + Twine(RegName) +
10184 "' allocated for constraint '" +
10185 Twine(OpInfo.ConstraintCode) +
10186 "' does not match required type");
10187 return;
10188 }
10189
10190 auto DetectWriteToReservedRegister = [&]() {
10191 const MachineFunction &MF = DAG.getMachineFunction();
10192 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10193 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10194 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10195 const char *RegName = TRI.getName(Reg);
10196 emitInlineAsmError(Call, "write to reserved register '" +
10197 Twine(RegName) + "'");
10198 return true;
10199 }
10200 }
10201 return false;
10202 };
10203 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10204 (OpInfo.Type == InlineAsm::isInput &&
10205 !OpInfo.isMatchingInputConstraint())) &&
10206 "Only address as input operand is allowed.");
10207
10208 switch (OpInfo.Type) {
10210 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10211 const InlineAsm::ConstraintCode ConstraintID =
10212 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10214 "Failed to convert memory constraint code to constraint id.");
10215
10216 // Add information to the INLINEASM node to know about this output.
10217 InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1);
10218 OpFlags.setMemConstraint(ConstraintID);
10219 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
10220 MVT::i32));
10221 AsmNodeOperands.push_back(OpInfo.CallOperand);
10222 } else {
10223 // Otherwise, this outputs to a register (directly for C_Register /
10224 // C_RegisterClass, and a target-defined fashion for
10225 // C_Immediate/C_Other). Find a register that we can use.
10226 if (OpInfo.AssignedRegs.Regs.empty()) {
10227 emitInlineAsmError(
10228 Call, "couldn't allocate output register for constraint '" +
10229 Twine(OpInfo.ConstraintCode) + "'");
10230 return;
10231 }
10232
10233 if (DetectWriteToReservedRegister())
10234 return;
10235
10236 // Add information to the INLINEASM node to know that this register is
10237 // set.
10238 OpInfo.AssignedRegs.AddInlineAsmOperands(
10239 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10241 false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
10242 }
10243 break;
10244
10245 case InlineAsm::isInput:
10246 case InlineAsm::isLabel: {
10247 SDValue InOperandVal = OpInfo.CallOperand;
10248
10249 if (OpInfo.isMatchingInputConstraint()) {
10250 // If this is required to match an output register we have already set,
10251 // just use its register.
10252 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10253 AsmNodeOperands);
10254 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
10255 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10256 if (OpInfo.isIndirect) {
10257 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10258 emitInlineAsmError(Call, "inline asm not supported yet: "
10259 "don't know how to handle tied "
10260 "indirect register inputs");
10261 return;
10262 }
10263
10265 MachineFunction &MF = DAG.getMachineFunction();
10266 MachineRegisterInfo &MRI = MF.getRegInfo();
10267 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10268 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
10269 Register TiedReg = R->getReg();
10270 MVT RegVT = R->getSimpleValueType(0);
10271 const TargetRegisterClass *RC =
10272 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10273 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10274 : TRI.getMinimalPhysRegClass(TiedReg);
10275 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
10276 Regs.push_back(MRI.createVirtualRegister(RC));
10277
10278 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10279
10280 SDLoc dl = getCurSDLoc();
10281 // Use the produced MatchedRegs object to
10282 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
10283 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
10284 OpInfo.getMatchedOperand(), dl, DAG,
10285 AsmNodeOperands);
10286 break;
10287 }
10288
10289 assert(Flag.isMemKind() && "Unknown matching constraint!");
10290 assert(Flag.getNumOperandRegisters() == 1 &&
10291 "Unexpected number of operands");
10292 // Add information to the INLINEASM node to know about this input.
10293 // See InlineAsm.h isUseOperandTiedToDef.
10294 Flag.clearMemConstraint();
10295 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10296 AsmNodeOperands.push_back(DAG.getTargetConstant(
10297 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10298 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
10299 break;
10300 }
10301
10302 // Treat indirect 'X' constraint as memory.
10303 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10304 OpInfo.isIndirect)
10305 OpInfo.ConstraintType = TargetLowering::C_Memory;
10306
10307 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10308 OpInfo.ConstraintType == TargetLowering::C_Other) {
10309 std::vector<SDValue> Ops;
10310 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10311 Ops, DAG);
10312 if (Ops.empty()) {
10313 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10314 if (isa<ConstantSDNode>(InOperandVal)) {
10315 emitInlineAsmError(Call, "value out of range for constraint '" +
10316 Twine(OpInfo.ConstraintCode) + "'");
10317 return;
10318 }
10319
10320 emitInlineAsmError(Call,
10321 "invalid operand for inline asm constraint '" +
10322 Twine(OpInfo.ConstraintCode) + "'");
10323 return;
10324 }
10325
10326 // Add information to the INLINEASM node to know about this input.
10327 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10328 AsmNodeOperands.push_back(DAG.getTargetConstant(
10329 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10330 llvm::append_range(AsmNodeOperands, Ops);
10331 break;
10332 }
10333
10334 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10335 assert((OpInfo.isIndirect ||
10336 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10337 "Operand must be indirect to be a mem!");
10338 assert(InOperandVal.getValueType() ==
10339 TLI.getPointerTy(DAG.getDataLayout()) &&
10340 "Memory operands expect pointer values");
10341
10342 const InlineAsm::ConstraintCode ConstraintID =
10343 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10345 "Failed to convert memory constraint code to constraint id.");
10346
10347 // Add information to the INLINEASM node to know about this input.
10348 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10349 ResOpType.setMemConstraint(ConstraintID);
10350 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10351 getCurSDLoc(),
10352 MVT::i32));
10353 AsmNodeOperands.push_back(InOperandVal);
10354 break;
10355 }
10356
10357 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10358 const InlineAsm::ConstraintCode ConstraintID =
10359 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10361 "Failed to convert memory constraint code to constraint id.");
10362
10363 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10364
10365 SDValue AsmOp = InOperandVal;
10366 if (isFunction(InOperandVal)) {
10367 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10368 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10369 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10370 InOperandVal.getValueType(),
10371 GA->getOffset());
10372 }
10373
10374 // Add information to the INLINEASM node to know about this input.
10375 ResOpType.setMemConstraint(ConstraintID);
10376
10377 AsmNodeOperands.push_back(
10378 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10379
10380 AsmNodeOperands.push_back(AsmOp);
10381 break;
10382 }
10383
10384 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10385 OpInfo.ConstraintType != TargetLowering::C_Register) {
10386 emitInlineAsmError(Call, "unknown asm constraint '" +
10387 Twine(OpInfo.ConstraintCode) + "'");
10388 return;
10389 }
10390
10391 // TODO: Support this.
10392 if (OpInfo.isIndirect) {
10393 emitInlineAsmError(
10394 Call, "Don't know how to handle indirect register inputs yet "
10395 "for constraint '" +
10396 Twine(OpInfo.ConstraintCode) + "'");
10397 return;
10398 }
10399
10400 // Copy the input into the appropriate registers.
10401 if (OpInfo.AssignedRegs.Regs.empty()) {
10402 emitInlineAsmError(Call,
10403 "couldn't allocate input reg for constraint '" +
10404 Twine(OpInfo.ConstraintCode) + "'");
10405 return;
10406 }
10407
10408 if (DetectWriteToReservedRegister())
10409 return;
10410
10411 SDLoc dl = getCurSDLoc();
10412
10413 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10414 &Call);
10415
10416 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10417 0, dl, DAG, AsmNodeOperands);
10418 break;
10419 }
10421 // Add the clobbered value to the operand list, so that the register
10422 // allocator is aware that the physreg got clobbered.
10423 if (!OpInfo.AssignedRegs.Regs.empty())
10425 false, 0, getCurSDLoc(), DAG,
10426 AsmNodeOperands);
10427 break;
10428 }
10429 }
10430
10431 // Finish up input operands. Set the input chain and add the flag last.
10432 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10433 if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10434
10435 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10436 Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10437 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10438 Glue = Chain.getValue(1);
10439
10440 // Do additional work to generate outputs.
10441
10442 SmallVector<EVT, 1> ResultVTs;
10443 SmallVector<SDValue, 1> ResultValues;
10444 SmallVector<SDValue, 8> OutChains;
10445
10446 llvm::Type *CallResultType = Call.getType();
10447 ArrayRef<Type *> ResultTypes;
10448 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10449 ResultTypes = StructResult->elements();
10450 else if (!CallResultType->isVoidTy())
10451 ResultTypes = ArrayRef(CallResultType);
10452
10453 auto CurResultType = ResultTypes.begin();
10454 auto handleRegAssign = [&](SDValue V) {
10455 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10456 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10457 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10458 ++CurResultType;
10459 // If the type of the inline asm call site return value is different but has
10460 // same size as the type of the asm output bitcast it. One example of this
10461 // is for vectors with different width / number of elements. This can
10462 // happen for register classes that can contain multiple different value
10463 // types. The preg or vreg allocated may not have the same VT as was
10464 // expected.
10465 //
10466 // This can also happen for a return value that disagrees with the register
10467 // class it is put in, eg. a double in a general-purpose register on a
10468 // 32-bit machine.
10469 if (ResultVT != V.getValueType() &&
10470 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10471 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10472 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10473 V.getValueType().isInteger()) {
10474 // If a result value was tied to an input value, the computed result
10475 // may have a wider width than the expected result. Extract the
10476 // relevant portion.
10477 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10478 }
10479 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10480 ResultVTs.push_back(ResultVT);
10481 ResultValues.push_back(V);
10482 };
10483
10484 // Deal with output operands.
10485 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10486 if (OpInfo.Type == InlineAsm::isOutput) {
10487 SDValue Val;
10488 // Skip trivial output operands.
10489 if (OpInfo.AssignedRegs.Regs.empty())
10490 continue;
10491
10492 switch (OpInfo.ConstraintType) {
10495 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10496 Chain, &Glue, &Call);
10497 break;
10500 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10501 OpInfo, DAG);
10502 break;
10504 break; // Already handled.
10506 break; // Silence warning.
10508 assert(false && "Unexpected unknown constraint");
10509 }
10510
10511 // Indirect output manifest as stores. Record output chains.
10512 if (OpInfo.isIndirect) {
10513 const Value *Ptr = OpInfo.CallOperandVal;
10514 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10515 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10516 MachinePointerInfo(Ptr));
10517 OutChains.push_back(Store);
10518 } else {
10519 // generate CopyFromRegs to associated registers.
10520 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10521 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10522 for (const SDValue &V : Val->op_values())
10523 handleRegAssign(V);
10524 } else
10525 handleRegAssign(Val);
10526 }
10527 }
10528 }
10529
10530 // Set results.
10531 if (!ResultValues.empty()) {
10532 assert(CurResultType == ResultTypes.end() &&
10533 "Mismatch in number of ResultTypes");
10534 assert(ResultValues.size() == ResultTypes.size() &&
10535 "Mismatch in number of output operands in asm result");
10536
10538 DAG.getVTList(ResultVTs), ResultValues);
10539 setValue(&Call, V);
10540 }
10541
10542 // Collect store chains.
10543 if (!OutChains.empty())
10544 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10545
10546 if (EmitEHLabels) {
10547 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10548 }
10549
10550 // Only Update Root if inline assembly has a memory effect.
10551 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10552 EmitEHLabels)
10553 DAG.setRoot(Chain);
10554}
10555
10556void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10557 const Twine &Message) {
10558 LLVMContext &Ctx = *DAG.getContext();
10559 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10560
10561 // Make sure we leave the DAG in a valid state
10562 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10563 SmallVector<EVT, 1> ValueVTs;
10564 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10565
10566 if (ValueVTs.empty())
10567 return;
10568
10570 for (const EVT &VT : ValueVTs)
10571 Ops.push_back(DAG.getUNDEF(VT));
10572
10573 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10574}
10575
10576void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10577 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10578 MVT::Other, getRoot(),
10579 getValue(I.getArgOperand(0)),
10580 DAG.getSrcValue(I.getArgOperand(0))));
10581}
10582
10583void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10584 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10585 const DataLayout &DL = DAG.getDataLayout();
10586 SDValue V = DAG.getVAArg(
10587 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10588 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10589 DL.getABITypeAlign(I.getType()).value());
10590 DAG.setRoot(V.getValue(1));
10591
10592 if (I.getType()->isPointerTy())
10593 V = DAG.getPtrExtOrTrunc(
10594 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10595 setValue(&I, V);
10596}
10597
10598void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10599 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10600 MVT::Other, getRoot(),
10601 getValue(I.getArgOperand(0)),
10602 DAG.getSrcValue(I.getArgOperand(0))));
10603}
10604
10605void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10606 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10607 MVT::Other, getRoot(),
10608 getValue(I.getArgOperand(0)),
10609 getValue(I.getArgOperand(1)),
10610 DAG.getSrcValue(I.getArgOperand(0)),
10611 DAG.getSrcValue(I.getArgOperand(1))));
10612}
10613
10615 const Instruction &I,
10616 SDValue Op) {
10617 std::optional<ConstantRange> CR = getRange(I);
10618
10619 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10620 return Op;
10621
10622 APInt Lo = CR->getUnsignedMin();
10623 if (!Lo.isMinValue())
10624 return Op;
10625
10626 APInt Hi = CR->getUnsignedMax();
10627 unsigned Bits = std::max(Hi.getActiveBits(),
10628 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10629
10630 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10631
10632 SDLoc SL = getCurSDLoc();
10633
10634 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10635 DAG.getValueType(SmallVT));
10636 unsigned NumVals = Op.getNode()->getNumValues();
10637 if (NumVals == 1)
10638 return ZExt;
10639
10641
10642 Ops.push_back(ZExt);
10643 for (unsigned I = 1; I != NumVals; ++I)
10644 Ops.push_back(Op.getValue(I));
10645
10646 return DAG.getMergeValues(Ops, SL);
10647}
10648
10649/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10650/// the call being lowered.
10651///
10652/// This is a helper for lowering intrinsics that follow a target calling
10653/// convention or require stack pointer adjustment. Only a subset of the
10654/// intrinsic's operands need to participate in the calling convention.
10657 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10658 AttributeSet RetAttrs, bool IsPatchPoint) {
10660 Args.reserve(NumArgs);
10661
10662 // Populate the argument list.
10663 // Attributes for args start at offset 1, after the return attribute.
10664 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10665 ArgI != ArgE; ++ArgI) {
10666 const Value *V = Call->getOperand(ArgI);
10667
10668 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10669
10670 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10671 Entry.setAttributes(Call, ArgI);
10672 Args.push_back(Entry);
10673 }
10674
10676 .setChain(getRoot())
10677 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10678 RetAttrs)
10679 .setDiscardResult(Call->use_empty())
10680 .setIsPatchPoint(IsPatchPoint)
10682 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10683}
10684
10685/// Add a stack map intrinsic call's live variable operands to a stackmap
10686/// or patchpoint target node's operand list.
10687///
10688/// Constants are converted to TargetConstants purely as an optimization to
10689/// avoid constant materialization and register allocation.
10690///
10691/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10692/// generate addess computation nodes, and so FinalizeISel can convert the
10693/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10694/// address materialization and register allocation, but may also be required
10695/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10696/// alloca in the entry block, then the runtime may assume that the alloca's
10697/// StackMap location can be read immediately after compilation and that the
10698/// location is valid at any point during execution (this is similar to the
10699/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10700/// only available in a register, then the runtime would need to trap when
10701/// execution reaches the StackMap in order to read the alloca's location.
10702static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10704 SelectionDAGBuilder &Builder) {
10705 SelectionDAG &DAG = Builder.DAG;
10706 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10707 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10708
10709 // Things on the stack are pointer-typed, meaning that they are already
10710 // legal and can be emitted directly to target nodes.
10712 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10713 } else {
10714 // Otherwise emit a target independent node to be legalised.
10715 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10716 }
10717 }
10718}
10719
10720/// Lower llvm.experimental.stackmap.
10721void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10722 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10723 // [live variables...])
10724
10725 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10726
10727 SDValue Chain, InGlue, Callee;
10729
10730 SDLoc DL = getCurSDLoc();
10732
10733 // The stackmap intrinsic only records the live variables (the arguments
10734 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10735 // intrinsic, this won't be lowered to a function call. This means we don't
10736 // have to worry about calling conventions and target specific lowering code.
10737 // Instead we perform the call lowering right here.
10738 //
10739 // chain, flag = CALLSEQ_START(chain, 0, 0)
10740 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10741 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10742 //
10743 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10744 InGlue = Chain.getValue(1);
10745
10746 // Add the STACKMAP operands, starting with DAG house-keeping.
10747 Ops.push_back(Chain);
10748 Ops.push_back(InGlue);
10749
10750 // Add the <id>, <numShadowBytes> operands.
10751 //
10752 // These do not require legalisation, and can be emitted directly to target
10753 // constant nodes.
10755 assert(ID.getValueType() == MVT::i64);
10756 SDValue IDConst =
10757 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10758 Ops.push_back(IDConst);
10759
10760 SDValue Shad = getValue(CI.getArgOperand(1));
10761 assert(Shad.getValueType() == MVT::i32);
10762 SDValue ShadConst =
10763 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
10764 Ops.push_back(ShadConst);
10765
10766 // Add the live variables.
10767 addStackMapLiveVars(CI, 2, DL, Ops, *this);
10768
10769 // Create the STACKMAP node.
10770 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10771 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10772 InGlue = Chain.getValue(1);
10773
10774 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10775
10776 // Stackmaps don't generate values, so nothing goes into the NodeMap.
10777
10778 // Set the root to the target-lowered call chain.
10779 DAG.setRoot(Chain);
10780
10781 // Inform the Frame Information that we have a stackmap in this function.
10782 FuncInfo.MF->getFrameInfo().setHasStackMap();
10783}
10784
10785/// Lower llvm.experimental.patchpoint directly to its target opcode.
10786void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10787 const BasicBlock *EHPadBB) {
10788 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10789 // i32 <numBytes>,
10790 // i8* <target>,
10791 // i32 <numArgs>,
10792 // [Args...],
10793 // [live variables...])
10794
10796 bool IsAnyRegCC = CC == CallingConv::AnyReg;
10797 bool HasDef = !CB.getType()->isVoidTy();
10798 SDLoc dl = getCurSDLoc();
10800
10801 // Handle immediate and symbolic callees.
10802 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10803 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10804 /*isTarget=*/true);
10805 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
10806 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
10807 SDLoc(SymbolicCallee),
10808 SymbolicCallee->getValueType(0));
10809
10810 // Get the real number of arguments participating in the call <numArgs>
10812 unsigned NumArgs = NArgVal->getAsZExtVal();
10813
10814 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
10815 // Intrinsics include all meta-operands up to but not including CC.
10816 unsigned NumMetaOpers = PatchPointOpers::CCPos;
10817 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
10818 "Not enough arguments provided to the patchpoint intrinsic");
10819
10820 // For AnyRegCC the arguments are lowered later on manually.
10821 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
10822 Type *ReturnTy =
10823 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
10824
10825 TargetLowering::CallLoweringInfo CLI(DAG);
10826 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
10827 ReturnTy, CB.getAttributes().getRetAttrs(), true);
10828 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
10829
10830 SDNode *CallEnd = Result.second.getNode();
10831 if (CallEnd->getOpcode() == ISD::EH_LABEL)
10832 CallEnd = CallEnd->getOperand(0).getNode();
10833 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
10834 CallEnd = CallEnd->getOperand(0).getNode();
10835
10836 /// Get a call instruction from the call sequence chain.
10837 /// Tail calls are not allowed.
10838 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
10839 "Expected a callseq node.");
10840 SDNode *Call = CallEnd->getOperand(0).getNode();
10841 bool HasGlue = Call->getGluedNode();
10842
10843 // Replace the target specific call node with the patchable intrinsic.
10845
10846 // Push the chain.
10847 Ops.push_back(*(Call->op_begin()));
10848
10849 // Optionally, push the glue (if any).
10850 if (HasGlue)
10851 Ops.push_back(*(Call->op_end() - 1));
10852
10853 // Push the register mask info.
10854 if (HasGlue)
10855 Ops.push_back(*(Call->op_end() - 2));
10856 else
10857 Ops.push_back(*(Call->op_end() - 1));
10858
10859 // Add the <id> and <numBytes> constants.
10861 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
10863 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
10864
10865 // Add the callee.
10866 Ops.push_back(Callee);
10867
10868 // Adjust <numArgs> to account for any arguments that have been passed on the
10869 // stack instead.
10870 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
10871 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
10872 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
10873 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
10874
10875 // Add the calling convention
10876 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
10877
10878 // Add the arguments we omitted previously. The register allocator should
10879 // place these in any free register.
10880 if (IsAnyRegCC)
10881 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
10882 Ops.push_back(getValue(CB.getArgOperand(i)));
10883
10884 // Push the arguments from the call instruction.
10885 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
10886 Ops.append(Call->op_begin() + 2, e);
10887
10888 // Push live variables for the stack map.
10889 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
10890
10891 SDVTList NodeTys;
10892 if (IsAnyRegCC && HasDef) {
10893 // Create the return types based on the intrinsic definition
10894 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10895 SmallVector<EVT, 3> ValueVTs;
10896 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
10897 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
10898
10899 // There is always a chain and a glue type at the end
10900 ValueVTs.push_back(MVT::Other);
10901 ValueVTs.push_back(MVT::Glue);
10902 NodeTys = DAG.getVTList(ValueVTs);
10903 } else
10904 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10905
10906 // Replace the target specific call node with a PATCHPOINT node.
10907 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
10908
10909 // Update the NodeMap.
10910 if (HasDef) {
10911 if (IsAnyRegCC)
10912 setValue(&CB, SDValue(PPV.getNode(), 0));
10913 else
10914 setValue(&CB, Result.first);
10915 }
10916
10917 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
10918 // call sequence. Furthermore the location of the chain and glue can change
10919 // when the AnyReg calling convention is used and the intrinsic returns a
10920 // value.
10921 if (IsAnyRegCC && HasDef) {
10922 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
10923 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
10924 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
10925 } else
10926 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
10927 DAG.DeleteNode(Call);
10928
10929 // Inform the Frame Information that we have a patchpoint in this function.
10930 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
10931}
10932
10933void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
10934 unsigned Intrinsic) {
10935 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10936 SDValue Op1 = getValue(I.getArgOperand(0));
10937 SDValue Op2;
10938 if (I.arg_size() > 1)
10939 Op2 = getValue(I.getArgOperand(1));
10940 SDLoc dl = getCurSDLoc();
10941 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
10942 SDValue Res;
10943 SDNodeFlags SDFlags;
10944 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
10945 SDFlags.copyFMF(*FPMO);
10946
10947 switch (Intrinsic) {
10948 case Intrinsic::vector_reduce_fadd:
10949 if (SDFlags.hasAllowReassociation())
10950 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
10951 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
10952 SDFlags);
10953 else
10954 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
10955 break;
10956 case Intrinsic::vector_reduce_fmul:
10957 if (SDFlags.hasAllowReassociation())
10958 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
10959 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
10960 SDFlags);
10961 else
10962 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
10963 break;
10964 case Intrinsic::vector_reduce_add:
10965 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
10966 break;
10967 case Intrinsic::vector_reduce_mul:
10968 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
10969 break;
10970 case Intrinsic::vector_reduce_and:
10971 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
10972 break;
10973 case Intrinsic::vector_reduce_or:
10974 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
10975 break;
10976 case Intrinsic::vector_reduce_xor:
10977 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
10978 break;
10979 case Intrinsic::vector_reduce_smax:
10980 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
10981 break;
10982 case Intrinsic::vector_reduce_smin:
10983 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
10984 break;
10985 case Intrinsic::vector_reduce_umax:
10986 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
10987 break;
10988 case Intrinsic::vector_reduce_umin:
10989 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
10990 break;
10991 case Intrinsic::vector_reduce_fmax:
10992 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
10993 break;
10994 case Intrinsic::vector_reduce_fmin:
10995 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
10996 break;
10997 case Intrinsic::vector_reduce_fmaximum:
10998 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
10999 break;
11000 case Intrinsic::vector_reduce_fminimum:
11001 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11002 break;
11003 default:
11004 llvm_unreachable("Unhandled vector reduce intrinsic");
11005 }
11006 setValue(&I, Res);
11007}
11008
11009/// Returns an AttributeList representing the attributes applied to the return
11010/// value of the given call.
11013 if (CLI.RetSExt)
11014 Attrs.push_back(Attribute::SExt);
11015 if (CLI.RetZExt)
11016 Attrs.push_back(Attribute::ZExt);
11017 if (CLI.IsInReg)
11018 Attrs.push_back(Attribute::InReg);
11019
11020 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11021 Attrs);
11022}
11023
11024/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11025/// implementation, which just calls LowerCall.
11026/// FIXME: When all targets are
11027/// migrated to using LowerCall, this hook should be integrated into SDISel.
11028std::pair<SDValue, SDValue>
11030 LLVMContext &Context = CLI.RetTy->getContext();
11031
11032 // Handle the incoming return values from the call.
11033 CLI.Ins.clear();
11034 SmallVector<Type *, 4> RetOrigTys;
11036 auto &DL = CLI.DAG.getDataLayout();
11037 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11038
11039 SmallVector<EVT, 4> RetVTs;
11040 if (CLI.RetTy != CLI.OrigRetTy) {
11041 assert(RetOrigTys.size() == 1 &&
11042 "Only supported for non-aggregate returns");
11043 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11044 } else {
11045 for (Type *Ty : RetOrigTys)
11046 RetVTs.push_back(getValueType(DL, Ty));
11047 }
11048
11049 if (CLI.IsPostTypeLegalization) {
11050 // If we are lowering a libcall after legalization, split the return type.
11051 SmallVector<Type *, 4> OldRetOrigTys;
11052 SmallVector<EVT, 4> OldRetVTs;
11053 SmallVector<TypeSize, 4> OldOffsets;
11054 RetOrigTys.swap(OldRetOrigTys);
11055 RetVTs.swap(OldRetVTs);
11056 Offsets.swap(OldOffsets);
11057
11058 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11059 EVT RetVT = OldRetVTs[i];
11060 uint64_t Offset = OldOffsets[i];
11061 MVT RegisterVT = getRegisterType(Context, RetVT);
11062 unsigned NumRegs = getNumRegisters(Context, RetVT);
11063 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11064 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11065 RetVTs.append(NumRegs, RegisterVT);
11066 for (unsigned j = 0; j != NumRegs; ++j)
11067 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11068 }
11069 }
11070
11072 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11073
11074 bool CanLowerReturn =
11076 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11077
11078 SDValue DemoteStackSlot;
11079 int DemoteStackIdx = -100;
11080 if (!CanLowerReturn) {
11081 // FIXME: equivalent assert?
11082 // assert(!CS.hasInAllocaArgument() &&
11083 // "sret demotion is incompatible with inalloca");
11084 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11085 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11087 DemoteStackIdx =
11088 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11089 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11090
11091 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11092 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11093 Entry.IsSRet = true;
11094 Entry.Alignment = Alignment;
11095 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11096 CLI.NumFixedArgs += 1;
11097 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11098 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11099
11100 // sret demotion isn't compatible with tail-calls, since the sret argument
11101 // points into the callers stack frame.
11102 CLI.IsTailCall = false;
11103 } else {
11104 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11105 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11106 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11107 ISD::ArgFlagsTy Flags;
11108 if (NeedsRegBlock) {
11109 Flags.setInConsecutiveRegs();
11110 if (I == RetVTs.size() - 1)
11111 Flags.setInConsecutiveRegsLast();
11112 }
11113 EVT VT = RetVTs[I];
11114 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11115 unsigned NumRegs =
11116 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11117 for (unsigned i = 0; i != NumRegs; ++i) {
11118 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11120 if (CLI.RetTy->isPointerTy()) {
11121 Ret.Flags.setPointer();
11122 Ret.Flags.setPointerAddrSpace(
11123 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11124 }
11125 if (CLI.RetSExt)
11126 Ret.Flags.setSExt();
11127 if (CLI.RetZExt)
11128 Ret.Flags.setZExt();
11129 if (CLI.IsInReg)
11130 Ret.Flags.setInReg();
11131 CLI.Ins.push_back(Ret);
11132 }
11133 }
11134 }
11135
11136 // We push in swifterror return as the last element of CLI.Ins.
11137 ArgListTy &Args = CLI.getArgs();
11138 if (supportSwiftError()) {
11139 for (const ArgListEntry &Arg : Args) {
11140 if (Arg.IsSwiftError) {
11141 ISD::ArgFlagsTy Flags;
11142 Flags.setSwiftError();
11144 PointerType::getUnqual(Context),
11145 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11146 CLI.Ins.push_back(Ret);
11147 }
11148 }
11149 }
11150
11151 // Handle all of the outgoing arguments.
11152 CLI.Outs.clear();
11153 CLI.OutVals.clear();
11154 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11155 SmallVector<Type *, 4> OrigArgTys;
11156 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11157 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11158 Type *FinalType = Args[i].Ty;
11159 if (Args[i].IsByVal)
11160 FinalType = Args[i].IndirectType;
11161 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11162 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11163 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11164 ++Value) {
11165 Type *OrigArgTy = OrigArgTys[Value];
11166 Type *ArgTy = OrigArgTy;
11167 if (Args[i].Ty != Args[i].OrigTy) {
11168 assert(Value == 0 && "Only supported for non-aggregate arguments");
11169 ArgTy = Args[i].Ty;
11170 }
11171
11172 EVT VT = getValueType(DL, ArgTy);
11173 SDValue Op = SDValue(Args[i].Node.getNode(),
11174 Args[i].Node.getResNo() + Value);
11175 ISD::ArgFlagsTy Flags;
11176
11177 // Certain targets (such as MIPS), may have a different ABI alignment
11178 // for a type depending on the context. Give the target a chance to
11179 // specify the alignment it wants.
11180 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11181 Flags.setOrigAlign(OriginalAlignment);
11182
11183 if (i >= CLI.NumFixedArgs)
11184 Flags.setVarArg();
11185 if (ArgTy->isPointerTy()) {
11186 Flags.setPointer();
11187 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11188 }
11189 if (Args[i].IsZExt)
11190 Flags.setZExt();
11191 if (Args[i].IsSExt)
11192 Flags.setSExt();
11193 if (Args[i].IsNoExt)
11194 Flags.setNoExt();
11195 if (Args[i].IsInReg) {
11196 // If we are using vectorcall calling convention, a structure that is
11197 // passed InReg - is surely an HVA
11199 isa<StructType>(FinalType)) {
11200 // The first value of a structure is marked
11201 if (0 == Value)
11202 Flags.setHvaStart();
11203 Flags.setHva();
11204 }
11205 // Set InReg Flag
11206 Flags.setInReg();
11207 }
11208 if (Args[i].IsSRet)
11209 Flags.setSRet();
11210 if (Args[i].IsSwiftSelf)
11211 Flags.setSwiftSelf();
11212 if (Args[i].IsSwiftAsync)
11213 Flags.setSwiftAsync();
11214 if (Args[i].IsSwiftError)
11215 Flags.setSwiftError();
11216 if (Args[i].IsCFGuardTarget)
11217 Flags.setCFGuardTarget();
11218 if (Args[i].IsByVal)
11219 Flags.setByVal();
11220 if (Args[i].IsByRef)
11221 Flags.setByRef();
11222 if (Args[i].IsPreallocated) {
11223 Flags.setPreallocated();
11224 // Set the byval flag for CCAssignFn callbacks that don't know about
11225 // preallocated. This way we can know how many bytes we should've
11226 // allocated and how many bytes a callee cleanup function will pop. If
11227 // we port preallocated to more targets, we'll have to add custom
11228 // preallocated handling in the various CC lowering callbacks.
11229 Flags.setByVal();
11230 }
11231 if (Args[i].IsInAlloca) {
11232 Flags.setInAlloca();
11233 // Set the byval flag for CCAssignFn callbacks that don't know about
11234 // inalloca. This way we can know how many bytes we should've allocated
11235 // and how many bytes a callee cleanup function will pop. If we port
11236 // inalloca to more targets, we'll have to add custom inalloca handling
11237 // in the various CC lowering callbacks.
11238 Flags.setByVal();
11239 }
11240 Align MemAlign;
11241 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11242 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11243 Flags.setByValSize(FrameSize);
11244
11245 // info is not there but there are cases it cannot get right.
11246 if (auto MA = Args[i].Alignment)
11247 MemAlign = *MA;
11248 else
11249 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11250 } else if (auto MA = Args[i].Alignment) {
11251 MemAlign = *MA;
11252 } else {
11253 MemAlign = OriginalAlignment;
11254 }
11255 Flags.setMemAlign(MemAlign);
11256 if (Args[i].IsNest)
11257 Flags.setNest();
11258 if (NeedsRegBlock)
11259 Flags.setInConsecutiveRegs();
11260
11261 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11262 unsigned NumParts =
11263 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11264 SmallVector<SDValue, 4> Parts(NumParts);
11265 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11266
11267 if (Args[i].IsSExt)
11268 ExtendKind = ISD::SIGN_EXTEND;
11269 else if (Args[i].IsZExt)
11270 ExtendKind = ISD::ZERO_EXTEND;
11271
11272 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11273 // for now.
11274 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11276 assert((CLI.RetTy == Args[i].Ty ||
11277 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11279 Args[i].Ty->getPointerAddressSpace())) &&
11280 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11281 // Before passing 'returned' to the target lowering code, ensure that
11282 // either the register MVT and the actual EVT are the same size or that
11283 // the return value and argument are extended in the same way; in these
11284 // cases it's safe to pass the argument register value unchanged as the
11285 // return register value (although it's at the target's option whether
11286 // to do so)
11287 // TODO: allow code generation to take advantage of partially preserved
11288 // registers rather than clobbering the entire register when the
11289 // parameter extension method is not compatible with the return
11290 // extension method
11291 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11292 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11293 CLI.RetZExt == Args[i].IsZExt))
11294 Flags.setReturned();
11295 }
11296
11297 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11298 CLI.CallConv, ExtendKind);
11299
11300 for (unsigned j = 0; j != NumParts; ++j) {
11301 // if it isn't first piece, alignment must be 1
11302 // For scalable vectors the scalable part is currently handled
11303 // by individual targets, so we just use the known minimum size here.
11304 ISD::OutputArg MyFlags(
11305 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11306 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11307 if (NumParts > 1 && j == 0)
11308 MyFlags.Flags.setSplit();
11309 else if (j != 0) {
11310 MyFlags.Flags.setOrigAlign(Align(1));
11311 if (j == NumParts - 1)
11312 MyFlags.Flags.setSplitEnd();
11313 }
11314
11315 CLI.Outs.push_back(MyFlags);
11316 CLI.OutVals.push_back(Parts[j]);
11317 }
11318
11319 if (NeedsRegBlock && Value == NumValues - 1)
11320 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11321 }
11322 }
11323
11325 CLI.Chain = LowerCall(CLI, InVals);
11326
11327 // Update CLI.InVals to use outside of this function.
11328 CLI.InVals = InVals;
11329
11330 // Verify that the target's LowerCall behaved as expected.
11331 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11332 "LowerCall didn't return a valid chain!");
11333 assert((!CLI.IsTailCall || InVals.empty()) &&
11334 "LowerCall emitted a return value for a tail call!");
11335 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11336 "LowerCall didn't emit the correct number of values!");
11337
11338 // For a tail call, the return value is merely live-out and there aren't
11339 // any nodes in the DAG representing it. Return a special value to
11340 // indicate that a tail call has been emitted and no more Instructions
11341 // should be processed in the current block.
11342 if (CLI.IsTailCall) {
11343 CLI.DAG.setRoot(CLI.Chain);
11344 return std::make_pair(SDValue(), SDValue());
11345 }
11346
11347#ifndef NDEBUG
11348 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11349 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11350 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11351 "LowerCall emitted a value with the wrong type!");
11352 }
11353#endif
11354
11355 SmallVector<SDValue, 4> ReturnValues;
11356 if (!CanLowerReturn) {
11357 // The instruction result is the result of loading from the
11358 // hidden sret parameter.
11359 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11360
11361 unsigned NumValues = RetVTs.size();
11362 ReturnValues.resize(NumValues);
11363 SmallVector<SDValue, 4> Chains(NumValues);
11364
11365 // An aggregate return value cannot wrap around the address space, so
11366 // offsets to its parts don't wrap either.
11368 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11369 for (unsigned i = 0; i < NumValues; ++i) {
11371 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11373 SDValue L = CLI.DAG.getLoad(
11374 RetVTs[i], CLI.DL, CLI.Chain, Add,
11376 DemoteStackIdx, Offsets[i]),
11377 HiddenSRetAlign);
11378 ReturnValues[i] = L;
11379 Chains[i] = L.getValue(1);
11380 }
11381
11382 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11383 } else {
11384 // Collect the legal value parts into potentially illegal values
11385 // that correspond to the original function's return values.
11386 std::optional<ISD::NodeType> AssertOp;
11387 if (CLI.RetSExt)
11388 AssertOp = ISD::AssertSext;
11389 else if (CLI.RetZExt)
11390 AssertOp = ISD::AssertZext;
11391 unsigned CurReg = 0;
11392 for (EVT VT : RetVTs) {
11393 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11394 unsigned NumRegs =
11395 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11396
11397 ReturnValues.push_back(getCopyFromParts(
11398 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11399 CLI.Chain, CLI.CallConv, AssertOp));
11400 CurReg += NumRegs;
11401 }
11402
11403 // For a function returning void, there is no return value. We can't create
11404 // such a node, so we just return a null return value in that case. In
11405 // that case, nothing will actually look at the value.
11406 if (ReturnValues.empty())
11407 return std::make_pair(SDValue(), CLI.Chain);
11408 }
11409
11410 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11411 CLI.DAG.getVTList(RetVTs), ReturnValues);
11412 return std::make_pair(Res, CLI.Chain);
11413}
11414
11415/// Places new result values for the node in Results (their number
11416/// and types must exactly match those of the original return values of
11417/// the node), or leaves Results empty, which indicates that the node is not
11418/// to be custom lowered after all.
11421 SelectionDAG &DAG) const {
11422 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11423
11424 if (!Res.getNode())
11425 return;
11426
11427 // If the original node has one result, take the return value from
11428 // LowerOperation as is. It might not be result number 0.
11429 if (N->getNumValues() == 1) {
11430 Results.push_back(Res);
11431 return;
11432 }
11433
11434 // If the original node has multiple results, then the return node should
11435 // have the same number of results.
11436 assert((N->getNumValues() == Res->getNumValues()) &&
11437 "Lowering returned the wrong number of results!");
11438
11439 // Places new result values base on N result number.
11440 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11441 Results.push_back(Res.getValue(I));
11442}
11443
11445 llvm_unreachable("LowerOperation not implemented for this target!");
11446}
11447
11449 Register Reg,
11450 ISD::NodeType ExtendType) {
11452 assert((Op.getOpcode() != ISD::CopyFromReg ||
11453 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11454 "Copy from a reg to the same reg!");
11455 assert(!Reg.isPhysical() && "Is a physreg");
11456
11457 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11458 // If this is an InlineAsm we have to match the registers required, not the
11459 // notional registers required by the type.
11460
11461 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11462 std::nullopt); // This is not an ABI copy.
11463 SDValue Chain = DAG.getEntryNode();
11464
11465 if (ExtendType == ISD::ANY_EXTEND) {
11466 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11467 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11468 ExtendType = PreferredExtendIt->second;
11469 }
11470 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11471 PendingExports.push_back(Chain);
11472}
11473
11475
11476/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11477/// entry block, return true. This includes arguments used by switches, since
11478/// the switch may expand into multiple basic blocks.
11479static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11480 // With FastISel active, we may be splitting blocks, so force creation
11481 // of virtual registers for all non-dead arguments.
11482 if (FastISel)
11483 return A->use_empty();
11484
11485 const BasicBlock &Entry = A->getParent()->front();
11486 for (const User *U : A->users())
11487 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11488 return false; // Use not in entry block.
11489
11490 return true;
11491}
11492
11494 DenseMap<const Argument *,
11495 std::pair<const AllocaInst *, const StoreInst *>>;
11496
11497/// Scan the entry block of the function in FuncInfo for arguments that look
11498/// like copies into a local alloca. Record any copied arguments in
11499/// ArgCopyElisionCandidates.
11500static void
11502 FunctionLoweringInfo *FuncInfo,
11503 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11504 // Record the state of every static alloca used in the entry block. Argument
11505 // allocas are all used in the entry block, so we need approximately as many
11506 // entries as we have arguments.
11507 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11509 unsigned NumArgs = FuncInfo->Fn->arg_size();
11510 StaticAllocas.reserve(NumArgs * 2);
11511
11512 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11513 if (!V)
11514 return nullptr;
11515 V = V->stripPointerCasts();
11516 const auto *AI = dyn_cast<AllocaInst>(V);
11517 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11518 return nullptr;
11519 auto Iter = StaticAllocas.insert({AI, Unknown});
11520 return &Iter.first->second;
11521 };
11522
11523 // Look for stores of arguments to static allocas. Look through bitcasts and
11524 // GEPs to handle type coercions, as long as the alloca is fully initialized
11525 // by the store. Any non-store use of an alloca escapes it and any subsequent
11526 // unanalyzed store might write it.
11527 // FIXME: Handle structs initialized with multiple stores.
11528 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11529 // Look for stores, and handle non-store uses conservatively.
11530 const auto *SI = dyn_cast<StoreInst>(&I);
11531 if (!SI) {
11532 // We will look through cast uses, so ignore them completely.
11533 if (I.isCast())
11534 continue;
11535 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11536 // to allocas.
11537 if (I.isDebugOrPseudoInst())
11538 continue;
11539 // This is an unknown instruction. Assume it escapes or writes to all
11540 // static alloca operands.
11541 for (const Use &U : I.operands()) {
11542 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11543 *Info = StaticAllocaInfo::Clobbered;
11544 }
11545 continue;
11546 }
11547
11548 // If the stored value is a static alloca, mark it as escaped.
11549 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11550 *Info = StaticAllocaInfo::Clobbered;
11551
11552 // Check if the destination is a static alloca.
11553 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11554 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11555 if (!Info)
11556 continue;
11557 const AllocaInst *AI = cast<AllocaInst>(Dst);
11558
11559 // Skip allocas that have been initialized or clobbered.
11560 if (*Info != StaticAllocaInfo::Unknown)
11561 continue;
11562
11563 // Check if the stored value is an argument, and that this store fully
11564 // initializes the alloca.
11565 // If the argument type has padding bits we can't directly forward a pointer
11566 // as the upper bits may contain garbage.
11567 // Don't elide copies from the same argument twice.
11568 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11569 const auto *Arg = dyn_cast<Argument>(Val);
11570 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11571 Arg->getType()->isEmptyTy() ||
11572 DL.getTypeStoreSize(Arg->getType()) !=
11573 DL.getTypeAllocSize(AI->getAllocatedType()) ||
11574 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11575 ArgCopyElisionCandidates.count(Arg)) {
11576 *Info = StaticAllocaInfo::Clobbered;
11577 continue;
11578 }
11579
11580 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11581 << '\n');
11582
11583 // Mark this alloca and store for argument copy elision.
11584 *Info = StaticAllocaInfo::Elidable;
11585 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11586
11587 // Stop scanning if we've seen all arguments. This will happen early in -O0
11588 // builds, which is useful, because -O0 builds have large entry blocks and
11589 // many allocas.
11590 if (ArgCopyElisionCandidates.size() == NumArgs)
11591 break;
11592 }
11593}
11594
11595/// Try to elide argument copies from memory into a local alloca. Succeeds if
11596/// ArgVal is a load from a suitable fixed stack object.
11599 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11600 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11601 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11602 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11603 // Check if this is a load from a fixed stack object.
11604 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11605 if (!LNode)
11606 return;
11607 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11608 if (!FINode)
11609 return;
11610
11611 // Check that the fixed stack object is the right size and alignment.
11612 // Look at the alignment that the user wrote on the alloca instead of looking
11613 // at the stack object.
11614 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11615 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11616 const AllocaInst *AI = ArgCopyIter->second.first;
11617 int FixedIndex = FINode->getIndex();
11618 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11619 int OldIndex = AllocaIndex;
11620 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11621 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11622 LLVM_DEBUG(
11623 dbgs() << " argument copy elision failed due to bad fixed stack "
11624 "object size\n");
11625 return;
11626 }
11627 Align RequiredAlignment = AI->getAlign();
11628 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11629 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11630 "greater than stack argument alignment ("
11631 << DebugStr(RequiredAlignment) << " vs "
11632 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11633 return;
11634 }
11635
11636 // Perform the elision. Delete the old stack object and replace its only use
11637 // in the variable info map. Mark the stack object as mutable and aliased.
11638 LLVM_DEBUG({
11639 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11640 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11641 << '\n';
11642 });
11643 MFI.RemoveStackObject(OldIndex);
11644 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11645 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11646 AllocaIndex = FixedIndex;
11647 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11648 for (SDValue ArgVal : ArgVals)
11649 Chains.push_back(ArgVal.getValue(1));
11650
11651 // Avoid emitting code for the store implementing the copy.
11652 const StoreInst *SI = ArgCopyIter->second.second;
11653 ElidedArgCopyInstrs.insert(SI);
11654
11655 // Check for uses of the argument again so that we can avoid exporting ArgVal
11656 // if it is't used by anything other than the store.
11657 for (const Value *U : Arg.users()) {
11658 if (U != SI) {
11659 ArgHasUses = true;
11660 break;
11661 }
11662 }
11663}
11664
11665void SelectionDAGISel::LowerArguments(const Function &F) {
11666 SelectionDAG &DAG = SDB->DAG;
11667 SDLoc dl = SDB->getCurSDLoc();
11668 const DataLayout &DL = DAG.getDataLayout();
11670
11671 // In Naked functions we aren't going to save any registers.
11672 if (F.hasFnAttribute(Attribute::Naked))
11673 return;
11674
11675 if (!FuncInfo->CanLowerReturn) {
11676 // Put in an sret pointer parameter before all the other parameters.
11677 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11678
11679 ISD::ArgFlagsTy Flags;
11680 Flags.setSRet();
11681 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11682 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11684 Ins.push_back(RetArg);
11685 }
11686
11687 // Look for stores of arguments to static allocas. Mark such arguments with a
11688 // flag to ask the target to give us the memory location of that argument if
11689 // available.
11690 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11692 ArgCopyElisionCandidates);
11693
11694 // Set up the incoming argument description vector.
11695 for (const Argument &Arg : F.args()) {
11696 unsigned ArgNo = Arg.getArgNo();
11698 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11699 bool isArgValueUsed = !Arg.use_empty();
11700 unsigned PartBase = 0;
11701 Type *FinalType = Arg.getType();
11702 if (Arg.hasAttribute(Attribute::ByVal))
11703 FinalType = Arg.getParamByValType();
11704 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11705 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11706 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11707 ++Value) {
11708 Type *ArgTy = Types[Value];
11709 EVT VT = TLI->getValueType(DL, ArgTy);
11710 ISD::ArgFlagsTy Flags;
11711
11712 if (ArgTy->isPointerTy()) {
11713 Flags.setPointer();
11714 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11715 }
11716 if (Arg.hasAttribute(Attribute::ZExt))
11717 Flags.setZExt();
11718 if (Arg.hasAttribute(Attribute::SExt))
11719 Flags.setSExt();
11720 if (Arg.hasAttribute(Attribute::InReg)) {
11721 // If we are using vectorcall calling convention, a structure that is
11722 // passed InReg - is surely an HVA
11723 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11724 isa<StructType>(Arg.getType())) {
11725 // The first value of a structure is marked
11726 if (0 == Value)
11727 Flags.setHvaStart();
11728 Flags.setHva();
11729 }
11730 // Set InReg Flag
11731 Flags.setInReg();
11732 }
11733 if (Arg.hasAttribute(Attribute::StructRet))
11734 Flags.setSRet();
11735 if (Arg.hasAttribute(Attribute::SwiftSelf))
11736 Flags.setSwiftSelf();
11737 if (Arg.hasAttribute(Attribute::SwiftAsync))
11738 Flags.setSwiftAsync();
11739 if (Arg.hasAttribute(Attribute::SwiftError))
11740 Flags.setSwiftError();
11741 if (Arg.hasAttribute(Attribute::ByVal))
11742 Flags.setByVal();
11743 if (Arg.hasAttribute(Attribute::ByRef))
11744 Flags.setByRef();
11745 if (Arg.hasAttribute(Attribute::InAlloca)) {
11746 Flags.setInAlloca();
11747 // Set the byval flag for CCAssignFn callbacks that don't know about
11748 // inalloca. This way we can know how many bytes we should've allocated
11749 // and how many bytes a callee cleanup function will pop. If we port
11750 // inalloca to more targets, we'll have to add custom inalloca handling
11751 // in the various CC lowering callbacks.
11752 Flags.setByVal();
11753 }
11754 if (Arg.hasAttribute(Attribute::Preallocated)) {
11755 Flags.setPreallocated();
11756 // Set the byval flag for CCAssignFn callbacks that don't know about
11757 // preallocated. This way we can know how many bytes we should've
11758 // allocated and how many bytes a callee cleanup function will pop. If
11759 // we port preallocated to more targets, we'll have to add custom
11760 // preallocated handling in the various CC lowering callbacks.
11761 Flags.setByVal();
11762 }
11763
11764 // Certain targets (such as MIPS), may have a different ABI alignment
11765 // for a type depending on the context. Give the target a chance to
11766 // specify the alignment it wants.
11767 const Align OriginalAlignment(
11768 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
11769 Flags.setOrigAlign(OriginalAlignment);
11770
11771 Align MemAlign;
11772 Type *ArgMemTy = nullptr;
11773 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11774 Flags.isByRef()) {
11775 if (!ArgMemTy)
11776 ArgMemTy = Arg.getPointeeInMemoryValueType();
11777
11778 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11779
11780 // For in-memory arguments, size and alignment should be passed from FE.
11781 // BE will guess if this info is not there but there are cases it cannot
11782 // get right.
11783 if (auto ParamAlign = Arg.getParamStackAlign())
11784 MemAlign = *ParamAlign;
11785 else if ((ParamAlign = Arg.getParamAlign()))
11786 MemAlign = *ParamAlign;
11787 else
11788 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
11789 if (Flags.isByRef())
11790 Flags.setByRefSize(MemSize);
11791 else
11792 Flags.setByValSize(MemSize);
11793 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11794 MemAlign = *ParamAlign;
11795 } else {
11796 MemAlign = OriginalAlignment;
11797 }
11798 Flags.setMemAlign(MemAlign);
11799
11800 if (Arg.hasAttribute(Attribute::Nest))
11801 Flags.setNest();
11802 if (NeedsRegBlock)
11803 Flags.setInConsecutiveRegs();
11804 if (ArgCopyElisionCandidates.count(&Arg))
11805 Flags.setCopyElisionCandidate();
11806 if (Arg.hasAttribute(Attribute::Returned))
11807 Flags.setReturned();
11808
11809 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
11810 *CurDAG->getContext(), F.getCallingConv(), VT);
11811 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
11812 *CurDAG->getContext(), F.getCallingConv(), VT);
11813 for (unsigned i = 0; i != NumRegs; ++i) {
11814 // For scalable vectors, use the minimum size; individual targets
11815 // are responsible for handling scalable vector arguments and
11816 // return values.
11817 ISD::InputArg MyFlags(
11818 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
11819 PartBase + i * RegisterVT.getStoreSize().getKnownMinValue());
11820 if (NumRegs > 1 && i == 0)
11821 MyFlags.Flags.setSplit();
11822 // if it isn't first piece, alignment must be 1
11823 else if (i > 0) {
11824 MyFlags.Flags.setOrigAlign(Align(1));
11825 if (i == NumRegs - 1)
11826 MyFlags.Flags.setSplitEnd();
11827 }
11828 Ins.push_back(MyFlags);
11829 }
11830 if (NeedsRegBlock && Value == NumValues - 1)
11831 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
11832 PartBase += VT.getStoreSize().getKnownMinValue();
11833 }
11834 }
11835
11836 // Call the target to set up the argument values.
11838 SDValue NewRoot = TLI->LowerFormalArguments(
11839 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
11840
11841 // Verify that the target's LowerFormalArguments behaved as expected.
11842 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
11843 "LowerFormalArguments didn't return a valid chain!");
11844 assert(InVals.size() == Ins.size() &&
11845 "LowerFormalArguments didn't emit the correct number of values!");
11846 LLVM_DEBUG({
11847 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
11848 assert(InVals[i].getNode() &&
11849 "LowerFormalArguments emitted a null value!");
11850 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
11851 "LowerFormalArguments emitted a value with the wrong type!");
11852 }
11853 });
11854
11855 // Update the DAG with the new chain value resulting from argument lowering.
11856 DAG.setRoot(NewRoot);
11857
11858 // Set up the argument values.
11859 unsigned i = 0;
11860 if (!FuncInfo->CanLowerReturn) {
11861 // Create a virtual register for the sret pointer, and put in a copy
11862 // from the sret argument into it.
11863 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11864 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
11865 std::optional<ISD::NodeType> AssertOp;
11866 SDValue ArgValue =
11867 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
11868 F.getCallingConv(), AssertOp);
11869
11870 MachineFunction& MF = SDB->DAG.getMachineFunction();
11871 MachineRegisterInfo& RegInfo = MF.getRegInfo();
11872 Register SRetReg =
11873 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
11874 FuncInfo->DemoteRegister = SRetReg;
11875 NewRoot =
11876 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
11877 DAG.setRoot(NewRoot);
11878
11879 // i indexes lowered arguments. Bump it past the hidden sret argument.
11880 ++i;
11881 }
11882
11884 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
11885 for (const Argument &Arg : F.args()) {
11886 SmallVector<SDValue, 4> ArgValues;
11887 SmallVector<EVT, 4> ValueVTs;
11888 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
11889 unsigned NumValues = ValueVTs.size();
11890 if (NumValues == 0)
11891 continue;
11892
11893 bool ArgHasUses = !Arg.use_empty();
11894
11895 // Elide the copying store if the target loaded this argument from a
11896 // suitable fixed stack object.
11897 if (Ins[i].Flags.isCopyElisionCandidate()) {
11898 unsigned NumParts = 0;
11899 for (EVT VT : ValueVTs)
11900 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
11901 F.getCallingConv(), VT);
11902
11903 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
11904 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
11905 ArrayRef(&InVals[i], NumParts), ArgHasUses);
11906 }
11907
11908 // If this argument is unused then remember its value. It is used to generate
11909 // debugging information.
11910 bool isSwiftErrorArg =
11911 TLI->supportSwiftError() &&
11912 Arg.hasAttribute(Attribute::SwiftError);
11913 if (!ArgHasUses && !isSwiftErrorArg) {
11914 SDB->setUnusedArgValue(&Arg, InVals[i]);
11915
11916 // Also remember any frame index for use in FastISel.
11917 if (FrameIndexSDNode *FI =
11919 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11920 }
11921
11922 for (unsigned Val = 0; Val != NumValues; ++Val) {
11923 EVT VT = ValueVTs[Val];
11924 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
11925 F.getCallingConv(), VT);
11926 unsigned NumParts = TLI->getNumRegistersForCallingConv(
11927 *CurDAG->getContext(), F.getCallingConv(), VT);
11928
11929 // Even an apparent 'unused' swifterror argument needs to be returned. So
11930 // we do generate a copy for it that can be used on return from the
11931 // function.
11932 if (ArgHasUses || isSwiftErrorArg) {
11933 std::optional<ISD::NodeType> AssertOp;
11934 if (Arg.hasAttribute(Attribute::SExt))
11935 AssertOp = ISD::AssertSext;
11936 else if (Arg.hasAttribute(Attribute::ZExt))
11937 AssertOp = ISD::AssertZext;
11938
11939 SDValue OutVal =
11940 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
11941 NewRoot, F.getCallingConv(), AssertOp);
11942
11943 FPClassTest NoFPClass = Arg.getNoFPClass();
11944 if (NoFPClass != fcNone) {
11945 SDValue SDNoFPClass = DAG.getTargetConstant(
11946 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
11947 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
11948 OutVal, SDNoFPClass);
11949 }
11950 ArgValues.push_back(OutVal);
11951 }
11952
11953 i += NumParts;
11954 }
11955
11956 // We don't need to do anything else for unused arguments.
11957 if (ArgValues.empty())
11958 continue;
11959
11960 // Note down frame index.
11961 if (FrameIndexSDNode *FI =
11962 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
11963 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11964
11965 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
11966 SDB->getCurSDLoc());
11967
11968 SDB->setValue(&Arg, Res);
11969 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
11970 // We want to associate the argument with the frame index, among
11971 // involved operands, that correspond to the lowest address. The
11972 // getCopyFromParts function, called earlier, is swapping the order of
11973 // the operands to BUILD_PAIR depending on endianness. The result of
11974 // that swapping is that the least significant bits of the argument will
11975 // be in the first operand of the BUILD_PAIR node, and the most
11976 // significant bits will be in the second operand.
11977 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
11978 if (LoadSDNode *LNode =
11979 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
11980 if (FrameIndexSDNode *FI =
11981 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
11982 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11983 }
11984
11985 // Analyses past this point are naive and don't expect an assertion.
11986 if (Res.getOpcode() == ISD::AssertZext)
11987 Res = Res.getOperand(0);
11988
11989 // Update the SwiftErrorVRegDefMap.
11990 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
11991 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
11992 if (Reg.isVirtual())
11993 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
11994 Reg);
11995 }
11996
11997 // If this argument is live outside of the entry block, insert a copy from
11998 // wherever we got it to the vreg that other BB's will reference it as.
11999 if (Res.getOpcode() == ISD::CopyFromReg) {
12000 // If we can, though, try to skip creating an unnecessary vreg.
12001 // FIXME: This isn't very clean... it would be nice to make this more
12002 // general.
12003 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12004 if (Reg.isVirtual()) {
12005 FuncInfo->ValueMap[&Arg] = Reg;
12006 continue;
12007 }
12008 }
12009 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12010 FuncInfo->InitializeRegForValue(&Arg);
12011 SDB->CopyToExportRegsIfNeeded(&Arg);
12012 }
12013 }
12014
12015 if (!Chains.empty()) {
12016 Chains.push_back(NewRoot);
12017 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12018 }
12019
12020 DAG.setRoot(NewRoot);
12021
12022 assert(i == InVals.size() && "Argument register count mismatch!");
12023
12024 // If any argument copy elisions occurred and we have debug info, update the
12025 // stale frame indices used in the dbg.declare variable info table.
12026 if (!ArgCopyElisionFrameIndexMap.empty()) {
12027 for (MachineFunction::VariableDbgInfo &VI :
12028 MF->getInStackSlotVariableDbgInfo()) {
12029 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12030 if (I != ArgCopyElisionFrameIndexMap.end())
12031 VI.updateStackSlot(I->second);
12032 }
12033 }
12034
12035 // Finally, if the target has anything special to do, allow it to do so.
12037}
12038
12039/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12040/// ensure constants are generated when needed. Remember the virtual registers
12041/// that need to be added to the Machine PHI nodes as input. We cannot just
12042/// directly add them, because expansion might result in multiple MBB's for one
12043/// BB. As such, the start of the BB might correspond to a different MBB than
12044/// the end.
12045void
12046SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12047 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12048
12049 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12050
12051 // Check PHI nodes in successors that expect a value to be available from this
12052 // block.
12053 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12054 if (!isa<PHINode>(SuccBB->begin())) continue;
12055 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12056
12057 // If this terminator has multiple identical successors (common for
12058 // switches), only handle each succ once.
12059 if (!SuccsHandled.insert(SuccMBB).second)
12060 continue;
12061
12063
12064 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12065 // nodes and Machine PHI nodes, but the incoming operands have not been
12066 // emitted yet.
12067 for (const PHINode &PN : SuccBB->phis()) {
12068 // Ignore dead phi's.
12069 if (PN.use_empty())
12070 continue;
12071
12072 // Skip empty types
12073 if (PN.getType()->isEmptyTy())
12074 continue;
12075
12076 Register Reg;
12077 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12078
12079 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12080 Register &RegOut = ConstantsOut[C];
12081 if (!RegOut) {
12082 RegOut = FuncInfo.CreateRegs(&PN);
12083 // We need to zero/sign extend ConstantInt phi operands to match
12084 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12085 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12086 if (auto *CI = dyn_cast<ConstantInt>(C))
12087 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12089 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12090 }
12091 Reg = RegOut;
12092 } else {
12094 FuncInfo.ValueMap.find(PHIOp);
12095 if (I != FuncInfo.ValueMap.end())
12096 Reg = I->second;
12097 else {
12098 assert(isa<AllocaInst>(PHIOp) &&
12099 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12100 "Didn't codegen value into a register!??");
12101 Reg = FuncInfo.CreateRegs(&PN);
12103 }
12104 }
12105
12106 // Remember that this register needs to added to the machine PHI node as
12107 // the input for this MBB.
12108 SmallVector<EVT, 4> ValueVTs;
12109 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12110 for (EVT VT : ValueVTs) {
12111 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12112 for (unsigned i = 0; i != NumRegisters; ++i)
12113 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12114 Reg += NumRegisters;
12115 }
12116 }
12117 }
12118
12119 ConstantsOut.clear();
12120}
12121
12122MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12124 if (++I == FuncInfo.MF->end())
12125 return nullptr;
12126 return &*I;
12127}
12128
12129/// During lowering new call nodes can be created (such as memset, etc.).
12130/// Those will become new roots of the current DAG, but complications arise
12131/// when they are tail calls. In such cases, the call lowering will update
12132/// the root, but the builder still needs to know that a tail call has been
12133/// lowered in order to avoid generating an additional return.
12134void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12135 // If the node is null, we do have a tail call.
12136 if (MaybeTC.getNode() != nullptr)
12137 DAG.setRoot(MaybeTC);
12138 else
12139 HasTailCall = true;
12140}
12141
12142void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12143 MachineBasicBlock *SwitchMBB,
12144 MachineBasicBlock *DefaultMBB) {
12145 MachineFunction *CurMF = FuncInfo.MF;
12146 MachineBasicBlock *NextMBB = nullptr;
12148 if (++BBI != FuncInfo.MF->end())
12149 NextMBB = &*BBI;
12150
12151 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12152
12153 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12154
12155 if (Size == 2 && W.MBB == SwitchMBB) {
12156 // If any two of the cases has the same destination, and if one value
12157 // is the same as the other, but has one bit unset that the other has set,
12158 // use bit manipulation to do two compares at once. For example:
12159 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12160 // TODO: This could be extended to merge any 2 cases in switches with 3
12161 // cases.
12162 // TODO: Handle cases where W.CaseBB != SwitchBB.
12163 CaseCluster &Small = *W.FirstCluster;
12164 CaseCluster &Big = *W.LastCluster;
12165
12166 if (Small.Low == Small.High && Big.Low == Big.High &&
12167 Small.MBB == Big.MBB) {
12168 const APInt &SmallValue = Small.Low->getValue();
12169 const APInt &BigValue = Big.Low->getValue();
12170
12171 // Check that there is only one bit different.
12172 APInt CommonBit = BigValue ^ SmallValue;
12173 if (CommonBit.isPowerOf2()) {
12174 SDValue CondLHS = getValue(Cond);
12175 EVT VT = CondLHS.getValueType();
12176 SDLoc DL = getCurSDLoc();
12177
12178 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12179 DAG.getConstant(CommonBit, DL, VT));
12180 SDValue Cond = DAG.getSetCC(
12181 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12182 ISD::SETEQ);
12183
12184 // Update successor info.
12185 // Both Small and Big will jump to Small.BB, so we sum up the
12186 // probabilities.
12187 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12188 if (BPI)
12189 addSuccessorWithProb(
12190 SwitchMBB, DefaultMBB,
12191 // The default destination is the first successor in IR.
12192 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12193 else
12194 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12195
12196 // Insert the true branch.
12197 SDValue BrCond =
12198 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12199 DAG.getBasicBlock(Small.MBB));
12200 // Insert the false branch.
12201 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12202 DAG.getBasicBlock(DefaultMBB));
12203
12204 DAG.setRoot(BrCond);
12205 return;
12206 }
12207 }
12208 }
12209
12210 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12211 // Here, we order cases by probability so the most likely case will be
12212 // checked first. However, two clusters can have the same probability in
12213 // which case their relative ordering is non-deterministic. So we use Low
12214 // as a tie-breaker as clusters are guaranteed to never overlap.
12215 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12216 [](const CaseCluster &a, const CaseCluster &b) {
12217 return a.Prob != b.Prob ?
12218 a.Prob > b.Prob :
12219 a.Low->getValue().slt(b.Low->getValue());
12220 });
12221
12222 // Rearrange the case blocks so that the last one falls through if possible
12223 // without changing the order of probabilities.
12224 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12225 --I;
12226 if (I->Prob > W.LastCluster->Prob)
12227 break;
12228 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12229 std::swap(*I, *W.LastCluster);
12230 break;
12231 }
12232 }
12233 }
12234
12235 // Compute total probability.
12236 BranchProbability DefaultProb = W.DefaultProb;
12237 BranchProbability UnhandledProbs = DefaultProb;
12238 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12239 UnhandledProbs += I->Prob;
12240
12241 MachineBasicBlock *CurMBB = W.MBB;
12242 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12243 bool FallthroughUnreachable = false;
12244 MachineBasicBlock *Fallthrough;
12245 if (I == W.LastCluster) {
12246 // For the last cluster, fall through to the default destination.
12247 Fallthrough = DefaultMBB;
12248 FallthroughUnreachable = isa<UnreachableInst>(
12249 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12250 } else {
12251 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12252 CurMF->insert(BBI, Fallthrough);
12253 // Put Cond in a virtual register to make it available from the new blocks.
12255 }
12256 UnhandledProbs -= I->Prob;
12257
12258 switch (I->Kind) {
12259 case CC_JumpTable: {
12260 // FIXME: Optimize away range check based on pivot comparisons.
12261 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12262 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12263
12264 // The jump block hasn't been inserted yet; insert it here.
12265 MachineBasicBlock *JumpMBB = JT->MBB;
12266 CurMF->insert(BBI, JumpMBB);
12267
12268 auto JumpProb = I->Prob;
12269 auto FallthroughProb = UnhandledProbs;
12270
12271 // If the default statement is a target of the jump table, we evenly
12272 // distribute the default probability to successors of CurMBB. Also
12273 // update the probability on the edge from JumpMBB to Fallthrough.
12274 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12275 SE = JumpMBB->succ_end();
12276 SI != SE; ++SI) {
12277 if (*SI == DefaultMBB) {
12278 JumpProb += DefaultProb / 2;
12279 FallthroughProb -= DefaultProb / 2;
12280 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12281 JumpMBB->normalizeSuccProbs();
12282 break;
12283 }
12284 }
12285
12286 // If the default clause is unreachable, propagate that knowledge into
12287 // JTH->FallthroughUnreachable which will use it to suppress the range
12288 // check.
12289 //
12290 // However, don't do this if we're doing branch target enforcement,
12291 // because a table branch _without_ a range check can be a tempting JOP
12292 // gadget - out-of-bounds inputs that are impossible in correct
12293 // execution become possible again if an attacker can influence the
12294 // control flow. So if an attacker doesn't already have a BTI bypass
12295 // available, we don't want them to be able to get one out of this
12296 // table branch.
12297 if (FallthroughUnreachable) {
12298 Function &CurFunc = CurMF->getFunction();
12299 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12300 JTH->FallthroughUnreachable = true;
12301 }
12302
12303 if (!JTH->FallthroughUnreachable)
12304 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12305 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12306 CurMBB->normalizeSuccProbs();
12307
12308 // The jump table header will be inserted in our current block, do the
12309 // range check, and fall through to our fallthrough block.
12310 JTH->HeaderBB = CurMBB;
12311 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12312
12313 // If we're in the right place, emit the jump table header right now.
12314 if (CurMBB == SwitchMBB) {
12315 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12316 JTH->Emitted = true;
12317 }
12318 break;
12319 }
12320 case CC_BitTests: {
12321 // FIXME: Optimize away range check based on pivot comparisons.
12322 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12323
12324 // The bit test blocks haven't been inserted yet; insert them here.
12325 for (BitTestCase &BTC : BTB->Cases)
12326 CurMF->insert(BBI, BTC.ThisBB);
12327
12328 // Fill in fields of the BitTestBlock.
12329 BTB->Parent = CurMBB;
12330 BTB->Default = Fallthrough;
12331
12332 BTB->DefaultProb = UnhandledProbs;
12333 // If the cases in bit test don't form a contiguous range, we evenly
12334 // distribute the probability on the edge to Fallthrough to two
12335 // successors of CurMBB.
12336 if (!BTB->ContiguousRange) {
12337 BTB->Prob += DefaultProb / 2;
12338 BTB->DefaultProb -= DefaultProb / 2;
12339 }
12340
12341 if (FallthroughUnreachable)
12342 BTB->FallthroughUnreachable = true;
12343
12344 // If we're in the right place, emit the bit test header right now.
12345 if (CurMBB == SwitchMBB) {
12346 visitBitTestHeader(*BTB, SwitchMBB);
12347 BTB->Emitted = true;
12348 }
12349 break;
12350 }
12351 case CC_Range: {
12352 const Value *RHS, *LHS, *MHS;
12353 ISD::CondCode CC;
12354 if (I->Low == I->High) {
12355 // Check Cond == I->Low.
12356 CC = ISD::SETEQ;
12357 LHS = Cond;
12358 RHS=I->Low;
12359 MHS = nullptr;
12360 } else {
12361 // Check I->Low <= Cond <= I->High.
12362 CC = ISD::SETLE;
12363 LHS = I->Low;
12364 MHS = Cond;
12365 RHS = I->High;
12366 }
12367
12368 // If Fallthrough is unreachable, fold away the comparison.
12369 if (FallthroughUnreachable)
12370 CC = ISD::SETTRUE;
12371
12372 // The false probability is the sum of all unhandled cases.
12373 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12374 getCurSDLoc(), I->Prob, UnhandledProbs);
12375
12376 if (CurMBB == SwitchMBB)
12377 visitSwitchCase(CB, SwitchMBB);
12378 else
12379 SL->SwitchCases.push_back(CB);
12380
12381 break;
12382 }
12383 }
12384 CurMBB = Fallthrough;
12385 }
12386}
12387
12388void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12389 const SwitchWorkListItem &W,
12390 Value *Cond,
12391 MachineBasicBlock *SwitchMBB) {
12392 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12393 "Clusters not sorted?");
12394 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12395
12396 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12397 SL->computeSplitWorkItemInfo(W);
12398
12399 // Use the first element on the right as pivot since we will make less-than
12400 // comparisons against it.
12401 CaseClusterIt PivotCluster = FirstRight;
12402 assert(PivotCluster > W.FirstCluster);
12403 assert(PivotCluster <= W.LastCluster);
12404
12405 CaseClusterIt FirstLeft = W.FirstCluster;
12406 CaseClusterIt LastRight = W.LastCluster;
12407
12408 const ConstantInt *Pivot = PivotCluster->Low;
12409
12410 // New blocks will be inserted immediately after the current one.
12412 ++BBI;
12413
12414 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12415 // we can branch to its destination directly if it's squeezed exactly in
12416 // between the known lower bound and Pivot - 1.
12417 MachineBasicBlock *LeftMBB;
12418 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12419 FirstLeft->Low == W.GE &&
12420 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12421 LeftMBB = FirstLeft->MBB;
12422 } else {
12423 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12424 FuncInfo.MF->insert(BBI, LeftMBB);
12425 WorkList.push_back(
12426 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12427 // Put Cond in a virtual register to make it available from the new blocks.
12429 }
12430
12431 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12432 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12433 // directly if RHS.High equals the current upper bound.
12434 MachineBasicBlock *RightMBB;
12435 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12436 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12437 RightMBB = FirstRight->MBB;
12438 } else {
12439 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12440 FuncInfo.MF->insert(BBI, RightMBB);
12441 WorkList.push_back(
12442 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12443 // Put Cond in a virtual register to make it available from the new blocks.
12445 }
12446
12447 // Create the CaseBlock record that will be used to lower the branch.
12448 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12449 getCurSDLoc(), LeftProb, RightProb);
12450
12451 if (W.MBB == SwitchMBB)
12452 visitSwitchCase(CB, SwitchMBB);
12453 else
12454 SL->SwitchCases.push_back(CB);
12455}
12456
12457// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12458// from the swith statement.
12460 BranchProbability PeeledCaseProb) {
12461 if (PeeledCaseProb == BranchProbability::getOne())
12463 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12464
12465 uint32_t Numerator = CaseProb.getNumerator();
12466 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12467 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12468}
12469
12470// Try to peel the top probability case if it exceeds the threshold.
12471// Return current MachineBasicBlock for the switch statement if the peeling
12472// does not occur.
12473// If the peeling is performed, return the newly created MachineBasicBlock
12474// for the peeled switch statement. Also update Clusters to remove the peeled
12475// case. PeeledCaseProb is the BranchProbability for the peeled case.
12476MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12477 const SwitchInst &SI, CaseClusterVector &Clusters,
12478 BranchProbability &PeeledCaseProb) {
12479 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12480 // Don't perform if there is only one cluster or optimizing for size.
12481 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12482 TM.getOptLevel() == CodeGenOptLevel::None ||
12483 SwitchMBB->getParent()->getFunction().hasMinSize())
12484 return SwitchMBB;
12485
12486 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12487 unsigned PeeledCaseIndex = 0;
12488 bool SwitchPeeled = false;
12489 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12490 CaseCluster &CC = Clusters[Index];
12491 if (CC.Prob < TopCaseProb)
12492 continue;
12493 TopCaseProb = CC.Prob;
12494 PeeledCaseIndex = Index;
12495 SwitchPeeled = true;
12496 }
12497 if (!SwitchPeeled)
12498 return SwitchMBB;
12499
12500 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12501 << TopCaseProb << "\n");
12502
12503 // Record the MBB for the peeled switch statement.
12504 MachineFunction::iterator BBI(SwitchMBB);
12505 ++BBI;
12506 MachineBasicBlock *PeeledSwitchMBB =
12507 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12508 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12509
12510 ExportFromCurrentBlock(SI.getCondition());
12511 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12512 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12513 nullptr, nullptr, TopCaseProb.getCompl()};
12514 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12515
12516 Clusters.erase(PeeledCaseIt);
12517 for (CaseCluster &CC : Clusters) {
12518 LLVM_DEBUG(
12519 dbgs() << "Scale the probablity for one cluster, before scaling: "
12520 << CC.Prob << "\n");
12521 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12522 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12523 }
12524 PeeledCaseProb = TopCaseProb;
12525 return PeeledSwitchMBB;
12526}
12527
12528void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12529 // Extract cases from the switch.
12530 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12531 CaseClusterVector Clusters;
12532 Clusters.reserve(SI.getNumCases());
12533 for (auto I : SI.cases()) {
12534 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12535 const ConstantInt *CaseVal = I.getCaseValue();
12536 BranchProbability Prob =
12537 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12538 : BranchProbability(1, SI.getNumCases() + 1);
12539 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12540 }
12541
12542 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12543
12544 // Cluster adjacent cases with the same destination. We do this at all
12545 // optimization levels because it's cheap to do and will make codegen faster
12546 // if there are many clusters.
12547 sortAndRangeify(Clusters);
12548
12549 // The branch probablity of the peeled case.
12550 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12551 MachineBasicBlock *PeeledSwitchMBB =
12552 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12553
12554 // If there is only the default destination, jump there directly.
12555 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12556 if (Clusters.empty()) {
12557 assert(PeeledSwitchMBB == SwitchMBB);
12558 SwitchMBB->addSuccessor(DefaultMBB);
12559 if (DefaultMBB != NextBlock(SwitchMBB)) {
12560 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12561 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12562 }
12563 return;
12564 }
12565
12566 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12567 DAG.getBFI());
12568 SL->findBitTestClusters(Clusters, &SI);
12569
12570 LLVM_DEBUG({
12571 dbgs() << "Case clusters: ";
12572 for (const CaseCluster &C : Clusters) {
12573 if (C.Kind == CC_JumpTable)
12574 dbgs() << "JT:";
12575 if (C.Kind == CC_BitTests)
12576 dbgs() << "BT:";
12577
12578 C.Low->getValue().print(dbgs(), true);
12579 if (C.Low != C.High) {
12580 dbgs() << '-';
12581 C.High->getValue().print(dbgs(), true);
12582 }
12583 dbgs() << ' ';
12584 }
12585 dbgs() << '\n';
12586 });
12587
12588 assert(!Clusters.empty());
12589 SwitchWorkList WorkList;
12590 CaseClusterIt First = Clusters.begin();
12591 CaseClusterIt Last = Clusters.end() - 1;
12592 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12593 // Scale the branchprobability for DefaultMBB if the peel occurs and
12594 // DefaultMBB is not replaced.
12595 if (PeeledCaseProb != BranchProbability::getZero() &&
12596 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12597 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12598 WorkList.push_back(
12599 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12600
12601 while (!WorkList.empty()) {
12602 SwitchWorkListItem W = WorkList.pop_back_val();
12603 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12604
12605 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12606 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12607 // For optimized builds, lower large range as a balanced binary tree.
12608 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12609 continue;
12610 }
12611
12612 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12613 }
12614}
12615
12616void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12617 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12618 auto DL = getCurSDLoc();
12619 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12620 setValue(&I, DAG.getStepVector(DL, ResultVT));
12621}
12622
12623void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12624 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12625 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12626
12627 SDLoc DL = getCurSDLoc();
12628 SDValue V = getValue(I.getOperand(0));
12629 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12630
12631 if (VT.isScalableVector()) {
12632 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12633 return;
12634 }
12635
12636 // Use VECTOR_SHUFFLE for the fixed-length vector
12637 // to maintain existing behavior.
12638 SmallVector<int, 8> Mask;
12639 unsigned NumElts = VT.getVectorMinNumElements();
12640 for (unsigned i = 0; i != NumElts; ++i)
12641 Mask.push_back(NumElts - 1 - i);
12642
12643 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12644}
12645
12646void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12647 unsigned Factor) {
12648 auto DL = getCurSDLoc();
12649 SDValue InVec = getValue(I.getOperand(0));
12650
12651 SmallVector<EVT, 4> ValueVTs;
12652 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12653 ValueVTs);
12654
12655 EVT OutVT = ValueVTs[0];
12656 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12657
12658 SmallVector<SDValue, 4> SubVecs(Factor);
12659 for (unsigned i = 0; i != Factor; ++i) {
12660 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12661 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12662 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12663 }
12664
12665 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12666 // from existing legalisation and combines.
12667 if (OutVT.isFixedLengthVector() && Factor == 2) {
12668 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12669 createStrideMask(0, 2, OutNumElts));
12670 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12671 createStrideMask(1, 2, OutNumElts));
12672 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12673 setValue(&I, Res);
12674 return;
12675 }
12676
12677 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12678 DAG.getVTList(ValueVTs), SubVecs);
12679 setValue(&I, Res);
12680}
12681
12682void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12683 unsigned Factor) {
12684 auto DL = getCurSDLoc();
12685 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12686 EVT InVT = getValue(I.getOperand(0)).getValueType();
12687 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12688
12689 SmallVector<SDValue, 8> InVecs(Factor);
12690 for (unsigned i = 0; i < Factor; ++i) {
12691 InVecs[i] = getValue(I.getOperand(i));
12692 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12693 "Expected VTs to be the same");
12694 }
12695
12696 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12697 // from existing legalisation and combines.
12698 if (OutVT.isFixedLengthVector() && Factor == 2) {
12699 unsigned NumElts = InVT.getVectorMinNumElements();
12700 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12701 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12702 createInterleaveMask(NumElts, 2)));
12703 return;
12704 }
12705
12706 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12707 SDValue Res =
12708 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12709
12711 for (unsigned i = 0; i < Factor; ++i)
12712 Results[i] = Res.getValue(i);
12713
12714 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12715 setValue(&I, Res);
12716}
12717
12718void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12719 SmallVector<EVT, 4> ValueVTs;
12720 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12721 ValueVTs);
12722 unsigned NumValues = ValueVTs.size();
12723 if (NumValues == 0) return;
12724
12725 SmallVector<SDValue, 4> Values(NumValues);
12726 SDValue Op = getValue(I.getOperand(0));
12727
12728 for (unsigned i = 0; i != NumValues; ++i)
12729 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12730 SDValue(Op.getNode(), Op.getResNo() + i));
12731
12733 DAG.getVTList(ValueVTs), Values));
12734}
12735
12736void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12737 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12738 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12739
12740 SDLoc DL = getCurSDLoc();
12741 SDValue V1 = getValue(I.getOperand(0));
12742 SDValue V2 = getValue(I.getOperand(1));
12743 int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue();
12744
12745 // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
12746 if (VT.isScalableVector()) {
12747 setValue(
12748 &I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2,
12749 DAG.getSignedConstant(
12750 Imm, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
12751 return;
12752 }
12753
12754 unsigned NumElts = VT.getVectorNumElements();
12755
12756 uint64_t Idx = (NumElts + Imm) % NumElts;
12757
12758 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12759 SmallVector<int, 8> Mask;
12760 for (unsigned i = 0; i < NumElts; ++i)
12761 Mask.push_back(Idx + i);
12762 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12763}
12764
12765// Consider the following MIR after SelectionDAG, which produces output in
12766// phyregs in the first case or virtregs in the second case.
12767//
12768// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12769// %5:gr32 = COPY $ebx
12770// %6:gr32 = COPY $edx
12771// %1:gr32 = COPY %6:gr32
12772// %0:gr32 = COPY %5:gr32
12773//
12774// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12775// %1:gr32 = COPY %6:gr32
12776// %0:gr32 = COPY %5:gr32
12777//
12778// Given %0, we'd like to return $ebx in the first case and %5 in the second.
12779// Given %1, we'd like to return $edx in the first case and %6 in the second.
12780//
12781// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12782// to a single virtreg (such as %0). The remaining outputs monotonically
12783// increase in virtreg number from there. If a callbr has no outputs, then it
12784// should not have a corresponding callbr landingpad; in fact, the callbr
12785// landingpad would not even be able to refer to such a callbr.
12787 MachineInstr *MI = MRI.def_begin(Reg)->getParent();
12788 // There is definitely at least one copy.
12789 assert(MI->getOpcode() == TargetOpcode::COPY &&
12790 "start of copy chain MUST be COPY");
12791 Reg = MI->getOperand(1).getReg();
12792
12793 // If the copied register in the first copy must be virtual.
12794 assert(Reg.isVirtual() && "expected COPY of virtual register");
12795 MI = MRI.def_begin(Reg)->getParent();
12796
12797 // There may be an optional second copy.
12798 if (MI->getOpcode() == TargetOpcode::COPY) {
12799 assert(Reg.isVirtual() && "expected COPY of virtual register");
12800 Reg = MI->getOperand(1).getReg();
12801 assert(Reg.isPhysical() && "expected COPY of physical register");
12802 } else {
12803 // The start of the chain must be an INLINEASM_BR.
12804 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12805 "end of copy chain MUST be INLINEASM_BR");
12806 }
12807
12808 return Reg;
12809}
12810
12811// We must do this walk rather than the simpler
12812// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
12813// otherwise we will end up with copies of virtregs only valid along direct
12814// edges.
12815void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
12816 SmallVector<EVT, 8> ResultVTs;
12817 SmallVector<SDValue, 8> ResultValues;
12818 const auto *CBR =
12819 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
12820
12821 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12822 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
12823 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
12824
12825 Register InitialDef = FuncInfo.ValueMap[CBR];
12826 SDValue Chain = DAG.getRoot();
12827
12828 // Re-parse the asm constraints string.
12829 TargetLowering::AsmOperandInfoVector TargetConstraints =
12830 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
12831 for (auto &T : TargetConstraints) {
12832 SDISelAsmOperandInfo OpInfo(T);
12833 if (OpInfo.Type != InlineAsm::isOutput)
12834 continue;
12835
12836 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
12837 // individual constraint.
12838 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
12839
12840 switch (OpInfo.ConstraintType) {
12843 // Fill in OpInfo.AssignedRegs.Regs.
12844 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
12845
12846 // getRegistersForValue may produce 1 to many registers based on whether
12847 // the OpInfo.ConstraintVT is legal on the target or not.
12848 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
12849 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
12850 if (OriginalDef.isPhysical())
12851 FuncInfo.MBB->addLiveIn(OriginalDef);
12852 // Update the assigned registers to use the original defs.
12853 Reg = OriginalDef;
12854 }
12855
12856 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
12857 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
12858 ResultValues.push_back(V);
12859 ResultVTs.push_back(OpInfo.ConstraintVT);
12860 break;
12861 }
12863 SDValue Flag;
12864 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
12865 OpInfo, DAG);
12866 ++InitialDef;
12867 ResultValues.push_back(V);
12868 ResultVTs.push_back(OpInfo.ConstraintVT);
12869 break;
12870 }
12871 default:
12872 break;
12873 }
12874 }
12876 DAG.getVTList(ResultVTs), ResultValues);
12877 setValue(&I, V);
12878}
unsigned const MachineRegisterInfo * MRI
return SDValue()
static unsigned getIntrinsicID(const SDNode *N)
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Function Alias Analysis Results
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI)
Returns an AttributeList representing the attributes applied to the return value of the given call.
Definition FastISel.cpp:942
#define Check(C,...)
Hexagon Common GEP
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
static void getRegistersForValue(MachineFunction &MF, MachineIRBuilder &MIRBuilder, GISelAsmOperandInfo &OpInfo, GISelAsmOperandInfo &RefOpInfo)
Assign virtual/physical registers for the specified register operand.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
lazy value info
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
Machine Check Debug Module
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static const Function * getCalledFunction(const Value *V)
This file provides utility analysis objects describing memory locations.
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
#define T
#define T1
static 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
OptimizedStructLayoutField Field
#define P(N)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static bool hasOnlySelectUsers(const Value *Cond)
static SDValue getLoadStackGuard(SelectionDAG &DAG, const SDLoc &DL, SDValue &Chain)
Create a LOAD_STACK_GUARD node, and let it carry the target specific global variable if there exists ...
static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index, SDValue &Scale, SelectionDAGBuilder *SDB, const BasicBlock *CurBB, uint64_t ElemSize)
static void failForInvalidBundles(const CallBase &I, StringRef Name, ArrayRef< uint32_t > AllowedBundles)
static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx, const SDLoc &DL, SmallVectorImpl< SDValue > &Ops, SelectionDAGBuilder &Builder)
Add a stack map intrinsic call's live variable operands to a stackmap or patchpoint target node's ope...
static const unsigned MaxParallelChains
static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
visitPow - Lower a pow intrinsic.
static const CallBase * FindPreallocatedCall(const Value *PreallocatedSetup)
Given a @llvm.call.preallocated.setup, return the corresponding preallocated call.
static cl::opt< unsigned > SwitchPeelThreshold("switch-peel-threshold", cl::Hidden, cl::init(66), cl::desc("Set the case probability threshold for peeling the case from a " "switch statement. A value greater than 100 will void this " "optimization"))
static cl::opt< bool > InsertAssertAlign("insert-assert-align", cl::init(true), cl::desc("Insert the experimental `assertalign` node."), cl::ReallyHidden)
static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin)
static bool handleDanglingVariadicDebugInfo(SelectionDAG &DAG, DILocalVariable *Variable, DebugLoc DL, unsigned Order, SmallVectorImpl< Value * > &Values, DIExpression *Expression)
static unsigned findMatchingInlineAsmOperand(unsigned OperandNo, const std::vector< SDValue > &AsmNodeOperands)
static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo, SDISelAsmOperandInfo &MatchingOpInfo, SelectionDAG &DAG)
Make sure that the output operand OpInfo and its corresponding input operand MatchingOpInfo have comp...
static void findUnwindDestinations(FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB, BranchProbability Prob, SmallVectorImpl< std::pair< MachineBasicBlock *, BranchProbability > > &UnwindDests)
When an invoke or a cleanupret unwinds to the next EH pad, there are many places it could ultimately ...
static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic)
static BranchProbability scaleCaseProbality(BranchProbability CaseProb, BranchProbability PeeledCaseProb)
static SDValue expandExp2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp2 - Lower an exp2 intrinsic.
static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue Scale, SelectionDAG &DAG, const TargetLowering &TLI)
static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt, const SDLoc &dl)
getF32Constant - Get 32-bit floating point constant.
static SDValue widenVectorToPartType(SelectionDAG &DAG, SDValue Val, const SDLoc &DL, EVT PartVT)
static SDValue expandLog10(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog10 - Lower a log10 intrinsic.
DenseMap< const Argument *, std::pair< const AllocaInst *, const StoreInst * > > ArgCopyElisionMapTy
static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv)
getCopyToPartsVector - Create a series of nodes that contain the specified value split into legal par...
static void getUnderlyingArgRegs(SmallVectorImpl< std::pair< Register, TypeSize > > &Regs, const SDValue &N)
static void getCopyToParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv=std::nullopt, ISD::NodeType ExtendKind=ISD::ANY_EXTEND)
getCopyToParts - Create a series of nodes that contain the specified value split into legal parts.
static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, SelectionDAGBuilder &Builder)
static SDValue expandLog2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog2 - Lower a log2 intrinsic.
static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location, SDISelAsmOperandInfo &OpInfo, SelectionDAG &DAG)
Get a direct memory input to behave well as an indirect operand.
static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel)
isOnlyUsedInEntryBlock - If the specified argument is only used in the entry block,...
static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V, const Twine &ErrMsg)
static bool collectInstructionDeps(SmallMapVector< const Instruction *, bool, 8 > *Deps, const Value *V, SmallMapVector< const Instruction *, bool, 8 > *Necessary=nullptr, unsigned Depth=0)
static void findArgumentCopyElisionCandidates(const DataLayout &DL, FunctionLoweringInfo *FuncInfo, ArgCopyElisionMapTy &ArgCopyElisionCandidates)
Scan the entry block of the function in FuncInfo for arguments that look like copies into a local all...
static bool isFunction(SDValue Op)
static SDValue GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, const SDLoc &dl)
GetExponent - Get the exponent:
static Register FollowCopyChain(MachineRegisterInfo &MRI, Register Reg)
static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS, SelectionDAG &DAG)
ExpandPowI - Expand a llvm.powi intrinsic.
static SDValue expandLog(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog - Lower a log intrinsic.
static SDValue getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC=std::nullopt, std::optional< ISD::NodeType > AssertOp=std::nullopt)
getCopyFromParts - Create a value that contains the specified legal parts combined into the value the...
static SDValue getLimitedPrecisionExp2(SDValue t0, const SDLoc &dl, SelectionDAG &DAG)
static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl)
GetSignificand - Get the significand and build it into a floating-point number with exponent of 1:
static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp - Lower an exp intrinsic.
static const MDNode * getRangeMetadata(const Instruction &I)
static cl::opt< unsigned, true > LimitFPPrecision("limit-float-precision", cl::desc("Generate low-precision inline sequences " "for some float libcalls"), cl::location(LimitFloatPrecision), cl::Hidden, cl::init(0))
static void tryToElideArgumentCopy(FunctionLoweringInfo &FuncInfo, SmallVectorImpl< SDValue > &Chains, DenseMap< int, int > &ArgCopyElisionFrameIndexMap, SmallPtrSetImpl< const Instruction * > &ElidedArgCopyInstrs, ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg, ArrayRef< SDValue > ArgVals, bool &ArgHasUses)
Try to elide argument copies from memory into a local alloca.
static unsigned LimitFloatPrecision
LimitFloatPrecision - Generate low-precision inline sequences for some float libcalls (6,...
static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC)
getCopyFromPartsVector - Create a value that contains the specified legal parts combined into the val...
static bool InBlock(const Value *V, const BasicBlock *BB)
static 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 TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
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
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Check if an argument has a given attribute.
Definition Function.cpp:339
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h: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,...
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:361
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This class represents a no-op cast from one type to another.
The address of a basic block.
Definition Constants.h:899
Conditional or Unconditional Branch instruction.
Analysis providing branch probability information.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static uint32_t getDenominator()
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
BranchProbability getCompl() const
static BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
CallingConv::ID getCallingConv() const
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI bool isTailCall() const
Tests if this call site is marked as a tail call.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition InstrTypes.h: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)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:214
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
A signed pointer, in the ptrauth sense.
Definition Constants.h:1032
uint64_t getZExtValue() const
Constant Vector Declarations.
Definition Constants.h:517
This is an important base class in LLVM.
Definition Constant.h:43
This is the common base class for constrained floating point intrinsics.
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI unsigned getNonMetadataArgCount() const
DWARF expression.
LLVM_ABI bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B)
Check if fragments overlap between a pair of FragmentInfos.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI std::optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
LLVM_ABI uint64_t getNumLocationOperands() const
Return the number of unique location operands referred to (via DW_OP_LLVM_arg) in this expression; th...
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
static LLVM_ABI const DIExpression * convertToUndefExpression(const DIExpression *Expr)
Removes all elements from Expr that do not apply to an undef debug value, which includes every operat...
static LLVM_ABI DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
static LLVM_ABI DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
bool isValidLocationForIntrinsic(const DILocation *DL) const
Check that a location is valid for this variable.
Base class for variables.
LLVM_ABI std::optional< uint64_t > getSizeInBits() const
Determines the size of the variable's type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isBigEndian() const
Definition DataLayout.h:199
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:165
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
bool empty() const
Definition DenseMap.h:107
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:214
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:112
Diagnostic information for inline asm reporting.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:64
An instruction for ordering other memory operations.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp: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
MachineBasicBlock * getMBB(const BasicBlock *BB) const
DenseMap< const AllocaInst *, int > StaticAllocaMap
StaticAllocaMap - Keep track of frame indices for fixed sized allocas in the entry block.
const LiveOutInfo * GetLiveOutRegInfo(Register Reg)
GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the register is a PHI destinat...
MachineBasicBlock * MBB
MBB - The current block.
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Type * getParamType(unsigned i) const
Parameter type accessors.
Type * getReturnType() const
Data structure describing the variable locations in a function.
const BasicBlock & getEntryBlock() const
Definition Function.h:807
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:244
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition Function.cpp:739
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:270
Constant * getPersonalityFn() const
Get the personality function associated with this function.
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:249
size_t arg_size() const
Definition Function.h:899
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
Garbage collection metadata for a single function.
Definition GCMetadata.h:80
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
bool hasDLLImportStorageClass() const
Module * getParent()
Get the module that this global value is contained inside of...
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
This instruction inserts a struct field of array element value into an aggregate value.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
@ MIN_INT_BITS
Minimum number of bits that can be specified.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
A helper class to return the specified delimiter string after the first invocation of operator String...
An instruction for reading from memory.
static LocationSize precise(uint64_t Value)
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI MCSymbol * getOrCreateFrameAllocSymbol(const Twine &FuncName, unsigned Idx)
Gets a symbol that will be defined to the final stack offset of a local variable after codegen.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h: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.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void setIsEHContTarget(bool V=true)
Indicates if this is a target of Windows EH Continuation Guard.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
MachineInstrBundleIterator< MachineInstr > iterator
void setIsEHScopeEntry(bool V=true)
Indicates if this is the entry block of an EH scope, i.e., the block that that used to have a catchpa...
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
int getStackProtectorIndex() const
Return the index for the stack protector object.
void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased)
Set "maybe pointed to by an LLVM IR value" for an object.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
void setFunctionContextIndex(int I)
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site)
Map the begin label for a call site.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD)
Record annotations associated with a particular label.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
void setHasEHContTarget(bool V)
void addInvoke(MachineBasicBlock *LandingPad, MCSymbol *BeginLabel, MCSymbol *EndLabel)
Provide the begin and end labels of an invoke style call and associate it with a try landing pad bloc...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
Representation of each machine instruction.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI MCRegister getLiveInPhysReg(Register VReg) const
getLiveInPhysReg - If VReg is a live-in virtual register, return the corresponding live-in physical r...
An SDNode that represents everything that will be needed to construct a MachineInstr.
bool contains(const KeyT &Key) const
Definition MapVector.h:137
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:107
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
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h: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.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getOpcode() const
SelectionDAGBuilder - This is the common target-independent lowering implementation that is parameter...
SDValue getValue(const Value *V)
getValue - Return an SDValue for the given Value.
DenseMap< const Constant *, Register > ConstantsOut
void addDanglingDebugInfo(SmallVectorImpl< Value * > &Values, DILocalVariable *Var, DIExpression *Expr, bool IsVariadic, DebugLoc DL, unsigned Order)
Register a dbg_value which relies on a Value which we have not yet seen.
void visitDbgInfo(const Instruction &I)
void clearDanglingDebugInfo()
Clear the dangling debug information map.
void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall, bool IsMustTailCall, const BasicBlock *EHPadBB=nullptr, const TargetLowering::PtrAuthInfo *PAI=nullptr)
void clear()
Clear out the current SelectionDAG and the associated state and prepare this SelectionDAGBuilder obje...
void visitBitTestHeader(SwitchCG::BitTestBlock &B, MachineBasicBlock *SwitchBB)
visitBitTestHeader - This function emits necessary code to produce value suitable for "bit tests"
void LowerStatepoint(const GCStatepointInst &I, const BasicBlock *EHPadBB=nullptr)
std::unique_ptr< SDAGSwitchLowering > SL
SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, SDValue Op)
bool HasTailCall
This is set to true if a call in the current block has been translated as a tail call.
bool ShouldEmitAsBranches(const std::vector< SwitchCG::CaseBlock > &Cases)
If the set of cases should be emitted as a series of branches, return true.
void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
EmitBranchForMergedCondition - Helper method for FindMergedConditions.
void LowerDeoptimizeCall(const CallInst *CI)
void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB)
SwiftErrorValueTracking & SwiftError
Information about the swifterror values used throughout the function.
SDValue getNonRegisterValue(const Value *V)
getNonRegisterValue - Return an SDValue for the given Value, but don't look in FuncInfo....
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"
bool canTailCall(const CallBase &CB) const
void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, AttributeSet RetAttrs, bool IsPatchPoint)
Populate a CallLowerinInfo (into CLI) based on the properties of the call being lowered.
void CopyValueToVirtualRegister(const Value *V, Register Reg, ISD::NodeType ExtendType=ISD::ANY_EXTEND)
void salvageUnresolvedDbgValue(const Value *V, DanglingDebugInfo &DDI)
For the given dangling debuginfo record, perform last-ditch efforts to resolve the debuginfo to somet...
SmallVector< SDValue, 8 > PendingLoads
Loads are not emitted to the program immediately.
GCFunctionInfo * GFI
Garbage collection metadata for the function.
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
MachineRegisterInfo * RegInfo
std::unique_ptr< SwiftErrorValueTracking > SwiftError
virtual void emitFunctionEntryCode()
std::unique_ptr< SelectionDAGBuilder > SDB
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrnlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const
virtual std::pair< SDValue, SDValue > 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
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SDValue getExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT, unsigned Opcode)
Convert Op, which must be of integer type, to the integer type VT, by either any/sign/zero-extending ...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
const TargetSubtargetInfo & getSubtarget() const
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
const TargetLowering & getTargetLoweringInfo() const
static constexpr unsigned MaxRecursionDepth
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
const DataLayout & getDataLayout() const
SDValue getTargetFrameIndex(int FI, EVT VT)
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
MachineFunction & getMachineFunction() const
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVMContext * getContext() const
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void swap(SmallVectorImpl &RHS)
void resize(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Encapsulates all of the information needed to generate a stack protector check, and signals to isel w...
MachineBasicBlock * getSuccessMBB()
MachineBasicBlock * getFailureMBB()
MachineBasicBlock * getParentMBB()
bool shouldEmitFunctionBasedCheckStackProtector() const
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h: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
Multiway switch.
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Provides information about what library functions are available for the current target.
virtual Align getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Returns the desired alignment for ByVal or InAlloca aggregate function arguments in the caller parame...
virtual bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, EVT) const
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
EVT getMemValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
virtual bool useStackGuardXorFP() const
If this function returns true, stack protection checks should XOR the frame pointer (or whichever poi...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
virtual bool isLegalScaleForGatherScatter(uint64_t Scale, uint64_t ElemSize) const
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
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.
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 AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, const CallBase &Call) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const
This callback is invoked for operations that are unsupported by the target, which are registered to u...
virtual bool functionArgumentNeedsConsecutiveRegisters(Type *Ty, CallingConv::ID CallConv, bool isVarArg, const DataLayout &DL) const
For some targets, an LLVM struct type must be broken down into multiple simple types,...
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo,...
virtual void CollectTargetIntrinsicOperands(const CallInst &I, SmallVectorImpl< SDValue > &Ops, SelectionDAG &DAG) const
virtual SDValue visitMaskedStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue Ptr, SDValue Val, SDValue Mask) const
virtual bool useLoadStackGuardNode(const Module &M) const
If this function returns true, SelectionDAGBuilder emits a LOAD_STACK_GUARD node when it is lowering ...
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
virtual void LowerOperationWrapper(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const
This callback is invoked by the type legalizer to legalize nodes with an illegal operand type but leg...
virtual bool isInlineAsmTargetBranch(const SmallVectorImpl< StringRef > &AsmStrs, unsigned OpNo) const
On x86, return true if the operand with index OpNo is a CALL or JUMP instruction, which can use eithe...
virtual MVT getJumpTableRegTy(const DataLayout &DL) const
virtual bool CanLowerReturn(CallingConv::ID, MachineFunction &, bool, const SmallVectorImpl< ISD::OutputArg > &, LLVMContext &, const Type *RetTy) const
This hook should be implemented to check whether the return values described by the Outs array can fi...
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
virtual TargetTransformInfo getTargetTransformInfo(const Function &F) const
Return a TargetTransformInfo for a given function.
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
unsigned getID() const
Return the register class ID number.
iterator begin() const
begin/end - Return all of the registers in this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
@ TCK_Latency
The latency of instruction.
LLVM_ABI bool hasConditionalLoadStoreForType(Type *Ty, bool IsStore) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
Definition Type.cpp:181
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:294
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:234
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:301
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_iterator op_begin()
Definition User.h:284
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
op_iterator op_end()
Definition User.h:286
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
LLVM_ABI CmpInst::Predicate getPredicate() const
This is the common base class for vector predication intrinsics.
static LLVM_ABI std::optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
LLVM_ABI MaybeAlign getPointerAlignment() const
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:701
bool use_empty() const
Definition Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1101
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
const ParentTy * getParent() const
Definition ilist_node.h:34
A raw_ostream that writes to an std::string.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:774
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:504
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ LOOP_DEPENDENCE_RAW_MASK
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition ISDOpcodes.h:163
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h: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
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:393
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:835
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ EH_SJLJ_SETUP_DISPATCH
OUTCHAIN = EH_SJLJ_SETUP_DISPATCH(INCHAIN) The target initializes the dispatch table here.
Definition ISDOpcodes.h:167
@ GlobalAddress
Definition ISDOpcodes.h:88
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:862
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:571
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:738
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition ISDOpcodes.h:508
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:400
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:151
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:826
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition ISDOpcodes.h:117
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:773
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h: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
@ 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
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:225
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:952
@ 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
@ 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
@ LOCAL_RECOVER
LOCAL_RECOVER - Represents the llvm.localrecover intrinsic.
Definition ISDOpcodes.h:130
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h: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
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:787
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:145
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:493
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:908
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:730
@ 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
@ 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
@ 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
@ 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
@ 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
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:521
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:611
@ 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
@ LOOP_DEPENDENCE_WAR_MASK
Set rounding mode.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Offsets
Offsets in bytes from the start of the input buffer.
std::pair< JumpTableHeader, JumpTable > JumpTableBlock
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::vector< CaseCluster > CaseClusterVector
@ CC_Range
A cluster of adjacent case labels with the same destination, or just one case.
@ CC_JumpTable
A cluster of cases suitable for jump table lowering.
@ CC_BitTests
A cluster of cases suitable for bit test lowering.
SmallVector< SwitchWorkListItem, 4 > SwitchWorkList
CaseClusterVector::iterator CaseClusterIt
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h: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
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:262
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
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1727
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:1685
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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
@ 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.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2138
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
auto cast_or_null(const Y &Val)
Definition Casting.h:720
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:1180
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
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
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:1734
LLVM_ABI llvm::SmallVector< int, 16 > createStrideMask(unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_FMINNUM
Unsigned maximum.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
detail::zippy< detail::zip_first, T, U, Args... > zip_first(T &&t, U &&u, Args &&...args)
zip iterator that, for the sake of efficiency, assumes the first iteratee to be the shortest.
Definition STLExtras.h:866
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
generic_gep_type_iterator<> gep_type_iterator
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
auto succ_size(const MachineBasicBlock *BB)
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition STLExtras.h:314
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred)
getFCmpCondCode - Return the ISD condition code corresponding to the given LLVM IR floating-point con...
Definition Analysis.cpp:207
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI Value * salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Ops, SmallVectorImpl< Value * > &AdditionalValues)
Definition Local.cpp:2274
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Global
Append to llvm.global_dtors.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
TargetTransformInfo TTI
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
LLVM_ABI llvm::SmallVector< int, 16 > createInterleaveMask(unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ And
Bitwise or logical AND of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h: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
ArrayRef(const T &OneElt) -> ArrayRef< T >
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
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:2122
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:1899
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:2110
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.
Definition Uniformity.h:20
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:180
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
#define N
#define NC
Definition regutils.h:42
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
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h: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
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.
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.
This struct represents the registers (physical or virtual) that a particular set of values is assigne...
SmallVector< std::pair< Register, TypeSize >, 4 > getRegsAndSizes() const
Return a list of registers and their sizes.
RegsForValue()=default
SmallVector< unsigned, 4 > RegCount
This list holds the number of registers for each value.
SmallVector< EVT, 4 > ValueVTs
The value types of the values, which may not be legal, and may need be promoted or synthesized from o...
SmallVector< Register, 4 > Regs
This list holds the registers assigned to the values.
void AddInlineAsmOperands(InlineAsm::Kind Code, bool HasMatching, unsigned MatchingIdx, const SDLoc &dl, SelectionDAG &DAG, std::vector< SDValue > &Ops) const
Add this value to the specified inlineasm node operand list.
SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr) const
Emit a series of CopyFromReg nodes that copies from this value and returns the result as a ValueVTs v...
SmallVector< MVT, 4 > RegVTs
The value types of the registers.
void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr, ISD::NodeType PreferredExtendType=ISD::ANY_EXTEND) const
Emit a series of CopyToReg nodes that copies the specified value into the registers specified by this...
std::optional< CallingConv::ID > CallConv
Records if this value needs to be treated in an ABI dependant manner, different to normal type legali...
bool occupiesMultipleRegs() const
Check if the total RegCount is greater than one.
These are IR-level optimization flags that may be propagated to SDNodes.
void copyFMF(const FPMathOperator &FPMO)
Propagate the fast-math-flags from an IR FPMathOperator.
void setUnpredictable(bool b)
bool hasAllowReassociation() const
void setNoUnsignedWrap(bool b)
void setNoSignedWrap(bool b)
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.
static CaseCluster range(const ConstantInt *Low, const ConstantInt *High, MachineBasicBlock *MBB, BranchProbability Prob)
This contains information for each constraint that we are lowering.
TargetLowering::ConstraintType ConstraintType
Information about the constraint code, e.g.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setConvergent(bool Value=true)
CallLoweringInfo & setCFIType(const ConstantInt *Type)
SmallVector< ISD::InputArg, 32 > Ins
Type * OrigRetTy
Original unlegalized return type.
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setIsPatchPoint(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setIsPreallocated(bool Value=true)
CallLoweringInfo & setConvergenceControlToken(SDValue Token)
SmallVector< ISD::OutputArg, 32 > Outs
Type * RetTy
Same as OrigRetTy, or partially legalized for soft float libcalls.
CallLoweringInfo & setChain(SDValue InChain)
CallLoweringInfo & setPtrAuth(PtrAuthInfo Value)
CallLoweringInfo & setCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList, AttributeSet ResultAttrs={})
This structure is used to pass arguments to makeLibCall function.
MakeLibCallOptions & setDiscardResult(bool Value=true)
This structure contains the information necessary for lowering pointer-authenticating indirect calls.
void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)