LLVM 23.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"
58#include "llvm/IR/Argument.h"
59#include "llvm/IR/Attributes.h"
60#include "llvm/IR/BasicBlock.h"
61#include "llvm/IR/CFG.h"
62#include "llvm/IR/CallingConv.h"
63#include "llvm/IR/Constant.h"
65#include "llvm/IR/Constants.h"
66#include "llvm/IR/DataLayout.h"
67#include "llvm/IR/DebugInfo.h"
72#include "llvm/IR/Function.h"
74#include "llvm/IR/InlineAsm.h"
75#include "llvm/IR/InstrTypes.h"
78#include "llvm/IR/Intrinsics.h"
79#include "llvm/IR/IntrinsicsAArch64.h"
80#include "llvm/IR/IntrinsicsAMDGPU.h"
81#include "llvm/IR/IntrinsicsWebAssembly.h"
82#include "llvm/IR/LLVMContext.h"
84#include "llvm/IR/Metadata.h"
85#include "llvm/IR/Module.h"
86#include "llvm/IR/Operator.h"
88#include "llvm/IR/Statepoint.h"
89#include "llvm/IR/Type.h"
90#include "llvm/IR/User.h"
91#include "llvm/IR/Value.h"
92#include "llvm/MC/MCContext.h"
97#include "llvm/Support/Debug.h"
105#include <cstddef>
106#include <limits>
107#include <optional>
108#include <tuple>
109
110using namespace llvm;
111using namespace PatternMatch;
112using namespace SwitchCG;
113
114#define DEBUG_TYPE "isel"
115
116/// LimitFloatPrecision - Generate low-precision inline sequences for
117/// some float libcalls (6, 8 or 12 bits).
118static unsigned LimitFloatPrecision;
119
120static cl::opt<bool>
121 InsertAssertAlign("insert-assert-align", cl::init(true),
122 cl::desc("Insert the experimental `assertalign` node."),
124
126 LimitFPPrecision("limit-float-precision",
127 cl::desc("Generate low-precision inline sequences "
128 "for some float libcalls"),
130 cl::init(0));
131
133 "switch-peel-threshold", cl::Hidden, cl::init(66),
134 cl::desc("Set the case probability threshold for peeling the case from a "
135 "switch statement. A value greater than 100 will void this "
136 "optimization"));
137
138// Limit the width of DAG chains. This is important in general to prevent
139// DAG-based analysis from blowing up. For example, alias analysis and
140// load clustering may not complete in reasonable time. It is difficult to
141// recognize and avoid this situation within each individual analysis, and
142// future analyses are likely to have the same behavior. Limiting DAG width is
143// the safe approach and will be especially important with global DAGs.
144//
145// MaxParallelChains default is arbitrarily high to avoid affecting
146// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
147// sequence over this should have been converted to llvm.memcpy by the
148// frontend. It is easy to induce this behavior with .ll code such as:
149// %buffer = alloca [4096 x i8]
150// %data = load [4096 x i8]* %argPtr
151// store [4096 x i8] %data, [4096 x i8]* %buffer
152static const unsigned MaxParallelChains = 64;
153
155 const SDValue *Parts, unsigned NumParts,
156 MVT PartVT, EVT ValueVT, const Value *V,
157 SDValue InChain,
158 std::optional<CallingConv::ID> CC);
159
160/// getCopyFromParts - Create a value that contains the specified legal parts
161/// combined into the value they represent. If the parts combine to a type
162/// larger than ValueVT then AssertOp can be used to specify whether the extra
163/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
164/// (ISD::AssertSext).
165static SDValue
166getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts,
167 unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V,
168 SDValue InChain,
169 std::optional<CallingConv::ID> CC = std::nullopt,
170 std::optional<ISD::NodeType> AssertOp = std::nullopt) {
171 // Let the target assemble the parts if it wants to
172 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
173 if (SDValue Val = TLI.joinRegisterPartsIntoValue(DAG, DL, Parts, NumParts,
174 PartVT, ValueVT, CC))
175 return Val;
176
177 if (ValueVT.isVector())
178 return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT, V,
179 InChain, CC);
180
181 assert(NumParts > 0 && "No parts to assemble!");
182 SDValue Val = Parts[0];
183
184 if (NumParts > 1) {
185 // Assemble the value from multiple parts.
186 if (ValueVT.isInteger()) {
187 unsigned PartBits = PartVT.getSizeInBits();
188 unsigned ValueBits = ValueVT.getSizeInBits();
189
190 // Assemble the power of 2 part.
191 unsigned RoundParts = llvm::bit_floor(NumParts);
192 unsigned RoundBits = PartBits * RoundParts;
193 EVT RoundVT = RoundBits == ValueBits ?
194 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
195 SDValue Lo, Hi;
196
197 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
198
199 if (RoundParts > 2) {
200 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, PartVT, HalfVT, V,
201 InChain);
202 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, RoundParts / 2,
203 PartVT, HalfVT, V, InChain);
204 } else {
205 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
206 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
207 }
208
209 if (DAG.getDataLayout().isBigEndian())
210 std::swap(Lo, Hi);
211
212 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
213
214 if (RoundParts < NumParts) {
215 // Assemble the trailing non-power-of-2 part.
216 unsigned OddParts = NumParts - RoundParts;
217 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
218 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts, OddParts, PartVT,
219 OddVT, V, InChain, CC);
220
221 // Combine the round and odd parts.
222 Lo = Val;
223 if (DAG.getDataLayout().isBigEndian())
224 std::swap(Lo, Hi);
225 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
226 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
227 Hi = DAG.getNode(
228 ISD::SHL, DL, TotalVT, Hi,
229 DAG.getShiftAmountConstant(Lo.getValueSizeInBits(), TotalVT, DL));
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(
664 ValueVT.changeVectorElementType(*DAG.getContext(), MVT::f16), Val);
665 } else if (PartEVT != ValueEVT) {
666 return SDValue();
667 }
668
669 // Widening a scalable vector to another scalable vector is done by inserting
670 // the vector into a larger undef one.
671 if (PartNumElts.isScalable())
672 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT),
673 Val, DAG.getVectorIdxConstant(0, DL));
674
675 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
676 // undef elements.
678 DAG.ExtractVectorElements(Val, Ops);
679 SDValue EltUndef = DAG.getUNDEF(PartEVT);
680 Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef);
681
682 // FIXME: Use CONCAT for 2x -> 4x.
683 return DAG.getBuildVector(PartVT, DL, Ops);
684}
685
686/// getCopyToPartsVector - Create a series of nodes that contain the specified
687/// value split into legal parts.
688static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL,
689 SDValue Val, SDValue *Parts, unsigned NumParts,
690 MVT PartVT, const Value *V,
691 std::optional<CallingConv::ID> CallConv) {
692 EVT ValueVT = Val.getValueType();
693 assert(ValueVT.isVector() && "Not a vector");
694 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
695 const bool IsABIRegCopy = CallConv.has_value();
696
697 if (NumParts == 1) {
698 EVT PartEVT = PartVT;
699 if (PartEVT == ValueVT) {
700 // Nothing to do.
701 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
702 // Bitconvert vector->vector case.
703 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
704 } else if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, PartVT)) {
705 Val = Widened;
706 } else if (PartVT.isVector() &&
708 ValueVT.getVectorElementType()) &&
709 PartEVT.getVectorElementCount() ==
710 ValueVT.getVectorElementCount()) {
711
712 // Promoted vector extract
713 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
714 } else if (PartEVT.isVector() &&
715 PartEVT.getVectorElementType() !=
716 ValueVT.getVectorElementType() &&
717 TLI.getTypeAction(*DAG.getContext(), ValueVT) ==
719 // Combination of widening and promotion.
720 EVT WidenVT =
722 PartVT.getVectorElementCount());
723 SDValue Widened = widenVectorToPartType(DAG, Val, DL, WidenVT);
724 Val = DAG.getAnyExtOrTrunc(Widened, DL, PartVT);
725 } else {
726 // Don't extract an integer from a float vector. This can happen if the
727 // FP type gets softened to integer and then promoted. The promotion
728 // prevents it from being picked up by the earlier bitcast case.
729 if (ValueVT.getVectorElementCount().isScalar() &&
730 (!ValueVT.isFloatingPoint() || !PartVT.isInteger())) {
731 // If we reach this condition and PartVT is FP, this means that
732 // ValueVT is also FP and both have a different size, otherwise we
733 // would have bitcasted them. Producing an EXTRACT_VECTOR_ELT here
734 // would be invalid since that would mean the smaller FP type has to
735 // be extended to the larger one.
736 if (PartVT.isFloatingPoint()) {
737 Val = DAG.getBitcast(ValueVT.getScalarType(), Val);
738 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
739 } else
740 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val,
741 DAG.getVectorIdxConstant(0, DL));
742 } else {
743 uint64_t ValueSize = ValueVT.getFixedSizeInBits();
744 assert(PartVT.getFixedSizeInBits() > ValueSize &&
745 "lossy conversion of vector to scalar type");
746 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
747 Val = DAG.getBitcast(IntermediateType, Val);
748 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
749 }
750 }
751
752 assert(Val.getValueType() == PartVT && "Unexpected vector part value type");
753 Parts[0] = Val;
754 return;
755 }
756
757 // Handle a multi-element vector.
758 EVT IntermediateVT;
759 MVT RegisterVT;
760 unsigned NumIntermediates;
761 unsigned NumRegs;
762 if (IsABIRegCopy) {
764 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT, NumIntermediates,
765 RegisterVT);
766 } else {
767 NumRegs =
768 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
769 NumIntermediates, RegisterVT);
770 }
771
772 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
773 NumParts = NumRegs; // Silence a compiler warning.
774 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
775
776 assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() &&
777 "Mixing scalable and fixed vectors when copying in parts");
778
779 std::optional<ElementCount> DestEltCnt;
780
781 if (IntermediateVT.isVector())
782 DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates;
783 else
784 DestEltCnt = ElementCount::getFixed(NumIntermediates);
785
786 EVT BuiltVectorTy = EVT::getVectorVT(
787 *DAG.getContext(), IntermediateVT.getScalarType(), *DestEltCnt);
788
789 if (ValueVT == BuiltVectorTy) {
790 // Nothing to do.
791 } else if (ValueVT.getSizeInBits() == BuiltVectorTy.getSizeInBits()) {
792 // Bitconvert vector->vector case.
793 Val = DAG.getNode(ISD::BITCAST, DL, BuiltVectorTy, Val);
794 } else {
795 if (BuiltVectorTy.getVectorElementType().bitsGT(
796 ValueVT.getVectorElementType())) {
797 // Integer promotion.
798 ValueVT = EVT::getVectorVT(*DAG.getContext(),
799 BuiltVectorTy.getVectorElementType(),
800 ValueVT.getVectorElementCount());
801 Val = DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
802 }
803
804 if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy)) {
805 Val = Widened;
806 }
807 }
808
809 assert(Val.getValueType() == BuiltVectorTy && "Unexpected vector value type");
810
811 // Split the vector into intermediate operands.
812 SmallVector<SDValue, 8> Ops(NumIntermediates);
813 for (unsigned i = 0; i != NumIntermediates; ++i) {
814 if (IntermediateVT.isVector()) {
815 // This does something sensible for scalable vectors - see the
816 // definition of EXTRACT_SUBVECTOR for further details.
817 unsigned IntermediateNumElts = IntermediateVT.getVectorMinNumElements();
818 Ops[i] =
819 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val,
820 DAG.getVectorIdxConstant(i * IntermediateNumElts, DL));
821 } else {
822 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val,
823 DAG.getVectorIdxConstant(i, DL));
824 }
825 }
826
827 // Split the intermediate operands into legal parts.
828 if (NumParts == NumIntermediates) {
829 // If the register was not expanded, promote or copy the value,
830 // as appropriate.
831 for (unsigned i = 0; i != NumParts; ++i)
832 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V, CallConv);
833 } else if (NumParts > 0) {
834 // If the intermediate type was expanded, split each the value into
835 // legal parts.
836 assert(NumIntermediates != 0 && "division by zero");
837 assert(NumParts % NumIntermediates == 0 &&
838 "Must expand into a divisible number of parts!");
839 unsigned Factor = NumParts / NumIntermediates;
840 for (unsigned i = 0; i != NumIntermediates; ++i)
841 getCopyToParts(DAG, DL, Ops[i], &Parts[i * Factor], Factor, PartVT, V,
842 CallConv);
843 }
844}
845
846static void failForInvalidBundles(const CallBase &I, StringRef Name,
847 ArrayRef<uint32_t> AllowedBundles) {
848 if (I.hasOperandBundlesOtherThan(AllowedBundles)) {
849 ListSeparator LS;
850 std::string Error;
852 for (unsigned i = 0, e = I.getNumOperandBundles(); i != e; ++i) {
853 OperandBundleUse U = I.getOperandBundleAt(i);
854 if (!is_contained(AllowedBundles, U.getTagID()))
855 OS << LS << U.getTagName();
856 }
858 Twine("cannot lower ", Name)
859 .concat(Twine(" with arbitrary operand bundles: ", Error)));
860 }
861}
862
864 EVT valuevt, std::optional<CallingConv::ID> CC)
865 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs),
866 RegCount(1, regs.size()), CallConv(CC) {}
867
869 const DataLayout &DL, Register Reg, Type *Ty,
870 std::optional<CallingConv::ID> CC) {
871 ComputeValueVTs(TLI, DL, Ty, ValueVTs);
872
873 CallConv = CC;
874
875 for (EVT ValueVT : ValueVTs) {
876 unsigned NumRegs =
878 ? TLI.getNumRegistersForCallingConv(Context, *CC, ValueVT)
879 : TLI.getNumRegisters(Context, ValueVT);
880 MVT RegisterVT =
882 ? TLI.getRegisterTypeForCallingConv(Context, *CC, ValueVT)
883 : TLI.getRegisterType(Context, ValueVT);
884 for (unsigned i = 0; i != NumRegs; ++i)
885 Regs.push_back(Reg + i);
886 RegVTs.push_back(RegisterVT);
887 RegCount.push_back(NumRegs);
888 Reg = Reg.id() + NumRegs;
889 }
890}
891
893 FunctionLoweringInfo &FuncInfo,
894 const SDLoc &dl, SDValue &Chain,
895 SDValue *Glue, const Value *V) const {
896 // A Value with type {} or [0 x %t] needs no registers.
897 if (ValueVTs.empty())
898 return SDValue();
899
900 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
901
902 // Assemble the legal parts into the final values.
903 SmallVector<SDValue, 4> Values(ValueVTs.size());
905 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
906 // Copy the legal parts from the registers.
907 EVT ValueVT = ValueVTs[Value];
908 unsigned NumRegs = RegCount[Value];
909 MVT RegisterVT = isABIMangled()
911 *DAG.getContext(), *CallConv, RegVTs[Value])
912 : RegVTs[Value];
913
914 Parts.resize(NumRegs);
915 for (unsigned i = 0; i != NumRegs; ++i) {
916 SDValue P;
917 if (!Glue) {
918 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
919 } else {
920 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Glue);
921 *Glue = P.getValue(2);
922 }
923
924 Chain = P.getValue(1);
925 Parts[i] = P;
926
927 // If the source register was virtual and if we know something about it,
928 // add an assert node.
929 if (!Regs[Part + i].isVirtual() || !RegisterVT.isInteger())
930 continue;
931
933 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
934 if (!LOI)
935 continue;
936
937 unsigned RegSize = RegisterVT.getScalarSizeInBits();
938 unsigned NumSignBits = LOI->NumSignBits;
939 unsigned NumZeroBits = LOI->Known.countMinLeadingZeros();
940
941 if (NumZeroBits == RegSize) {
942 // The current value is a zero.
943 // Explicitly express that as it would be easier for
944 // optimizations to kick in.
945 Parts[i] = DAG.getConstant(0, dl, RegisterVT);
946 continue;
947 }
948
949 // FIXME: We capture more information than the dag can represent. For
950 // now, just use the tightest assertzext/assertsext possible.
951 bool isSExt;
952 EVT FromVT(MVT::Other);
953 if (NumZeroBits) {
954 FromVT = EVT::getIntegerVT(*DAG.getContext(), RegSize - NumZeroBits);
955 isSExt = false;
956 } else if (NumSignBits > 1) {
957 FromVT =
958 EVT::getIntegerVT(*DAG.getContext(), RegSize - NumSignBits + 1);
959 isSExt = true;
960 } else {
961 continue;
962 }
963 // Add an assertion node.
964 assert(FromVT != MVT::Other);
965 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
966 RegisterVT, P, DAG.getValueType(FromVT));
967 }
968
969 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), NumRegs,
970 RegisterVT, ValueVT, V, Chain, CallConv);
971 Part += NumRegs;
972 Parts.clear();
973 }
974
975 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values);
976}
977
979 const SDLoc &dl, SDValue &Chain, SDValue *Glue,
980 const Value *V,
981 ISD::NodeType PreferredExtendType) const {
982 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
983 ISD::NodeType ExtendKind = PreferredExtendType;
984
985 // Get the list of the values's legal parts.
986 unsigned NumRegs = Regs.size();
987 SmallVector<SDValue, 8> Parts(NumRegs);
988 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
989 unsigned NumParts = RegCount[Value];
990
991 MVT RegisterVT = isABIMangled()
993 *DAG.getContext(), *CallConv, RegVTs[Value])
994 : RegVTs[Value];
995
996 if (ExtendKind == ISD::ANY_EXTEND)
997 if (TLI.isZExtFree(peekThroughFreeze(Val), RegisterVT))
998 ExtendKind = ISD::ZERO_EXTEND;
999
1000 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
1001 NumParts, RegisterVT, V, CallConv, ExtendKind);
1002 Part += NumParts;
1003 }
1004
1005 // Copy the parts into the registers.
1006 SmallVector<SDValue, 8> Chains(NumRegs);
1007 for (unsigned i = 0; i != NumRegs; ++i) {
1008 SDValue Part;
1009 if (!Glue) {
1010 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
1011 } else {
1012 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue);
1013 *Glue = Part.getValue(1);
1014 }
1015
1016 Chains[i] = Part.getValue(0);
1017 }
1018
1019 if (NumRegs == 1 || Glue)
1020 // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is
1021 // flagged to it. That is the CopyToReg nodes and the user are considered
1022 // a single scheduling unit. If we create a TokenFactor and return it as
1023 // chain, then the TokenFactor is both a predecessor (operand) of the
1024 // user as well as a successor (the TF operands are flagged to the user).
1025 // c1, f1 = CopyToReg
1026 // c2, f2 = CopyToReg
1027 // c3 = TokenFactor c1, c2
1028 // ...
1029 // = op c3, ..., f2
1030 Chain = Chains[NumRegs-1];
1031 else
1032 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1033}
1034
1036 unsigned MatchingIdx, const SDLoc &dl,
1037 SelectionDAG &DAG,
1038 std::vector<SDValue> &Ops) const {
1039 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1040
1041 InlineAsm::Flag Flag(Code, Regs.size());
1042 if (HasMatching)
1043 Flag.setMatchingOp(MatchingIdx);
1044 else if (!Regs.empty() && Regs.front().isVirtual()) {
1045 // Put the register class of the virtual registers in the flag word. That
1046 // way, later passes can recompute register class constraints for inline
1047 // assembly as well as normal instructions.
1048 // Don't do this for tied operands that can use the regclass information
1049 // from the def.
1051 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
1052 Flag.setRegClass(RC->getID());
1053 }
1054
1055 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
1056 Ops.push_back(Res);
1057
1058 if (Code == InlineAsm::Kind::Clobber) {
1059 // Clobbers should always have a 1:1 mapping with registers, and may
1060 // reference registers that have illegal (e.g. vector) types. Hence, we
1061 // shouldn't try to apply any sort of splitting logic to them.
1062 assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
1063 "No 1:1 mapping from clobbers to regs?");
1065 (void)SP;
1066 for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
1067 Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
1068 assert(
1069 (Regs[I] != SP ||
1071 "If we clobbered the stack pointer, MFI should know about it.");
1072 }
1073 return;
1074 }
1075
1076 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1077 MVT RegisterVT = RegVTs[Value];
1078 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1079 RegisterVT);
1080 for (unsigned i = 0; i != NumRegs; ++i) {
1081 assert(Reg < Regs.size() && "Mismatch in # registers expected");
1082 Register TheReg = Regs[Reg++];
1083 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1084 }
1085 }
1086}
1087
1091 unsigned I = 0;
1092 for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1093 unsigned RegCount = std::get<0>(CountAndVT);
1094 MVT RegisterVT = std::get<1>(CountAndVT);
1095 TypeSize RegisterSize = RegisterVT.getSizeInBits();
1096 for (unsigned E = I + RegCount; I != E; ++I)
1097 OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1098 }
1099 return OutVec;
1100}
1101
1103 AssumptionCache *ac, const TargetLibraryInfo *li,
1104 const TargetTransformInfo &TTI) {
1105 BatchAA = aa;
1106 AC = ac;
1107 GFI = gfi;
1108 LibInfo = li;
1109 Context = DAG.getContext();
1110 LPadToCallSiteMap.clear();
1111 this->TTI = &TTI;
1112 SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1113 AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
1114 *DAG.getMachineFunction().getFunction().getParent());
1115}
1116
1118 NodeMap.clear();
1119 UnusedArgNodeMap.clear();
1120 PendingLoads.clear();
1121 PendingExports.clear();
1122 PendingConstrainedFP.clear();
1123 PendingConstrainedFPStrict.clear();
1124 CurInst = nullptr;
1125 HasTailCall = false;
1126 SDNodeOrder = LowestSDNodeOrder;
1127 StatepointLowering.clear();
1128}
1129
1131 DanglingDebugInfoMap.clear();
1132}
1133
1134// Update DAG root to include dependencies on Pending chains.
1135SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1136 SDValue Root = DAG.getRoot();
1137
1138 if (Pending.empty())
1139 return Root;
1140
1141 // Add current root to PendingChains, unless we already indirectly
1142 // depend on it.
1143 if (Root.getOpcode() != ISD::EntryToken) {
1144 unsigned i = 0, e = Pending.size();
1145 for (; i != e; ++i) {
1146 assert(Pending[i].getNode()->getNumOperands() > 1);
1147 if (Pending[i].getNode()->getOperand(0) == Root)
1148 break; // Don't add the root if we already indirectly depend on it.
1149 }
1150
1151 if (i == e)
1152 Pending.push_back(Root);
1153 }
1154
1155 if (Pending.size() == 1)
1156 Root = Pending[0];
1157 else
1158 Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1159
1160 DAG.setRoot(Root);
1161 Pending.clear();
1162 return Root;
1163}
1164
1168
1170 // If the new exception behavior differs from that of the pending
1171 // ones, chain up them and update the root.
1172 switch (EB) {
1175 // Floating-point exceptions produced by such operations are not intended
1176 // to be observed, so the sequence of these operations does not need to be
1177 // preserved.
1178 //
1179 // They however must not be mixed with the instructions that have strict
1180 // exception behavior. Placing an operation with 'ebIgnore' behavior between
1181 // 'ebStrict' operations could distort the observed exception behavior.
1182 if (!PendingConstrainedFPStrict.empty()) {
1183 assert(PendingConstrainedFP.empty());
1184 updateRoot(PendingConstrainedFPStrict);
1185 }
1186 break;
1188 // Floating-point exception produced by these operations may be observed, so
1189 // they must be correctly chained. If trapping on FP exceptions is
1190 // disabled, the exceptions can be observed only by functions that read
1191 // exception flags, like 'llvm.get_fpenv' or 'fetestexcept'. It means that
1192 // the order of operations is not significant between barriers.
1193 //
1194 // If trapping is enabled, each operation becomes an implicit observation
1195 // point, so the operations must be sequenced according their original
1196 // source order.
1197 if (!PendingConstrainedFP.empty()) {
1198 assert(PendingConstrainedFPStrict.empty());
1199 updateRoot(PendingConstrainedFP);
1200 }
1201 // TODO: Add support for trapping-enabled scenarios.
1202 }
1203 return DAG.getRoot();
1204}
1205
1207 // Chain up all pending constrained intrinsics together with all
1208 // pending loads, by simply appending them to PendingLoads and
1209 // then calling getMemoryRoot().
1210 PendingLoads.reserve(PendingLoads.size() +
1211 PendingConstrainedFP.size() +
1212 PendingConstrainedFPStrict.size());
1213 PendingLoads.append(PendingConstrainedFP.begin(),
1214 PendingConstrainedFP.end());
1215 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1216 PendingConstrainedFPStrict.end());
1217 PendingConstrainedFP.clear();
1218 PendingConstrainedFPStrict.clear();
1219 return getMemoryRoot();
1220}
1221
1223 // We need to emit pending fpexcept.strict constrained intrinsics,
1224 // so append them to the PendingExports list.
1225 PendingExports.append(PendingConstrainedFPStrict.begin(),
1226 PendingConstrainedFPStrict.end());
1227 PendingConstrainedFPStrict.clear();
1228 return updateRoot(PendingExports);
1229}
1230
1232 DILocalVariable *Variable,
1234 DebugLoc DL) {
1235 assert(Variable && "Missing variable");
1236
1237 // Check if address has undef value.
1238 if (!Address || isa<UndefValue>(Address) ||
1239 (Address->use_empty() && !isa<Argument>(Address))) {
1240 LLVM_DEBUG(
1241 dbgs()
1242 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1243 return;
1244 }
1245
1246 bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1247
1248 SDValue &N = NodeMap[Address];
1249 if (!N.getNode() && isa<Argument>(Address))
1250 // Check unused arguments map.
1251 N = UnusedArgNodeMap[Address];
1252 SDDbgValue *SDV;
1253 if (N.getNode()) {
1254 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1255 Address = BCI->getOperand(0);
1256 // Parameters are handled specially.
1257 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1258 if (IsParameter && FINode) {
1259 // Byval parameter. We have a frame index at this point.
1260 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1261 /*IsIndirect*/ true, DL, SDNodeOrder);
1262 } else if (isa<Argument>(Address)) {
1263 // Address is an argument, so try to emit its dbg value using
1264 // virtual register info from the FuncInfo.ValueMap.
1265 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1266 FuncArgumentDbgValueKind::Declare, N);
1267 return;
1268 } else {
1269 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1270 true, DL, SDNodeOrder);
1271 }
1272 DAG.AddDbgValue(SDV, IsParameter);
1273 } else {
1274 // If Address is an argument then try to emit its dbg value using
1275 // virtual register info from the FuncInfo.ValueMap.
1276 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1277 FuncArgumentDbgValueKind::Declare, N)) {
1278 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1279 << " (could not emit func-arg dbg_value)\n");
1280 }
1281 }
1282}
1283
1285 // Add SDDbgValue nodes for any var locs here. Do so before updating
1286 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1287 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1288 // Add SDDbgValue nodes for any var locs here. Do so before updating
1289 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1290 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1291 It != End; ++It) {
1292 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1293 dropDanglingDebugInfo(Var, It->Expr);
1294 if (It->Values.isKillLocation(It->Expr)) {
1295 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1296 continue;
1297 }
1298 SmallVector<Value *> Values(It->Values.location_ops());
1299 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1300 It->Values.hasArgList())) {
1301 SmallVector<Value *, 4> Vals(It->Values.location_ops());
1303 FnVarLocs->getDILocalVariable(It->VariableID),
1304 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1305 }
1306 }
1307 }
1308
1309 // We must skip DbgVariableRecords if they've already been processed above as
1310 // we have just emitted the debug values resulting from assignment tracking
1311 // analysis, making any existing DbgVariableRecords redundant (and probably
1312 // less correct). We still need to process DbgLabelRecords. This does sink
1313 // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1314 // be important as it does so deterministcally and ordering between
1315 // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1316 // printing).
1317 bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1318 // Is there is any debug-info attached to this instruction, in the form of
1319 // DbgRecord non-instruction debug-info records.
1320 for (DbgRecord &DR : I.getDbgRecordRange()) {
1321 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1322 assert(DLR->getLabel() && "Missing label");
1323 SDDbgLabel *SDV =
1324 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1325 DAG.AddDbgLabel(SDV);
1326 continue;
1327 }
1328
1329 if (SkipDbgVariableRecords)
1330 continue;
1332 DILocalVariable *Variable = DVR.getVariable();
1335
1337 if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1338 continue;
1339 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1340 << "\n");
1342 DVR.getDebugLoc());
1343 continue;
1344 }
1345
1346 // A DbgVariableRecord with no locations is a kill location.
1348 if (Values.empty()) {
1350 SDNodeOrder);
1351 continue;
1352 }
1353
1354 // A DbgVariableRecord with an undef or absent location is also a kill
1355 // location.
1356 if (llvm::any_of(Values,
1357 [](Value *V) { return !V || isa<UndefValue>(V); })) {
1359 SDNodeOrder);
1360 continue;
1361 }
1362
1363 bool IsVariadic = DVR.hasArgList();
1364 if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1365 SDNodeOrder, IsVariadic)) {
1366 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1367 DVR.getDebugLoc(), SDNodeOrder);
1368 }
1369 }
1370}
1371
1373 visitDbgInfo(I);
1374
1375 // Set up outgoing PHI node register values before emitting the terminator.
1376 if (I.isTerminator()) {
1377 HandlePHINodesInSuccessorBlocks(I.getParent());
1378 }
1379
1380 ++SDNodeOrder;
1381 CurInst = &I;
1382
1383 // Set inserted listener only if required.
1384 bool NodeInserted = false;
1385 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1386 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1387 MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1388 if (PCSectionsMD || MMRA) {
1389 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1390 DAG, [&](SDNode *) { NodeInserted = true; });
1391 }
1392
1393 visit(I.getOpcode(), I);
1394
1395 if (!I.isTerminator() && !HasTailCall &&
1396 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1398
1399 // Handle metadata.
1400 if (PCSectionsMD || MMRA) {
1401 auto It = NodeMap.find(&I);
1402 if (It != NodeMap.end()) {
1403 if (PCSectionsMD)
1404 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1405 if (MMRA)
1406 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1407 } else if (NodeInserted) {
1408 // This should not happen; if it does, don't let it go unnoticed so we can
1409 // fix it. Relevant visit*() function is probably missing a setValue().
1410 errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1411 << I.getModule()->getName() << "]\n";
1412 LLVM_DEBUG(I.dump());
1413 assert(false);
1414 }
1415 }
1416
1417 CurInst = nullptr;
1418}
1419
1420void SelectionDAGBuilder::visitPHI(const PHINode &) {
1421 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1422}
1423
1424void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1425 // Note: this doesn't use InstVisitor, because it has to work with
1426 // ConstantExpr's in addition to instructions.
1427 switch (Opcode) {
1428 default: llvm_unreachable("Unknown instruction type encountered!");
1429 // Build the switch statement using the Instruction.def file.
1430#define HANDLE_INST(NUM, OPCODE, CLASS) \
1431 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1432#include "llvm/IR/Instruction.def"
1433 }
1434}
1435
1437 DILocalVariable *Variable,
1438 DebugLoc DL, unsigned Order,
1441 // For variadic dbg_values we will now insert poison.
1442 // FIXME: We can potentially recover these!
1444 for (const Value *V : Values) {
1445 auto *Poison = PoisonValue::get(V->getType());
1447 }
1448 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1449 /*IsIndirect=*/false, DL, Order,
1450 /*IsVariadic=*/true);
1451 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1452 return true;
1453}
1454
1456 DILocalVariable *Var,
1457 DIExpression *Expr,
1458 bool IsVariadic, DebugLoc DL,
1459 unsigned Order) {
1460 if (IsVariadic) {
1461 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1462 return;
1463 }
1464 // TODO: Dangling debug info will eventually either be resolved or produce
1465 // a poison DBG_VALUE. However in the resolution case, a gap may appear
1466 // between the original dbg.value location and its resolved DBG_VALUE,
1467 // which we should ideally fill with an extra poison DBG_VALUE.
1468 assert(Values.size() == 1);
1469 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1470}
1471
1473 const DIExpression *Expr) {
1474 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1475 DIVariable *DanglingVariable = DDI.getVariable();
1476 DIExpression *DanglingExpr = DDI.getExpression();
1477 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1478 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1479 << printDDI(nullptr, DDI) << "\n");
1480 return true;
1481 }
1482 return false;
1483 };
1484
1485 for (auto &DDIMI : DanglingDebugInfoMap) {
1486 DanglingDebugInfoVector &DDIV = DDIMI.second;
1487
1488 // If debug info is to be dropped, run it through final checks to see
1489 // whether it can be salvaged.
1490 for (auto &DDI : DDIV)
1491 if (isMatchingDbgValue(DDI))
1492 salvageUnresolvedDbgValue(DDIMI.first, DDI);
1493
1494 erase_if(DDIV, isMatchingDbgValue);
1495 }
1496}
1497
1498// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1499// generate the debug data structures now that we've seen its definition.
1501 SDValue Val) {
1502 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1503 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1504 return;
1505
1506 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1507 for (auto &DDI : DDIV) {
1508 DebugLoc DL = DDI.getDebugLoc();
1509 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1510 DILocalVariable *Variable = DDI.getVariable();
1511 DIExpression *Expr = DDI.getExpression();
1512 assert(Variable->isValidLocationForIntrinsic(DL) &&
1513 "Expected inlined-at fields to agree");
1514 SDDbgValue *SDV;
1515 if (Val.getNode()) {
1516 // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1517 // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1518 // we couldn't resolve it directly when examining the DbgValue intrinsic
1519 // in the first place we should not be more successful here). Unless we
1520 // have some test case that prove this to be correct we should avoid
1521 // calling EmitFuncArgumentDbgValue here.
1522 unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1523 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1524 FuncArgumentDbgValueKind::Value, Val)) {
1525 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1526 << printDDI(V, DDI) << "\n");
1527 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump());
1528 // Increase the SDNodeOrder for the DbgValue here to make sure it is
1529 // inserted after the definition of Val when emitting the instructions
1530 // after ISel. An alternative could be to teach
1531 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1532 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1533 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1534 << ValSDNodeOrder << "\n");
1535 SDV = getDbgValue(Val, Variable, Expr, DL,
1536 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1537 DAG.AddDbgValue(SDV, false);
1538 } else
1539 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1540 << printDDI(V, DDI)
1541 << " in EmitFuncArgumentDbgValue\n");
1542 } else {
1543 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1544 << "\n");
1545 auto Poison = PoisonValue::get(V->getType());
1546 auto SDV =
1547 DAG.getConstantDbgValue(Variable, Expr, Poison, DL, DbgSDNodeOrder);
1548 DAG.AddDbgValue(SDV, false);
1549 }
1550 }
1551 DDIV.clear();
1552}
1553
1555 DanglingDebugInfo &DDI) {
1556 // TODO: For the variadic implementation, instead of only checking the fail
1557 // state of `handleDebugValue`, we need know specifically which values were
1558 // invalid, so that we attempt to salvage only those values when processing
1559 // a DIArgList.
1560 const Value *OrigV = V;
1561 DILocalVariable *Var = DDI.getVariable();
1562 DIExpression *Expr = DDI.getExpression();
1563 DebugLoc DL = DDI.getDebugLoc();
1564 unsigned SDOrder = DDI.getSDNodeOrder();
1565
1566 // Currently we consider only dbg.value intrinsics -- we tell the salvager
1567 // that DW_OP_stack_value is desired.
1568 bool StackValue = true;
1569
1570 // Can this Value can be encoded without any further work?
1571 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1572 return;
1573
1574 // Attempt to salvage back through as many instructions as possible. Bail if
1575 // a non-instruction is seen, such as a constant expression or global
1576 // variable. FIXME: Further work could recover those too.
1577 while (isa<Instruction>(V)) {
1578 const Instruction &VAsInst = *cast<const Instruction>(V);
1579 // Temporary "0", awaiting real implementation.
1581 SmallVector<Value *, 4> AdditionalValues;
1582 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1583 Expr->getNumLocationOperands(), Ops,
1584 AdditionalValues);
1585 // If we cannot salvage any further, and haven't yet found a suitable debug
1586 // expression, bail out.
1587 if (!V)
1588 break;
1589
1590 // TODO: If AdditionalValues isn't empty, then the salvage can only be
1591 // represented with a DBG_VALUE_LIST, so we give up. When we have support
1592 // here for variadic dbg_values, remove that condition.
1593 if (!AdditionalValues.empty())
1594 break;
1595
1596 // New value and expr now represent this debuginfo.
1597 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1598
1599 // Some kind of simplification occurred: check whether the operand of the
1600 // salvaged debug expression can be encoded in this DAG.
1601 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1602 LLVM_DEBUG(
1603 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n"
1604 << *OrigV << "\nBy stripping back to:\n " << *V << "\n");
1605 return;
1606 }
1607 }
1608
1609 // This was the final opportunity to salvage this debug information, and it
1610 // couldn't be done. Place a poison DBG_VALUE at this location to terminate
1611 // any earlier variable location.
1612 assert(OrigV && "V shouldn't be null");
1613 auto *Poison = PoisonValue::get(OrigV->getType());
1614 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Poison, DL, SDNodeOrder);
1615 DAG.AddDbgValue(SDV, false);
1616 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n "
1617 << printDDI(OrigV, DDI) << "\n");
1618}
1619
1621 DIExpression *Expr,
1622 DebugLoc DbgLoc,
1623 unsigned Order) {
1627 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1628 /*IsVariadic*/ false);
1629}
1630
1632 DILocalVariable *Var,
1633 DIExpression *Expr, DebugLoc DbgLoc,
1634 unsigned Order, bool IsVariadic) {
1635 if (Values.empty())
1636 return true;
1637
1638 // Filter EntryValue locations out early.
1639 if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1640 return true;
1641
1642 SmallVector<SDDbgOperand> LocationOps;
1643 SmallVector<SDNode *> Dependencies;
1644 for (const Value *V : Values) {
1645 // Constant value.
1648 LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1649 continue;
1650 }
1651
1652 // Look through IntToPtr constants.
1653 if (auto *CE = dyn_cast<ConstantExpr>(V))
1654 if (CE->getOpcode() == Instruction::IntToPtr) {
1655 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1656 continue;
1657 }
1658
1659 // If the Value is a frame index, we can create a FrameIndex debug value
1660 // without relying on the DAG at all.
1661 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1662 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1663 if (SI != FuncInfo.StaticAllocaMap.end()) {
1664 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1665 continue;
1666 }
1667 }
1668
1669 // Do not use getValue() in here; we don't want to generate code at
1670 // this point if it hasn't been done yet.
1671 SDValue N = NodeMap[V];
1672 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1673 N = UnusedArgNodeMap[V];
1674
1675 if (N.getNode()) {
1676 // Only emit func arg dbg value for non-variadic dbg.values for now.
1677 if (!IsVariadic &&
1678 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1679 FuncArgumentDbgValueKind::Value, N))
1680 return true;
1681 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1682 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1683 // describe stack slot locations.
1684 //
1685 // Consider "int x = 0; int *px = &x;". There are two kinds of
1686 // interesting debug values here after optimization:
1687 //
1688 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
1689 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1690 //
1691 // Both describe the direct values of their associated variables.
1692 Dependencies.push_back(N.getNode());
1693 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1694 continue;
1695 }
1696 LocationOps.emplace_back(
1697 SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1698 continue;
1699 }
1700
1701 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1702 // Special rules apply for the first dbg.values of parameter variables in a
1703 // function. Identify them by the fact they reference Argument Values, that
1704 // they're parameters, and they are parameters of the current function. We
1705 // need to let them dangle until they get an SDNode.
1706 bool IsParamOfFunc =
1707 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1708 if (IsParamOfFunc)
1709 return false;
1710
1711 // The value is not used in this block yet (or it would have an SDNode).
1712 // We still want the value to appear for the user if possible -- if it has
1713 // an associated VReg, we can refer to that instead.
1714 auto VMI = FuncInfo.ValueMap.find(V);
1715 if (VMI != FuncInfo.ValueMap.end()) {
1716 Register Reg = VMI->second;
1717 // If this is a PHI node, it may be split up into several MI PHI nodes
1718 // (in FunctionLoweringInfo::set).
1719 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1720 V->getType(), std::nullopt);
1721 if (RFV.occupiesMultipleRegs()) {
1722 // FIXME: We could potentially support variadic dbg_values here.
1723 if (IsVariadic)
1724 return false;
1725 unsigned Offset = 0;
1726 unsigned BitsToDescribe = 0;
1727 if (auto VarSize = Var->getSizeInBits())
1728 BitsToDescribe = *VarSize;
1729 if (auto Fragment = Expr->getFragmentInfo())
1730 BitsToDescribe = Fragment->SizeInBits;
1731 for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1732 // Bail out if all bits are described already.
1733 if (Offset >= BitsToDescribe)
1734 break;
1735 // TODO: handle scalable vectors.
1736 unsigned RegisterSize = RegAndSize.second;
1737 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1738 ? BitsToDescribe - Offset
1739 : RegisterSize;
1740 auto FragmentExpr = DIExpression::createFragmentExpression(
1741 Expr, Offset, FragmentSize);
1742 if (!FragmentExpr)
1743 continue;
1744 SDDbgValue *SDV = DAG.getVRegDbgValue(
1745 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1746 DAG.AddDbgValue(SDV, false);
1747 Offset += RegisterSize;
1748 }
1749 return true;
1750 }
1751 // We can use simple vreg locations for variadic dbg_values as well.
1752 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1753 continue;
1754 }
1755 // We failed to create a SDDbgOperand for V.
1756 return false;
1757 }
1758
1759 // We have created a SDDbgOperand for each Value in Values.
1760 assert(!LocationOps.empty());
1761 SDDbgValue *SDV =
1762 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1763 /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1764 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1765 return true;
1766}
1767
1769 // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1770 for (auto &Pair : DanglingDebugInfoMap)
1771 for (auto &DDI : Pair.second)
1772 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1774}
1775
1776/// getCopyFromRegs - If there was virtual register allocated for the value V
1777/// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1780 SDValue Result;
1781
1782 if (It != FuncInfo.ValueMap.end()) {
1783 Register InReg = It->second;
1784
1785 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1786 DAG.getDataLayout(), InReg, Ty,
1787 std::nullopt); // This is not an ABI copy.
1788 SDValue Chain = DAG.getEntryNode();
1789 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1790 V);
1791 resolveDanglingDebugInfo(V, Result);
1792 }
1793
1794 return Result;
1795}
1796
1797/// getValue - Return an SDValue for the given Value.
1799 // If we already have an SDValue for this value, use it. It's important
1800 // to do this first, so that we don't create a CopyFromReg if we already
1801 // have a regular SDValue.
1802 SDValue &N = NodeMap[V];
1803 if (N.getNode()) return N;
1804
1805 // If there's a virtual register allocated and initialized for this
1806 // value, use it.
1807 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1808 return copyFromReg;
1809
1810 // Otherwise create a new SDValue and remember it.
1811 SDValue Val = getValueImpl(V);
1812 NodeMap[V] = Val;
1814 return Val;
1815}
1816
1817/// getNonRegisterValue - Return an SDValue for the given Value, but
1818/// don't look in FuncInfo.ValueMap for a virtual register.
1820 // If we already have an SDValue for this value, use it.
1821 SDValue &N = NodeMap[V];
1822 if (N.getNode()) {
1823 if (isIntOrFPConstant(N)) {
1824 // Remove the debug location from the node as the node is about to be used
1825 // in a location which may differ from the original debug location. This
1826 // is relevant to Constant and ConstantFP nodes because they can appear
1827 // as constant expressions inside PHI nodes.
1828 N->setDebugLoc(DebugLoc());
1829 }
1830 return N;
1831 }
1832
1833 // Otherwise create a new SDValue and remember it.
1834 SDValue Val = getValueImpl(V);
1835 NodeMap[V] = Val;
1837 return Val;
1838}
1839
1840/// getValueImpl - Helper function for getValue and getNonRegisterValue.
1841/// Create an SDValue for the given value.
1843 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1844
1845 if (const Constant *C = dyn_cast<Constant>(V)) {
1846 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1847
1848 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1849 SDLoc DL = getCurSDLoc();
1850
1851 // DAG.getConstant() may attempt to legalise the vector constant which can
1852 // significantly change the combines applied to the DAG. To reduce the
1853 // divergence when enabling ConstantInt based vectors we try to construct
1854 // the DAG in the same way as shufflevector based splats. TODO: The
1855 // divergence sometimes leads to better optimisations. Ideally we should
1856 // prevent DAG.getConstant() from legalising too early but there are some
1857 // degradations preventing this.
1858 if (VT.isScalableVector())
1859 return DAG.getNode(
1860 ISD::SPLAT_VECTOR, DL, VT,
1861 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1862 if (VT.isFixedLengthVector())
1863 return DAG.getSplatBuildVector(
1864 VT, DL,
1865 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1866 return DAG.getConstant(*CI, DL, VT);
1867 }
1868
1869 if (const ConstantByte *CB = dyn_cast<ConstantByte>(C))
1870 return DAG.getConstant(CB->getValue(), getCurSDLoc(), VT);
1871
1872 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1873 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1874
1875 if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1876 return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1877 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1878 getValue(CPA->getAddrDiscriminator()),
1879 getValue(CPA->getDiscriminator()));
1880 }
1881
1883 return DAG.getConstant(0, getCurSDLoc(), VT);
1884
1885 if (match(C, m_VScale()))
1886 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1887
1888 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1889 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1890
1891 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1892 return isa<PoisonValue>(C) ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
1893
1894 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1895 visit(CE->getOpcode(), *CE);
1896 SDValue N1 = NodeMap[V];
1897 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1898 return N1;
1899 }
1900
1902 SmallVector<SDValue, 4> Constants;
1903 for (const Use &U : C->operands()) {
1904 SDNode *Val = getValue(U).getNode();
1905 // If the operand is an empty aggregate, there are no values.
1906 if (!Val) continue;
1907 // Add each leaf value from the operand to the Constants list
1908 // to form a flattened list of all the values.
1909 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1910 Constants.push_back(SDValue(Val, i));
1911 }
1912
1913 return DAG.getMergeValues(Constants, getCurSDLoc());
1914 }
1915
1916 if (const ConstantDataSequential *CDS =
1919 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1920 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1921 // Add each leaf value from the operand to the Constants list
1922 // to form a flattened list of all the values.
1923 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1924 Ops.push_back(SDValue(Val, i));
1925 }
1926
1927 if (isa<ArrayType>(CDS->getType()))
1928 return DAG.getMergeValues(Ops, getCurSDLoc());
1929 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1930 }
1931
1932 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1934 "Unknown struct or array constant!");
1935
1936 SmallVector<EVT, 4> ValueVTs;
1937 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1938 unsigned NumElts = ValueVTs.size();
1939 if (NumElts == 0)
1940 return SDValue(); // empty struct
1941 SmallVector<SDValue, 4> Constants(NumElts);
1942 for (unsigned i = 0; i != NumElts; ++i) {
1943 EVT EltVT = ValueVTs[i];
1944 if (isa<UndefValue>(C))
1945 Constants[i] = DAG.getUNDEF(EltVT);
1946 else if (EltVT.isFloatingPoint())
1947 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1948 else
1949 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1950 }
1951
1952 return DAG.getMergeValues(Constants, getCurSDLoc());
1953 }
1954
1955 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1956 return DAG.getBlockAddress(BA, VT);
1957
1958 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1959 return getValue(Equiv->getGlobalValue());
1960
1961 if (const auto *NC = dyn_cast<NoCFIValue>(C))
1962 return getValue(NC->getGlobalValue());
1963
1964 if (VT == MVT::aarch64svcount) {
1965 assert(C->isNullValue() && "Can only zero this target type!");
1966 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1967 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1968 }
1969
1970 if (VT.isRISCVVectorTuple()) {
1971 assert(C->isNullValue() && "Can only zero this target type!");
1972 return DAG.getNode(
1974 DAG.getNode(
1976 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1977 VT.getSizeInBits().getKnownMinValue() / 8, true),
1978 DAG.getConstant(0, getCurSDLoc(), MVT::getIntegerVT(8))));
1979 }
1980
1981 VectorType *VecTy = cast<VectorType>(V->getType());
1982
1983 // Now that we know the number and type of the elements, get that number of
1984 // elements into the Ops array based on what kind of constant it is.
1985 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1987 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1988 for (unsigned i = 0; i != NumElements; ++i)
1989 Ops.push_back(getValue(CV->getOperand(i)));
1990
1991 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1992 }
1993
1995 EVT EltVT =
1996 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1997
1998 SDValue Op;
1999 if (EltVT.isFloatingPoint())
2000 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
2001 else
2002 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
2003
2004 return DAG.getSplat(VT, getCurSDLoc(), Op);
2005 }
2006
2007 llvm_unreachable("Unknown vector constant");
2008 }
2009
2010 // If this is a static alloca, generate it as the frameindex instead of
2011 // computation.
2012 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
2014 FuncInfo.StaticAllocaMap.find(AI);
2015 if (SI != FuncInfo.StaticAllocaMap.end())
2016 return DAG.getFrameIndex(
2017 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
2018 }
2019
2020 // If this is an instruction which fast-isel has deferred, select it now.
2021 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2022 Register InReg = FuncInfo.InitializeRegForValue(Inst);
2023 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
2024 Inst->getType(), std::nullopt);
2025 SDValue Chain = DAG.getEntryNode();
2026 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
2027 }
2028
2029 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
2030 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
2031
2032 if (const auto *BB = dyn_cast<BasicBlock>(V))
2033 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
2034
2035 llvm_unreachable("Can't get register for value!");
2036}
2037
2038void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
2040 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
2041 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
2042 bool IsSEH = isAsynchronousEHPersonality(Pers);
2043 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2044 if (IsSEH) {
2045 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2046 CatchPadMBB->setIsEHContTarget(true);
2048 } else
2049 CatchPadMBB->setIsEHScopeEntry();
2050 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2051 if (IsMSVCCXX || IsCoreCLR)
2052 CatchPadMBB->setIsEHFuncletEntry();
2053}
2054
2055void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2056 // Update machine-CFG edge.
2057 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2058 FuncInfo.MBB->addSuccessor(TargetMBB);
2059
2060 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2061 bool IsSEH = isAsynchronousEHPersonality(Pers);
2062 if (IsSEH) {
2063 // If this is not a fall-through branch or optimizations are switched off,
2064 // emit the branch.
2065 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2066 TM.getOptLevel() == CodeGenOptLevel::None)
2067 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2068 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2069 return;
2070 }
2071
2072 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2073 TargetMBB->setIsEHContTarget(true);
2074 DAG.getMachineFunction().setHasEHContTarget(true);
2075
2076 // Figure out the funclet membership for the catchret's successor.
2077 // This will be used by the FuncletLayout pass to determine how to order the
2078 // BB's.
2079 // A 'catchret' returns to the outer scope's color.
2080 Value *ParentPad = I.getCatchSwitchParentPad();
2081 const BasicBlock *SuccessorColor;
2082 if (isa<ConstantTokenNone>(ParentPad))
2083 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2084 else
2085 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2086 assert(SuccessorColor && "No parent funclet for catchret!");
2087 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2088 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2089
2090 // Create the terminator node.
2091 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2092 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2093 DAG.getBasicBlock(SuccessorColorMBB));
2094 DAG.setRoot(Ret);
2095}
2096
2097void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2098 // Don't emit any special code for the cleanuppad instruction. It just marks
2099 // the start of an EH scope/funclet.
2100 FuncInfo.MBB->setIsEHScopeEntry();
2101 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2102 if (Pers != EHPersonality::Wasm_CXX) {
2103 FuncInfo.MBB->setIsEHFuncletEntry();
2104 FuncInfo.MBB->setIsCleanupFuncletEntry();
2105 }
2106}
2107
2108/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2109/// many places it could ultimately go. In the IR, we have a single unwind
2110/// destination, but in the machine CFG, we enumerate all the possible blocks.
2111/// This function skips over imaginary basic blocks that hold catchswitch
2112/// instructions, and finds all the "real" machine
2113/// basic block destinations. As those destinations may not be successors of
2114/// EHPadBB, here we also calculate the edge probability to those destinations.
2115/// The passed-in Prob is the edge probability to EHPadBB.
2117 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2118 BranchProbability Prob,
2119 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2120 &UnwindDests) {
2121 EHPersonality Personality =
2123 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2124 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2125 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2126 bool IsSEH = isAsynchronousEHPersonality(Personality);
2127
2128 while (EHPadBB) {
2130 BasicBlock *NewEHPadBB = nullptr;
2131 if (isa<LandingPadInst>(Pad)) {
2132 // Stop on landingpads. They are not funclets.
2133 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2134 break;
2135 } else if (isa<CleanupPadInst>(Pad)) {
2136 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2137 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2138 // which always catches an exception.
2139 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2140 UnwindDests.back().first->setIsEHScopeEntry();
2141 // In Wasm, EH scopes are not funclets
2142 if (!IsWasmCXX)
2143 UnwindDests.back().first->setIsEHFuncletEntry();
2144 break;
2145 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2146 // Add the catchpad handlers to the possible destinations.
2147 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2148 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2149 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2150 if (IsMSVCCXX || IsCoreCLR)
2151 UnwindDests.back().first->setIsEHFuncletEntry();
2152 if (!IsSEH)
2153 UnwindDests.back().first->setIsEHScopeEntry();
2154 }
2155 NewEHPadBB = CatchSwitch->getUnwindDest();
2156 } else {
2157 continue;
2158 }
2159
2160 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2161 if (BPI && NewEHPadBB)
2162 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2163 EHPadBB = NewEHPadBB;
2164 }
2165}
2166
2167void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2168 // Update successor info.
2170 auto UnwindDest = I.getUnwindDest();
2171 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2172 BranchProbability UnwindDestProb =
2173 (BPI && UnwindDest)
2174 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2176 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2177 for (auto &UnwindDest : UnwindDests) {
2178 UnwindDest.first->setIsEHPad();
2179 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2180 }
2181 FuncInfo.MBB->normalizeSuccProbs();
2182
2183 // Create the terminator node.
2184 MachineBasicBlock *CleanupPadMBB =
2185 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2186 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
2187 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2188 DAG.setRoot(Ret);
2189}
2190
2191void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2192 report_fatal_error("visitCatchSwitch not yet implemented!");
2193}
2194
2195void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2196 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2197 auto &DL = DAG.getDataLayout();
2198 SDValue Chain = getControlRoot();
2201
2202 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2203 // lower
2204 //
2205 // %val = call <ty> @llvm.experimental.deoptimize()
2206 // ret <ty> %val
2207 //
2208 // differently.
2209 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2211 return;
2212 }
2213
2214 if (!FuncInfo.CanLowerReturn) {
2215 Register DemoteReg = FuncInfo.DemoteRegister;
2216
2217 // Emit a store of the return value through the virtual register.
2218 // Leave Outs empty so that LowerReturn won't try to load return
2219 // registers the usual way.
2220 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2221 SDValue RetPtr =
2222 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2223 SDValue RetOp = getValue(I.getOperand(0));
2224
2225 SmallVector<EVT, 4> ValueVTs, MemVTs;
2226 SmallVector<uint64_t, 4> Offsets;
2227 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2228 &Offsets, 0);
2229 unsigned NumValues = ValueVTs.size();
2230
2231 SmallVector<SDValue, 4> Chains(NumValues);
2232 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2233 for (unsigned i = 0; i != NumValues; ++i) {
2234 // An aggregate return value cannot wrap around the address space, so
2235 // offsets to its parts don't wrap either.
2236 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2237 TypeSize::getFixed(Offsets[i]));
2238
2239 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2240 if (MemVTs[i] != ValueVTs[i])
2241 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2242 Chains[i] = DAG.getStore(
2243 Chain, getCurSDLoc(), Val,
2244 // FIXME: better loc info would be nice.
2245 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2246 commonAlignment(BaseAlign, Offsets[i]));
2247 }
2248
2249 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2250 MVT::Other, Chains);
2251 } else if (I.getNumOperands() != 0) {
2253 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2254 unsigned NumValues = Types.size();
2255 if (NumValues) {
2256 SDValue RetOp = getValue(I.getOperand(0));
2257
2258 const Function *F = I.getParent()->getParent();
2259
2260 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2261 I.getOperand(0)->getType(), F->getCallingConv(),
2262 /*IsVarArg*/ false, DL);
2263
2264 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2265 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2266 ExtendKind = ISD::SIGN_EXTEND;
2267 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2268 ExtendKind = ISD::ZERO_EXTEND;
2269
2270 LLVMContext &Context = F->getContext();
2271 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2272
2273 for (unsigned j = 0; j != NumValues; ++j) {
2274 EVT VT = TLI.getValueType(DL, Types[j]);
2275
2276 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2277 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2278
2279 CallingConv::ID CC = F->getCallingConv();
2280
2281 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2282 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2283 SmallVector<SDValue, 4> Parts(NumParts);
2285 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2286 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2287
2288 // 'inreg' on function refers to return value
2289 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2290 if (RetInReg)
2291 Flags.setInReg();
2292
2293 if (I.getOperand(0)->getType()->isPointerTy()) {
2294 Flags.setPointer();
2295 Flags.setPointerAddrSpace(
2296 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2297 }
2298
2299 if (NeedsRegBlock) {
2300 Flags.setInConsecutiveRegs();
2301 if (j == NumValues - 1)
2302 Flags.setInConsecutiveRegsLast();
2303 }
2304
2305 // Propagate extension type if any
2306 if (ExtendKind == ISD::SIGN_EXTEND)
2307 Flags.setSExt();
2308 else if (ExtendKind == ISD::ZERO_EXTEND)
2309 Flags.setZExt();
2310 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2311 Flags.setNoExt();
2312
2313 for (unsigned i = 0; i < NumParts; ++i) {
2314 Outs.push_back(ISD::OutputArg(Flags,
2315 Parts[i].getValueType().getSimpleVT(),
2316 VT, Types[j], 0, 0));
2317 OutVals.push_back(Parts[i]);
2318 }
2319 }
2320 }
2321 }
2322
2323 // Push in swifterror virtual register as the last element of Outs. This makes
2324 // sure swifterror virtual register will be returned in the swifterror
2325 // physical register.
2326 const Function *F = I.getParent()->getParent();
2327 if (TLI.supportSwiftError() &&
2328 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2329 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2330 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2331 Flags.setSwiftError();
2332 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2333 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2334 PointerType::getUnqual(*DAG.getContext()),
2335 /*origidx=*/1, /*partOffs=*/0));
2336 // Create SDNode for the swifterror virtual register.
2337 OutVals.push_back(
2338 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2339 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2340 EVT(TLI.getPointerTy(DL))));
2341 }
2342
2343 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2344 CallingConv::ID CallConv =
2345 DAG.getMachineFunction().getFunction().getCallingConv();
2346 Chain = DAG.getTargetLoweringInfo().LowerReturn(
2347 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2348
2349 // Verify that the target's LowerReturn behaved as expected.
2350 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2351 "LowerReturn didn't return a valid chain!");
2352
2353 // Update the DAG with the new chain value resulting from return lowering.
2354 DAG.setRoot(Chain);
2355}
2356
2357/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2358/// created for it, emit nodes to copy the value into the virtual
2359/// registers.
2361 // Skip empty types
2362 if (V->getType()->isEmptyTy())
2363 return;
2364
2366 if (VMI != FuncInfo.ValueMap.end()) {
2367 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2368 "Unused value assigned virtual registers!");
2369 CopyValueToVirtualRegister(V, VMI->second);
2370 }
2371}
2372
2373/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2374/// the current basic block, add it to ValueMap now so that we'll get a
2375/// CopyTo/FromReg.
2377 // No need to export constants.
2378 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2379
2380 // Already exported?
2381 if (FuncInfo.isExportedInst(V)) return;
2382
2383 Register Reg = FuncInfo.InitializeRegForValue(V);
2385}
2386
2388 const BasicBlock *FromBB) {
2389 // The operands of the setcc have to be in this block. We don't know
2390 // how to export them from some other block.
2391 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2392 // Can export from current BB.
2393 if (VI->getParent() == FromBB)
2394 return true;
2395
2396 // Is already exported, noop.
2397 return FuncInfo.isExportedInst(V);
2398 }
2399
2400 // If this is an argument, we can export it if the BB is the entry block or
2401 // if it is already exported.
2402 if (isa<Argument>(V)) {
2403 if (FromBB->isEntryBlock())
2404 return true;
2405
2406 // Otherwise, can only export this if it is already exported.
2407 return FuncInfo.isExportedInst(V);
2408 }
2409
2410 // Otherwise, constants can always be exported.
2411 return true;
2412}
2413
2414/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2416SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2417 const MachineBasicBlock *Dst) const {
2419 const BasicBlock *SrcBB = Src->getBasicBlock();
2420 const BasicBlock *DstBB = Dst->getBasicBlock();
2421 if (!BPI) {
2422 // If BPI is not available, set the default probability as 1 / N, where N is
2423 // the number of successors.
2424 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2425 return BranchProbability(1, SuccSize);
2426 }
2427 return BPI->getEdgeProbability(SrcBB, DstBB);
2428}
2429
2430void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2431 MachineBasicBlock *Dst,
2432 BranchProbability Prob) {
2433 if (!FuncInfo.BPI)
2434 Src->addSuccessorWithoutProb(Dst);
2435 else {
2436 if (Prob.isUnknown())
2437 Prob = getEdgeProbability(Src, Dst);
2438 Src->addSuccessor(Dst, Prob);
2439 }
2440}
2441
2442static bool InBlock(const Value *V, const BasicBlock *BB) {
2443 if (const Instruction *I = dyn_cast<Instruction>(V))
2444 return I->getParent() == BB;
2445 return true;
2446}
2447
2448/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2449/// This function emits a branch and is used at the leaves of an OR or an
2450/// AND operator tree.
2451void
2454 MachineBasicBlock *FBB,
2455 MachineBasicBlock *CurBB,
2456 MachineBasicBlock *SwitchBB,
2457 BranchProbability TProb,
2458 BranchProbability FProb,
2459 bool InvertCond) {
2460 const BasicBlock *BB = CurBB->getBasicBlock();
2461
2462 // If the leaf of the tree is a comparison, merge the condition into
2463 // the caseblock.
2464 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2465 // The operands of the cmp have to be in this block. We don't know
2466 // how to export them from some other block. If this is the first block
2467 // of the sequence, no exporting is needed.
2468 if (CurBB == SwitchBB ||
2469 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2470 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2471 ISD::CondCode Condition;
2472 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2473 ICmpInst::Predicate Pred =
2474 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2475 Condition = getICmpCondCode(Pred);
2476 } else {
2477 const FCmpInst *FC = cast<FCmpInst>(Cond);
2478 FCmpInst::Predicate Pred =
2479 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2480 Condition = getFCmpCondCode(Pred);
2481 if (FC->hasNoNaNs() ||
2482 (isKnownNeverNaN(FC->getOperand(0),
2483 SimplifyQuery(DAG.getDataLayout(), FC)) &&
2484 isKnownNeverNaN(FC->getOperand(1),
2485 SimplifyQuery(DAG.getDataLayout(), FC))))
2486 Condition = getFCmpCodeWithoutNaN(Condition);
2487 }
2488
2489 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2490 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2491 SL->SwitchCases.push_back(CB);
2492 return;
2493 }
2494 }
2495
2496 // Create a CaseBlock record representing this branch.
2497 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2498 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2499 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2500 SL->SwitchCases.push_back(CB);
2501}
2502
2503// Collect dependencies on V recursively. This is used for the cost analysis in
2504// `shouldKeepJumpConditionsTogether`.
2508 unsigned Depth = 0) {
2509 // Return false if we have an incomplete count.
2511 return false;
2512
2513 auto *I = dyn_cast<Instruction>(V);
2514 if (I == nullptr)
2515 return true;
2516
2517 if (Necessary != nullptr) {
2518 // This instruction is necessary for the other side of the condition so
2519 // don't count it.
2520 if (Necessary->contains(I))
2521 return true;
2522 }
2523
2524 // Already added this dep.
2525 if (!Deps->try_emplace(I, false).second)
2526 return true;
2527
2528 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2529 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2530 Depth + 1))
2531 return false;
2532 return true;
2533}
2534
2537 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2539 if (Params.BaseCost < 0)
2540 return false;
2541
2542 // Baseline cost.
2543 InstructionCost CostThresh = Params.BaseCost;
2544
2545 BranchProbabilityInfo *BPI = nullptr;
2546 if (Params.LikelyBias || Params.UnlikelyBias)
2547 BPI = FuncInfo.BPI;
2548 if (BPI != nullptr) {
2549 // See if we are either likely to get an early out or compute both lhs/rhs
2550 // of the condition.
2551 BasicBlock *IfFalse = I.getSuccessor(0);
2552 BasicBlock *IfTrue = I.getSuccessor(1);
2553
2554 std::optional<bool> Likely;
2555 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2556 Likely = true;
2557 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2558 Likely = false;
2559
2560 if (Likely) {
2561 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2562 // Its likely we will have to compute both lhs and rhs of condition
2563 CostThresh += Params.LikelyBias;
2564 else {
2565 if (Params.UnlikelyBias < 0)
2566 return false;
2567 // Its likely we will get an early out.
2568 CostThresh -= Params.UnlikelyBias;
2569 }
2570 }
2571 }
2572
2573 if (CostThresh <= 0)
2574 return false;
2575
2576 // Collect "all" instructions that lhs condition is dependent on.
2577 // Use map for stable iteration (to avoid non-determanism of iteration of
2578 // SmallPtrSet). The `bool` value is just a dummy.
2580 collectInstructionDeps(&LhsDeps, Lhs);
2581 // Collect "all" instructions that rhs condition is dependent on AND are
2582 // dependencies of lhs. This gives us an estimate on which instructions we
2583 // stand to save by splitting the condition.
2584 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2585 return false;
2586 // Add the compare instruction itself unless its a dependency on the LHS.
2587 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2588 if (!LhsDeps.contains(RhsI))
2589 RhsDeps.try_emplace(RhsI, false);
2590
2591 InstructionCost CostOfIncluding = 0;
2592 // See if this instruction will need to computed independently of whether RHS
2593 // is.
2594 Value *BrCond = I.getCondition();
2595 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2596 for (const auto *U : Ins->users()) {
2597 // If user is independent of RHS calculation we don't need to count it.
2598 if (auto *UIns = dyn_cast<Instruction>(U))
2599 if (UIns != BrCond && !RhsDeps.contains(UIns))
2600 return false;
2601 }
2602 return true;
2603 };
2604
2605 // Prune instructions from RHS Deps that are dependencies of unrelated
2606 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2607 // arbitrary and just meant to cap the how much time we spend in the pruning
2608 // loop. Its highly unlikely to come into affect.
2609 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2610 // Stop after a certain point. No incorrectness from including too many
2611 // instructions.
2612 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2613 const Instruction *ToDrop = nullptr;
2614 for (const auto &InsPair : RhsDeps) {
2615 if (!ShouldCountInsn(InsPair.first)) {
2616 ToDrop = InsPair.first;
2617 break;
2618 }
2619 }
2620 if (ToDrop == nullptr)
2621 break;
2622 RhsDeps.erase(ToDrop);
2623 }
2624
2625 for (const auto &InsPair : RhsDeps) {
2626 // Finally accumulate latency that we can only attribute to computing the
2627 // RHS condition. Use latency because we are essentially trying to calculate
2628 // the cost of the dependency chain.
2629 // Possible TODO: We could try to estimate ILP and make this more precise.
2630 CostOfIncluding += TTI->getInstructionCost(
2631 InsPair.first, TargetTransformInfo::TCK_Latency);
2632
2633 if (CostOfIncluding > CostThresh)
2634 return false;
2635 }
2636 return true;
2637}
2638
2641 MachineBasicBlock *FBB,
2642 MachineBasicBlock *CurBB,
2643 MachineBasicBlock *SwitchBB,
2645 BranchProbability TProb,
2646 BranchProbability FProb,
2647 bool InvertCond) {
2648 // Skip over not part of the tree and remember to invert op and operands at
2649 // next level.
2650 Value *NotCond;
2651 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2652 InBlock(NotCond, CurBB->getBasicBlock())) {
2653 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2654 !InvertCond);
2655 return;
2656 }
2657
2659 const Value *BOpOp0, *BOpOp1;
2660 // Compute the effective opcode for Cond, taking into account whether it needs
2661 // to be inverted, e.g.
2662 // and (not (or A, B)), C
2663 // gets lowered as
2664 // and (and (not A, not B), C)
2666 if (BOp) {
2667 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2668 ? Instruction::And
2669 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2670 ? Instruction::Or
2672 if (InvertCond) {
2673 if (BOpc == Instruction::And)
2674 BOpc = Instruction::Or;
2675 else if (BOpc == Instruction::Or)
2676 BOpc = Instruction::And;
2677 }
2678 }
2679
2680 // If this node is not part of the or/and tree, emit it as a branch.
2681 // Note that all nodes in the tree should have same opcode.
2682 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2683 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2684 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2685 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2686 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2687 TProb, FProb, InvertCond);
2688 return;
2689 }
2690
2691 // Create TmpBB after CurBB.
2692 MachineFunction::iterator BBI(CurBB);
2693 MachineFunction &MF = DAG.getMachineFunction();
2695 CurBB->getParent()->insert(++BBI, TmpBB);
2696
2697 if (Opc == Instruction::Or) {
2698 // Codegen X | Y as:
2699 // BB1:
2700 // jmp_if_X TBB
2701 // jmp TmpBB
2702 // TmpBB:
2703 // jmp_if_Y TBB
2704 // jmp FBB
2705 //
2706
2707 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2708 // The requirement is that
2709 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2710 // = TrueProb for original BB.
2711 // Assuming the original probabilities are A and B, one choice is to set
2712 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2713 // A/(1+B) and 2B/(1+B). This choice assumes that
2714 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2715 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2716 // TmpBB, but the math is more complicated.
2717
2718 auto NewTrueProb = TProb / 2;
2719 auto NewFalseProb = TProb / 2 + FProb;
2720 // Emit the LHS condition.
2721 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2722 NewFalseProb, InvertCond);
2723
2724 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2725 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2727 // Emit the RHS condition into TmpBB.
2728 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2729 Probs[1], InvertCond);
2730 } else {
2731 assert(Opc == Instruction::And && "Unknown merge op!");
2732 // Codegen X & Y as:
2733 // BB1:
2734 // jmp_if_X TmpBB
2735 // jmp FBB
2736 // TmpBB:
2737 // jmp_if_Y TBB
2738 // jmp FBB
2739 //
2740 // This requires creation of TmpBB after CurBB.
2741
2742 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2743 // The requirement is that
2744 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2745 // = FalseProb for original BB.
2746 // Assuming the original probabilities are A and B, one choice is to set
2747 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2748 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2749 // TrueProb for BB1 * FalseProb for TmpBB.
2750
2751 auto NewTrueProb = TProb + FProb / 2;
2752 auto NewFalseProb = FProb / 2;
2753 // Emit the LHS condition.
2754 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2755 NewFalseProb, InvertCond);
2756
2757 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2758 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2760 // Emit the RHS condition into TmpBB.
2761 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2762 Probs[1], InvertCond);
2763 }
2764}
2765
2766/// If the set of cases should be emitted as a series of branches, return true.
2767/// If we should emit this as a bunch of and/or'd together conditions, return
2768/// false.
2769bool
2770SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2771 if (Cases.size() != 2) return true;
2772
2773 // If this is two comparisons of the same values or'd or and'd together, they
2774 // will get folded into a single comparison, so don't emit two blocks.
2775 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2776 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2777 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2778 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2779 return false;
2780 }
2781
2782 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2783 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2784 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2785 Cases[0].CC == Cases[1].CC &&
2786 isa<Constant>(Cases[0].CmpRHS) &&
2787 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2788 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2789 return false;
2790 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2791 return false;
2792 }
2793
2794 return true;
2795}
2796
2797void SelectionDAGBuilder::visitUncondBr(const UncondBrInst &I) {
2799
2800 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2801
2802 // Update machine-CFG edges.
2803 BrMBB->addSuccessor(Succ0MBB);
2804
2805 // If this is not a fall-through branch or optimizations are switched off,
2806 // emit the branch.
2807 if (Succ0MBB != NextBlock(BrMBB) ||
2809 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
2810 DAG.getBasicBlock(Succ0MBB));
2811 setValue(&I, Br);
2812 DAG.setRoot(Br);
2813 }
2814}
2815
2816void SelectionDAGBuilder::visitCondBr(const CondBrInst &I) {
2817 MachineBasicBlock *BrMBB = FuncInfo.MBB;
2818
2819 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2820
2821 // If this condition is one of the special cases we handle, do special stuff
2822 // now.
2823 const Value *CondVal = I.getCondition();
2824 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2825
2826 // If this is a series of conditions that are or'd or and'd together, emit
2827 // this as a sequence of branches instead of setcc's with and/or operations.
2828 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2829 // unpredictable branches, and vector extracts because those jumps are likely
2830 // expensive for any target), this should improve performance.
2831 // For example, instead of something like:
2832 // cmp A, B
2833 // C = seteq
2834 // cmp D, E
2835 // F = setle
2836 // or C, F
2837 // jnz foo
2838 // Emit:
2839 // cmp A, B
2840 // je foo
2841 // cmp D, E
2842 // jle foo
2843 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2844 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2845 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2846 BOp->hasOneUse() && !IsUnpredictable) {
2847 Value *Vec;
2848 const Value *BOp0, *BOp1;
2850 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2851 Opcode = Instruction::And;
2852 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2853 Opcode = Instruction::Or;
2854
2855 if (Opcode &&
2856 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2857 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2859 FuncInfo, I, Opcode, BOp0, BOp1,
2860 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2861 Opcode, BOp0, BOp1))) {
2862 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2863 getEdgeProbability(BrMBB, Succ0MBB),
2864 getEdgeProbability(BrMBB, Succ1MBB),
2865 /*InvertCond=*/false);
2866 // If the compares in later blocks need to use values not currently
2867 // exported from this block, export them now. This block should always
2868 // be the first entry.
2869 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2870
2871 // Allow some cases to be rejected.
2872 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2873 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2874 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2875 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2876 }
2877
2878 // Emit the branch for this block.
2879 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2880 SL->SwitchCases.erase(SL->SwitchCases.begin());
2881 return;
2882 }
2883
2884 // Okay, we decided not to do this, remove any inserted MBB's and clear
2885 // SwitchCases.
2886 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2887 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2888
2889 SL->SwitchCases.clear();
2890 }
2891 }
2892
2893 // Create a CaseBlock record representing this branch.
2894 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2895 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2897 IsUnpredictable);
2898
2899 // Use visitSwitchCase to actually insert the fast branch sequence for this
2900 // cond branch.
2901 visitSwitchCase(CB, BrMBB);
2902}
2903
2904/// visitSwitchCase - Emits the necessary code to represent a single node in
2905/// the binary search tree resulting from lowering a switch instruction.
2907 MachineBasicBlock *SwitchBB) {
2908 SDValue Cond;
2909 SDValue CondLHS = getValue(CB.CmpLHS);
2910 SDLoc dl = CB.DL;
2911
2912 if (CB.CC == ISD::SETTRUE) {
2913 // Branch or fall through to TrueBB.
2914 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2915 SwitchBB->normalizeSuccProbs();
2916 if (CB.TrueBB != NextBlock(SwitchBB)) {
2917 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2918 DAG.getBasicBlock(CB.TrueBB)));
2919 }
2920 return;
2921 }
2922
2923 auto &TLI = DAG.getTargetLoweringInfo();
2924 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2925
2926 // Build the setcc now.
2927 if (!CB.CmpMHS) {
2928 // Fold "(X == true)" to X and "(X == false)" to !X to
2929 // handle common cases produced by branch lowering.
2930 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2931 CB.CC == ISD::SETEQ)
2932 Cond = CondLHS;
2933 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2934 CB.CC == ISD::SETEQ) {
2935 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2936 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2937 } else {
2938 SDValue CondRHS = getValue(CB.CmpRHS);
2939
2940 // If a pointer's DAG type is larger than its memory type then the DAG
2941 // values are zero-extended. This breaks signed comparisons so truncate
2942 // back to the underlying type before doing the compare.
2943 if (CondLHS.getValueType() != MemVT) {
2944 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2945 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2946 }
2947 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2948 }
2949 } else {
2950 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2951
2952 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2953 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2954
2955 SDValue CmpOp = getValue(CB.CmpMHS);
2956 EVT VT = CmpOp.getValueType();
2957
2958 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2959 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2960 ISD::SETLE);
2961 } else {
2962 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2963 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2964 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2965 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2966 }
2967 }
2968
2969 // Update successor info
2970 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2971 // TrueBB and FalseBB are always different unless the incoming IR is
2972 // degenerate. This only happens when running llc on weird IR.
2973 if (CB.TrueBB != CB.FalseBB)
2974 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2975 SwitchBB->normalizeSuccProbs();
2976
2977 // If the lhs block is the next block, invert the condition so that we can
2978 // fall through to the lhs instead of the rhs block.
2979 if (CB.TrueBB == NextBlock(SwitchBB)) {
2980 std::swap(CB.TrueBB, CB.FalseBB);
2981 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2982 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2983 }
2984
2985 SDNodeFlags Flags;
2987 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2988 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2989
2990 setValue(CurInst, BrCond);
2991
2992 // Insert the false branch. Do this even if it's a fall through branch,
2993 // this makes it easier to do DAG optimizations which require inverting
2994 // the branch condition.
2995 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2996 DAG.getBasicBlock(CB.FalseBB));
2997
2998 DAG.setRoot(BrCond);
2999}
3000
3001/// visitJumpTable - Emit JumpTable node in the current MBB
3003 // Emit the code for the jump table
3004 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3005 assert(JT.Reg && "Should lower JT Header first!");
3006 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
3007 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
3008 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
3009 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
3010 Index.getValue(1), Table, Index);
3011 DAG.setRoot(BrJumpTable);
3012}
3013
3014/// visitJumpTableHeader - This function emits necessary code to produce index
3015/// in the JumpTable from switch case.
3017 JumpTableHeader &JTH,
3018 MachineBasicBlock *SwitchBB) {
3019 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3020 const SDLoc &dl = *JT.SL;
3021
3022 // Subtract the lowest switch case value from the value being switched on.
3023 SDValue SwitchOp = getValue(JTH.SValue);
3024 EVT VT = SwitchOp.getValueType();
3025 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
3026 DAG.getConstant(JTH.First, dl, VT));
3027
3028 // The SDNode we just created, which holds the value being switched on minus
3029 // the smallest case value, needs to be copied to a virtual register so it
3030 // can be used as an index into the jump table in a subsequent basic block.
3031 // This value may be smaller or larger than the target's pointer type, and
3032 // therefore require extension or truncating.
3033 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3034 SwitchOp =
3035 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
3036
3037 Register JumpTableReg =
3038 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
3039 SDValue CopyTo =
3040 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3041 JT.Reg = JumpTableReg;
3042
3043 if (!JTH.FallthroughUnreachable) {
3044 // Emit the range check for the jump table, and branch to the default block
3045 // for the switch statement if the value being switched on exceeds the
3046 // largest case in the switch.
3047 SDValue CMP = DAG.getSetCC(
3048 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3049 Sub.getValueType()),
3050 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3051
3052 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3053 MVT::Other, CopyTo, CMP,
3054 DAG.getBasicBlock(JT.Default));
3055
3056 // Avoid emitting unnecessary branches to the next block.
3057 if (JT.MBB != NextBlock(SwitchBB))
3058 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3059 DAG.getBasicBlock(JT.MBB));
3060
3061 DAG.setRoot(BrCond);
3062 } else {
3063 // Avoid emitting unnecessary branches to the next block.
3064 if (JT.MBB != NextBlock(SwitchBB))
3065 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3066 DAG.getBasicBlock(JT.MBB)));
3067 else
3068 DAG.setRoot(CopyTo);
3069 }
3070}
3071
3072/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3073/// variable if there exists one.
3075 SDValue &Chain) {
3076 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3077 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3078 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3080 Value *Global =
3083 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3084 if (Global) {
3085 MachinePointerInfo MPInfo(Global);
3089 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3090 DAG.setNodeMemRefs(Node, {MemRef});
3091 }
3092 if (PtrTy != PtrMemTy)
3093 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3094 return SDValue(Node, 0);
3095}
3096
3097/// Codegen a new tail for a stack protector check ParentMBB which has had its
3098/// tail spliced into a stack protector check success bb.
3099///
3100/// For a high level explanation of how this fits into the stack protector
3101/// generation see the comment on the declaration of class
3102/// StackProtectorDescriptor.
3104 MachineBasicBlock *ParentBB) {
3105
3106 // First create the loads to the guard/stack slot for the comparison.
3107 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3108 auto &DL = DAG.getDataLayout();
3109 EVT PtrTy = TLI.getFrameIndexTy(DL);
3110 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3111
3112 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3113 int FI = MFI.getStackProtectorIndex();
3114
3115 SDValue Guard;
3116 SDLoc dl = getCurSDLoc();
3117 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3118 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3119 Align Align = DL.getPrefTypeAlign(
3120 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3121
3122 // Generate code to load the content of the guard slot.
3123 SDValue GuardVal = DAG.getLoad(
3124 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3125 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3127
3128 if (TLI.useStackGuardXorFP())
3129 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3130
3131 // If we're using function-based instrumentation, call the guard check
3132 // function
3134 // Get the guard check function from the target and verify it exists since
3135 // we're using function-based instrumentation
3136 const Function *GuardCheckFn =
3137 TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3138 assert(GuardCheckFn && "Guard check function is null");
3139
3140 // The target provides a guard check function to validate the guard value.
3141 // Generate a call to that function with the content of the guard slot as
3142 // argument.
3143 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3144 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3145
3147 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3148 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3149 Entry.IsInReg = true;
3150 Args.push_back(Entry);
3151
3154 .setChain(DAG.getEntryNode())
3155 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3156 getValue(GuardCheckFn), std::move(Args));
3157
3158 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3159 DAG.setRoot(Result.second);
3160 return;
3161 }
3162
3163 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3164 // Otherwise, emit a volatile load to retrieve the stack guard value.
3165 SDValue Chain = DAG.getEntryNode();
3166 if (TLI.useLoadStackGuardNode(M)) {
3167 Guard = getLoadStackGuard(DAG, dl, Chain);
3168 } else {
3169 if (const Value *IRGuard = TLI.getSDagStackGuard(M, DAG.getLibcalls())) {
3170 SDValue GuardPtr = getValue(IRGuard);
3171 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3172 MachinePointerInfo(IRGuard, 0), Align,
3174 } else {
3175 LLVMContext &Ctx = *DAG.getContext();
3176 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
3177 Guard = DAG.getPOISON(PtrMemTy);
3178 }
3179 }
3180
3181 // Perform the comparison via a getsetcc.
3182 SDValue Cmp = DAG.getSetCC(
3183 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3184 Guard, GuardVal, ISD::SETNE);
3185
3186 // If the guard/stackslot do not equal, branch to failure MBB.
3187 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
3188 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3189 // Otherwise branch to success MBB.
3190 SDValue Br = DAG.getNode(ISD::BR, dl,
3191 MVT::Other, BrCond,
3192 DAG.getBasicBlock(SPD.getSuccessMBB()));
3193
3194 DAG.setRoot(Br);
3195}
3196
3197/// Codegen the failure basic block for a stack protector check.
3198///
3199/// A failure stack protector machine basic block consists simply of a call to
3200/// __stack_chk_fail().
3201///
3202/// For a high level explanation of how this fits into the stack protector
3203/// generation see the comment on the declaration of class
3204/// StackProtectorDescriptor.
3207
3208 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3209 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3210 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3211 SDValue Chain;
3212
3213 // For -Oz builds with a guard check function, we use function-based
3214 // instrumentation. Otherwise, if we have a guard check function, we call it
3215 // in the failure block.
3216 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3217 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3218 // First create the loads to the guard/stack slot for the comparison.
3219 auto &DL = DAG.getDataLayout();
3220 EVT PtrTy = TLI.getFrameIndexTy(DL);
3221 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3222
3223 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3224 int FI = MFI.getStackProtectorIndex();
3225
3226 SDLoc dl = getCurSDLoc();
3227 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3228 Align Align = DL.getPrefTypeAlign(
3229 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3230
3231 // Generate code to load the content of the guard slot.
3232 SDValue GuardVal = DAG.getLoad(
3233 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3234 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3236
3237 if (TLI.useStackGuardXorFP())
3238 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3239
3240 // The target provides a guard check function to validate the guard value.
3241 // Generate a call to that function with the content of the guard slot as
3242 // argument.
3243 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3244 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3245
3247 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3248 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3249 Entry.IsInReg = true;
3250 Args.push_back(Entry);
3251
3254 .setChain(DAG.getEntryNode())
3255 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3256 getValue(GuardCheckFn), std::move(Args));
3257
3258 Chain = TLI.LowerCallTo(CLI).second;
3259 } else {
3261 CallOptions.setDiscardResult(true);
3262 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3263 {}, CallOptions, getCurSDLoc())
3264 .second;
3265 }
3266
3267 // Emit a trap instruction if we are required to do so.
3268 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3269 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3270 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3271
3272 DAG.setRoot(Chain);
3273}
3274
3275/// visitBitTestHeader - This function emits necessary code to produce value
3276/// suitable for "bit tests"
3278 MachineBasicBlock *SwitchBB) {
3279 SDLoc dl = getCurSDLoc();
3280
3281 // Subtract the minimum value.
3282 SDValue SwitchOp = getValue(B.SValue);
3283 EVT VT = SwitchOp.getValueType();
3284 SDValue RangeSub =
3285 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3286
3287 // Determine the type of the test operands.
3288 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3289 bool UsePtrType = false;
3290 if (!TLI.isTypeLegal(VT)) {
3291 UsePtrType = true;
3292 } else {
3293 for (const BitTestCase &Case : B.Cases)
3294 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3295 // Switch table case range are encoded into series of masks.
3296 // Just use pointer type, it's guaranteed to fit.
3297 UsePtrType = true;
3298 break;
3299 }
3300 }
3301 SDValue Sub = RangeSub;
3302 if (UsePtrType) {
3303 VT = TLI.getPointerTy(DAG.getDataLayout());
3304 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3305 }
3306
3307 B.RegVT = VT.getSimpleVT();
3308 B.Reg = FuncInfo.CreateReg(B.RegVT);
3309 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3310
3311 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3312
3313 if (!B.FallthroughUnreachable)
3314 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3315 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3316 SwitchBB->normalizeSuccProbs();
3317
3318 SDValue Root = CopyTo;
3319 if (!B.FallthroughUnreachable) {
3320 // Conditional branch to the default block.
3321 SDValue RangeCmp = DAG.getSetCC(dl,
3322 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3323 RangeSub.getValueType()),
3324 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3325 ISD::SETUGT);
3326
3327 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3328 DAG.getBasicBlock(B.Default));
3329 }
3330
3331 // Avoid emitting unnecessary branches to the next block.
3332 if (MBB != NextBlock(SwitchBB))
3333 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3334
3335 DAG.setRoot(Root);
3336}
3337
3338/// visitBitTestCase - this function produces one "bit test"
3340 MachineBasicBlock *NextMBB,
3341 BranchProbability BranchProbToNext,
3342 Register Reg, BitTestCase &B,
3343 MachineBasicBlock *SwitchBB) {
3344 SDLoc dl = getCurSDLoc();
3345 MVT VT = BB.RegVT;
3346 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3347 SDValue Cmp;
3348 unsigned PopCount = llvm::popcount(B.Mask);
3349 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3350 if (PopCount == 1) {
3351 // Testing for a single bit; just compare the shift count with what it
3352 // would need to be to shift a 1 bit in that position.
3353 Cmp = DAG.getSetCC(
3354 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3355 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3356 ISD::SETEQ);
3357 } else if (PopCount == BB.Range) {
3358 // There is only one zero bit in the range, test for it directly.
3359 Cmp = DAG.getSetCC(
3360 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3361 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3362 } else {
3363 // Make desired shift
3364 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3365 DAG.getConstant(1, dl, VT), ShiftOp);
3366
3367 // Emit bit tests and jumps
3368 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3369 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3370 Cmp = DAG.getSetCC(
3371 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3372 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3373 }
3374
3375 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3376 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3377 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3378 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3379 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3380 // one as they are relative probabilities (and thus work more like weights),
3381 // and hence we need to normalize them to let the sum of them become one.
3382 SwitchBB->normalizeSuccProbs();
3383
3384 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3385 MVT::Other, getControlRoot(),
3386 Cmp, DAG.getBasicBlock(B.TargetBB));
3387
3388 // Avoid emitting unnecessary branches to the next block.
3389 if (NextMBB != NextBlock(SwitchBB))
3390 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3391 DAG.getBasicBlock(NextMBB));
3392
3393 DAG.setRoot(BrAnd);
3394}
3395
3396void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3397 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3398
3399 // Retrieve successors. Look through artificial IR level blocks like
3400 // catchswitch for successors.
3401 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3402 const BasicBlock *EHPadBB = I.getSuccessor(1);
3403 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3404
3405 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3406 // have to do anything here to lower funclet bundles.
3407 failForInvalidBundles(I, "invokes",
3413
3414 const Value *Callee(I.getCalledOperand());
3415 const Function *Fn = dyn_cast<Function>(Callee);
3416 if (isa<InlineAsm>(Callee))
3417 visitInlineAsm(I, EHPadBB);
3418 else if (Fn && Fn->isIntrinsic()) {
3419 switch (Fn->getIntrinsicID()) {
3420 default:
3421 llvm_unreachable("Cannot invoke this intrinsic");
3422 case Intrinsic::donothing:
3423 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3424 case Intrinsic::seh_try_begin:
3425 case Intrinsic::seh_scope_begin:
3426 case Intrinsic::seh_try_end:
3427 case Intrinsic::seh_scope_end:
3428 if (EHPadMBB)
3429 // a block referenced by EH table
3430 // so dtor-funclet not removed by opts
3431 EHPadMBB->setMachineBlockAddressTaken();
3432 break;
3433 case Intrinsic::experimental_patchpoint_void:
3434 case Intrinsic::experimental_patchpoint:
3435 visitPatchpoint(I, EHPadBB);
3436 break;
3437 case Intrinsic::experimental_gc_statepoint:
3439 break;
3440 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3441 // but these intrinsics are special because they can be invoked, so we
3442 // manually lower it to a DAG node here.
3443 case Intrinsic::wasm_throw: {
3445 std::array<SDValue, 4> Ops = {
3446 getControlRoot(), // inchain for the terminator node
3447 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3449 getValue(I.getArgOperand(0)), // tag
3450 getValue(I.getArgOperand(1)) // thrown value
3451 };
3452 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3453 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3454 break;
3455 }
3456 case Intrinsic::wasm_rethrow: {
3457 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3458 std::array<SDValue, 2> Ops = {
3459 getControlRoot(), // inchain for the terminator node
3460 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3461 TLI.getPointerTy(DAG.getDataLayout()))};
3462 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3463 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3464 break;
3465 }
3466 }
3467 } else if (I.hasDeoptState()) {
3468 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3469 // Eventually we will support lowering the @llvm.experimental.deoptimize
3470 // intrinsic, and right now there are no plans to support other intrinsics
3471 // with deopt state.
3472 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3473 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3475 } else {
3476 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3477 }
3478
3479 // If the value of the invoke is used outside of its defining block, make it
3480 // available as a virtual register.
3481 // We already took care of the exported value for the statepoint instruction
3482 // during call to the LowerStatepoint.
3483 if (!isa<GCStatepointInst>(I)) {
3485 }
3486
3488 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3489 BranchProbability EHPadBBProb =
3490 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3492 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3493
3494 // Update successor info.
3495 addSuccessorWithProb(InvokeMBB, Return);
3496 for (auto &UnwindDest : UnwindDests) {
3497 UnwindDest.first->setIsEHPad();
3498 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3499 }
3500 InvokeMBB->normalizeSuccProbs();
3501
3502 // Drop into normal successor.
3503 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3504 DAG.getBasicBlock(Return)));
3505}
3506
3507/// The intrinsics currently supported by callbr are implicit control flow
3508/// intrinsics such as amdgcn.kill.
3509/// - they should be called (no "dontcall-" attributes)
3510/// - they do not touch memory on the target (= !TLI.getTgtMemIntrinsic())
3511/// - they do not need custom argument handling (no
3512/// TLI.CollectTargetIntrinsicOperands())
3513void SelectionDAGBuilder::visitCallBrIntrinsic(const CallBrInst &I) {
3514#ifndef NDEBUG
3516 DAG.getTargetLoweringInfo().getTgtMemIntrinsic(
3517 Infos, I, DAG.getMachineFunction(), I.getIntrinsicID());
3518 assert(Infos.empty() && "Intrinsic touches memory");
3519#endif
3520
3521 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
3522
3524 getTargetIntrinsicOperands(I, HasChain, OnlyLoad);
3525 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
3526
3527 // Create the node.
3528 SDValue Result =
3529 getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
3530 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
3531
3532 setValue(&I, Result);
3533}
3534
3535void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3536 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3537
3538 if (I.isInlineAsm()) {
3539 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3540 // have to do anything here to lower funclet bundles.
3541 failForInvalidBundles(I, "callbrs",
3543 visitInlineAsm(I);
3544 } else {
3545 assert(!I.hasOperandBundles() &&
3546 "Can't have operand bundles for intrinsics");
3547 visitCallBrIntrinsic(I);
3548 }
3550
3551 // Retrieve successors.
3552 SmallPtrSet<BasicBlock *, 8> Dests;
3553 Dests.insert(I.getDefaultDest());
3554 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3555
3556 // Update successor info.
3557 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3558 // TODO: For most of the cases where there is an intrinsic callbr, we're
3559 // having exactly one indirect target, which will be unreachable. As soon as
3560 // this changes, we might need to enhance
3561 // Target->setIsInlineAsmBrIndirectTarget or add something similar for
3562 // intrinsic indirect branches.
3563 if (I.isInlineAsm()) {
3564 for (BasicBlock *Dest : I.getIndirectDests()) {
3565 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
3566 Target->setIsInlineAsmBrIndirectTarget();
3567 // If we introduce a type of asm goto statement that is permitted to use
3568 // an indirect call instruction to jump to its labels, then we should add
3569 // a call to Target->setMachineBlockAddressTaken() here, to mark the
3570 // target block as requiring a BTI.
3571
3572 Target->setLabelMustBeEmitted();
3573 // Don't add duplicate machine successors.
3574 if (Dests.insert(Dest).second)
3575 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3576 }
3577 }
3578 CallBrMBB->normalizeSuccProbs();
3579
3580 // Drop into default successor.
3581 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3582 MVT::Other, getControlRoot(),
3583 DAG.getBasicBlock(Return)));
3584}
3585
3586void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3587 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3588}
3589
3590void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3591 assert(FuncInfo.MBB->isEHPad() &&
3592 "Call to landingpad not in landing pad!");
3593
3594 // If there aren't registers to copy the values into (e.g., during SjLj
3595 // exceptions), then don't bother to create these DAG nodes.
3596 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3597 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3598 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3599 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3600 return;
3601
3602 // If landingpad's return type is token type, we don't create DAG nodes
3603 // for its exception pointer and selector value. The extraction of exception
3604 // pointer or selector value from token type landingpads is not currently
3605 // supported.
3606 if (LP.getType()->isTokenTy())
3607 return;
3608
3609 SmallVector<EVT, 2> ValueVTs;
3610 SDLoc dl = getCurSDLoc();
3611 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3612 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3613
3614 // Get the two live-in registers as SDValues. The physregs have already been
3615 // copied into virtual registers.
3616 SDValue Ops[2];
3617 if (FuncInfo.ExceptionPointerVirtReg) {
3618 Ops[0] = DAG.getZExtOrTrunc(
3619 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3620 FuncInfo.ExceptionPointerVirtReg,
3621 TLI.getPointerTy(DAG.getDataLayout())),
3622 dl, ValueVTs[0]);
3623 } else {
3624 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3625 }
3626 Ops[1] = DAG.getZExtOrTrunc(
3627 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3628 FuncInfo.ExceptionSelectorVirtReg,
3629 TLI.getPointerTy(DAG.getDataLayout())),
3630 dl, ValueVTs[1]);
3631
3632 // Merge into one.
3633 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3634 DAG.getVTList(ValueVTs), Ops);
3635 setValue(&LP, Res);
3636}
3637
3640 // Update JTCases.
3641 for (JumpTableBlock &JTB : SL->JTCases)
3642 if (JTB.first.HeaderBB == First)
3643 JTB.first.HeaderBB = Last;
3644
3645 // Update BitTestCases.
3646 for (BitTestBlock &BTB : SL->BitTestCases)
3647 if (BTB.Parent == First)
3648 BTB.Parent = Last;
3649}
3650
3651void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3652 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3653
3654 // Update machine-CFG edges with unique successors.
3656 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3657 BasicBlock *BB = I.getSuccessor(i);
3658 bool Inserted = Done.insert(BB).second;
3659 if (!Inserted)
3660 continue;
3661
3662 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3663 addSuccessorWithProb(IndirectBrMBB, Succ);
3664 }
3665 IndirectBrMBB->normalizeSuccProbs();
3666
3668 MVT::Other, getControlRoot(),
3669 getValue(I.getAddress())));
3670}
3671
3672void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3673 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3674 DAG.getTarget().Options.NoTrapAfterNoreturn))
3675 return;
3676
3677 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3678}
3679
3680void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3681 SDNodeFlags Flags;
3682 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3683 Flags.copyFMF(*FPOp);
3684
3685 SDValue Op = getValue(I.getOperand(0));
3686 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3687 Op, Flags);
3688 setValue(&I, UnNodeValue);
3689}
3690
3691void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3692 SDNodeFlags Flags;
3693 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3694 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3695 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3696 }
3697 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3698 Flags.setExact(ExactOp->isExact());
3699 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3700 Flags.setDisjoint(DisjointOp->isDisjoint());
3701 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3702 Flags.copyFMF(*FPOp);
3703
3704 SDValue Op1 = getValue(I.getOperand(0));
3705 SDValue Op2 = getValue(I.getOperand(1));
3706 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3707 Op1, Op2, Flags);
3708 setValue(&I, BinNodeValue);
3709}
3710
3711void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3712 SDValue Op1 = getValue(I.getOperand(0));
3713 SDValue Op2 = getValue(I.getOperand(1));
3714
3715 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3716 Op1.getValueType(), DAG.getDataLayout());
3717
3718 // Coerce the shift amount to the right type if we can. This exposes the
3719 // truncate or zext to optimization early.
3720 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3722 "Unexpected shift type");
3723 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3724 }
3725
3726 bool nuw = false;
3727 bool nsw = false;
3728 bool exact = false;
3729
3730 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3731
3732 if (const OverflowingBinaryOperator *OFBinOp =
3734 nuw = OFBinOp->hasNoUnsignedWrap();
3735 nsw = OFBinOp->hasNoSignedWrap();
3736 }
3737 if (const PossiblyExactOperator *ExactOp =
3739 exact = ExactOp->isExact();
3740 }
3741 SDNodeFlags Flags;
3742 Flags.setExact(exact);
3743 Flags.setNoSignedWrap(nsw);
3744 Flags.setNoUnsignedWrap(nuw);
3745 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3746 Flags);
3747 setValue(&I, Res);
3748}
3749
3750void SelectionDAGBuilder::visitSDiv(const User &I) {
3751 SDValue Op1 = getValue(I.getOperand(0));
3752 SDValue Op2 = getValue(I.getOperand(1));
3753
3754 SDNodeFlags Flags;
3755 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3756 cast<PossiblyExactOperator>(&I)->isExact());
3757 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3758 Op2, Flags));
3759}
3760
3761void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3762 ICmpInst::Predicate predicate = I.getPredicate();
3763 SDValue Op1 = getValue(I.getOperand(0));
3764 SDValue Op2 = getValue(I.getOperand(1));
3765 ISD::CondCode Opcode = getICmpCondCode(predicate);
3766
3767 auto &TLI = DAG.getTargetLoweringInfo();
3768 EVT MemVT =
3769 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3770
3771 // If a pointer's DAG type is larger than its memory type then the DAG values
3772 // are zero-extended. This breaks signed comparisons so truncate back to the
3773 // underlying type before doing the compare.
3774 if (Op1.getValueType() != MemVT) {
3775 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3776 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3777 }
3778
3779 SDNodeFlags Flags;
3780 Flags.setSameSign(I.hasSameSign());
3781
3782 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3783 I.getType());
3784 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode,
3785 /*Chain=*/{}, /*IsSignaling=*/false, Flags));
3786}
3787
3788void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3789 FCmpInst::Predicate predicate = I.getPredicate();
3790 SDValue Op1 = getValue(I.getOperand(0));
3791 SDValue Op2 = getValue(I.getOperand(1));
3792
3793 ISD::CondCode Condition = getFCmpCondCode(predicate);
3794 auto *FPMO = cast<FPMathOperator>(&I);
3795 if (FPMO->hasNoNaNs() ||
3796 (DAG.isKnownNeverNaN(Op1) && DAG.isKnownNeverNaN(Op2)))
3797 Condition = getFCmpCodeWithoutNaN(Condition);
3798
3799 SDNodeFlags Flags;
3800 Flags.copyFMF(*FPMO);
3801
3802 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3803 I.getType());
3804 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition,
3805 /*Chain=*/{}, /*IsSignaling=*/false, Flags));
3806}
3807
3808// Check if the condition of the select has one use or two users that are both
3809// selects with the same condition.
3810static bool hasOnlySelectUsers(const Value *Cond) {
3811 return llvm::all_of(Cond->users(), [](const Value *V) {
3812 return isa<SelectInst>(V);
3813 });
3814}
3815
3816void SelectionDAGBuilder::visitSelect(const User &I) {
3817 SmallVector<EVT, 4> ValueVTs;
3818 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3819 ValueVTs);
3820 unsigned NumValues = ValueVTs.size();
3821 if (NumValues == 0) return;
3822
3823 SmallVector<SDValue, 4> Values(NumValues);
3824 SDValue Cond = getValue(I.getOperand(0));
3825 SDValue LHSVal = getValue(I.getOperand(1));
3826 SDValue RHSVal = getValue(I.getOperand(2));
3827 SmallVector<SDValue, 1> BaseOps(1, Cond);
3829 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3830
3831 bool IsUnaryAbs = false;
3832 bool Negate = false;
3833
3834 SDNodeFlags Flags;
3835 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3836 Flags.copyFMF(*FPOp);
3837
3838 Flags.setUnpredictable(
3839 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3840
3841 // Min/max matching is only viable if all output VTs are the same.
3842 if (all_equal(ValueVTs)) {
3843 EVT VT = ValueVTs[0];
3844 LLVMContext &Ctx = *DAG.getContext();
3845 auto &TLI = DAG.getTargetLoweringInfo();
3846
3847 // We care about the legality of the operation after it has been type
3848 // legalized.
3849 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3850 VT = TLI.getTypeToTransformTo(Ctx, VT);
3851
3852 // If the vselect is legal, assume we want to leave this as a vector setcc +
3853 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3854 // min/max is legal on the scalar type.
3855 bool UseScalarMinMax = VT.isVector() &&
3857
3858 // ValueTracking's select pattern matching does not account for -0.0,
3859 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3860 // -0.0 is less than +0.0.
3861 const Value *LHS, *RHS;
3862 auto SPR = matchSelectPattern(&I, LHS, RHS);
3864 switch (SPR.Flavor) {
3865 case SPF_UMAX: Opc = ISD::UMAX; break;
3866 case SPF_UMIN: Opc = ISD::UMIN; break;
3867 case SPF_SMAX: Opc = ISD::SMAX; break;
3868 case SPF_SMIN: Opc = ISD::SMIN; break;
3869 case SPF_FMINNUM:
3871 break;
3872
3873 switch (SPR.NaNBehavior) {
3874 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3875 case SPNB_RETURNS_NAN: break;
3876 case SPNB_RETURNS_OTHER:
3878 Flags.setNoSignedZeros(true);
3879 break;
3880 case SPNB_RETURNS_ANY:
3882 (UseScalarMinMax &&
3884 Opc = ISD::FMINNUM;
3885 break;
3886 }
3887 break;
3888 case SPF_FMAXNUM:
3890 break;
3891
3892 switch (SPR.NaNBehavior) {
3893 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3894 case SPNB_RETURNS_NAN: break;
3895 case SPNB_RETURNS_OTHER:
3897 Flags.setNoSignedZeros(true);
3898 break;
3899 case SPNB_RETURNS_ANY:
3901 (UseScalarMinMax &&
3903 Opc = ISD::FMAXNUM;
3904 break;
3905 }
3906 break;
3907 case SPF_NABS:
3908 Negate = true;
3909 [[fallthrough]];
3910 case SPF_ABS:
3911 IsUnaryAbs = true;
3912 Opc = ISD::ABS;
3913 break;
3914 default: break;
3915 }
3916
3917 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3918 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3919 (UseScalarMinMax &&
3921 // If the underlying comparison instruction is used by any other
3922 // instruction, the consumed instructions won't be destroyed, so it is
3923 // not profitable to convert to a min/max.
3925 OpCode = Opc;
3926 LHSVal = getValue(LHS);
3927 RHSVal = getValue(RHS);
3928 BaseOps.clear();
3929 }
3930
3931 if (IsUnaryAbs) {
3932 OpCode = Opc;
3933 LHSVal = getValue(LHS);
3934 BaseOps.clear();
3935 }
3936 }
3937
3938 if (IsUnaryAbs) {
3939 for (unsigned i = 0; i != NumValues; ++i) {
3940 SDLoc dl = getCurSDLoc();
3941 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3942 Values[i] =
3943 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3944 if (Negate)
3945 Values[i] = DAG.getNegative(Values[i], dl, VT);
3946 }
3947 } else {
3948 for (unsigned i = 0; i != NumValues; ++i) {
3949 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3950 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3951 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3952 Values[i] = DAG.getNode(
3953 OpCode, getCurSDLoc(),
3954 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3955 }
3956 }
3957
3959 DAG.getVTList(ValueVTs), Values));
3960}
3961
3962void SelectionDAGBuilder::visitTrunc(const User &I) {
3963 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3964 SDValue N = getValue(I.getOperand(0));
3965 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3966 I.getType());
3967 SDNodeFlags Flags;
3968 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3969 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
3970 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
3971 }
3972
3973 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
3974}
3975
3976void SelectionDAGBuilder::visitZExt(const User &I) {
3977 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3978 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3979 SDValue N = getValue(I.getOperand(0));
3980 auto &TLI = DAG.getTargetLoweringInfo();
3981 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3982
3983 SDNodeFlags Flags;
3984 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3985 Flags.setNonNeg(PNI->hasNonNeg());
3986
3987 // Eagerly use nonneg information to canonicalize towards sign_extend if
3988 // that is the target's preference.
3989 // TODO: Let the target do this later.
3990 if (Flags.hasNonNeg() &&
3991 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3992 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3993 return;
3994 }
3995
3996 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3997}
3998
3999void SelectionDAGBuilder::visitSExt(const User &I) {
4000 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
4001 // SExt also can't be a cast to bool for same reason. So, nothing much to do
4002 SDValue N = getValue(I.getOperand(0));
4003 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4004 I.getType());
4005 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
4006}
4007
4008void SelectionDAGBuilder::visitFPTrunc(const User &I) {
4009 // FPTrunc is never a no-op cast, no need to check
4010 SDValue N = getValue(I.getOperand(0));
4011 SDLoc dl = getCurSDLoc();
4012 SDNodeFlags Flags;
4013 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4014 Flags.copyFMF(*FPOp);
4015 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4016 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4017 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
4018 DAG.getTargetConstant(
4019 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
4020 Flags));
4021}
4022
4023void SelectionDAGBuilder::visitFPExt(const User &I) {
4024 // FPExt is never a no-op cast, no need to check
4025 SDValue N = getValue(I.getOperand(0));
4026 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4027 I.getType());
4028 SDNodeFlags Flags;
4029 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4030 Flags.copyFMF(*FPOp);
4031 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N, Flags));
4032}
4033
4034void SelectionDAGBuilder::visitFPToUI(const User &I) {
4035 // FPToUI is never a no-op cast, no need to check
4036 SDValue N = getValue(I.getOperand(0));
4037 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4038 I.getType());
4039 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
4040}
4041
4042void SelectionDAGBuilder::visitFPToSI(const User &I) {
4043 // FPToSI is never a no-op cast, no need to check
4044 SDValue N = getValue(I.getOperand(0));
4045 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4046 I.getType());
4047 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
4048}
4049
4050void SelectionDAGBuilder::visitUIToFP(const User &I) {
4051 // UIToFP is never a no-op cast, no need to check
4052 SDValue N = getValue(I.getOperand(0));
4053 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4054 I.getType());
4055 SDNodeFlags Flags;
4056 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
4057 Flags.setNonNeg(PNI->hasNonNeg());
4058
4059 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4060}
4061
4062void SelectionDAGBuilder::visitSIToFP(const User &I) {
4063 // SIToFP is never a no-op cast, no need to check
4064 SDValue N = getValue(I.getOperand(0));
4065 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4066 I.getType());
4067 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
4068}
4069
4070void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
4071 SDValue N = getValue(I.getOperand(0));
4072 // By definition the type of the ptrtoaddr must be equal to the address type.
4073 const auto &TLI = DAG.getTargetLoweringInfo();
4074 EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4075 // The address width must be smaller or equal to the pointer representation
4076 // width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
4077 N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
4078 setValue(&I, N);
4079}
4080
4081void SelectionDAGBuilder::visitPtrToInt(const User &I) {
4082 // What to do depends on the size of the integer and the size of the pointer.
4083 // We can either truncate, zero extend, or no-op, accordingly.
4084 SDValue N = getValue(I.getOperand(0));
4085 auto &TLI = DAG.getTargetLoweringInfo();
4086 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4087 I.getType());
4088 EVT PtrMemVT =
4089 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
4090 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4091 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
4092 setValue(&I, N);
4093}
4094
4095void SelectionDAGBuilder::visitIntToPtr(const User &I) {
4096 // What to do depends on the size of the integer and the size of the pointer.
4097 // We can either truncate, zero extend, or no-op, accordingly.
4098 SDValue N = getValue(I.getOperand(0));
4099 auto &TLI = DAG.getTargetLoweringInfo();
4100 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4101 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4102 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4103 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4104 setValue(&I, N);
4105}
4106
4107void SelectionDAGBuilder::visitBitCast(const User &I) {
4108 SDValue N = getValue(I.getOperand(0));
4109 SDLoc dl = getCurSDLoc();
4110 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4111 I.getType());
4112
4113 // BitCast assures us that source and destination are the same size so this is
4114 // either a BITCAST or a no-op.
4115 if (DestVT != N.getValueType())
4116 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
4117 DestVT, N)); // convert types.
4118 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4119 // might fold any kind of constant expression to an integer constant and that
4120 // is not what we are looking for. Only recognize a bitcast of a genuine
4121 // constant integer as an opaque constant.
4122 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4123 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4124 /*isOpaque*/true));
4125 else
4126 setValue(&I, N); // noop cast.
4127}
4128
4129void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4130 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4131 const Value *SV = I.getOperand(0);
4132 SDValue N = getValue(SV);
4133 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4134
4135 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4136 unsigned DestAS = I.getType()->getPointerAddressSpace();
4137
4138 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4139 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4140
4141 setValue(&I, N);
4142}
4143
4144void SelectionDAGBuilder::visitInsertElement(const User &I) {
4145 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4146 SDValue InVec = getValue(I.getOperand(0));
4147 SDValue InVal = getValue(I.getOperand(1));
4148 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4149 TLI.getVectorIdxTy(DAG.getDataLayout()));
4151 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4152 InVec, InVal, InIdx));
4153}
4154
4155void SelectionDAGBuilder::visitExtractElement(const User &I) {
4156 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4157 SDValue InVec = getValue(I.getOperand(0));
4158 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4159 TLI.getVectorIdxTy(DAG.getDataLayout()));
4161 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4162 InVec, InIdx));
4163}
4164
4165void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4166 SDValue Src1 = getValue(I.getOperand(0));
4167 SDValue Src2 = getValue(I.getOperand(1));
4168 ArrayRef<int> Mask;
4169 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4170 Mask = SVI->getShuffleMask();
4171 else
4172 Mask = cast<ConstantExpr>(I).getShuffleMask();
4173 SDLoc DL = getCurSDLoc();
4174 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4175 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4176 EVT SrcVT = Src1.getValueType();
4177
4178 if (all_of(Mask, equal_to(0)) && VT.isScalableVector()) {
4179 // Canonical splat form of first element of first input vector.
4180 SDValue FirstElt =
4181 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4182 DAG.getVectorIdxConstant(0, DL));
4183 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4184 return;
4185 }
4186
4187 // For now, we only handle splats for scalable vectors.
4188 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4189 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4190 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4191
4192 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4193 unsigned MaskNumElts = Mask.size();
4194
4195 if (SrcNumElts == MaskNumElts) {
4196 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4197 return;
4198 }
4199
4200 // Normalize the shuffle vector since mask and vector length don't match.
4201 if (SrcNumElts < MaskNumElts) {
4202 // Mask is longer than the source vectors. We can use concatenate vector to
4203 // make the mask and vectors lengths match.
4204
4205 if (MaskNumElts % SrcNumElts == 0) {
4206 // Mask length is a multiple of the source vector length.
4207 // Check if the shuffle is some kind of concatenation of the input
4208 // vectors.
4209 unsigned NumConcat = MaskNumElts / SrcNumElts;
4210 bool IsConcat = true;
4211 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4212 for (unsigned i = 0; i != MaskNumElts; ++i) {
4213 int Idx = Mask[i];
4214 if (Idx < 0)
4215 continue;
4216 // Ensure the indices in each SrcVT sized piece are sequential and that
4217 // the same source is used for the whole piece.
4218 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4219 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4220 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4221 IsConcat = false;
4222 break;
4223 }
4224 // Remember which source this index came from.
4225 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4226 }
4227
4228 // The shuffle is concatenating multiple vectors together. Just emit
4229 // a CONCAT_VECTORS operation.
4230 if (IsConcat) {
4231 SmallVector<SDValue, 8> ConcatOps;
4232 for (auto Src : ConcatSrcs) {
4233 if (Src < 0)
4234 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4235 else if (Src == 0)
4236 ConcatOps.push_back(Src1);
4237 else
4238 ConcatOps.push_back(Src2);
4239 }
4240 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4241 return;
4242 }
4243 }
4244
4245 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4246 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4247 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4248 PaddedMaskNumElts);
4249
4250 // Pad both vectors with undefs to make them the same length as the mask.
4251 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4252
4253 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4254 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4255 MOps1[0] = Src1;
4256 MOps2[0] = Src2;
4257
4258 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4259 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4260
4261 // Readjust mask for new input vector length.
4262 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4263 for (unsigned i = 0; i != MaskNumElts; ++i) {
4264 int Idx = Mask[i];
4265 if (Idx >= (int)SrcNumElts)
4266 Idx -= SrcNumElts - PaddedMaskNumElts;
4267 MappedOps[i] = Idx;
4268 }
4269
4270 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4271
4272 // If the concatenated vector was padded, extract a subvector with the
4273 // correct number of elements.
4274 if (MaskNumElts != PaddedMaskNumElts)
4275 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4276 DAG.getVectorIdxConstant(0, DL));
4277
4278 setValue(&I, Result);
4279 return;
4280 }
4281
4282 assert(SrcNumElts > MaskNumElts);
4283
4284 // Analyze the access pattern of the vector to see if we can extract
4285 // two subvectors and do the shuffle.
4286 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4287 bool CanExtract = true;
4288 for (int Idx : Mask) {
4289 unsigned Input = 0;
4290 if (Idx < 0)
4291 continue;
4292
4293 if (Idx >= (int)SrcNumElts) {
4294 Input = 1;
4295 Idx -= SrcNumElts;
4296 }
4297
4298 // If all the indices come from the same MaskNumElts sized portion of
4299 // the sources we can use extract. Also make sure the extract wouldn't
4300 // extract past the end of the source.
4301 int NewStartIdx = alignDown(Idx, MaskNumElts);
4302 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4303 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4304 CanExtract = false;
4305 // Make sure we always update StartIdx as we use it to track if all
4306 // elements are undef.
4307 StartIdx[Input] = NewStartIdx;
4308 }
4309
4310 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4311 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4312 return;
4313 }
4314 if (CanExtract) {
4315 // Extract appropriate subvector and generate a vector shuffle
4316 for (unsigned Input = 0; Input < 2; ++Input) {
4317 SDValue &Src = Input == 0 ? Src1 : Src2;
4318 if (StartIdx[Input] < 0)
4319 Src = DAG.getUNDEF(VT);
4320 else {
4321 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4322 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4323 }
4324 }
4325
4326 // Calculate new mask.
4327 SmallVector<int, 8> MappedOps(Mask);
4328 for (int &Idx : MappedOps) {
4329 if (Idx >= (int)SrcNumElts)
4330 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4331 else if (Idx >= 0)
4332 Idx -= StartIdx[0];
4333 }
4334
4335 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4336 return;
4337 }
4338
4339 // We can't use either concat vectors or extract subvectors so fall back to
4340 // replacing the shuffle with extract and build vector.
4341 // to insert and build vector.
4342 EVT EltVT = VT.getVectorElementType();
4344 for (int Idx : Mask) {
4345 SDValue Res;
4346
4347 if (Idx < 0) {
4348 Res = DAG.getUNDEF(EltVT);
4349 } else {
4350 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4351 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4352
4353 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4354 DAG.getVectorIdxConstant(Idx, DL));
4355 }
4356
4357 Ops.push_back(Res);
4358 }
4359
4360 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4361}
4362
4363void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4364 ArrayRef<unsigned> Indices = I.getIndices();
4365 const Value *Op0 = I.getOperand(0);
4366 const Value *Op1 = I.getOperand(1);
4367 Type *AggTy = I.getType();
4368 Type *ValTy = Op1->getType();
4369 bool IntoUndef = isa<UndefValue>(Op0);
4370 bool FromUndef = isa<UndefValue>(Op1);
4371
4372 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4373
4374 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4375 SmallVector<EVT, 4> AggValueVTs;
4376 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4377 SmallVector<EVT, 4> ValValueVTs;
4378 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4379
4380 unsigned NumAggValues = AggValueVTs.size();
4381 unsigned NumValValues = ValValueVTs.size();
4382 SmallVector<SDValue, 4> Values(NumAggValues);
4383
4384 // Ignore an insertvalue that produces an empty object
4385 if (!NumAggValues) {
4386 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4387 return;
4388 }
4389
4390 SDValue Agg = getValue(Op0);
4391 unsigned i = 0;
4392 // Copy the beginning value(s) from the original aggregate.
4393 for (; i != LinearIndex; ++i)
4394 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4395 SDValue(Agg.getNode(), Agg.getResNo() + i);
4396 // Copy values from the inserted value(s).
4397 if (NumValValues) {
4398 SDValue Val = getValue(Op1);
4399 for (; i != LinearIndex + NumValValues; ++i)
4400 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4401 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4402 }
4403 // Copy remaining value(s) from the original aggregate.
4404 for (; i != NumAggValues; ++i)
4405 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4406 SDValue(Agg.getNode(), Agg.getResNo() + i);
4407
4409 DAG.getVTList(AggValueVTs), Values));
4410}
4411
4412void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4413 ArrayRef<unsigned> Indices = I.getIndices();
4414 const Value *Op0 = I.getOperand(0);
4415 Type *AggTy = Op0->getType();
4416 Type *ValTy = I.getType();
4417 bool OutOfUndef = isa<UndefValue>(Op0);
4418
4419 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4420
4421 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4422 SmallVector<EVT, 4> ValValueVTs;
4423 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4424
4425 unsigned NumValValues = ValValueVTs.size();
4426
4427 // Ignore a extractvalue that produces an empty object
4428 if (!NumValValues) {
4429 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4430 return;
4431 }
4432
4433 SmallVector<SDValue, 4> Values(NumValValues);
4434
4435 SDValue Agg = getValue(Op0);
4436 // Copy out the selected value(s).
4437 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4438 Values[i - LinearIndex] =
4439 OutOfUndef ?
4440 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4441 SDValue(Agg.getNode(), Agg.getResNo() + i);
4442
4444 DAG.getVTList(ValValueVTs), Values));
4445}
4446
4447void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4448 Value *Op0 = I.getOperand(0);
4449 // Note that the pointer operand may be a vector of pointers. Take the scalar
4450 // element which holds a pointer.
4451 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4452 SDValue N = getValue(Op0);
4453 SDLoc dl = getCurSDLoc();
4454 auto &TLI = DAG.getTargetLoweringInfo();
4455 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4456
4457 // For a vector GEP, keep the prefix scalar as long as possible, then
4458 // convert any scalars encountered after the first vector operand to vectors.
4459 bool IsVectorGEP = I.getType()->isVectorTy();
4460 ElementCount VectorElementCount =
4461 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4463
4465 GTI != E; ++GTI) {
4466 const Value *Idx = GTI.getOperand();
4467 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4468 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4469 if (Field) {
4470 // N = N + Offset
4471 uint64_t Offset =
4472 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4473
4474 // In an inbounds GEP with an offset that is nonnegative even when
4475 // interpreted as signed, assume there is no unsigned overflow.
4476 SDNodeFlags Flags;
4477 if (NW.hasNoUnsignedWrap() ||
4478 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4480 Flags.setInBounds(NW.isInBounds());
4481
4482 N = DAG.getMemBasePlusOffset(
4483 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4484 }
4485 } else {
4486 // IdxSize is the width of the arithmetic according to IR semantics.
4487 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4488 // (and fix up the result later).
4489 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4490 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4491 TypeSize ElementSize =
4492 GTI.getSequentialElementStride(DAG.getDataLayout());
4493 // We intentionally mask away the high bits here; ElementSize may not
4494 // fit in IdxTy.
4495 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4496 /*isSigned=*/false, /*implicitTrunc=*/true);
4497 bool ElementScalable = ElementSize.isScalable();
4498
4499 // If this is a scalar constant or a splat vector of constants,
4500 // handle it quickly.
4501 const auto *C = dyn_cast<Constant>(Idx);
4502 if (C && isa<VectorType>(C->getType()))
4503 C = C->getSplatValue();
4504
4505 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4506 if (CI && CI->isZero())
4507 continue;
4508 if (CI && !ElementScalable) {
4509 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4510 LLVMContext &Context = *DAG.getContext();
4511 SDValue OffsVal;
4512 if (N.getValueType().isVector())
4513 OffsVal = DAG.getConstant(
4514 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4515 else
4516 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4517
4518 // In an inbounds GEP with an offset that is nonnegative even when
4519 // interpreted as signed, assume there is no unsigned overflow.
4520 SDNodeFlags Flags;
4521 if (NW.hasNoUnsignedWrap() ||
4522 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4523 Flags.setNoUnsignedWrap(true);
4524 Flags.setInBounds(NW.isInBounds());
4525
4526 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4527
4528 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4529 continue;
4530 }
4531
4532 // N = N + Idx * ElementMul;
4533 SDValue IdxN = getValue(Idx);
4534
4535 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4536 if (N.getValueType().isVector()) {
4537 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4538 VectorElementCount);
4539 IdxN = DAG.getSplat(VT, dl, IdxN);
4540 } else {
4541 EVT VT =
4542 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4543 N = DAG.getSplat(VT, dl, N);
4544 }
4545 }
4546
4547 // If the index is smaller or larger than intptr_t, truncate or extend
4548 // it.
4549 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4550
4551 SDNodeFlags ScaleFlags;
4552 // The multiplication of an index by the type size does not wrap the
4553 // pointer index type in a signed sense (mul nsw).
4555
4556 // The multiplication of an index by the type size does not wrap the
4557 // pointer index type in an unsigned sense (mul nuw).
4558 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4559
4560 if (ElementScalable) {
4561 EVT VScaleTy = N.getValueType().getScalarType();
4562 SDValue VScale = DAG.getNode(
4563 ISD::VSCALE, dl, VScaleTy,
4564 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4565 if (N.getValueType().isVector())
4566 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4567 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4568 ScaleFlags);
4569 } else {
4570 // If this is a multiply by a power of two, turn it into a shl
4571 // immediately. This is a very common case.
4572 if (ElementMul != 1) {
4573 if (ElementMul.isPowerOf2()) {
4574 unsigned Amt = ElementMul.logBase2();
4575 IdxN = DAG.getNode(
4576 ISD::SHL, dl, N.getValueType(), IdxN,
4577 DAG.getShiftAmountConstant(Amt, N.getValueType(), dl),
4578 ScaleFlags);
4579 } else {
4580 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4581 IdxN.getValueType());
4582 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4583 ScaleFlags);
4584 }
4585 }
4586 }
4587
4588 // The successive addition of the current address, truncated to the
4589 // pointer index type and interpreted as an unsigned number, and each
4590 // offset, also interpreted as an unsigned number, does not wrap the
4591 // pointer index type (add nuw).
4592 SDNodeFlags AddFlags;
4593 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4594 AddFlags.setInBounds(NW.isInBounds());
4595
4596 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4597 }
4598 }
4599
4600 if (IsVectorGEP && !N.getValueType().isVector()) {
4601 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4602 N = DAG.getSplat(VT, dl, N);
4603 }
4604
4605 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4606 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4607 if (IsVectorGEP) {
4608 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4609 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4610 }
4611
4612 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4613 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4614
4615 setValue(&I, N);
4616}
4617
4618void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4619 // If this is a fixed sized alloca in the entry block of the function,
4620 // allocate it statically on the stack.
4621 if (FuncInfo.StaticAllocaMap.count(&I))
4622 return; // getValue will auto-populate this.
4623
4624 SDLoc dl = getCurSDLoc();
4625 Type *Ty = I.getAllocatedType();
4626 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4627 auto &DL = DAG.getDataLayout();
4628 TypeSize TySize = DL.getTypeAllocSize(Ty);
4629 MaybeAlign Alignment = I.getAlign();
4630
4631 SDValue AllocSize = getValue(I.getArraySize());
4632
4633 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4634 if (AllocSize.getValueType() != IntPtr)
4635 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4636
4637 AllocSize = DAG.getNode(
4638 ISD::MUL, dl, IntPtr, AllocSize,
4639 DAG.getZExtOrTrunc(DAG.getTypeSize(dl, MVT::i64, TySize), dl, IntPtr));
4640
4641 // Handle alignment. If the requested alignment is less than or equal to
4642 // the stack alignment, ignore it. If the size is greater than or equal to
4643 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4644 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4645 if (*Alignment <= StackAlign)
4646 Alignment = std::nullopt;
4647
4648 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4649 // Round the size of the allocation up to the stack alignment size
4650 // by add SA-1 to the size. This doesn't overflow because we're computing
4651 // an address inside an alloca.
4652 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4653 DAG.getConstant(StackAlignMask, dl, IntPtr),
4655
4656 // Mask out the low bits for alignment purposes.
4657 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4658 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4659
4660 SDValue Ops[] = {
4661 getRoot(), AllocSize,
4662 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4663 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4664 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4665 setValue(&I, DSA);
4666 DAG.setRoot(DSA.getValue(1));
4667
4668 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4669}
4670
4671static const MDNode *getRangeMetadata(const Instruction &I) {
4672 return I.getMetadata(LLVMContext::MD_range);
4673}
4674
4675static std::optional<ConstantRange> getRange(const Instruction &I) {
4676 if (const auto *CB = dyn_cast<CallBase>(&I))
4677 if (std::optional<ConstantRange> CR = CB->getRange())
4678 return CR;
4679 if (const MDNode *Range = getRangeMetadata(I))
4681 return std::nullopt;
4682}
4683
4685 if (const auto *CB = dyn_cast<CallBase>(&I))
4686 return CB->getRetNoFPClass();
4687 return fcNone;
4688}
4689
4690void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4691 if (I.isAtomic())
4692 return visitAtomicLoad(I);
4693
4694 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4695 const Value *SV = I.getOperand(0);
4696 if (TLI.supportSwiftError()) {
4697 // Swifterror values can come from either a function parameter with
4698 // swifterror attribute or an alloca with swifterror attribute.
4699 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4700 if (Arg->hasSwiftErrorAttr())
4701 return visitLoadFromSwiftError(I);
4702 }
4703
4704 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4705 if (Alloca->isSwiftError())
4706 return visitLoadFromSwiftError(I);
4707 }
4708 }
4709
4710 SDValue Ptr = getValue(SV);
4711
4712 Type *Ty = I.getType();
4713 SmallVector<EVT, 4> ValueVTs, MemVTs;
4715 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4716 unsigned NumValues = ValueVTs.size();
4717 if (NumValues == 0)
4718 return;
4719
4720 Align Alignment = I.getAlign();
4721 AAMDNodes AAInfo = I.getAAMetadata();
4722 const MDNode *Ranges = getRangeMetadata(I);
4723 bool isVolatile = I.isVolatile();
4724 MachineMemOperand::Flags MMOFlags =
4725 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4726
4727 SDValue Root;
4728 bool ConstantMemory = false;
4729 if (isVolatile)
4730 // Serialize volatile loads with other side effects.
4731 Root = getRoot();
4732 else if (NumValues > MaxParallelChains)
4733 Root = getMemoryRoot();
4734 else if (BatchAA &&
4735 BatchAA->pointsToConstantMemory(MemoryLocation(
4736 SV,
4737 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4738 AAInfo))) {
4739 // Do not serialize (non-volatile) loads of constant memory with anything.
4740 Root = DAG.getEntryNode();
4741 ConstantMemory = true;
4743 } else {
4744 // Do not serialize non-volatile loads against each other.
4745 Root = DAG.getRoot();
4746 }
4747
4748 SDLoc dl = getCurSDLoc();
4749
4750 if (isVolatile)
4751 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4752
4753 SmallVector<SDValue, 4> Values(NumValues);
4754 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4755
4756 unsigned ChainI = 0;
4757 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4758 // Serializing loads here may result in excessive register pressure, and
4759 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4760 // could recover a bit by hoisting nodes upward in the chain by recognizing
4761 // they are side-effect free or do not alias. The optimizer should really
4762 // avoid this case by converting large object/array copies to llvm.memcpy
4763 // (MaxParallelChains should always remain as failsafe).
4764 if (ChainI == MaxParallelChains) {
4765 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4766 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4767 ArrayRef(Chains.data(), ChainI));
4768 Root = Chain;
4769 ChainI = 0;
4770 }
4771
4772 // TODO: MachinePointerInfo only supports a fixed length offset.
4773 MachinePointerInfo PtrInfo =
4774 !Offsets[i].isScalable() || Offsets[i].isZero()
4775 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4776 : MachinePointerInfo();
4777
4778 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4779 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4780 MMOFlags, AAInfo, Ranges);
4781 Chains[ChainI] = L.getValue(1);
4782
4783 if (MemVTs[i] != ValueVTs[i])
4784 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4785
4786 if (MDNode *NoFPClassMD = I.getMetadata(LLVMContext::MD_nofpclass)) {
4787 uint64_t FPTestInt =
4788 cast<ConstantInt>(
4789 cast<ConstantAsMetadata>(NoFPClassMD->getOperand(0))->getValue())
4790 ->getZExtValue();
4791 if (FPTestInt != fcNone) {
4792 SDValue FPTestConst =
4793 DAG.getTargetConstant(FPTestInt, SDLoc(), MVT::i32);
4794 L = DAG.getNode(ISD::AssertNoFPClass, dl, L.getValueType(), L,
4795 FPTestConst);
4796 }
4797 }
4798 Values[i] = L;
4799 }
4800
4801 if (!ConstantMemory) {
4802 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4803 ArrayRef(Chains.data(), ChainI));
4804 if (isVolatile)
4805 DAG.setRoot(Chain);
4806 else
4807 PendingLoads.push_back(Chain);
4808 }
4809
4810 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4811 DAG.getVTList(ValueVTs), Values));
4812}
4813
4814void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4815 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4816 "call visitStoreToSwiftError when backend supports swifterror");
4817
4818 SmallVector<EVT, 4> ValueVTs;
4819 SmallVector<uint64_t, 4> Offsets;
4820 const Value *SrcV = I.getOperand(0);
4821 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4822 SrcV->getType(), ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4823 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4824 "expect a single EVT for swifterror");
4825
4826 SDValue Src = getValue(SrcV);
4827 // Create a virtual register, then update the virtual register.
4828 Register VReg =
4829 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4830 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4831 // Chain can be getRoot or getControlRoot.
4832 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4833 SDValue(Src.getNode(), Src.getResNo()));
4834 DAG.setRoot(CopyNode);
4835}
4836
4837void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4838 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4839 "call visitLoadFromSwiftError when backend supports swifterror");
4840
4841 assert(!I.isVolatile() &&
4842 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4843 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4844 "Support volatile, non temporal, invariant for load_from_swift_error");
4845
4846 const Value *SV = I.getOperand(0);
4847 Type *Ty = I.getType();
4848 assert(
4849 (!BatchAA ||
4850 !BatchAA->pointsToConstantMemory(MemoryLocation(
4851 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4852 I.getAAMetadata()))) &&
4853 "load_from_swift_error should not be constant memory");
4854
4855 SmallVector<EVT, 4> ValueVTs;
4856 SmallVector<uint64_t, 4> Offsets;
4857 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4858 ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4859 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4860 "expect a single EVT for swifterror");
4861
4862 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4863 SDValue L = DAG.getCopyFromReg(
4864 getRoot(), getCurSDLoc(),
4865 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4866
4867 setValue(&I, L);
4868}
4869
4870void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4871 if (I.isAtomic())
4872 return visitAtomicStore(I);
4873
4874 const Value *SrcV = I.getOperand(0);
4875 const Value *PtrV = I.getOperand(1);
4876
4877 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4878 if (TLI.supportSwiftError()) {
4879 // Swifterror values can come from either a function parameter with
4880 // swifterror attribute or an alloca with swifterror attribute.
4881 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4882 if (Arg->hasSwiftErrorAttr())
4883 return visitStoreToSwiftError(I);
4884 }
4885
4886 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4887 if (Alloca->isSwiftError())
4888 return visitStoreToSwiftError(I);
4889 }
4890 }
4891
4892 SmallVector<EVT, 4> ValueVTs, MemVTs;
4894 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4895 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4896 unsigned NumValues = ValueVTs.size();
4897 if (NumValues == 0)
4898 return;
4899
4900 // Get the lowered operands. Note that we do this after
4901 // checking if NumResults is zero, because with zero results
4902 // the operands won't have values in the map.
4903 SDValue Src = getValue(SrcV);
4904 SDValue Ptr = getValue(PtrV);
4905
4906 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4907 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4908 SDLoc dl = getCurSDLoc();
4909 Align Alignment = I.getAlign();
4910 AAMDNodes AAInfo = I.getAAMetadata();
4911
4912 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4913
4914 unsigned ChainI = 0;
4915 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4916 // See visitLoad comments.
4917 if (ChainI == MaxParallelChains) {
4918 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4919 ArrayRef(Chains.data(), ChainI));
4920 Root = Chain;
4921 ChainI = 0;
4922 }
4923
4924 // TODO: MachinePointerInfo only supports a fixed length offset.
4925 MachinePointerInfo PtrInfo =
4926 !Offsets[i].isScalable() || Offsets[i].isZero()
4927 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4928 : MachinePointerInfo();
4929
4930 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4931 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4932 if (MemVTs[i] != ValueVTs[i])
4933 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4934 SDValue St =
4935 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4936 Chains[ChainI] = St;
4937 }
4938
4939 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4940 ArrayRef(Chains.data(), ChainI));
4941 setValue(&I, StoreNode);
4942 DAG.setRoot(StoreNode);
4943}
4944
4945void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4946 bool IsCompressing) {
4947 SDLoc sdl = getCurSDLoc();
4948
4949 Value *Src0Operand = I.getArgOperand(0);
4950 Value *PtrOperand = I.getArgOperand(1);
4951 Value *MaskOperand = I.getArgOperand(2);
4952 Align Alignment = I.getParamAlign(1).valueOrOne();
4953
4954 SDValue Ptr = getValue(PtrOperand);
4955 SDValue Src0 = getValue(Src0Operand);
4956 SDValue Mask = getValue(MaskOperand);
4957 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4958
4959 EVT VT = Src0.getValueType();
4960
4961 auto MMOFlags = MachineMemOperand::MOStore;
4962 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4964
4965 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4966 MachinePointerInfo(PtrOperand), MMOFlags,
4967 LocationSize::upperBound(VT.getStoreSize()), Alignment,
4968 I.getAAMetadata());
4969
4970 const auto &TLI = DAG.getTargetLoweringInfo();
4971
4972 SDValue StoreNode =
4973 !IsCompressing && TTI->hasConditionalLoadStoreForType(
4974 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4975 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4976 Mask)
4977 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4978 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4979 IsCompressing);
4980 DAG.setRoot(StoreNode);
4981 setValue(&I, StoreNode);
4982}
4983
4984// Get a uniform base for the Gather/Scatter intrinsic.
4985// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4986// We try to represent it as a base pointer + vector of indices.
4987// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4988// The first operand of the GEP may be a single pointer or a vector of pointers
4989// Example:
4990// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4991// or
4992// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4993// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4994//
4995// When the first GEP operand is a single pointer - it is the uniform base we
4996// are looking for. If first operand of the GEP is a splat vector - we
4997// extract the splat value and use it as a uniform base.
4998// In all other cases the function returns 'false'.
4999static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
5000 SDValue &Scale, SelectionDAGBuilder *SDB,
5001 const BasicBlock *CurBB, uint64_t ElemSize) {
5002 SelectionDAG& DAG = SDB->DAG;
5003 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5004 const DataLayout &DL = DAG.getDataLayout();
5005
5006 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
5007
5008 // Handle splat constant pointer.
5009 if (auto *C = dyn_cast<Constant>(Ptr)) {
5010 C = C->getSplatValue();
5011 if (!C)
5012 return false;
5013
5014 Base = SDB->getValue(C);
5015
5016 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
5017 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
5018 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
5019 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
5020 return true;
5021 }
5022
5024 if (!GEP || GEP->getParent() != CurBB)
5025 return false;
5026
5027 if (GEP->getNumOperands() != 2)
5028 return false;
5029
5030 const Value *BasePtr = GEP->getPointerOperand();
5031 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
5032
5033 // Make sure the base is scalar and the index is a vector.
5034 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
5035 return false;
5036
5037 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
5038 if (ScaleVal.isScalable())
5039 return false;
5040
5041 // Target may not support the required addressing mode.
5042 if (ScaleVal != 1 &&
5043 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
5044 return false;
5045
5046 Base = SDB->getValue(BasePtr);
5047 Index = SDB->getValue(IndexVal);
5048
5049 Scale =
5050 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
5051 return true;
5052}
5053
5054void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
5055 SDLoc sdl = getCurSDLoc();
5056
5057 // llvm.masked.scatter.*(Src0, Ptrs, Mask)
5058 const Value *Ptr = I.getArgOperand(1);
5059 SDValue Src0 = getValue(I.getArgOperand(0));
5060 SDValue Mask = getValue(I.getArgOperand(2));
5061 EVT VT = Src0.getValueType();
5062 Align Alignment = I.getParamAlign(1).valueOrOne();
5063 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5064
5065 SDValue Base;
5066 SDValue Index;
5067 SDValue Scale;
5068 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5069 I.getParent(), VT.getScalarStoreSize());
5070
5071 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5072 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5073 MachinePointerInfo(AS), MachineMemOperand::MOStore,
5074 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
5075 if (!UniformBase) {
5076 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5077 Index = getValue(Ptr);
5078 Scale =
5079 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5080 }
5081
5082 EVT IdxVT = Index.getValueType();
5083 EVT EltTy = IdxVT.getVectorElementType();
5084 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5085 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5086 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5087 }
5088
5089 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
5090 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
5091 Ops, MMO, ISD::SIGNED_SCALED, false);
5092 DAG.setRoot(Scatter);
5093 setValue(&I, Scatter);
5094}
5095
5096void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5097 SDLoc sdl = getCurSDLoc();
5098
5099 Value *PtrOperand = I.getArgOperand(0);
5100 Value *MaskOperand = I.getArgOperand(1);
5101 Value *Src0Operand = I.getArgOperand(2);
5102 Align Alignment = I.getParamAlign(0).valueOrOne();
5103
5104 SDValue Ptr = getValue(PtrOperand);
5105 SDValue Src0 = getValue(Src0Operand);
5106 SDValue Mask = getValue(MaskOperand);
5107 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5108
5109 EVT VT = Src0.getValueType();
5110 AAMDNodes AAInfo = I.getAAMetadata();
5111 const MDNode *Ranges = getRangeMetadata(I);
5112
5113 // Do not serialize masked loads of constant memory with anything.
5114 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5115 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5116
5117 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5118
5119 auto MMOFlags = MachineMemOperand::MOLoad;
5120 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5122 if (I.hasMetadata(LLVMContext::MD_invariant_load))
5124
5125 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5126 MachinePointerInfo(PtrOperand), MMOFlags,
5127 LocationSize::upperBound(VT.getStoreSize()), Alignment, AAInfo, Ranges);
5128
5129 const auto &TLI = DAG.getTargetLoweringInfo();
5130
5131 // The Load/Res may point to different values and both of them are output
5132 // variables.
5133 SDValue Load;
5134 SDValue Res;
5135 if (!IsExpanding &&
5136 TTI->hasConditionalLoadStoreForType(Src0Operand->getType(),
5137 /*IsStore=*/false))
5138 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5139 else
5140 Res = Load =
5141 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5142 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5143 if (AddToChain)
5144 PendingLoads.push_back(Load.getValue(1));
5145 setValue(&I, Res);
5146}
5147
5148void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5149 SDLoc sdl = getCurSDLoc();
5150
5151 // @llvm.masked.gather.*(Ptrs, Mask, Src0)
5152 const Value *Ptr = I.getArgOperand(0);
5153 SDValue Src0 = getValue(I.getArgOperand(2));
5154 SDValue Mask = getValue(I.getArgOperand(1));
5155
5156 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5157 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5158 Align Alignment = I.getParamAlign(0).valueOrOne();
5159
5160 const MDNode *Ranges = getRangeMetadata(I);
5161
5162 SDValue Root = DAG.getRoot();
5163 SDValue Base;
5164 SDValue Index;
5165 SDValue Scale;
5166 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5167 I.getParent(), VT.getScalarStoreSize());
5168 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5169 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5170 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5171 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5172 Ranges);
5173
5174 if (!UniformBase) {
5175 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5176 Index = getValue(Ptr);
5177 Scale =
5178 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5179 }
5180
5181 EVT IdxVT = Index.getValueType();
5182 EVT EltTy = IdxVT.getVectorElementType();
5183 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5184 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5185 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5186 }
5187
5188 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5189 SDValue Gather =
5190 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5192
5193 PendingLoads.push_back(Gather.getValue(1));
5194 setValue(&I, Gather);
5195}
5196
5197void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5198 SDLoc dl = getCurSDLoc();
5199 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5200 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5201 SyncScope::ID SSID = I.getSyncScopeID();
5202
5203 SDValue InChain = getRoot();
5204
5205 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5206 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5207
5208 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5209 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5210
5211 MachineFunction &MF = DAG.getMachineFunction();
5212 MachineMemOperand *MMO = MF.getMachineMemOperand(
5213 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5214 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5215 FailureOrdering);
5216
5218 dl, MemVT, VTs, InChain,
5219 getValue(I.getPointerOperand()),
5220 getValue(I.getCompareOperand()),
5221 getValue(I.getNewValOperand()), MMO);
5222
5223 SDValue OutChain = L.getValue(2);
5224
5225 setValue(&I, L);
5226 DAG.setRoot(OutChain);
5227}
5228
5229void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5230 SDLoc dl = getCurSDLoc();
5232 switch (I.getOperation()) {
5233 default: llvm_unreachable("Unknown atomicrmw operation");
5251 break;
5254 break;
5257 break;
5260 break;
5263 break;
5266 break;
5269 break;
5272 break;
5273 }
5274 AtomicOrdering Ordering = I.getOrdering();
5275 SyncScope::ID SSID = I.getSyncScopeID();
5276
5277 SDValue InChain = getRoot();
5278
5279 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5280 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5281 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5282
5283 MachineFunction &MF = DAG.getMachineFunction();
5284 MachineMemOperand *MMO = MF.getMachineMemOperand(
5285 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5286 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
5287
5288 SDValue L =
5289 DAG.getAtomic(NT, dl, MemVT, InChain,
5290 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5291 MMO);
5292
5293 SDValue OutChain = L.getValue(1);
5294
5295 setValue(&I, L);
5296 DAG.setRoot(OutChain);
5297}
5298
5299void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5300 SDLoc dl = getCurSDLoc();
5301 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5302 SDValue Ops[3];
5303 Ops[0] = getRoot();
5304 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5305 TLI.getFenceOperandTy(DAG.getDataLayout()));
5306 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5307 TLI.getFenceOperandTy(DAG.getDataLayout()));
5308 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5309 setValue(&I, N);
5310 DAG.setRoot(N);
5311}
5312
5313void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5314 SDLoc dl = getCurSDLoc();
5315 AtomicOrdering Order = I.getOrdering();
5316 SyncScope::ID SSID = I.getSyncScopeID();
5317
5318 SDValue InChain = getRoot();
5319
5320 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5321 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5322 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5323
5324 if (!TLI.supportsUnalignedAtomics() &&
5325 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5326 report_fatal_error("Cannot generate unaligned atomic load");
5327
5328 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5329
5330 const MDNode *Ranges = getRangeMetadata(I);
5331 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5332 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5333 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5334
5335 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5336
5337 SDValue Ptr = getValue(I.getPointerOperand());
5338 SDValue L =
5339 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5340
5341 SDValue OutChain = L.getValue(1);
5342 if (MemVT != VT)
5343 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5344
5345 setValue(&I, L);
5346 DAG.setRoot(OutChain);
5347}
5348
5349void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5350 SDLoc dl = getCurSDLoc();
5351
5352 AtomicOrdering Ordering = I.getOrdering();
5353 SyncScope::ID SSID = I.getSyncScopeID();
5354
5355 SDValue InChain = getRoot();
5356
5357 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5358 EVT MemVT =
5359 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5360
5361 if (!TLI.supportsUnalignedAtomics() &&
5362 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5363 report_fatal_error("Cannot generate unaligned atomic store");
5364
5365 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5366
5367 MachineFunction &MF = DAG.getMachineFunction();
5368 MachineMemOperand *MMO = MF.getMachineMemOperand(
5369 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5370 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5371
5372 SDValue Val = getValue(I.getValueOperand());
5373 if (Val.getValueType() != MemVT)
5374 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5375 SDValue Ptr = getValue(I.getPointerOperand());
5376
5377 SDValue OutChain =
5378 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5379
5380 setValue(&I, OutChain);
5381 DAG.setRoot(OutChain);
5382}
5383
5384/// Check if this intrinsic call depends on the chain (1st return value)
5385/// and if it only *loads* memory.
5386/// Ignore the callsite's attributes. A specific call site may be marked with
5387/// readnone, but the lowering code will expect the chain based on the
5388/// definition.
5389std::pair<bool, bool>
5390SelectionDAGBuilder::getTargetIntrinsicCallProperties(const CallBase &I) {
5391 const Function *F = I.getCalledFunction();
5392 bool HasChain = !F->doesNotAccessMemory();
5393 bool OnlyLoad =
5394 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5395
5396 return {HasChain, OnlyLoad};
5397}
5398
5399SmallVector<SDValue, 8> SelectionDAGBuilder::getTargetIntrinsicOperands(
5400 const CallBase &I, bool HasChain, bool OnlyLoad,
5401 TargetLowering::IntrinsicInfo *TgtMemIntrinsicInfo) {
5402 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5403
5404 // Build the operand list.
5406 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5407 if (OnlyLoad) {
5408 // We don't need to serialize loads against other loads.
5409 Ops.push_back(DAG.getRoot());
5410 } else {
5411 Ops.push_back(getRoot());
5412 }
5413 }
5414
5415 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5416 if (!TgtMemIntrinsicInfo || TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_VOID ||
5417 TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_W_CHAIN)
5418 Ops.push_back(DAG.getTargetConstant(I.getIntrinsicID(), getCurSDLoc(),
5419 TLI.getPointerTy(DAG.getDataLayout())));
5420
5421 // Add all operands of the call to the operand list.
5422 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5423 const Value *Arg = I.getArgOperand(i);
5424 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5425 Ops.push_back(getValue(Arg));
5426 continue;
5427 }
5428
5429 // Use TargetConstant instead of a regular constant for immarg.
5430 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5431 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5432 assert(CI->getBitWidth() <= 64 &&
5433 "large intrinsic immediates not handled");
5434 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5435 } else {
5436 Ops.push_back(
5437 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5438 }
5439 }
5440
5441 if (std::optional<OperandBundleUse> Bundle =
5442 I.getOperandBundle(LLVMContext::OB_deactivation_symbol)) {
5443 auto *Sym = Bundle->Inputs[0].get();
5444 SDValue SDSym = getValue(Sym);
5445 SDSym = DAG.getDeactivationSymbol(cast<GlobalValue>(Sym));
5446 Ops.push_back(SDSym);
5447 }
5448
5449 if (std::optional<OperandBundleUse> Bundle =
5450 I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5451 Value *Token = Bundle->Inputs[0].get();
5452 SDValue ConvControlToken = getValue(Token);
5453 assert(Ops.back().getValueType() != MVT::Glue &&
5454 "Did not expect another glue node here.");
5455 ConvControlToken =
5456 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5457 Ops.push_back(ConvControlToken);
5458 }
5459
5460 return Ops;
5461}
5462
5463SDVTList SelectionDAGBuilder::getTargetIntrinsicVTList(const CallBase &I,
5464 bool HasChain) {
5465 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5466
5467 SmallVector<EVT, 4> ValueVTs;
5468 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5469
5470 if (HasChain)
5471 ValueVTs.push_back(MVT::Other);
5472
5473 return DAG.getVTList(ValueVTs);
5474}
5475
5476/// Get an INTRINSIC node for a target intrinsic which does not touch memory.
5477SDValue SelectionDAGBuilder::getTargetNonMemIntrinsicNode(
5478 const Type &IntrinsicVT, bool HasChain, ArrayRef<SDValue> Ops,
5479 const SDVTList &VTs) {
5480 if (!HasChain)
5481 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5482 if (!IntrinsicVT.isVoidTy())
5483 return DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5484 return DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5485}
5486
5487/// Set root, convert return type if necessary and check alignment.
5488SDValue SelectionDAGBuilder::handleTargetIntrinsicRet(const CallBase &I,
5489 bool HasChain,
5490 bool OnlyLoad,
5491 SDValue Result) {
5492 if (HasChain) {
5493 SDValue Chain = Result.getValue(Result.getNode()->getNumValues() - 1);
5494 if (OnlyLoad)
5495 PendingLoads.push_back(Chain);
5496 else
5497 DAG.setRoot(Chain);
5498 }
5499
5500 if (I.getType()->isVoidTy())
5501 return Result;
5502
5503 if (MaybeAlign Alignment = I.getRetAlign(); InsertAssertAlign && Alignment) {
5504 // Insert `assertalign` node if there's an alignment.
5505 Result = DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5506 } else if (!isa<VectorType>(I.getType())) {
5507 Result = lowerRangeToAssertZExt(DAG, I, Result);
5508 }
5509
5510 return Result;
5511}
5512
5513/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5514/// node.
5515void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5516 unsigned Intrinsic) {
5517 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
5518
5519 // Infos is set by getTgtMemIntrinsic.
5521 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5522 TLI.getTgtMemIntrinsic(Infos, I, DAG.getMachineFunction(), Intrinsic);
5523 // Use the first (primary) info determines the node opcode.
5524 TargetLowering::IntrinsicInfo *Info = !Infos.empty() ? &Infos[0] : nullptr;
5525
5527 getTargetIntrinsicOperands(I, HasChain, OnlyLoad, Info);
5528 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
5529
5530 // Propagate fast-math-flags from IR to node(s).
5531 SDNodeFlags Flags;
5532 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5533 Flags.copyFMF(*FPMO);
5534 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5535
5536 // Create the node.
5538
5539 // In some cases, custom collection of operands from CallInst I may be needed.
5541 if (!Infos.empty()) {
5542 // This is target intrinsic that touches memory
5543 // Create MachineMemOperands for each memory access described by the target.
5544 MachineFunction &MF = DAG.getMachineFunction();
5546 for (const auto &Info : Infos) {
5547 // TODO: We currently just fallback to address space 0 if
5548 // getTgtMemIntrinsic didn't yield anything useful.
5549 MachinePointerInfo MPI;
5550 if (Info.ptrVal)
5551 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5552 else if (Info.fallbackAddressSpace)
5553 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5554 EVT MemVT = Info.memVT;
5555 LocationSize Size = LocationSize::precise(Info.size);
5556 if (Size.hasValue() && !Size.getValue())
5558 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5559 MachineMemOperand *MMO = MF.getMachineMemOperand(
5560 MPI, Info.flags, Size, Alignment, I.getAAMetadata(),
5561 /*Ranges=*/nullptr, Info.ssid, Info.order, Info.failureOrder);
5562 MMOs.push_back(MMO);
5563 }
5564
5565 Result = DAG.getMemIntrinsicNode(Info->opc, getCurSDLoc(), VTs, Ops,
5566 Info->memVT, MMOs);
5567 } else {
5568 Result = getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
5569 }
5570
5571 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
5572
5573 setValue(&I, Result);
5574}
5575
5576/// GetSignificand - Get the significand and build it into a floating-point
5577/// number with exponent of 1:
5578///
5579/// Op = (Op & 0x007fffff) | 0x3f800000;
5580///
5581/// where Op is the hexadecimal representation of floating point value.
5583 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5584 DAG.getConstant(0x007fffff, dl, MVT::i32));
5585 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5586 DAG.getConstant(0x3f800000, dl, MVT::i32));
5587 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5588}
5589
5590/// GetExponent - Get the exponent:
5591///
5592/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5593///
5594/// where Op is the hexadecimal representation of floating point value.
5596 const TargetLowering &TLI, const SDLoc &dl) {
5597 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5598 DAG.getConstant(0x7f800000, dl, MVT::i32));
5599 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5600 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5601 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5602 DAG.getConstant(127, dl, MVT::i32));
5603 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5604}
5605
5606/// getF32Constant - Get 32-bit floating point constant.
5607static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt,
5608 const SDLoc &dl) {
5609 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5610 MVT::f32);
5611}
5612
5614 SelectionDAG &DAG) {
5615 // TODO: What fast-math-flags should be set on the floating-point nodes?
5616
5617 // IntegerPartOfX = ((int32_t)(t0);
5618 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5619
5620 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5621 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5622 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5623
5624 // IntegerPartOfX <<= 23;
5625 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5626 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5627
5628 SDValue TwoToFractionalPartOfX;
5629 if (LimitFloatPrecision <= 6) {
5630 // For floating-point precision of 6:
5631 //
5632 // TwoToFractionalPartOfX =
5633 // 0.997535578f +
5634 // (0.735607626f + 0.252464424f * x) * x;
5635 //
5636 // error 0.0144103317, which is 6 bits
5637 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5638 getF32Constant(DAG, 0x3e814304, dl));
5639 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5640 getF32Constant(DAG, 0x3f3c50c8, dl));
5641 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5642 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5643 getF32Constant(DAG, 0x3f7f5e7e, dl));
5644 } else if (LimitFloatPrecision <= 12) {
5645 // For floating-point precision of 12:
5646 //
5647 // TwoToFractionalPartOfX =
5648 // 0.999892986f +
5649 // (0.696457318f +
5650 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5651 //
5652 // error 0.000107046256, which is 13 to 14 bits
5653 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5654 getF32Constant(DAG, 0x3da235e3, dl));
5655 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5656 getF32Constant(DAG, 0x3e65b8f3, dl));
5657 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5658 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5659 getF32Constant(DAG, 0x3f324b07, dl));
5660 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5661 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5662 getF32Constant(DAG, 0x3f7ff8fd, dl));
5663 } else { // LimitFloatPrecision <= 18
5664 // For floating-point precision of 18:
5665 //
5666 // TwoToFractionalPartOfX =
5667 // 0.999999982f +
5668 // (0.693148872f +
5669 // (0.240227044f +
5670 // (0.554906021e-1f +
5671 // (0.961591928e-2f +
5672 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5673 // error 2.47208000*10^(-7), which is better than 18 bits
5674 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5675 getF32Constant(DAG, 0x3924b03e, dl));
5676 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5677 getF32Constant(DAG, 0x3ab24b87, dl));
5678 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5679 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5680 getF32Constant(DAG, 0x3c1d8c17, dl));
5681 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5682 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5683 getF32Constant(DAG, 0x3d634a1d, dl));
5684 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5685 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5686 getF32Constant(DAG, 0x3e75fe14, dl));
5687 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5688 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5689 getF32Constant(DAG, 0x3f317234, dl));
5690 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5691 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5692 getF32Constant(DAG, 0x3f800000, dl));
5693 }
5694
5695 // Add the exponent into the result in integer domain.
5696 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5697 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5698 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5699}
5700
5701/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5702/// limited-precision mode.
5704 const TargetLowering &TLI, SDNodeFlags Flags) {
5705 if (Op.getValueType() == MVT::f32 &&
5707
5708 // Put the exponent in the right bit position for later addition to the
5709 // final result:
5710 //
5711 // t0 = Op * log2(e)
5712
5713 // TODO: What fast-math-flags should be set here?
5714 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5715 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5716 return getLimitedPrecisionExp2(t0, dl, DAG);
5717 }
5718
5719 // No special expansion.
5720 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5721}
5722
5723/// expandLog - Lower a log intrinsic. Handles the special sequences for
5724/// limited-precision mode.
5726 const TargetLowering &TLI, SDNodeFlags Flags) {
5727 // TODO: What fast-math-flags should be set on the floating-point nodes?
5728
5729 if (Op.getValueType() == MVT::f32 &&
5731 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5732
5733 // Scale the exponent by log(2).
5734 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5735 SDValue LogOfExponent =
5736 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5737 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5738
5739 // Get the significand and build it into a floating-point number with
5740 // exponent of 1.
5741 SDValue X = GetSignificand(DAG, Op1, dl);
5742
5743 SDValue LogOfMantissa;
5744 if (LimitFloatPrecision <= 6) {
5745 // For floating-point precision of 6:
5746 //
5747 // LogofMantissa =
5748 // -1.1609546f +
5749 // (1.4034025f - 0.23903021f * x) * x;
5750 //
5751 // error 0.0034276066, which is better than 8 bits
5752 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5753 getF32Constant(DAG, 0xbe74c456, dl));
5754 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5755 getF32Constant(DAG, 0x3fb3a2b1, dl));
5756 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5757 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5758 getF32Constant(DAG, 0x3f949a29, dl));
5759 } else if (LimitFloatPrecision <= 12) {
5760 // For floating-point precision of 12:
5761 //
5762 // LogOfMantissa =
5763 // -1.7417939f +
5764 // (2.8212026f +
5765 // (-1.4699568f +
5766 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5767 //
5768 // error 0.000061011436, which is 14 bits
5769 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5770 getF32Constant(DAG, 0xbd67b6d6, dl));
5771 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5772 getF32Constant(DAG, 0x3ee4f4b8, dl));
5773 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5774 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5775 getF32Constant(DAG, 0x3fbc278b, dl));
5776 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5777 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5778 getF32Constant(DAG, 0x40348e95, dl));
5779 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5780 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5781 getF32Constant(DAG, 0x3fdef31a, dl));
5782 } else { // LimitFloatPrecision <= 18
5783 // For floating-point precision of 18:
5784 //
5785 // LogOfMantissa =
5786 // -2.1072184f +
5787 // (4.2372794f +
5788 // (-3.7029485f +
5789 // (2.2781945f +
5790 // (-0.87823314f +
5791 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5792 //
5793 // error 0.0000023660568, which is better than 18 bits
5794 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5795 getF32Constant(DAG, 0xbc91e5ac, dl));
5796 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5797 getF32Constant(DAG, 0x3e4350aa, dl));
5798 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5799 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5800 getF32Constant(DAG, 0x3f60d3e3, dl));
5801 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5802 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5803 getF32Constant(DAG, 0x4011cdf0, dl));
5804 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5805 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5806 getF32Constant(DAG, 0x406cfd1c, dl));
5807 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5808 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5809 getF32Constant(DAG, 0x408797cb, dl));
5810 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5811 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5812 getF32Constant(DAG, 0x4006dcab, dl));
5813 }
5814
5815 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5816 }
5817
5818 // No special expansion.
5819 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5820}
5821
5822/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5823/// limited-precision mode.
5825 const TargetLowering &TLI, SDNodeFlags Flags) {
5826 // TODO: What fast-math-flags should be set on the floating-point nodes?
5827
5828 if (Op.getValueType() == MVT::f32 &&
5830 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5831
5832 // Get the exponent.
5833 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5834
5835 // Get the significand and build it into a floating-point number with
5836 // exponent of 1.
5837 SDValue X = GetSignificand(DAG, Op1, dl);
5838
5839 // Different possible minimax approximations of significand in
5840 // floating-point for various degrees of accuracy over [1,2].
5841 SDValue Log2ofMantissa;
5842 if (LimitFloatPrecision <= 6) {
5843 // For floating-point precision of 6:
5844 //
5845 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5846 //
5847 // error 0.0049451742, which is more than 7 bits
5848 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5849 getF32Constant(DAG, 0xbeb08fe0, dl));
5850 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5851 getF32Constant(DAG, 0x40019463, dl));
5852 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5853 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5854 getF32Constant(DAG, 0x3fd6633d, dl));
5855 } else if (LimitFloatPrecision <= 12) {
5856 // For floating-point precision of 12:
5857 //
5858 // Log2ofMantissa =
5859 // -2.51285454f +
5860 // (4.07009056f +
5861 // (-2.12067489f +
5862 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5863 //
5864 // error 0.0000876136000, which is better than 13 bits
5865 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5866 getF32Constant(DAG, 0xbda7262e, dl));
5867 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5868 getF32Constant(DAG, 0x3f25280b, dl));
5869 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5870 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5871 getF32Constant(DAG, 0x4007b923, dl));
5872 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5873 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5874 getF32Constant(DAG, 0x40823e2f, dl));
5875 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5876 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5877 getF32Constant(DAG, 0x4020d29c, dl));
5878 } else { // LimitFloatPrecision <= 18
5879 // For floating-point precision of 18:
5880 //
5881 // Log2ofMantissa =
5882 // -3.0400495f +
5883 // (6.1129976f +
5884 // (-5.3420409f +
5885 // (3.2865683f +
5886 // (-1.2669343f +
5887 // (0.27515199f -
5888 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5889 //
5890 // error 0.0000018516, which is better than 18 bits
5891 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5892 getF32Constant(DAG, 0xbcd2769e, dl));
5893 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5894 getF32Constant(DAG, 0x3e8ce0b9, dl));
5895 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5896 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5897 getF32Constant(DAG, 0x3fa22ae7, dl));
5898 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5899 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5900 getF32Constant(DAG, 0x40525723, dl));
5901 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5902 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5903 getF32Constant(DAG, 0x40aaf200, dl));
5904 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5905 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5906 getF32Constant(DAG, 0x40c39dad, dl));
5907 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5908 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5909 getF32Constant(DAG, 0x4042902c, dl));
5910 }
5911
5912 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5913 }
5914
5915 // No special expansion.
5916 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5917}
5918
5919/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5920/// limited-precision mode.
5922 const TargetLowering &TLI, SDNodeFlags Flags) {
5923 // TODO: What fast-math-flags should be set on the floating-point nodes?
5924
5925 if (Op.getValueType() == MVT::f32 &&
5927 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5928
5929 // Scale the exponent by log10(2) [0.30102999f].
5930 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5931 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5932 getF32Constant(DAG, 0x3e9a209a, dl));
5933
5934 // Get the significand and build it into a floating-point number with
5935 // exponent of 1.
5936 SDValue X = GetSignificand(DAG, Op1, dl);
5937
5938 SDValue Log10ofMantissa;
5939 if (LimitFloatPrecision <= 6) {
5940 // For floating-point precision of 6:
5941 //
5942 // Log10ofMantissa =
5943 // -0.50419619f +
5944 // (0.60948995f - 0.10380950f * x) * x;
5945 //
5946 // error 0.0014886165, which is 6 bits
5947 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5948 getF32Constant(DAG, 0xbdd49a13, dl));
5949 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5950 getF32Constant(DAG, 0x3f1c0789, dl));
5951 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5952 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5953 getF32Constant(DAG, 0x3f011300, dl));
5954 } else if (LimitFloatPrecision <= 12) {
5955 // For floating-point precision of 12:
5956 //
5957 // Log10ofMantissa =
5958 // -0.64831180f +
5959 // (0.91751397f +
5960 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5961 //
5962 // error 0.00019228036, which is better than 12 bits
5963 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5964 getF32Constant(DAG, 0x3d431f31, dl));
5965 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5966 getF32Constant(DAG, 0x3ea21fb2, dl));
5967 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5968 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5969 getF32Constant(DAG, 0x3f6ae232, dl));
5970 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5971 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5972 getF32Constant(DAG, 0x3f25f7c3, dl));
5973 } else { // LimitFloatPrecision <= 18
5974 // For floating-point precision of 18:
5975 //
5976 // Log10ofMantissa =
5977 // -0.84299375f +
5978 // (1.5327582f +
5979 // (-1.0688956f +
5980 // (0.49102474f +
5981 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5982 //
5983 // error 0.0000037995730, which is better than 18 bits
5984 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5985 getF32Constant(DAG, 0x3c5d51ce, dl));
5986 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5987 getF32Constant(DAG, 0x3e00685a, dl));
5988 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5989 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5990 getF32Constant(DAG, 0x3efb6798, dl));
5991 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5992 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5993 getF32Constant(DAG, 0x3f88d192, dl));
5994 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5995 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5996 getF32Constant(DAG, 0x3fc4316c, dl));
5997 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5998 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5999 getF32Constant(DAG, 0x3f57ce70, dl));
6000 }
6001
6002 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
6003 }
6004
6005 // No special expansion.
6006 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
6007}
6008
6009/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
6010/// limited-precision mode.
6012 const TargetLowering &TLI, SDNodeFlags Flags) {
6013 if (Op.getValueType() == MVT::f32 &&
6015 return getLimitedPrecisionExp2(Op, dl, DAG);
6016
6017 // No special expansion.
6018 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
6019}
6020
6021/// visitPow - Lower a pow intrinsic. Handles the special sequences for
6022/// limited-precision mode with x == 10.0f.
6024 SelectionDAG &DAG, const TargetLowering &TLI,
6025 SDNodeFlags Flags) {
6026 bool IsExp10 = false;
6027 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
6030 APFloat Ten(10.0f);
6031 IsExp10 = LHSC->isExactlyValue(Ten);
6032 }
6033 }
6034
6035 // TODO: What fast-math-flags should be set on the FMUL node?
6036 if (IsExp10) {
6037 // Put the exponent in the right bit position for later addition to the
6038 // final result:
6039 //
6040 // #define LOG2OF10 3.3219281f
6041 // t0 = Op * LOG2OF10;
6042 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
6043 getF32Constant(DAG, 0x40549a78, dl));
6044 return getLimitedPrecisionExp2(t0, dl, DAG);
6045 }
6046
6047 // No special expansion.
6048 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
6049}
6050
6051/// ExpandPowI - Expand a llvm.powi intrinsic.
6053 SelectionDAG &DAG) {
6054 // If RHS is a constant, we can expand this out to a multiplication tree if
6055 // it's beneficial on the target, otherwise we end up lowering to a call to
6056 // __powidf2 (for example).
6058 unsigned Val = RHSC->getSExtValue();
6059
6060 // powi(x, 0) -> 1.0
6061 if (Val == 0)
6062 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
6063
6065 Val, DAG.shouldOptForSize())) {
6066 // Get the exponent as a positive value.
6067 if ((int)Val < 0)
6068 Val = -Val;
6069 // We use the simple binary decomposition method to generate the multiply
6070 // sequence. There are more optimal ways to do this (for example,
6071 // powi(x,15) generates one more multiply than it should), but this has
6072 // the benefit of being both really simple and much better than a libcall.
6073 SDValue Res; // Logically starts equal to 1.0
6074 SDValue CurSquare = LHS;
6075 // TODO: Intrinsics should have fast-math-flags that propagate to these
6076 // nodes.
6077 while (Val) {
6078 if (Val & 1) {
6079 if (Res.getNode())
6080 Res =
6081 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
6082 else
6083 Res = CurSquare; // 1.0*CurSquare.
6084 }
6085
6086 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
6087 CurSquare, CurSquare);
6088 Val >>= 1;
6089 }
6090
6091 // If the original was negative, invert the result, producing 1/(x*x*x).
6092 if (RHSC->getSExtValue() < 0)
6093 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
6094 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
6095 return Res;
6096 }
6097 }
6098
6099 // Otherwise, expand to a libcall.
6100 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
6101}
6102
6103static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
6104 SDValue LHS, SDValue RHS, SDValue Scale,
6105 SelectionDAG &DAG, const TargetLowering &TLI) {
6106 EVT VT = LHS.getValueType();
6107 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
6108 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
6109 LLVMContext &Ctx = *DAG.getContext();
6110
6111 // If the type is legal but the operation isn't, this node might survive all
6112 // the way to operation legalization. If we end up there and we do not have
6113 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
6114 // node.
6115
6116 // Coax the legalizer into expanding the node during type legalization instead
6117 // by bumping the size by one bit. This will force it to Promote, enabling the
6118 // early expansion and avoiding the need to expand later.
6119
6120 // We don't have to do this if Scale is 0; that can always be expanded, unless
6121 // it's a saturating signed operation. Those can experience true integer
6122 // division overflow, a case which we must avoid.
6123
6124 // FIXME: We wouldn't have to do this (or any of the early
6125 // expansion/promotion) if it was possible to expand a libcall of an
6126 // illegal type during operation legalization. But it's not, so things
6127 // get a bit hacky.
6128 unsigned ScaleInt = Scale->getAsZExtVal();
6129 if ((ScaleInt > 0 || (Saturating && Signed)) &&
6130 (TLI.isTypeLegal(VT) ||
6131 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6133 Opcode, VT, ScaleInt);
6134 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6135 EVT PromVT;
6136 if (VT.isScalarInteger())
6137 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6138 else if (VT.isVector()) {
6139 PromVT = VT.getVectorElementType();
6140 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6141 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6142 } else
6143 llvm_unreachable("Wrong VT for DIVFIX?");
6144 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6145 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6146 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6147 // For saturating operations, we need to shift up the LHS to get the
6148 // proper saturation width, and then shift down again afterwards.
6149 if (Saturating)
6150 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6151 DAG.getConstant(1, DL, ShiftTy));
6152 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6153 if (Saturating)
6154 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6155 DAG.getConstant(1, DL, ShiftTy));
6156 return DAG.getZExtOrTrunc(Res, DL, VT);
6157 }
6158 }
6159
6160 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6161}
6162
6163// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6164// bitcasted, or split argument. Returns a list of <Register, size in bits>
6165static void
6166getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6167 const SDValue &N) {
6168 switch (N.getOpcode()) {
6169 case ISD::CopyFromReg: {
6170 SDValue Op = N.getOperand(1);
6171 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6172 Op.getValueType().getSizeInBits());
6173 return;
6174 }
6175 case ISD::BITCAST:
6176 case ISD::AssertZext:
6177 case ISD::AssertSext:
6178 case ISD::TRUNCATE:
6179 getUnderlyingArgRegs(Regs, N.getOperand(0));
6180 return;
6181 case ISD::BUILD_PAIR:
6182 case ISD::BUILD_VECTOR:
6184 for (SDValue Op : N->op_values())
6185 getUnderlyingArgRegs(Regs, Op);
6186 return;
6187 default:
6188 return;
6189 }
6190}
6191
6192/// If the DbgValueInst is a dbg_value of a function argument, create the
6193/// corresponding DBG_VALUE machine instruction for it now. At the end of
6194/// instruction selection, they will be inserted to the entry BB.
6195/// We don't currently support this for variadic dbg_values, as they shouldn't
6196/// appear for function arguments or in the prologue.
6197bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6198 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6199 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6200 const Argument *Arg = dyn_cast<Argument>(V);
6201 if (!Arg)
6202 return false;
6203
6204 MachineFunction &MF = DAG.getMachineFunction();
6205 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6206
6207 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6208 // we've been asked to pursue.
6209 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6210 bool Indirect) {
6211 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6212 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6213 // pointing at the VReg, which will be patched up later.
6214 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6216 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6217 /* isKill */ false, /* isDead */ false,
6218 /* isUndef */ false, /* isEarlyClobber */ false,
6219 /* SubReg */ 0, /* isDebug */ true)});
6220
6221 auto *NewDIExpr = FragExpr;
6222 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6223 // the DIExpression.
6224 if (Indirect)
6225 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6227 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6228 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6229 } else {
6230 // Create a completely standard DBG_VALUE.
6231 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6232 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6233 }
6234 };
6235
6236 if (Kind == FuncArgumentDbgValueKind::Value) {
6237 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6238 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6239 // the entry block.
6240 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6241 if (!IsInEntryBlock)
6242 return false;
6243
6244 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6245 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6246 // variable that also is a param.
6247 //
6248 // Although, if we are at the top of the entry block already, we can still
6249 // emit using ArgDbgValue. This might catch some situations when the
6250 // dbg.value refers to an argument that isn't used in the entry block, so
6251 // any CopyToReg node would be optimized out and the only way to express
6252 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6253 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6254 // we should only emit as ArgDbgValue if the Variable is an argument to the
6255 // current function, and the dbg.value intrinsic is found in the entry
6256 // block.
6257 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6258 !DL->getInlinedAt();
6259 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6260 if (!IsInPrologue && !VariableIsFunctionInputArg)
6261 return false;
6262
6263 // Here we assume that a function argument on IR level only can be used to
6264 // describe one input parameter on source level. If we for example have
6265 // source code like this
6266 //
6267 // struct A { long x, y; };
6268 // void foo(struct A a, long b) {
6269 // ...
6270 // b = a.x;
6271 // ...
6272 // }
6273 //
6274 // and IR like this
6275 //
6276 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6277 // entry:
6278 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6279 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6280 // call void @llvm.dbg.value(metadata i32 %b, "b",
6281 // ...
6282 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6283 // ...
6284 //
6285 // then the last dbg.value is describing a parameter "b" using a value that
6286 // is an argument. But since we already has used %a1 to describe a parameter
6287 // we should not handle that last dbg.value here (that would result in an
6288 // incorrect hoisting of the DBG_VALUE to the function entry).
6289 // Notice that we allow one dbg.value per IR level argument, to accommodate
6290 // for the situation with fragments above.
6291 // If there is no node for the value being handled, we return true to skip
6292 // the normal generation of debug info, as it would kill existing debug
6293 // info for the parameter in case of duplicates.
6294 if (VariableIsFunctionInputArg) {
6295 unsigned ArgNo = Arg->getArgNo();
6296 if (ArgNo >= FuncInfo.DescribedArgs.size())
6297 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6298 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6299 return !NodeMap[V].getNode();
6300 FuncInfo.DescribedArgs.set(ArgNo);
6301 }
6302 }
6303
6304 bool IsIndirect = false;
6305 std::optional<MachineOperand> Op;
6306 // Some arguments' frame index is recorded during argument lowering.
6307 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6308 if (FI != std::numeric_limits<int>::max())
6310
6312 if (!Op && N.getNode()) {
6313 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6314 Register Reg;
6315 if (ArgRegsAndSizes.size() == 1)
6316 Reg = ArgRegsAndSizes.front().first;
6317
6318 if (Reg && Reg.isVirtual()) {
6319 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6320 Register PR = RegInfo.getLiveInPhysReg(Reg);
6321 if (PR)
6322 Reg = PR;
6323 }
6324 if (Reg) {
6326 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6327 }
6328 }
6329
6330 if (!Op && N.getNode()) {
6331 // Check if frame index is available.
6332 SDValue LCandidate = peekThroughBitcasts(N);
6333 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6334 if (FrameIndexSDNode *FINode =
6335 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6336 Op = MachineOperand::CreateFI(FINode->getIndex());
6337 }
6338
6339 if (!Op) {
6340 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6341 auto splitMultiRegDbgValue =
6342 [&](ArrayRef<std::pair<Register, TypeSize>> SplitRegs) -> bool {
6343 unsigned Offset = 0;
6344 for (const auto &[Reg, RegSizeInBits] : SplitRegs) {
6345 // FIXME: Scalable sizes are not supported in fragment expressions.
6346 if (RegSizeInBits.isScalable())
6347 return false;
6348
6349 // If the expression is already a fragment, the current register
6350 // offset+size might extend beyond the fragment. In this case, only
6351 // the register bits that are inside the fragment are relevant.
6352 int RegFragmentSizeInBits = RegSizeInBits.getFixedValue();
6353 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6354 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6355 // The register is entirely outside the expression fragment,
6356 // so is irrelevant for debug info.
6357 if (Offset >= ExprFragmentSizeInBits)
6358 break;
6359 // The register is partially outside the expression fragment, only
6360 // the low bits within the fragment are relevant for debug info.
6361 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6362 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6363 }
6364 }
6365
6366 auto FragmentExpr = DIExpression::createFragmentExpression(
6367 Expr, Offset, RegFragmentSizeInBits);
6368 Offset += RegSizeInBits.getFixedValue();
6369 // If a valid fragment expression cannot be created, the variable's
6370 // correct value cannot be determined and so it is set as poison.
6371 if (!FragmentExpr) {
6372 SDDbgValue *SDV = DAG.getConstantDbgValue(
6373 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6374 DAG.AddDbgValue(SDV, false);
6375 continue;
6376 }
6377 MachineInstr *NewMI = MakeVRegDbgValue(
6378 Reg, *FragmentExpr, Kind != FuncArgumentDbgValueKind::Value);
6379 FuncInfo.ArgDbgValues.push_back(NewMI);
6380 }
6381
6382 return true;
6383 };
6384
6385 // Check if ValueMap has reg number.
6387 VMI = FuncInfo.ValueMap.find(V);
6388 if (VMI != FuncInfo.ValueMap.end()) {
6389 const auto &TLI = DAG.getTargetLoweringInfo();
6390 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6391 V->getType(), std::nullopt);
6392 if (RFV.occupiesMultipleRegs())
6393 return splitMultiRegDbgValue(RFV.getRegsAndSizes());
6394
6395 Op = MachineOperand::CreateReg(VMI->second, false);
6396 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6397 } else if (ArgRegsAndSizes.size() > 1) {
6398 // This was split due to the calling convention, and no virtual register
6399 // mapping exists for the value.
6400 return splitMultiRegDbgValue(ArgRegsAndSizes);
6401 }
6402 }
6403
6404 if (!Op)
6405 return false;
6406
6407 assert(Variable->isValidLocationForIntrinsic(DL) &&
6408 "Expected inlined-at fields to agree");
6409 MachineInstr *NewMI = nullptr;
6410
6411 if (Op->isReg())
6412 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6413 else
6414 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6415 Variable, Expr);
6416
6417 // Otherwise, use ArgDbgValues.
6418 FuncInfo.ArgDbgValues.push_back(NewMI);
6419 return true;
6420}
6421
6422/// Return the appropriate SDDbgValue based on N.
6423SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6424 DILocalVariable *Variable,
6425 DIExpression *Expr,
6426 const DebugLoc &dl,
6427 unsigned DbgSDNodeOrder) {
6428 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6429 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6430 // stack slot locations.
6431 //
6432 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6433 // debug values here after optimization:
6434 //
6435 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6436 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6437 //
6438 // Both describe the direct values of their associated variables.
6439 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6440 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6441 }
6442 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6443 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6444}
6445
6446static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6447 switch (Intrinsic) {
6448 case Intrinsic::smul_fix:
6449 return ISD::SMULFIX;
6450 case Intrinsic::umul_fix:
6451 return ISD::UMULFIX;
6452 case Intrinsic::smul_fix_sat:
6453 return ISD::SMULFIXSAT;
6454 case Intrinsic::umul_fix_sat:
6455 return ISD::UMULFIXSAT;
6456 case Intrinsic::sdiv_fix:
6457 return ISD::SDIVFIX;
6458 case Intrinsic::udiv_fix:
6459 return ISD::UDIVFIX;
6460 case Intrinsic::sdiv_fix_sat:
6461 return ISD::SDIVFIXSAT;
6462 case Intrinsic::udiv_fix_sat:
6463 return ISD::UDIVFIXSAT;
6464 default:
6465 llvm_unreachable("Unhandled fixed point intrinsic");
6466 }
6467}
6468
6469/// Given a @llvm.call.preallocated.setup, return the corresponding
6470/// preallocated call.
6471static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6472 assert(cast<CallBase>(PreallocatedSetup)
6474 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6475 "expected call_preallocated_setup Value");
6476 for (const auto *U : PreallocatedSetup->users()) {
6477 auto *UseCall = cast<CallBase>(U);
6478 const Function *Fn = UseCall->getCalledFunction();
6479 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6480 return UseCall;
6481 }
6482 }
6483 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6484}
6485
6486/// If DI is a debug value with an EntryValue expression, lower it using the
6487/// corresponding physical register of the associated Argument value
6488/// (guaranteed to exist by the verifier).
6489bool SelectionDAGBuilder::visitEntryValueDbgValue(
6490 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6491 DIExpression *Expr, DebugLoc DbgLoc) {
6492 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6493 return false;
6494
6495 // These properties are guaranteed by the verifier.
6496 const Argument *Arg = cast<Argument>(Values[0]);
6497 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6498
6499 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6500 if (ArgIt == FuncInfo.ValueMap.end()) {
6501 LLVM_DEBUG(
6502 dbgs() << "Dropping dbg.value: expression is entry_value but "
6503 "couldn't find an associated register for the Argument\n");
6504 return true;
6505 }
6506 Register ArgVReg = ArgIt->getSecond();
6507
6508 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6509 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6510 SDDbgValue *SDV = DAG.getVRegDbgValue(
6511 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6512 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6513 return true;
6514 }
6515 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6516 "couldn't find a physical register\n");
6517 return true;
6518}
6519
6520/// Lower the call to the specified intrinsic function.
6521void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6522 unsigned Intrinsic) {
6523 SDLoc sdl = getCurSDLoc();
6524 switch (Intrinsic) {
6525 case Intrinsic::experimental_convergence_anchor:
6526 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6527 break;
6528 case Intrinsic::experimental_convergence_entry:
6529 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6530 break;
6531 case Intrinsic::experimental_convergence_loop: {
6532 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6533 auto *Token = Bundle->Inputs[0].get();
6534 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6535 getValue(Token)));
6536 break;
6537 }
6538 }
6539}
6540
6541void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6542 unsigned IntrinsicID) {
6543 // For now, we're only lowering an 'add' histogram.
6544 // We can add others later, e.g. saturating adds, min/max.
6545 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6546 "Tried to lower unsupported histogram type");
6547 SDLoc sdl = getCurSDLoc();
6548 Value *Ptr = I.getOperand(0);
6549 SDValue Inc = getValue(I.getOperand(1));
6550 SDValue Mask = getValue(I.getOperand(2));
6551
6552 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6553 DataLayout TargetDL = DAG.getDataLayout();
6554 EVT VT = Inc.getValueType();
6555 Align Alignment = DAG.getEVTAlign(VT);
6556
6557 const MDNode *Ranges = getRangeMetadata(I);
6558
6559 SDValue Root = DAG.getRoot();
6560 SDValue Base;
6561 SDValue Index;
6562 SDValue Scale;
6563 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6564 I.getParent(), VT.getScalarStoreSize());
6565
6566 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6567
6568 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6569 MachinePointerInfo(AS),
6571 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6572
6573 if (!UniformBase) {
6574 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6575 Index = getValue(Ptr);
6576 Scale =
6577 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6578 }
6579
6580 EVT IdxVT = Index.getValueType();
6581
6582 // Avoid using e.g. i32 as index type when the increment must be performed
6583 // on i64's.
6584 bool MustExtendIndex = VT.getScalarSizeInBits() > IdxVT.getScalarSizeInBits();
6585 EVT EltTy = MustExtendIndex ? VT : IdxVT.getVectorElementType();
6586 if (MustExtendIndex || TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6587 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
6588 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6589 }
6590
6591 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6592
6593 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6594 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6595 Ops, MMO, ISD::SIGNED_SCALED);
6596
6597 setValue(&I, Histogram);
6598 DAG.setRoot(Histogram);
6599}
6600
6601void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6602 unsigned Intrinsic) {
6603 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6604 "Tried lowering invalid vector extract last");
6605 SDLoc sdl = getCurSDLoc();
6606 const DataLayout &Layout = DAG.getDataLayout();
6607 SDValue Data = getValue(I.getOperand(0));
6608 SDValue Mask = getValue(I.getOperand(1));
6609
6610 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6611 EVT ResVT = TLI.getValueType(Layout, I.getType());
6612
6613 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6614 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6615 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6616
6617 Value *Default = I.getOperand(2);
6619 SDValue PassThru = getValue(Default);
6620 EVT BoolVT = Mask.getValueType().getScalarType();
6621 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6622 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6623 }
6624
6625 setValue(&I, Result);
6626}
6627
6628/// Lower the call to the specified intrinsic function.
6629void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6630 unsigned Intrinsic) {
6631 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6632 SDLoc sdl = getCurSDLoc();
6633 DebugLoc dl = getCurDebugLoc();
6634 SDValue Res;
6635
6636 SDNodeFlags Flags;
6637 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6638 Flags.copyFMF(*FPOp);
6639
6640 switch (Intrinsic) {
6641 default:
6642 // By default, turn this into a target intrinsic node.
6643 visitTargetIntrinsic(I, Intrinsic);
6644 return;
6645 case Intrinsic::vscale: {
6646 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6647 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6648 return;
6649 }
6650 case Intrinsic::vastart: visitVAStart(I); return;
6651 case Intrinsic::vaend: visitVAEnd(I); return;
6652 case Intrinsic::vacopy: visitVACopy(I); return;
6653 case Intrinsic::returnaddress:
6654 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6655 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6656 getValue(I.getArgOperand(0))));
6657 return;
6658 case Intrinsic::addressofreturnaddress:
6659 setValue(&I,
6660 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6661 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6662 return;
6663 case Intrinsic::sponentry:
6664 setValue(&I,
6665 DAG.getNode(ISD::SPONENTRY, sdl,
6666 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6667 return;
6668 case Intrinsic::frameaddress:
6669 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6670 TLI.getFrameIndexTy(DAG.getDataLayout()),
6671 getValue(I.getArgOperand(0))));
6672 return;
6673 case Intrinsic::read_volatile_register:
6674 case Intrinsic::read_register: {
6675 Value *Reg = I.getArgOperand(0);
6676 SDValue Chain = getRoot();
6678 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6679 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6680 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6681 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6682 setValue(&I, Res);
6683 DAG.setRoot(Res.getValue(1));
6684 return;
6685 }
6686 case Intrinsic::write_register: {
6687 Value *Reg = I.getArgOperand(0);
6688 Value *RegValue = I.getArgOperand(1);
6689 SDValue Chain = getRoot();
6691 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6692 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6693 RegName, getValue(RegValue)));
6694 return;
6695 }
6696 case Intrinsic::memcpy:
6697 case Intrinsic::memcpy_inline: {
6698 const auto &MCI = cast<MemCpyInst>(I);
6699 SDValue Dst = getValue(I.getArgOperand(0));
6700 SDValue Src = getValue(I.getArgOperand(1));
6701 SDValue Size = getValue(I.getArgOperand(2));
6702 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6703 "memcpy_inline needs constant size");
6704 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6705 Align DstAlign = MCI.getDestAlign().valueOrOne();
6706 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6707 Align Alignment = std::min(DstAlign, SrcAlign);
6708 bool isVol = MCI.isVolatile();
6709 // FIXME: Support passing different dest/src alignments to the memcpy DAG
6710 // node.
6711 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6712 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol,
6713 MCI.isForceInlined(), &I, std::nullopt,
6714 MachinePointerInfo(I.getArgOperand(0)),
6715 MachinePointerInfo(I.getArgOperand(1)),
6716 I.getAAMetadata(), BatchAA);
6717 updateDAGForMaybeTailCall(MC);
6718 return;
6719 }
6720 case Intrinsic::memset:
6721 case Intrinsic::memset_inline: {
6722 const auto &MSII = cast<MemSetInst>(I);
6723 SDValue Dst = getValue(I.getArgOperand(0));
6724 SDValue Value = getValue(I.getArgOperand(1));
6725 SDValue Size = getValue(I.getArgOperand(2));
6726 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6727 "memset_inline needs constant size");
6728 // @llvm.memset defines 0 and 1 to both mean no alignment.
6729 Align DstAlign = MSII.getDestAlign().valueOrOne();
6730 bool isVol = MSII.isVolatile();
6731 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6732 SDValue MC = DAG.getMemset(
6733 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6734 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6735 updateDAGForMaybeTailCall(MC);
6736 return;
6737 }
6738 case Intrinsic::memmove: {
6739 const auto &MMI = cast<MemMoveInst>(I);
6740 SDValue Op1 = getValue(I.getArgOperand(0));
6741 SDValue Op2 = getValue(I.getArgOperand(1));
6742 SDValue Op3 = getValue(I.getArgOperand(2));
6743 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6744 Align DstAlign = MMI.getDestAlign().valueOrOne();
6745 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6746 Align Alignment = std::min(DstAlign, SrcAlign);
6747 bool isVol = MMI.isVolatile();
6748 // FIXME: Support passing different dest/src alignments to the memmove DAG
6749 // node.
6750 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6751 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, &I,
6752 /* OverrideTailCall */ std::nullopt,
6753 MachinePointerInfo(I.getArgOperand(0)),
6754 MachinePointerInfo(I.getArgOperand(1)),
6755 I.getAAMetadata(), BatchAA);
6756 updateDAGForMaybeTailCall(MM);
6757 return;
6758 }
6759 case Intrinsic::memcpy_element_unordered_atomic: {
6760 auto &MI = cast<AnyMemCpyInst>(I);
6761 SDValue Dst = getValue(MI.getRawDest());
6762 SDValue Src = getValue(MI.getRawSource());
6763 SDValue Length = getValue(MI.getLength());
6764
6765 Type *LengthTy = MI.getLength()->getType();
6766 unsigned ElemSz = MI.getElementSizeInBytes();
6767 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6768 SDValue MC =
6769 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6770 isTC, MachinePointerInfo(MI.getRawDest()),
6771 MachinePointerInfo(MI.getRawSource()));
6772 updateDAGForMaybeTailCall(MC);
6773 return;
6774 }
6775 case Intrinsic::memmove_element_unordered_atomic: {
6776 auto &MI = cast<AnyMemMoveInst>(I);
6777 SDValue Dst = getValue(MI.getRawDest());
6778 SDValue Src = getValue(MI.getRawSource());
6779 SDValue Length = getValue(MI.getLength());
6780
6781 Type *LengthTy = MI.getLength()->getType();
6782 unsigned ElemSz = MI.getElementSizeInBytes();
6783 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6784 SDValue MC =
6785 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6786 isTC, MachinePointerInfo(MI.getRawDest()),
6787 MachinePointerInfo(MI.getRawSource()));
6788 updateDAGForMaybeTailCall(MC);
6789 return;
6790 }
6791 case Intrinsic::memset_element_unordered_atomic: {
6792 auto &MI = cast<AnyMemSetInst>(I);
6793 SDValue Dst = getValue(MI.getRawDest());
6794 SDValue Val = getValue(MI.getValue());
6795 SDValue Length = getValue(MI.getLength());
6796
6797 Type *LengthTy = MI.getLength()->getType();
6798 unsigned ElemSz = MI.getElementSizeInBytes();
6799 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6800 SDValue MC =
6801 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6802 isTC, MachinePointerInfo(MI.getRawDest()));
6803 updateDAGForMaybeTailCall(MC);
6804 return;
6805 }
6806 case Intrinsic::call_preallocated_setup: {
6807 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6808 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6809 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6810 getRoot(), SrcValue);
6811 setValue(&I, Res);
6812 DAG.setRoot(Res);
6813 return;
6814 }
6815 case Intrinsic::call_preallocated_arg: {
6816 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6817 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6818 SDValue Ops[3];
6819 Ops[0] = getRoot();
6820 Ops[1] = SrcValue;
6821 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6822 MVT::i32); // arg index
6823 SDValue Res = DAG.getNode(
6825 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6826 setValue(&I, Res);
6827 DAG.setRoot(Res.getValue(1));
6828 return;
6829 }
6830
6831 case Intrinsic::eh_typeid_for: {
6832 // Find the type id for the given typeinfo.
6833 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6834 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6835 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6836 setValue(&I, Res);
6837 return;
6838 }
6839
6840 case Intrinsic::eh_return_i32:
6841 case Intrinsic::eh_return_i64:
6842 DAG.getMachineFunction().setCallsEHReturn(true);
6843 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6844 MVT::Other,
6846 getValue(I.getArgOperand(0)),
6847 getValue(I.getArgOperand(1))));
6848 return;
6849 case Intrinsic::eh_unwind_init:
6850 DAG.getMachineFunction().setCallsUnwindInit(true);
6851 return;
6852 case Intrinsic::eh_dwarf_cfa:
6853 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6854 TLI.getPointerTy(DAG.getDataLayout()),
6855 getValue(I.getArgOperand(0))));
6856 return;
6857 case Intrinsic::eh_sjlj_callsite: {
6858 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6859 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6860
6861 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6862 return;
6863 }
6864 case Intrinsic::eh_sjlj_functioncontext: {
6865 // Get and store the index of the function context.
6866 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6867 AllocaInst *FnCtx =
6868 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6869 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6871 return;
6872 }
6873 case Intrinsic::eh_sjlj_setjmp: {
6874 SDValue Ops[2];
6875 Ops[0] = getRoot();
6876 Ops[1] = getValue(I.getArgOperand(0));
6877 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6878 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6879 setValue(&I, Op.getValue(0));
6880 DAG.setRoot(Op.getValue(1));
6881 return;
6882 }
6883 case Intrinsic::eh_sjlj_longjmp:
6884 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6885 getRoot(), getValue(I.getArgOperand(0))));
6886 return;
6887 case Intrinsic::eh_sjlj_setup_dispatch:
6888 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6889 getRoot()));
6890 return;
6891 case Intrinsic::masked_gather:
6892 visitMaskedGather(I);
6893 return;
6894 case Intrinsic::masked_load:
6895 visitMaskedLoad(I);
6896 return;
6897 case Intrinsic::masked_scatter:
6898 visitMaskedScatter(I);
6899 return;
6900 case Intrinsic::masked_store:
6901 visitMaskedStore(I);
6902 return;
6903 case Intrinsic::masked_expandload:
6904 visitMaskedLoad(I, true /* IsExpanding */);
6905 return;
6906 case Intrinsic::masked_compressstore:
6907 visitMaskedStore(I, true /* IsCompressing */);
6908 return;
6909 case Intrinsic::powi:
6910 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6911 getValue(I.getArgOperand(1)), DAG));
6912 return;
6913 case Intrinsic::log:
6914 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6915 return;
6916 case Intrinsic::log2:
6917 setValue(&I,
6918 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6919 return;
6920 case Intrinsic::log10:
6921 setValue(&I,
6922 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6923 return;
6924 case Intrinsic::exp:
6925 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6926 return;
6927 case Intrinsic::exp2:
6928 setValue(&I,
6929 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6930 return;
6931 case Intrinsic::pow:
6932 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6933 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6934 return;
6935 case Intrinsic::sqrt:
6936 case Intrinsic::fabs:
6937 case Intrinsic::sin:
6938 case Intrinsic::cos:
6939 case Intrinsic::tan:
6940 case Intrinsic::asin:
6941 case Intrinsic::acos:
6942 case Intrinsic::atan:
6943 case Intrinsic::sinh:
6944 case Intrinsic::cosh:
6945 case Intrinsic::tanh:
6946 case Intrinsic::exp10:
6947 case Intrinsic::floor:
6948 case Intrinsic::ceil:
6949 case Intrinsic::trunc:
6950 case Intrinsic::rint:
6951 case Intrinsic::nearbyint:
6952 case Intrinsic::round:
6953 case Intrinsic::roundeven:
6954 case Intrinsic::canonicalize: {
6955 unsigned Opcode;
6956 // clang-format off
6957 switch (Intrinsic) {
6958 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6959 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6960 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6961 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6962 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6963 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6964 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6965 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6966 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6967 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6968 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6969 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6970 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6971 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6972 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6973 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6974 case Intrinsic::rint: Opcode = ISD::FRINT; break;
6975 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6976 case Intrinsic::round: Opcode = ISD::FROUND; break;
6977 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6978 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6979 }
6980 // clang-format on
6981
6982 setValue(&I, DAG.getNode(Opcode, sdl,
6983 getValue(I.getArgOperand(0)).getValueType(),
6984 getValue(I.getArgOperand(0)), Flags));
6985 return;
6986 }
6987 case Intrinsic::atan2:
6988 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
6989 getValue(I.getArgOperand(0)).getValueType(),
6990 getValue(I.getArgOperand(0)),
6991 getValue(I.getArgOperand(1)), Flags));
6992 return;
6993 case Intrinsic::lround:
6994 case Intrinsic::llround:
6995 case Intrinsic::lrint:
6996 case Intrinsic::llrint: {
6997 unsigned Opcode;
6998 // clang-format off
6999 switch (Intrinsic) {
7000 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7001 case Intrinsic::lround: Opcode = ISD::LROUND; break;
7002 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
7003 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
7004 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
7005 }
7006 // clang-format on
7007
7008 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7009 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
7010 getValue(I.getArgOperand(0))));
7011 return;
7012 }
7013 case Intrinsic::minnum:
7014 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
7015 getValue(I.getArgOperand(0)).getValueType(),
7016 getValue(I.getArgOperand(0)),
7017 getValue(I.getArgOperand(1)), Flags));
7018 return;
7019 case Intrinsic::maxnum:
7020 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
7021 getValue(I.getArgOperand(0)).getValueType(),
7022 getValue(I.getArgOperand(0)),
7023 getValue(I.getArgOperand(1)), Flags));
7024 return;
7025 case Intrinsic::minimum:
7026 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
7027 getValue(I.getArgOperand(0)).getValueType(),
7028 getValue(I.getArgOperand(0)),
7029 getValue(I.getArgOperand(1)), Flags));
7030 return;
7031 case Intrinsic::maximum:
7032 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
7033 getValue(I.getArgOperand(0)).getValueType(),
7034 getValue(I.getArgOperand(0)),
7035 getValue(I.getArgOperand(1)), Flags));
7036 return;
7037 case Intrinsic::minimumnum:
7038 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
7039 getValue(I.getArgOperand(0)).getValueType(),
7040 getValue(I.getArgOperand(0)),
7041 getValue(I.getArgOperand(1)), Flags));
7042 return;
7043 case Intrinsic::maximumnum:
7044 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
7045 getValue(I.getArgOperand(0)).getValueType(),
7046 getValue(I.getArgOperand(0)),
7047 getValue(I.getArgOperand(1)), Flags));
7048 return;
7049 case Intrinsic::copysign:
7050 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
7051 getValue(I.getArgOperand(0)).getValueType(),
7052 getValue(I.getArgOperand(0)),
7053 getValue(I.getArgOperand(1)), Flags));
7054 return;
7055 case Intrinsic::ldexp:
7056 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
7057 getValue(I.getArgOperand(0)).getValueType(),
7058 getValue(I.getArgOperand(0)),
7059 getValue(I.getArgOperand(1)), Flags));
7060 return;
7061 case Intrinsic::modf:
7062 case Intrinsic::sincos:
7063 case Intrinsic::sincospi:
7064 case Intrinsic::frexp: {
7065 unsigned Opcode;
7066 switch (Intrinsic) {
7067 default:
7068 llvm_unreachable("unexpected intrinsic");
7069 case Intrinsic::sincos:
7070 Opcode = ISD::FSINCOS;
7071 break;
7072 case Intrinsic::sincospi:
7073 Opcode = ISD::FSINCOSPI;
7074 break;
7075 case Intrinsic::modf:
7076 Opcode = ISD::FMODF;
7077 break;
7078 case Intrinsic::frexp:
7079 Opcode = ISD::FFREXP;
7080 break;
7081 }
7082 SmallVector<EVT, 2> ValueVTs;
7083 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
7084 SDVTList VTs = DAG.getVTList(ValueVTs);
7085 setValue(
7086 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
7087 return;
7088 }
7089 case Intrinsic::arithmetic_fence: {
7090 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
7091 getValue(I.getArgOperand(0)).getValueType(),
7092 getValue(I.getArgOperand(0)), Flags));
7093 return;
7094 }
7095 case Intrinsic::fma:
7096 setValue(&I, DAG.getNode(
7097 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
7098 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
7099 getValue(I.getArgOperand(2)), Flags));
7100 return;
7101#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
7102 case Intrinsic::INTRINSIC:
7103#include "llvm/IR/ConstrainedOps.def"
7104 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
7105 return;
7106#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
7107#include "llvm/IR/VPIntrinsics.def"
7108 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
7109 return;
7110 case Intrinsic::fptrunc_round: {
7111 // Get the last argument, the metadata and convert it to an integer in the
7112 // call
7113 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7114 std::optional<RoundingMode> RoundMode =
7115 convertStrToRoundingMode(cast<MDString>(MD)->getString());
7116
7117 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7118
7119 // Propagate fast-math-flags from IR to node(s).
7120 SDNodeFlags Flags;
7121 Flags.copyFMF(*cast<FPMathOperator>(&I));
7122 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
7123
7125 Result = DAG.getNode(
7126 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
7127 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
7128 setValue(&I, Result);
7129
7130 return;
7131 }
7132 case Intrinsic::fmuladd: {
7133 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7134 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
7135 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
7136 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7137 getValue(I.getArgOperand(0)).getValueType(),
7138 getValue(I.getArgOperand(0)),
7139 getValue(I.getArgOperand(1)),
7140 getValue(I.getArgOperand(2)), Flags));
7141 } else if (TLI.isOperationLegalOrCustom(ISD::FMULADD, VT)) {
7142 // TODO: Support splitting the vector.
7143 setValue(&I, DAG.getNode(ISD::FMULADD, sdl,
7144 getValue(I.getArgOperand(0)).getValueType(),
7145 getValue(I.getArgOperand(0)),
7146 getValue(I.getArgOperand(1)),
7147 getValue(I.getArgOperand(2)), Flags));
7148 } else {
7149 // TODO: Intrinsic calls should have fast-math-flags.
7150 SDValue Mul = DAG.getNode(
7151 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7152 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7153 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7154 getValue(I.getArgOperand(0)).getValueType(),
7155 Mul, getValue(I.getArgOperand(2)), Flags);
7156 setValue(&I, Add);
7157 }
7158 return;
7159 }
7160 case Intrinsic::fptosi_sat: {
7161 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7162 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7163 getValue(I.getArgOperand(0)),
7164 DAG.getValueType(VT.getScalarType())));
7165 return;
7166 }
7167 case Intrinsic::fptoui_sat: {
7168 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7169 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7170 getValue(I.getArgOperand(0)),
7171 DAG.getValueType(VT.getScalarType())));
7172 return;
7173 }
7174 case Intrinsic::convert_from_arbitrary_fp: {
7175 // Extract format metadata and convert to semantics enum.
7176 EVT DstVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7177 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7178 StringRef FormatStr = cast<MDString>(MD)->getString();
7179 const fltSemantics *SrcSem =
7181 if (!SrcSem) {
7182 DAG.getContext()->emitError(
7183 "convert_from_arbitrary_fp: not implemented format '" + FormatStr +
7184 "'");
7185 setValue(&I, DAG.getPOISON(DstVT));
7186 return;
7187 }
7189
7190 SDValue IntVal = getValue(I.getArgOperand(0));
7191
7192 // Emit ISD::CONVERT_FROM_ARBITRARY_FP node.
7193 SDValue SemConst =
7194 DAG.getTargetConstant(static_cast<int>(SemEnum), sdl, MVT::i32);
7195 setValue(&I, DAG.getNode(ISD::CONVERT_FROM_ARBITRARY_FP, sdl, DstVT, IntVal,
7196 SemConst));
7197 return;
7198 }
7199 case Intrinsic::set_rounding:
7200 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7201 {getRoot(), getValue(I.getArgOperand(0))});
7202 setValue(&I, Res);
7203 DAG.setRoot(Res.getValue(0));
7204 return;
7205 case Intrinsic::is_fpclass: {
7206 const DataLayout DLayout = DAG.getDataLayout();
7207 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7208 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7209 FPClassTest Test = static_cast<FPClassTest>(
7210 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7211 MachineFunction &MF = DAG.getMachineFunction();
7212 const Function &F = MF.getFunction();
7213 SDValue Op = getValue(I.getArgOperand(0));
7214 SDNodeFlags Flags;
7215 Flags.setNoFPExcept(
7216 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7217 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7218 // expansion can use illegal types. Making expansion early allows
7219 // legalizing these types prior to selection.
7220 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7221 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7222 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7223 setValue(&I, Result);
7224 return;
7225 }
7226
7227 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7228 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7229 setValue(&I, V);
7230 return;
7231 }
7232 case Intrinsic::get_fpenv: {
7233 const DataLayout DLayout = DAG.getDataLayout();
7234 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7235 Align TempAlign = DAG.getEVTAlign(EnvVT);
7236 SDValue Chain = getRoot();
7237 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7238 // and temporary storage in stack.
7239 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7240 Res = DAG.getNode(
7241 ISD::GET_FPENV, sdl,
7242 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7243 MVT::Other),
7244 Chain);
7245 } else {
7246 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7247 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7248 auto MPI =
7249 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7250 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7252 TempAlign);
7253 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7254 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7255 }
7256 setValue(&I, Res);
7257 DAG.setRoot(Res.getValue(1));
7258 return;
7259 }
7260 case Intrinsic::set_fpenv: {
7261 const DataLayout DLayout = DAG.getDataLayout();
7262 SDValue Env = getValue(I.getArgOperand(0));
7263 EVT EnvVT = Env.getValueType();
7264 Align TempAlign = DAG.getEVTAlign(EnvVT);
7265 SDValue Chain = getRoot();
7266 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7267 // environment from memory.
7268 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7269 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7270 } else {
7271 // Allocate space in stack, copy environment bits into it and use this
7272 // memory in SET_FPENV_MEM.
7273 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7274 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7275 auto MPI =
7276 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7277 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7279 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7281 TempAlign);
7282 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7283 }
7284 DAG.setRoot(Chain);
7285 return;
7286 }
7287 case Intrinsic::reset_fpenv:
7288 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7289 return;
7290 case Intrinsic::get_fpmode:
7291 Res = DAG.getNode(
7292 ISD::GET_FPMODE, sdl,
7293 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7294 MVT::Other),
7295 DAG.getRoot());
7296 setValue(&I, Res);
7297 DAG.setRoot(Res.getValue(1));
7298 return;
7299 case Intrinsic::set_fpmode:
7300 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7301 getValue(I.getArgOperand(0)));
7302 DAG.setRoot(Res);
7303 return;
7304 case Intrinsic::reset_fpmode: {
7305 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7306 DAG.setRoot(Res);
7307 return;
7308 }
7309 case Intrinsic::pcmarker: {
7310 SDValue Tmp = getValue(I.getArgOperand(0));
7311 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7312 return;
7313 }
7314 case Intrinsic::readcyclecounter: {
7315 SDValue Op = getRoot();
7316 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7317 DAG.getVTList(MVT::i64, MVT::Other), Op);
7318 setValue(&I, Res);
7319 DAG.setRoot(Res.getValue(1));
7320 return;
7321 }
7322 case Intrinsic::readsteadycounter: {
7323 SDValue Op = getRoot();
7324 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7325 DAG.getVTList(MVT::i64, MVT::Other), Op);
7326 setValue(&I, Res);
7327 DAG.setRoot(Res.getValue(1));
7328 return;
7329 }
7330 case Intrinsic::bitreverse:
7331 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7332 getValue(I.getArgOperand(0)).getValueType(),
7333 getValue(I.getArgOperand(0))));
7334 return;
7335 case Intrinsic::bswap:
7336 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7337 getValue(I.getArgOperand(0)).getValueType(),
7338 getValue(I.getArgOperand(0))));
7339 return;
7340 case Intrinsic::cttz: {
7341 SDValue Arg = getValue(I.getArgOperand(0));
7342 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7343 EVT Ty = Arg.getValueType();
7344 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
7345 sdl, Ty, Arg));
7346 return;
7347 }
7348 case Intrinsic::ctlz: {
7349 SDValue Arg = getValue(I.getArgOperand(0));
7350 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7351 EVT Ty = Arg.getValueType();
7352 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
7353 sdl, Ty, Arg));
7354 return;
7355 }
7356 case Intrinsic::ctpop: {
7357 SDValue Arg = getValue(I.getArgOperand(0));
7358 EVT Ty = Arg.getValueType();
7359 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7360 return;
7361 }
7362 case Intrinsic::fshl:
7363 case Intrinsic::fshr: {
7364 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7365 SDValue X = getValue(I.getArgOperand(0));
7366 SDValue Y = getValue(I.getArgOperand(1));
7367 SDValue Z = getValue(I.getArgOperand(2));
7368 EVT VT = X.getValueType();
7369
7370 if (X == Y) {
7371 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7372 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7373 } else {
7374 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7375 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7376 }
7377 return;
7378 }
7379 case Intrinsic::clmul: {
7380 SDValue X = getValue(I.getArgOperand(0));
7381 SDValue Y = getValue(I.getArgOperand(1));
7382 setValue(&I, DAG.getNode(ISD::CLMUL, sdl, X.getValueType(), X, Y));
7383 return;
7384 }
7385 case Intrinsic::sadd_sat: {
7386 SDValue Op1 = getValue(I.getArgOperand(0));
7387 SDValue Op2 = getValue(I.getArgOperand(1));
7388 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7389 return;
7390 }
7391 case Intrinsic::uadd_sat: {
7392 SDValue Op1 = getValue(I.getArgOperand(0));
7393 SDValue Op2 = getValue(I.getArgOperand(1));
7394 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7395 return;
7396 }
7397 case Intrinsic::ssub_sat: {
7398 SDValue Op1 = getValue(I.getArgOperand(0));
7399 SDValue Op2 = getValue(I.getArgOperand(1));
7400 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7401 return;
7402 }
7403 case Intrinsic::usub_sat: {
7404 SDValue Op1 = getValue(I.getArgOperand(0));
7405 SDValue Op2 = getValue(I.getArgOperand(1));
7406 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7407 return;
7408 }
7409 case Intrinsic::sshl_sat:
7410 case Intrinsic::ushl_sat: {
7411 SDValue Op1 = getValue(I.getArgOperand(0));
7412 SDValue Op2 = getValue(I.getArgOperand(1));
7413
7414 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
7415 Op1.getValueType(), DAG.getDataLayout());
7416
7417 // Coerce the shift amount to the right type if we can. This exposes the
7418 // truncate or zext to optimization early.
7419 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
7420 assert(ShiftTy.getSizeInBits() >=
7422 "Unexpected shift type");
7423 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
7424 }
7425
7426 unsigned Opc =
7427 Intrinsic == Intrinsic::sshl_sat ? ISD::SSHLSAT : ISD::USHLSAT;
7428 setValue(&I, DAG.getNode(Opc, sdl, Op1.getValueType(), Op1, Op2));
7429 return;
7430 }
7431 case Intrinsic::smul_fix:
7432 case Intrinsic::umul_fix:
7433 case Intrinsic::smul_fix_sat:
7434 case Intrinsic::umul_fix_sat: {
7435 SDValue Op1 = getValue(I.getArgOperand(0));
7436 SDValue Op2 = getValue(I.getArgOperand(1));
7437 SDValue Op3 = getValue(I.getArgOperand(2));
7438 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7439 Op1.getValueType(), Op1, Op2, Op3));
7440 return;
7441 }
7442 case Intrinsic::sdiv_fix:
7443 case Intrinsic::udiv_fix:
7444 case Intrinsic::sdiv_fix_sat:
7445 case Intrinsic::udiv_fix_sat: {
7446 SDValue Op1 = getValue(I.getArgOperand(0));
7447 SDValue Op2 = getValue(I.getArgOperand(1));
7448 SDValue Op3 = getValue(I.getArgOperand(2));
7450 Op1, Op2, Op3, DAG, TLI));
7451 return;
7452 }
7453 case Intrinsic::smax: {
7454 SDValue Op1 = getValue(I.getArgOperand(0));
7455 SDValue Op2 = getValue(I.getArgOperand(1));
7456 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7457 return;
7458 }
7459 case Intrinsic::smin: {
7460 SDValue Op1 = getValue(I.getArgOperand(0));
7461 SDValue Op2 = getValue(I.getArgOperand(1));
7462 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7463 return;
7464 }
7465 case Intrinsic::umax: {
7466 SDValue Op1 = getValue(I.getArgOperand(0));
7467 SDValue Op2 = getValue(I.getArgOperand(1));
7468 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7469 return;
7470 }
7471 case Intrinsic::umin: {
7472 SDValue Op1 = getValue(I.getArgOperand(0));
7473 SDValue Op2 = getValue(I.getArgOperand(1));
7474 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7475 return;
7476 }
7477 case Intrinsic::abs: {
7478 // TODO: Preserve "int min is poison" arg in SDAG?
7479 SDValue Op1 = getValue(I.getArgOperand(0));
7480 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7481 return;
7482 }
7483 case Intrinsic::scmp: {
7484 SDValue Op1 = getValue(I.getArgOperand(0));
7485 SDValue Op2 = getValue(I.getArgOperand(1));
7486 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7487 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7488 break;
7489 }
7490 case Intrinsic::ucmp: {
7491 SDValue Op1 = getValue(I.getArgOperand(0));
7492 SDValue Op2 = getValue(I.getArgOperand(1));
7493 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7494 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7495 break;
7496 }
7497 case Intrinsic::stackaddress:
7498 case Intrinsic::stacksave: {
7499 unsigned SDOpcode = Intrinsic == Intrinsic::stackaddress ? ISD::STACKADDRESS
7501 SDValue Op = getRoot();
7502 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7503 Res = DAG.getNode(SDOpcode, sdl, DAG.getVTList(VT, MVT::Other), Op);
7504 setValue(&I, Res);
7505 DAG.setRoot(Res.getValue(1));
7506 return;
7507 }
7508 case Intrinsic::stackrestore:
7509 Res = getValue(I.getArgOperand(0));
7510 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7511 return;
7512 case Intrinsic::get_dynamic_area_offset: {
7513 SDValue Op = getRoot();
7514 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7515 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7516 Op);
7517 DAG.setRoot(Op);
7518 setValue(&I, Res);
7519 return;
7520 }
7521 case Intrinsic::stackguard: {
7522 MachineFunction &MF = DAG.getMachineFunction();
7523 const Module &M = *MF.getFunction().getParent();
7524 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7525 SDValue Chain = getRoot();
7526 if (TLI.useLoadStackGuardNode(M)) {
7527 Res = getLoadStackGuard(DAG, sdl, Chain);
7528 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7529 } else {
7530 const Value *Global = TLI.getSDagStackGuard(M, DAG.getLibcalls());
7531 if (!Global) {
7532 LLVMContext &Ctx = *DAG.getContext();
7533 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
7534 setValue(&I, DAG.getPOISON(PtrTy));
7535 return;
7536 }
7537
7538 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7539 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7540 MachinePointerInfo(Global, 0), Align,
7542 }
7543 if (TLI.useStackGuardXorFP())
7544 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7545 DAG.setRoot(Chain);
7546 setValue(&I, Res);
7547 return;
7548 }
7549 case Intrinsic::stackprotector: {
7550 // Emit code into the DAG to store the stack guard onto the stack.
7551 MachineFunction &MF = DAG.getMachineFunction();
7552 MachineFrameInfo &MFI = MF.getFrameInfo();
7553 const Module &M = *MF.getFunction().getParent();
7554 SDValue Src, Chain = getRoot();
7555
7556 if (TLI.useLoadStackGuardNode(M))
7557 Src = getLoadStackGuard(DAG, sdl, Chain);
7558 else
7559 Src = getValue(I.getArgOperand(0)); // The guard's value.
7560
7561 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7562
7563 int FI = FuncInfo.StaticAllocaMap[Slot];
7564 MFI.setStackProtectorIndex(FI);
7565 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7566
7567 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7568
7569 // Store the stack protector onto the stack.
7570 Res = DAG.getStore(
7571 Chain, sdl, Src, FIN,
7572 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7573 MaybeAlign(), MachineMemOperand::MOVolatile);
7574 setValue(&I, Res);
7575 DAG.setRoot(Res);
7576 return;
7577 }
7578 case Intrinsic::objectsize:
7579 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7580
7581 case Intrinsic::is_constant:
7582 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7583
7584 case Intrinsic::annotation:
7585 case Intrinsic::ptr_annotation:
7586 case Intrinsic::launder_invariant_group:
7587 case Intrinsic::strip_invariant_group:
7588 // Drop the intrinsic, but forward the value
7589 setValue(&I, getValue(I.getOperand(0)));
7590 return;
7591
7592 case Intrinsic::type_test:
7593 case Intrinsic::public_type_test:
7594 reportFatalUsageError("llvm.type.test intrinsic must be lowered by the "
7595 "LowerTypeTests pass before code generation");
7596 return;
7597
7598 case Intrinsic::assume:
7599 case Intrinsic::experimental_noalias_scope_decl:
7600 case Intrinsic::var_annotation:
7601 case Intrinsic::sideeffect:
7602 // Discard annotate attributes, noalias scope declarations, assumptions, and
7603 // artificial side-effects.
7604 return;
7605
7606 case Intrinsic::codeview_annotation: {
7607 // Emit a label associated with this metadata.
7608 MachineFunction &MF = DAG.getMachineFunction();
7609 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7610 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7611 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7612 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7613 DAG.setRoot(Res);
7614 return;
7615 }
7616
7617 case Intrinsic::init_trampoline: {
7618 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7619
7620 SDValue Ops[6];
7621 Ops[0] = getRoot();
7622 Ops[1] = getValue(I.getArgOperand(0));
7623 Ops[2] = getValue(I.getArgOperand(1));
7624 Ops[3] = getValue(I.getArgOperand(2));
7625 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7626 Ops[5] = DAG.getSrcValue(F);
7627
7628 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7629
7630 DAG.setRoot(Res);
7631 return;
7632 }
7633 case Intrinsic::adjust_trampoline:
7634 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7635 TLI.getPointerTy(DAG.getDataLayout()),
7636 getValue(I.getArgOperand(0))));
7637 return;
7638 case Intrinsic::gcroot: {
7639 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7640 "only valid in functions with gc specified, enforced by Verifier");
7641 assert(GFI && "implied by previous");
7642 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7643 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7644
7645 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7646 GFI->addStackRoot(FI->getIndex(), TypeMap);
7647 return;
7648 }
7649 case Intrinsic::gcread:
7650 case Intrinsic::gcwrite:
7651 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7652 case Intrinsic::get_rounding:
7653 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7654 setValue(&I, Res);
7655 DAG.setRoot(Res.getValue(1));
7656 return;
7657
7658 case Intrinsic::expect:
7659 case Intrinsic::expect_with_probability:
7660 // Just replace __builtin_expect(exp, c) and
7661 // __builtin_expect_with_probability(exp, c, p) with EXP.
7662 setValue(&I, getValue(I.getArgOperand(0)));
7663 return;
7664
7665 case Intrinsic::ubsantrap:
7666 case Intrinsic::debugtrap:
7667 case Intrinsic::trap: {
7668 StringRef TrapFuncName =
7669 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7670 if (TrapFuncName.empty()) {
7671 switch (Intrinsic) {
7672 case Intrinsic::trap:
7673 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7674 break;
7675 case Intrinsic::debugtrap:
7676 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7677 break;
7678 case Intrinsic::ubsantrap:
7679 DAG.setRoot(DAG.getNode(
7680 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7681 DAG.getTargetConstant(
7682 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7683 MVT::i32)));
7684 break;
7685 default: llvm_unreachable("unknown trap intrinsic");
7686 }
7687 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7688 I.hasFnAttr(Attribute::NoMerge));
7689 return;
7690 }
7692 if (Intrinsic == Intrinsic::ubsantrap) {
7693 Value *Arg = I.getArgOperand(0);
7694 Args.emplace_back(Arg, getValue(Arg));
7695 }
7696
7697 TargetLowering::CallLoweringInfo CLI(DAG);
7698 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7699 CallingConv::C, I.getType(),
7700 DAG.getExternalSymbol(TrapFuncName.data(),
7701 TLI.getPointerTy(DAG.getDataLayout())),
7702 std::move(Args));
7703 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7704 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7705 DAG.setRoot(Result.second);
7706 return;
7707 }
7708
7709 case Intrinsic::allow_runtime_check:
7710 case Intrinsic::allow_ubsan_check:
7711 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7712 return;
7713
7714 case Intrinsic::uadd_with_overflow:
7715 case Intrinsic::sadd_with_overflow:
7716 case Intrinsic::usub_with_overflow:
7717 case Intrinsic::ssub_with_overflow:
7718 case Intrinsic::umul_with_overflow:
7719 case Intrinsic::smul_with_overflow: {
7721 switch (Intrinsic) {
7722 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7723 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7724 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7725 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7726 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7727 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7728 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7729 }
7730 SDValue Op1 = getValue(I.getArgOperand(0));
7731 SDValue Op2 = getValue(I.getArgOperand(1));
7732
7733 EVT ResultVT = Op1.getValueType();
7734 EVT OverflowVT = ResultVT.changeElementType(*Context, MVT::i1);
7735
7736 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7737 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7738 return;
7739 }
7740 case Intrinsic::prefetch: {
7741 SDValue Ops[5];
7742 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7744 Ops[0] = DAG.getRoot();
7745 Ops[1] = getValue(I.getArgOperand(0));
7746 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7747 MVT::i32);
7748 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7749 MVT::i32);
7750 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7751 MVT::i32);
7752 SDValue Result = DAG.getMemIntrinsicNode(
7753 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7754 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7755 /* align */ std::nullopt, Flags);
7756
7757 // Chain the prefetch in parallel with any pending loads, to stay out of
7758 // the way of later optimizations.
7759 PendingLoads.push_back(Result);
7760 Result = getRoot();
7761 DAG.setRoot(Result);
7762 return;
7763 }
7764 case Intrinsic::lifetime_start:
7765 case Intrinsic::lifetime_end: {
7766 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7767 // Stack coloring is not enabled in O0, discard region information.
7768 if (TM.getOptLevel() == CodeGenOptLevel::None)
7769 return;
7770
7771 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7772 if (!LifetimeObject)
7773 return;
7774
7775 // First check that the Alloca is static, otherwise it won't have a
7776 // valid frame index.
7777 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7778 if (SI == FuncInfo.StaticAllocaMap.end())
7779 return;
7780
7781 const int FrameIndex = SI->second;
7782 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7783 DAG.setRoot(Res);
7784 return;
7785 }
7786 case Intrinsic::pseudoprobe: {
7787 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7788 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7789 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7790 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7791 DAG.setRoot(Res);
7792 return;
7793 }
7794 case Intrinsic::invariant_start:
7795 // Discard region information.
7796 setValue(&I,
7797 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7798 return;
7799 case Intrinsic::invariant_end:
7800 // Discard region information.
7801 return;
7802 case Intrinsic::clear_cache: {
7803 SDValue InputChain = DAG.getRoot();
7804 SDValue StartVal = getValue(I.getArgOperand(0));
7805 SDValue EndVal = getValue(I.getArgOperand(1));
7806 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7807 {InputChain, StartVal, EndVal});
7808 setValue(&I, Res);
7809 DAG.setRoot(Res);
7810 return;
7811 }
7812 case Intrinsic::donothing:
7813 case Intrinsic::seh_try_begin:
7814 case Intrinsic::seh_scope_begin:
7815 case Intrinsic::seh_try_end:
7816 case Intrinsic::seh_scope_end:
7817 // ignore
7818 return;
7819 case Intrinsic::experimental_stackmap:
7820 visitStackmap(I);
7821 return;
7822 case Intrinsic::experimental_patchpoint_void:
7823 case Intrinsic::experimental_patchpoint:
7824 visitPatchpoint(I);
7825 return;
7826 case Intrinsic::experimental_gc_statepoint:
7828 return;
7829 case Intrinsic::experimental_gc_result:
7830 visitGCResult(cast<GCResultInst>(I));
7831 return;
7832 case Intrinsic::experimental_gc_relocate:
7833 visitGCRelocate(cast<GCRelocateInst>(I));
7834 return;
7835 case Intrinsic::instrprof_cover:
7836 llvm_unreachable("instrprof failed to lower a cover");
7837 case Intrinsic::instrprof_increment:
7838 llvm_unreachable("instrprof failed to lower an increment");
7839 case Intrinsic::instrprof_timestamp:
7840 llvm_unreachable("instrprof failed to lower a timestamp");
7841 case Intrinsic::instrprof_value_profile:
7842 llvm_unreachable("instrprof failed to lower a value profiling call");
7843 case Intrinsic::instrprof_mcdc_parameters:
7844 llvm_unreachable("instrprof failed to lower mcdc parameters");
7845 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7846 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7847 case Intrinsic::localescape: {
7848 MachineFunction &MF = DAG.getMachineFunction();
7849 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7850
7851 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7852 // is the same on all targets.
7853 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7854 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7855 if (isa<ConstantPointerNull>(Arg))
7856 continue; // Skip null pointers. They represent a hole in index space.
7857 AllocaInst *Slot = cast<AllocaInst>(Arg);
7858 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7859 "can only escape static allocas");
7860 int FI = FuncInfo.StaticAllocaMap[Slot];
7861 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7863 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7864 TII->get(TargetOpcode::LOCAL_ESCAPE))
7865 .addSym(FrameAllocSym)
7866 .addFrameIndex(FI);
7867 }
7868
7869 return;
7870 }
7871
7872 case Intrinsic::localrecover: {
7873 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7874 MachineFunction &MF = DAG.getMachineFunction();
7875
7876 // Get the symbol that defines the frame offset.
7877 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7878 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7879 unsigned IdxVal =
7880 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7881 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7883
7884 Value *FP = I.getArgOperand(1);
7885 SDValue FPVal = getValue(FP);
7886 EVT PtrVT = FPVal.getValueType();
7887
7888 // Create a MCSymbol for the label to avoid any target lowering
7889 // that would make this PC relative.
7890 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7891 SDValue OffsetVal =
7892 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7893
7894 // Add the offset to the FP.
7895 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7896 setValue(&I, Add);
7897
7898 return;
7899 }
7900
7901 case Intrinsic::fake_use: {
7902 Value *V = I.getArgOperand(0);
7903 SDValue Ops[2];
7904 // For Values not declared or previously used in this basic block, the
7905 // NodeMap will not have an entry, and `getValue` will assert if V has no
7906 // valid register value.
7907 auto FakeUseValue = [&]() -> SDValue {
7908 SDValue &N = NodeMap[V];
7909 if (N.getNode())
7910 return N;
7911
7912 // If there's a virtual register allocated and initialized for this
7913 // value, use it.
7914 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7915 return copyFromReg;
7916 // FIXME: Do we want to preserve constants? It seems pointless.
7917 if (isa<Constant>(V))
7918 return getValue(V);
7919 return SDValue();
7920 }();
7921 if (!FakeUseValue || FakeUseValue.isUndef())
7922 return;
7923 Ops[0] = getRoot();
7924 Ops[1] = FakeUseValue;
7925 // Also, do not translate a fake use with an undef operand, or any other
7926 // empty SDValues.
7927 if (!Ops[1] || Ops[1].isUndef())
7928 return;
7929 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7930 return;
7931 }
7932
7933 case Intrinsic::reloc_none: {
7934 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7935 StringRef SymbolName = cast<MDString>(MD)->getString();
7936 SDValue Ops[2] = {
7937 getRoot(),
7938 DAG.getTargetExternalSymbol(
7939 SymbolName.data(), TLI.getProgramPointerTy(DAG.getDataLayout()))};
7940 DAG.setRoot(DAG.getNode(ISD::RELOC_NONE, sdl, MVT::Other, Ops));
7941 return;
7942 }
7943
7944 case Intrinsic::cond_loop: {
7945 SDValue InputChain = DAG.getRoot();
7946 SDValue P = getValue(I.getArgOperand(0));
7947 Res = DAG.getNode(ISD::COND_LOOP, sdl, DAG.getVTList(MVT::Other),
7948 {InputChain, P});
7949 setValue(&I, Res);
7950 DAG.setRoot(Res);
7951 return;
7952 }
7953
7954 case Intrinsic::eh_exceptionpointer:
7955 case Intrinsic::eh_exceptioncode: {
7956 // Get the exception pointer vreg, copy from it, and resize it to fit.
7957 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7958 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7959 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7960 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7961 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7962 if (Intrinsic == Intrinsic::eh_exceptioncode)
7963 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7964 setValue(&I, N);
7965 return;
7966 }
7967 case Intrinsic::xray_customevent: {
7968 // Here we want to make sure that the intrinsic behaves as if it has a
7969 // specific calling convention.
7970 const auto &Triple = DAG.getTarget().getTargetTriple();
7971 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7972 return;
7973
7975
7976 // We want to say that we always want the arguments in registers.
7977 SDValue LogEntryVal = getValue(I.getArgOperand(0));
7978 SDValue StrSizeVal = getValue(I.getArgOperand(1));
7979 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7980 SDValue Chain = getRoot();
7981 Ops.push_back(LogEntryVal);
7982 Ops.push_back(StrSizeVal);
7983 Ops.push_back(Chain);
7984
7985 // We need to enforce the calling convention for the callsite, so that
7986 // argument ordering is enforced correctly, and that register allocation can
7987 // see that some registers may be assumed clobbered and have to preserve
7988 // them across calls to the intrinsic.
7989 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7990 sdl, NodeTys, Ops);
7991 SDValue patchableNode = SDValue(MN, 0);
7992 DAG.setRoot(patchableNode);
7993 setValue(&I, patchableNode);
7994 return;
7995 }
7996 case Intrinsic::xray_typedevent: {
7997 // Here we want to make sure that the intrinsic behaves as if it has a
7998 // specific calling convention.
7999 const auto &Triple = DAG.getTarget().getTargetTriple();
8000 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
8001 return;
8002
8004
8005 // We want to say that we always want the arguments in registers.
8006 // It's unclear to me how manipulating the selection DAG here forces callers
8007 // to provide arguments in registers instead of on the stack.
8008 SDValue LogTypeId = getValue(I.getArgOperand(0));
8009 SDValue LogEntryVal = getValue(I.getArgOperand(1));
8010 SDValue StrSizeVal = getValue(I.getArgOperand(2));
8011 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
8012 SDValue Chain = getRoot();
8013 Ops.push_back(LogTypeId);
8014 Ops.push_back(LogEntryVal);
8015 Ops.push_back(StrSizeVal);
8016 Ops.push_back(Chain);
8017
8018 // We need to enforce the calling convention for the callsite, so that
8019 // argument ordering is enforced correctly, and that register allocation can
8020 // see that some registers may be assumed clobbered and have to preserve
8021 // them across calls to the intrinsic.
8022 MachineSDNode *MN = DAG.getMachineNode(
8023 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
8024 SDValue patchableNode = SDValue(MN, 0);
8025 DAG.setRoot(patchableNode);
8026 setValue(&I, patchableNode);
8027 return;
8028 }
8029 case Intrinsic::experimental_deoptimize:
8031 return;
8032 case Intrinsic::stepvector:
8033 visitStepVector(I);
8034 return;
8035 case Intrinsic::vector_reduce_fadd:
8036 case Intrinsic::vector_reduce_fmul:
8037 case Intrinsic::vector_reduce_add:
8038 case Intrinsic::vector_reduce_mul:
8039 case Intrinsic::vector_reduce_and:
8040 case Intrinsic::vector_reduce_or:
8041 case Intrinsic::vector_reduce_xor:
8042 case Intrinsic::vector_reduce_smax:
8043 case Intrinsic::vector_reduce_smin:
8044 case Intrinsic::vector_reduce_umax:
8045 case Intrinsic::vector_reduce_umin:
8046 case Intrinsic::vector_reduce_fmax:
8047 case Intrinsic::vector_reduce_fmin:
8048 case Intrinsic::vector_reduce_fmaximum:
8049 case Intrinsic::vector_reduce_fminimum:
8050 visitVectorReduce(I, Intrinsic);
8051 return;
8052
8053 case Intrinsic::icall_branch_funnel: {
8055 Ops.push_back(getValue(I.getArgOperand(0)));
8056
8057 int64_t Offset;
8059 I.getArgOperand(1), Offset, DAG.getDataLayout()));
8060 if (!Base)
8062 "llvm.icall.branch.funnel operand must be a GlobalValue");
8063 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
8064
8065 struct BranchFunnelTarget {
8066 int64_t Offset;
8068 };
8070
8071 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
8073 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
8074 if (ElemBase != Base)
8075 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
8076 "to the same GlobalValue");
8077
8078 SDValue Val = getValue(I.getArgOperand(Op + 1));
8079 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
8080 if (!GA)
8082 "llvm.icall.branch.funnel operand must be a GlobalValue");
8083 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
8084 GA->getGlobal(), sdl, Val.getValueType(),
8085 GA->getOffset())});
8086 }
8087 llvm::sort(Targets,
8088 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
8089 return T1.Offset < T2.Offset;
8090 });
8091
8092 for (auto &T : Targets) {
8093 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
8094 Ops.push_back(T.Target);
8095 }
8096
8097 Ops.push_back(DAG.getRoot()); // Chain
8098 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
8099 MVT::Other, Ops),
8100 0);
8101 DAG.setRoot(N);
8102 setValue(&I, N);
8103 HasTailCall = true;
8104 return;
8105 }
8106
8107 case Intrinsic::wasm_landingpad_index:
8108 // Information this intrinsic contained has been transferred to
8109 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
8110 // delete it now.
8111 return;
8112
8113 case Intrinsic::aarch64_settag:
8114 case Intrinsic::aarch64_settag_zero: {
8115 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8116 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
8118 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
8119 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
8120 ZeroMemory);
8121 DAG.setRoot(Val);
8122 setValue(&I, Val);
8123 return;
8124 }
8125 case Intrinsic::amdgcn_cs_chain: {
8126 // At this point we don't care if it's amdgpu_cs_chain or
8127 // amdgpu_cs_chain_preserve.
8129
8130 Type *RetTy = I.getType();
8131 assert(RetTy->isVoidTy() && "Should not return");
8132
8133 SDValue Callee = getValue(I.getOperand(0));
8134
8135 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
8136 // We'll also tack the value of the EXEC mask at the end.
8138 Args.reserve(3);
8139
8140 for (unsigned Idx : {2, 3, 1}) {
8141 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8142 I.getOperand(Idx)->getType());
8143 Arg.setAttributes(&I, Idx);
8144 Args.push_back(Arg);
8145 }
8146
8147 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
8148 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
8149 Args[2].IsInReg = true; // EXEC should be inreg
8150
8151 // Forward the flags and any additional arguments.
8152 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
8153 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8154 I.getOperand(Idx)->getType());
8155 Arg.setAttributes(&I, Idx);
8156 Args.push_back(Arg);
8157 }
8158
8159 TargetLowering::CallLoweringInfo CLI(DAG);
8160 CLI.setDebugLoc(getCurSDLoc())
8161 .setChain(getRoot())
8162 .setCallee(CC, RetTy, Callee, std::move(Args))
8163 .setNoReturn(true)
8164 .setTailCall(true)
8165 .setConvergent(I.isConvergent());
8166 CLI.CB = &I;
8167 std::pair<SDValue, SDValue> Result =
8168 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
8169 (void)Result;
8170 assert(!Result.first.getNode() && !Result.second.getNode() &&
8171 "Should've lowered as tail call");
8172
8173 HasTailCall = true;
8174 return;
8175 }
8176 case Intrinsic::amdgcn_call_whole_wave: {
8178 bool isTailCall = I.isTailCall();
8179
8180 // The first argument is the callee. Skip it when assembling the call args.
8181 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
8182 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
8183 I.getArgOperand(Idx)->getType());
8184 Arg.setAttributes(&I, Idx);
8185
8186 // If we have an explicit sret argument that is an Instruction, (i.e., it
8187 // might point to function-local memory), we can't meaningfully tail-call.
8188 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
8189 isTailCall = false;
8190
8191 Args.push_back(Arg);
8192 }
8193
8194 SDValue ConvControlToken;
8195 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8196 auto *Token = Bundle->Inputs[0].get();
8197 ConvControlToken = getValue(Token);
8198 }
8199
8200 TargetLowering::CallLoweringInfo CLI(DAG);
8201 CLI.setDebugLoc(getCurSDLoc())
8202 .setChain(getRoot())
8203 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8204 getValue(I.getArgOperand(0)), std::move(Args))
8205 .setTailCall(isTailCall && canTailCall(I))
8206 .setIsPreallocated(
8207 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8208 .setConvergent(I.isConvergent())
8209 .setConvergenceControlToken(ConvControlToken);
8210 CLI.CB = &I;
8211
8212 std::pair<SDValue, SDValue> Result =
8213 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8214
8215 if (Result.first.getNode())
8216 setValue(&I, Result.first);
8217 return;
8218 }
8219 case Intrinsic::ptrmask: {
8220 SDValue Ptr = getValue(I.getOperand(0));
8221 SDValue Mask = getValue(I.getOperand(1));
8222
8223 // On arm64_32, pointers are 32 bits when stored in memory, but
8224 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8225 // match the index type, but the pointer is 64 bits, so the mask must be
8226 // zero-extended up to 64 bits to match the pointer.
8227 EVT PtrVT =
8228 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8229 EVT MemVT =
8230 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8231 assert(PtrVT == Ptr.getValueType());
8232 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8233 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8234 // 128-bit, so we have to pad the mask with ones for unused bits.
8235 auto HighOnes = DAG.getNode(
8236 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8237 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8238 PtrVT, sdl));
8239 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8240 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8241 } else if (Mask.getValueType() != PtrVT)
8242 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8243
8244 assert(Mask.getValueType() == PtrVT);
8245 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8246 return;
8247 }
8248 case Intrinsic::threadlocal_address: {
8249 setValue(&I, getValue(I.getOperand(0)));
8250 return;
8251 }
8252 case Intrinsic::get_active_lane_mask: {
8253 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8254 SDValue Index = getValue(I.getOperand(0));
8255 SDValue TripCount = getValue(I.getOperand(1));
8256 EVT ElementVT = Index.getValueType();
8257
8258 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8259 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8260 TripCount));
8261 return;
8262 }
8263
8264 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8265 CCVT.getVectorElementCount());
8266
8267 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8268 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8269 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8270 SDValue VectorInduction = DAG.getNode(
8271 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8272 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8273 VectorTripCount, ISD::CondCode::SETULT);
8274 setValue(&I, SetCC);
8275 return;
8276 }
8277 case Intrinsic::experimental_get_vector_length: {
8278 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8279 "Expected positive VF");
8280 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8281 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8282
8283 SDValue Count = getValue(I.getOperand(0));
8284 EVT CountVT = Count.getValueType();
8285
8286 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8287 visitTargetIntrinsic(I, Intrinsic);
8288 return;
8289 }
8290
8291 // Expand to a umin between the trip count and the maximum elements the type
8292 // can hold.
8293 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8294
8295 // Extend the trip count to at least the result VT.
8296 if (CountVT.bitsLT(VT)) {
8297 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8298 CountVT = VT;
8299 }
8300
8301 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8302 ElementCount::get(VF, IsScalable));
8303
8304 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8305 // Clip to the result type if needed.
8306 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8307
8308 setValue(&I, Trunc);
8309 return;
8310 }
8311 case Intrinsic::vector_partial_reduce_add: {
8312 SDValue Acc = getValue(I.getOperand(0));
8313 SDValue Input = getValue(I.getOperand(1));
8314 setValue(&I,
8315 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8316 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8317 return;
8318 }
8319 case Intrinsic::vector_partial_reduce_fadd: {
8320 SDValue Acc = getValue(I.getOperand(0));
8321 SDValue Input = getValue(I.getOperand(1));
8322 setValue(&I, DAG.getNode(
8323 ISD::PARTIAL_REDUCE_FMLA, sdl, Acc.getValueType(), Acc,
8324 Input, DAG.getConstantFP(1.0, sdl, Input.getValueType())));
8325 return;
8326 }
8327 case Intrinsic::experimental_cttz_elts: {
8328 SDValue Op = getValue(I.getOperand(0));
8329 EVT OpVT = Op.getValueType();
8330 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8331 bool ZeroIsPoison =
8332 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8333 if (OpVT.getVectorElementType() != MVT::i1) {
8334 // Compare the input vector elements to zero & use to count trailing
8335 // zeros.
8336 SDValue AllZero = DAG.getConstant(0, sdl, OpVT);
8337 EVT I1OpVT = OpVT.changeVectorElementType(*DAG.getContext(), MVT::i1);
8338 Op = DAG.getSetCC(sdl, I1OpVT, Op, AllZero, ISD::SETNE);
8339 }
8340 setValue(&I, DAG.getNode(ZeroIsPoison ? ISD::CTTZ_ELTS_ZERO_POISON
8342 sdl, RetTy, Op));
8343 return;
8344 }
8345 case Intrinsic::vector_insert: {
8346 SDValue Vec = getValue(I.getOperand(0));
8347 SDValue SubVec = getValue(I.getOperand(1));
8348 SDValue Index = getValue(I.getOperand(2));
8349
8350 // The intrinsic's index type is i64, but the SDNode requires an index type
8351 // suitable for the target. Convert the index as required.
8352 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8353 if (Index.getValueType() != VectorIdxTy)
8354 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8355
8356 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8357 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8358 Index));
8359 return;
8360 }
8361 case Intrinsic::vector_extract: {
8362 SDValue Vec = getValue(I.getOperand(0));
8363 SDValue Index = getValue(I.getOperand(1));
8364 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8365
8366 // The intrinsic's index type is i64, but the SDNode requires an index type
8367 // suitable for the target. Convert the index as required.
8368 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8369 if (Index.getValueType() != VectorIdxTy)
8370 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8371
8372 setValue(&I,
8373 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8374 return;
8375 }
8376 case Intrinsic::experimental_vector_match: {
8377 SDValue Op1 = getValue(I.getOperand(0));
8378 SDValue Op2 = getValue(I.getOperand(1));
8379 SDValue Mask = getValue(I.getOperand(2));
8380 EVT Op1VT = Op1.getValueType();
8381 EVT Op2VT = Op2.getValueType();
8382 EVT ResVT = Mask.getValueType();
8383 unsigned SearchSize = Op2VT.getVectorNumElements();
8384
8385 // If the target has native support for this vector match operation, lower
8386 // the intrinsic untouched; otherwise, expand it below.
8387 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8388 visitTargetIntrinsic(I, Intrinsic);
8389 return;
8390 }
8391
8392 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8393
8394 for (unsigned i = 0; i < SearchSize; ++i) {
8395 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8396 Op2VT.getVectorElementType(), Op2,
8397 DAG.getVectorIdxConstant(i, sdl));
8398 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8399 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8400 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8401 }
8402
8403 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8404 return;
8405 }
8406 case Intrinsic::vector_reverse:
8407 visitVectorReverse(I);
8408 return;
8409 case Intrinsic::vector_splice_left:
8410 case Intrinsic::vector_splice_right:
8411 visitVectorSplice(I);
8412 return;
8413 case Intrinsic::callbr_landingpad:
8414 visitCallBrLandingPad(I);
8415 return;
8416 case Intrinsic::vector_interleave2:
8417 visitVectorInterleave(I, 2);
8418 return;
8419 case Intrinsic::vector_interleave3:
8420 visitVectorInterleave(I, 3);
8421 return;
8422 case Intrinsic::vector_interleave4:
8423 visitVectorInterleave(I, 4);
8424 return;
8425 case Intrinsic::vector_interleave5:
8426 visitVectorInterleave(I, 5);
8427 return;
8428 case Intrinsic::vector_interleave6:
8429 visitVectorInterleave(I, 6);
8430 return;
8431 case Intrinsic::vector_interleave7:
8432 visitVectorInterleave(I, 7);
8433 return;
8434 case Intrinsic::vector_interleave8:
8435 visitVectorInterleave(I, 8);
8436 return;
8437 case Intrinsic::vector_deinterleave2:
8438 visitVectorDeinterleave(I, 2);
8439 return;
8440 case Intrinsic::vector_deinterleave3:
8441 visitVectorDeinterleave(I, 3);
8442 return;
8443 case Intrinsic::vector_deinterleave4:
8444 visitVectorDeinterleave(I, 4);
8445 return;
8446 case Intrinsic::vector_deinterleave5:
8447 visitVectorDeinterleave(I, 5);
8448 return;
8449 case Intrinsic::vector_deinterleave6:
8450 visitVectorDeinterleave(I, 6);
8451 return;
8452 case Intrinsic::vector_deinterleave7:
8453 visitVectorDeinterleave(I, 7);
8454 return;
8455 case Intrinsic::vector_deinterleave8:
8456 visitVectorDeinterleave(I, 8);
8457 return;
8458 case Intrinsic::experimental_vector_compress:
8459 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8460 getValue(I.getArgOperand(0)).getValueType(),
8461 getValue(I.getArgOperand(0)),
8462 getValue(I.getArgOperand(1)),
8463 getValue(I.getArgOperand(2)), Flags));
8464 return;
8465 case Intrinsic::experimental_convergence_anchor:
8466 case Intrinsic::experimental_convergence_entry:
8467 case Intrinsic::experimental_convergence_loop:
8468 visitConvergenceControl(I, Intrinsic);
8469 return;
8470 case Intrinsic::experimental_vector_histogram_add: {
8471 visitVectorHistogram(I, Intrinsic);
8472 return;
8473 }
8474 case Intrinsic::experimental_vector_extract_last_active: {
8475 visitVectorExtractLastActive(I, Intrinsic);
8476 return;
8477 }
8478 case Intrinsic::loop_dependence_war_mask:
8479 setValue(&I,
8481 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8482 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8483 DAG.getConstant(0, sdl, MVT::i64)));
8484 return;
8485 case Intrinsic::loop_dependence_raw_mask:
8486 setValue(&I,
8488 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8489 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8490 DAG.getConstant(0, sdl, MVT::i64)));
8491 return;
8492 case Intrinsic::masked_udiv:
8493 setValue(&I,
8494 DAG.getNode(ISD::MASKED_UDIV, sdl, EVT::getEVT(I.getType()),
8495 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8496 getValue(I.getOperand(2))));
8497 return;
8498 case Intrinsic::masked_sdiv:
8499 setValue(&I,
8500 DAG.getNode(ISD::MASKED_SDIV, sdl, EVT::getEVT(I.getType()),
8501 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8502 getValue(I.getOperand(2))));
8503 return;
8504 case Intrinsic::masked_urem:
8505 setValue(&I,
8506 DAG.getNode(ISD::MASKED_UREM, sdl, EVT::getEVT(I.getType()),
8507 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8508 getValue(I.getOperand(2))));
8509 return;
8510 case Intrinsic::masked_srem:
8511 setValue(&I,
8512 DAG.getNode(ISD::MASKED_SREM, sdl, EVT::getEVT(I.getType()),
8513 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8514 getValue(I.getOperand(2))));
8515 return;
8516 }
8517}
8518
8519void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
8521 assert(Result.getNode()->getNumValues() == 2);
8522 SDValue OutChain = Result.getValue(1);
8523 assert(OutChain.getValueType() == MVT::Other);
8524
8525 // Instead of updating the root immediately, push the produced chain to the
8526 // appropriate list, deferring the update until the root is requested. In this
8527 // case, the nodes from the lists are chained using TokenFactor, indicating
8528 // that the operations are independent.
8529 //
8530 // In particular, the root is updated before any call that might access the
8531 // floating-point environment, except for constrained intrinsics.
8532 switch (EB) {
8535 PendingConstrainedFP.push_back(OutChain);
8536 break;
8538 PendingConstrainedFPStrict.push_back(OutChain);
8539 break;
8540 }
8541}
8542
8543void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8544 const ConstrainedFPIntrinsic &FPI) {
8545 SDLoc sdl = getCurSDLoc();
8546
8547 // We do not need to serialize constrained FP intrinsics against
8548 // each other or against (nonvolatile) loads, so they can be
8549 // chained like loads.
8551 SDValue Chain = getFPOperationRoot(EB);
8553 Opers.push_back(Chain);
8554 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8555 Opers.push_back(getValue(FPI.getArgOperand(I)));
8556
8557 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8558 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8559 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8560
8561 SDNodeFlags Flags;
8563 Flags.setNoFPExcept(true);
8564
8565 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8566 Flags.copyFMF(*FPOp);
8567
8568 unsigned Opcode;
8569 switch (FPI.getIntrinsicID()) {
8570 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8571#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8572 case Intrinsic::INTRINSIC: \
8573 Opcode = ISD::STRICT_##DAGN; \
8574 break;
8575#include "llvm/IR/ConstrainedOps.def"
8576 case Intrinsic::experimental_constrained_fmuladd: {
8577 Opcode = ISD::STRICT_FMA;
8578 // Break fmuladd into fmul and fadd.
8579 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8580 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8581 Opers.pop_back();
8582 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8583 pushFPOpOutChain(Mul, EB);
8584 Opcode = ISD::STRICT_FADD;
8585 Opers.clear();
8586 Opers.push_back(Mul.getValue(1));
8587 Opers.push_back(Mul.getValue(0));
8588 Opers.push_back(getValue(FPI.getArgOperand(2)));
8589 }
8590 break;
8591 }
8592 }
8593
8594 // A few strict DAG nodes carry additional operands that are not
8595 // set up by the default code above.
8596 switch (Opcode) {
8597 default: break;
8599 Opers.push_back(
8600 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8601 break;
8602 case ISD::STRICT_FSETCC:
8603 case ISD::STRICT_FSETCCS: {
8604 auto *FPCmp = dyn_cast<ConstrainedFPCmpIntrinsic>(&FPI);
8605 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8606 if (DAG.isKnownNeverNaN(Opers[1]) && DAG.isKnownNeverNaN(Opers[2]))
8607 Condition = getFCmpCodeWithoutNaN(Condition);
8608 Opers.push_back(DAG.getCondCode(Condition));
8609 break;
8610 }
8611 }
8612
8613 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8614 pushFPOpOutChain(Result, EB);
8615
8616 SDValue FPResult = Result.getValue(0);
8617 setValue(&FPI, FPResult);
8618}
8619
8620static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8621 std::optional<unsigned> ResOPC;
8622 switch (VPIntrin.getIntrinsicID()) {
8623 case Intrinsic::vp_ctlz: {
8624 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8625 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8626 break;
8627 }
8628 case Intrinsic::vp_cttz: {
8629 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8630 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8631 break;
8632 }
8633 case Intrinsic::vp_cttz_elts: {
8634 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8635 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8636 break;
8637 }
8638#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8639 case Intrinsic::VPID: \
8640 ResOPC = ISD::VPSD; \
8641 break;
8642#include "llvm/IR/VPIntrinsics.def"
8643 }
8644
8645 if (!ResOPC)
8647 "Inconsistency: no SDNode available for this VPIntrinsic!");
8648
8649 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8650 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8651 if (VPIntrin.getFastMathFlags().allowReassoc())
8652 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8653 : ISD::VP_REDUCE_FMUL;
8654 }
8655
8656 return *ResOPC;
8657}
8658
8659void SelectionDAGBuilder::visitVPLoad(
8660 const VPIntrinsic &VPIntrin, EVT VT,
8661 const SmallVectorImpl<SDValue> &OpValues) {
8662 SDLoc DL = getCurSDLoc();
8663 Value *PtrOperand = VPIntrin.getArgOperand(0);
8664 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8665 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8666 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8667 SDValue LD;
8668 // Do not serialize variable-length loads of constant memory with
8669 // anything.
8670 if (!Alignment)
8671 Alignment = DAG.getEVTAlign(VT);
8672 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8673 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8674 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8675 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8676 MachineMemOperand::Flags MMOFlags =
8677 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8678 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8679 MachinePointerInfo(PtrOperand), MMOFlags,
8680 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8681 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8682 MMO, false /*IsExpanding */);
8683 if (AddToChain)
8684 PendingLoads.push_back(LD.getValue(1));
8685 setValue(&VPIntrin, LD);
8686}
8687
8688void SelectionDAGBuilder::visitVPLoadFF(
8689 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8690 const SmallVectorImpl<SDValue> &OpValues) {
8691 assert(OpValues.size() == 3 && "Unexpected number of operands");
8692 SDLoc DL = getCurSDLoc();
8693 Value *PtrOperand = VPIntrin.getArgOperand(0);
8694 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8695 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8696 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8697 SDValue LD;
8698 // Do not serialize variable-length loads of constant memory with
8699 // anything.
8700 if (!Alignment)
8701 Alignment = DAG.getEVTAlign(VT);
8702 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8703 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8704 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8705 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8706 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8707 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8708 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8709 MMO);
8710 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8711 if (AddToChain)
8712 PendingLoads.push_back(LD.getValue(2));
8713 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8714}
8715
8716void SelectionDAGBuilder::visitVPGather(
8717 const VPIntrinsic &VPIntrin, EVT VT,
8718 const SmallVectorImpl<SDValue> &OpValues) {
8719 SDLoc DL = getCurSDLoc();
8720 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8721 Value *PtrOperand = VPIntrin.getArgOperand(0);
8722 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8723 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8724 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8725 SDValue LD;
8726 if (!Alignment)
8727 Alignment = DAG.getEVTAlign(VT.getScalarType());
8728 unsigned AS =
8729 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8730 MachineMemOperand::Flags MMOFlags =
8731 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8732 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8733 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8734 *Alignment, AAInfo, Ranges);
8735 SDValue Base, Index, Scale;
8736 bool UniformBase =
8737 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8738 VT.getScalarStoreSize());
8739 if (!UniformBase) {
8740 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8741 Index = getValue(PtrOperand);
8742 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8743 }
8744 EVT IdxVT = Index.getValueType();
8745 EVT EltTy = IdxVT.getVectorElementType();
8746 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8747 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8748 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8749 }
8750 LD = DAG.getGatherVP(
8751 DAG.getVTList(VT, MVT::Other), VT, DL,
8752 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8754 PendingLoads.push_back(LD.getValue(1));
8755 setValue(&VPIntrin, LD);
8756}
8757
8758void SelectionDAGBuilder::visitVPStore(
8759 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8760 SDLoc DL = getCurSDLoc();
8761 Value *PtrOperand = VPIntrin.getArgOperand(1);
8762 EVT VT = OpValues[0].getValueType();
8763 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8764 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8765 SDValue ST;
8766 if (!Alignment)
8767 Alignment = DAG.getEVTAlign(VT);
8768 SDValue Ptr = OpValues[1];
8769 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8770 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8771 MachineMemOperand::Flags MMOFlags =
8772 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8773 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8774 MachinePointerInfo(PtrOperand), MMOFlags,
8775 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8776 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8777 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8778 /* IsTruncating */ false, /*IsCompressing*/ false);
8779 DAG.setRoot(ST);
8780 setValue(&VPIntrin, ST);
8781}
8782
8783void SelectionDAGBuilder::visitVPScatter(
8784 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8785 SDLoc DL = getCurSDLoc();
8786 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8787 Value *PtrOperand = VPIntrin.getArgOperand(1);
8788 EVT VT = OpValues[0].getValueType();
8789 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8790 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8791 SDValue ST;
8792 if (!Alignment)
8793 Alignment = DAG.getEVTAlign(VT.getScalarType());
8794 unsigned AS =
8795 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8796 MachineMemOperand::Flags MMOFlags =
8797 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8798 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8799 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8800 *Alignment, AAInfo);
8801 SDValue Base, Index, Scale;
8802 bool UniformBase =
8803 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8804 VT.getScalarStoreSize());
8805 if (!UniformBase) {
8806 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8807 Index = getValue(PtrOperand);
8808 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8809 }
8810 EVT IdxVT = Index.getValueType();
8811 EVT EltTy = IdxVT.getVectorElementType();
8812 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8813 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8814 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8815 }
8816 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8817 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8818 OpValues[2], OpValues[3]},
8819 MMO, ISD::SIGNED_SCALED);
8820 DAG.setRoot(ST);
8821 setValue(&VPIntrin, ST);
8822}
8823
8824void SelectionDAGBuilder::visitVPStridedLoad(
8825 const VPIntrinsic &VPIntrin, EVT VT,
8826 const SmallVectorImpl<SDValue> &OpValues) {
8827 SDLoc DL = getCurSDLoc();
8828 Value *PtrOperand = VPIntrin.getArgOperand(0);
8829 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8830 if (!Alignment)
8831 Alignment = DAG.getEVTAlign(VT.getScalarType());
8832 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8833 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8834 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8835 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8836 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8837 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8838 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8839 MachineMemOperand::Flags MMOFlags =
8840 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8841 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8842 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8843 *Alignment, AAInfo, Ranges);
8844
8845 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8846 OpValues[2], OpValues[3], MMO,
8847 false /*IsExpanding*/);
8848
8849 if (AddToChain)
8850 PendingLoads.push_back(LD.getValue(1));
8851 setValue(&VPIntrin, LD);
8852}
8853
8854void SelectionDAGBuilder::visitVPStridedStore(
8855 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8856 SDLoc DL = getCurSDLoc();
8857 Value *PtrOperand = VPIntrin.getArgOperand(1);
8858 EVT VT = OpValues[0].getValueType();
8859 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8860 if (!Alignment)
8861 Alignment = DAG.getEVTAlign(VT.getScalarType());
8862 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8863 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8864 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8865 MachineMemOperand::Flags MMOFlags =
8866 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8867 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8868 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8869 *Alignment, AAInfo);
8870
8871 SDValue ST = DAG.getStridedStoreVP(
8872 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8873 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8874 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8875 /*IsCompressing*/ false);
8876
8877 DAG.setRoot(ST);
8878 setValue(&VPIntrin, ST);
8879}
8880
8881void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8882 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8883 SDLoc DL = getCurSDLoc();
8884
8885 ISD::CondCode Condition;
8887
8888 Value *Op1 = VPIntrin.getOperand(0);
8889 Value *Op2 = VPIntrin.getOperand(1);
8890 // #2 is the condition code
8891 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8892 SDValue EVL = getValue(VPIntrin.getOperand(4));
8893 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8894 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8895 "Unexpected target EVL type");
8896 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8897
8898 if (VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy()) {
8899 Condition = getFCmpCondCode(CondCode);
8900 SimplifyQuery SQ(DAG.getDataLayout(), &VPIntrin);
8901 if (isKnownNeverNaN(Op2, SQ) && isKnownNeverNaN(Op1, SQ))
8902 Condition = getFCmpCodeWithoutNaN(Condition);
8903 } else {
8904 Condition = getICmpCondCode(CondCode);
8905 }
8906
8907 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8908 VPIntrin.getType());
8909 setValue(&VPIntrin, DAG.getSetCCVP(DL, DestVT, getValue(Op1), getValue(Op2),
8910 Condition, MaskOp, EVL));
8911}
8912
8913void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8914 const VPIntrinsic &VPIntrin) {
8915 SDLoc DL = getCurSDLoc();
8916 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8917
8918 auto IID = VPIntrin.getIntrinsicID();
8919
8920 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8921 return visitVPCmp(*CmpI);
8922
8923 SmallVector<EVT, 4> ValueVTs;
8924 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8925 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8926 SDVTList VTs = DAG.getVTList(ValueVTs);
8927
8928 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8929
8930 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8931 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8932 "Unexpected target EVL type");
8933
8934 // Request operands.
8935 SmallVector<SDValue, 7> OpValues;
8936 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8937 auto Op = getValue(VPIntrin.getArgOperand(I));
8938 if (I == EVLParamPos)
8939 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8940 OpValues.push_back(Op);
8941 }
8942
8943 switch (Opcode) {
8944 default: {
8945 SDNodeFlags SDFlags;
8946 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8947 SDFlags.copyFMF(*FPMO);
8948 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8949 setValue(&VPIntrin, Result);
8950 break;
8951 }
8952 case ISD::VP_LOAD:
8953 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8954 break;
8955 case ISD::VP_LOAD_FF:
8956 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8957 break;
8958 case ISD::VP_GATHER:
8959 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8960 break;
8961 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8962 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8963 break;
8964 case ISD::VP_STORE:
8965 visitVPStore(VPIntrin, OpValues);
8966 break;
8967 case ISD::VP_SCATTER:
8968 visitVPScatter(VPIntrin, OpValues);
8969 break;
8970 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8971 visitVPStridedStore(VPIntrin, OpValues);
8972 break;
8973 case ISD::VP_FMULADD: {
8974 assert(OpValues.size() == 5 && "Unexpected number of operands");
8975 SDNodeFlags SDFlags;
8976 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8977 SDFlags.copyFMF(*FPMO);
8978 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8979 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8980 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8981 } else {
8982 SDValue Mul = DAG.getNode(
8983 ISD::VP_FMUL, DL, VTs,
8984 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8985 SDValue Add =
8986 DAG.getNode(ISD::VP_FADD, DL, VTs,
8987 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8988 setValue(&VPIntrin, Add);
8989 }
8990 break;
8991 }
8992 case ISD::VP_IS_FPCLASS: {
8993 const DataLayout DLayout = DAG.getDataLayout();
8994 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8995 auto Constant = OpValues[1]->getAsZExtVal();
8996 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8997 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8998 {OpValues[0], Check, OpValues[2], OpValues[3]});
8999 setValue(&VPIntrin, V);
9000 return;
9001 }
9002 case ISD::VP_INTTOPTR: {
9003 SDValue N = OpValues[0];
9004 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
9005 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
9006 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
9007 OpValues[2]);
9008 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
9009 OpValues[2]);
9010 setValue(&VPIntrin, N);
9011 break;
9012 }
9013 case ISD::VP_PTRTOINT: {
9014 SDValue N = OpValues[0];
9015 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9016 VPIntrin.getType());
9017 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
9018 VPIntrin.getOperand(0)->getType());
9019 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
9020 OpValues[2]);
9021 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
9022 OpValues[2]);
9023 setValue(&VPIntrin, N);
9024 break;
9025 }
9026 case ISD::VP_ABS:
9027 case ISD::VP_CTLZ:
9028 case ISD::VP_CTLZ_ZERO_UNDEF:
9029 case ISD::VP_CTTZ:
9030 case ISD::VP_CTTZ_ZERO_UNDEF:
9031 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
9032 case ISD::VP_CTTZ_ELTS: {
9033 SDValue Result =
9034 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
9035 setValue(&VPIntrin, Result);
9036 break;
9037 }
9038 }
9039}
9040
9042 const BasicBlock *EHPadBB,
9043 MCSymbol *&BeginLabel) {
9044 MachineFunction &MF = DAG.getMachineFunction();
9045
9046 // Insert a label before the invoke call to mark the try range. This can be
9047 // used to detect deletion of the invoke via the MachineModuleInfo.
9048 BeginLabel = MF.getContext().createTempSymbol();
9049
9050 // For SjLj, keep track of which landing pads go with which invokes
9051 // so as to maintain the ordering of pads in the LSDA.
9052 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
9053 if (CallSiteIndex) {
9054 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
9055 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
9056
9057 // Now that the call site is handled, stop tracking it.
9058 FuncInfo.setCurrentCallSite(0);
9059 }
9060
9061 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
9062}
9063
9064SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
9065 const BasicBlock *EHPadBB,
9066 MCSymbol *BeginLabel) {
9067 assert(BeginLabel && "BeginLabel should've been set");
9068
9070
9071 // Insert a label at the end of the invoke call to mark the try range. This
9072 // can be used to detect deletion of the invoke via the MachineModuleInfo.
9073 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
9074 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
9075
9076 // Inform MachineModuleInfo of range.
9078 // There is a platform (e.g. wasm) that uses funclet style IR but does not
9079 // actually use outlined funclets and their LSDA info style.
9080 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
9081 assert(II && "II should've been set");
9082 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
9083 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
9084 } else if (!isScopedEHPersonality(Pers)) {
9085 assert(EHPadBB);
9086 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
9087 }
9088
9089 return Chain;
9090}
9091
9092std::pair<SDValue, SDValue>
9094 const BasicBlock *EHPadBB) {
9095 MCSymbol *BeginLabel = nullptr;
9096
9097 if (EHPadBB) {
9098 // Both PendingLoads and PendingExports must be flushed here;
9099 // this call might not return.
9100 (void)getRoot();
9101 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
9102 CLI.setChain(getRoot());
9103 }
9104
9105 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9106 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
9107
9108 assert((CLI.IsTailCall || Result.second.getNode()) &&
9109 "Non-null chain expected with non-tail call!");
9110 assert((Result.second.getNode() || !Result.first.getNode()) &&
9111 "Null value expected with tail call!");
9112
9113 if (!Result.second.getNode()) {
9114 // As a special case, a null chain means that a tail call has been emitted
9115 // and the DAG root is already updated.
9116 HasTailCall = true;
9117
9118 // Since there's no actual continuation from this block, nothing can be
9119 // relying on us setting vregs for them.
9120 PendingExports.clear();
9121 } else {
9122 DAG.setRoot(Result.second);
9123 }
9124
9125 if (EHPadBB) {
9126 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
9127 BeginLabel));
9128 Result.second = getRoot();
9129 }
9130
9131 return Result;
9132}
9133
9135 bool isMustTailCall = CB.isMustTailCall();
9136
9137 // Avoid emitting tail calls in functions with the disable-tail-calls
9138 // attribute.
9139 const Function *Caller = CB.getParent()->getParent();
9140 if (!isMustTailCall &&
9141 Caller->getFnAttribute("disable-tail-calls").getValueAsBool())
9142 return false;
9143
9144 // We can't tail call inside a function with a swifterror argument. Lowering
9145 // does not support this yet. It would have to move into the swifterror
9146 // register before the call.
9147 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
9148 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
9149 return false;
9150
9151 // Check if target-independent constraints permit a tail call here.
9152 // Target-dependent constraints are checked within TLI->LowerCallTo.
9153 return isInTailCallPosition(CB, DAG.getTarget());
9154}
9155
9157 bool isTailCall, bool isMustTailCall,
9158 const BasicBlock *EHPadBB,
9159 const TargetLowering::PtrAuthInfo *PAI) {
9160 auto &DL = DAG.getDataLayout();
9161 FunctionType *FTy = CB.getFunctionType();
9162 Type *RetTy = CB.getType();
9163
9165 Args.reserve(CB.arg_size());
9166
9167 const Value *SwiftErrorVal = nullptr;
9168 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9169
9170 if (isTailCall)
9171 isTailCall = canTailCall(CB);
9172
9173 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
9174 const Value *V = *I;
9175
9176 // Skip empty types
9177 if (V->getType()->isEmptyTy())
9178 continue;
9179
9180 SDValue ArgNode = getValue(V);
9181 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
9182 Entry.setAttributes(&CB, I - CB.arg_begin());
9183
9184 // Use swifterror virtual register as input to the call.
9185 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
9186 SwiftErrorVal = V;
9187 // We find the virtual register for the actual swifterror argument.
9188 // Instead of using the Value, we use the virtual register instead.
9189 Entry.Node =
9190 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
9191 EVT(TLI.getPointerTy(DL)));
9192 }
9193
9194 Args.push_back(Entry);
9195
9196 // If we have an explicit sret argument that is an Instruction, (i.e., it
9197 // might point to function-local memory), we can't meaningfully tail-call.
9198 if (Entry.IsSRet && isa<Instruction>(V))
9199 isTailCall = false;
9200 }
9201
9202 // If call site has a cfguardtarget operand bundle, create and add an
9203 // additional ArgListEntry.
9204 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9205 Value *V = Bundle->Inputs[0];
9207 Entry.IsCFGuardTarget = true;
9208 Args.push_back(Entry);
9209 }
9210
9211 // Disable tail calls if there is an swifterror argument. Targets have not
9212 // been updated to support tail calls.
9213 if (TLI.supportSwiftError() && SwiftErrorVal)
9214 isTailCall = false;
9215
9216 ConstantInt *CFIType = nullptr;
9217 if (CB.isIndirectCall()) {
9218 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9219 if (!TLI.supportKCFIBundles())
9221 "Target doesn't support calls with kcfi operand bundles.");
9222 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9223 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9224 }
9225 }
9226
9227 SDValue ConvControlToken;
9228 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9229 auto *Token = Bundle->Inputs[0].get();
9230 ConvControlToken = getValue(Token);
9231 }
9232
9233 GlobalValue *DeactivationSymbol = nullptr;
9235 DeactivationSymbol = cast<GlobalValue>(Bundle->Inputs[0].get());
9236 }
9237
9240 .setChain(getRoot())
9241 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9242 .setTailCall(isTailCall)
9246 .setCFIType(CFIType)
9247 .setConvergenceControlToken(ConvControlToken)
9248 .setDeactivationSymbol(DeactivationSymbol);
9249
9250 // Set the pointer authentication info if we have it.
9251 if (PAI) {
9252 if (!TLI.supportPtrAuthBundles())
9254 "This target doesn't support calls with ptrauth operand bundles.");
9255 CLI.setPtrAuth(*PAI);
9256 }
9257
9258 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9259
9260 if (Result.first.getNode()) {
9261 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9262 Result.first = lowerNoFPClassToAssertNoFPClass(DAG, CB, Result.first);
9263 setValue(&CB, Result.first);
9264 }
9265
9266 // The last element of CLI.InVals has the SDValue for swifterror return.
9267 // Here we copy it to a virtual register and update SwiftErrorMap for
9268 // book-keeping.
9269 if (SwiftErrorVal && TLI.supportSwiftError()) {
9270 // Get the last element of InVals.
9271 SDValue Src = CLI.InVals.back();
9272 Register VReg =
9273 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9274 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9275 DAG.setRoot(CopyNode);
9276 }
9277}
9278
9279static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9280 SelectionDAGBuilder &Builder) {
9281 // Check to see if this load can be trivially constant folded, e.g. if the
9282 // input is from a string literal.
9283 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9284 // Cast pointer to the type we really want to load.
9285 Type *LoadTy =
9286 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9287 if (LoadVT.isVector())
9288 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9289 if (const Constant *LoadCst =
9290 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9291 LoadTy, Builder.DAG.getDataLayout()))
9292 return Builder.getValue(LoadCst);
9293 }
9294
9295 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9296 // still constant memory, the input chain can be the entry node.
9297 SDValue Root;
9298 bool ConstantMemory = false;
9299
9300 // Do not serialize (non-volatile) loads of constant memory with anything.
9301 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9302 Root = Builder.DAG.getEntryNode();
9303 ConstantMemory = true;
9304 } else {
9305 // Do not serialize non-volatile loads against each other.
9306 Root = Builder.DAG.getRoot();
9307 }
9308
9309 SDValue Ptr = Builder.getValue(PtrVal);
9310 SDValue LoadVal =
9311 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9312 MachinePointerInfo(PtrVal), Align(1));
9313
9314 if (!ConstantMemory)
9315 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9316 return LoadVal;
9317}
9318
9319/// Record the value for an instruction that produces an integer result,
9320/// converting the type where necessary.
9321void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9322 SDValue Value,
9323 bool IsSigned) {
9324 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9325 I.getType(), true);
9326 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9327 setValue(&I, Value);
9328}
9329
9330/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9331/// true and lower it. Otherwise return false, and it will be lowered like a
9332/// normal call.
9333/// The caller already checked that \p I calls the appropriate LibFunc with a
9334/// correct prototype.
9335bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9336 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9337 const Value *Size = I.getArgOperand(2);
9338 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9339 if (CSize && CSize->getZExtValue() == 0) {
9340 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9341 I.getType(), true);
9342 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9343 return true;
9344 }
9345
9346 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9347 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9348 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9349 getValue(Size), &I);
9350 if (Res.first.getNode()) {
9351 processIntegerCallValue(I, Res.first, true);
9352 PendingLoads.push_back(Res.second);
9353 return true;
9354 }
9355
9356 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9357 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9358 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9359 return false;
9360
9361 // If the target has a fast compare for the given size, it will return a
9362 // preferred load type for that size. Require that the load VT is legal and
9363 // that the target supports unaligned loads of that type. Otherwise, return
9364 // INVALID.
9365 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9366 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9367 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9368 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9369 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9370 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9371 // TODO: Check alignment of src and dest ptrs.
9372 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9373 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9374 if (!TLI.isTypeLegal(LVT) ||
9375 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9376 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9378 }
9379
9380 return LVT;
9381 };
9382
9383 // This turns into unaligned loads. We only do this if the target natively
9384 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9385 // we'll only produce a small number of byte loads.
9386 MVT LoadVT;
9387 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9388 switch (NumBitsToCompare) {
9389 default:
9390 return false;
9391 case 16:
9392 LoadVT = MVT::i16;
9393 break;
9394 case 32:
9395 LoadVT = MVT::i32;
9396 break;
9397 case 64:
9398 case 128:
9399 case 256:
9400 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9401 break;
9402 }
9403
9404 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9405 return false;
9406
9407 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9408 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9409
9410 // Bitcast to a wide integer type if the loads are vectors.
9411 if (LoadVT.isVector()) {
9412 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9413 LoadL = DAG.getBitcast(CmpVT, LoadL);
9414 LoadR = DAG.getBitcast(CmpVT, LoadR);
9415 }
9416
9417 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9418 processIntegerCallValue(I, Cmp, false);
9419 return true;
9420}
9421
9422/// See if we can lower a memchr call into an optimized form. If so, return
9423/// true and lower it. Otherwise return false, and it will be lowered like a
9424/// normal call.
9425/// The caller already checked that \p I calls the appropriate LibFunc with a
9426/// correct prototype.
9427bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9428 const Value *Src = I.getArgOperand(0);
9429 const Value *Char = I.getArgOperand(1);
9430 const Value *Length = I.getArgOperand(2);
9431
9432 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9433 std::pair<SDValue, SDValue> Res =
9434 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9435 getValue(Src), getValue(Char), getValue(Length),
9436 MachinePointerInfo(Src));
9437 if (Res.first.getNode()) {
9438 setValue(&I, Res.first);
9439 PendingLoads.push_back(Res.second);
9440 return true;
9441 }
9442
9443 return false;
9444}
9445
9446/// See if we can lower a memccpy call into an optimized form. If so, return
9447/// true and lower it, otherwise return false and it will be lowered like a
9448/// normal call.
9449/// The caller already checked that \p I calls the appropriate LibFunc with a
9450/// correct prototype.
9451bool SelectionDAGBuilder::visitMemCCpyCall(const CallInst &I) {
9452 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9453 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemccpy(
9454 DAG, getCurSDLoc(), DAG.getRoot(), getValue(I.getArgOperand(0)),
9455 getValue(I.getArgOperand(1)), getValue(I.getArgOperand(2)),
9456 getValue(I.getArgOperand(3)), &I);
9457
9458 if (Res.first) {
9459 processIntegerCallValue(I, Res.first, true);
9460 PendingLoads.push_back(Res.second);
9461 return true;
9462 }
9463 return false;
9464}
9465
9466/// See if we can lower a mempcpy call into an optimized form. If so, return
9467/// true and lower it. Otherwise return false, and it will be lowered like a
9468/// normal call.
9469/// The caller already checked that \p I calls the appropriate LibFunc with a
9470/// correct prototype.
9471bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9472 SDValue Dst = getValue(I.getArgOperand(0));
9473 SDValue Src = getValue(I.getArgOperand(1));
9474 SDValue Size = getValue(I.getArgOperand(2));
9475
9476 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9477 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9478 // DAG::getMemcpy needs Alignment to be defined.
9479 Align Alignment = std::min(DstAlign, SrcAlign);
9480
9481 SDLoc sdl = getCurSDLoc();
9482
9483 // In the mempcpy context we need to pass in a false value for isTailCall
9484 // because the return pointer needs to be adjusted by the size of
9485 // the copied memory.
9486 SDValue Root = getMemoryRoot();
9487 SDValue MC = DAG.getMemcpy(
9488 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9489 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9490 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9491 assert(MC.getNode() != nullptr &&
9492 "** memcpy should not be lowered as TailCall in mempcpy context **");
9493 DAG.setRoot(MC);
9494
9495 // Check if Size needs to be truncated or extended.
9496 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9497
9498 // Adjust return pointer to point just past the last dst byte.
9499 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9500 setValue(&I, DstPlusSize);
9501 return true;
9502}
9503
9504/// See if we can lower a strcpy call into an optimized form. If so, return
9505/// true and lower it, otherwise return false and it will be lowered like a
9506/// normal call.
9507/// The caller already checked that \p I calls the appropriate LibFunc with a
9508/// correct prototype.
9509bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9510 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9511
9512 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9513 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrcpy(
9514 DAG, getCurSDLoc(), getRoot(), getValue(Arg0), getValue(Arg1),
9515 MachinePointerInfo(Arg0), MachinePointerInfo(Arg1), isStpcpy, &I);
9516 if (Res.first.getNode()) {
9517 setValue(&I, Res.first);
9518 DAG.setRoot(Res.second);
9519 return true;
9520 }
9521
9522 return false;
9523}
9524
9525/// See if we can lower a strcmp call into an optimized form. If so, return
9526/// true and lower it, otherwise return false and it will be lowered like a
9527/// normal call.
9528/// The caller already checked that \p I calls the appropriate LibFunc with a
9529/// correct prototype.
9530bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9531 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9532
9533 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9534 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrcmp(
9535 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), getValue(Arg1),
9536 MachinePointerInfo(Arg0), MachinePointerInfo(Arg1), &I);
9537 if (Res.first.getNode()) {
9538 processIntegerCallValue(I, Res.first, true);
9539 PendingLoads.push_back(Res.second);
9540 return true;
9541 }
9542
9543 return false;
9544}
9545
9546/// See if we can lower a strlen call into an optimized form. If so, return
9547/// true and lower it, otherwise return false and it will be lowered like a
9548/// normal call.
9549/// The caller already checked that \p I calls the appropriate LibFunc with a
9550/// correct prototype.
9551bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9552 const Value *Arg0 = I.getArgOperand(0);
9553
9554 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9555 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrlen(
9556 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), &I);
9557 if (Res.first.getNode()) {
9558 processIntegerCallValue(I, Res.first, false);
9559 PendingLoads.push_back(Res.second);
9560 return true;
9561 }
9562
9563 return false;
9564}
9565
9566/// See if we can lower a strnlen call into an optimized form. If so, return
9567/// true and lower it, otherwise return false and it will be lowered like a
9568/// normal call.
9569/// The caller already checked that \p I calls the appropriate LibFunc with a
9570/// correct prototype.
9571bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9572 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9573
9574 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9575 std::pair<SDValue, SDValue> Res =
9576 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9577 getValue(Arg0), getValue(Arg1),
9578 MachinePointerInfo(Arg0));
9579 if (Res.first.getNode()) {
9580 processIntegerCallValue(I, Res.first, false);
9581 PendingLoads.push_back(Res.second);
9582 return true;
9583 }
9584
9585 return false;
9586}
9587
9588/// See if we can lower a Strstr call into an optimized form. If so, return
9589/// true and lower it, otherwise return false and it will be lowered like a
9590/// normal call.
9591/// The caller already checked that \p I calls the appropriate LibFunc with a
9592/// correct prototype.
9593bool SelectionDAGBuilder::visitStrstrCall(const CallInst &I) {
9594 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9595 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9596 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrstr(
9597 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), getValue(Arg1), &I);
9598 if (Res.first) {
9599 processIntegerCallValue(I, Res.first, false);
9600 PendingLoads.push_back(Res.second);
9601 return true;
9602 }
9603 return false;
9604}
9605
9606/// See if we can lower a unary floating-point operation into an SDNode with
9607/// the specified Opcode. If so, return true and lower it, otherwise return
9608/// false and it will be lowered like a normal call.
9609/// The caller already checked that \p I calls the appropriate LibFunc with a
9610/// correct prototype.
9611bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9612 unsigned Opcode) {
9613 // We already checked this call's prototype; verify it doesn't modify errno.
9614 // Do not perform optimizations for call sites that require strict
9615 // floating-point semantics.
9616 if (!I.onlyReadsMemory() || I.isStrictFP())
9617 return false;
9618
9619 SDNodeFlags Flags;
9620 Flags.copyFMF(cast<FPMathOperator>(I));
9621
9622 SDValue Tmp = getValue(I.getArgOperand(0));
9623 setValue(&I,
9624 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9625 return true;
9626}
9627
9628/// See if we can lower a binary floating-point operation into an SDNode with
9629/// the specified Opcode. If so, return true and lower it. Otherwise return
9630/// false, and it will be lowered like a normal call.
9631/// The caller already checked that \p I calls the appropriate LibFunc with a
9632/// correct prototype.
9633bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9634 unsigned Opcode) {
9635 // We already checked this call's prototype; verify it doesn't modify errno.
9636 // Do not perform optimizations for call sites that require strict
9637 // floating-point semantics.
9638 if (!I.onlyReadsMemory() || I.isStrictFP())
9639 return false;
9640
9641 SDNodeFlags Flags;
9642 Flags.copyFMF(cast<FPMathOperator>(I));
9643
9644 SDValue Tmp0 = getValue(I.getArgOperand(0));
9645 SDValue Tmp1 = getValue(I.getArgOperand(1));
9646 EVT VT = Tmp0.getValueType();
9647 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9648 return true;
9649}
9650
9651void SelectionDAGBuilder::visitCall(const CallInst &I) {
9652 // Handle inline assembly differently.
9653 if (I.isInlineAsm()) {
9654 visitInlineAsm(I);
9655 return;
9656 }
9657
9659
9660 if (Function *F = I.getCalledFunction()) {
9661 if (F->isDeclaration()) {
9662 // Is this an LLVM intrinsic?
9663 if (unsigned IID = F->getIntrinsicID()) {
9664 visitIntrinsicCall(I, IID);
9665 return;
9666 }
9667 }
9668
9669 // Check for well-known libc/libm calls. If the function is internal, it
9670 // can't be a library call. Don't do the check if marked as nobuiltin for
9671 // some reason.
9672 // This code should not handle libcalls that are already canonicalized to
9673 // intrinsics by the middle-end.
9674 LibFunc Func;
9675 if (!I.isNoBuiltin() && !F->hasLocalLinkage() && F->hasName() &&
9676 LibInfo->getLibFunc(*F, Func) && LibInfo->hasOptimizedCodeGen(Func)) {
9677 switch (Func) {
9678 default: break;
9679 case LibFunc_bcmp:
9680 if (visitMemCmpBCmpCall(I))
9681 return;
9682 break;
9683 case LibFunc_copysign:
9684 case LibFunc_copysignf:
9685 case LibFunc_copysignl:
9686 // We already checked this call's prototype; verify it doesn't modify
9687 // errno.
9688 if (I.onlyReadsMemory()) {
9689 SDValue LHS = getValue(I.getArgOperand(0));
9690 SDValue RHS = getValue(I.getArgOperand(1));
9692 LHS.getValueType(), LHS, RHS));
9693 return;
9694 }
9695 break;
9696 case LibFunc_sin:
9697 case LibFunc_sinf:
9698 case LibFunc_sinl:
9699 if (visitUnaryFloatCall(I, ISD::FSIN))
9700 return;
9701 break;
9702 case LibFunc_cos:
9703 case LibFunc_cosf:
9704 case LibFunc_cosl:
9705 if (visitUnaryFloatCall(I, ISD::FCOS))
9706 return;
9707 break;
9708 case LibFunc_tan:
9709 case LibFunc_tanf:
9710 case LibFunc_tanl:
9711 if (visitUnaryFloatCall(I, ISD::FTAN))
9712 return;
9713 break;
9714 case LibFunc_asin:
9715 case LibFunc_asinf:
9716 case LibFunc_asinl:
9717 if (visitUnaryFloatCall(I, ISD::FASIN))
9718 return;
9719 break;
9720 case LibFunc_acos:
9721 case LibFunc_acosf:
9722 case LibFunc_acosl:
9723 if (visitUnaryFloatCall(I, ISD::FACOS))
9724 return;
9725 break;
9726 case LibFunc_atan:
9727 case LibFunc_atanf:
9728 case LibFunc_atanl:
9729 if (visitUnaryFloatCall(I, ISD::FATAN))
9730 return;
9731 break;
9732 case LibFunc_atan2:
9733 case LibFunc_atan2f:
9734 case LibFunc_atan2l:
9735 if (visitBinaryFloatCall(I, ISD::FATAN2))
9736 return;
9737 break;
9738 case LibFunc_sinh:
9739 case LibFunc_sinhf:
9740 case LibFunc_sinhl:
9741 if (visitUnaryFloatCall(I, ISD::FSINH))
9742 return;
9743 break;
9744 case LibFunc_cosh:
9745 case LibFunc_coshf:
9746 case LibFunc_coshl:
9747 if (visitUnaryFloatCall(I, ISD::FCOSH))
9748 return;
9749 break;
9750 case LibFunc_tanh:
9751 case LibFunc_tanhf:
9752 case LibFunc_tanhl:
9753 if (visitUnaryFloatCall(I, ISD::FTANH))
9754 return;
9755 break;
9756 case LibFunc_sqrt:
9757 case LibFunc_sqrtf:
9758 case LibFunc_sqrtl:
9759 case LibFunc_sqrt_finite:
9760 case LibFunc_sqrtf_finite:
9761 case LibFunc_sqrtl_finite:
9762 if (visitUnaryFloatCall(I, ISD::FSQRT))
9763 return;
9764 break;
9765 case LibFunc_log2:
9766 case LibFunc_log2f:
9767 case LibFunc_log2l:
9768 if (visitUnaryFloatCall(I, ISD::FLOG2))
9769 return;
9770 break;
9771 case LibFunc_exp2:
9772 case LibFunc_exp2f:
9773 case LibFunc_exp2l:
9774 if (visitUnaryFloatCall(I, ISD::FEXP2))
9775 return;
9776 break;
9777 case LibFunc_exp10:
9778 case LibFunc_exp10f:
9779 case LibFunc_exp10l:
9780 if (visitUnaryFloatCall(I, ISD::FEXP10))
9781 return;
9782 break;
9783 case LibFunc_ldexp:
9784 case LibFunc_ldexpf:
9785 case LibFunc_ldexpl:
9786 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9787 return;
9788 break;
9789 case LibFunc_strstr:
9790 if (visitStrstrCall(I))
9791 return;
9792 break;
9793 case LibFunc_memcmp:
9794 if (visitMemCmpBCmpCall(I))
9795 return;
9796 break;
9797 case LibFunc_memccpy:
9798 if (visitMemCCpyCall(I))
9799 return;
9800 break;
9801 case LibFunc_mempcpy:
9802 if (visitMemPCpyCall(I))
9803 return;
9804 break;
9805 case LibFunc_memchr:
9806 if (visitMemChrCall(I))
9807 return;
9808 break;
9809 case LibFunc_strcpy:
9810 if (visitStrCpyCall(I, false))
9811 return;
9812 break;
9813 case LibFunc_stpcpy:
9814 if (visitStrCpyCall(I, true))
9815 return;
9816 break;
9817 case LibFunc_strcmp:
9818 if (visitStrCmpCall(I))
9819 return;
9820 break;
9821 case LibFunc_strlen:
9822 if (visitStrLenCall(I))
9823 return;
9824 break;
9825 case LibFunc_strnlen:
9826 if (visitStrNLenCall(I))
9827 return;
9828 break;
9829 }
9830 }
9831 }
9832
9833 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9834 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9835 return;
9836 }
9837
9838 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9839 // have to do anything here to lower funclet bundles.
9840 // CFGuardTarget bundles are lowered in LowerCallTo.
9842 I, "calls",
9847
9848 SDValue Callee = getValue(I.getCalledOperand());
9849
9850 if (I.hasDeoptState())
9851 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9852 else
9853 // Check if we can potentially perform a tail call. More detailed checking
9854 // is be done within LowerCallTo, after more information about the call is
9855 // known.
9856 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9857}
9858
9860 const CallBase &CB, const BasicBlock *EHPadBB) {
9861 auto PAB = CB.getOperandBundle("ptrauth");
9862 const Value *CalleeV = CB.getCalledOperand();
9863
9864 // Gather the call ptrauth data from the operand bundle:
9865 // [ i32 <key>, i64 <discriminator> ]
9866 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9867 const Value *Discriminator = PAB->Inputs[1];
9868
9869 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9870 assert(Discriminator->getType()->isIntegerTy(64) &&
9871 "Invalid ptrauth discriminator");
9872
9873 // Look through ptrauth constants to find the raw callee.
9874 // Do a direct unauthenticated call if we found it and everything matches.
9875 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9876 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9877 DAG.getDataLayout()))
9878 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9879 CB.isMustTailCall(), EHPadBB);
9880
9881 // Functions should never be ptrauth-called directly.
9882 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9883
9884 // Otherwise, do an authenticated indirect call.
9885 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9886 getValue(Discriminator)};
9887
9888 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9889 EHPadBB, &PAI);
9890}
9891
9892namespace {
9893
9894/// AsmOperandInfo - This contains information for each constraint that we are
9895/// lowering.
9896class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9897public:
9898 /// CallOperand - If this is the result output operand or a clobber
9899 /// this is null, otherwise it is the incoming operand to the CallInst.
9900 /// This gets modified as the asm is processed.
9901 SDValue CallOperand;
9902
9903 /// AssignedRegs - If this is a register or register class operand, this
9904 /// contains the set of register corresponding to the operand.
9905 RegsForValue AssignedRegs;
9906
9907 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9908 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9909 }
9910
9911 /// Whether or not this operand accesses memory
9912 bool hasMemory(const TargetLowering &TLI) const {
9913 // Indirect operand accesses access memory.
9914 if (isIndirect)
9915 return true;
9916
9917 for (const auto &Code : Codes)
9919 return true;
9920
9921 return false;
9922 }
9923};
9924
9925
9926} // end anonymous namespace
9927
9928/// Make sure that the output operand \p OpInfo and its corresponding input
9929/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9930/// out).
9931static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9932 SDISelAsmOperandInfo &MatchingOpInfo,
9933 SelectionDAG &DAG) {
9934 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9935 return;
9936
9938 const auto &TLI = DAG.getTargetLoweringInfo();
9939
9940 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9941 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9942 OpInfo.ConstraintVT);
9943 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9944 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9945 MatchingOpInfo.ConstraintVT);
9946 const bool OutOpIsIntOrFP =
9947 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9948 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9949 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9950 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9951 // FIXME: error out in a more elegant fashion
9952 report_fatal_error("Unsupported asm: input constraint"
9953 " with a matching output constraint of"
9954 " incompatible type!");
9955 }
9956 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9957}
9958
9959/// Get a direct memory input to behave well as an indirect operand.
9960/// This may introduce stores, hence the need for a \p Chain.
9961/// \return The (possibly updated) chain.
9962static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9963 SDISelAsmOperandInfo &OpInfo,
9964 SelectionDAG &DAG) {
9965 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9966
9967 // If we don't have an indirect input, put it in the constpool if we can,
9968 // otherwise spill it to a stack slot.
9969 // TODO: This isn't quite right. We need to handle these according to
9970 // the addressing mode that the constraint wants. Also, this may take
9971 // an additional register for the computation and we don't want that
9972 // either.
9973
9974 // If the operand is a float, integer, or vector constant, spill to a
9975 // constant pool entry to get its address.
9976 const Value *OpVal = OpInfo.CallOperandVal;
9977 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9979 OpInfo.CallOperand = DAG.getConstantPool(
9980 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9981 return Chain;
9982 }
9983
9984 // Otherwise, create a stack slot and emit a store to it before the asm.
9985 Type *Ty = OpVal->getType();
9986 auto &DL = DAG.getDataLayout();
9987 TypeSize TySize = DL.getTypeAllocSize(Ty);
9990 int StackID = 0;
9991 if (TySize.isScalable())
9992 StackID = TFI->getStackIDForScalableVectors();
9993 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9994 DL.getPrefTypeAlign(Ty), false,
9995 nullptr, StackID);
9996 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9997 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9999 TLI.getMemValueType(DL, Ty));
10000 OpInfo.CallOperand = StackSlot;
10001
10002 return Chain;
10003}
10004
10005/// GetRegistersForValue - Assign registers (virtual or physical) for the
10006/// specified operand. We prefer to assign virtual registers, to allow the
10007/// register allocator to handle the assignment process. However, if the asm
10008/// uses features that we can't model on machineinstrs, we have SDISel do the
10009/// allocation. This produces generally horrible, but correct, code.
10010///
10011/// OpInfo describes the operand
10012/// RefOpInfo describes the matching operand if any, the operand otherwise
10013static std::optional<unsigned>
10015 SDISelAsmOperandInfo &OpInfo,
10016 SDISelAsmOperandInfo &RefOpInfo) {
10017 LLVMContext &Context = *DAG.getContext();
10018 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10019
10023
10024 // No work to do for memory/address operands.
10025 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
10026 OpInfo.ConstraintType == TargetLowering::C_Address)
10027 return std::nullopt;
10028
10029 // If this is a constraint for a single physreg, or a constraint for a
10030 // register class, find it.
10031 unsigned AssignedReg;
10032 const TargetRegisterClass *RC;
10033 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
10034 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
10035 // RC is unset only on failure. Return immediately.
10036 if (!RC)
10037 return std::nullopt;
10038
10039 // Get the actual register value type. This is important, because the user
10040 // may have asked for (e.g.) the AX register in i32 type. We need to
10041 // remember that AX is actually i16 to get the right extension.
10042 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
10043
10044 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
10045 // If this is an FP operand in an integer register (or visa versa), or more
10046 // generally if the operand value disagrees with the register class we plan
10047 // to stick it in, fix the operand type.
10048 //
10049 // If this is an input value, the bitcast to the new type is done now.
10050 // Bitcast for output value is done at the end of visitInlineAsm().
10051 if ((OpInfo.Type == InlineAsm::isOutput ||
10052 OpInfo.Type == InlineAsm::isInput) &&
10053 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
10054 // Try to convert to the first EVT that the reg class contains. If the
10055 // types are identical size, use a bitcast to convert (e.g. two differing
10056 // vector types). Note: output bitcast is done at the end of
10057 // visitInlineAsm().
10058 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
10059 // Exclude indirect inputs while they are unsupported because the code
10060 // to perform the load is missing and thus OpInfo.CallOperand still
10061 // refers to the input address rather than the pointed-to value.
10062 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
10063 OpInfo.CallOperand =
10064 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
10065 OpInfo.ConstraintVT = RegVT;
10066 // If the operand is an FP value and we want it in integer registers,
10067 // use the corresponding integer type. This turns an f64 value into
10068 // i64, which can be passed with two i32 values on a 32-bit machine.
10069 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
10070 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
10071 if (OpInfo.Type == InlineAsm::isInput)
10072 OpInfo.CallOperand =
10073 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
10074 OpInfo.ConstraintVT = VT;
10075 }
10076 }
10077 }
10078
10079 // No need to allocate a matching input constraint since the constraint it's
10080 // matching to has already been allocated.
10081 if (OpInfo.isMatchingInputConstraint())
10082 return std::nullopt;
10083
10084 EVT ValueVT = OpInfo.ConstraintVT;
10085 if (OpInfo.ConstraintVT == MVT::Other)
10086 ValueVT = RegVT;
10087
10088 // Initialize NumRegs.
10089 unsigned NumRegs = 1;
10090 if (OpInfo.ConstraintVT != MVT::Other)
10091 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
10092
10093 // If this is a constraint for a specific physical register, like {r17},
10094 // assign it now.
10095
10096 // If this associated to a specific register, initialize iterator to correct
10097 // place. If virtual, make sure we have enough registers
10098
10099 // Initialize iterator if necessary
10102
10103 // Do not check for single registers.
10104 if (AssignedReg) {
10105 I = std::find(I, RC->end(), AssignedReg);
10106 if (I == RC->end()) {
10107 // RC does not contain the selected register, which indicates a
10108 // mismatch between the register and the required type/bitwidth.
10109 return {AssignedReg};
10110 }
10111 }
10112
10113 for (; NumRegs; --NumRegs, ++I) {
10114 assert(I != RC->end() && "Ran out of registers to allocate!");
10115 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
10116 Regs.push_back(R);
10117 }
10118
10119 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
10120 return std::nullopt;
10121}
10122
10123static unsigned
10125 const std::vector<SDValue> &AsmNodeOperands) {
10126 // Scan until we find the definition we already emitted of this operand.
10127 unsigned CurOp = InlineAsm::Op_FirstOperand;
10128 for (; OperandNo; --OperandNo) {
10129 // Advance to the next operand.
10130 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
10131 const InlineAsm::Flag F(OpFlag);
10132 assert(
10133 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
10134 "Skipped past definitions?");
10135 CurOp += F.getNumOperandRegisters() + 1;
10136 }
10137 return CurOp;
10138}
10139
10140namespace {
10141
10142class ExtraFlags {
10143 unsigned Flags = 0;
10144
10145public:
10146 explicit ExtraFlags(const CallBase &Call) {
10147 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10148 if (IA->hasSideEffects())
10150 if (IA->isAlignStack())
10152 if (IA->canThrow())
10154 if (Call.isConvergent())
10156 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
10157 }
10158
10159 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
10160 // Ideally, we would only check against memory constraints. However, the
10161 // meaning of an Other constraint can be target-specific and we can't easily
10162 // reason about it. Therefore, be conservative and set MayLoad/MayStore
10163 // for Other constraints as well.
10166 if (OpInfo.Type == InlineAsm::isInput)
10168 else if (OpInfo.Type == InlineAsm::isOutput)
10170 else if (OpInfo.Type == InlineAsm::isClobber)
10172 }
10173 }
10174
10175 unsigned get() const { return Flags; }
10176};
10177
10178} // end anonymous namespace
10179
10180static bool isFunction(SDValue Op) {
10181 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
10182 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10183 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10184
10185 // In normal "call dllimport func" instruction (non-inlineasm) it force
10186 // indirect access by specifing call opcode. And usually specially print
10187 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10188 // not do in this way now. (In fact, this is similar with "Data Access"
10189 // action). So here we ignore dllimport function.
10190 if (Fn && !Fn->hasDLLImportStorageClass())
10191 return true;
10192 }
10193 }
10194 return false;
10195}
10196
10197namespace {
10198
10199struct ConstraintDecisionInfo {
10200 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10201 std::vector<SDValue> AsmNodeOperands;
10202 SDValue Glue, Chain;
10203 bool HasSideEffect = false;
10204 MCSymbol *BeginLabel = nullptr;
10205
10206 SmallVector<char> Buffer;
10207 raw_svector_ostream ErrorMsg;
10208
10209 ConstraintDecisionInfo() : ErrorMsg(Buffer) {}
10210};
10211
10212} // end anonymous namespace
10213
10214/// Construct operand info objects.
10215static bool
10216constructOperandInfo(ConstraintDecisionInfo &Info,
10217 TargetLowering::AsmOperandInfoVector &TargetConstraints,
10218 SelectionDAGBuilder &Builder, const TargetLowering &TLI,
10219 ExtraFlags &ExtraInfo) {
10220 for (auto &T : TargetConstraints) {
10221 Info.ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10222 SDISelAsmOperandInfo &OpInfo = Info.ConstraintOperands.back();
10223
10224 if (OpInfo.CallOperandVal)
10225 OpInfo.CallOperand = Builder.getValue(OpInfo.CallOperandVal);
10226
10227 if (!Info.HasSideEffect)
10228 Info.HasSideEffect = OpInfo.hasMemory(TLI);
10229
10230 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10231 // FIXME: Could we compute this on OpInfo rather than T?
10232
10233 // Compute the constraint code and ConstraintType to use.
10235
10236 if (T.ConstraintType == TargetLowering::C_Immediate && OpInfo.CallOperand &&
10237 !isa<ConstantSDNode>(OpInfo.CallOperand)) {
10238 // We've delayed emitting a diagnostic like the "n" constraint because
10239 // inlining could cause an integer showing up.
10240 Info.ErrorMsg << "constraint '" << T.ConstraintCode
10241 << "' expects an integer constant expression";
10242 return true;
10243 }
10244
10245 ExtraInfo.update(T);
10246 }
10247
10248 return false;
10249}
10250
10251/// Compute which constraint option to use for each operand.
10252static void
10253computeConstraintToUse(ConstraintDecisionInfo &Info, const CallBase &Call,
10254 TargetLowering::AsmOperandInfoVector &TargetConstraints,
10255 SelectionDAGBuilder &Builder, const TargetLowering &TLI,
10256 const TargetMachine &TM, SelectionDAG &DAG) {
10257 const auto *IA = cast<InlineAsm>(Call.getCalledOperand());
10259 IA->collectAsmStrs(AsmStrs);
10260
10261 int OpNo = -1;
10262 for (SDISelAsmOperandInfo &OpInfo : Info.ConstraintOperands) {
10263 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10264 OpNo++;
10265
10266 // If this is an output operand with a matching input operand, look up the
10267 // matching input. If their types mismatch, e.g. one is an integer, the
10268 // other is floating point, or their sizes are different, flag it as an
10269 // error.
10270 if (OpInfo.hasMatchingInput()) {
10271 SDISelAsmOperandInfo &Input =
10272 Info.ConstraintOperands[OpInfo.MatchingInput];
10273 patchMatchingInput(OpInfo, Input, DAG);
10274 }
10275
10276 // Compute the constraint code and ConstraintType to use.
10277 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10278
10279 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10280 OpInfo.Type == InlineAsm::isClobber) ||
10281 OpInfo.ConstraintType == TargetLowering::C_Address)
10282 continue;
10283
10284 // In Linux PIC model, there are 4 cases about value/label addressing:
10285 //
10286 // 1: Function call or Label jmp inside the module.
10287 // 2: Data access (such as global variable, static variable) inside module.
10288 // 3: Function call or Label jmp outside the module.
10289 // 4: Data access (such as global variable) outside the module.
10290 //
10291 // Due to current llvm inline asm architecture designed to not "recognize"
10292 // the asm code, there are quite troubles for us to treat mem addressing
10293 // differently for same value/adress used in different instuctions.
10294 // For example, in pic model, call a func may in plt way or direclty
10295 // pc-related, but lea/mov a function adress may use got.
10296 //
10297 // Here we try to "recognize" function call for the case 1 and case 3 in
10298 // inline asm. And try to adjust the constraint for them.
10299 //
10300 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10301 // label, so here we don't handle jmp function label now, but we need to
10302 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10303 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10304 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10306 OpInfo.isIndirect = false;
10307 OpInfo.ConstraintType = TargetLowering::C_Address;
10308 }
10309
10310 // If this is a memory input, and if the operand is not indirect, do what we
10311 // need to provide an address for the memory input.
10312 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10313 !OpInfo.isIndirect) {
10314 assert((OpInfo.isMultipleAlternative ||
10315 (OpInfo.Type == InlineAsm::isInput)) &&
10316 "Can only indirectify direct input operands!");
10317
10318 // Memory operands really want the address of the value.
10319 Info.Chain = getAddressForMemoryInput(Info.Chain, Builder.getCurSDLoc(),
10320 OpInfo, DAG);
10321
10322 // There is no longer a Value* corresponding to this operand.
10323 OpInfo.CallOperandVal = nullptr;
10324
10325 // It is now an indirect operand.
10326 OpInfo.isIndirect = true;
10327 }
10328 }
10329}
10330
10331/// Prepare DAG-level operands. As part of this, assign virtual and physical
10332/// registers for inputs and output.
10333static bool prepareDAGLevelOperands(ConstraintDecisionInfo &Info,
10334 const CallBase &Call,
10335 SelectionDAGBuilder &Builder,
10336 const TargetLowering &TLI,
10337 SelectionDAG &DAG) {
10338 SDLoc DL = Builder.getCurSDLoc();
10339 for (SDISelAsmOperandInfo &OpInfo : Info.ConstraintOperands) {
10340 // Assign Registers.
10341 SDISelAsmOperandInfo &RefOpInfo =
10342 OpInfo.isMatchingInputConstraint()
10343 ? Info.ConstraintOperands[OpInfo.getMatchedOperand()]
10344 : OpInfo;
10345 const auto RegError = getRegistersForValue(DAG, DL, OpInfo, RefOpInfo);
10346 if (RegError) {
10347 const MachineFunction &MF = DAG.getMachineFunction();
10349 const char *RegName = TRI.getName(*RegError);
10350 Info.ErrorMsg << "register '" << RegName << "' allocated for constraint '"
10351 << OpInfo.ConstraintCode
10352 << "' does not match required type";
10353 return true;
10354 }
10355
10356 auto DetectWriteToReservedRegister = [&]() {
10357 const MachineFunction &MF = DAG.getMachineFunction();
10359
10360 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10361 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10362 Info.ErrorMsg << "write to reserved register '"
10363 << TRI.getRegAsmName(Reg) << "'";
10364 return true;
10365 }
10366 }
10367
10368 return false;
10369 };
10370 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10371 (OpInfo.Type == InlineAsm::isInput &&
10372 !OpInfo.isMatchingInputConstraint())) &&
10373 "Only address as input operand is allowed.");
10374
10375 switch (OpInfo.Type) {
10377 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10378 const InlineAsm::ConstraintCode ConstraintID =
10379 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10381 "Failed to convert memory constraint code to constraint id.");
10382
10383 // Add information to the INLINEASM node to know about this output.
10385 OpFlags.setMemConstraint(ConstraintID);
10386 Info.AsmNodeOperands.push_back(
10387 DAG.getTargetConstant(OpFlags, DL, MVT::i32));
10388 Info.AsmNodeOperands.push_back(OpInfo.CallOperand);
10389 } else {
10390 // Otherwise, this outputs to a register (directly for C_Register /
10391 // C_RegisterClass, and a target-defined fashion for
10392 // C_Immediate/C_Other). Find a register that we can use.
10393 if (OpInfo.AssignedRegs.Regs.empty()) {
10394 Info.ErrorMsg << "could not allocate output register for "
10395 << "constraint '" << OpInfo.ConstraintCode << "'";
10396 return true;
10397 }
10398
10399 if (DetectWriteToReservedRegister())
10400 return true;
10401
10402 // Add information to the INLINEASM node to know that this register is
10403 // set.
10404 OpInfo.AssignedRegs.AddInlineAsmOperands(
10405 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10407 false, 0, DL, DAG, Info.AsmNodeOperands);
10408 }
10409 break;
10410
10411 case InlineAsm::isInput:
10412 case InlineAsm::isLabel: {
10413 SDValue InOperandVal = OpInfo.CallOperand;
10414
10415 if (OpInfo.isMatchingInputConstraint()) {
10416 // If this is required to match an output register we have already set,
10417 // just use its register.
10418 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10419 Info.AsmNodeOperands);
10420 InlineAsm::Flag Flag(Info.AsmNodeOperands[CurOp]->getAsZExtVal());
10421 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10422 if (OpInfo.isIndirect) {
10423 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10424 Info.ErrorMsg << "inline asm not supported yet: cannot handle "
10425 << "tied indirect register inputs";
10426 return true;
10427 }
10428
10431 MachineRegisterInfo &MRI = MF.getRegInfo();
10433 auto *R = cast<RegisterSDNode>(Info.AsmNodeOperands[CurOp + 1]);
10434 Register TiedReg = R->getReg();
10435 MVT RegVT = R->getSimpleValueType(0);
10436 const TargetRegisterClass *RC =
10437 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10438 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10439 : TRI.getMinimalPhysRegClass(TiedReg);
10440 for (unsigned I = 0, E = Flag.getNumOperandRegisters(); I != E; ++I)
10441 Regs.push_back(MRI.createVirtualRegister(RC));
10442
10443 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10444
10445 // Use the produced MatchedRegs object to
10446 MatchedRegs.getCopyToRegs(InOperandVal, DAG, DL, Info.Chain,
10447 &Info.Glue, &Call);
10449 OpInfo.getMatchedOperand(), DL, DAG,
10450 Info.AsmNodeOperands);
10451 break;
10452 }
10453
10454 assert(Flag.isMemKind() && "Unknown matching constraint!");
10455 assert(Flag.getNumOperandRegisters() == 1 &&
10456 "Unexpected number of operands");
10457
10458 // Add information to the INLINEASM node to know about this input.
10459 // See InlineAsm.h isUseOperandTiedToDef.
10460 Flag.clearMemConstraint();
10461 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10462 Info.AsmNodeOperands.push_back(DAG.getTargetConstant(
10463 Flag, DL, TLI.getPointerTy(DAG.getDataLayout())));
10464 Info.AsmNodeOperands.push_back(Info.AsmNodeOperands[CurOp + 1]);
10465 break;
10466 }
10467
10468 // Treat indirect 'X' constraint as memory.
10469 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10470 OpInfo.isIndirect)
10471 OpInfo.ConstraintType = TargetLowering::C_Memory;
10472
10473 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10474 OpInfo.ConstraintType == TargetLowering::C_Other) {
10475 std::vector<SDValue> Ops;
10476 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10477 Ops, DAG);
10478 if (Ops.empty()) {
10479 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10480 if (isa<ConstantSDNode>(InOperandVal)) {
10481 Info.ErrorMsg << "value out of range for constraint '"
10482 << OpInfo.ConstraintCode << "'";
10483 return true;
10484 }
10485
10486 Info.ErrorMsg << "invalid operand for inline asm constraint '"
10487 << OpInfo.ConstraintCode << "'";
10488 return true;
10489 }
10490
10491 // Add information to the INLINEASM node to know about this input.
10492 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10493 Info.AsmNodeOperands.push_back(DAG.getTargetConstant(
10494 ResOpType, DL, TLI.getPointerTy(DAG.getDataLayout())));
10495 llvm::append_range(Info.AsmNodeOperands, Ops);
10496 break;
10497 }
10498
10499 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10500 assert((OpInfo.isIndirect ||
10501 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10502 "Operand must be indirect to be a mem!");
10503 assert(InOperandVal.getValueType() ==
10504 TLI.getPointerTy(DAG.getDataLayout()) &&
10505 "Memory operands expect pointer values");
10506
10507 const InlineAsm::ConstraintCode ConstraintID =
10508 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10510 "Failed to convert memory constraint code to constraint id.");
10511
10512 // Add information to the INLINEASM node to know about this input.
10514 ResOpType.setMemConstraint(ConstraintID);
10515 Info.AsmNodeOperands.push_back(
10516 DAG.getTargetConstant(ResOpType, DL, MVT::i32));
10517 Info.AsmNodeOperands.push_back(InOperandVal);
10518 break;
10519 }
10520
10521 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10522 const InlineAsm::ConstraintCode ConstraintID =
10523 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10525 "Failed to convert memory constraint code to constraint id.");
10526
10528
10529 SDValue AsmOp = InOperandVal;
10530 if (isFunction(InOperandVal)) {
10531 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10532 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10533 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
10534 InOperandVal.getValueType(),
10535 GA->getOffset());
10536 }
10537
10538 // Add information to the INLINEASM node to know about this input.
10539 ResOpType.setMemConstraint(ConstraintID);
10540
10541 Info.AsmNodeOperands.push_back(
10542 DAG.getTargetConstant(ResOpType, DL, MVT::i32));
10543 Info.AsmNodeOperands.push_back(AsmOp);
10544 break;
10545 }
10546
10547 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10548 OpInfo.ConstraintType != TargetLowering::C_Register) {
10549 Info.ErrorMsg << "unknown asm constraint '" << OpInfo.ConstraintCode
10550 << "'";
10551 return true;
10552 }
10553
10554 // TODO: Support this.
10555 if (OpInfo.isIndirect) {
10556 Info.ErrorMsg << "cannot handle indirect register inputs yet for "
10557 << "constraint '" << OpInfo.ConstraintCode << "'";
10558 return true;
10559 }
10560
10561 // Copy the input into the appropriate registers.
10562 if (OpInfo.AssignedRegs.Regs.empty()) {
10563 Info.ErrorMsg << "could not allocate input reg for constraint '"
10564 << OpInfo.ConstraintCode << "'";
10565 return true;
10566 }
10567
10568 if (DetectWriteToReservedRegister())
10569 return true;
10570
10571 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, DL, Info.Chain,
10572 &Info.Glue, &Call);
10573 OpInfo.AssignedRegs.AddInlineAsmOperands(
10574 InlineAsm::Kind::RegUse, false, 0, DL, DAG, Info.AsmNodeOperands);
10575 break;
10576 }
10577
10579 // Add the clobbered value to the operand list, so that the register
10580 // allocator is aware that the physreg got clobbered.
10581 if (!OpInfo.AssignedRegs.Regs.empty())
10582 OpInfo.AssignedRegs.AddInlineAsmOperands(
10583 InlineAsm::Kind::Clobber, false, 0, DL, DAG, Info.AsmNodeOperands);
10584 break;
10585 }
10586 }
10587
10588 return false;
10589}
10590
10591/// DetermineConstraints - Find the constraints to use for inline asm operands.
10592static bool
10593determineConstraints(ConstraintDecisionInfo &Info,
10594 TargetLowering::AsmOperandInfoVector &TargetConstraints,
10595 const CallBase &Call, SelectionDAGBuilder &Builder,
10596 const TargetLowering &TLI, const TargetMachine &TM,
10597 SelectionDAG &DAG, const BasicBlock *EHPadBB) {
10598 const auto *IA = cast<InlineAsm>(Call.getCalledOperand());
10599 ExtraFlags ExtraInfo(Call);
10600
10601 // First pass: Construct operand info objects.
10602 Info.HasSideEffect = IA->hasSideEffects();
10603 if (constructOperandInfo(Info, TargetConstraints, Builder, TLI, ExtraInfo))
10604 return true;
10605
10606 // We won't need to flush pending loads if this asm doesn't touch
10607 // memory and is nonvolatile.
10608 Info.Chain = Info.HasSideEffect ? Builder.getRoot() : DAG.getRoot();
10609
10610 bool IsCallBr = isa<CallBrInst>(Call);
10611 bool EmitEHLabels = isa<InvokeInst>(Call);
10612 if (IsCallBr || EmitEHLabels)
10613 // If this is a callbr or invoke we need to flush pending exports since
10614 // inlineasm_br and invoke are terminators.
10615 // We need to do this before nodes are glued to the inlineasm_br node.
10616 Info.Chain = Builder.getControlRoot();
10617
10618 if (EmitEHLabels)
10619 Info.Chain = Builder.lowerStartEH(Info.Chain, EHPadBB, Info.BeginLabel);
10620
10621 // Second pass: Compute which constraint option to use.
10622 computeConstraintToUse(Info, Call, TargetConstraints, Builder, TLI, TM, DAG);
10623
10624 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10625 Info.AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10626 Info.AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10627 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10628
10629 // If we have a !srcloc metadata node associated with it, we want to attach
10630 // this to the ultimately generated inline asm machineinstr. To do this, we
10631 // pass in the third operand as this (potentially null) inline asm MDNode.
10632 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10633 Info.AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10634
10635 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10636 // bits as operand 3.
10637 Info.AsmNodeOperands.push_back(
10638 DAG.getTargetConstant(ExtraInfo.get(), Builder.getCurSDLoc(),
10639 TLI.getPointerTy(DAG.getDataLayout())));
10640
10641 // Third pass: Prepare DAG-level operands
10642 return prepareDAGLevelOperands(Info, Call, Builder, TLI, DAG);
10643}
10644
10645/// visitInlineAsm - Handle a call to an InlineAsm object.
10646void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10647 const BasicBlock *EHPadBB) {
10648 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10650 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10651
10652 assert((!isa<InvokeInst>(Call) || EHPadBB) &&
10653 "InvokeInst must have an EHPadBB");
10654
10655 ConstraintDecisionInfo Info;
10656 if (determineConstraints(Info, TargetConstraints, Call, *this, TLI, TM, DAG,
10657 EHPadBB))
10658 return emitInlineAsmError(Call, Info.ErrorMsg.str());
10659
10660 SDValue Glue = Info.Glue;
10661 SDValue Chain = Info.Chain;
10662
10663 // Finish up input operands. Set the input chain and add the flag last.
10664 Info.AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10665 if (Glue.getNode())
10666 Info.AsmNodeOperands.push_back(Glue);
10667
10668 bool IsCallBr = isa<CallBrInst>(Call);
10669 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10670 Chain =
10671 DAG.getNode(ISDOpc, getCurSDLoc(), DAG.getVTList(MVT::Other, MVT::Glue),
10672 Info.AsmNodeOperands);
10673 Glue = Chain.getValue(1);
10674
10675 // Do additional work to generate outputs.
10676
10677 SmallVector<EVT, 1> ResultVTs;
10678 SmallVector<SDValue, 1> ResultValues;
10679 SmallVector<SDValue, 8> OutChains;
10680
10681 llvm::Type *CallResultType = Call.getType();
10682 ArrayRef<Type *> ResultTypes;
10683 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10684 ResultTypes = StructResult->elements();
10685 else if (!CallResultType->isVoidTy())
10686 ResultTypes = ArrayRef(CallResultType);
10687
10688 auto CurResultType = ResultTypes.begin();
10689 auto handleRegAssign = [&](SDValue V) {
10690 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10691 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10692 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10693 ++CurResultType;
10694 // If the type of the inline asm call site return value is different but has
10695 // same size as the type of the asm output bitcast it. One example of this
10696 // is for vectors with different width / number of elements. This can
10697 // happen for register classes that can contain multiple different value
10698 // types. The preg or vreg allocated may not have the same VT as was
10699 // expected.
10700 //
10701 // This can also happen for a return value that disagrees with the register
10702 // class it is put in, eg. a double in a general-purpose register on a
10703 // 32-bit machine.
10704 if (ResultVT != V.getValueType() &&
10705 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10706 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10707 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10708 V.getValueType().isInteger()) {
10709 // If a result value was tied to an input value, the computed result
10710 // may have a wider width than the expected result. Extract the
10711 // relevant portion.
10712 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10713 }
10714 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10715 ResultVTs.push_back(ResultVT);
10716 ResultValues.push_back(V);
10717 };
10718
10719 // Deal with output operands.
10720 for (SDISelAsmOperandInfo &OpInfo : Info.ConstraintOperands) {
10721 if (OpInfo.Type == InlineAsm::isOutput) {
10722 SDValue Val;
10723 // Skip trivial output operands.
10724 if (OpInfo.AssignedRegs.Regs.empty())
10725 continue;
10726
10727 switch (OpInfo.ConstraintType) {
10730 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10731 Chain, &Glue, &Call);
10732 break;
10735 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10736 OpInfo, DAG);
10737 break;
10739 break; // Already handled.
10741 break; // Silence warning.
10743 assert(false && "Unexpected unknown constraint");
10744 }
10745
10746 // Indirect output manifest as stores. Record output chains.
10747 if (OpInfo.isIndirect) {
10748 const Value *Ptr = OpInfo.CallOperandVal;
10749 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10750 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10751 MachinePointerInfo(Ptr));
10752 OutChains.push_back(Store);
10753 } else {
10754 // generate CopyFromRegs to associated registers.
10755 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10756 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10757 for (const SDValue &V : Val->op_values())
10758 handleRegAssign(V);
10759 } else
10760 handleRegAssign(Val);
10761 }
10762 }
10763 }
10764
10765 // Set results.
10766 if (!ResultValues.empty()) {
10767 assert(CurResultType == ResultTypes.end() &&
10768 "Mismatch in number of ResultTypes");
10769 assert(ResultValues.size() == ResultTypes.size() &&
10770 "Mismatch in number of output operands in asm result");
10771
10773 DAG.getVTList(ResultVTs), ResultValues);
10774 setValue(&Call, V);
10775 }
10776
10777 // Collect store chains.
10778 if (!OutChains.empty())
10779 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10780
10781 if (const auto *II = dyn_cast<InvokeInst>(&Call))
10782 Chain = lowerEndEH(Chain, II, EHPadBB, Info.BeginLabel);
10783
10784 // Only Update Root if inline assembly has a memory effect.
10785 if (ResultValues.empty() || Info.HasSideEffect || !OutChains.empty() ||
10786 IsCallBr || isa<InvokeInst>(Call))
10787 DAG.setRoot(Chain);
10788}
10789
10790void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10791 const Twine &Message) {
10792 LLVMContext &Ctx = *DAG.getContext();
10793 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10794
10795 // Make sure we leave the DAG in a valid state
10796 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10797 SmallVector<EVT, 1> ValueVTs;
10798 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10799
10800 if (ValueVTs.empty())
10801 return;
10802
10804 for (const EVT &VT : ValueVTs)
10805 Ops.push_back(DAG.getUNDEF(VT));
10806
10807 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10808}
10809
10810void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10811 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10812 MVT::Other, getRoot(),
10813 getValue(I.getArgOperand(0)),
10814 DAG.getSrcValue(I.getArgOperand(0))));
10815}
10816
10817void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10818 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10819 const DataLayout &DL = DAG.getDataLayout();
10820 SDValue V = DAG.getVAArg(
10821 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10822 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10823 DL.getABITypeAlign(I.getType()).value());
10824 DAG.setRoot(V.getValue(1));
10825
10826 if (I.getType()->isPointerTy())
10827 V = DAG.getPtrExtOrTrunc(
10828 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10829 setValue(&I, V);
10830}
10831
10832void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10833 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10834 MVT::Other, getRoot(),
10835 getValue(I.getArgOperand(0)),
10836 DAG.getSrcValue(I.getArgOperand(0))));
10837}
10838
10839void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10840 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10841 MVT::Other, getRoot(),
10842 getValue(I.getArgOperand(0)),
10843 getValue(I.getArgOperand(1)),
10844 DAG.getSrcValue(I.getArgOperand(0)),
10845 DAG.getSrcValue(I.getArgOperand(1))));
10846}
10847
10849 const Instruction &I,
10850 SDValue Op) {
10851 std::optional<ConstantRange> CR = getRange(I);
10852
10853 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10854 return Op;
10855
10856 APInt Lo = CR->getUnsignedMin();
10857 if (!Lo.isMinValue())
10858 return Op;
10859
10860 APInt Hi = CR->getUnsignedMax();
10861 unsigned Bits = std::max(Hi.getActiveBits(),
10862 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10863
10864 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10865
10866 SDLoc SL = getCurSDLoc();
10867
10868 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10869 DAG.getValueType(SmallVT));
10870 unsigned NumVals = Op.getNode()->getNumValues();
10871 if (NumVals == 1)
10872 return ZExt;
10873
10875
10876 Ops.push_back(ZExt);
10877 for (unsigned I = 1; I != NumVals; ++I)
10878 Ops.push_back(Op.getValue(I));
10879
10880 return DAG.getMergeValues(Ops, SL);
10881}
10882
10884 SelectionDAG &DAG, const Instruction &I, SDValue Op) {
10885 FPClassTest Classes = getNoFPClass(I);
10886 if (Classes == fcNone)
10887 return Op;
10888
10889 SDLoc SL = getCurSDLoc();
10890 SDValue TestConst = DAG.getTargetConstant(Classes, SDLoc(), MVT::i32);
10891
10892 if (Op.getOpcode() != ISD::MERGE_VALUES) {
10893 return DAG.getNode(ISD::AssertNoFPClass, SL, Op.getValueType(), Op,
10894 TestConst);
10895 }
10896
10897 SmallVector<SDValue, 8> Ops(Op.getNumOperands());
10898 for (unsigned I = 0, E = Ops.size(); I != E; ++I) {
10899 SDValue MergeOp = Op.getOperand(I);
10900 Ops[I] = DAG.getNode(ISD::AssertNoFPClass, SL, MergeOp.getValueType(),
10901 MergeOp, TestConst);
10902 }
10903
10904 return DAG.getMergeValues(Ops, SL);
10905}
10906
10907/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10908/// the call being lowered.
10909///
10910/// This is a helper for lowering intrinsics that follow a target calling
10911/// convention or require stack pointer adjustment. Only a subset of the
10912/// intrinsic's operands need to participate in the calling convention.
10915 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10916 AttributeSet RetAttrs, bool IsPatchPoint) {
10918 Args.reserve(NumArgs);
10919
10920 // Populate the argument list.
10921 // Attributes for args start at offset 1, after the return attribute.
10922 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10923 ArgI != ArgE; ++ArgI) {
10924 const Value *V = Call->getOperand(ArgI);
10925
10926 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10927
10928 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10929 Entry.setAttributes(Call, ArgI);
10930 Args.push_back(Entry);
10931 }
10932
10934 .setChain(getRoot())
10935 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10936 RetAttrs)
10937 .setDiscardResult(Call->use_empty())
10938 .setIsPatchPoint(IsPatchPoint)
10940 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10941}
10942
10943/// Add a stack map intrinsic call's live variable operands to a stackmap
10944/// or patchpoint target node's operand list.
10945///
10946/// Constants are converted to TargetConstants purely as an optimization to
10947/// avoid constant materialization and register allocation.
10948///
10949/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10950/// generate addess computation nodes, and so FinalizeISel can convert the
10951/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10952/// address materialization and register allocation, but may also be required
10953/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10954/// alloca in the entry block, then the runtime may assume that the alloca's
10955/// StackMap location can be read immediately after compilation and that the
10956/// location is valid at any point during execution (this is similar to the
10957/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10958/// only available in a register, then the runtime would need to trap when
10959/// execution reaches the StackMap in order to read the alloca's location.
10960static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10962 SelectionDAGBuilder &Builder) {
10963 SelectionDAG &DAG = Builder.DAG;
10964 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10965 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10966
10967 // Things on the stack are pointer-typed, meaning that they are already
10968 // legal and can be emitted directly to target nodes.
10970 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10971 } else {
10972 // Otherwise emit a target independent node to be legalised.
10973 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10974 }
10975 }
10976}
10977
10978/// Lower llvm.experimental.stackmap.
10979void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10980 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10981 // [live variables...])
10982
10983 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10984
10985 SDValue Chain, InGlue, Callee;
10987
10988 SDLoc DL = getCurSDLoc();
10990
10991 // The stackmap intrinsic only records the live variables (the arguments
10992 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10993 // intrinsic, this won't be lowered to a function call. This means we don't
10994 // have to worry about calling conventions and target specific lowering code.
10995 // Instead we perform the call lowering right here.
10996 //
10997 // chain, flag = CALLSEQ_START(chain, 0, 0)
10998 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10999 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
11000 //
11001 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
11002 InGlue = Chain.getValue(1);
11003
11004 // Add the STACKMAP operands, starting with DAG house-keeping.
11005 Ops.push_back(Chain);
11006 Ops.push_back(InGlue);
11007
11008 // Add the <id>, <numShadowBytes> operands.
11009 //
11010 // These do not require legalisation, and can be emitted directly to target
11011 // constant nodes.
11013 assert(ID.getValueType() == MVT::i64);
11014 SDValue IDConst =
11015 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
11016 Ops.push_back(IDConst);
11017
11018 SDValue Shad = getValue(CI.getArgOperand(1));
11019 assert(Shad.getValueType() == MVT::i32);
11020 SDValue ShadConst =
11021 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
11022 Ops.push_back(ShadConst);
11023
11024 // Add the live variables.
11025 addStackMapLiveVars(CI, 2, DL, Ops, *this);
11026
11027 // Create the STACKMAP node.
11028 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11029 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
11030 InGlue = Chain.getValue(1);
11031
11032 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
11033
11034 // Stackmaps don't generate values, so nothing goes into the NodeMap.
11035
11036 // Set the root to the target-lowered call chain.
11037 DAG.setRoot(Chain);
11038
11039 // Inform the Frame Information that we have a stackmap in this function.
11040 FuncInfo.MF->getFrameInfo().setHasStackMap();
11041}
11042
11043/// Lower llvm.experimental.patchpoint directly to its target opcode.
11044void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
11045 const BasicBlock *EHPadBB) {
11046 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
11047 // i32 <numBytes>,
11048 // i8* <target>,
11049 // i32 <numArgs>,
11050 // [Args...],
11051 // [live variables...])
11052
11054 bool IsAnyRegCC = CC == CallingConv::AnyReg;
11055 bool HasDef = !CB.getType()->isVoidTy();
11056 SDLoc dl = getCurSDLoc();
11058
11059 // Handle immediate and symbolic callees.
11060 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
11061 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
11062 /*isTarget=*/true);
11063 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
11064 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
11065 SDLoc(SymbolicCallee),
11066 SymbolicCallee->getValueType(0));
11067
11068 // Get the real number of arguments participating in the call <numArgs>
11070 unsigned NumArgs = NArgVal->getAsZExtVal();
11071
11072 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
11073 // Intrinsics include all meta-operands up to but not including CC.
11074 unsigned NumMetaOpers = PatchPointOpers::CCPos;
11075 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
11076 "Not enough arguments provided to the patchpoint intrinsic");
11077
11078 // For AnyRegCC the arguments are lowered later on manually.
11079 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
11080 Type *ReturnTy =
11081 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
11082
11083 TargetLowering::CallLoweringInfo CLI(DAG);
11084 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
11085 ReturnTy, CB.getAttributes().getRetAttrs(), true);
11086 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
11087
11088 SDNode *CallEnd = Result.second.getNode();
11089 if (CallEnd->getOpcode() == ISD::EH_LABEL)
11090 CallEnd = CallEnd->getOperand(0).getNode();
11091 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
11092 CallEnd = CallEnd->getOperand(0).getNode();
11093
11094 /// Get a call instruction from the call sequence chain.
11095 /// Tail calls are not allowed.
11096 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
11097 "Expected a callseq node.");
11098 SDNode *Call = CallEnd->getOperand(0).getNode();
11099 bool HasGlue = Call->getGluedNode();
11100
11101 // Replace the target specific call node with the patchable intrinsic.
11103
11104 // Push the chain.
11105 Ops.push_back(*(Call->op_begin()));
11106
11107 // Optionally, push the glue (if any).
11108 if (HasGlue)
11109 Ops.push_back(*(Call->op_end() - 1));
11110
11111 // Push the register mask info.
11112 if (HasGlue)
11113 Ops.push_back(*(Call->op_end() - 2));
11114 else
11115 Ops.push_back(*(Call->op_end() - 1));
11116
11117 // Add the <id> and <numBytes> constants.
11119 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
11121 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
11122
11123 // Add the callee.
11124 Ops.push_back(Callee);
11125
11126 // Adjust <numArgs> to account for any arguments that have been passed on the
11127 // stack instead.
11128 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
11129 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
11130 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
11131 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
11132
11133 // Add the calling convention
11134 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
11135
11136 // Add the arguments we omitted previously. The register allocator should
11137 // place these in any free register.
11138 if (IsAnyRegCC)
11139 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
11140 Ops.push_back(getValue(CB.getArgOperand(i)));
11141
11142 // Push the arguments from the call instruction.
11143 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
11144 Ops.append(Call->op_begin() + 2, e);
11145
11146 // Push live variables for the stack map.
11147 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
11148
11149 SDVTList NodeTys;
11150 if (IsAnyRegCC && HasDef) {
11151 // Create the return types based on the intrinsic definition
11152 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11153 SmallVector<EVT, 3> ValueVTs;
11154 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
11155 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
11156
11157 // There is always a chain and a glue type at the end
11158 ValueVTs.push_back(MVT::Other);
11159 ValueVTs.push_back(MVT::Glue);
11160 NodeTys = DAG.getVTList(ValueVTs);
11161 } else
11162 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11163
11164 // Replace the target specific call node with a PATCHPOINT node.
11165 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
11166
11167 // Update the NodeMap.
11168 if (HasDef) {
11169 if (IsAnyRegCC)
11170 setValue(&CB, SDValue(PPV.getNode(), 0));
11171 else
11172 setValue(&CB, Result.first);
11173 }
11174
11175 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
11176 // call sequence. Furthermore the location of the chain and glue can change
11177 // when the AnyReg calling convention is used and the intrinsic returns a
11178 // value.
11179 if (IsAnyRegCC && HasDef) {
11180 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
11181 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
11182 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
11183 } else
11184 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
11185 DAG.DeleteNode(Call);
11186
11187 // Inform the Frame Information that we have a patchpoint in this function.
11188 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
11189}
11190
11191void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
11192 unsigned Intrinsic) {
11193 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11194 SDValue Op1 = getValue(I.getArgOperand(0));
11195 SDValue Op2;
11196 if (I.arg_size() > 1)
11197 Op2 = getValue(I.getArgOperand(1));
11198 SDLoc dl = getCurSDLoc();
11199 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11200 SDValue Res;
11201 SDNodeFlags SDFlags;
11202 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
11203 SDFlags.copyFMF(*FPMO);
11204
11205 switch (Intrinsic) {
11206 case Intrinsic::vector_reduce_fadd:
11207 if (SDFlags.hasAllowReassociation())
11208 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
11209 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
11210 SDFlags);
11211 else
11212 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
11213 break;
11214 case Intrinsic::vector_reduce_fmul:
11215 if (SDFlags.hasAllowReassociation())
11216 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
11217 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
11218 SDFlags);
11219 else
11220 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
11221 break;
11222 case Intrinsic::vector_reduce_add:
11223 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
11224 break;
11225 case Intrinsic::vector_reduce_mul:
11226 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
11227 break;
11228 case Intrinsic::vector_reduce_and:
11229 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
11230 break;
11231 case Intrinsic::vector_reduce_or:
11232 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
11233 break;
11234 case Intrinsic::vector_reduce_xor:
11235 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
11236 break;
11237 case Intrinsic::vector_reduce_smax:
11238 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
11239 break;
11240 case Intrinsic::vector_reduce_smin:
11241 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
11242 break;
11243 case Intrinsic::vector_reduce_umax:
11244 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
11245 break;
11246 case Intrinsic::vector_reduce_umin:
11247 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
11248 break;
11249 case Intrinsic::vector_reduce_fmax:
11250 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
11251 break;
11252 case Intrinsic::vector_reduce_fmin:
11253 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
11254 break;
11255 case Intrinsic::vector_reduce_fmaximum:
11256 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
11257 break;
11258 case Intrinsic::vector_reduce_fminimum:
11259 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11260 break;
11261 default:
11262 llvm_unreachable("Unhandled vector reduce intrinsic");
11263 }
11264 setValue(&I, Res);
11265}
11266
11267/// Returns an AttributeList representing the attributes applied to the return
11268/// value of the given call.
11271 if (CLI.RetSExt)
11272 Attrs.push_back(Attribute::SExt);
11273 if (CLI.RetZExt)
11274 Attrs.push_back(Attribute::ZExt);
11275 if (CLI.IsInReg)
11276 Attrs.push_back(Attribute::InReg);
11277
11278 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11279 Attrs);
11280}
11281
11282/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11283/// implementation, which just calls LowerCall.
11284/// FIXME: When all targets are
11285/// migrated to using LowerCall, this hook should be integrated into SDISel.
11286std::pair<SDValue, SDValue>
11288 LLVMContext &Context = CLI.RetTy->getContext();
11289
11290 // Handle the incoming return values from the call.
11291 CLI.Ins.clear();
11292 SmallVector<Type *, 4> RetOrigTys;
11294 auto &DL = CLI.DAG.getDataLayout();
11295 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11296
11297 SmallVector<EVT, 4> RetVTs;
11298 if (CLI.RetTy != CLI.OrigRetTy) {
11299 assert(RetOrigTys.size() == 1 &&
11300 "Only supported for non-aggregate returns");
11301 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11302 } else {
11303 for (Type *Ty : RetOrigTys)
11304 RetVTs.push_back(getValueType(DL, Ty));
11305 }
11306
11307 if (CLI.IsPostTypeLegalization) {
11308 // If we are lowering a libcall after legalization, split the return type.
11309 SmallVector<Type *, 4> OldRetOrigTys;
11310 SmallVector<EVT, 4> OldRetVTs;
11311 SmallVector<TypeSize, 4> OldOffsets;
11312 RetOrigTys.swap(OldRetOrigTys);
11313 RetVTs.swap(OldRetVTs);
11314 Offsets.swap(OldOffsets);
11315
11316 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11317 EVT RetVT = OldRetVTs[i];
11318 uint64_t Offset = OldOffsets[i];
11319 MVT RegisterVT = getRegisterType(Context, RetVT);
11320 unsigned NumRegs = getNumRegisters(Context, RetVT);
11321 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11322 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11323 RetVTs.append(NumRegs, RegisterVT);
11324 for (unsigned j = 0; j != NumRegs; ++j)
11325 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11326 }
11327 }
11328
11330 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11331
11332 bool CanLowerReturn =
11334 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11335
11336 SDValue DemoteStackSlot;
11337 int DemoteStackIdx = -100;
11338 if (!CanLowerReturn) {
11339 // FIXME: equivalent assert?
11340 // assert(!CS.hasInAllocaArgument() &&
11341 // "sret demotion is incompatible with inalloca");
11342 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11343 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11345 DemoteStackIdx =
11346 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11347 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11348
11349 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11350 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11351 Entry.IsSRet = true;
11352 Entry.Alignment = Alignment;
11353 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11354 CLI.NumFixedArgs += 1;
11355 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11356 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11357
11358 // sret demotion isn't compatible with tail-calls, since the sret argument
11359 // points into the callers stack frame.
11360 CLI.IsTailCall = false;
11361 } else {
11362 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11363 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11364 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11365 ISD::ArgFlagsTy Flags;
11366 if (NeedsRegBlock) {
11367 Flags.setInConsecutiveRegs();
11368 if (I == RetVTs.size() - 1)
11369 Flags.setInConsecutiveRegsLast();
11370 }
11371 EVT VT = RetVTs[I];
11372 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11373 unsigned NumRegs =
11374 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11375 for (unsigned i = 0; i != NumRegs; ++i) {
11376 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11378 if (CLI.RetTy->isPointerTy()) {
11379 Ret.Flags.setPointer();
11381 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11382 }
11383 if (CLI.RetSExt)
11384 Ret.Flags.setSExt();
11385 if (CLI.RetZExt)
11386 Ret.Flags.setZExt();
11387 if (CLI.IsInReg)
11388 Ret.Flags.setInReg();
11389 CLI.Ins.push_back(Ret);
11390 }
11391 }
11392 }
11393
11394 // We push in swifterror return as the last element of CLI.Ins.
11395 ArgListTy &Args = CLI.getArgs();
11396 if (supportSwiftError()) {
11397 for (const ArgListEntry &Arg : Args) {
11398 if (Arg.IsSwiftError) {
11399 ISD::ArgFlagsTy Flags;
11400 Flags.setSwiftError();
11402 PointerType::getUnqual(Context),
11403 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11404 CLI.Ins.push_back(Ret);
11405 }
11406 }
11407 }
11408
11409 // Handle all of the outgoing arguments.
11410 CLI.Outs.clear();
11411 CLI.OutVals.clear();
11412 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11413 SmallVector<Type *, 4> OrigArgTys;
11414 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11415 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11416 Type *FinalType = Args[i].Ty;
11417 if (Args[i].IsByVal)
11418 FinalType = Args[i].IndirectType;
11419 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11420 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11421 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11422 ++Value) {
11423 Type *OrigArgTy = OrigArgTys[Value];
11424 Type *ArgTy = OrigArgTy;
11425 if (Args[i].Ty != Args[i].OrigTy) {
11426 assert(Value == 0 && "Only supported for non-aggregate arguments");
11427 ArgTy = Args[i].Ty;
11428 }
11429
11430 EVT VT = getValueType(DL, ArgTy);
11431 SDValue Op = SDValue(Args[i].Node.getNode(),
11432 Args[i].Node.getResNo() + Value);
11433 ISD::ArgFlagsTy Flags;
11434
11435 // Certain targets (such as MIPS), may have a different ABI alignment
11436 // for a type depending on the context. Give the target a chance to
11437 // specify the alignment it wants.
11438 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11439 Flags.setOrigAlign(OriginalAlignment);
11440
11441 if (i >= CLI.NumFixedArgs)
11442 Flags.setVarArg();
11443 if (ArgTy->isPointerTy()) {
11444 Flags.setPointer();
11445 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11446 }
11447 if (Args[i].IsZExt)
11448 Flags.setZExt();
11449 if (Args[i].IsSExt)
11450 Flags.setSExt();
11451 if (Args[i].IsNoExt)
11452 Flags.setNoExt();
11453 if (Args[i].IsInReg) {
11454 // If we are using vectorcall calling convention, a structure that is
11455 // passed InReg - is surely an HVA
11457 isa<StructType>(FinalType)) {
11458 // The first value of a structure is marked
11459 if (0 == Value)
11460 Flags.setHvaStart();
11461 Flags.setHva();
11462 }
11463 // Set InReg Flag
11464 Flags.setInReg();
11465 }
11466 if (Args[i].IsSRet)
11467 Flags.setSRet();
11468 if (Args[i].IsSwiftSelf)
11469 Flags.setSwiftSelf();
11470 if (Args[i].IsSwiftAsync)
11471 Flags.setSwiftAsync();
11472 if (Args[i].IsSwiftError)
11473 Flags.setSwiftError();
11474 if (Args[i].IsCFGuardTarget)
11475 Flags.setCFGuardTarget();
11476 if (Args[i].IsByVal)
11477 Flags.setByVal();
11478 if (Args[i].IsByRef)
11479 Flags.setByRef();
11480 if (Args[i].IsPreallocated) {
11481 Flags.setPreallocated();
11482 // Set the byval flag for CCAssignFn callbacks that don't know about
11483 // preallocated. This way we can know how many bytes we should've
11484 // allocated and how many bytes a callee cleanup function will pop. If
11485 // we port preallocated to more targets, we'll have to add custom
11486 // preallocated handling in the various CC lowering callbacks.
11487 Flags.setByVal();
11488 }
11489 if (Args[i].IsInAlloca) {
11490 Flags.setInAlloca();
11491 // Set the byval flag for CCAssignFn callbacks that don't know about
11492 // inalloca. This way we can know how many bytes we should've allocated
11493 // and how many bytes a callee cleanup function will pop. If we port
11494 // inalloca to more targets, we'll have to add custom inalloca handling
11495 // in the various CC lowering callbacks.
11496 Flags.setByVal();
11497 }
11498 Align MemAlign;
11499 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11500 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11501 Flags.setByValSize(FrameSize);
11502
11503 // info is not there but there are cases it cannot get right.
11504 if (auto MA = Args[i].Alignment)
11505 MemAlign = *MA;
11506 else
11507 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11508 } else if (auto MA = Args[i].Alignment) {
11509 MemAlign = *MA;
11510 } else {
11511 MemAlign = OriginalAlignment;
11512 }
11513 Flags.setMemAlign(MemAlign);
11514 if (Args[i].IsNest)
11515 Flags.setNest();
11516 if (NeedsRegBlock)
11517 Flags.setInConsecutiveRegs();
11518
11519 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11520 unsigned NumParts =
11521 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11522 SmallVector<SDValue, 4> Parts(NumParts);
11523 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11524
11525 if (Args[i].IsSExt)
11526 ExtendKind = ISD::SIGN_EXTEND;
11527 else if (Args[i].IsZExt)
11528 ExtendKind = ISD::ZERO_EXTEND;
11529
11530 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11531 // for now.
11532 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11534 assert((CLI.RetTy == Args[i].Ty ||
11535 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11537 Args[i].Ty->getPointerAddressSpace())) &&
11538 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11539 // Before passing 'returned' to the target lowering code, ensure that
11540 // either the register MVT and the actual EVT are the same size or that
11541 // the return value and argument are extended in the same way; in these
11542 // cases it's safe to pass the argument register value unchanged as the
11543 // return register value (although it's at the target's option whether
11544 // to do so)
11545 // TODO: allow code generation to take advantage of partially preserved
11546 // registers rather than clobbering the entire register when the
11547 // parameter extension method is not compatible with the return
11548 // extension method
11549 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11550 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11551 CLI.RetZExt == Args[i].IsZExt))
11552 Flags.setReturned();
11553 }
11554
11555 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11556 CLI.CallConv, ExtendKind);
11557
11558 for (unsigned j = 0; j != NumParts; ++j) {
11559 // if it isn't first piece, alignment must be 1
11560 // For scalable vectors the scalable part is currently handled
11561 // by individual targets, so we just use the known minimum size here.
11562 ISD::OutputArg MyFlags(
11563 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11564 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11565 if (NumParts > 1 && j == 0)
11566 MyFlags.Flags.setSplit();
11567 else if (j != 0) {
11568 MyFlags.Flags.setOrigAlign(Align(1));
11569 if (j == NumParts - 1)
11570 MyFlags.Flags.setSplitEnd();
11571 }
11572
11573 CLI.Outs.push_back(MyFlags);
11574 CLI.OutVals.push_back(Parts[j]);
11575 }
11576
11577 if (NeedsRegBlock && Value == NumValues - 1)
11578 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11579 }
11580 }
11581
11583 CLI.Chain = LowerCall(CLI, InVals);
11584
11585 // Update CLI.InVals to use outside of this function.
11586 CLI.InVals = InVals;
11587
11588 // Verify that the target's LowerCall behaved as expected.
11589 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11590 "LowerCall didn't return a valid chain!");
11591 assert((!CLI.IsTailCall || InVals.empty()) &&
11592 "LowerCall emitted a return value for a tail call!");
11593 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11594 "LowerCall didn't emit the correct number of values!");
11595
11596 // For a tail call, the return value is merely live-out and there aren't
11597 // any nodes in the DAG representing it. Return a special value to
11598 // indicate that a tail call has been emitted and no more Instructions
11599 // should be processed in the current block.
11600 if (CLI.IsTailCall) {
11601 CLI.DAG.setRoot(CLI.Chain);
11602 return std::make_pair(SDValue(), SDValue());
11603 }
11604
11605#ifndef NDEBUG
11606 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11607 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11608 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11609 "LowerCall emitted a value with the wrong type!");
11610 }
11611#endif
11612
11613 SmallVector<SDValue, 4> ReturnValues;
11614 if (!CanLowerReturn) {
11615 // The instruction result is the result of loading from the
11616 // hidden sret parameter.
11617 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11618
11619 unsigned NumValues = RetVTs.size();
11620 ReturnValues.resize(NumValues);
11621 SmallVector<SDValue, 4> Chains(NumValues);
11622
11623 // An aggregate return value cannot wrap around the address space, so
11624 // offsets to its parts don't wrap either.
11626 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11627 for (unsigned i = 0; i < NumValues; ++i) {
11629 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11631 SDValue L = CLI.DAG.getLoad(
11632 RetVTs[i], CLI.DL, CLI.Chain, Add,
11634 DemoteStackIdx, Offsets[i]),
11635 HiddenSRetAlign);
11636 ReturnValues[i] = L;
11637 Chains[i] = L.getValue(1);
11638 }
11639
11640 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11641 } else {
11642 // Collect the legal value parts into potentially illegal values
11643 // that correspond to the original function's return values.
11644 std::optional<ISD::NodeType> AssertOp;
11645 if (CLI.RetSExt)
11646 AssertOp = ISD::AssertSext;
11647 else if (CLI.RetZExt)
11648 AssertOp = ISD::AssertZext;
11649 unsigned CurReg = 0;
11650 for (EVT VT : RetVTs) {
11651 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11652 unsigned NumRegs =
11653 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11654
11655 ReturnValues.push_back(getCopyFromParts(
11656 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11657 CLI.Chain, CLI.CallConv, AssertOp));
11658 CurReg += NumRegs;
11659 }
11660
11661 // For a function returning void, there is no return value. We can't create
11662 // such a node, so we just return a null return value in that case. In
11663 // that case, nothing will actually look at the value.
11664 if (ReturnValues.empty())
11665 return std::make_pair(SDValue(), CLI.Chain);
11666 }
11667
11668 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11669 CLI.DAG.getVTList(RetVTs), ReturnValues);
11670 return std::make_pair(Res, CLI.Chain);
11671}
11672
11673/// Places new result values for the node in Results (their number
11674/// and types must exactly match those of the original return values of
11675/// the node), or leaves Results empty, which indicates that the node is not
11676/// to be custom lowered after all.
11679 SelectionDAG &DAG) const {
11680 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11681
11682 if (!Res.getNode())
11683 return;
11684
11685 // If the original node has one result, take the return value from
11686 // LowerOperation as is. It might not be result number 0.
11687 if (N->getNumValues() == 1) {
11688 Results.push_back(Res);
11689 return;
11690 }
11691
11692 // If the original node has multiple results, then the return node should
11693 // have the same number of results.
11694 assert((N->getNumValues() == Res->getNumValues()) &&
11695 "Lowering returned the wrong number of results!");
11696
11697 // Places new result values base on N result number.
11698 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11699 Results.push_back(Res.getValue(I));
11700}
11701
11703 llvm_unreachable("LowerOperation not implemented for this target!");
11704}
11705
11707 Register Reg,
11708 ISD::NodeType ExtendType) {
11710 assert((Op.getOpcode() != ISD::CopyFromReg ||
11711 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11712 "Copy from a reg to the same reg!");
11713 assert(!Reg.isPhysical() && "Is a physreg");
11714
11715 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11716 // If this is an InlineAsm we have to match the registers required, not the
11717 // notional registers required by the type.
11718
11719 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11720 std::nullopt); // This is not an ABI copy.
11721 SDValue Chain = DAG.getEntryNode();
11722
11723 if (ExtendType == ISD::ANY_EXTEND) {
11724 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11725 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11726 ExtendType = PreferredExtendIt->second;
11727 }
11728 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11729 PendingExports.push_back(Chain);
11730}
11731
11733
11734/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11735/// entry block, return true. This includes arguments used by switches, since
11736/// the switch may expand into multiple basic blocks.
11737static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11738 // With FastISel active, we may be splitting blocks, so force creation
11739 // of virtual registers for all non-dead arguments.
11740 if (FastISel)
11741 return A->use_empty();
11742
11743 const BasicBlock &Entry = A->getParent()->front();
11744 for (const User *U : A->users())
11745 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11746 return false; // Use not in entry block.
11747
11748 return true;
11749}
11750
11752 DenseMap<const Argument *,
11753 std::pair<const AllocaInst *, const StoreInst *>>;
11754
11755/// Scan the entry block of the function in FuncInfo for arguments that look
11756/// like copies into a local alloca. Record any copied arguments in
11757/// ArgCopyElisionCandidates.
11758static void
11760 FunctionLoweringInfo *FuncInfo,
11761 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11762 // Record the state of every static alloca used in the entry block. Argument
11763 // allocas are all used in the entry block, so we need approximately as many
11764 // entries as we have arguments.
11765 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11767 unsigned NumArgs = FuncInfo->Fn->arg_size();
11768 StaticAllocas.reserve(NumArgs * 2);
11769
11770 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11771 if (!V)
11772 return nullptr;
11773 V = V->stripPointerCasts();
11774 const auto *AI = dyn_cast<AllocaInst>(V);
11775 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11776 return nullptr;
11777 auto Iter = StaticAllocas.insert({AI, Unknown});
11778 return &Iter.first->second;
11779 };
11780
11781 // Look for stores of arguments to static allocas. Look through bitcasts and
11782 // GEPs to handle type coercions, as long as the alloca is fully initialized
11783 // by the store. Any non-store use of an alloca escapes it and any subsequent
11784 // unanalyzed store might write it.
11785 // FIXME: Handle structs initialized with multiple stores.
11786 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11787 // Look for stores, and handle non-store uses conservatively.
11788 const auto *SI = dyn_cast<StoreInst>(&I);
11789 if (!SI) {
11790 // We will look through cast uses, so ignore them completely.
11791 if (I.isCast())
11792 continue;
11793 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11794 // to allocas.
11795 if (I.isDebugOrPseudoInst())
11796 continue;
11797 // This is an unknown instruction. Assume it escapes or writes to all
11798 // static alloca operands.
11799 for (const Use &U : I.operands()) {
11800 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11801 *Info = StaticAllocaInfo::Clobbered;
11802 }
11803 continue;
11804 }
11805
11806 // If the stored value is a static alloca, mark it as escaped.
11807 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11808 *Info = StaticAllocaInfo::Clobbered;
11809
11810 // Check if the destination is a static alloca.
11811 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11812 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11813 if (!Info)
11814 continue;
11815 const AllocaInst *AI = cast<AllocaInst>(Dst);
11816
11817 // Skip allocas that have been initialized or clobbered.
11818 if (*Info != StaticAllocaInfo::Unknown)
11819 continue;
11820
11821 // Check if the stored value is an argument, and that this store fully
11822 // initializes the alloca.
11823 // If the argument type has padding bits we can't directly forward a pointer
11824 // as the upper bits may contain garbage.
11825 // Don't elide copies from the same argument twice.
11826 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11827 const auto *Arg = dyn_cast<Argument>(Val);
11828 std::optional<TypeSize> AllocaSize = AI->getAllocationSize(DL);
11829 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11830 Arg->getType()->isEmptyTy() || !AllocaSize ||
11831 DL.getTypeStoreSize(Arg->getType()) != *AllocaSize ||
11832 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11833 ArgCopyElisionCandidates.count(Arg)) {
11834 *Info = StaticAllocaInfo::Clobbered;
11835 continue;
11836 }
11837
11838 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11839 << '\n');
11840
11841 // Mark this alloca and store for argument copy elision.
11842 *Info = StaticAllocaInfo::Elidable;
11843 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11844
11845 // Stop scanning if we've seen all arguments. This will happen early in -O0
11846 // builds, which is useful, because -O0 builds have large entry blocks and
11847 // many allocas.
11848 if (ArgCopyElisionCandidates.size() == NumArgs)
11849 break;
11850 }
11851}
11852
11853/// Try to elide argument copies from memory into a local alloca. Succeeds if
11854/// ArgVal is a load from a suitable fixed stack object.
11857 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11858 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11859 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11860 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11861 // Check if this is a load from a fixed stack object.
11862 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11863 if (!LNode)
11864 return;
11865 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11866 if (!FINode)
11867 return;
11868
11869 // Check that the fixed stack object is the right size and alignment.
11870 // Look at the alignment that the user wrote on the alloca instead of looking
11871 // at the stack object.
11872 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11873 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11874 const AllocaInst *AI = ArgCopyIter->second.first;
11875 int FixedIndex = FINode->getIndex();
11876 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11877 int OldIndex = AllocaIndex;
11878 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11879 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11880 LLVM_DEBUG(
11881 dbgs() << " argument copy elision failed due to bad fixed stack "
11882 "object size\n");
11883 return;
11884 }
11885 Align RequiredAlignment = AI->getAlign();
11886 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11887 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11888 "greater than stack argument alignment ("
11889 << DebugStr(RequiredAlignment) << " vs "
11890 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11891 return;
11892 }
11893
11894 // Perform the elision. Delete the old stack object and replace its only use
11895 // in the variable info map. Mark the stack object as mutable and aliased.
11896 LLVM_DEBUG({
11897 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11898 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11899 << '\n';
11900 });
11901 MFI.RemoveStackObject(OldIndex);
11902 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11903 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11904 AllocaIndex = FixedIndex;
11905 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11906 for (SDValue ArgVal : ArgVals)
11907 Chains.push_back(ArgVal.getValue(1));
11908
11909 // Avoid emitting code for the store implementing the copy.
11910 const StoreInst *SI = ArgCopyIter->second.second;
11911 ElidedArgCopyInstrs.insert(SI);
11912
11913 // Check for uses of the argument again so that we can avoid exporting ArgVal
11914 // if it is't used by anything other than the store.
11915 for (const Value *U : Arg.users()) {
11916 if (U != SI) {
11917 ArgHasUses = true;
11918 break;
11919 }
11920 }
11921}
11922
11923void SelectionDAGISel::LowerArguments(const Function &F) {
11924 SelectionDAG &DAG = SDB->DAG;
11925 SDLoc dl = SDB->getCurSDLoc();
11926 const DataLayout &DL = DAG.getDataLayout();
11928
11929 // In Naked functions we aren't going to save any registers.
11930 if (F.hasFnAttribute(Attribute::Naked))
11931 return;
11932
11933 if (!FuncInfo->CanLowerReturn) {
11934 // Put in an sret pointer parameter before all the other parameters.
11935 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11936
11937 ISD::ArgFlagsTy Flags;
11938 Flags.setSRet();
11939 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11940 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11942 Ins.push_back(RetArg);
11943 }
11944
11945 // Look for stores of arguments to static allocas. Mark such arguments with a
11946 // flag to ask the target to give us the memory location of that argument if
11947 // available.
11948 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11950 ArgCopyElisionCandidates);
11951
11952 // Set up the incoming argument description vector.
11953 for (const Argument &Arg : F.args()) {
11954 unsigned ArgNo = Arg.getArgNo();
11956 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11957 bool isArgValueUsed = !Arg.use_empty();
11958 Type *FinalType = Arg.getType();
11959 if (Arg.hasAttribute(Attribute::ByVal))
11960 FinalType = Arg.getParamByValType();
11961 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11962 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11963 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11964 ++Value) {
11965 Type *ArgTy = Types[Value];
11966 EVT VT = TLI->getValueType(DL, ArgTy);
11967 ISD::ArgFlagsTy Flags;
11968
11969 if (ArgTy->isPointerTy()) {
11970 Flags.setPointer();
11971 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11972 }
11973 if (Arg.hasAttribute(Attribute::ZExt))
11974 Flags.setZExt();
11975 if (Arg.hasAttribute(Attribute::SExt))
11976 Flags.setSExt();
11977 if (Arg.hasAttribute(Attribute::InReg)) {
11978 // If we are using vectorcall calling convention, a structure that is
11979 // passed InReg - is surely an HVA
11980 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11981 isa<StructType>(Arg.getType())) {
11982 // The first value of a structure is marked
11983 if (0 == Value)
11984 Flags.setHvaStart();
11985 Flags.setHva();
11986 }
11987 // Set InReg Flag
11988 Flags.setInReg();
11989 }
11990 if (Arg.hasAttribute(Attribute::StructRet))
11991 Flags.setSRet();
11992 if (Arg.hasAttribute(Attribute::SwiftSelf))
11993 Flags.setSwiftSelf();
11994 if (Arg.hasAttribute(Attribute::SwiftAsync))
11995 Flags.setSwiftAsync();
11996 if (Arg.hasAttribute(Attribute::SwiftError))
11997 Flags.setSwiftError();
11998 if (Arg.hasAttribute(Attribute::ByVal))
11999 Flags.setByVal();
12000 if (Arg.hasAttribute(Attribute::ByRef))
12001 Flags.setByRef();
12002 if (Arg.hasAttribute(Attribute::InAlloca)) {
12003 Flags.setInAlloca();
12004 // Set the byval flag for CCAssignFn callbacks that don't know about
12005 // inalloca. This way we can know how many bytes we should've allocated
12006 // and how many bytes a callee cleanup function will pop. If we port
12007 // inalloca to more targets, we'll have to add custom inalloca handling
12008 // in the various CC lowering callbacks.
12009 Flags.setByVal();
12010 }
12011 if (Arg.hasAttribute(Attribute::Preallocated)) {
12012 Flags.setPreallocated();
12013 // Set the byval flag for CCAssignFn callbacks that don't know about
12014 // preallocated. This way we can know how many bytes we should've
12015 // allocated and how many bytes a callee cleanup function will pop. If
12016 // we port preallocated to more targets, we'll have to add custom
12017 // preallocated handling in the various CC lowering callbacks.
12018 Flags.setByVal();
12019 }
12020
12021 // Certain targets (such as MIPS), may have a different ABI alignment
12022 // for a type depending on the context. Give the target a chance to
12023 // specify the alignment it wants.
12024 const Align OriginalAlignment(
12025 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
12026 Flags.setOrigAlign(OriginalAlignment);
12027
12028 Align MemAlign;
12029 Type *ArgMemTy = nullptr;
12030 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
12031 Flags.isByRef()) {
12032 if (!ArgMemTy)
12033 ArgMemTy = Arg.getPointeeInMemoryValueType();
12034
12035 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
12036
12037 // For in-memory arguments, size and alignment should be passed from FE.
12038 // BE will guess if this info is not there but there are cases it cannot
12039 // get right.
12040 if (auto ParamAlign = Arg.getParamStackAlign())
12041 MemAlign = *ParamAlign;
12042 else if ((ParamAlign = Arg.getParamAlign()))
12043 MemAlign = *ParamAlign;
12044 else
12045 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
12046 if (Flags.isByRef())
12047 Flags.setByRefSize(MemSize);
12048 else
12049 Flags.setByValSize(MemSize);
12050 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
12051 MemAlign = *ParamAlign;
12052 } else {
12053 MemAlign = OriginalAlignment;
12054 }
12055 Flags.setMemAlign(MemAlign);
12056
12057 if (Arg.hasAttribute(Attribute::Nest))
12058 Flags.setNest();
12059 if (NeedsRegBlock)
12060 Flags.setInConsecutiveRegs();
12061 if (ArgCopyElisionCandidates.count(&Arg))
12062 Flags.setCopyElisionCandidate();
12063 if (Arg.hasAttribute(Attribute::Returned))
12064 Flags.setReturned();
12065
12066 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
12067 *CurDAG->getContext(), F.getCallingConv(), VT);
12068 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
12069 *CurDAG->getContext(), F.getCallingConv(), VT);
12070 for (unsigned i = 0; i != NumRegs; ++i) {
12071 // For scalable vectors, use the minimum size; individual targets
12072 // are responsible for handling scalable vector arguments and
12073 // return values.
12074 ISD::InputArg MyFlags(
12075 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
12076 i * RegisterVT.getStoreSize().getKnownMinValue());
12077 if (NumRegs > 1 && i == 0)
12078 MyFlags.Flags.setSplit();
12079 // if it isn't first piece, alignment must be 1
12080 else if (i > 0) {
12081 MyFlags.Flags.setOrigAlign(Align(1));
12082 if (i == NumRegs - 1)
12083 MyFlags.Flags.setSplitEnd();
12084 }
12085 Ins.push_back(MyFlags);
12086 }
12087 if (NeedsRegBlock && Value == NumValues - 1)
12088 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
12089 }
12090 }
12091
12092 // Call the target to set up the argument values.
12094 SDValue NewRoot = TLI->LowerFormalArguments(
12095 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
12096
12097 // Verify that the target's LowerFormalArguments behaved as expected.
12098 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
12099 "LowerFormalArguments didn't return a valid chain!");
12100 assert(InVals.size() == Ins.size() &&
12101 "LowerFormalArguments didn't emit the correct number of values!");
12102 LLVM_DEBUG({
12103 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
12104 assert(InVals[i].getNode() &&
12105 "LowerFormalArguments emitted a null value!");
12106 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
12107 "LowerFormalArguments emitted a value with the wrong type!");
12108 }
12109 });
12110
12111 // Update the DAG with the new chain value resulting from argument lowering.
12112 DAG.setRoot(NewRoot);
12113
12114 // Set up the argument values.
12115 unsigned i = 0;
12116 if (!FuncInfo->CanLowerReturn) {
12117 // Create a virtual register for the sret pointer, and put in a copy
12118 // from the sret argument into it.
12119 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
12120 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
12121 std::optional<ISD::NodeType> AssertOp;
12122 SDValue ArgValue =
12123 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
12124 F.getCallingConv(), AssertOp);
12125
12126 MachineFunction& MF = SDB->DAG.getMachineFunction();
12127 MachineRegisterInfo& RegInfo = MF.getRegInfo();
12128 Register SRetReg =
12129 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
12130 FuncInfo->DemoteRegister = SRetReg;
12131 NewRoot =
12132 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
12133 DAG.setRoot(NewRoot);
12134
12135 // i indexes lowered arguments. Bump it past the hidden sret argument.
12136 ++i;
12137 }
12138
12140 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
12141 for (const Argument &Arg : F.args()) {
12142 SmallVector<SDValue, 4> ArgValues;
12143 SmallVector<EVT, 4> ValueVTs;
12144 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
12145 unsigned NumValues = ValueVTs.size();
12146 if (NumValues == 0)
12147 continue;
12148
12149 bool ArgHasUses = !Arg.use_empty();
12150
12151 // Elide the copying store if the target loaded this argument from a
12152 // suitable fixed stack object.
12153 if (Ins[i].Flags.isCopyElisionCandidate()) {
12154 unsigned NumParts = 0;
12155 for (EVT VT : ValueVTs)
12156 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
12157 F.getCallingConv(), VT);
12158
12159 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
12160 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
12161 ArrayRef(&InVals[i], NumParts), ArgHasUses);
12162 }
12163
12164 // If this argument is unused then remember its value. It is used to generate
12165 // debugging information.
12166 bool isSwiftErrorArg =
12167 TLI->supportSwiftError() &&
12168 Arg.hasAttribute(Attribute::SwiftError);
12169 if (!ArgHasUses && !isSwiftErrorArg) {
12170 SDB->setUnusedArgValue(&Arg, InVals[i]);
12171
12172 // Also remember any frame index for use in FastISel.
12173 if (FrameIndexSDNode *FI =
12175 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12176 }
12177
12178 for (unsigned Val = 0; Val != NumValues; ++Val) {
12179 EVT VT = ValueVTs[Val];
12180 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
12181 F.getCallingConv(), VT);
12182 unsigned NumParts = TLI->getNumRegistersForCallingConv(
12183 *CurDAG->getContext(), F.getCallingConv(), VT);
12184
12185 // Even an apparent 'unused' swifterror argument needs to be returned. So
12186 // we do generate a copy for it that can be used on return from the
12187 // function.
12188 if (ArgHasUses || isSwiftErrorArg) {
12189 std::optional<ISD::NodeType> AssertOp;
12190 if (Arg.hasAttribute(Attribute::SExt))
12191 AssertOp = ISD::AssertSext;
12192 else if (Arg.hasAttribute(Attribute::ZExt))
12193 AssertOp = ISD::AssertZext;
12194
12195 SDValue OutVal =
12196 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
12197 NewRoot, F.getCallingConv(), AssertOp);
12198
12199 FPClassTest NoFPClass = Arg.getNoFPClass();
12200 if (NoFPClass != fcNone) {
12201 SDValue SDNoFPClass = DAG.getTargetConstant(
12202 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
12203 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
12204 OutVal, SDNoFPClass);
12205 }
12206 ArgValues.push_back(OutVal);
12207 }
12208
12209 i += NumParts;
12210 }
12211
12212 // We don't need to do anything else for unused arguments.
12213 if (ArgValues.empty())
12214 continue;
12215
12216 // Note down frame index.
12217 if (FrameIndexSDNode *FI =
12218 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
12219 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12220
12221 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
12222 SDB->getCurSDLoc());
12223
12224 SDB->setValue(&Arg, Res);
12225 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
12226 // We want to associate the argument with the frame index, among
12227 // involved operands, that correspond to the lowest address. The
12228 // getCopyFromParts function, called earlier, is swapping the order of
12229 // the operands to BUILD_PAIR depending on endianness. The result of
12230 // that swapping is that the least significant bits of the argument will
12231 // be in the first operand of the BUILD_PAIR node, and the most
12232 // significant bits will be in the second operand.
12233 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
12234 if (LoadSDNode *LNode =
12235 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
12236 if (FrameIndexSDNode *FI =
12237 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
12238 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12239 }
12240
12241 // Analyses past this point are naive and don't expect an assertion.
12242 if (Res.getOpcode() == ISD::AssertZext)
12243 Res = Res.getOperand(0);
12244
12245 // Update the SwiftErrorVRegDefMap.
12246 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
12247 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12248 if (Reg.isVirtual())
12249 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
12250 Reg);
12251 }
12252
12253 // If this argument is live outside of the entry block, insert a copy from
12254 // wherever we got it to the vreg that other BB's will reference it as.
12255 if (Res.getOpcode() == ISD::CopyFromReg) {
12256 // If we can, though, try to skip creating an unnecessary vreg.
12257 // FIXME: This isn't very clean... it would be nice to make this more
12258 // general.
12259 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12260 if (Reg.isVirtual()) {
12261 FuncInfo->ValueMap[&Arg] = Reg;
12262 continue;
12263 }
12264 }
12265 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12266 FuncInfo->InitializeRegForValue(&Arg);
12267 SDB->CopyToExportRegsIfNeeded(&Arg);
12268 }
12269 }
12270
12271 if (!Chains.empty()) {
12272 Chains.push_back(NewRoot);
12273 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12274 }
12275
12276 DAG.setRoot(NewRoot);
12277
12278 assert(i == InVals.size() && "Argument register count mismatch!");
12279
12280 // If any argument copy elisions occurred and we have debug info, update the
12281 // stale frame indices used in the dbg.declare variable info table.
12282 if (!ArgCopyElisionFrameIndexMap.empty()) {
12283 for (MachineFunction::VariableDbgInfo &VI :
12284 MF->getInStackSlotVariableDbgInfo()) {
12285 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12286 if (I != ArgCopyElisionFrameIndexMap.end())
12287 VI.updateStackSlot(I->second);
12288 }
12289 }
12290
12291 // Finally, if the target has anything special to do, allow it to do so.
12293}
12294
12295/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12296/// ensure constants are generated when needed. Remember the virtual registers
12297/// that need to be added to the Machine PHI nodes as input. We cannot just
12298/// directly add them, because expansion might result in multiple MBB's for one
12299/// BB. As such, the start of the BB might correspond to a different MBB than
12300/// the end.
12301void
12302SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12303 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12304
12305 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12306
12307 // Check PHI nodes in successors that expect a value to be available from this
12308 // block.
12309 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12310 if (!isa<PHINode>(SuccBB->begin())) continue;
12311 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12312
12313 // If this terminator has multiple identical successors (common for
12314 // switches), only handle each succ once.
12315 if (!SuccsHandled.insert(SuccMBB).second)
12316 continue;
12317
12319
12320 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12321 // nodes and Machine PHI nodes, but the incoming operands have not been
12322 // emitted yet.
12323 for (const PHINode &PN : SuccBB->phis()) {
12324 // Ignore dead phi's.
12325 if (PN.use_empty())
12326 continue;
12327
12328 // Skip empty types
12329 if (PN.getType()->isEmptyTy())
12330 continue;
12331
12332 Register Reg;
12333 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12334
12335 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12336 Register &RegOut = ConstantsOut[C];
12337 if (!RegOut) {
12338 RegOut = FuncInfo.CreateRegs(&PN);
12339 // We need to zero/sign extend ConstantInt phi operands to match
12340 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12341 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12342 if (auto *CI = dyn_cast<ConstantInt>(C))
12343 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12345 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12346 }
12347 Reg = RegOut;
12348 } else {
12350 FuncInfo.ValueMap.find(PHIOp);
12351 if (I != FuncInfo.ValueMap.end())
12352 Reg = I->second;
12353 else {
12354 assert(isa<AllocaInst>(PHIOp) &&
12355 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12356 "Didn't codegen value into a register!??");
12357 Reg = FuncInfo.CreateRegs(&PN);
12359 }
12360 }
12361
12362 // Remember that this register needs to added to the machine PHI node as
12363 // the input for this MBB.
12364 SmallVector<EVT, 4> ValueVTs;
12365 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12366 for (EVT VT : ValueVTs) {
12367 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12368 for (unsigned i = 0; i != NumRegisters; ++i)
12369 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12370 Reg += NumRegisters;
12371 }
12372 }
12373 }
12374
12375 ConstantsOut.clear();
12376}
12377
12378MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12380 if (++I == FuncInfo.MF->end())
12381 return nullptr;
12382 return &*I;
12383}
12384
12385/// During lowering new call nodes can be created (such as memset, etc.).
12386/// Those will become new roots of the current DAG, but complications arise
12387/// when they are tail calls. In such cases, the call lowering will update
12388/// the root, but the builder still needs to know that a tail call has been
12389/// lowered in order to avoid generating an additional return.
12390void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12391 // If the node is null, we do have a tail call.
12392 if (MaybeTC.getNode() != nullptr)
12393 DAG.setRoot(MaybeTC);
12394 else
12395 HasTailCall = true;
12396}
12397
12398void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12399 MachineBasicBlock *SwitchMBB,
12400 MachineBasicBlock *DefaultMBB) {
12401 MachineFunction *CurMF = FuncInfo.MF;
12402 MachineBasicBlock *NextMBB = nullptr;
12404 if (++BBI != FuncInfo.MF->end())
12405 NextMBB = &*BBI;
12406
12407 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12408
12409 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12410
12411 if (Size == 2 && W.MBB == SwitchMBB) {
12412 // If any two of the cases has the same destination, and if one value
12413 // is the same as the other, but has one bit unset that the other has set,
12414 // use bit manipulation to do two compares at once. For example:
12415 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12416 // TODO: This could be extended to merge any 2 cases in switches with 3
12417 // cases.
12418 // TODO: Handle cases where W.CaseBB != SwitchBB.
12419 CaseCluster &Small = *W.FirstCluster;
12420 CaseCluster &Big = *W.LastCluster;
12421
12422 if (Small.Low == Small.High && Big.Low == Big.High &&
12423 Small.MBB == Big.MBB) {
12424 const APInt &SmallValue = Small.Low->getValue();
12425 const APInt &BigValue = Big.Low->getValue();
12426
12427 // Check that there is only one bit different.
12428 APInt CommonBit = BigValue ^ SmallValue;
12429 if (CommonBit.isPowerOf2()) {
12430 SDValue CondLHS = getValue(Cond);
12431 EVT VT = CondLHS.getValueType();
12432 SDLoc DL = getCurSDLoc();
12433
12434 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12435 DAG.getConstant(CommonBit, DL, VT));
12436 SDValue Cond = DAG.getSetCC(
12437 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12438 ISD::SETEQ);
12439
12440 // Update successor info.
12441 // Both Small and Big will jump to Small.BB, so we sum up the
12442 // probabilities.
12443 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12444 if (BPI)
12445 addSuccessorWithProb(
12446 SwitchMBB, DefaultMBB,
12447 // The default destination is the first successor in IR.
12448 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12449 else
12450 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12451
12452 // Insert the true branch.
12453 SDValue BrCond =
12454 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12455 DAG.getBasicBlock(Small.MBB));
12456 // Insert the false branch.
12457 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12458 DAG.getBasicBlock(DefaultMBB));
12459
12460 DAG.setRoot(BrCond);
12461 return;
12462 }
12463 }
12464 }
12465
12466 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12467 // Here, we order cases by probability so the most likely case will be
12468 // checked first. However, two clusters can have the same probability in
12469 // which case their relative ordering is non-deterministic. So we use Low
12470 // as a tie-breaker as clusters are guaranteed to never overlap.
12471 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12472 [](const CaseCluster &a, const CaseCluster &b) {
12473 return a.Prob != b.Prob ?
12474 a.Prob > b.Prob :
12475 a.Low->getValue().slt(b.Low->getValue());
12476 });
12477
12478 // Rearrange the case blocks so that the last one falls through if possible
12479 // without changing the order of probabilities.
12480 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12481 --I;
12482 if (I->Prob > W.LastCluster->Prob)
12483 break;
12484 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12485 std::swap(*I, *W.LastCluster);
12486 break;
12487 }
12488 }
12489 }
12490
12491 // Compute total probability.
12492 BranchProbability DefaultProb = W.DefaultProb;
12493 BranchProbability UnhandledProbs = DefaultProb;
12494 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12495 UnhandledProbs += I->Prob;
12496
12497 MachineBasicBlock *CurMBB = W.MBB;
12498 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12499 bool FallthroughUnreachable = false;
12500 MachineBasicBlock *Fallthrough;
12501 if (I == W.LastCluster) {
12502 // For the last cluster, fall through to the default destination.
12503 Fallthrough = DefaultMBB;
12504 FallthroughUnreachable = isa<UnreachableInst>(
12505 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12506 } else {
12507 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12508 CurMF->insert(BBI, Fallthrough);
12509 // Put Cond in a virtual register to make it available from the new blocks.
12511 }
12512 UnhandledProbs -= I->Prob;
12513
12514 switch (I->Kind) {
12515 case CC_JumpTable: {
12516 // FIXME: Optimize away range check based on pivot comparisons.
12517 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12518 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12519
12520 // The jump block hasn't been inserted yet; insert it here.
12521 MachineBasicBlock *JumpMBB = JT->MBB;
12522 CurMF->insert(BBI, JumpMBB);
12523
12524 auto JumpProb = I->Prob;
12525 auto FallthroughProb = UnhandledProbs;
12526
12527 // If the default statement is a target of the jump table, we evenly
12528 // distribute the default probability to successors of CurMBB. Also
12529 // update the probability on the edge from JumpMBB to Fallthrough.
12530 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12531 SE = JumpMBB->succ_end();
12532 SI != SE; ++SI) {
12533 if (*SI == DefaultMBB) {
12534 JumpProb += DefaultProb / 2;
12535 FallthroughProb -= DefaultProb / 2;
12536 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12537 JumpMBB->normalizeSuccProbs();
12538 break;
12539 }
12540 }
12541
12542 // If the default clause is unreachable, propagate that knowledge into
12543 // JTH->FallthroughUnreachable which will use it to suppress the range
12544 // check.
12545 //
12546 // However, don't do this if we're doing branch target enforcement,
12547 // because a table branch _without_ a range check can be a tempting JOP
12548 // gadget - out-of-bounds inputs that are impossible in correct
12549 // execution become possible again if an attacker can influence the
12550 // control flow. So if an attacker doesn't already have a BTI bypass
12551 // available, we don't want them to be able to get one out of this
12552 // table branch.
12553 if (FallthroughUnreachable) {
12554 Function &CurFunc = CurMF->getFunction();
12555 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12556 JTH->FallthroughUnreachable = true;
12557 }
12558
12559 if (!JTH->FallthroughUnreachable)
12560 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12561 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12562 CurMBB->normalizeSuccProbs();
12563
12564 // The jump table header will be inserted in our current block, do the
12565 // range check, and fall through to our fallthrough block.
12566 JTH->HeaderBB = CurMBB;
12567 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12568
12569 // If we're in the right place, emit the jump table header right now.
12570 if (CurMBB == SwitchMBB) {
12571 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12572 JTH->Emitted = true;
12573 }
12574 break;
12575 }
12576 case CC_BitTests: {
12577 // FIXME: Optimize away range check based on pivot comparisons.
12578 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12579
12580 // The bit test blocks haven't been inserted yet; insert them here.
12581 for (BitTestCase &BTC : BTB->Cases)
12582 CurMF->insert(BBI, BTC.ThisBB);
12583
12584 // Fill in fields of the BitTestBlock.
12585 BTB->Parent = CurMBB;
12586 BTB->Default = Fallthrough;
12587
12588 BTB->DefaultProb = UnhandledProbs;
12589 // If the cases in bit test don't form a contiguous range, we evenly
12590 // distribute the probability on the edge to Fallthrough to two
12591 // successors of CurMBB.
12592 if (!BTB->ContiguousRange) {
12593 BTB->Prob += DefaultProb / 2;
12594 BTB->DefaultProb -= DefaultProb / 2;
12595 }
12596
12597 if (FallthroughUnreachable)
12598 BTB->FallthroughUnreachable = true;
12599
12600 // If we're in the right place, emit the bit test header right now.
12601 if (CurMBB == SwitchMBB) {
12602 visitBitTestHeader(*BTB, SwitchMBB);
12603 BTB->Emitted = true;
12604 }
12605 break;
12606 }
12607 case CC_Range: {
12608 const Value *RHS, *LHS, *MHS;
12609 ISD::CondCode CC;
12610 if (I->Low == I->High) {
12611 // Check Cond == I->Low.
12612 CC = ISD::SETEQ;
12613 LHS = Cond;
12614 RHS=I->Low;
12615 MHS = nullptr;
12616 } else {
12617 // Check I->Low <= Cond <= I->High.
12618 CC = ISD::SETLE;
12619 LHS = I->Low;
12620 MHS = Cond;
12621 RHS = I->High;
12622 }
12623
12624 // If Fallthrough is unreachable, fold away the comparison.
12625 if (FallthroughUnreachable)
12626 CC = ISD::SETTRUE;
12627
12628 // The false probability is the sum of all unhandled cases.
12629 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12630 getCurSDLoc(), I->Prob, UnhandledProbs);
12631
12632 if (CurMBB == SwitchMBB)
12633 visitSwitchCase(CB, SwitchMBB);
12634 else
12635 SL->SwitchCases.push_back(CB);
12636
12637 break;
12638 }
12639 }
12640 CurMBB = Fallthrough;
12641 }
12642}
12643
12644void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12645 const SwitchWorkListItem &W,
12646 Value *Cond,
12647 MachineBasicBlock *SwitchMBB) {
12648 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12649 "Clusters not sorted?");
12650 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12651
12652 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12653 SL->computeSplitWorkItemInfo(W);
12654
12655 // Use the first element on the right as pivot since we will make less-than
12656 // comparisons against it.
12657 CaseClusterIt PivotCluster = FirstRight;
12658 assert(PivotCluster > W.FirstCluster);
12659 assert(PivotCluster <= W.LastCluster);
12660
12661 CaseClusterIt FirstLeft = W.FirstCluster;
12662 CaseClusterIt LastRight = W.LastCluster;
12663
12664 const ConstantInt *Pivot = PivotCluster->Low;
12665
12666 // New blocks will be inserted immediately after the current one.
12668 ++BBI;
12669
12670 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12671 // we can branch to its destination directly if it's squeezed exactly in
12672 // between the known lower bound and Pivot - 1.
12673 MachineBasicBlock *LeftMBB;
12674 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12675 FirstLeft->Low == W.GE &&
12676 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12677 LeftMBB = FirstLeft->MBB;
12678 } else {
12679 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12680 FuncInfo.MF->insert(BBI, LeftMBB);
12681 WorkList.push_back(
12682 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12683 // Put Cond in a virtual register to make it available from the new blocks.
12685 }
12686
12687 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12688 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12689 // directly if RHS.High equals the current upper bound.
12690 MachineBasicBlock *RightMBB;
12691 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12692 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12693 RightMBB = FirstRight->MBB;
12694 } else {
12695 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12696 FuncInfo.MF->insert(BBI, RightMBB);
12697 WorkList.push_back(
12698 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12699 // Put Cond in a virtual register to make it available from the new blocks.
12701 }
12702
12703 // Create the CaseBlock record that will be used to lower the branch.
12704 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12705 getCurSDLoc(), LeftProb, RightProb);
12706
12707 if (W.MBB == SwitchMBB)
12708 visitSwitchCase(CB, SwitchMBB);
12709 else
12710 SL->SwitchCases.push_back(CB);
12711}
12712
12713// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12714// from the swith statement.
12716 BranchProbability PeeledCaseProb) {
12717 if (PeeledCaseProb == BranchProbability::getOne())
12719 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12720
12721 uint32_t Numerator = CaseProb.getNumerator();
12722 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12723 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12724}
12725
12726// Try to peel the top probability case if it exceeds the threshold.
12727// Return current MachineBasicBlock for the switch statement if the peeling
12728// does not occur.
12729// If the peeling is performed, return the newly created MachineBasicBlock
12730// for the peeled switch statement. Also update Clusters to remove the peeled
12731// case. PeeledCaseProb is the BranchProbability for the peeled case.
12732MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12733 const SwitchInst &SI, CaseClusterVector &Clusters,
12734 BranchProbability &PeeledCaseProb) {
12735 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12736 // Don't perform if there is only one cluster or optimizing for size.
12737 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12738 TM.getOptLevel() == CodeGenOptLevel::None ||
12739 SwitchMBB->getParent()->getFunction().hasMinSize())
12740 return SwitchMBB;
12741
12742 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12743 unsigned PeeledCaseIndex = 0;
12744 bool SwitchPeeled = false;
12745 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12746 CaseCluster &CC = Clusters[Index];
12747 if (CC.Prob < TopCaseProb)
12748 continue;
12749 TopCaseProb = CC.Prob;
12750 PeeledCaseIndex = Index;
12751 SwitchPeeled = true;
12752 }
12753 if (!SwitchPeeled)
12754 return SwitchMBB;
12755
12756 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12757 << TopCaseProb << "\n");
12758
12759 // Record the MBB for the peeled switch statement.
12760 MachineFunction::iterator BBI(SwitchMBB);
12761 ++BBI;
12762 MachineBasicBlock *PeeledSwitchMBB =
12763 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12764 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12765
12766 ExportFromCurrentBlock(SI.getCondition());
12767 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12768 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12769 nullptr, nullptr, TopCaseProb.getCompl()};
12770 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12771
12772 Clusters.erase(PeeledCaseIt);
12773 for (CaseCluster &CC : Clusters) {
12774 LLVM_DEBUG(
12775 dbgs() << "Scale the probablity for one cluster, before scaling: "
12776 << CC.Prob << "\n");
12777 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12778 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12779 }
12780 PeeledCaseProb = TopCaseProb;
12781 return PeeledSwitchMBB;
12782}
12783
12784void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12785 // Extract cases from the switch.
12786 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12787 CaseClusterVector Clusters;
12788 Clusters.reserve(SI.getNumCases());
12789 for (auto I : SI.cases()) {
12790 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12791 const ConstantInt *CaseVal = I.getCaseValue();
12792 BranchProbability Prob =
12793 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12794 : BranchProbability(1, SI.getNumCases() + 1);
12795 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12796 }
12797
12798 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12799
12800 // Cluster adjacent cases with the same destination. We do this at all
12801 // optimization levels because it's cheap to do and will make codegen faster
12802 // if there are many clusters.
12803 sortAndRangeify(Clusters);
12804
12805 // The branch probablity of the peeled case.
12806 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12807 MachineBasicBlock *PeeledSwitchMBB =
12808 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12809
12810 // If there is only the default destination, jump there directly.
12811 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12812 if (Clusters.empty()) {
12813 assert(PeeledSwitchMBB == SwitchMBB);
12814 SwitchMBB->addSuccessor(DefaultMBB);
12815 if (DefaultMBB != NextBlock(SwitchMBB)) {
12816 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12817 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12818 }
12819 return;
12820 }
12821
12822 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12823 DAG.getBFI());
12824 SL->findBitTestClusters(Clusters, &SI);
12825
12826 LLVM_DEBUG({
12827 dbgs() << "Case clusters: ";
12828 for (const CaseCluster &C : Clusters) {
12829 if (C.Kind == CC_JumpTable)
12830 dbgs() << "JT:";
12831 if (C.Kind == CC_BitTests)
12832 dbgs() << "BT:";
12833
12834 C.Low->getValue().print(dbgs(), true);
12835 if (C.Low != C.High) {
12836 dbgs() << '-';
12837 C.High->getValue().print(dbgs(), true);
12838 }
12839 dbgs() << ' ';
12840 }
12841 dbgs() << '\n';
12842 });
12843
12844 assert(!Clusters.empty());
12845 SwitchWorkList WorkList;
12846 CaseClusterIt First = Clusters.begin();
12847 CaseClusterIt Last = Clusters.end() - 1;
12848 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12849 // Scale the branchprobability for DefaultMBB if the peel occurs and
12850 // DefaultMBB is not replaced.
12851 if (PeeledCaseProb != BranchProbability::getZero() &&
12852 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12853 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12854 WorkList.push_back(
12855 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12856
12857 while (!WorkList.empty()) {
12858 SwitchWorkListItem W = WorkList.pop_back_val();
12859 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12860
12861 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12862 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12863 // For optimized builds, lower large range as a balanced binary tree.
12864 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12865 continue;
12866 }
12867
12868 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12869 }
12870}
12871
12872void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12873 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12874 auto DL = getCurSDLoc();
12875 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12876 setValue(&I, DAG.getStepVector(DL, ResultVT));
12877}
12878
12879void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12880 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12881 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12882
12883 SDLoc DL = getCurSDLoc();
12884 SDValue V = getValue(I.getOperand(0));
12885 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12886
12887 if (VT.isScalableVector()) {
12888 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12889 return;
12890 }
12891
12892 // Use VECTOR_SHUFFLE for the fixed-length vector
12893 // to maintain existing behavior.
12894 SmallVector<int, 8> Mask;
12895 unsigned NumElts = VT.getVectorMinNumElements();
12896 for (unsigned i = 0; i != NumElts; ++i)
12897 Mask.push_back(NumElts - 1 - i);
12898
12899 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12900}
12901
12902void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12903 unsigned Factor) {
12904 auto DL = getCurSDLoc();
12905 SDValue InVec = getValue(I.getOperand(0));
12906
12907 SmallVector<EVT, 4> ValueVTs;
12908 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12909 ValueVTs);
12910
12911 EVT OutVT = ValueVTs[0];
12912 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12913
12914 SmallVector<SDValue, 4> SubVecs(Factor);
12915 for (unsigned i = 0; i != Factor; ++i) {
12916 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12917 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12918 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12919 }
12920
12921 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12922 // from existing legalisation and combines.
12923 if (OutVT.isFixedLengthVector() && Factor == 2) {
12924 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12925 createStrideMask(0, 2, OutNumElts));
12926 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12927 createStrideMask(1, 2, OutNumElts));
12928 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12929 setValue(&I, Res);
12930 return;
12931 }
12932
12933 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12934 DAG.getVTList(ValueVTs), SubVecs);
12935 setValue(&I, Res);
12936}
12937
12938void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12939 unsigned Factor) {
12940 auto DL = getCurSDLoc();
12941 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12942 EVT InVT = getValue(I.getOperand(0)).getValueType();
12943 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12944
12945 SmallVector<SDValue, 8> InVecs(Factor);
12946 for (unsigned i = 0; i < Factor; ++i) {
12947 InVecs[i] = getValue(I.getOperand(i));
12948 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12949 "Expected VTs to be the same");
12950 }
12951
12952 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12953 // from existing legalisation and combines.
12954 if (OutVT.isFixedLengthVector() && Factor == 2) {
12955 unsigned NumElts = InVT.getVectorMinNumElements();
12956 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12957 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12958 createInterleaveMask(NumElts, 2)));
12959 return;
12960 }
12961
12962 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12963 SDValue Res =
12964 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12965
12967 for (unsigned i = 0; i < Factor; ++i)
12968 Results[i] = Res.getValue(i);
12969
12970 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12971 setValue(&I, Res);
12972}
12973
12974void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12975 SmallVector<EVT, 4> ValueVTs;
12976 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12977 ValueVTs);
12978 unsigned NumValues = ValueVTs.size();
12979 if (NumValues == 0) return;
12980
12981 SmallVector<SDValue, 4> Values(NumValues);
12982 SDValue Op = getValue(I.getOperand(0));
12983
12984 for (unsigned i = 0; i != NumValues; ++i)
12985 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12986 SDValue(Op.getNode(), Op.getResNo() + i));
12987
12989 DAG.getVTList(ValueVTs), Values));
12990}
12991
12992void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12993 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12994 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12995
12996 SDLoc DL = getCurSDLoc();
12997 SDValue V1 = getValue(I.getOperand(0));
12998 SDValue V2 = getValue(I.getOperand(1));
12999 const bool IsLeft = I.getIntrinsicID() == Intrinsic::vector_splice_left;
13000
13001 // VECTOR_SHUFFLE doesn't support a scalable or non-constant mask.
13002 if (VT.isScalableVector() || !isa<ConstantInt>(I.getOperand(2))) {
13003 SDValue Offset = DAG.getZExtOrTrunc(
13004 getValue(I.getOperand(2)), DL, TLI.getVectorIdxTy(DAG.getDataLayout()));
13005 setValue(&I, DAG.getNode(IsLeft ? ISD::VECTOR_SPLICE_LEFT
13007 DL, VT, V1, V2, Offset));
13008 return;
13009 }
13010 uint64_t Imm = cast<ConstantInt>(I.getOperand(2))->getZExtValue();
13011
13012 unsigned NumElts = VT.getVectorNumElements();
13013
13014 uint64_t Idx = IsLeft ? Imm : NumElts - Imm;
13015
13016 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
13017 SmallVector<int, 8> Mask;
13018 for (unsigned i = 0; i < NumElts; ++i)
13019 Mask.push_back(Idx + i);
13020 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
13021}
13022
13023// Consider the following MIR after SelectionDAG, which produces output in
13024// phyregs in the first case or virtregs in the second case.
13025//
13026// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
13027// %5:gr32 = COPY $ebx
13028// %6:gr32 = COPY $edx
13029// %1:gr32 = COPY %6:gr32
13030// %0:gr32 = COPY %5:gr32
13031//
13032// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
13033// %1:gr32 = COPY %6:gr32
13034// %0:gr32 = COPY %5:gr32
13035//
13036// Given %0, we'd like to return $ebx in the first case and %5 in the second.
13037// Given %1, we'd like to return $edx in the first case and %6 in the second.
13038//
13039// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
13040// to a single virtreg (such as %0). The remaining outputs monotonically
13041// increase in virtreg number from there. If a callbr has no outputs, then it
13042// should not have a corresponding callbr landingpad; in fact, the callbr
13043// landingpad would not even be able to refer to such a callbr.
13046 // There is definitely at least one copy.
13047 assert(MI->getOpcode() == TargetOpcode::COPY &&
13048 "start of copy chain MUST be COPY");
13049 Reg = MI->getOperand(1).getReg();
13050
13051 // If the copied register in the first copy must be virtual.
13052 assert(Reg.isVirtual() && "expected COPY of virtual register");
13053 MI = MRI.def_begin(Reg)->getParent();
13054
13055 // There may be an optional second copy.
13056 if (MI->getOpcode() == TargetOpcode::COPY) {
13057 assert(Reg.isVirtual() && "expected COPY of virtual register");
13058 Reg = MI->getOperand(1).getReg();
13059 assert(Reg.isPhysical() && "expected COPY of physical register");
13060 } else {
13061 // The start of the chain must be an INLINEASM_BR.
13062 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
13063 "end of copy chain MUST be INLINEASM_BR");
13064 }
13065
13066 return Reg;
13067}
13068
13069// We must do this walk rather than the simpler
13070// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
13071// otherwise we will end up with copies of virtregs only valid along direct
13072// edges.
13073void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
13074 SmallVector<EVT, 8> ResultVTs;
13075 SmallVector<SDValue, 8> ResultValues;
13076 const auto *CBR =
13077 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
13078
13079 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13080 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
13081 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
13082
13083 Register InitialDef = FuncInfo.ValueMap[CBR];
13084 SDValue Chain = DAG.getRoot();
13085
13086 // Re-parse the asm constraints string.
13087 TargetLowering::AsmOperandInfoVector TargetConstraints =
13088 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
13089 for (auto &T : TargetConstraints) {
13090 SDISelAsmOperandInfo OpInfo(T);
13091 if (OpInfo.Type != InlineAsm::isOutput)
13092 continue;
13093
13094 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
13095 // individual constraint.
13096 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
13097
13098 switch (OpInfo.ConstraintType) {
13101 // Fill in OpInfo.AssignedRegs.Regs.
13102 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
13103
13104 // getRegistersForValue may produce 1 to many registers based on whether
13105 // the OpInfo.ConstraintVT is legal on the target or not.
13106 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
13107 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
13108 if (OriginalDef.isPhysical())
13109 FuncInfo.MBB->addLiveIn(OriginalDef);
13110 // Update the assigned registers to use the original defs.
13111 Reg = OriginalDef;
13112 }
13113
13114 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
13115 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
13116 ResultValues.push_back(V);
13117 ResultVTs.push_back(OpInfo.ConstraintVT);
13118 break;
13119 }
13121 SDValue Flag;
13122 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
13123 OpInfo, DAG);
13124 ++InitialDef;
13125 ResultValues.push_back(V);
13126 ResultVTs.push_back(OpInfo.ConstraintVT);
13127 break;
13128 }
13129 default:
13130 break;
13131 }
13132 }
13134 DAG.getVTList(ResultVTs), ResultValues);
13135 setValue(&I, V);
13136}
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)
#define X(NUM, ENUM, NAME)
Definition ELF.h:851
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")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI)
Returns an AttributeList representing the attributes applied to the return value of the given call.
Definition FastISel.cpp:942
#define Check(C,...)
static Value * getCondition(Instruction *I)
Hexagon Common GEP
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
static void getRegistersForValue(MachineFunction &MF, MachineIRBuilder &MIRBuilder, GISelAsmOperandInfo &OpInfo, GISelAsmOperandInfo &RefOpInfo)
Assign virtual/physical registers for the specified register operand.
static void computeConstraintToUse(const TargetLowering *TLI, TargetLowering::AsmOperandInfo &OpInfo)
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
lazy value info
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static const Function * getCalledFunction(const Value *V)
This file provides utility analysis objects describing memory locations.
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
#define T
#define T1
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static unsigned getAddressSpace(const Value *V, unsigned MaxLookup)
MachineInstr unsigned OpIdx
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t High
uint64_t IntrinsicInst * II
OptimizedStructLayoutField Field
#define P(N)
if(PassOpts->AAPipeline)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V, bool LookThroughCmp=false)
Returns the "element 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 bool prepareDAGLevelOperands(ConstraintDecisionInfo &Info, const CallBase &Call, SelectionDAGBuilder &Builder, const TargetLowering &TLI, SelectionDAG &DAG)
Prepare DAG-level operands.
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 bool determineConstraints(ConstraintDecisionInfo &Info, TargetLowering::AsmOperandInfoVector &TargetConstraints, const CallBase &Call, SelectionDAGBuilder &Builder, const TargetLowering &TLI, const TargetMachine &TM, SelectionDAG &DAG, const BasicBlock *EHPadBB)
DetermineConstraints - Find the constraints to use for inline asm operands.
static bool constructOperandInfo(ConstraintDecisionInfo &Info, TargetLowering::AsmOperandInfoVector &TargetConstraints, SelectionDAGBuilder &Builder, const TargetLowering &TLI, ExtraFlags &ExtraInfo)
Construct operand info objects.
static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl)
GetSignificand - Get the significand and build it into a floating-point number with exponent of 1:
static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp - Lower an exp intrinsic.
static const MDNode * getRangeMetadata(const Instruction &I)
static cl::opt< unsigned, true > LimitFPPrecision("limit-float-precision", cl::desc("Generate low-precision inline sequences " "for some float libcalls"), cl::location(LimitFloatPrecision), cl::Hidden, cl::init(0))
static void tryToElideArgumentCopy(FunctionLoweringInfo &FuncInfo, SmallVectorImpl< SDValue > &Chains, DenseMap< int, int > &ArgCopyElisionFrameIndexMap, SmallPtrSetImpl< const Instruction * > &ElidedArgCopyInstrs, ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg, ArrayRef< SDValue > ArgVals, bool &ArgHasUses)
Try to elide argument copies from memory into a local alloca.
static unsigned LimitFloatPrecision
LimitFloatPrecision - Generate low-precision inline sequences for some float libcalls (6,...
static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC)
getCopyFromPartsVector - Create a value that contains the specified legal parts combined into the val...
static bool InBlock(const Value *V, const BasicBlock *BB)
static FPClassTest getNoFPClass(const Instruction &I)
static LLVM_ATTRIBUTE_ALWAYS_INLINE MVT::SimpleValueType getSimpleVT(const uint8_t *MatcherTable, size_t &MatcherIndex)
getSimpleVT - Decode a value in MatcherTable, if it's a VBR encoded value, use GetVBR to decode it.
This file defines the SmallPtrSet class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
uint16_t RegSizeInBits(const MCRegisterInfo &MRI, MCRegister RegNo)
Value * RHS
Value * LHS
The Input class is used to parse a yaml document into in-memory structs and vectors.
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
static LLVM_ABI Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition APFloat.cpp:145
static LLVM_ABI const fltSemantics * getArbitraryFPSemantics(StringRef Format)
Returns the fltSemantics for a given arbitrary FP format string, or nullptr if invalid.
Definition APFloat.cpp:5998
Class for arbitrary precision integers.
Definition APInt.h:78
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
an instruction to allocate memory on the stack
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
LLVM_ABI std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
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:338
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:39
iterator end() const
Definition ArrayRef.h:129
size_t size() const
Get the array size.
Definition ArrayRef.h:140
iterator begin() const
Definition ArrayRef.h:128
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:135
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
@ FMaximumNum
*p = maximumnum(old, v) maximumnum matches the behavior of llvm.maximumnum.
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ FMinimumNum
*p = minimumnum(old, v) minimumnum matches the behavior of llvm.minimumnum.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:407
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; assumes that the block is well-formed.
Definition BasicBlock.h:237
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:1071
Analysis providing branch probability information.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static uint32_t getDenominator()
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
BranchProbability getCompl() const
static BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
CallingConv::ID getCallingConv() const
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI bool isTailCall() const
Tests if this call site is marked as a tail call.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
Conditional Branch instruction.
Class for constant bytes.
Definition Constants.h:281
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:742
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1297
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
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:219
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:168
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
A signed pointer, in the ptrauth sense.
Definition Constants.h:1204
uint64_t getZExtValue() const
Constant Vector Declarations.
Definition Constants.h:663
This is an important base class in LLVM.
Definition Constant.h:43
This is the common base class for constrained floating point intrinsics.
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI unsigned getNonMetadataArgCount() const
DWARF expression.
LLVM_ABI bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B)
Check if fragments overlap between a pair of FragmentInfos.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI std::optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
LLVM_ABI uint64_t getNumLocationOperands() const
Return the number of unique location operands referred to (via DW_OP_LLVM_arg) in this expression; th...
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
static LLVM_ABI const DIExpression * convertToUndefExpression(const DIExpression *Expr)
Removes all elements from Expr that do not apply to an undef debug value, which includes every operat...
static LLVM_ABI DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
static LLVM_ABI DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
Base class for variables.
LLVM_ABI std::optional< uint64_t > getSizeInBits() const
Determines the size of the variable's type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isBigEndian() const
Definition DataLayout.h:218
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
LLVM_ABI iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
A debug info location.
Definition DebugLoc.h:123
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:67
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
bool empty() const
Definition DenseMap.h:109
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:239
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:114
Diagnostic information for inline asm reporting.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:67
An instruction for ordering other memory operations.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:873
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:809
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:211
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:246
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:711
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition Function.cpp:740
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
Constant * getPersonalityFn() const
Get the personality function associated with this function.
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:354
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:251
size_t arg_size() const
Definition Function.h:901
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:728
Garbage collection metadata for a single function.
Definition GCMetadata.h:80
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
bool isInBounds() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
bool hasDLLImportStorageClass() const
Module * getParent()
Get the module that this global value is contained inside of...
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
void setMemConstraint(ConstraintCode C)
setMemConstraint - Augment an existing flag with the constraint code for a memory constraint.
Definition InlineAsm.h:414
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).
static LocationSize upperBound(uint64_t Value)
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:1080
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.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
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,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
def_iterator def_begin(Register RegNo) const
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
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:146
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:116
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:184
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
Resume the propagation of an exception.
Return a value (possibly void), from a function.
Holds the information from a dbg_label node through SDISel.
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getOpcode() const
SelectionDAGBuilder - This is the common target-independent lowering implementation that is parameter...
SDValue getValue(const Value *V)
getValue - Return an SDValue for the given Value.
bool shouldKeepJumpConditionsTogether(const FunctionLoweringInfo &FuncInfo, const CondBrInst &I, Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs, TargetLoweringBase::CondMergingParams Params) const
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.
SDValue lowerStartEH(SDValue Chain, const BasicBlock *EHPadBB, MCSymbol *&BeginLabel)
void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall, bool IsMustTailCall, const BasicBlock *EHPadBB=nullptr, const TargetLowering::PtrAuthInfo *PAI=nullptr)
void clear()
Clear out the current SelectionDAG and the associated state and prepare this SelectionDAGBuilder obje...
void visitBitTestHeader(SwitchCG::BitTestBlock &B, MachineBasicBlock *SwitchBB)
visitBitTestHeader - This function emits necessary code to produce value suitable for "bit tests"
void LowerStatepoint(const GCStatepointInst &I, const BasicBlock *EHPadBB=nullptr)
std::unique_ptr< SDAGSwitchLowering > SL
SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, SDValue Op)
bool HasTailCall
This is set to true if a call in the current block has been translated as a tail call.
bool ShouldEmitAsBranches(const std::vector< SwitchCG::CaseBlock > &Cases)
If the set of cases should be emitted as a series of branches, return true.
void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
EmitBranchForMergedCondition - Helper method for FindMergedConditions.
void LowerDeoptimizeCall(const CallInst *CI)
void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB)
SwiftErrorValueTracking & SwiftError
Information about the swifterror values used throughout the function.
SDValue getNonRegisterValue(const Value *V)
getNonRegisterValue - Return an SDValue for the given Value, but don't look in FuncInfo....
const TargetTransformInfo * TTI
DenseMap< MachineBasicBlock *, SmallVector< unsigned, 4 > > LPadToCallSiteMap
Map a landing pad to the call site indexes.
SDValue lowerNoFPClassToAssertNoFPClass(SelectionDAG &DAG, const Instruction &I, SDValue Op)
void handleDebugDeclare(Value *Address, DILocalVariable *Variable, DIExpression *Expression, DebugLoc DL)
StatepointLoweringState StatepointLowering
State used while lowering a statepoint sequence (gc_statepoint, gc_relocate, and gc_result).
void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB, BranchProbability BranchProbToNext, Register Reg, SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB)
visitBitTestCase - this function produces one "bit test"
bool canTailCall(const CallBase &CB) const
void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, AttributeSet RetAttrs, bool IsPatchPoint)
Populate a CallLowerinInfo (into CLI) based on the properties of the call being lowered.
void CopyValueToVirtualRegister(const Value *V, Register Reg, ISD::NodeType ExtendType=ISD::ANY_EXTEND)
void salvageUnresolvedDbgValue(const Value *V, DanglingDebugInfo &DDI)
For the given dangling debuginfo record, perform last-ditch efforts to resolve the debuginfo to somet...
SmallVector< SDValue, 8 > PendingLoads
Loads are not emitted to the program immediately.
GCFunctionInfo * GFI
Garbage collection metadata for the function.
void init(GCFunctionInfo *gfi, BatchAAResults *BatchAA, AssumptionCache *AC, const TargetLibraryInfo *li, const TargetTransformInfo &TTI)
SDValue getRoot()
Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) items.
void ExportFromCurrentBlock(const Value *V)
ExportFromCurrentBlock - If this condition isn't known to be exported from the current basic block,...
void resolveOrClearDbgInfo()
Evict any dangling debug information, attempting to salvage it first.
std::pair< SDValue, SDValue > lowerInvokable(TargetLowering::CallLoweringInfo &CLI, const BasicBlock *EHPadBB=nullptr)
SDValue getMemoryRoot()
Return the current virtual root of the Selection DAG, flushing any PendingLoad items.
void resolveDanglingDebugInfo(const Value *V, SDValue Val)
If we saw an earlier dbg_value referring to V, generate the debug data structures now that we've seen...
void visit(const Instruction &I)
void dropDanglingDebugInfo(const DILocalVariable *Variable, const DIExpression *Expr)
If we have dangling debug info that describes Variable, or an overlapping part of variable considerin...
SDValue getCopyFromRegs(const Value *V, Type *Ty)
If there was virtual register allocated for the value V emit CopyFromReg of the specified type Ty.
void CopyToExportRegsIfNeeded(const Value *V)
CopyToExportRegsIfNeeded - If the given value has virtual registers created for it,...
void handleKillDebugValue(DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order)
Create a record for a kill location debug intrinsic.
void visitJumpTable(SwitchCG::JumpTable &JT)
visitJumpTable - Emit JumpTable node in the current MBB
SDValue getFPOperationRoot(fp::ExceptionBehavior EB)
Return the current virtual root of the Selection DAG, flushing PendingConstrainedFP or PendingConstra...
void visitJumpTableHeader(SwitchCG::JumpTable &JT, SwitchCG::JumpTableHeader &JTH, MachineBasicBlock *SwitchBB)
visitJumpTableHeader - This function emits necessary code to produce index in the JumpTable from swit...
void LowerCallSiteWithPtrAuthBundle(const CallBase &CB, const BasicBlock *EHPadBB)
static const unsigned LowestSDNodeOrder
Lowest valid SDNodeOrder.
FunctionLoweringInfo & FuncInfo
Information about the function as a whole.
void setValue(const Value *V, SDValue NewN)
void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, Instruction::BinaryOps Opc, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
const TargetLibraryInfo * LibInfo
bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB)
void visitSPDescriptorParent(StackProtectorDescriptor &SPD, MachineBasicBlock *ParentBB)
Codegen a new tail for a stack protector check ParentMBB which has had its tail spliced into a stack ...
bool handleDebugValue(ArrayRef< const Value * > Values, DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order, bool IsVariadic)
For a given list of Values, attempt to create and record a SDDbgValue in the SelectionDAG.
SDValue getControlRoot()
Similar to getRoot, but instead of flushing all the PendingLoad items, flush all the PendingExports (...
void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last)
When an MBB was split during scheduling, update the references that need to refer to the last resulti...
SDValue getValueImpl(const Value *V)
getValueImpl - Helper function for getValue and getNonRegisterValue.
void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB)
visitSwitchCase - Emits the necessary code to represent a single node in the binary search tree resul...
void visitSPDescriptorFailure(StackProtectorDescriptor &SPD)
Codegen the failure basic block for a stack protector check.
std::unique_ptr< FunctionLoweringInfo > FuncInfo
SmallPtrSet< const Instruction *, 4 > ElidedArgCopyInstrs
const TargetLowering * TLI
MachineRegisterInfo * RegInfo
std::unique_ptr< SwiftErrorValueTracking > SwiftError
virtual void emitFunctionEntryCode()
std::unique_ptr< SelectionDAGBuilder > SDB
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemccpy(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, SDValue C, SDValue Size, const CallInst *CI) const
Emit target-specific code that performs a memccpy, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrnlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, const CallInst *CI) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrstr(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, const CallInst *CI) const
Emit target-specific code that performs a strstr, in cases where that is faster than a libcall.
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 > EmitTargetCodeForStrcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, MachinePointerInfo Op1PtrInfo, MachinePointerInfo Op2PtrInfo, const CallInst *CI) const
Emit target-specific code that performs a strcmp, 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 SDValue EmitTargetCodeForSetTag(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Addr, SDValue Size, MachinePointerInfo DstPtrInfo, bool ZeroData) 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 CallInst *CI) const
Emit target-specific code that performs a strcpy or stpcpy, in cases where that is faster than a libc...
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned TargetFlags=0)
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 getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
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...
const LibcallLoweringInfo & getLibcalls() const
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.
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:140
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:137
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
Function * getSSPStackGuardCheck(const Module &M, const LibcallLoweringInfo &Libcalls) const
If the target has a standard stack protection check function that performs validation and error handl...
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
virtual bool useStackGuardXorFP() const
If this function returns true, stack protection checks should XOR the frame pointer (or whichever poi...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
virtual bool isLegalScaleForGatherScatter(uint64_t Scale, uint64_t ElemSize) const
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
virtual unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain targets require unusual breakdowns of certain types.
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const
Return the number of registers that this ValueType will eventually require.
MachineMemOperand::Flags getLoadMemOperandFlags(const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC=nullptr, const TargetLibraryInfo *LibInfo=nullptr, CodeGenOptLevel OptLevel=CodeGenOptLevel::Default) const
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...
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 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.
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 bool isProfitableToCombineMinNumMaxNum(EVT VT) const
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 void getTgtMemIntrinsic(SmallVectorImpl< IntrinsicInfo > &Infos, const CallBase &I, MachineFunction &MF, unsigned Intrinsic) const
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
virtual bool signExtendConstant(const ConstantInt *C) const
Return true if this constant should be sign extended when promoting to a larger type.
virtual Value * getSDagStackGuard(const Module &M, const LibcallLoweringInfo &Libcalls) const
Return the variable that's previously inserted by insertSSPDeclarations, if any, otherwise return nul...
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.
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.
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::LibcallImpl LibcallImpl, EVT RetVT, ArrayRef< SDValue > Ops, MakeLibCallOptions CallOptions, const SDLoc &dl, SDValue Chain=SDValue()) const
Returns a pair of (return value, chain).
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...
Primary interface to the complete machine description for the target machine.
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
CodeModel::Model getCodeModel() const
Returns the code model.
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
unsigned getID() const
Return the register class ID number.
iterator begin() const
begin/end - Return all of the registers in this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
@ TCK_Latency
The latency of instruction.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
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:184
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:290
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:284
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:286
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:370
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:310
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:236
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:317
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:227
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
Unconditional Branch instruction.
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:259
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
op_iterator op_end()
Definition User.h:261
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:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
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:709
bool use_empty() const
Definition Value.h:346
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:318
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
const ParentTy * getParent() const
Definition ilist_node.h:34
A raw_ostream that writes to an std::string.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ STACKRESTORE
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain.
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:788
@ CONVERGENCECTRL_ANCHOR
The llvm.experimental.convergence.* intrinsics.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:511
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ SET_FPENV
Sets the current floating-point environment.
@ ATOMIC_LOAD_FMINIMUMNUM
@ LOOP_DEPENDENCE_RAW_MASK
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ COND_LOOP
COND_LOOP is a conditional branch to self, used for implementing efficient conditional traps.
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition ISDOpcodes.h:168
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:600
@ STACKADDRESS
STACKADDRESS - Represents the llvm.stackaddress intrinsic.
Definition ISDOpcodes.h:127
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:779
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:394
@ VAEND
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE.
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ RESET_FPENV
Set floating-point environment to default state.
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:400
@ SET_FPMODE
Sets the current dynamic floating-point control modes.
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:853
@ CTTZ_ELTS
Returns the number of number of trailing (least significant) zero elements in a vector.
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:518
@ VECTOR_FIND_LAST_ACTIVE
Finds the index of the last active mask element Operands: Mask.
@ FMODF
FMODF - Decomposes the operand into integral and fractional parts, each having the same type and sign...
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ FSINCOSPI
FSINCOSPI - Compute both the sine and cosine times pi more accurately than FSINCOS(pi*x),...
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ EH_SJLJ_SETUP_DISPATCH
OUTCHAIN = EH_SJLJ_SETUP_DISPATCH(INCHAIN) The target initializes the dispatch table here.
Definition ISDOpcodes.h:172
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:880
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:584
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:747
@ ATOMIC_FENCE
OUTCHAIN = ATOMIC_FENCE(INCHAIN, ordering, scope) This corresponds to the fence instruction.
@ RESET_FPMODE
Sets default dynamic floating-point control modes.
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:528
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition ISDOpcodes.h:515
@ FAKE_USE
FAKE_USE represents a use of the operand but does not do anything.
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:993
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:774
@ INIT_TRAMPOLINE
INIT_TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:407
@ CONVERT_FROM_ARBITRARY_FP
CONVERT_FROM_ARBITRARY_FP - This operator converts from an arbitrary floating-point represented as an...
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ATOMIC_LOAD_USUB_SAT
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:156
@ ANNOTATION_LABEL
ANNOTATION_LABEL - Represents a mid basic block label used by annotations.
@ SET_ROUNDING
Set rounding mode.
Definition ISDOpcodes.h:975
@ CONVERGENCECTRL_GLUE
This does not correspond to any convergence control intrinsic.
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:844
@ PREALLOCATED_SETUP
PREALLOCATED_SETUP - This has 2 operands: an input chain and a SRCVALUE with the preallocated call Va...
@ READSTEADYCOUNTER
READSTEADYCOUNTER - This corresponds to the readfixedcounter intrinsic.
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition ISDOpcodes.h:117
@ CONVERGENCECTRL_ENTRY
@ BR
Control flow instructions. These all have token chains.
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:787
@ PARTIAL_REDUCE_FMLA
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ PREALLOCATED_ARG
PREALLOCATED_ARG - This has 3 operands: an input chain, a SRCVALUE with the preallocated call Value,...
@ BRIND
BRIND - Indirect branch.
@ BR_JT
BR_JT - Jumptable branch.
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:635
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:541
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:548
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:796
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:672
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ 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:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ 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:970
@ CLEANUPRET
CLEANUPRET - Represents a return from a cleanup block funclet.
@ ATOMIC_LOAD_FMAXIMUM
@ GET_FPMODE
Reads the current dynamic floating-point control modes.
@ GET_FPENV
Gets the current floating-point environment.
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:765
@ 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:614
@ 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:139
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:576
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:850
@ DEBUGTRAP
DEBUGTRAP - Trap intended to get the attention of a debugger.
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ LOCAL_RECOVER
LOCAL_RECOVER - Represents the llvm.localrecover intrinsic.
Definition ISDOpcodes.h:135
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ UBSANTRAP
UBSANTRAP - Trap with an immediate describing the kind of sanitizer failure.
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ PATCHPOINT
The llvm.experimental.patchpoint.
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ ATOMIC_LOAD_FMINIMUM
@ DYNAMIC_STACKALLOC
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary.
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
Definition ISDOpcodes.h:653
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:727
@ MASKED_UDIV
Masked vector arithmetic that returns poison on disabled lanes.
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:640
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:413
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:978
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:805
@ PCMARKER
PCMARKER - This corresponds to the pcmarker intrinsic.
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ ATOMIC_LOAD_FMAXIMUMNUM
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:150
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ ATOMIC_LOAD_UDEC_WRAP
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:500
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:926
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ RELOC_NONE
Issue a no-op relocation against a given symbol at the current location.
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:739
@ TRAP
TRAP - Trapping instruction.
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:735
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:657
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition ISDOpcodes.h:427
@ STACKMAP
The llvm.experimental.stackmap intrinsic.
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:959
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:699
@ SPONENTRY
SPONENTRY - Represents the llvm.sponentry intrinsic.
Definition ISDOpcodes.h:122
@ CLEAR_CACHE
llvm.clear_cache intrinsic Operands: Input Chain, Start Addres, End Address Outputs: Output Chain
@ CONVERGENCECTRL_LOOP
@ INLINEASM
INLINEASM - Represents an inline asm block.
@ 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:945
@ VECREDUCE_FMINIMUM
@ EH_SJLJ_SETJMP
RESULT, OUTCHAIN = EH_SJLJ_SETJMP(INCHAIN, buffer) This corresponds to the eh.sjlj....
Definition ISDOpcodes.h:162
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:856
@ BRCOND
BRCOND - Conditional branch.
@ VECREDUCE_SEQ_FMUL
@ CATCHRET
CATCHRET - Represents a return from a catch block funclet.
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:534
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:624
@ GET_DYNAMIC_AREA_OFFSET
GET_DYNAMIC_AREA_OFFSET - get offset from native SP to the address of the most recent dynamic alloca.
@ CTTZ_ELTS_ZERO_POISON
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ ADJUST_TRAMPOLINE
ADJUST_TRAMPOLINE - This corresponds to the adjust_trampoline intrinsic.
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
@ LOOP_DEPENDENCE_WAR_MASK
The llvm.loop.dependence.
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_Value()
Match an arbitrary value and ignore it.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Offsets
Offsets in bytes from the start of the input buffer.
std::pair< JumpTableHeader, JumpTable > JumpTableBlock
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::vector< CaseCluster > CaseClusterVector
@ CC_Range
A cluster of adjacent case labels with the same destination, or just one case.
@ CC_JumpTable
A cluster of cases suitable for jump table lowering.
@ CC_BitTests
A cluster of cases suitable for bit test lowering.
SmallVector< SwitchWorkListItem, 4 > SwitchWorkList
CaseClusterVector::iterator CaseClusterIt
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebStrict
This corresponds to "fpexcept.strict".
Definition FPEnv.h:42
@ ebMayTrap
This corresponds to "fpexcept.maytrap".
Definition FPEnv.h:41
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
constexpr float log2ef
Definition MathExtras.h:51
constexpr double e
constexpr float ln2f
Definition MathExtras.h:49
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:557
@ Length
Definition DWP.cpp:557
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:237
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:1738
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:1668
SDValue peekThroughFreeze(SDValue V)
Return the non-frozen source operand of V if it exists.
LLVM_ABI void GetReturnInfo(CallingConv::ID CC, Type *ReturnType, AttributeList attr, SmallVectorImpl< ISD::OutputArg > &Outs, const TargetLowering &TLI, const DataLayout &DL)
Given an LLVM IR type and return type attributes, compute the return value EVTs and flags,...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs=nullptr, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition Analysis.cpp:119
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
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:2207
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
auto cast_or_null(const Y &Val)
Definition Casting.h:714
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:546
gep_type_iterator gep_type_end(const User *GEP)
constexpr auto equal_to(T &&Arg)
Functor variant of std::equal_to that can be used as a UnaryPredicate in functional algorithms like a...
Definition STLExtras.h:2172
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:156
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:1151
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:204
void ComputeValueTypes(const DataLayout &DL, Type *Ty, SmallVectorImpl< Type * > &Types, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
Given an LLVM IR type, compute non-aggregate subtypes.
Definition Analysis.cpp:72
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1745
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:853
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1635
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:163
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
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:299
ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred)
getFCmpCondCode - Return the ISD condition code corresponding to the given LLVM IR floating-point con...
Definition Analysis.cpp:203
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI Value * salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Ops, SmallVectorImpl< Value * > &AdditionalValues)
Definition Local.cpp:2289
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Global
Append to llvm.global_dtors.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
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.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
@ 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:539
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:225
ArrayRef(const T &OneElt) -> ArrayRef< T >
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
LLVM_ABI std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition FPEnv.cpp:25
gep_type_iterator gep_type_begin(const User *GEP)
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2191
GlobalValue * ExtractTypeInfo(Value *V)
ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
Definition Analysis.cpp:181
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2165
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:347
@ Default
The result value is 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:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:876
#define N
#define NC
Definition regutils.h:42
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:403
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:70
uint64_t getScalarStoreSize() const
Definition ValueTypes.h:410
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:292
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:308
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
ElementCount getVectorElementCount() const
Definition ValueTypes.h:358
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:381
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:367
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:393
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
EVT changeVectorElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:98
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:324
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
bool isRISCVVectorTuple() const
Return true if this is a vector value type.
Definition ValueTypes.h:187
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:389
bool isFixedLengthVector() const
Definition ValueTypes.h:189
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:331
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:300
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:182
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:336
EVT changeElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a type whose attributes match ourselves with the exception of the element type that i...
Definition ValueTypes.h:121
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:165
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:344
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
void setPointerAddrSpace(unsigned AS)
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:262
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
A lightweight accessor for an operand bundle meant to be passed around by value.
This struct represents the registers (physical or virtual) that a particular set of values is assigne...
SmallVector< std::pair< Register, TypeSize >, 4 > getRegsAndSizes() const
Return a list of registers and their sizes.
RegsForValue()=default
SmallVector< unsigned, 4 > RegCount
This list holds the number of registers for each value.
SmallVector< EVT, 4 > ValueVTs
The value types of the values, which may not be legal, and may need be promoted or synthesized from o...
SmallVector< Register, 4 > Regs
This list holds the registers assigned to the values.
void AddInlineAsmOperands(InlineAsm::Kind Code, bool HasMatching, unsigned MatchingIdx, const SDLoc &dl, SelectionDAG &DAG, std::vector< SDValue > &Ops) const
Add this value to the specified inlineasm node operand list.
SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr) const
Emit a series of CopyFromReg nodes that copies from this value and returns the result as a ValueVTs v...
SmallVector< MVT, 4 > RegVTs
The value types of the registers.
void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr, ISD::NodeType PreferredExtendType=ISD::ANY_EXTEND) const
Emit a series of CopyToReg nodes that copies the specified value into the registers specified by this...
std::optional< CallingConv::ID > CallConv
Records if this value needs to be treated in an ABI dependant manner, different to normal type legali...
bool occupiesMultipleRegs() const
Check if the total RegCount is greater than one.
These are IR-level optimization flags that may be propagated to SDNodes.
void copyFMF(const FPMathOperator &FPMO)
Propagate the fast-math-flags from an IR FPMathOperator.
void setUnpredictable(bool b)
bool hasAllowReassociation() const
void setNoUnsignedWrap(bool b)
void setNoSignedWrap(bool b)
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:276
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)
Register Reg
The virtual register containing the index of the jump table entry to jump to.
MachineBasicBlock * Default
The MBB of the default bb, which is a successor of the range check MBB.
unsigned JTI
The JumpTableIndex for this jump table in the function.
MachineBasicBlock * MBB
The MBB into which to emit the code for the indirect jump.
std::optional< SDLoc > SL
The debug location of the instruction this JumpTable was produced from.
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 & setDeactivationSymbol(GlobalValue *Sym)
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)