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.
1779 auto It = FuncInfo.ValueMap.find(V);
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)) {
2013 auto SI = FuncInfo.StaticAllocaMap.find(AI);
2014 if (SI != FuncInfo.StaticAllocaMap.end())
2015 return DAG.getFrameIndex(
2016 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
2017 }
2018
2019 // If this is an instruction which fast-isel has deferred, select it now.
2020 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2021 Register InReg = FuncInfo.InitializeRegForValue(Inst);
2022 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
2023 Inst->getType(), std::nullopt);
2024 SDValue Chain = DAG.getEntryNode();
2025 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
2026 }
2027
2028 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
2029 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
2030
2031 if (const auto *BB = dyn_cast<BasicBlock>(V))
2032 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
2033
2034 llvm_unreachable("Can't get register for value!");
2035}
2036
2037void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
2039 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
2040 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
2041 bool IsSEH = isAsynchronousEHPersonality(Pers);
2042 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2043 if (IsSEH) {
2044 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2045 CatchPadMBB->setIsEHContTarget(true);
2047 } else
2048 CatchPadMBB->setIsEHScopeEntry();
2049 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2050 if (IsMSVCCXX || IsCoreCLR)
2051 CatchPadMBB->setIsEHFuncletEntry();
2052}
2053
2054void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2055 // Update machine-CFG edge.
2056 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2057 FuncInfo.MBB->addSuccessor(TargetMBB);
2058
2059 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2060 bool IsSEH = isAsynchronousEHPersonality(Pers);
2061 if (IsSEH) {
2062 // If this is not a fall-through branch or optimizations are switched off,
2063 // emit the branch.
2064 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2065 TM.getOptLevel() == CodeGenOptLevel::None)
2066 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2067 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2068 return;
2069 }
2070
2071 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2072 TargetMBB->setIsEHContTarget(true);
2073 DAG.getMachineFunction().setHasEHContTarget(true);
2074
2075 // Figure out the funclet membership for the catchret's successor.
2076 // This will be used by the FuncletLayout pass to determine how to order the
2077 // BB's.
2078 // A 'catchret' returns to the outer scope's color.
2079 Value *ParentPad = I.getCatchSwitchParentPad();
2080 const BasicBlock *SuccessorColor;
2081 if (isa<ConstantTokenNone>(ParentPad))
2082 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2083 else
2084 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2085 assert(SuccessorColor && "No parent funclet for catchret!");
2086 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2087 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2088
2089 // Create the terminator node.
2090 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2091 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2092 DAG.getBasicBlock(SuccessorColorMBB));
2093 DAG.setRoot(Ret);
2094}
2095
2096void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2097 // Don't emit any special code for the cleanuppad instruction. It just marks
2098 // the start of an EH scope/funclet.
2099 FuncInfo.MBB->setIsEHScopeEntry();
2100 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2101 if (Pers != EHPersonality::Wasm_CXX) {
2102 FuncInfo.MBB->setIsEHFuncletEntry();
2103 FuncInfo.MBB->setIsCleanupFuncletEntry();
2104 }
2105}
2106
2107/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2108/// many places it could ultimately go. In the IR, we have a single unwind
2109/// destination, but in the machine CFG, we enumerate all the possible blocks.
2110/// This function skips over imaginary basic blocks that hold catchswitch
2111/// instructions, and finds all the "real" machine
2112/// basic block destinations. As those destinations may not be successors of
2113/// EHPadBB, here we also calculate the edge probability to those destinations.
2114/// The passed-in Prob is the edge probability to EHPadBB.
2116 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2117 BranchProbability Prob,
2118 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2119 &UnwindDests) {
2120 EHPersonality Personality =
2122 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2123 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2124 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2125 bool IsSEH = isAsynchronousEHPersonality(Personality);
2126
2127 while (EHPadBB) {
2129 BasicBlock *NewEHPadBB = nullptr;
2130 if (isa<LandingPadInst>(Pad)) {
2131 // Stop on landingpads. They are not funclets.
2132 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2133 break;
2134 } else if (isa<CleanupPadInst>(Pad)) {
2135 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2136 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2137 // which always catches an exception.
2138 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2139 UnwindDests.back().first->setIsEHScopeEntry();
2140 // In Wasm, EH scopes are not funclets
2141 if (!IsWasmCXX)
2142 UnwindDests.back().first->setIsEHFuncletEntry();
2143 break;
2144 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2145 // Add the catchpad handlers to the possible destinations.
2146 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2147 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2148 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2149 if (IsMSVCCXX || IsCoreCLR)
2150 UnwindDests.back().first->setIsEHFuncletEntry();
2151 if (!IsSEH)
2152 UnwindDests.back().first->setIsEHScopeEntry();
2153 }
2154 NewEHPadBB = CatchSwitch->getUnwindDest();
2155 } else {
2156 continue;
2157 }
2158
2159 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2160 if (BPI && NewEHPadBB)
2161 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2162 EHPadBB = NewEHPadBB;
2163 }
2164}
2165
2166void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2167 // Update successor info.
2169 auto UnwindDest = I.getUnwindDest();
2170 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2171 BranchProbability UnwindDestProb =
2172 (BPI && UnwindDest)
2173 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2175 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2176 for (auto &UnwindDest : UnwindDests) {
2177 UnwindDest.first->setIsEHPad();
2178 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2179 }
2180 FuncInfo.MBB->normalizeSuccProbs();
2181
2182 // Create the terminator node.
2183 MachineBasicBlock *CleanupPadMBB =
2184 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2185 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
2186 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2187 DAG.setRoot(Ret);
2188}
2189
2190void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2191 report_fatal_error("visitCatchSwitch not yet implemented!");
2192}
2193
2194void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2195 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2196 auto &DL = DAG.getDataLayout();
2197 SDValue Chain = getControlRoot();
2200
2201 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2202 // lower
2203 //
2204 // %val = call <ty> @llvm.experimental.deoptimize()
2205 // ret <ty> %val
2206 //
2207 // differently.
2208 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2210 return;
2211 }
2212
2213 if (!FuncInfo.CanLowerReturn) {
2214 Register DemoteReg = FuncInfo.DemoteRegister;
2215
2216 // Emit a store of the return value through the virtual register.
2217 // Leave Outs empty so that LowerReturn won't try to load return
2218 // registers the usual way.
2219 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2220 SDValue RetPtr =
2221 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2222 SDValue RetOp = getValue(I.getOperand(0));
2223
2224 SmallVector<EVT, 4> ValueVTs, MemVTs;
2225 SmallVector<uint64_t, 4> Offsets;
2226 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2227 &Offsets, 0);
2228 unsigned NumValues = ValueVTs.size();
2229
2230 SmallVector<SDValue, 4> Chains(NumValues);
2231 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2232 for (unsigned i = 0; i != NumValues; ++i) {
2233 // An aggregate return value cannot wrap around the address space, so
2234 // offsets to its parts don't wrap either.
2235 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2236 TypeSize::getFixed(Offsets[i]));
2237
2238 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2239 if (MemVTs[i] != ValueVTs[i])
2240 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2241 Chains[i] = DAG.getStore(
2242 Chain, getCurSDLoc(), Val,
2243 // FIXME: better loc info would be nice.
2244 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2245 commonAlignment(BaseAlign, Offsets[i]));
2246 }
2247
2248 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2249 MVT::Other, Chains);
2250 } else if (I.getNumOperands() != 0) {
2252 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2253 unsigned NumValues = Types.size();
2254 if (NumValues) {
2255 SDValue RetOp = getValue(I.getOperand(0));
2256
2257 const Function *F = I.getParent()->getParent();
2258
2259 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2260 I.getOperand(0)->getType(), F->getCallingConv(),
2261 /*IsVarArg*/ false, DL);
2262
2263 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2264 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2265 ExtendKind = ISD::SIGN_EXTEND;
2266 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2267 ExtendKind = ISD::ZERO_EXTEND;
2268
2269 LLVMContext &Context = F->getContext();
2270 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2271
2272 for (unsigned j = 0; j != NumValues; ++j) {
2273 EVT VT = TLI.getValueType(DL, Types[j]);
2274
2275 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2276 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2277
2278 CallingConv::ID CC = F->getCallingConv();
2279
2280 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2281 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2282 SmallVector<SDValue, 4> Parts(NumParts);
2284 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2285 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2286
2287 // 'inreg' on function refers to return value
2288 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2289 if (RetInReg)
2290 Flags.setInReg();
2291
2292 if (I.getOperand(0)->getType()->isPointerTy()) {
2293 Flags.setPointer();
2294 Flags.setPointerAddrSpace(
2295 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2296 }
2297
2298 if (NeedsRegBlock) {
2299 Flags.setInConsecutiveRegs();
2300 if (j == NumValues - 1)
2301 Flags.setInConsecutiveRegsLast();
2302 }
2303
2304 // Propagate extension type if any
2305 if (ExtendKind == ISD::SIGN_EXTEND)
2306 Flags.setSExt();
2307 else if (ExtendKind == ISD::ZERO_EXTEND)
2308 Flags.setZExt();
2309 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2310 Flags.setNoExt();
2311
2312 for (unsigned i = 0; i < NumParts; ++i) {
2313 Outs.push_back(ISD::OutputArg(Flags,
2314 Parts[i].getValueType().getSimpleVT(),
2315 VT, Types[j], 0, 0));
2316 OutVals.push_back(Parts[i]);
2317 }
2318 }
2319 }
2320 }
2321
2322 // Push in swifterror virtual register as the last element of Outs. This makes
2323 // sure swifterror virtual register will be returned in the swifterror
2324 // physical register.
2325 const Function *F = I.getParent()->getParent();
2326 if (TLI.supportSwiftError() &&
2327 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2328 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2329 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2330 Flags.setSwiftError();
2331 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2332 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2333 PointerType::getUnqual(*DAG.getContext()),
2334 /*origidx=*/1, /*partOffs=*/0));
2335 // Create SDNode for the swifterror virtual register.
2336 OutVals.push_back(
2337 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2338 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2339 EVT(TLI.getPointerTy(DL))));
2340 }
2341
2342 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2343 CallingConv::ID CallConv =
2344 DAG.getMachineFunction().getFunction().getCallingConv();
2345 Chain = DAG.getTargetLoweringInfo().LowerReturn(
2346 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2347
2348 // Verify that the target's LowerReturn behaved as expected.
2349 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2350 "LowerReturn didn't return a valid chain!");
2351
2352 // Update the DAG with the new chain value resulting from return lowering.
2353 DAG.setRoot(Chain);
2354}
2355
2356/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2357/// created for it, emit nodes to copy the value into the virtual
2358/// registers.
2360 // Skip empty types
2361 if (V->getType()->isEmptyTy())
2362 return;
2363
2364 auto VMI = FuncInfo.ValueMap.find(V);
2365 if (VMI != FuncInfo.ValueMap.end()) {
2366 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2367 "Unused value assigned virtual registers!");
2368 CopyValueToVirtualRegister(V, VMI->second);
2369 }
2370}
2371
2372/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2373/// the current basic block, add it to ValueMap now so that we'll get a
2374/// CopyTo/FromReg.
2376 // No need to export constants.
2377 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2378
2379 // Already exported?
2380 if (FuncInfo.isExportedInst(V)) return;
2381
2382 Register Reg = FuncInfo.InitializeRegForValue(V);
2384}
2385
2387 const BasicBlock *FromBB) {
2388 // The operands of the setcc have to be in this block. We don't know
2389 // how to export them from some other block.
2390 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2391 // Can export from current BB.
2392 if (VI->getParent() == FromBB)
2393 return true;
2394
2395 // Is already exported, noop.
2396 return FuncInfo.isExportedInst(V);
2397 }
2398
2399 // If this is an argument, we can export it if the BB is the entry block or
2400 // if it is already exported.
2401 if (isa<Argument>(V)) {
2402 if (FromBB->isEntryBlock())
2403 return true;
2404
2405 // Otherwise, can only export this if it is already exported.
2406 return FuncInfo.isExportedInst(V);
2407 }
2408
2409 // Otherwise, constants can always be exported.
2410 return true;
2411}
2412
2413/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2415SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2416 const MachineBasicBlock *Dst) const {
2418 const BasicBlock *SrcBB = Src->getBasicBlock();
2419 const BasicBlock *DstBB = Dst->getBasicBlock();
2420 if (!BPI) {
2421 // If BPI is not available, set the default probability as 1 / N, where N is
2422 // the number of successors.
2423 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2424 return BranchProbability(1, SuccSize);
2425 }
2426 return BPI->getEdgeProbability(SrcBB, DstBB);
2427}
2428
2429void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2430 MachineBasicBlock *Dst,
2431 BranchProbability Prob) {
2432 if (!FuncInfo.BPI)
2433 Src->addSuccessorWithoutProb(Dst);
2434 else {
2435 if (Prob.isUnknown())
2436 Prob = getEdgeProbability(Src, Dst);
2437 Src->addSuccessor(Dst, Prob);
2438 }
2439}
2440
2441static bool InBlock(const Value *V, const BasicBlock *BB) {
2442 if (const Instruction *I = dyn_cast<Instruction>(V))
2443 return I->getParent() == BB;
2444 return true;
2445}
2446
2447/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2448/// This function emits a branch and is used at the leaves of an OR or an
2449/// AND operator tree.
2450void
2453 MachineBasicBlock *FBB,
2454 MachineBasicBlock *CurBB,
2455 MachineBasicBlock *SwitchBB,
2456 BranchProbability TProb,
2457 BranchProbability FProb,
2458 bool InvertCond) {
2459 const BasicBlock *BB = CurBB->getBasicBlock();
2460
2461 // If the leaf of the tree is a comparison, merge the condition into
2462 // the caseblock.
2463 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2464 // The operands of the cmp have to be in this block. We don't know
2465 // how to export them from some other block. If this is the first block
2466 // of the sequence, no exporting is needed.
2467 if (CurBB == SwitchBB ||
2468 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2469 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2470 ISD::CondCode Condition;
2471 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2472 ICmpInst::Predicate Pred =
2473 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2474 Condition = getICmpCondCode(Pred);
2475 } else {
2476 const FCmpInst *FC = cast<FCmpInst>(Cond);
2477 FCmpInst::Predicate Pred =
2478 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2479 Condition = getFCmpCondCode(Pred);
2480 if (FC->hasNoNaNs() ||
2481 (isKnownNeverNaN(FC->getOperand(0),
2482 SimplifyQuery(DAG.getDataLayout(), FC)) &&
2483 isKnownNeverNaN(FC->getOperand(1),
2484 SimplifyQuery(DAG.getDataLayout(), FC))))
2485 Condition = getFCmpCodeWithoutNaN(Condition);
2486 }
2487
2488 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2489 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2490 SL->SwitchCases.push_back(CB);
2491 return;
2492 }
2493 }
2494
2495 // Create a CaseBlock record representing this branch.
2496 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2497 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2498 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2499 SL->SwitchCases.push_back(CB);
2500}
2501
2502// Collect dependencies on V recursively. This is used for the cost analysis in
2503// `shouldKeepJumpConditionsTogether`.
2507 unsigned Depth = 0) {
2508 // Return false if we have an incomplete count.
2510 return false;
2511
2512 auto *I = dyn_cast<Instruction>(V);
2513 if (I == nullptr)
2514 return true;
2515
2516 if (Necessary != nullptr) {
2517 // This instruction is necessary for the other side of the condition so
2518 // don't count it.
2519 if (Necessary->contains(I))
2520 return true;
2521 }
2522
2523 // Already added this dep.
2524 if (!Deps->try_emplace(I, false).second)
2525 return true;
2526
2527 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2528 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2529 Depth + 1))
2530 return false;
2531 return true;
2532}
2533
2536 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2538 if (Params.BaseCost < 0)
2539 return false;
2540
2541 // Baseline cost.
2542 InstructionCost CostThresh = Params.BaseCost;
2543
2544 BranchProbabilityInfo *BPI = nullptr;
2545 if (Params.LikelyBias || Params.UnlikelyBias)
2546 BPI = FuncInfo.BPI;
2547 if (BPI != nullptr) {
2548 // See if we are either likely to get an early out or compute both lhs/rhs
2549 // of the condition.
2550 BasicBlock *IfFalse = I.getSuccessor(0);
2551 BasicBlock *IfTrue = I.getSuccessor(1);
2552
2553 std::optional<bool> Likely;
2554 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2555 Likely = true;
2556 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2557 Likely = false;
2558
2559 if (Likely) {
2560 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2561 // Its likely we will have to compute both lhs and rhs of condition
2562 CostThresh += Params.LikelyBias;
2563 else {
2564 if (Params.UnlikelyBias < 0)
2565 return false;
2566 // Its likely we will get an early out.
2567 CostThresh -= Params.UnlikelyBias;
2568 }
2569 }
2570 }
2571
2572 if (CostThresh <= 0)
2573 return false;
2574
2575 // Collect "all" instructions that lhs condition is dependent on.
2576 // Use map for stable iteration (to avoid non-determanism of iteration of
2577 // SmallPtrSet). The `bool` value is just a dummy.
2579 collectInstructionDeps(&LhsDeps, Lhs);
2580 // Collect "all" instructions that rhs condition is dependent on AND are
2581 // dependencies of lhs. This gives us an estimate on which instructions we
2582 // stand to save by splitting the condition.
2583 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2584 return false;
2585 // Add the compare instruction itself unless its a dependency on the LHS.
2586 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2587 if (!LhsDeps.contains(RhsI))
2588 RhsDeps.try_emplace(RhsI, false);
2589
2590 InstructionCost CostOfIncluding = 0;
2591 // See if this instruction will need to computed independently of whether RHS
2592 // is.
2593 Value *BrCond = I.getCondition();
2594 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2595 for (const auto *U : Ins->users()) {
2596 // If user is independent of RHS calculation we don't need to count it.
2597 if (auto *UIns = dyn_cast<Instruction>(U))
2598 if (UIns != BrCond && !RhsDeps.contains(UIns))
2599 return false;
2600 }
2601 return true;
2602 };
2603
2604 // Prune instructions from RHS Deps that are dependencies of unrelated
2605 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2606 // arbitrary and just meant to cap the how much time we spend in the pruning
2607 // loop. Its highly unlikely to come into affect.
2608 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2609 // Stop after a certain point. No incorrectness from including too many
2610 // instructions.
2611 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2612 const Instruction *ToDrop = nullptr;
2613 for (const auto &InsPair : RhsDeps) {
2614 if (!ShouldCountInsn(InsPair.first)) {
2615 ToDrop = InsPair.first;
2616 break;
2617 }
2618 }
2619 if (ToDrop == nullptr)
2620 break;
2621 RhsDeps.erase(ToDrop);
2622 }
2623
2624 for (const auto &InsPair : RhsDeps) {
2625 // Finally accumulate latency that we can only attribute to computing the
2626 // RHS condition. Use latency because we are essentially trying to calculate
2627 // the cost of the dependency chain.
2628 // Possible TODO: We could try to estimate ILP and make this more precise.
2629 CostOfIncluding += TTI->getInstructionCost(
2630 InsPair.first, TargetTransformInfo::TCK_Latency);
2631
2632 if (CostOfIncluding > CostThresh)
2633 return false;
2634 }
2635 return true;
2636}
2637
2640 MachineBasicBlock *FBB,
2641 MachineBasicBlock *CurBB,
2642 MachineBasicBlock *SwitchBB,
2644 BranchProbability TProb,
2645 BranchProbability FProb,
2646 bool InvertCond) {
2647 // Skip over not part of the tree and remember to invert op and operands at
2648 // next level.
2649 Value *NotCond;
2650 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2651 InBlock(NotCond, CurBB->getBasicBlock())) {
2652 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2653 !InvertCond);
2654 return;
2655 }
2656
2658 const Value *BOpOp0, *BOpOp1;
2659 // Compute the effective opcode for Cond, taking into account whether it needs
2660 // to be inverted, e.g.
2661 // and (not (or A, B)), C
2662 // gets lowered as
2663 // and (and (not A, not B), C)
2665 if (BOp) {
2666 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2667 ? Instruction::And
2668 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2669 ? Instruction::Or
2671 if (InvertCond) {
2672 if (BOpc == Instruction::And)
2673 BOpc = Instruction::Or;
2674 else if (BOpc == Instruction::Or)
2675 BOpc = Instruction::And;
2676 }
2677 }
2678
2679 // If this node is not part of the or/and tree, emit it as a branch.
2680 // Note that all nodes in the tree should have same opcode.
2681 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2682 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2683 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2684 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2685 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2686 TProb, FProb, InvertCond);
2687 return;
2688 }
2689
2690 // Create TmpBB after CurBB.
2691 MachineFunction::iterator BBI(CurBB);
2692 MachineFunction &MF = DAG.getMachineFunction();
2694 CurBB->getParent()->insert(++BBI, TmpBB);
2695
2696 if (Opc == Instruction::Or) {
2697 // Codegen X | Y as:
2698 // BB1:
2699 // jmp_if_X TBB
2700 // jmp TmpBB
2701 // TmpBB:
2702 // jmp_if_Y TBB
2703 // jmp FBB
2704 //
2705
2706 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2707 // The requirement is that
2708 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2709 // = TrueProb for original BB.
2710 // Assuming the original probabilities are A and B, one choice is to set
2711 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2712 // A/(1+B) and 2B/(1+B). This choice assumes that
2713 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2714 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2715 // TmpBB, but the math is more complicated.
2716
2717 auto NewTrueProb = TProb / 2;
2718 auto NewFalseProb = TProb / 2 + FProb;
2719 // Emit the LHS condition.
2720 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2721 NewFalseProb, InvertCond);
2722
2723 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2724 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2726 // Emit the RHS condition into TmpBB.
2727 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2728 Probs[1], InvertCond);
2729 } else {
2730 assert(Opc == Instruction::And && "Unknown merge op!");
2731 // Codegen X & Y as:
2732 // BB1:
2733 // jmp_if_X TmpBB
2734 // jmp FBB
2735 // TmpBB:
2736 // jmp_if_Y TBB
2737 // jmp FBB
2738 //
2739 // This requires creation of TmpBB after CurBB.
2740
2741 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2742 // The requirement is that
2743 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2744 // = FalseProb for original BB.
2745 // Assuming the original probabilities are A and B, one choice is to set
2746 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2747 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2748 // TrueProb for BB1 * FalseProb for TmpBB.
2749
2750 auto NewTrueProb = TProb + FProb / 2;
2751 auto NewFalseProb = FProb / 2;
2752 // Emit the LHS condition.
2753 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2754 NewFalseProb, InvertCond);
2755
2756 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2757 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2759 // Emit the RHS condition into TmpBB.
2760 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2761 Probs[1], InvertCond);
2762 }
2763}
2764
2765/// If the set of cases should be emitted as a series of branches, return true.
2766/// If we should emit this as a bunch of and/or'd together conditions, return
2767/// false.
2768bool
2769SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2770 if (Cases.size() != 2) return true;
2771
2772 // If this is two comparisons of the same values or'd or and'd together, they
2773 // will get folded into a single comparison, so don't emit two blocks.
2774 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2775 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2776 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2777 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2778 return false;
2779 }
2780
2781 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2782 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2783 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2784 Cases[0].CC == Cases[1].CC &&
2785 isa<Constant>(Cases[0].CmpRHS) &&
2786 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2787 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2788 return false;
2789 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2790 return false;
2791 }
2792
2793 return true;
2794}
2795
2796void SelectionDAGBuilder::visitUncondBr(const UncondBrInst &I) {
2798
2799 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2800
2801 // Update machine-CFG edges.
2802 BrMBB->addSuccessor(Succ0MBB);
2803
2804 // If this is not a fall-through branch or optimizations are switched off,
2805 // emit the branch.
2806 if (Succ0MBB != NextBlock(BrMBB) ||
2808 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
2809 DAG.getBasicBlock(Succ0MBB));
2810 setValue(&I, Br);
2811 DAG.setRoot(Br);
2812 }
2813}
2814
2815void SelectionDAGBuilder::visitCondBr(const CondBrInst &I) {
2816 MachineBasicBlock *BrMBB = FuncInfo.MBB;
2817
2818 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2819
2820 // If this condition is one of the special cases we handle, do special stuff
2821 // now.
2822 const Value *CondVal = I.getCondition();
2823 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2824
2825 // If this is a series of conditions that are or'd or and'd together, emit
2826 // this as a sequence of branches instead of setcc's with and/or operations.
2827 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2828 // unpredictable branches, and vector extracts because those jumps are likely
2829 // expensive for any target), this should improve performance.
2830 // For example, instead of something like:
2831 // cmp A, B
2832 // C = seteq
2833 // cmp D, E
2834 // F = setle
2835 // or C, F
2836 // jnz foo
2837 // Emit:
2838 // cmp A, B
2839 // je foo
2840 // cmp D, E
2841 // jle foo
2842 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2843 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2844 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2845 BOp->hasOneUse() && !IsUnpredictable) {
2846 Value *Vec;
2847 const Value *BOp0, *BOp1;
2849 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2850 Opcode = Instruction::And;
2851 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2852 Opcode = Instruction::Or;
2853
2854 if (Opcode &&
2855 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2856 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2858 FuncInfo, I, Opcode, BOp0, BOp1,
2859 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2860 Opcode, BOp0, BOp1))) {
2861 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2862 getEdgeProbability(BrMBB, Succ0MBB),
2863 getEdgeProbability(BrMBB, Succ1MBB),
2864 /*InvertCond=*/false);
2865 // If the compares in later blocks need to use values not currently
2866 // exported from this block, export them now. This block should always
2867 // be the first entry.
2868 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2869
2870 // Allow some cases to be rejected.
2871 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2872 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2873 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2874 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2875 }
2876
2877 // Emit the branch for this block.
2878 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2879 SL->SwitchCases.erase(SL->SwitchCases.begin());
2880 return;
2881 }
2882
2883 // Okay, we decided not to do this, remove any inserted MBB's and clear
2884 // SwitchCases.
2885 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2886 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2887
2888 SL->SwitchCases.clear();
2889 }
2890 }
2891
2892 // Create a CaseBlock record representing this branch.
2893 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2894 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2896 IsUnpredictable);
2897
2898 // Use visitSwitchCase to actually insert the fast branch sequence for this
2899 // cond branch.
2900 visitSwitchCase(CB, BrMBB);
2901}
2902
2903/// visitSwitchCase - Emits the necessary code to represent a single node in
2904/// the binary search tree resulting from lowering a switch instruction.
2906 MachineBasicBlock *SwitchBB) {
2907 SDValue Cond;
2908 SDValue CondLHS = getValue(CB.CmpLHS);
2909 SDLoc dl = CB.DL;
2910
2911 if (CB.CC == ISD::SETTRUE) {
2912 // Branch or fall through to TrueBB.
2913 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2914 SwitchBB->normalizeSuccProbs();
2915 if (CB.TrueBB != NextBlock(SwitchBB)) {
2916 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2917 DAG.getBasicBlock(CB.TrueBB)));
2918 }
2919 return;
2920 }
2921
2922 auto &TLI = DAG.getTargetLoweringInfo();
2923 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2924
2925 // Build the setcc now.
2926 if (!CB.CmpMHS) {
2927 // Fold "(X == true)" to X and "(X == false)" to !X to
2928 // handle common cases produced by branch lowering.
2929 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2930 CB.CC == ISD::SETEQ)
2931 Cond = CondLHS;
2932 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2933 CB.CC == ISD::SETEQ) {
2934 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2935 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2936 } else {
2937 SDValue CondRHS = getValue(CB.CmpRHS);
2938
2939 // If a pointer's DAG type is larger than its memory type then the DAG
2940 // values are zero-extended. This breaks signed comparisons so truncate
2941 // back to the underlying type before doing the compare.
2942 if (CondLHS.getValueType() != MemVT) {
2943 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2944 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2945 }
2946 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2947 }
2948 } else {
2949 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2950
2951 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2952 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2953
2954 SDValue CmpOp = getValue(CB.CmpMHS);
2955 EVT VT = CmpOp.getValueType();
2956
2957 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2958 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2959 ISD::SETLE);
2960 } else {
2961 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2962 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2963 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2964 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2965 }
2966 }
2967
2968 // Update successor info
2969 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2970 // TrueBB and FalseBB are always different unless the incoming IR is
2971 // degenerate. This only happens when running llc on weird IR.
2972 if (CB.TrueBB != CB.FalseBB)
2973 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2974 SwitchBB->normalizeSuccProbs();
2975
2976 // If the lhs block is the next block, invert the condition so that we can
2977 // fall through to the lhs instead of the rhs block.
2978 if (CB.TrueBB == NextBlock(SwitchBB)) {
2979 std::swap(CB.TrueBB, CB.FalseBB);
2980 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2981 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2982 }
2983
2984 SDNodeFlags Flags;
2986 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2987 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2988
2989 setValue(CurInst, BrCond);
2990
2991 // Insert the false branch. Do this even if it's a fall through branch,
2992 // this makes it easier to do DAG optimizations which require inverting
2993 // the branch condition.
2994 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2995 DAG.getBasicBlock(CB.FalseBB));
2996
2997 DAG.setRoot(BrCond);
2998}
2999
3000/// visitJumpTable - Emit JumpTable node in the current MBB
3002 // Emit the code for the jump table
3003 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3004 assert(JT.Reg && "Should lower JT Header first!");
3005 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
3006 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
3007 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
3008 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
3009 Index.getValue(1), Table, Index);
3010 DAG.setRoot(BrJumpTable);
3011}
3012
3013/// visitJumpTableHeader - This function emits necessary code to produce index
3014/// in the JumpTable from switch case.
3016 JumpTableHeader &JTH,
3017 MachineBasicBlock *SwitchBB) {
3018 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3019 const SDLoc &dl = *JT.SL;
3020
3021 // Subtract the lowest switch case value from the value being switched on.
3022 SDValue SwitchOp = getValue(JTH.SValue);
3023 EVT VT = SwitchOp.getValueType();
3024 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
3025 DAG.getConstant(JTH.First, dl, VT));
3026
3027 // The SDNode we just created, which holds the value being switched on minus
3028 // the smallest case value, needs to be copied to a virtual register so it
3029 // can be used as an index into the jump table in a subsequent basic block.
3030 // This value may be smaller or larger than the target's pointer type, and
3031 // therefore require extension or truncating.
3032 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3033 SwitchOp =
3034 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
3035
3036 Register JumpTableReg =
3037 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
3038 SDValue CopyTo =
3039 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3040 JT.Reg = JumpTableReg;
3041
3042 if (!JTH.FallthroughUnreachable) {
3043 // Emit the range check for the jump table, and branch to the default block
3044 // for the switch statement if the value being switched on exceeds the
3045 // largest case in the switch.
3046 SDValue CMP = DAG.getSetCC(
3047 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3048 Sub.getValueType()),
3049 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3050
3051 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3052 MVT::Other, CopyTo, CMP,
3053 DAG.getBasicBlock(JT.Default));
3054
3055 // Avoid emitting unnecessary branches to the next block.
3056 if (JT.MBB != NextBlock(SwitchBB))
3057 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3058 DAG.getBasicBlock(JT.MBB));
3059
3060 DAG.setRoot(BrCond);
3061 } else {
3062 // Avoid emitting unnecessary branches to the next block.
3063 if (JT.MBB != NextBlock(SwitchBB))
3064 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3065 DAG.getBasicBlock(JT.MBB)));
3066 else
3067 DAG.setRoot(CopyTo);
3068 }
3069}
3070
3071/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3072/// variable if there exists one.
3074 SDValue &Chain) {
3075 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3076 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3077 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3079 Value *Global =
3082 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3083 if (Global) {
3084 MachinePointerInfo MPInfo(Global);
3088 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3089 DAG.setNodeMemRefs(Node, {MemRef});
3090 }
3091 if (PtrTy != PtrMemTy)
3092 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3093 return SDValue(Node, 0);
3094}
3095
3096/// Codegen a new tail for a stack protector check ParentMBB which has had its
3097/// tail spliced into a stack protector check success bb.
3098///
3099/// For a high level explanation of how this fits into the stack protector
3100/// generation see the comment on the declaration of class
3101/// StackProtectorDescriptor.
3103 MachineBasicBlock *ParentBB) {
3104
3105 // First create the loads to the guard/stack slot for the comparison.
3106 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3107 auto &DL = DAG.getDataLayout();
3108 EVT PtrTy = TLI.getFrameIndexTy(DL);
3109 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3110
3111 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3112 int FI = MFI.getStackProtectorIndex();
3113
3114 SDValue Guard;
3115 SDLoc dl = getCurSDLoc();
3116 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3117 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3118 Align Align = DL.getPrefTypeAlign(
3119 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3120
3121 // Generate code to load the content of the guard slot.
3122 SDValue GuardVal = DAG.getLoad(
3123 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3124 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3126
3127 if (TLI.useStackGuardXorFP())
3128 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3129
3130 // If we're using function-based instrumentation, call the guard check
3131 // function
3133 // Get the guard check function from the target and verify it exists since
3134 // we're using function-based instrumentation
3135 const Function *GuardCheckFn =
3136 TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3137 assert(GuardCheckFn && "Guard check function is null");
3138
3139 // The target provides a guard check function to validate the guard value.
3140 // Generate a call to that function with the content of the guard slot as
3141 // argument.
3142 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3143 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3144
3146 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3147 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3148 Entry.IsInReg = true;
3149 Args.push_back(Entry);
3150
3153 .setChain(DAG.getEntryNode())
3154 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3155 getValue(GuardCheckFn), std::move(Args));
3156
3157 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3158 DAG.setRoot(Result.second);
3159 return;
3160 }
3161
3162 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3163 // Otherwise, emit a volatile load to retrieve the stack guard value.
3164 SDValue Chain = DAG.getEntryNode();
3165 if (TLI.useLoadStackGuardNode(M)) {
3166 Guard = getLoadStackGuard(DAG, dl, Chain);
3167 } else {
3168 if (const Value *IRGuard = TLI.getSDagStackGuard(M, DAG.getLibcalls())) {
3169 SDValue GuardPtr = getValue(IRGuard);
3170 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3171 MachinePointerInfo(IRGuard, 0), Align,
3173 } else {
3174 LLVMContext &Ctx = *DAG.getContext();
3175 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
3176 Guard = DAG.getPOISON(PtrMemTy);
3177 }
3178 }
3179
3180 // Perform the comparison via a getsetcc.
3181 SDValue Cmp = DAG.getSetCC(
3182 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3183 Guard, GuardVal, ISD::SETNE);
3184
3185 // If the guard/stackslot do not equal, branch to failure MBB.
3186 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
3187 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3188 // Otherwise branch to success MBB.
3189 SDValue Br = DAG.getNode(ISD::BR, dl,
3190 MVT::Other, BrCond,
3191 DAG.getBasicBlock(SPD.getSuccessMBB()));
3192
3193 DAG.setRoot(Br);
3194}
3195
3196/// Codegen the failure basic block for a stack protector check.
3197///
3198/// A failure stack protector machine basic block consists simply of a call to
3199/// __stack_chk_fail().
3200///
3201/// For a high level explanation of how this fits into the stack protector
3202/// generation see the comment on the declaration of class
3203/// StackProtectorDescriptor.
3206
3207 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3208 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3209 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3210 SDValue Chain;
3211
3212 // For -Oz builds with a guard check function, we use function-based
3213 // instrumentation. Otherwise, if we have a guard check function, we call it
3214 // in the failure block.
3215 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3216 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3217 // First create the loads to the guard/stack slot for the comparison.
3218 auto &DL = DAG.getDataLayout();
3219 EVT PtrTy = TLI.getFrameIndexTy(DL);
3220 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3221
3222 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3223 int FI = MFI.getStackProtectorIndex();
3224
3225 SDLoc dl = getCurSDLoc();
3226 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3227 Align Align = DL.getPrefTypeAlign(
3228 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3229
3230 // Generate code to load the content of the guard slot.
3231 SDValue GuardVal = DAG.getLoad(
3232 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3233 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3235
3236 if (TLI.useStackGuardXorFP())
3237 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3238
3239 // The target provides a guard check function to validate the guard value.
3240 // Generate a call to that function with the content of the guard slot as
3241 // argument.
3242 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3243 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3244
3246 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3247 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3248 Entry.IsInReg = true;
3249 Args.push_back(Entry);
3250
3253 .setChain(DAG.getEntryNode())
3254 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3255 getValue(GuardCheckFn), std::move(Args));
3256
3257 Chain = TLI.LowerCallTo(CLI).second;
3258 } else {
3260 CallOptions.setDiscardResult(true);
3261 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3262 {}, CallOptions, getCurSDLoc())
3263 .second;
3264 }
3265
3266 // Emit a trap instruction if we are required to do so.
3267 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3268 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3269 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3270
3271 DAG.setRoot(Chain);
3272}
3273
3274/// visitBitTestHeader - This function emits necessary code to produce value
3275/// suitable for "bit tests"
3277 MachineBasicBlock *SwitchBB) {
3278 SDLoc dl = getCurSDLoc();
3279
3280 // Subtract the minimum value.
3281 SDValue SwitchOp = getValue(B.SValue);
3282 EVT VT = SwitchOp.getValueType();
3283 SDValue RangeSub =
3284 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3285
3286 // Determine the type of the test operands.
3287 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3288 bool UsePtrType = false;
3289 if (!TLI.isTypeLegal(VT)) {
3290 UsePtrType = true;
3291 } else {
3292 for (const BitTestCase &Case : B.Cases)
3293 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3294 // Switch table case range are encoded into series of masks.
3295 // Just use pointer type, it's guaranteed to fit.
3296 UsePtrType = true;
3297 break;
3298 }
3299 }
3300 SDValue Sub = RangeSub;
3301 if (UsePtrType) {
3302 VT = TLI.getPointerTy(DAG.getDataLayout());
3303 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3304 }
3305
3306 B.RegVT = VT.getSimpleVT();
3307 B.Reg = FuncInfo.CreateReg(B.RegVT);
3308 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3309
3310 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3311
3312 if (!B.FallthroughUnreachable)
3313 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3314 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3315 SwitchBB->normalizeSuccProbs();
3316
3317 SDValue Root = CopyTo;
3318 if (!B.FallthroughUnreachable) {
3319 // Conditional branch to the default block.
3320 SDValue RangeCmp = DAG.getSetCC(dl,
3321 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3322 RangeSub.getValueType()),
3323 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3324 ISD::SETUGT);
3325
3326 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3327 DAG.getBasicBlock(B.Default));
3328 }
3329
3330 // Avoid emitting unnecessary branches to the next block.
3331 if (MBB != NextBlock(SwitchBB))
3332 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3333
3334 DAG.setRoot(Root);
3335}
3336
3337/// visitBitTestCase - this function produces one "bit test"
3339 MachineBasicBlock *NextMBB,
3340 BranchProbability BranchProbToNext,
3341 Register Reg, BitTestCase &B,
3342 MachineBasicBlock *SwitchBB) {
3343 SDLoc dl = getCurSDLoc();
3344 MVT VT = BB.RegVT;
3345 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3346 SDValue Cmp;
3347 unsigned PopCount = llvm::popcount(B.Mask);
3348 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3349 if (PopCount == 1) {
3350 // Testing for a single bit; just compare the shift count with what it
3351 // would need to be to shift a 1 bit in that position.
3352 Cmp = DAG.getSetCC(
3353 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3354 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3355 ISD::SETEQ);
3356 } else if (PopCount == BB.Range) {
3357 // There is only one zero bit in the range, test for it directly.
3358 Cmp = DAG.getSetCC(
3359 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3360 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3361 } else {
3362 // Make desired shift
3363 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3364 DAG.getConstant(1, dl, VT), ShiftOp);
3365
3366 // Emit bit tests and jumps
3367 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3368 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3369 Cmp = DAG.getSetCC(
3370 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3371 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3372 }
3373
3374 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3375 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3376 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3377 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3378 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3379 // one as they are relative probabilities (and thus work more like weights),
3380 // and hence we need to normalize them to let the sum of them become one.
3381 SwitchBB->normalizeSuccProbs();
3382
3383 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3384 MVT::Other, getControlRoot(),
3385 Cmp, DAG.getBasicBlock(B.TargetBB));
3386
3387 // Avoid emitting unnecessary branches to the next block.
3388 if (NextMBB != NextBlock(SwitchBB))
3389 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3390 DAG.getBasicBlock(NextMBB));
3391
3392 DAG.setRoot(BrAnd);
3393}
3394
3395void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3396 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3397
3398 // Retrieve successors. Look through artificial IR level blocks like
3399 // catchswitch for successors.
3400 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3401 const BasicBlock *EHPadBB = I.getSuccessor(1);
3402 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3403
3404 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3405 // have to do anything here to lower funclet bundles.
3406 failForInvalidBundles(I, "invokes",
3412
3413 const Value *Callee(I.getCalledOperand());
3414 const Function *Fn = dyn_cast<Function>(Callee);
3415 if (isa<InlineAsm>(Callee))
3416 visitInlineAsm(I, EHPadBB);
3417 else if (Fn && Fn->isIntrinsic()) {
3418 switch (Fn->getIntrinsicID()) {
3419 default:
3420 llvm_unreachable("Cannot invoke this intrinsic");
3421 case Intrinsic::donothing:
3422 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3423 case Intrinsic::seh_try_begin:
3424 case Intrinsic::seh_scope_begin:
3425 case Intrinsic::seh_try_end:
3426 case Intrinsic::seh_scope_end:
3427 if (EHPadMBB)
3428 // a block referenced by EH table
3429 // so dtor-funclet not removed by opts
3430 EHPadMBB->setMachineBlockAddressTaken();
3431 break;
3432 case Intrinsic::experimental_patchpoint_void:
3433 case Intrinsic::experimental_patchpoint:
3434 visitPatchpoint(I, EHPadBB);
3435 break;
3436 case Intrinsic::experimental_gc_statepoint:
3438 break;
3439 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3440 // but these intrinsics are special because they can be invoked, so we
3441 // manually lower it to a DAG node here.
3442 case Intrinsic::wasm_throw: {
3444 std::array<SDValue, 4> Ops = {
3445 getControlRoot(), // inchain for the terminator node
3446 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3448 getValue(I.getArgOperand(0)), // tag
3449 getValue(I.getArgOperand(1)) // thrown value
3450 };
3451 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3452 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3453 break;
3454 }
3455 case Intrinsic::wasm_rethrow: {
3456 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3457 std::array<SDValue, 2> Ops = {
3458 getControlRoot(), // inchain for the terminator node
3459 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3460 TLI.getPointerTy(DAG.getDataLayout()))};
3461 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3462 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3463 break;
3464 }
3465 }
3466 } else if (I.hasDeoptState()) {
3467 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3468 // Eventually we will support lowering the @llvm.experimental.deoptimize
3469 // intrinsic, and right now there are no plans to support other intrinsics
3470 // with deopt state.
3471 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3472 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3474 } else {
3475 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3476 }
3477
3478 // If the value of the invoke is used outside of its defining block, make it
3479 // available as a virtual register.
3480 // We already took care of the exported value for the statepoint instruction
3481 // during call to the LowerStatepoint.
3482 if (!isa<GCStatepointInst>(I)) {
3484 }
3485
3487 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3488 BranchProbability EHPadBBProb =
3489 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3491 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3492
3493 // Update successor info.
3494 addSuccessorWithProb(InvokeMBB, Return);
3495 for (auto &UnwindDest : UnwindDests) {
3496 UnwindDest.first->setIsEHPad();
3497 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3498 }
3499 InvokeMBB->normalizeSuccProbs();
3500
3501 // Drop into normal successor.
3502 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3503 DAG.getBasicBlock(Return)));
3504}
3505
3506/// The intrinsics currently supported by callbr are implicit control flow
3507/// intrinsics such as amdgcn.kill.
3508/// - they should be called (no "dontcall-" attributes)
3509/// - they do not touch memory on the target (= !TLI.getTgtMemIntrinsic())
3510/// - they do not need custom argument handling (no
3511/// TLI.CollectTargetIntrinsicOperands())
3512void SelectionDAGBuilder::visitCallBrIntrinsic(const CallBrInst &I) {
3513#ifndef NDEBUG
3515 DAG.getTargetLoweringInfo().getTgtMemIntrinsic(
3516 Infos, I, DAG.getMachineFunction(), I.getIntrinsicID());
3517 assert(Infos.empty() && "Intrinsic touches memory");
3518#endif
3519
3520 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
3521
3523 getTargetIntrinsicOperands(I, HasChain, OnlyLoad);
3524 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
3525
3526 // Create the node.
3527 SDValue Result =
3528 getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
3529 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
3530
3531 setValue(&I, Result);
3532}
3533
3534void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3535 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3536
3537 if (I.isInlineAsm()) {
3538 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3539 // have to do anything here to lower funclet bundles.
3540 failForInvalidBundles(I, "callbrs",
3542 visitInlineAsm(I);
3543 } else {
3544 assert(!I.hasOperandBundles() &&
3545 "Can't have operand bundles for intrinsics");
3546 visitCallBrIntrinsic(I);
3547 }
3549
3550 // Retrieve successors.
3551 SmallPtrSet<BasicBlock *, 8> Dests;
3552 Dests.insert(I.getDefaultDest());
3553 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3554
3555 // Update successor info.
3556 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3557 // TODO: For most of the cases where there is an intrinsic callbr, we're
3558 // having exactly one indirect target, which will be unreachable. As soon as
3559 // this changes, we might need to enhance
3560 // Target->setIsInlineAsmBrIndirectTarget or add something similar for
3561 // intrinsic indirect branches.
3562 if (I.isInlineAsm()) {
3563 for (BasicBlock *Dest : I.getIndirectDests()) {
3564 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
3565 Target->setIsInlineAsmBrIndirectTarget();
3566 // If we introduce a type of asm goto statement that is permitted to use
3567 // an indirect call instruction to jump to its labels, then we should add
3568 // a call to Target->setMachineBlockAddressTaken() here, to mark the
3569 // target block as requiring a BTI.
3570
3571 Target->setLabelMustBeEmitted();
3572 // Don't add duplicate machine successors.
3573 if (Dests.insert(Dest).second)
3574 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3575 }
3576 }
3577 CallBrMBB->normalizeSuccProbs();
3578
3579 // Drop into default successor.
3580 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3581 MVT::Other, getControlRoot(),
3582 DAG.getBasicBlock(Return)));
3583}
3584
3585void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3586 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3587}
3588
3589void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3590 assert(FuncInfo.MBB->isEHPad() &&
3591 "Call to landingpad not in landing pad!");
3592
3593 // If there aren't registers to copy the values into (e.g., during SjLj
3594 // exceptions), then don't bother to create these DAG nodes.
3595 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3596 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3597 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3598 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3599 return;
3600
3601 // If landingpad's return type is token type, we don't create DAG nodes
3602 // for its exception pointer and selector value. The extraction of exception
3603 // pointer or selector value from token type landingpads is not currently
3604 // supported.
3605 if (LP.getType()->isTokenTy())
3606 return;
3607
3608 SmallVector<EVT, 2> ValueVTs;
3609 SDLoc dl = getCurSDLoc();
3610 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3611 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3612
3613 // Get the two live-in registers as SDValues. The physregs have already been
3614 // copied into virtual registers.
3615 SDValue Ops[2];
3616 if (FuncInfo.ExceptionPointerVirtReg) {
3617 Ops[0] = DAG.getZExtOrTrunc(
3618 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3619 FuncInfo.ExceptionPointerVirtReg,
3620 TLI.getPointerTy(DAG.getDataLayout())),
3621 dl, ValueVTs[0]);
3622 } else {
3623 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3624 }
3625 Ops[1] = DAG.getZExtOrTrunc(
3626 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3627 FuncInfo.ExceptionSelectorVirtReg,
3628 TLI.getPointerTy(DAG.getDataLayout())),
3629 dl, ValueVTs[1]);
3630
3631 // Merge into one.
3632 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3633 DAG.getVTList(ValueVTs), Ops);
3634 setValue(&LP, Res);
3635}
3636
3639 // Update JTCases.
3640 for (JumpTableBlock &JTB : SL->JTCases)
3641 if (JTB.first.HeaderBB == First)
3642 JTB.first.HeaderBB = Last;
3643
3644 // Update BitTestCases.
3645 for (BitTestBlock &BTB : SL->BitTestCases)
3646 if (BTB.Parent == First)
3647 BTB.Parent = Last;
3648}
3649
3650void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3651 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3652
3653 // Update machine-CFG edges with unique successors.
3655 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3656 BasicBlock *BB = I.getSuccessor(i);
3657 bool Inserted = Done.insert(BB).second;
3658 if (!Inserted)
3659 continue;
3660
3661 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3662 addSuccessorWithProb(IndirectBrMBB, Succ);
3663 }
3664 IndirectBrMBB->normalizeSuccProbs();
3665
3667 MVT::Other, getControlRoot(),
3668 getValue(I.getAddress())));
3669}
3670
3671void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3672 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3673 DAG.getTarget().Options.NoTrapAfterNoreturn))
3674 return;
3675
3676 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3677}
3678
3679void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3680 SDNodeFlags Flags;
3681 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3682 Flags.copyFMF(*FPOp);
3683
3684 SDValue Op = getValue(I.getOperand(0));
3685 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3686 Op, Flags);
3687 setValue(&I, UnNodeValue);
3688}
3689
3690void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3691 SDNodeFlags Flags;
3692 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3693 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3694 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3695 }
3696 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3697 Flags.setExact(ExactOp->isExact());
3698 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3699 Flags.setDisjoint(DisjointOp->isDisjoint());
3700 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3701 Flags.copyFMF(*FPOp);
3702
3703 SDValue Op1 = getValue(I.getOperand(0));
3704 SDValue Op2 = getValue(I.getOperand(1));
3705 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3706 Op1, Op2, Flags);
3707 setValue(&I, BinNodeValue);
3708}
3709
3710void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3711 SDValue Op1 = getValue(I.getOperand(0));
3712 SDValue Op2 = getValue(I.getOperand(1));
3713
3714 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3715 Op1.getValueType(), DAG.getDataLayout());
3716
3717 // Coerce the shift amount to the right type if we can. This exposes the
3718 // truncate or zext to optimization early.
3719 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3721 "Unexpected shift type");
3722 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3723 }
3724
3725 bool nuw = false;
3726 bool nsw = false;
3727 bool exact = false;
3728
3729 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3730
3731 if (const OverflowingBinaryOperator *OFBinOp =
3733 nuw = OFBinOp->hasNoUnsignedWrap();
3734 nsw = OFBinOp->hasNoSignedWrap();
3735 }
3736 if (const PossiblyExactOperator *ExactOp =
3738 exact = ExactOp->isExact();
3739 }
3740 SDNodeFlags Flags;
3741 Flags.setExact(exact);
3742 Flags.setNoSignedWrap(nsw);
3743 Flags.setNoUnsignedWrap(nuw);
3744 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3745 Flags);
3746 setValue(&I, Res);
3747}
3748
3749void SelectionDAGBuilder::visitSDiv(const User &I) {
3750 SDValue Op1 = getValue(I.getOperand(0));
3751 SDValue Op2 = getValue(I.getOperand(1));
3752
3753 SDNodeFlags Flags;
3754 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3755 cast<PossiblyExactOperator>(&I)->isExact());
3756 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3757 Op2, Flags));
3758}
3759
3760void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3761 ICmpInst::Predicate predicate = I.getPredicate();
3762 SDValue Op1 = getValue(I.getOperand(0));
3763 SDValue Op2 = getValue(I.getOperand(1));
3764 ISD::CondCode Opcode = getICmpCondCode(predicate);
3765
3766 auto &TLI = DAG.getTargetLoweringInfo();
3767 EVT MemVT =
3768 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3769
3770 // If a pointer's DAG type is larger than its memory type then the DAG values
3771 // are zero-extended. This breaks signed comparisons so truncate back to the
3772 // underlying type before doing the compare.
3773 if (Op1.getValueType() != MemVT) {
3774 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3775 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3776 }
3777
3778 SDNodeFlags Flags;
3779 Flags.setSameSign(I.hasSameSign());
3780
3781 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3782 I.getType());
3783 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode,
3784 /*Chain=*/{}, /*IsSignaling=*/false, Flags));
3785}
3786
3787void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3788 FCmpInst::Predicate predicate = I.getPredicate();
3789 SDValue Op1 = getValue(I.getOperand(0));
3790 SDValue Op2 = getValue(I.getOperand(1));
3791
3792 ISD::CondCode Condition = getFCmpCondCode(predicate);
3793 auto *FPMO = cast<FPMathOperator>(&I);
3794 if (FPMO->hasNoNaNs() ||
3795 (DAG.isKnownNeverNaN(Op1) && DAG.isKnownNeverNaN(Op2)))
3796 Condition = getFCmpCodeWithoutNaN(Condition);
3797
3798 SDNodeFlags Flags;
3799 Flags.copyFMF(*FPMO);
3800
3801 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3802 I.getType());
3803 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition,
3804 /*Chain=*/{}, /*IsSignaling=*/false, Flags));
3805}
3806
3807// Check if the condition of the select has one use or two users that are both
3808// selects with the same condition.
3809static bool hasOnlySelectUsers(const Value *Cond) {
3810 return llvm::all_of(Cond->users(), [](const Value *V) {
3811 return isa<SelectInst>(V);
3812 });
3813}
3814
3815void SelectionDAGBuilder::visitSelect(const User &I) {
3816 SmallVector<EVT, 4> ValueVTs;
3817 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3818 ValueVTs);
3819 unsigned NumValues = ValueVTs.size();
3820 if (NumValues == 0) return;
3821
3822 SmallVector<SDValue, 4> Values(NumValues);
3823 SDValue Cond = getValue(I.getOperand(0));
3824 SDValue LHSVal = getValue(I.getOperand(1));
3825 SDValue RHSVal = getValue(I.getOperand(2));
3826 SmallVector<SDValue, 1> BaseOps(1, Cond);
3828 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3829
3830 bool IsUnaryAbs = false;
3831 bool Negate = false;
3832
3833 SDNodeFlags Flags;
3834 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3835 Flags.copyFMF(*FPOp);
3836
3837 Flags.setUnpredictable(
3838 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3839
3840 // Min/max matching is only viable if all output VTs are the same.
3841 if (all_equal(ValueVTs)) {
3842 EVT VT = ValueVTs[0];
3843 LLVMContext &Ctx = *DAG.getContext();
3844 auto &TLI = DAG.getTargetLoweringInfo();
3845
3846 // We care about the legality of the operation after it has been type
3847 // legalized.
3848 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3849 VT = TLI.getTypeToTransformTo(Ctx, VT);
3850
3851 // If the vselect is legal, assume we want to leave this as a vector setcc +
3852 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3853 // min/max is legal on the scalar type.
3854 bool UseScalarMinMax = VT.isVector() &&
3856
3857 // ValueTracking's select pattern matching does not account for -0.0,
3858 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3859 // -0.0 is less than +0.0.
3860 const Value *LHS, *RHS;
3861 auto SPR = matchSelectPattern(&I, LHS, RHS);
3863 switch (SPR.Flavor) {
3864 case SPF_UMAX: Opc = ISD::UMAX; break;
3865 case SPF_UMIN: Opc = ISD::UMIN; break;
3866 case SPF_SMAX: Opc = ISD::SMAX; break;
3867 case SPF_SMIN: Opc = ISD::SMIN; break;
3868 case SPF_FMINNUM:
3870 break;
3871
3872 switch (SPR.NaNBehavior) {
3873 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3874 case SPNB_RETURNS_NAN: break;
3875 case SPNB_RETURNS_OTHER:
3877 Flags.setNoSignedZeros(true);
3878 break;
3879 case SPNB_RETURNS_ANY:
3881 (UseScalarMinMax &&
3883 Opc = ISD::FMINNUM;
3884 break;
3885 }
3886 break;
3887 case SPF_FMAXNUM:
3889 break;
3890
3891 switch (SPR.NaNBehavior) {
3892 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3893 case SPNB_RETURNS_NAN: break;
3894 case SPNB_RETURNS_OTHER:
3896 Flags.setNoSignedZeros(true);
3897 break;
3898 case SPNB_RETURNS_ANY:
3900 (UseScalarMinMax &&
3902 Opc = ISD::FMAXNUM;
3903 break;
3904 }
3905 break;
3906 case SPF_NABS:
3907 Negate = true;
3908 [[fallthrough]];
3909 case SPF_ABS:
3910 IsUnaryAbs = true;
3911 Opc = ISD::ABS;
3912 break;
3913 default: break;
3914 }
3915
3916 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3917 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3918 (UseScalarMinMax &&
3920 // If the underlying comparison instruction is used by any other
3921 // instruction, the consumed instructions won't be destroyed, so it is
3922 // not profitable to convert to a min/max.
3924 OpCode = Opc;
3925 LHSVal = getValue(LHS);
3926 RHSVal = getValue(RHS);
3927 BaseOps.clear();
3928 }
3929
3930 if (IsUnaryAbs) {
3931 OpCode = Opc;
3932 LHSVal = getValue(LHS);
3933 BaseOps.clear();
3934 }
3935 }
3936
3937 if (IsUnaryAbs) {
3938 for (unsigned i = 0; i != NumValues; ++i) {
3939 SDLoc dl = getCurSDLoc();
3940 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3941 Values[i] =
3942 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3943 if (Negate)
3944 Values[i] = DAG.getNegative(Values[i], dl, VT);
3945 }
3946 } else {
3947 for (unsigned i = 0; i != NumValues; ++i) {
3948 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3949 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3950 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3951 Values[i] = DAG.getNode(
3952 OpCode, getCurSDLoc(),
3953 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3954 }
3955 }
3956
3958 DAG.getVTList(ValueVTs), Values));
3959}
3960
3961void SelectionDAGBuilder::visitTrunc(const User &I) {
3962 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3963 SDValue N = getValue(I.getOperand(0));
3964 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3965 I.getType());
3966 SDNodeFlags Flags;
3967 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3968 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
3969 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
3970 }
3971
3972 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
3973}
3974
3975void SelectionDAGBuilder::visitZExt(const User &I) {
3976 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3977 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3978 SDValue N = getValue(I.getOperand(0));
3979 auto &TLI = DAG.getTargetLoweringInfo();
3980 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3981
3982 SDNodeFlags Flags;
3983 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3984 Flags.setNonNeg(PNI->hasNonNeg());
3985
3986 // Eagerly use nonneg information to canonicalize towards sign_extend if
3987 // that is the target's preference.
3988 // TODO: Let the target do this later.
3989 if (Flags.hasNonNeg() &&
3990 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3991 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3992 return;
3993 }
3994
3995 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3996}
3997
3998void SelectionDAGBuilder::visitSExt(const User &I) {
3999 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
4000 // SExt also can't be a cast to bool for same reason. So, nothing much to do
4001 SDValue N = getValue(I.getOperand(0));
4002 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4003 I.getType());
4004 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
4005}
4006
4007void SelectionDAGBuilder::visitFPTrunc(const User &I) {
4008 // FPTrunc is never a no-op cast, no need to check
4009 SDValue N = getValue(I.getOperand(0));
4010 SDLoc dl = getCurSDLoc();
4011 SDNodeFlags Flags;
4012 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4013 Flags.copyFMF(*FPOp);
4014 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4015 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4016 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
4017 DAG.getTargetConstant(
4018 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
4019 Flags));
4020}
4021
4022void SelectionDAGBuilder::visitFPExt(const User &I) {
4023 // FPExt is never a no-op cast, no need to check
4024 SDValue N = getValue(I.getOperand(0));
4025 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4026 I.getType());
4027 SDNodeFlags Flags;
4028 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4029 Flags.copyFMF(*FPOp);
4030 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N, Flags));
4031}
4032
4033void SelectionDAGBuilder::visitFPToUI(const User &I) {
4034 // FPToUI is never a no-op cast, no need to check
4035 SDValue N = getValue(I.getOperand(0));
4036 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4037 I.getType());
4038 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
4039}
4040
4041void SelectionDAGBuilder::visitFPToSI(const User &I) {
4042 // FPToSI is never a no-op cast, no need to check
4043 SDValue N = getValue(I.getOperand(0));
4044 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4045 I.getType());
4046 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
4047}
4048
4049void SelectionDAGBuilder::visitUIToFP(const User &I) {
4050 // UIToFP is never a no-op cast, no need to check
4051 SDValue N = getValue(I.getOperand(0));
4052 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4053 I.getType());
4054 SDNodeFlags Flags;
4055 Flags.setNonNeg(cast<PossiblyNonNegInst>(&I)->hasNonNeg());
4056 Flags.copyFMF(*cast<FPMathOperator>(&I));
4057
4058 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4059}
4060
4061void SelectionDAGBuilder::visitSIToFP(const User &I) {
4062 // SIToFP is never a no-op cast, no need to check
4063 SDValue N = getValue(I.getOperand(0));
4064 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4065 I.getType());
4066 SDNodeFlags Flags;
4067 Flags.copyFMF(*cast<FPMathOperator>(&I));
4068
4069 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4070}
4071
4072void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
4073 SDValue N = getValue(I.getOperand(0));
4074 // By definition the type of the ptrtoaddr must be equal to the address type.
4075 const auto &TLI = DAG.getTargetLoweringInfo();
4076 EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4077 // The address width must be smaller or equal to the pointer representation
4078 // width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
4079 N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
4080 setValue(&I, N);
4081}
4082
4083void SelectionDAGBuilder::visitPtrToInt(const User &I) {
4084 // What to do depends on the size of the integer and the size of the pointer.
4085 // We can either truncate, zero extend, or no-op, accordingly.
4086 SDValue N = getValue(I.getOperand(0));
4087 auto &TLI = DAG.getTargetLoweringInfo();
4088 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4089 I.getType());
4090 EVT PtrMemVT =
4091 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
4092 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4093 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
4094 setValue(&I, N);
4095}
4096
4097void SelectionDAGBuilder::visitIntToPtr(const User &I) {
4098 // What to do depends on the size of the integer and the size of the pointer.
4099 // We can either truncate, zero extend, or no-op, accordingly.
4100 SDValue N = getValue(I.getOperand(0));
4101 auto &TLI = DAG.getTargetLoweringInfo();
4102 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4103 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4104 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4105 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4106 setValue(&I, N);
4107}
4108
4109void SelectionDAGBuilder::visitBitCast(const User &I) {
4110 SDValue N = getValue(I.getOperand(0));
4111 SDLoc dl = getCurSDLoc();
4112 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4113 I.getType());
4114
4115 // BitCast assures us that source and destination are the same size so this is
4116 // either a BITCAST or a no-op.
4117 if (DestVT != N.getValueType())
4118 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
4119 DestVT, N)); // convert types.
4120 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4121 // might fold any kind of constant expression to an integer constant and that
4122 // is not what we are looking for. Only recognize a bitcast of a genuine
4123 // constant integer as an opaque constant.
4124 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4125 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4126 /*isOpaque*/true));
4127 else
4128 setValue(&I, N); // noop cast.
4129}
4130
4131void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4132 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4133 const Value *SV = I.getOperand(0);
4134 SDValue N = getValue(SV);
4135 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4136
4137 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4138 unsigned DestAS = I.getType()->getPointerAddressSpace();
4139
4140 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4141 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4142
4143 setValue(&I, N);
4144}
4145
4146void SelectionDAGBuilder::visitInsertElement(const User &I) {
4147 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4148 SDValue InVec = getValue(I.getOperand(0));
4149 SDValue InVal = getValue(I.getOperand(1));
4150 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4151 TLI.getVectorIdxTy(DAG.getDataLayout()));
4153 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4154 InVec, InVal, InIdx));
4155}
4156
4157void SelectionDAGBuilder::visitExtractElement(const User &I) {
4158 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4159 SDValue InVec = getValue(I.getOperand(0));
4160 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4161 TLI.getVectorIdxTy(DAG.getDataLayout()));
4163 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4164 InVec, InIdx));
4165}
4166
4167void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4168 SDValue Src1 = getValue(I.getOperand(0));
4169 SDValue Src2 = getValue(I.getOperand(1));
4170 ArrayRef<int> Mask;
4171 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4172 Mask = SVI->getShuffleMask();
4173 else
4174 Mask = cast<ConstantExpr>(I).getShuffleMask();
4175 SDLoc DL = getCurSDLoc();
4176 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4177 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4178 EVT SrcVT = Src1.getValueType();
4179
4180 if (all_of(Mask, equal_to(0)) && VT.isScalableVector()) {
4181 // Canonical splat form of first element of first input vector.
4182 SDValue FirstElt =
4183 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4184 DAG.getVectorIdxConstant(0, DL));
4185 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4186 return;
4187 }
4188
4189 // For now, we only handle splats for scalable vectors.
4190 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4191 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4192 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4193
4194 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4195 unsigned MaskNumElts = Mask.size();
4196
4197 if (SrcNumElts == MaskNumElts) {
4198 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4199 return;
4200 }
4201
4202 // Normalize the shuffle vector since mask and vector length don't match.
4203 if (SrcNumElts < MaskNumElts) {
4204 // Mask is longer than the source vectors. We can use concatenate vector to
4205 // make the mask and vectors lengths match.
4206
4207 if (MaskNumElts % SrcNumElts == 0) {
4208 // Mask length is a multiple of the source vector length.
4209 // Check if the shuffle is some kind of concatenation of the input
4210 // vectors.
4211 unsigned NumConcat = MaskNumElts / SrcNumElts;
4212 bool IsConcat = true;
4213 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4214 for (unsigned i = 0; i != MaskNumElts; ++i) {
4215 int Idx = Mask[i];
4216 if (Idx < 0)
4217 continue;
4218 // Ensure the indices in each SrcVT sized piece are sequential and that
4219 // the same source is used for the whole piece.
4220 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4221 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4222 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4223 IsConcat = false;
4224 break;
4225 }
4226 // Remember which source this index came from.
4227 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4228 }
4229
4230 // The shuffle is concatenating multiple vectors together. Just emit
4231 // a CONCAT_VECTORS operation.
4232 if (IsConcat) {
4233 SmallVector<SDValue, 8> ConcatOps;
4234 for (auto Src : ConcatSrcs) {
4235 if (Src < 0)
4236 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4237 else if (Src == 0)
4238 ConcatOps.push_back(Src1);
4239 else
4240 ConcatOps.push_back(Src2);
4241 }
4242 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4243 return;
4244 }
4245 }
4246
4247 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4248 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4249 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4250 PaddedMaskNumElts);
4251
4252 // Pad both vectors with undefs to make them the same length as the mask.
4253 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4254
4255 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4256 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4257 MOps1[0] = Src1;
4258 MOps2[0] = Src2;
4259
4260 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4261 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4262
4263 // Readjust mask for new input vector length.
4264 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4265 for (unsigned i = 0; i != MaskNumElts; ++i) {
4266 int Idx = Mask[i];
4267 if (Idx >= (int)SrcNumElts)
4268 Idx -= SrcNumElts - PaddedMaskNumElts;
4269 MappedOps[i] = Idx;
4270 }
4271
4272 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4273
4274 // If the concatenated vector was padded, extract a subvector with the
4275 // correct number of elements.
4276 if (MaskNumElts != PaddedMaskNumElts)
4277 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4278 DAG.getVectorIdxConstant(0, DL));
4279
4280 setValue(&I, Result);
4281 return;
4282 }
4283
4284 assert(SrcNumElts > MaskNumElts);
4285
4286 // Analyze the access pattern of the vector to see if we can extract
4287 // two subvectors and do the shuffle.
4288 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4289 bool CanExtract = true;
4290 for (int Idx : Mask) {
4291 unsigned Input = 0;
4292 if (Idx < 0)
4293 continue;
4294
4295 if (Idx >= (int)SrcNumElts) {
4296 Input = 1;
4297 Idx -= SrcNumElts;
4298 }
4299
4300 // If all the indices come from the same MaskNumElts sized portion of
4301 // the sources we can use extract. Also make sure the extract wouldn't
4302 // extract past the end of the source.
4303 int NewStartIdx = alignDown(Idx, MaskNumElts);
4304 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4305 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4306 CanExtract = false;
4307 // Make sure we always update StartIdx as we use it to track if all
4308 // elements are undef.
4309 StartIdx[Input] = NewStartIdx;
4310 }
4311
4312 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4313 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4314 return;
4315 }
4316 if (CanExtract) {
4317 // Extract appropriate subvector and generate a vector shuffle
4318 for (unsigned Input = 0; Input < 2; ++Input) {
4319 SDValue &Src = Input == 0 ? Src1 : Src2;
4320 if (StartIdx[Input] < 0)
4321 Src = DAG.getUNDEF(VT);
4322 else {
4323 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4324 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4325 }
4326 }
4327
4328 // Calculate new mask.
4329 SmallVector<int, 8> MappedOps(Mask);
4330 for (int &Idx : MappedOps) {
4331 if (Idx >= (int)SrcNumElts)
4332 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4333 else if (Idx >= 0)
4334 Idx -= StartIdx[0];
4335 }
4336
4337 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4338 return;
4339 }
4340
4341 // We can't use either concat vectors or extract subvectors so fall back to
4342 // replacing the shuffle with extract and build vector.
4343 // to insert and build vector.
4344 EVT EltVT = VT.getVectorElementType();
4346 for (int Idx : Mask) {
4347 SDValue Res;
4348
4349 if (Idx < 0) {
4350 Res = DAG.getUNDEF(EltVT);
4351 } else {
4352 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4353 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4354
4355 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4356 DAG.getVectorIdxConstant(Idx, DL));
4357 }
4358
4359 Ops.push_back(Res);
4360 }
4361
4362 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4363}
4364
4365void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4366 ArrayRef<unsigned> Indices = I.getIndices();
4367 const Value *Op0 = I.getOperand(0);
4368 const Value *Op1 = I.getOperand(1);
4369 Type *AggTy = I.getType();
4370 Type *ValTy = Op1->getType();
4371 bool IntoUndef = isa<UndefValue>(Op0);
4372 bool FromUndef = isa<UndefValue>(Op1);
4373
4374 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4375
4376 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4377 SmallVector<EVT, 4> AggValueVTs;
4378 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4379 SmallVector<EVT, 4> ValValueVTs;
4380 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4381
4382 unsigned NumAggValues = AggValueVTs.size();
4383 unsigned NumValValues = ValValueVTs.size();
4384 SmallVector<SDValue, 4> Values(NumAggValues);
4385
4386 // Ignore an insertvalue that produces an empty object
4387 if (!NumAggValues) {
4388 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4389 return;
4390 }
4391
4392 SDValue Agg = getValue(Op0);
4393 unsigned i = 0;
4394 // Copy the beginning value(s) from the original aggregate.
4395 for (; i != LinearIndex; ++i)
4396 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4397 SDValue(Agg.getNode(), Agg.getResNo() + i);
4398 // Copy values from the inserted value(s).
4399 if (NumValValues) {
4400 SDValue Val = getValue(Op1);
4401 for (; i != LinearIndex + NumValValues; ++i)
4402 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4403 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4404 }
4405 // Copy remaining value(s) from the original aggregate.
4406 for (; i != NumAggValues; ++i)
4407 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4408 SDValue(Agg.getNode(), Agg.getResNo() + i);
4409
4411 DAG.getVTList(AggValueVTs), Values));
4412}
4413
4414void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4415 ArrayRef<unsigned> Indices = I.getIndices();
4416 const Value *Op0 = I.getOperand(0);
4417 Type *AggTy = Op0->getType();
4418 Type *ValTy = I.getType();
4419 bool OutOfUndef = isa<UndefValue>(Op0);
4420
4421 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4422
4423 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4424 SmallVector<EVT, 4> ValValueVTs;
4425 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4426
4427 unsigned NumValValues = ValValueVTs.size();
4428
4429 // Ignore a extractvalue that produces an empty object
4430 if (!NumValValues) {
4431 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4432 return;
4433 }
4434
4435 SmallVector<SDValue, 4> Values(NumValValues);
4436
4437 SDValue Agg = getValue(Op0);
4438 // Copy out the selected value(s).
4439 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4440 Values[i - LinearIndex] =
4441 OutOfUndef ?
4442 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4443 SDValue(Agg.getNode(), Agg.getResNo() + i);
4444
4446 DAG.getVTList(ValValueVTs), Values));
4447}
4448
4449void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4450 Value *Op0 = I.getOperand(0);
4451 // Note that the pointer operand may be a vector of pointers. Take the scalar
4452 // element which holds a pointer.
4453 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4454 SDValue N = getValue(Op0);
4455 SDLoc dl = getCurSDLoc();
4456 auto &TLI = DAG.getTargetLoweringInfo();
4457 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4458
4459 // For a vector GEP, keep the prefix scalar as long as possible, then
4460 // convert any scalars encountered after the first vector operand to vectors.
4461 bool IsVectorGEP = I.getType()->isVectorTy();
4462 ElementCount VectorElementCount =
4463 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4465
4467 GTI != E; ++GTI) {
4468 const Value *Idx = GTI.getOperand();
4469 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4470 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4471 if (Field) {
4472 // N = N + Offset
4473 uint64_t Offset =
4474 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4475
4476 // In an inbounds GEP with an offset that is nonnegative even when
4477 // interpreted as signed, assume there is no unsigned overflow.
4478 SDNodeFlags Flags;
4479 if (NW.hasNoUnsignedWrap() ||
4480 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4482 Flags.setInBounds(NW.isInBounds());
4483
4484 N = DAG.getMemBasePlusOffset(
4485 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4486 }
4487 } else {
4488 // IdxSize is the width of the arithmetic according to IR semantics.
4489 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4490 // (and fix up the result later).
4491 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4492 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4493 TypeSize ElementSize =
4494 GTI.getSequentialElementStride(DAG.getDataLayout());
4495 // We intentionally mask away the high bits here; ElementSize may not
4496 // fit in IdxTy.
4497 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4498 /*isSigned=*/false, /*implicitTrunc=*/true);
4499 bool ElementScalable = ElementSize.isScalable();
4500
4501 // If this is a scalar constant or a splat vector of constants,
4502 // handle it quickly.
4503 const auto *C = dyn_cast<Constant>(Idx);
4504 if (C && isa<VectorType>(C->getType()))
4505 C = C->getSplatValue();
4506
4507 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4508 if (CI && CI->isZero())
4509 continue;
4510 if (CI && !ElementScalable) {
4511 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4512 LLVMContext &Context = *DAG.getContext();
4513 SDValue OffsVal;
4514 if (N.getValueType().isVector())
4515 OffsVal = DAG.getConstant(
4516 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4517 else
4518 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4519
4520 // In an inbounds GEP with an offset that is nonnegative even when
4521 // interpreted as signed, assume there is no unsigned overflow.
4522 SDNodeFlags Flags;
4523 if (NW.hasNoUnsignedWrap() ||
4524 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4525 Flags.setNoUnsignedWrap(true);
4526 Flags.setInBounds(NW.isInBounds());
4527
4528 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4529
4530 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4531 continue;
4532 }
4533
4534 // N = N + Idx * ElementMul;
4535 SDValue IdxN = getValue(Idx);
4536
4537 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4538 if (N.getValueType().isVector()) {
4539 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4540 VectorElementCount);
4541 IdxN = DAG.getSplat(VT, dl, IdxN);
4542 } else {
4543 EVT VT =
4544 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4545 N = DAG.getSplat(VT, dl, N);
4546 }
4547 }
4548
4549 // If the index is smaller or larger than intptr_t, truncate or extend
4550 // it.
4551 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4552
4553 SDNodeFlags ScaleFlags;
4554 // The multiplication of an index by the type size does not wrap the
4555 // pointer index type in a signed sense (mul nsw).
4557
4558 // The multiplication of an index by the type size does not wrap the
4559 // pointer index type in an unsigned sense (mul nuw).
4560 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4561
4562 if (ElementScalable) {
4563 EVT VScaleTy = N.getValueType().getScalarType();
4564 SDValue VScale = DAG.getNode(
4565 ISD::VSCALE, dl, VScaleTy,
4566 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4567 if (N.getValueType().isVector())
4568 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4569 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4570 ScaleFlags);
4571 } else {
4572 // If this is a multiply by a power of two, turn it into a shl
4573 // immediately. This is a very common case.
4574 if (ElementMul != 1) {
4575 if (ElementMul.isPowerOf2()) {
4576 unsigned Amt = ElementMul.logBase2();
4577 IdxN = DAG.getNode(
4578 ISD::SHL, dl, N.getValueType(), IdxN,
4579 DAG.getShiftAmountConstant(Amt, N.getValueType(), dl),
4580 ScaleFlags);
4581 } else {
4582 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4583 IdxN.getValueType());
4584 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4585 ScaleFlags);
4586 }
4587 }
4588 }
4589
4590 // The successive addition of the current address, truncated to the
4591 // pointer index type and interpreted as an unsigned number, and each
4592 // offset, also interpreted as an unsigned number, does not wrap the
4593 // pointer index type (add nuw).
4594 SDNodeFlags AddFlags;
4595 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4596 AddFlags.setInBounds(NW.isInBounds());
4597
4598 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4599 }
4600 }
4601
4602 if (IsVectorGEP && !N.getValueType().isVector()) {
4603 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4604 N = DAG.getSplat(VT, dl, N);
4605 }
4606
4607 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4608 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4609 if (IsVectorGEP) {
4610 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4611 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4612 }
4613
4614 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4615 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4616
4617 setValue(&I, N);
4618}
4619
4620void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4621 // If this is a fixed sized alloca in the entry block of the function,
4622 // allocate it statically on the stack.
4623 if (FuncInfo.StaticAllocaMap.count(&I))
4624 return; // getValue will auto-populate this.
4625
4626 SDLoc dl = getCurSDLoc();
4627 Type *Ty = I.getAllocatedType();
4628 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4629 auto &DL = DAG.getDataLayout();
4630 TypeSize TySize = DL.getTypeAllocSize(Ty);
4631 MaybeAlign Alignment = I.getAlign();
4632
4633 SDValue AllocSize = getValue(I.getArraySize());
4634
4635 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4636 if (AllocSize.getValueType() != IntPtr)
4637 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4638
4639 AllocSize = DAG.getNode(
4640 ISD::MUL, dl, IntPtr, AllocSize,
4641 DAG.getZExtOrTrunc(DAG.getTypeSize(dl, MVT::i64, TySize), dl, IntPtr));
4642
4643 // Handle alignment. If the requested alignment is less than or equal to
4644 // the stack alignment, ignore it. If the size is greater than or equal to
4645 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4646 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4647 if (*Alignment <= StackAlign)
4648 Alignment = std::nullopt;
4649
4650 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4651 // Round the size of the allocation up to the stack alignment size
4652 // by add SA-1 to the size. This doesn't overflow because we're computing
4653 // an address inside an alloca.
4654 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4655 DAG.getConstant(StackAlignMask, dl, IntPtr),
4657
4658 // Mask out the low bits for alignment purposes.
4659 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4660 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4661
4662 SDValue Ops[] = {
4663 getRoot(), AllocSize,
4664 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4665 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4666 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4667 setValue(&I, DSA);
4668 DAG.setRoot(DSA.getValue(1));
4669
4670 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4671}
4672
4673static const MDNode *getRangeMetadata(const Instruction &I) {
4674 return I.getMetadata(LLVMContext::MD_range);
4675}
4676
4677static std::optional<ConstantRange> getRange(const Instruction &I) {
4678 if (const auto *CB = dyn_cast<CallBase>(&I))
4679 if (std::optional<ConstantRange> CR = CB->getRange())
4680 return CR;
4681 if (const MDNode *Range = getRangeMetadata(I))
4683 return std::nullopt;
4684}
4685
4687 if (const auto *CB = dyn_cast<CallBase>(&I))
4688 return CB->getRetNoFPClass();
4689 return fcNone;
4690}
4691
4692void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4693 if (I.isAtomic())
4694 return visitAtomicLoad(I);
4695
4696 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4697 const Value *SV = I.getOperand(0);
4698 if (TLI.supportSwiftError()) {
4699 // Swifterror values can come from either a function parameter with
4700 // swifterror attribute or an alloca with swifterror attribute.
4701 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4702 if (Arg->hasSwiftErrorAttr())
4703 return visitLoadFromSwiftError(I);
4704 }
4705
4706 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4707 if (Alloca->isSwiftError())
4708 return visitLoadFromSwiftError(I);
4709 }
4710 }
4711
4712 SDValue Ptr = getValue(SV);
4713
4714 Type *Ty = I.getType();
4715 SmallVector<EVT, 4> ValueVTs, MemVTs;
4717 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4718 unsigned NumValues = ValueVTs.size();
4719 if (NumValues == 0)
4720 return;
4721
4722 Align Alignment = I.getAlign();
4723 AAMDNodes AAInfo = I.getAAMetadata();
4724 const MDNode *Ranges = getRangeMetadata(I);
4725 bool isVolatile = I.isVolatile();
4726 MachineMemOperand::Flags MMOFlags =
4727 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4728
4729 SDValue Root;
4730 bool ConstantMemory = false;
4731 if (isVolatile)
4732 // Serialize volatile loads with other side effects.
4733 Root = getRoot();
4734 else if (NumValues > MaxParallelChains)
4735 Root = getMemoryRoot();
4736 else if (BatchAA &&
4737 BatchAA->pointsToConstantMemory(MemoryLocation(
4738 SV,
4739 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4740 AAInfo))) {
4741 // Do not serialize (non-volatile) loads of constant memory with anything.
4742 Root = DAG.getEntryNode();
4743 ConstantMemory = true;
4745 } else {
4746 // Do not serialize non-volatile loads against each other.
4747 Root = DAG.getRoot();
4748 }
4749
4750 SDLoc dl = getCurSDLoc();
4751
4752 if (isVolatile)
4753 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4754
4755 SmallVector<SDValue, 4> Values(NumValues);
4756 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4757
4758 unsigned ChainI = 0;
4759 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4760 // Serializing loads here may result in excessive register pressure, and
4761 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4762 // could recover a bit by hoisting nodes upward in the chain by recognizing
4763 // they are side-effect free or do not alias. The optimizer should really
4764 // avoid this case by converting large object/array copies to llvm.memcpy
4765 // (MaxParallelChains should always remain as failsafe).
4766 if (ChainI == MaxParallelChains) {
4767 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4768 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4769 ArrayRef(Chains.data(), ChainI));
4770 Root = Chain;
4771 ChainI = 0;
4772 }
4773
4774 // TODO: MachinePointerInfo only supports a fixed length offset.
4775 MachinePointerInfo PtrInfo =
4776 !Offsets[i].isScalable() || Offsets[i].isZero()
4777 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4778 : MachinePointerInfo();
4779
4780 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4781 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4782 MMOFlags, AAInfo, Ranges);
4783 Chains[ChainI] = L.getValue(1);
4784
4785 if (MemVTs[i] != ValueVTs[i])
4786 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4787
4788 if (MDNode *NoFPClassMD = I.getMetadata(LLVMContext::MD_nofpclass)) {
4789 uint64_t FPTestInt =
4790 cast<ConstantInt>(
4791 cast<ConstantAsMetadata>(NoFPClassMD->getOperand(0))->getValue())
4792 ->getZExtValue();
4793 if (FPTestInt != fcNone) {
4794 SDValue FPTestConst =
4795 DAG.getTargetConstant(FPTestInt, SDLoc(), MVT::i32);
4796 L = DAG.getNode(ISD::AssertNoFPClass, dl, L.getValueType(), L,
4797 FPTestConst);
4798 }
4799 }
4800 Values[i] = L;
4801 }
4802
4803 if (!ConstantMemory) {
4804 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4805 ArrayRef(Chains.data(), ChainI));
4806 if (isVolatile)
4807 DAG.setRoot(Chain);
4808 else
4809 PendingLoads.push_back(Chain);
4810 }
4811
4812 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4813 DAG.getVTList(ValueVTs), Values));
4814}
4815
4816void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4817 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4818 "call visitStoreToSwiftError when backend supports swifterror");
4819
4820 SmallVector<EVT, 4> ValueVTs;
4821 SmallVector<uint64_t, 4> Offsets;
4822 const Value *SrcV = I.getOperand(0);
4823 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4824 SrcV->getType(), ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4825 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4826 "expect a single EVT for swifterror");
4827
4828 SDValue Src = getValue(SrcV);
4829 // Create a virtual register, then update the virtual register.
4830 Register VReg =
4831 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4832 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4833 // Chain can be getRoot or getControlRoot.
4834 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4835 SDValue(Src.getNode(), Src.getResNo()));
4836 DAG.setRoot(CopyNode);
4837}
4838
4839void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4840 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4841 "call visitLoadFromSwiftError when backend supports swifterror");
4842
4843 assert(!I.isVolatile() &&
4844 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4845 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4846 "Support volatile, non temporal, invariant for load_from_swift_error");
4847
4848 const Value *SV = I.getOperand(0);
4849 Type *Ty = I.getType();
4850 assert(
4851 (!BatchAA ||
4852 !BatchAA->pointsToConstantMemory(MemoryLocation(
4853 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4854 I.getAAMetadata()))) &&
4855 "load_from_swift_error should not be constant memory");
4856
4857 SmallVector<EVT, 4> ValueVTs;
4858 SmallVector<uint64_t, 4> Offsets;
4859 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4860 ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4861 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4862 "expect a single EVT for swifterror");
4863
4864 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4865 SDValue L = DAG.getCopyFromReg(
4866 getRoot(), getCurSDLoc(),
4867 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4868
4869 setValue(&I, L);
4870}
4871
4872void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4873 if (I.isAtomic())
4874 return visitAtomicStore(I);
4875
4876 const Value *SrcV = I.getOperand(0);
4877 const Value *PtrV = I.getOperand(1);
4878
4879 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4880 if (TLI.supportSwiftError()) {
4881 // Swifterror values can come from either a function parameter with
4882 // swifterror attribute or an alloca with swifterror attribute.
4883 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4884 if (Arg->hasSwiftErrorAttr())
4885 return visitStoreToSwiftError(I);
4886 }
4887
4888 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4889 if (Alloca->isSwiftError())
4890 return visitStoreToSwiftError(I);
4891 }
4892 }
4893
4894 SmallVector<EVT, 4> ValueVTs, MemVTs;
4896 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4897 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4898 unsigned NumValues = ValueVTs.size();
4899 if (NumValues == 0)
4900 return;
4901
4902 // Get the lowered operands. Note that we do this after
4903 // checking if NumResults is zero, because with zero results
4904 // the operands won't have values in the map.
4905 SDValue Src = getValue(SrcV);
4906 SDValue Ptr = getValue(PtrV);
4907
4908 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4909 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4910 SDLoc dl = getCurSDLoc();
4911 Align Alignment = I.getAlign();
4912 AAMDNodes AAInfo = I.getAAMetadata();
4913
4914 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4915
4916 unsigned ChainI = 0;
4917 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4918 // See visitLoad comments.
4919 if (ChainI == MaxParallelChains) {
4920 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4921 ArrayRef(Chains.data(), ChainI));
4922 Root = Chain;
4923 ChainI = 0;
4924 }
4925
4926 // TODO: MachinePointerInfo only supports a fixed length offset.
4927 MachinePointerInfo PtrInfo =
4928 !Offsets[i].isScalable() || Offsets[i].isZero()
4929 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4930 : MachinePointerInfo();
4931
4932 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4933 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4934 if (MemVTs[i] != ValueVTs[i])
4935 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4936 SDValue St =
4937 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4938 Chains[ChainI] = St;
4939 }
4940
4941 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4942 ArrayRef(Chains.data(), ChainI));
4943 setValue(&I, StoreNode);
4944 DAG.setRoot(StoreNode);
4945}
4946
4947void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4948 bool IsCompressing) {
4949 SDLoc sdl = getCurSDLoc();
4950
4951 Value *Src0Operand = I.getArgOperand(0);
4952 Value *PtrOperand = I.getArgOperand(1);
4953 Value *MaskOperand = I.getArgOperand(2);
4954 Align Alignment = I.getParamAlign(1).valueOrOne();
4955
4956 SDValue Ptr = getValue(PtrOperand);
4957 SDValue Src0 = getValue(Src0Operand);
4958 SDValue Mask = getValue(MaskOperand);
4959 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4960
4961 EVT VT = Src0.getValueType();
4962
4963 auto MMOFlags = MachineMemOperand::MOStore;
4964 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4966
4967 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4968 MachinePointerInfo(PtrOperand), MMOFlags,
4969 LocationSize::upperBound(VT.getStoreSize()), Alignment,
4970 I.getAAMetadata());
4971
4972 const auto &TLI = DAG.getTargetLoweringInfo();
4973
4974 SDValue StoreNode =
4975 !IsCompressing && TTI->hasConditionalLoadStoreForType(
4976 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4977 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4978 Mask)
4979 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4980 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4981 IsCompressing);
4982 DAG.setRoot(StoreNode);
4983 setValue(&I, StoreNode);
4984}
4985
4986// Get a uniform base for the Gather/Scatter intrinsic.
4987// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4988// We try to represent it as a base pointer + vector of indices.
4989// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4990// The first operand of the GEP may be a single pointer or a vector of pointers
4991// Example:
4992// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4993// or
4994// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4995// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4996//
4997// When the first GEP operand is a single pointer - it is the uniform base we
4998// are looking for. If first operand of the GEP is a splat vector - we
4999// extract the splat value and use it as a uniform base.
5000// In all other cases the function returns 'false'.
5001static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
5002 SDValue &Scale, SelectionDAGBuilder *SDB,
5003 const BasicBlock *CurBB, uint64_t ElemSize) {
5004 SelectionDAG& DAG = SDB->DAG;
5005 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5006 const DataLayout &DL = DAG.getDataLayout();
5007
5008 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
5009
5010 // Handle splat constant pointer.
5011 if (auto *C = dyn_cast<Constant>(Ptr)) {
5012 C = C->getSplatValue();
5013 if (!C)
5014 return false;
5015
5016 Base = SDB->getValue(C);
5017
5018 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
5019 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
5020 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
5021 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
5022 return true;
5023 }
5024
5026 if (!GEP || GEP->getParent() != CurBB)
5027 return false;
5028
5029 if (GEP->getNumOperands() != 2)
5030 return false;
5031
5032 const Value *BasePtr = GEP->getPointerOperand();
5033 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
5034
5035 // Make sure the base is scalar and the index is a vector.
5036 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
5037 return false;
5038
5039 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
5040 if (ScaleVal.isScalable())
5041 return false;
5042
5043 // Target may not support the required addressing mode.
5044 if (ScaleVal != 1 &&
5045 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
5046 return false;
5047
5048 Base = SDB->getValue(BasePtr);
5049 Index = SDB->getValue(IndexVal);
5050
5051 Scale =
5052 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
5053 return true;
5054}
5055
5056void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
5057 SDLoc sdl = getCurSDLoc();
5058
5059 // llvm.masked.scatter.*(Src0, Ptrs, Mask)
5060 const Value *Ptr = I.getArgOperand(1);
5061 SDValue Src0 = getValue(I.getArgOperand(0));
5062 SDValue Mask = getValue(I.getArgOperand(2));
5063 EVT VT = Src0.getValueType();
5064 Align Alignment = I.getParamAlign(1).valueOrOne();
5065 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5066
5067 SDValue Base;
5068 SDValue Index;
5069 SDValue Scale;
5070 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5071 I.getParent(), VT.getScalarStoreSize());
5072
5073 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5074 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5075 MachinePointerInfo(AS), MachineMemOperand::MOStore,
5076 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
5077 if (!UniformBase) {
5078 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5079 Index = getValue(Ptr);
5080 Scale =
5081 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5082 }
5083
5084 EVT IdxVT = Index.getValueType();
5085 EVT EltTy = IdxVT.getVectorElementType();
5086 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5087 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5088 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5089 }
5090
5091 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
5092 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
5093 Ops, MMO, ISD::SIGNED_SCALED, false);
5094 DAG.setRoot(Scatter);
5095 setValue(&I, Scatter);
5096}
5097
5098void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5099 SDLoc sdl = getCurSDLoc();
5100
5101 Value *PtrOperand = I.getArgOperand(0);
5102 Value *MaskOperand = I.getArgOperand(1);
5103 Value *Src0Operand = I.getArgOperand(2);
5104 Align Alignment = I.getParamAlign(0).valueOrOne();
5105
5106 SDValue Ptr = getValue(PtrOperand);
5107 SDValue Src0 = getValue(Src0Operand);
5108 SDValue Mask = getValue(MaskOperand);
5109 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5110
5111 EVT VT = Src0.getValueType();
5112 AAMDNodes AAInfo = I.getAAMetadata();
5113 const MDNode *Ranges = getRangeMetadata(I);
5114
5115 // Do not serialize masked loads of constant memory with anything.
5116 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5117 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5118
5119 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5120
5121 auto MMOFlags = MachineMemOperand::MOLoad;
5122 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5124 if (I.hasMetadata(LLVMContext::MD_invariant_load))
5126
5127 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5128 MachinePointerInfo(PtrOperand), MMOFlags,
5129 LocationSize::upperBound(VT.getStoreSize()), Alignment, AAInfo, Ranges);
5130
5131 const auto &TLI = DAG.getTargetLoweringInfo();
5132
5133 // The Load/Res may point to different values and both of them are output
5134 // variables.
5135 SDValue Load;
5136 SDValue Res;
5137 if (!IsExpanding &&
5138 TTI->hasConditionalLoadStoreForType(Src0Operand->getType(),
5139 /*IsStore=*/false))
5140 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5141 else
5142 Res = Load =
5143 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5144 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5145 if (AddToChain)
5146 PendingLoads.push_back(Load.getValue(1));
5147 setValue(&I, Res);
5148}
5149
5150void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5151 SDLoc sdl = getCurSDLoc();
5152
5153 // @llvm.masked.gather.*(Ptrs, Mask, Src0)
5154 const Value *Ptr = I.getArgOperand(0);
5155 SDValue Src0 = getValue(I.getArgOperand(2));
5156 SDValue Mask = getValue(I.getArgOperand(1));
5157
5158 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5159 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5160 Align Alignment = I.getParamAlign(0).valueOrOne();
5161
5162 const MDNode *Ranges = getRangeMetadata(I);
5163
5164 SDValue Root = DAG.getRoot();
5165 SDValue Base;
5166 SDValue Index;
5167 SDValue Scale;
5168 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5169 I.getParent(), VT.getScalarStoreSize());
5170 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5171 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5172 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5173 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5174 Ranges);
5175
5176 if (!UniformBase) {
5177 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5178 Index = getValue(Ptr);
5179 Scale =
5180 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5181 }
5182
5183 EVT IdxVT = Index.getValueType();
5184 EVT EltTy = IdxVT.getVectorElementType();
5185 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5186 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5187 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5188 }
5189
5190 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5191 SDValue Gather =
5192 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5194
5195 PendingLoads.push_back(Gather.getValue(1));
5196 setValue(&I, Gather);
5197}
5198
5199void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5200 SDLoc dl = getCurSDLoc();
5201 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5202 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5203 SyncScope::ID SSID = I.getSyncScopeID();
5204
5205 SDValue InChain = getRoot();
5206
5207 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5208 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5209
5210 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5211 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5212
5213 MachineFunction &MF = DAG.getMachineFunction();
5214 MachineMemOperand *MMO =
5215 MF.getMachineMemOperand(MachinePointerInfo(I.getPointerOperand()), Flags,
5216 MemVT.getStoreSize(), I.getAlign(), AAMDNodes(),
5217 nullptr, SSID, SuccessOrdering, FailureOrdering);
5218
5220 dl, MemVT, VTs, InChain,
5221 getValue(I.getPointerOperand()),
5222 getValue(I.getCompareOperand()),
5223 getValue(I.getNewValOperand()), MMO);
5224
5225 SDValue OutChain = L.getValue(2);
5226
5227 setValue(&I, L);
5228 DAG.setRoot(OutChain);
5229}
5230
5231void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5232 SDLoc dl = getCurSDLoc();
5234 switch (I.getOperation()) {
5235 default: llvm_unreachable("Unknown atomicrmw operation");
5253 break;
5256 break;
5259 break;
5262 break;
5265 break;
5268 break;
5271 break;
5274 break;
5275 }
5276 AtomicOrdering Ordering = I.getOrdering();
5277 SyncScope::ID SSID = I.getSyncScopeID();
5278
5279 SDValue InChain = getRoot();
5280
5281 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5282 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5283 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5284
5285 MachineFunction &MF = DAG.getMachineFunction();
5286 MachineMemOperand *MMO = MF.getMachineMemOperand(
5287 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5288 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5289
5290 SDValue L =
5291 DAG.getAtomic(NT, dl, MemVT, InChain,
5292 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5293 MMO);
5294
5295 SDValue OutChain = L.getValue(1);
5296
5297 setValue(&I, L);
5298 DAG.setRoot(OutChain);
5299}
5300
5301void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5302 SDLoc dl = getCurSDLoc();
5303 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5304 SDValue Ops[3];
5305 Ops[0] = getRoot();
5306 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5307 TLI.getFenceOperandTy(DAG.getDataLayout()));
5308 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5309 TLI.getFenceOperandTy(DAG.getDataLayout()));
5310 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5311 setValue(&I, N);
5312 DAG.setRoot(N);
5313}
5314
5315void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5316 SDLoc dl = getCurSDLoc();
5317 AtomicOrdering Order = I.getOrdering();
5318 SyncScope::ID SSID = I.getSyncScopeID();
5319
5320 SDValue InChain = getRoot();
5321
5322 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5323 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5324 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5325
5326 if (!TLI.supportsUnalignedAtomics() &&
5327 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5328 report_fatal_error("Cannot generate unaligned atomic load");
5329
5330 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5331
5332 const MDNode *Ranges = getRangeMetadata(I);
5333 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5334 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5335 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5336
5337 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5338
5339 SDValue Ptr = getValue(I.getPointerOperand());
5340 SDValue L =
5341 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5342
5343 SDValue OutChain = L.getValue(1);
5344 if (MemVT != VT)
5345 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5346
5347 setValue(&I, L);
5348 DAG.setRoot(OutChain);
5349}
5350
5351void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5352 SDLoc dl = getCurSDLoc();
5353
5354 AtomicOrdering Ordering = I.getOrdering();
5355 SyncScope::ID SSID = I.getSyncScopeID();
5356
5357 SDValue InChain = getRoot();
5358
5359 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5360 EVT MemVT =
5361 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5362
5363 if (!TLI.supportsUnalignedAtomics() &&
5364 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5365 report_fatal_error("Cannot generate unaligned atomic store");
5366
5367 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5368
5369 MachineFunction &MF = DAG.getMachineFunction();
5370 MachineMemOperand *MMO = MF.getMachineMemOperand(
5371 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5372 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5373
5374 SDValue Val = getValue(I.getValueOperand());
5375 if (Val.getValueType() != MemVT)
5376 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5377 SDValue Ptr = getValue(I.getPointerOperand());
5378
5379 SDValue OutChain =
5380 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5381
5382 setValue(&I, OutChain);
5383 DAG.setRoot(OutChain);
5384}
5385
5386/// Check if this intrinsic call depends on the chain (1st return value)
5387/// and if it only *loads* memory.
5388/// Ignore the callsite's attributes. A specific call site may be marked with
5389/// readnone, but the lowering code will expect the chain based on the
5390/// definition.
5391std::pair<bool, bool>
5392SelectionDAGBuilder::getTargetIntrinsicCallProperties(const CallBase &I) {
5393 const Function *F = I.getCalledFunction();
5394 bool HasChain = !F->doesNotAccessMemory();
5395 bool OnlyLoad =
5396 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5397
5398 return {HasChain, OnlyLoad};
5399}
5400
5401SmallVector<SDValue, 8> SelectionDAGBuilder::getTargetIntrinsicOperands(
5402 const CallBase &I, bool HasChain, bool OnlyLoad,
5403 TargetLowering::IntrinsicInfo *TgtMemIntrinsicInfo) {
5404 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5405
5406 // Build the operand list.
5408 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5409 if (OnlyLoad) {
5410 // We don't need to serialize loads against other loads.
5411 Ops.push_back(DAG.getRoot());
5412 } else {
5413 Ops.push_back(getRoot());
5414 }
5415 }
5416
5417 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5418 if (!TgtMemIntrinsicInfo || TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_VOID ||
5419 TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_W_CHAIN)
5420 Ops.push_back(DAG.getTargetConstant(I.getIntrinsicID(), getCurSDLoc(),
5421 TLI.getPointerTy(DAG.getDataLayout())));
5422
5423 // Add all operands of the call to the operand list.
5424 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5425 const Value *Arg = I.getArgOperand(i);
5426 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5427 Ops.push_back(getValue(Arg));
5428 continue;
5429 }
5430
5431 // Use TargetConstant instead of a regular constant for immarg.
5432 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5433 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5434 assert(CI->getBitWidth() <= 64 &&
5435 "large intrinsic immediates not handled");
5436 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5437 } else {
5438 Ops.push_back(
5439 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5440 }
5441 }
5442
5443 if (std::optional<OperandBundleUse> Bundle =
5444 I.getOperandBundle(LLVMContext::OB_deactivation_symbol)) {
5445 auto *Sym = Bundle->Inputs[0].get();
5446 SDValue SDSym = getValue(Sym);
5447 SDSym = DAG.getDeactivationSymbol(cast<GlobalValue>(Sym));
5448 Ops.push_back(SDSym);
5449 }
5450
5451 if (std::optional<OperandBundleUse> Bundle =
5452 I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5453 Value *Token = Bundle->Inputs[0].get();
5454 SDValue ConvControlToken = getValue(Token);
5455 assert(Ops.back().getValueType() != MVT::Glue &&
5456 "Did not expect another glue node here.");
5457 ConvControlToken =
5458 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5459 Ops.push_back(ConvControlToken);
5460 }
5461
5462 return Ops;
5463}
5464
5465SDVTList SelectionDAGBuilder::getTargetIntrinsicVTList(const CallBase &I,
5466 bool HasChain) {
5467 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5468
5469 SmallVector<EVT, 4> ValueVTs;
5470 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5471
5472 if (HasChain)
5473 ValueVTs.push_back(MVT::Other);
5474
5475 return DAG.getVTList(ValueVTs);
5476}
5477
5478/// Get an INTRINSIC node for a target intrinsic which does not touch memory.
5479SDValue SelectionDAGBuilder::getTargetNonMemIntrinsicNode(
5480 const Type &IntrinsicVT, bool HasChain, ArrayRef<SDValue> Ops,
5481 const SDVTList &VTs) {
5482 if (!HasChain)
5483 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5484 if (!IntrinsicVT.isVoidTy())
5485 return DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5486 return DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5487}
5488
5489/// Set root, convert return type if necessary and check alignment.
5490SDValue SelectionDAGBuilder::handleTargetIntrinsicRet(const CallBase &I,
5491 bool HasChain,
5492 bool OnlyLoad,
5493 SDValue Result) {
5494 if (HasChain) {
5495 SDValue Chain = Result.getValue(Result.getNode()->getNumValues() - 1);
5496 if (OnlyLoad)
5497 PendingLoads.push_back(Chain);
5498 else
5499 DAG.setRoot(Chain);
5500 }
5501
5502 if (I.getType()->isVoidTy())
5503 return Result;
5504
5505 if (MaybeAlign Alignment = I.getRetAlign(); InsertAssertAlign && Alignment) {
5506 // Insert `assertalign` node if there's an alignment.
5507 Result = DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5508 } else if (!isa<VectorType>(I.getType())) {
5509 Result = lowerRangeToAssertZExt(DAG, I, Result);
5510 }
5511
5512 return Result;
5513}
5514
5515/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5516/// node.
5517void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5518 unsigned Intrinsic) {
5519 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
5520
5521 // Infos is set by getTgtMemIntrinsic.
5523 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5524 TLI.getTgtMemIntrinsic(Infos, I, DAG.getMachineFunction(), Intrinsic);
5525 // Use the first (primary) info determines the node opcode.
5526 TargetLowering::IntrinsicInfo *Info = !Infos.empty() ? &Infos[0] : nullptr;
5527
5529 getTargetIntrinsicOperands(I, HasChain, OnlyLoad, Info);
5530 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
5531
5532 // Propagate fast-math-flags from IR to node(s).
5533 SDNodeFlags Flags;
5534 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5535 Flags.copyFMF(*FPMO);
5536 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5537
5538 // Create the node.
5540
5541 // In some cases, custom collection of operands from CallInst I may be needed.
5543 if (!Infos.empty()) {
5544 // This is target intrinsic that touches memory
5545 // Create MachineMemOperands for each memory access described by the target.
5546 MachineFunction &MF = DAG.getMachineFunction();
5548 for (const auto &Info : Infos) {
5549 // TODO: We currently just fallback to address space 0 if
5550 // getTgtMemIntrinsic didn't yield anything useful.
5551 MachinePointerInfo MPI;
5552 if (Info.ptrVal)
5553 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5554 else if (Info.fallbackAddressSpace)
5555 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5556 EVT MemVT = Info.memVT;
5557 LocationSize Size = LocationSize::precise(Info.size);
5558 if (Size.hasValue() && !Size.getValue())
5560 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5561 MachineMemOperand *MMO = MF.getMachineMemOperand(
5562 MPI, Info.flags, Size, Alignment, I.getAAMetadata(),
5563 /*Ranges=*/nullptr, Info.ssid, Info.order, Info.failureOrder);
5564 MMOs.push_back(MMO);
5565 }
5566
5567 Result = DAG.getMemIntrinsicNode(Info->opc, getCurSDLoc(), VTs, Ops,
5568 Info->memVT, MMOs);
5569 } else {
5570 Result = getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
5571 }
5572
5573 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
5574
5575 setValue(&I, Result);
5576}
5577
5578/// GetSignificand - Get the significand and build it into a floating-point
5579/// number with exponent of 1:
5580///
5581/// Op = (Op & 0x007fffff) | 0x3f800000;
5582///
5583/// where Op is the hexadecimal representation of floating point value.
5585 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5586 DAG.getConstant(0x007fffff, dl, MVT::i32));
5587 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5588 DAG.getConstant(0x3f800000, dl, MVT::i32));
5589 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5590}
5591
5592/// GetExponent - Get the exponent:
5593///
5594/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5595///
5596/// where Op is the hexadecimal representation of floating point value.
5598 const TargetLowering &TLI, const SDLoc &dl) {
5599 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5600 DAG.getConstant(0x7f800000, dl, MVT::i32));
5601 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5602 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5603 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5604 DAG.getConstant(127, dl, MVT::i32));
5605 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5606}
5607
5608/// getF32Constant - Get 32-bit floating point constant.
5609static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt,
5610 const SDLoc &dl) {
5611 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5612 MVT::f32);
5613}
5614
5616 SelectionDAG &DAG) {
5617 // TODO: What fast-math-flags should be set on the floating-point nodes?
5618
5619 // IntegerPartOfX = ((int32_t)(t0);
5620 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5621
5622 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5623 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5624 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5625
5626 // IntegerPartOfX <<= 23;
5627 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5628 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5629
5630 SDValue TwoToFractionalPartOfX;
5631 if (LimitFloatPrecision <= 6) {
5632 // For floating-point precision of 6:
5633 //
5634 // TwoToFractionalPartOfX =
5635 // 0.997535578f +
5636 // (0.735607626f + 0.252464424f * x) * x;
5637 //
5638 // error 0.0144103317, which is 6 bits
5639 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5640 getF32Constant(DAG, 0x3e814304, dl));
5641 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5642 getF32Constant(DAG, 0x3f3c50c8, dl));
5643 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5644 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5645 getF32Constant(DAG, 0x3f7f5e7e, dl));
5646 } else if (LimitFloatPrecision <= 12) {
5647 // For floating-point precision of 12:
5648 //
5649 // TwoToFractionalPartOfX =
5650 // 0.999892986f +
5651 // (0.696457318f +
5652 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5653 //
5654 // error 0.000107046256, which is 13 to 14 bits
5655 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5656 getF32Constant(DAG, 0x3da235e3, dl));
5657 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5658 getF32Constant(DAG, 0x3e65b8f3, dl));
5659 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5660 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5661 getF32Constant(DAG, 0x3f324b07, dl));
5662 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5663 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5664 getF32Constant(DAG, 0x3f7ff8fd, dl));
5665 } else { // LimitFloatPrecision <= 18
5666 // For floating-point precision of 18:
5667 //
5668 // TwoToFractionalPartOfX =
5669 // 0.999999982f +
5670 // (0.693148872f +
5671 // (0.240227044f +
5672 // (0.554906021e-1f +
5673 // (0.961591928e-2f +
5674 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5675 // error 2.47208000*10^(-7), which is better than 18 bits
5676 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5677 getF32Constant(DAG, 0x3924b03e, dl));
5678 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5679 getF32Constant(DAG, 0x3ab24b87, dl));
5680 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5681 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5682 getF32Constant(DAG, 0x3c1d8c17, dl));
5683 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5684 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5685 getF32Constant(DAG, 0x3d634a1d, dl));
5686 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5687 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5688 getF32Constant(DAG, 0x3e75fe14, dl));
5689 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5690 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5691 getF32Constant(DAG, 0x3f317234, dl));
5692 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5693 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5694 getF32Constant(DAG, 0x3f800000, dl));
5695 }
5696
5697 // Add the exponent into the result in integer domain.
5698 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5699 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5700 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5701}
5702
5703/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5704/// limited-precision mode.
5706 const TargetLowering &TLI, SDNodeFlags Flags) {
5707 if (Op.getValueType() == MVT::f32 &&
5709
5710 // Put the exponent in the right bit position for later addition to the
5711 // final result:
5712 //
5713 // t0 = Op * log2(e)
5714
5715 // TODO: What fast-math-flags should be set here?
5716 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5717 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5718 return getLimitedPrecisionExp2(t0, dl, DAG);
5719 }
5720
5721 // No special expansion.
5722 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5723}
5724
5725/// expandLog - Lower a log intrinsic. Handles the special sequences for
5726/// limited-precision mode.
5728 const TargetLowering &TLI, SDNodeFlags Flags) {
5729 // TODO: What fast-math-flags should be set on the floating-point nodes?
5730
5731 if (Op.getValueType() == MVT::f32 &&
5733 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5734
5735 // Scale the exponent by log(2).
5736 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5737 SDValue LogOfExponent =
5738 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5739 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5740
5741 // Get the significand and build it into a floating-point number with
5742 // exponent of 1.
5743 SDValue X = GetSignificand(DAG, Op1, dl);
5744
5745 SDValue LogOfMantissa;
5746 if (LimitFloatPrecision <= 6) {
5747 // For floating-point precision of 6:
5748 //
5749 // LogofMantissa =
5750 // -1.1609546f +
5751 // (1.4034025f - 0.23903021f * x) * x;
5752 //
5753 // error 0.0034276066, which is better than 8 bits
5754 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5755 getF32Constant(DAG, 0xbe74c456, dl));
5756 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5757 getF32Constant(DAG, 0x3fb3a2b1, dl));
5758 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5759 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5760 getF32Constant(DAG, 0x3f949a29, dl));
5761 } else if (LimitFloatPrecision <= 12) {
5762 // For floating-point precision of 12:
5763 //
5764 // LogOfMantissa =
5765 // -1.7417939f +
5766 // (2.8212026f +
5767 // (-1.4699568f +
5768 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5769 //
5770 // error 0.000061011436, which is 14 bits
5771 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5772 getF32Constant(DAG, 0xbd67b6d6, dl));
5773 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5774 getF32Constant(DAG, 0x3ee4f4b8, dl));
5775 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5776 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5777 getF32Constant(DAG, 0x3fbc278b, dl));
5778 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5779 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5780 getF32Constant(DAG, 0x40348e95, dl));
5781 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5782 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5783 getF32Constant(DAG, 0x3fdef31a, dl));
5784 } else { // LimitFloatPrecision <= 18
5785 // For floating-point precision of 18:
5786 //
5787 // LogOfMantissa =
5788 // -2.1072184f +
5789 // (4.2372794f +
5790 // (-3.7029485f +
5791 // (2.2781945f +
5792 // (-0.87823314f +
5793 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5794 //
5795 // error 0.0000023660568, which is better than 18 bits
5796 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5797 getF32Constant(DAG, 0xbc91e5ac, dl));
5798 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5799 getF32Constant(DAG, 0x3e4350aa, dl));
5800 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5801 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5802 getF32Constant(DAG, 0x3f60d3e3, dl));
5803 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5804 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5805 getF32Constant(DAG, 0x4011cdf0, dl));
5806 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5807 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5808 getF32Constant(DAG, 0x406cfd1c, dl));
5809 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5810 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5811 getF32Constant(DAG, 0x408797cb, dl));
5812 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5813 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5814 getF32Constant(DAG, 0x4006dcab, dl));
5815 }
5816
5817 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5818 }
5819
5820 // No special expansion.
5821 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5822}
5823
5824/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5825/// limited-precision mode.
5827 const TargetLowering &TLI, SDNodeFlags Flags) {
5828 // TODO: What fast-math-flags should be set on the floating-point nodes?
5829
5830 if (Op.getValueType() == MVT::f32 &&
5832 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5833
5834 // Get the exponent.
5835 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5836
5837 // Get the significand and build it into a floating-point number with
5838 // exponent of 1.
5839 SDValue X = GetSignificand(DAG, Op1, dl);
5840
5841 // Different possible minimax approximations of significand in
5842 // floating-point for various degrees of accuracy over [1,2].
5843 SDValue Log2ofMantissa;
5844 if (LimitFloatPrecision <= 6) {
5845 // For floating-point precision of 6:
5846 //
5847 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5848 //
5849 // error 0.0049451742, which is more than 7 bits
5850 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5851 getF32Constant(DAG, 0xbeb08fe0, dl));
5852 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5853 getF32Constant(DAG, 0x40019463, dl));
5854 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5855 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5856 getF32Constant(DAG, 0x3fd6633d, dl));
5857 } else if (LimitFloatPrecision <= 12) {
5858 // For floating-point precision of 12:
5859 //
5860 // Log2ofMantissa =
5861 // -2.51285454f +
5862 // (4.07009056f +
5863 // (-2.12067489f +
5864 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5865 //
5866 // error 0.0000876136000, which is better than 13 bits
5867 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5868 getF32Constant(DAG, 0xbda7262e, dl));
5869 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5870 getF32Constant(DAG, 0x3f25280b, dl));
5871 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5872 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5873 getF32Constant(DAG, 0x4007b923, dl));
5874 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5875 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5876 getF32Constant(DAG, 0x40823e2f, dl));
5877 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5878 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5879 getF32Constant(DAG, 0x4020d29c, dl));
5880 } else { // LimitFloatPrecision <= 18
5881 // For floating-point precision of 18:
5882 //
5883 // Log2ofMantissa =
5884 // -3.0400495f +
5885 // (6.1129976f +
5886 // (-5.3420409f +
5887 // (3.2865683f +
5888 // (-1.2669343f +
5889 // (0.27515199f -
5890 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5891 //
5892 // error 0.0000018516, which is better than 18 bits
5893 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5894 getF32Constant(DAG, 0xbcd2769e, dl));
5895 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5896 getF32Constant(DAG, 0x3e8ce0b9, dl));
5897 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5898 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5899 getF32Constant(DAG, 0x3fa22ae7, dl));
5900 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5901 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5902 getF32Constant(DAG, 0x40525723, dl));
5903 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5904 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5905 getF32Constant(DAG, 0x40aaf200, dl));
5906 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5907 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5908 getF32Constant(DAG, 0x40c39dad, dl));
5909 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5910 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5911 getF32Constant(DAG, 0x4042902c, dl));
5912 }
5913
5914 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5915 }
5916
5917 // No special expansion.
5918 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5919}
5920
5921/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5922/// limited-precision mode.
5924 const TargetLowering &TLI, SDNodeFlags Flags) {
5925 // TODO: What fast-math-flags should be set on the floating-point nodes?
5926
5927 if (Op.getValueType() == MVT::f32 &&
5929 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5930
5931 // Scale the exponent by log10(2) [0.30102999f].
5932 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5933 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5934 getF32Constant(DAG, 0x3e9a209a, dl));
5935
5936 // Get the significand and build it into a floating-point number with
5937 // exponent of 1.
5938 SDValue X = GetSignificand(DAG, Op1, dl);
5939
5940 SDValue Log10ofMantissa;
5941 if (LimitFloatPrecision <= 6) {
5942 // For floating-point precision of 6:
5943 //
5944 // Log10ofMantissa =
5945 // -0.50419619f +
5946 // (0.60948995f - 0.10380950f * x) * x;
5947 //
5948 // error 0.0014886165, which is 6 bits
5949 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5950 getF32Constant(DAG, 0xbdd49a13, dl));
5951 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5952 getF32Constant(DAG, 0x3f1c0789, dl));
5953 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5954 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5955 getF32Constant(DAG, 0x3f011300, dl));
5956 } else if (LimitFloatPrecision <= 12) {
5957 // For floating-point precision of 12:
5958 //
5959 // Log10ofMantissa =
5960 // -0.64831180f +
5961 // (0.91751397f +
5962 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5963 //
5964 // error 0.00019228036, which is better than 12 bits
5965 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5966 getF32Constant(DAG, 0x3d431f31, dl));
5967 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5968 getF32Constant(DAG, 0x3ea21fb2, dl));
5969 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5970 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5971 getF32Constant(DAG, 0x3f6ae232, dl));
5972 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5973 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5974 getF32Constant(DAG, 0x3f25f7c3, dl));
5975 } else { // LimitFloatPrecision <= 18
5976 // For floating-point precision of 18:
5977 //
5978 // Log10ofMantissa =
5979 // -0.84299375f +
5980 // (1.5327582f +
5981 // (-1.0688956f +
5982 // (0.49102474f +
5983 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5984 //
5985 // error 0.0000037995730, which is better than 18 bits
5986 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5987 getF32Constant(DAG, 0x3c5d51ce, dl));
5988 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5989 getF32Constant(DAG, 0x3e00685a, dl));
5990 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5991 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5992 getF32Constant(DAG, 0x3efb6798, dl));
5993 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5994 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5995 getF32Constant(DAG, 0x3f88d192, dl));
5996 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5997 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5998 getF32Constant(DAG, 0x3fc4316c, dl));
5999 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
6000 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
6001 getF32Constant(DAG, 0x3f57ce70, dl));
6002 }
6003
6004 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
6005 }
6006
6007 // No special expansion.
6008 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
6009}
6010
6011/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
6012/// limited-precision mode.
6014 const TargetLowering &TLI, SDNodeFlags Flags) {
6015 if (Op.getValueType() == MVT::f32 &&
6017 return getLimitedPrecisionExp2(Op, dl, DAG);
6018
6019 // No special expansion.
6020 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
6021}
6022
6023/// visitPow - Lower a pow intrinsic. Handles the special sequences for
6024/// limited-precision mode with x == 10.0f.
6026 SelectionDAG &DAG, const TargetLowering &TLI,
6027 SDNodeFlags Flags) {
6028 bool IsExp10 = false;
6029 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
6032 APFloat Ten(10.0f);
6033 IsExp10 = LHSC->isExactlyValue(Ten);
6034 }
6035 }
6036
6037 // TODO: What fast-math-flags should be set on the FMUL node?
6038 if (IsExp10) {
6039 // Put the exponent in the right bit position for later addition to the
6040 // final result:
6041 //
6042 // #define LOG2OF10 3.3219281f
6043 // t0 = Op * LOG2OF10;
6044 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
6045 getF32Constant(DAG, 0x40549a78, dl));
6046 return getLimitedPrecisionExp2(t0, dl, DAG);
6047 }
6048
6049 // No special expansion.
6050 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
6051}
6052
6053/// ExpandPowI - Expand a llvm.powi intrinsic.
6055 SelectionDAG &DAG) {
6056 // If RHS is a constant, we can expand this out to a multiplication tree if
6057 // it's beneficial on the target, otherwise we end up lowering to a call to
6058 // __powidf2 (for example).
6060 unsigned Val = RHSC->getSExtValue();
6061
6062 // powi(x, 0) -> 1.0
6063 if (Val == 0)
6064 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
6065
6067 Val, DAG.shouldOptForSize())) {
6068 // Get the exponent as a positive value.
6069 if ((int)Val < 0)
6070 Val = -Val;
6071 // We use the simple binary decomposition method to generate the multiply
6072 // sequence. There are more optimal ways to do this (for example,
6073 // powi(x,15) generates one more multiply than it should), but this has
6074 // the benefit of being both really simple and much better than a libcall.
6075 SDValue Res; // Logically starts equal to 1.0
6076 SDValue CurSquare = LHS;
6077 // TODO: Intrinsics should have fast-math-flags that propagate to these
6078 // nodes.
6079 while (Val) {
6080 if (Val & 1) {
6081 if (Res.getNode())
6082 Res =
6083 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
6084 else
6085 Res = CurSquare; // 1.0*CurSquare.
6086 }
6087
6088 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
6089 CurSquare, CurSquare);
6090 Val >>= 1;
6091 }
6092
6093 // If the original was negative, invert the result, producing 1/(x*x*x).
6094 if (RHSC->getSExtValue() < 0)
6095 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
6096 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
6097 return Res;
6098 }
6099 }
6100
6101 // Otherwise, expand to a libcall.
6102 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
6103}
6104
6105static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
6106 SDValue LHS, SDValue RHS, SDValue Scale,
6107 SelectionDAG &DAG, const TargetLowering &TLI) {
6108 EVT VT = LHS.getValueType();
6109 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
6110 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
6111 LLVMContext &Ctx = *DAG.getContext();
6112
6113 // If the type is legal but the operation isn't, this node might survive all
6114 // the way to operation legalization. If we end up there and we do not have
6115 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
6116 // node.
6117
6118 // Coax the legalizer into expanding the node during type legalization instead
6119 // by bumping the size by one bit. This will force it to Promote, enabling the
6120 // early expansion and avoiding the need to expand later.
6121
6122 // We don't have to do this if Scale is 0; that can always be expanded, unless
6123 // it's a saturating signed operation. Those can experience true integer
6124 // division overflow, a case which we must avoid.
6125
6126 // FIXME: We wouldn't have to do this (or any of the early
6127 // expansion/promotion) if it was possible to expand a libcall of an
6128 // illegal type during operation legalization. But it's not, so things
6129 // get a bit hacky.
6130 unsigned ScaleInt = Scale->getAsZExtVal();
6131 if ((ScaleInt > 0 || (Saturating && Signed)) &&
6132 (TLI.isTypeLegal(VT) ||
6133 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6135 Opcode, VT, ScaleInt);
6136 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6137 EVT PromVT;
6138 if (VT.isScalarInteger())
6139 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6140 else if (VT.isVector()) {
6141 PromVT = VT.getVectorElementType();
6142 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6143 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6144 } else
6145 llvm_unreachable("Wrong VT for DIVFIX?");
6146 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6147 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6148 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6149 // For saturating operations, we need to shift up the LHS to get the
6150 // proper saturation width, and then shift down again afterwards.
6151 if (Saturating)
6152 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6153 DAG.getConstant(1, DL, ShiftTy));
6154 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6155 if (Saturating)
6156 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6157 DAG.getConstant(1, DL, ShiftTy));
6158 return DAG.getZExtOrTrunc(Res, DL, VT);
6159 }
6160 }
6161
6162 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6163}
6164
6165// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6166// bitcasted, or split argument. Returns a list of <Register, size in bits>
6167static void
6168getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6169 const SDValue &N) {
6170 switch (N.getOpcode()) {
6171 case ISD::CopyFromReg: {
6172 SDValue Op = N.getOperand(1);
6173 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6174 Op.getValueType().getSizeInBits());
6175 return;
6176 }
6177 case ISD::BITCAST:
6178 case ISD::AssertZext:
6179 case ISD::AssertSext:
6180 case ISD::TRUNCATE:
6181 getUnderlyingArgRegs(Regs, N.getOperand(0));
6182 return;
6183 case ISD::BUILD_PAIR:
6184 case ISD::BUILD_VECTOR:
6186 for (SDValue Op : N->op_values())
6187 getUnderlyingArgRegs(Regs, Op);
6188 return;
6189 default:
6190 return;
6191 }
6192}
6193
6194/// If the DbgValueInst is a dbg_value of a function argument, create the
6195/// corresponding DBG_VALUE machine instruction for it now. At the end of
6196/// instruction selection, they will be inserted to the entry BB.
6197/// We don't currently support this for variadic dbg_values, as they shouldn't
6198/// appear for function arguments or in the prologue.
6199bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6200 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6201 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6202 const Argument *Arg = dyn_cast<Argument>(V);
6203 if (!Arg)
6204 return false;
6205
6206 MachineFunction &MF = DAG.getMachineFunction();
6207 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6208
6209 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6210 // we've been asked to pursue.
6211 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6212 bool Indirect) {
6213 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6214 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6215 // pointing at the VReg, which will be patched up later.
6216 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6218 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6219 /* isKill */ false, /* isDead */ false,
6220 /* isUndef */ false, /* isEarlyClobber */ false,
6221 /* SubReg */ 0, /* isDebug */ true)});
6222
6223 auto *NewDIExpr = FragExpr;
6224 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6225 // the DIExpression.
6226 if (Indirect)
6227 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6229 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6230 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6231 } else {
6232 // Create a completely standard DBG_VALUE.
6233 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6234 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6235 }
6236 };
6237
6238 if (Kind == FuncArgumentDbgValueKind::Value) {
6239 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6240 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6241 // the entry block.
6242 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6243 if (!IsInEntryBlock)
6244 return false;
6245
6246 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6247 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6248 // variable that also is a param.
6249 //
6250 // Although, if we are at the top of the entry block already, we can still
6251 // emit using ArgDbgValue. This might catch some situations when the
6252 // dbg.value refers to an argument that isn't used in the entry block, so
6253 // any CopyToReg node would be optimized out and the only way to express
6254 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6255 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6256 // we should only emit as ArgDbgValue if the Variable is an argument to the
6257 // current function, and the dbg.value intrinsic is found in the entry
6258 // block.
6259 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6260 !DL->getInlinedAt();
6261 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6262 if (!IsInPrologue && !VariableIsFunctionInputArg)
6263 return false;
6264
6265 // Here we assume that a function argument on IR level only can be used to
6266 // describe one input parameter on source level. If we for example have
6267 // source code like this
6268 //
6269 // struct A { long x, y; };
6270 // void foo(struct A a, long b) {
6271 // ...
6272 // b = a.x;
6273 // ...
6274 // }
6275 //
6276 // and IR like this
6277 //
6278 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6279 // entry:
6280 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6281 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6282 // call void @llvm.dbg.value(metadata i32 %b, "b",
6283 // ...
6284 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6285 // ...
6286 //
6287 // then the last dbg.value is describing a parameter "b" using a value that
6288 // is an argument. But since we already has used %a1 to describe a parameter
6289 // we should not handle that last dbg.value here (that would result in an
6290 // incorrect hoisting of the DBG_VALUE to the function entry).
6291 // Notice that we allow one dbg.value per IR level argument, to accommodate
6292 // for the situation with fragments above.
6293 // If there is no node for the value being handled, we return true to skip
6294 // the normal generation of debug info, as it would kill existing debug
6295 // info for the parameter in case of duplicates.
6296 if (VariableIsFunctionInputArg) {
6297 unsigned ArgNo = Arg->getArgNo();
6298 if (ArgNo >= FuncInfo.DescribedArgs.size())
6299 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6300 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6301 return !NodeMap[V].getNode();
6302 FuncInfo.DescribedArgs.set(ArgNo);
6303 }
6304 }
6305
6306 bool IsIndirect = false;
6307 std::optional<MachineOperand> Op;
6308 // Some arguments' frame index is recorded during argument lowering.
6309 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6310 if (FI != std::numeric_limits<int>::max())
6312
6314 if (!Op && N.getNode()) {
6315 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6316 Register Reg;
6317 if (ArgRegsAndSizes.size() == 1)
6318 Reg = ArgRegsAndSizes.front().first;
6319
6320 if (Reg && Reg.isVirtual()) {
6321 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6322 Register PR = RegInfo.getLiveInPhysReg(Reg);
6323 if (PR)
6324 Reg = PR;
6325 }
6326 if (Reg) {
6328 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6329 }
6330 }
6331
6332 if (!Op && N.getNode()) {
6333 // Check if frame index is available.
6334 SDValue LCandidate = peekThroughBitcasts(N);
6335 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6336 if (FrameIndexSDNode *FINode =
6337 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6338 Op = MachineOperand::CreateFI(FINode->getIndex());
6339 }
6340
6341 if (!Op) {
6342 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6343 auto splitMultiRegDbgValue =
6344 [&](ArrayRef<std::pair<Register, TypeSize>> SplitRegs) -> bool {
6345 unsigned Offset = 0;
6346 for (const auto &[Reg, RegSizeInBits] : SplitRegs) {
6347 // FIXME: Scalable sizes are not supported in fragment expressions.
6348 if (RegSizeInBits.isScalable())
6349 return false;
6350
6351 // If the expression is already a fragment, the current register
6352 // offset+size might extend beyond the fragment. In this case, only
6353 // the register bits that are inside the fragment are relevant.
6354 int RegFragmentSizeInBits = RegSizeInBits.getFixedValue();
6355 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6356 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6357 // The register is entirely outside the expression fragment,
6358 // so is irrelevant for debug info.
6359 if (Offset >= ExprFragmentSizeInBits)
6360 break;
6361 // The register is partially outside the expression fragment, only
6362 // the low bits within the fragment are relevant for debug info.
6363 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6364 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6365 }
6366 }
6367
6368 auto FragmentExpr = DIExpression::createFragmentExpression(
6369 Expr, Offset, RegFragmentSizeInBits);
6370 Offset += RegSizeInBits.getFixedValue();
6371 // If a valid fragment expression cannot be created, the variable's
6372 // correct value cannot be determined and so it is set as poison.
6373 if (!FragmentExpr) {
6374 SDDbgValue *SDV = DAG.getConstantDbgValue(
6375 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6376 DAG.AddDbgValue(SDV, false);
6377 continue;
6378 }
6379 MachineInstr *NewMI = MakeVRegDbgValue(
6380 Reg, *FragmentExpr, Kind != FuncArgumentDbgValueKind::Value);
6381 FuncInfo.ArgDbgValues.push_back(NewMI);
6382 }
6383
6384 return true;
6385 };
6386
6387 // Check if ValueMap has reg number.
6389 VMI = FuncInfo.ValueMap.find(V);
6390 if (VMI != FuncInfo.ValueMap.end()) {
6391 const auto &TLI = DAG.getTargetLoweringInfo();
6392 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6393 V->getType(), std::nullopt);
6394 if (RFV.occupiesMultipleRegs())
6395 return splitMultiRegDbgValue(RFV.getRegsAndSizes());
6396
6397 Op = MachineOperand::CreateReg(VMI->second, false);
6398 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6399 } else if (ArgRegsAndSizes.size() > 1) {
6400 // This was split due to the calling convention, and no virtual register
6401 // mapping exists for the value.
6402 return splitMultiRegDbgValue(ArgRegsAndSizes);
6403 }
6404 }
6405
6406 if (!Op)
6407 return false;
6408
6409 assert(Variable->isValidLocationForIntrinsic(DL) &&
6410 "Expected inlined-at fields to agree");
6411 MachineInstr *NewMI = nullptr;
6412
6413 if (Op->isReg())
6414 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6415 else
6416 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6417 Variable, Expr);
6418
6419 // Otherwise, use ArgDbgValues.
6420 FuncInfo.ArgDbgValues.push_back(NewMI);
6421 return true;
6422}
6423
6424/// Return the appropriate SDDbgValue based on N.
6425SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6426 DILocalVariable *Variable,
6427 DIExpression *Expr,
6428 const DebugLoc &dl,
6429 unsigned DbgSDNodeOrder) {
6430 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6431 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6432 // stack slot locations.
6433 //
6434 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6435 // debug values here after optimization:
6436 //
6437 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6438 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6439 //
6440 // Both describe the direct values of their associated variables.
6441 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6442 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6443 }
6444 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6445 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6446}
6447
6448static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6449 switch (Intrinsic) {
6450 case Intrinsic::smul_fix:
6451 return ISD::SMULFIX;
6452 case Intrinsic::umul_fix:
6453 return ISD::UMULFIX;
6454 case Intrinsic::smul_fix_sat:
6455 return ISD::SMULFIXSAT;
6456 case Intrinsic::umul_fix_sat:
6457 return ISD::UMULFIXSAT;
6458 case Intrinsic::sdiv_fix:
6459 return ISD::SDIVFIX;
6460 case Intrinsic::udiv_fix:
6461 return ISD::UDIVFIX;
6462 case Intrinsic::sdiv_fix_sat:
6463 return ISD::SDIVFIXSAT;
6464 case Intrinsic::udiv_fix_sat:
6465 return ISD::UDIVFIXSAT;
6466 default:
6467 llvm_unreachable("Unhandled fixed point intrinsic");
6468 }
6469}
6470
6471/// Given a @llvm.call.preallocated.setup, return the corresponding
6472/// preallocated call.
6473static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6474 assert(cast<CallBase>(PreallocatedSetup)
6476 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6477 "expected call_preallocated_setup Value");
6478 for (const auto *U : PreallocatedSetup->users()) {
6479 auto *UseCall = cast<CallBase>(U);
6480 const Function *Fn = UseCall->getCalledFunction();
6481 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6482 return UseCall;
6483 }
6484 }
6485 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6486}
6487
6488/// If DI is a debug value with an EntryValue expression, lower it using the
6489/// corresponding physical register of the associated Argument value
6490/// (guaranteed to exist by the verifier).
6491bool SelectionDAGBuilder::visitEntryValueDbgValue(
6492 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6493 DIExpression *Expr, DebugLoc DbgLoc) {
6494 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6495 return false;
6496
6497 // These properties are guaranteed by the verifier.
6498 const Argument *Arg = cast<Argument>(Values[0]);
6499 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6500
6501 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6502 if (ArgIt == FuncInfo.ValueMap.end()) {
6503 LLVM_DEBUG(
6504 dbgs() << "Dropping dbg.value: expression is entry_value but "
6505 "couldn't find an associated register for the Argument\n");
6506 return true;
6507 }
6508 Register ArgVReg = ArgIt->getSecond();
6509
6510 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6511 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6512 SDDbgValue *SDV = DAG.getVRegDbgValue(
6513 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6514 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6515 return true;
6516 }
6517 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6518 "couldn't find a physical register\n");
6519 return true;
6520}
6521
6522/// Lower the call to the specified intrinsic function.
6523void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6524 unsigned Intrinsic) {
6525 SDLoc sdl = getCurSDLoc();
6526 switch (Intrinsic) {
6527 case Intrinsic::experimental_convergence_anchor:
6528 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6529 break;
6530 case Intrinsic::experimental_convergence_entry:
6531 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6532 break;
6533 case Intrinsic::experimental_convergence_loop: {
6534 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6535 auto *Token = Bundle->Inputs[0].get();
6536 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6537 getValue(Token)));
6538 break;
6539 }
6540 }
6541}
6542
6543void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6544 unsigned IntrinsicID) {
6545 // For now, we're only lowering an 'add' histogram.
6546 // We can add others later, e.g. saturating adds, min/max.
6547 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6548 "Tried to lower unsupported histogram type");
6549 SDLoc sdl = getCurSDLoc();
6550 Value *Ptr = I.getOperand(0);
6551 SDValue Inc = getValue(I.getOperand(1));
6552 SDValue Mask = getValue(I.getOperand(2));
6553
6554 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6555 DataLayout TargetDL = DAG.getDataLayout();
6556 EVT VT = Inc.getValueType();
6557 Align Alignment = DAG.getEVTAlign(VT);
6558
6559 const MDNode *Ranges = getRangeMetadata(I);
6560
6561 SDValue Root = DAG.getRoot();
6562 SDValue Base;
6563 SDValue Index;
6564 SDValue Scale;
6565 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6566 I.getParent(), VT.getScalarStoreSize());
6567
6568 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6569
6570 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6571 MachinePointerInfo(AS),
6573 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6574
6575 if (!UniformBase) {
6576 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6577 Index = getValue(Ptr);
6578 Scale =
6579 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6580 }
6581
6582 EVT IdxVT = Index.getValueType();
6583
6584 // Avoid using e.g. i32 as index type when the increment must be performed
6585 // on i64's.
6586 bool MustExtendIndex = VT.getScalarSizeInBits() > IdxVT.getScalarSizeInBits();
6587 EVT EltTy = MustExtendIndex ? VT : IdxVT.getVectorElementType();
6588 if (MustExtendIndex || TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6589 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
6590 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6591 }
6592
6593 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6594
6595 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6596 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6597 Ops, MMO, ISD::SIGNED_SCALED);
6598
6599 setValue(&I, Histogram);
6600 DAG.setRoot(Histogram);
6601}
6602
6603void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6604 unsigned Intrinsic) {
6605 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6606 "Tried lowering invalid vector extract last");
6607 SDLoc sdl = getCurSDLoc();
6608 const DataLayout &Layout = DAG.getDataLayout();
6609 SDValue Data = getValue(I.getOperand(0));
6610 SDValue Mask = getValue(I.getOperand(1));
6611
6612 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6613 EVT ResVT = TLI.getValueType(Layout, I.getType());
6614
6615 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6616 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6617 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6618
6619 Value *Default = I.getOperand(2);
6621 SDValue PassThru = getValue(Default);
6622 EVT BoolVT = Mask.getValueType().getScalarType();
6623 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6624 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6625 }
6626
6627 setValue(&I, Result);
6628}
6629
6630/// Lower the call to the specified intrinsic function.
6631void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6632 unsigned Intrinsic) {
6633 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6634 SDLoc sdl = getCurSDLoc();
6635 DebugLoc dl = getCurDebugLoc();
6636 SDValue Res;
6637
6638 SDNodeFlags Flags;
6639 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6640 Flags.copyFMF(*FPOp);
6641
6642 switch (Intrinsic) {
6643 default:
6644 // By default, turn this into a target intrinsic node.
6645 visitTargetIntrinsic(I, Intrinsic);
6646 return;
6647 case Intrinsic::vscale: {
6648 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6649 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6650 return;
6651 }
6652 case Intrinsic::vastart: visitVAStart(I); return;
6653 case Intrinsic::vaend: visitVAEnd(I); return;
6654 case Intrinsic::vacopy: visitVACopy(I); return;
6655 case Intrinsic::returnaddress:
6656 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6657 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6658 getValue(I.getArgOperand(0))));
6659 return;
6660 case Intrinsic::addressofreturnaddress:
6661 setValue(&I,
6662 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6663 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6664 return;
6665 case Intrinsic::sponentry:
6666 setValue(&I,
6667 DAG.getNode(ISD::SPONENTRY, sdl,
6668 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6669 return;
6670 case Intrinsic::frameaddress:
6671 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6672 TLI.getFrameIndexTy(DAG.getDataLayout()),
6673 getValue(I.getArgOperand(0))));
6674 return;
6675 case Intrinsic::read_volatile_register:
6676 case Intrinsic::read_register: {
6677 Value *Reg = I.getArgOperand(0);
6678 SDValue Chain = getRoot();
6680 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6681 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6682 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6683 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6684 setValue(&I, Res);
6685 DAG.setRoot(Res.getValue(1));
6686 return;
6687 }
6688 case Intrinsic::write_register: {
6689 Value *Reg = I.getArgOperand(0);
6690 Value *RegValue = I.getArgOperand(1);
6691 SDValue Chain = getRoot();
6693 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6694 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6695 RegName, getValue(RegValue)));
6696 return;
6697 }
6698 case Intrinsic::write_volatile_register: {
6699 Value *Reg = I.getArgOperand(0);
6700 Value *RegValue = I.getArgOperand(1);
6701 SDValue Chain = getRoot();
6702 const MDNode *MD = cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata());
6703 SDValue RegName = DAG.getMDNode(MD);
6704 EVT VT = TLI.getValueType(DAG.getDataLayout(), RegValue->getType());
6705 SDValue WriteChain = DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other,
6706 Chain, RegName, getValue(RegValue));
6707 // FAKE_USE of the physical register marks it live after the WRITE_REGISTER,
6708 // preventing the backend from dead-eliminating the write. This is
6709 // preferred over READ_REGISTER, which would emit extra register copies
6710 // (e.g. fmov xN, dN for FP/SIMD registers).
6711 const MDString *RegStr = cast<MDString>(MD->getOperand(0));
6712 LLT Ty = VT.isSimple() ? getLLTForMVT(VT.getSimpleVT()) : LLT();
6713 const MachineFunction &MF = DAG.getMachineFunction();
6714 Register PhysReg =
6715 TLI.getRegisterByName(RegStr->getString().data(), Ty, MF);
6716 if (PhysReg.isValid()) {
6717 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
6718 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(PhysReg);
6719 MVT RegVT = *TRI->legalclasstypes_begin(*RC);
6720 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other,
6721 {WriteChain, DAG.getRegister(PhysReg, RegVT)}));
6722 } else {
6723 DAG.setRoot(WriteChain);
6724 }
6725 return;
6726 }
6727 case Intrinsic::memcpy:
6728 case Intrinsic::memcpy_inline: {
6729 const auto &MCI = cast<MemCpyInst>(I);
6730 SDValue Dst = getValue(I.getArgOperand(0));
6731 SDValue Src = getValue(I.getArgOperand(1));
6732 SDValue Size = getValue(I.getArgOperand(2));
6733 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6734 "memcpy_inline needs constant size");
6735 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6736 Align DstAlign = MCI.getDestAlign().valueOrOne();
6737 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6738 bool isVol = MCI.isVolatile();
6739 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6740 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, DstAlign, SrcAlign,
6741 isVol, MCI.isForceInlined(), &I, std::nullopt,
6742 MachinePointerInfo(I.getArgOperand(0)),
6743 MachinePointerInfo(I.getArgOperand(1)),
6744 I.getAAMetadata(), BatchAA);
6745 updateDAGForMaybeTailCall(MC);
6746 return;
6747 }
6748 case Intrinsic::memset:
6749 case Intrinsic::memset_inline: {
6750 const auto &MSII = cast<MemSetInst>(I);
6751 SDValue Dst = getValue(I.getArgOperand(0));
6752 SDValue Value = getValue(I.getArgOperand(1));
6753 SDValue Size = getValue(I.getArgOperand(2));
6754 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6755 "memset_inline needs constant size");
6756 // @llvm.memset defines 0 and 1 to both mean no alignment.
6757 Align DstAlign = MSII.getDestAlign().valueOrOne();
6758 bool isVol = MSII.isVolatile();
6759 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6760 SDValue MC = DAG.getMemset(
6761 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6762 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6763 updateDAGForMaybeTailCall(MC);
6764 return;
6765 }
6766 case Intrinsic::memmove: {
6767 const auto &MMI = cast<MemMoveInst>(I);
6768 SDValue Op1 = getValue(I.getArgOperand(0));
6769 SDValue Op2 = getValue(I.getArgOperand(1));
6770 SDValue Op3 = getValue(I.getArgOperand(2));
6771 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6772 Align DstAlign = MMI.getDestAlign().valueOrOne();
6773 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6774 bool isVol = MMI.isVolatile();
6775 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6776 SDValue MM = DAG.getMemmove(
6777 Root, sdl, Op1, Op2, Op3, DstAlign, SrcAlign, isVol, &I,
6778 /* OverrideTailCall */ std::nullopt,
6779 MachinePointerInfo(I.getArgOperand(0)),
6780 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata(), BatchAA);
6781 updateDAGForMaybeTailCall(MM);
6782 return;
6783 }
6784 case Intrinsic::memcpy_element_unordered_atomic: {
6785 auto &MI = cast<AnyMemCpyInst>(I);
6786 SDValue Dst = getValue(MI.getRawDest());
6787 SDValue Src = getValue(MI.getRawSource());
6788 SDValue Length = getValue(MI.getLength());
6789
6790 Type *LengthTy = MI.getLength()->getType();
6791 unsigned ElemSz = MI.getElementSizeInBytes();
6792 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6793 SDValue MC =
6794 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6795 isTC, MachinePointerInfo(MI.getRawDest()),
6796 MachinePointerInfo(MI.getRawSource()));
6797 updateDAGForMaybeTailCall(MC);
6798 return;
6799 }
6800 case Intrinsic::memmove_element_unordered_atomic: {
6801 auto &MI = cast<AnyMemMoveInst>(I);
6802 SDValue Dst = getValue(MI.getRawDest());
6803 SDValue Src = getValue(MI.getRawSource());
6804 SDValue Length = getValue(MI.getLength());
6805
6806 Type *LengthTy = MI.getLength()->getType();
6807 unsigned ElemSz = MI.getElementSizeInBytes();
6808 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6809 SDValue MC =
6810 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6811 isTC, MachinePointerInfo(MI.getRawDest()),
6812 MachinePointerInfo(MI.getRawSource()));
6813 updateDAGForMaybeTailCall(MC);
6814 return;
6815 }
6816 case Intrinsic::memset_element_unordered_atomic: {
6817 auto &MI = cast<AnyMemSetInst>(I);
6818 SDValue Dst = getValue(MI.getRawDest());
6819 SDValue Val = getValue(MI.getValue());
6820 SDValue Length = getValue(MI.getLength());
6821
6822 Type *LengthTy = MI.getLength()->getType();
6823 unsigned ElemSz = MI.getElementSizeInBytes();
6824 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6825 SDValue MC =
6826 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6827 isTC, MachinePointerInfo(MI.getRawDest()));
6828 updateDAGForMaybeTailCall(MC);
6829 return;
6830 }
6831 case Intrinsic::call_preallocated_setup: {
6832 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6833 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6834 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6835 getRoot(), SrcValue);
6836 setValue(&I, Res);
6837 DAG.setRoot(Res);
6838 return;
6839 }
6840 case Intrinsic::call_preallocated_arg: {
6841 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6842 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6843 SDValue Ops[3];
6844 Ops[0] = getRoot();
6845 Ops[1] = SrcValue;
6846 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6847 MVT::i32); // arg index
6848 SDValue Res = DAG.getNode(
6850 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6851 setValue(&I, Res);
6852 DAG.setRoot(Res.getValue(1));
6853 return;
6854 }
6855
6856 case Intrinsic::eh_typeid_for: {
6857 // Find the type id for the given typeinfo.
6858 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6859 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6860 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6861 setValue(&I, Res);
6862 return;
6863 }
6864
6865 case Intrinsic::eh_return_i32:
6866 case Intrinsic::eh_return_i64:
6867 DAG.getMachineFunction().setCallsEHReturn(true);
6868 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6869 MVT::Other,
6871 getValue(I.getArgOperand(0)),
6872 getValue(I.getArgOperand(1))));
6873 return;
6874 case Intrinsic::eh_unwind_init:
6875 DAG.getMachineFunction().setCallsUnwindInit(true);
6876 return;
6877 case Intrinsic::eh_dwarf_cfa:
6878 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6879 TLI.getPointerTy(DAG.getDataLayout()),
6880 getValue(I.getArgOperand(0))));
6881 return;
6882 case Intrinsic::eh_sjlj_callsite: {
6883 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6884 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6885
6886 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6887 return;
6888 }
6889 case Intrinsic::eh_sjlj_functioncontext: {
6890 // Get and store the index of the function context.
6891 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6892 AllocaInst *FnCtx =
6893 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6894 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6896 return;
6897 }
6898 case Intrinsic::eh_sjlj_setjmp: {
6899 SDValue Ops[2];
6900 Ops[0] = getRoot();
6901 Ops[1] = getValue(I.getArgOperand(0));
6902 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6903 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6904 setValue(&I, Op.getValue(0));
6905 DAG.setRoot(Op.getValue(1));
6906 return;
6907 }
6908 case Intrinsic::eh_sjlj_longjmp:
6909 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6910 getRoot(), getValue(I.getArgOperand(0))));
6911 return;
6912 case Intrinsic::eh_sjlj_setup_dispatch:
6913 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6914 getRoot()));
6915 return;
6916 case Intrinsic::masked_gather:
6917 visitMaskedGather(I);
6918 return;
6919 case Intrinsic::masked_load:
6920 visitMaskedLoad(I);
6921 return;
6922 case Intrinsic::masked_scatter:
6923 visitMaskedScatter(I);
6924 return;
6925 case Intrinsic::masked_store:
6926 visitMaskedStore(I);
6927 return;
6928 case Intrinsic::masked_expandload:
6929 visitMaskedLoad(I, true /* IsExpanding */);
6930 return;
6931 case Intrinsic::masked_compressstore:
6932 visitMaskedStore(I, true /* IsCompressing */);
6933 return;
6934 case Intrinsic::powi:
6935 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6936 getValue(I.getArgOperand(1)), DAG));
6937 return;
6938 case Intrinsic::log:
6939 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6940 return;
6941 case Intrinsic::log2:
6942 setValue(&I,
6943 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6944 return;
6945 case Intrinsic::log10:
6946 setValue(&I,
6947 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6948 return;
6949 case Intrinsic::exp:
6950 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6951 return;
6952 case Intrinsic::exp2:
6953 setValue(&I,
6954 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6955 return;
6956 case Intrinsic::pow:
6957 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6958 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6959 return;
6960 case Intrinsic::sqrt:
6961 case Intrinsic::fabs:
6962 case Intrinsic::sin:
6963 case Intrinsic::cos:
6964 case Intrinsic::tan:
6965 case Intrinsic::asin:
6966 case Intrinsic::acos:
6967 case Intrinsic::atan:
6968 case Intrinsic::sinh:
6969 case Intrinsic::cosh:
6970 case Intrinsic::tanh:
6971 case Intrinsic::exp10:
6972 case Intrinsic::floor:
6973 case Intrinsic::ceil:
6974 case Intrinsic::trunc:
6975 case Intrinsic::rint:
6976 case Intrinsic::nearbyint:
6977 case Intrinsic::round:
6978 case Intrinsic::roundeven:
6979 case Intrinsic::canonicalize: {
6980 unsigned Opcode;
6981 // clang-format off
6982 switch (Intrinsic) {
6983 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6984 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6985 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6986 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6987 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6988 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6989 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6990 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6991 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6992 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6993 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6994 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6995 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6996 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6997 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6998 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6999 case Intrinsic::rint: Opcode = ISD::FRINT; break;
7000 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
7001 case Intrinsic::round: Opcode = ISD::FROUND; break;
7002 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
7003 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
7004 }
7005 // clang-format on
7006
7007 setValue(&I, DAG.getNode(Opcode, sdl,
7008 getValue(I.getArgOperand(0)).getValueType(),
7009 getValue(I.getArgOperand(0)), Flags));
7010 return;
7011 }
7012 case Intrinsic::atan2:
7013 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
7014 getValue(I.getArgOperand(0)).getValueType(),
7015 getValue(I.getArgOperand(0)),
7016 getValue(I.getArgOperand(1)), Flags));
7017 return;
7018 case Intrinsic::lround:
7019 case Intrinsic::llround:
7020 case Intrinsic::lrint:
7021 case Intrinsic::llrint: {
7022 unsigned Opcode;
7023 // clang-format off
7024 switch (Intrinsic) {
7025 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7026 case Intrinsic::lround: Opcode = ISD::LROUND; break;
7027 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
7028 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
7029 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
7030 }
7031 // clang-format on
7032
7033 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7034 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
7035 getValue(I.getArgOperand(0))));
7036 return;
7037 }
7038 case Intrinsic::minnum:
7039 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
7040 getValue(I.getArgOperand(0)).getValueType(),
7041 getValue(I.getArgOperand(0)),
7042 getValue(I.getArgOperand(1)), Flags));
7043 return;
7044 case Intrinsic::maxnum:
7045 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
7046 getValue(I.getArgOperand(0)).getValueType(),
7047 getValue(I.getArgOperand(0)),
7048 getValue(I.getArgOperand(1)), Flags));
7049 return;
7050 case Intrinsic::minimum:
7051 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
7052 getValue(I.getArgOperand(0)).getValueType(),
7053 getValue(I.getArgOperand(0)),
7054 getValue(I.getArgOperand(1)), Flags));
7055 return;
7056 case Intrinsic::maximum:
7057 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
7058 getValue(I.getArgOperand(0)).getValueType(),
7059 getValue(I.getArgOperand(0)),
7060 getValue(I.getArgOperand(1)), Flags));
7061 return;
7062 case Intrinsic::minimumnum:
7063 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
7064 getValue(I.getArgOperand(0)).getValueType(),
7065 getValue(I.getArgOperand(0)),
7066 getValue(I.getArgOperand(1)), Flags));
7067 return;
7068 case Intrinsic::maximumnum:
7069 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
7070 getValue(I.getArgOperand(0)).getValueType(),
7071 getValue(I.getArgOperand(0)),
7072 getValue(I.getArgOperand(1)), Flags));
7073 return;
7074 case Intrinsic::copysign:
7075 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
7076 getValue(I.getArgOperand(0)).getValueType(),
7077 getValue(I.getArgOperand(0)),
7078 getValue(I.getArgOperand(1)), Flags));
7079 return;
7080 case Intrinsic::ldexp:
7081 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
7082 getValue(I.getArgOperand(0)).getValueType(),
7083 getValue(I.getArgOperand(0)),
7084 getValue(I.getArgOperand(1)), Flags));
7085 return;
7086 case Intrinsic::modf:
7087 case Intrinsic::sincos:
7088 case Intrinsic::sincospi:
7089 case Intrinsic::frexp: {
7090 unsigned Opcode;
7091 switch (Intrinsic) {
7092 default:
7093 llvm_unreachable("unexpected intrinsic");
7094 case Intrinsic::sincos:
7095 Opcode = ISD::FSINCOS;
7096 break;
7097 case Intrinsic::sincospi:
7098 Opcode = ISD::FSINCOSPI;
7099 break;
7100 case Intrinsic::modf:
7101 Opcode = ISD::FMODF;
7102 break;
7103 case Intrinsic::frexp:
7104 Opcode = ISD::FFREXP;
7105 break;
7106 }
7107 SmallVector<EVT, 2> ValueVTs;
7108 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
7109 SDVTList VTs = DAG.getVTList(ValueVTs);
7110 setValue(
7111 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
7112 return;
7113 }
7114 case Intrinsic::arithmetic_fence: {
7115 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
7116 getValue(I.getArgOperand(0)).getValueType(),
7117 getValue(I.getArgOperand(0)), Flags));
7118 return;
7119 }
7120 case Intrinsic::fma:
7121 setValue(&I, DAG.getNode(
7122 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
7123 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
7124 getValue(I.getArgOperand(2)), Flags));
7125 return;
7126#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
7127 case Intrinsic::INTRINSIC:
7128#include "llvm/IR/ConstrainedOps.def"
7129 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
7130 return;
7131#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
7132#include "llvm/IR/VPIntrinsics.def"
7133 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
7134 return;
7135 case Intrinsic::fptrunc_round: {
7136 // Get the last argument, the metadata and convert it to an integer in the
7137 // call
7138 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7139 std::optional<RoundingMode> RoundMode =
7140 convertStrToRoundingMode(cast<MDString>(MD)->getString());
7141
7142 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7143
7144 // Propagate fast-math-flags from IR to node(s).
7145 SDNodeFlags Flags;
7146 Flags.copyFMF(*cast<FPMathOperator>(&I));
7147 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
7148
7150 Result = DAG.getNode(
7151 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
7152 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
7153 setValue(&I, Result);
7154
7155 return;
7156 }
7157 case Intrinsic::fmuladd: {
7158 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7159 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
7160 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
7161 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7162 getValue(I.getArgOperand(0)).getValueType(),
7163 getValue(I.getArgOperand(0)),
7164 getValue(I.getArgOperand(1)),
7165 getValue(I.getArgOperand(2)), Flags));
7166 } else if (TLI.isOperationLegalOrCustom(ISD::FMULADD, VT)) {
7167 // TODO: Support splitting the vector.
7168 setValue(&I, DAG.getNode(ISD::FMULADD, sdl,
7169 getValue(I.getArgOperand(0)).getValueType(),
7170 getValue(I.getArgOperand(0)),
7171 getValue(I.getArgOperand(1)),
7172 getValue(I.getArgOperand(2)), Flags));
7173 } else {
7174 // TODO: Intrinsic calls should have fast-math-flags.
7175 SDValue Mul = DAG.getNode(
7176 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7177 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7178 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7179 getValue(I.getArgOperand(0)).getValueType(),
7180 Mul, getValue(I.getArgOperand(2)), Flags);
7181 setValue(&I, Add);
7182 }
7183 return;
7184 }
7185 case Intrinsic::fptosi_sat: {
7186 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7187 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7188 getValue(I.getArgOperand(0)),
7189 DAG.getValueType(VT.getScalarType())));
7190 return;
7191 }
7192 case Intrinsic::fptoui_sat: {
7193 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7194 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7195 getValue(I.getArgOperand(0)),
7196 DAG.getValueType(VT.getScalarType())));
7197 return;
7198 }
7199 case Intrinsic::convert_from_arbitrary_fp: {
7200 // Extract format metadata and convert to semantics enum.
7201 EVT DstVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7202 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7203 StringRef FormatStr = cast<MDString>(MD)->getString();
7204 const fltSemantics *SrcSem =
7206 if (!SrcSem) {
7207 DAG.getContext()->emitError(
7208 "convert_from_arbitrary_fp: not implemented format '" + FormatStr +
7209 "'");
7210 setValue(&I, DAG.getPOISON(DstVT));
7211 return;
7212 }
7214
7215 SDValue IntVal = getValue(I.getArgOperand(0));
7216
7217 // Emit ISD::CONVERT_FROM_ARBITRARY_FP node.
7218 SDValue SemConst =
7219 DAG.getTargetConstant(static_cast<int>(SemEnum), sdl, MVT::i32);
7220 setValue(&I, DAG.getNode(ISD::CONVERT_FROM_ARBITRARY_FP, sdl, DstVT, IntVal,
7221 SemConst));
7222 return;
7223 }
7224 case Intrinsic::set_rounding:
7225 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7226 {getRoot(), getValue(I.getArgOperand(0))});
7227 setValue(&I, Res);
7228 DAG.setRoot(Res.getValue(0));
7229 return;
7230 case Intrinsic::is_fpclass: {
7231 const DataLayout DLayout = DAG.getDataLayout();
7232 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7233 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7234 FPClassTest Test = static_cast<FPClassTest>(
7235 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7236 MachineFunction &MF = DAG.getMachineFunction();
7237 const Function &F = MF.getFunction();
7238 SDValue Op = getValue(I.getArgOperand(0));
7239 SDNodeFlags Flags;
7240 Flags.setNoFPExcept(
7241 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7242 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7243 // expansion can use illegal types. Making expansion early allows
7244 // legalizing these types prior to selection.
7245 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7246 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7247 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7248 setValue(&I, Result);
7249 return;
7250 }
7251
7252 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7253 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7254 setValue(&I, V);
7255 return;
7256 }
7257 case Intrinsic::get_fpenv: {
7258 const DataLayout DLayout = DAG.getDataLayout();
7259 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7260 Align TempAlign = DAG.getEVTAlign(EnvVT);
7261 SDValue Chain = getRoot();
7262 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7263 // and temporary storage in stack.
7264 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7265 Res = DAG.getNode(
7266 ISD::GET_FPENV, sdl,
7267 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7268 MVT::Other),
7269 Chain);
7270 } else {
7271 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7272 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7273 auto MPI =
7274 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7275 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7277 TempAlign);
7278 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7279 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7280 }
7281 setValue(&I, Res);
7282 DAG.setRoot(Res.getValue(1));
7283 return;
7284 }
7285 case Intrinsic::set_fpenv: {
7286 const DataLayout DLayout = DAG.getDataLayout();
7287 SDValue Env = getValue(I.getArgOperand(0));
7288 EVT EnvVT = Env.getValueType();
7289 Align TempAlign = DAG.getEVTAlign(EnvVT);
7290 SDValue Chain = getRoot();
7291 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7292 // environment from memory.
7293 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7294 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7295 } else {
7296 // Allocate space in stack, copy environment bits into it and use this
7297 // memory in SET_FPENV_MEM.
7298 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7299 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7300 auto MPI =
7301 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7302 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7304 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7306 TempAlign);
7307 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7308 }
7309 DAG.setRoot(Chain);
7310 return;
7311 }
7312 case Intrinsic::reset_fpenv:
7313 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7314 return;
7315 case Intrinsic::get_fpmode:
7316 Res = DAG.getNode(
7317 ISD::GET_FPMODE, sdl,
7318 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7319 MVT::Other),
7320 DAG.getRoot());
7321 setValue(&I, Res);
7322 DAG.setRoot(Res.getValue(1));
7323 return;
7324 case Intrinsic::set_fpmode:
7325 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7326 getValue(I.getArgOperand(0)));
7327 DAG.setRoot(Res);
7328 return;
7329 case Intrinsic::reset_fpmode: {
7330 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7331 DAG.setRoot(Res);
7332 return;
7333 }
7334 case Intrinsic::pcmarker: {
7335 SDValue Tmp = getValue(I.getArgOperand(0));
7336 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7337 return;
7338 }
7339 case Intrinsic::readcyclecounter: {
7340 SDValue Op = getRoot();
7341 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7342 DAG.getVTList(MVT::i64, MVT::Other), Op);
7343 setValue(&I, Res);
7344 DAG.setRoot(Res.getValue(1));
7345 return;
7346 }
7347 case Intrinsic::readsteadycounter: {
7348 SDValue Op = getRoot();
7349 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7350 DAG.getVTList(MVT::i64, MVT::Other), Op);
7351 setValue(&I, Res);
7352 DAG.setRoot(Res.getValue(1));
7353 return;
7354 }
7355 case Intrinsic::bitreverse:
7356 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7357 getValue(I.getArgOperand(0)).getValueType(),
7358 getValue(I.getArgOperand(0))));
7359 return;
7360 case Intrinsic::bswap:
7361 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7362 getValue(I.getArgOperand(0)).getValueType(),
7363 getValue(I.getArgOperand(0))));
7364 return;
7365 case Intrinsic::cttz: {
7366 SDValue Arg = getValue(I.getArgOperand(0));
7367 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7368 EVT Ty = Arg.getValueType();
7369 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_POISON,
7370 sdl, Ty, Arg));
7371 return;
7372 }
7373 case Intrinsic::ctlz: {
7374 SDValue Arg = getValue(I.getArgOperand(0));
7375 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7376 EVT Ty = Arg.getValueType();
7377 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_POISON,
7378 sdl, Ty, Arg));
7379 return;
7380 }
7381 case Intrinsic::ctpop: {
7382 SDValue Arg = getValue(I.getArgOperand(0));
7383 EVT Ty = Arg.getValueType();
7384 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7385 return;
7386 }
7387 case Intrinsic::fshl:
7388 case Intrinsic::fshr: {
7389 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7390 SDValue X = getValue(I.getArgOperand(0));
7391 SDValue Y = getValue(I.getArgOperand(1));
7392 SDValue Z = getValue(I.getArgOperand(2));
7393 EVT VT = X.getValueType();
7394
7395 if (X == Y) {
7396 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7397 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7398 } else {
7399 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7400 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7401 }
7402 return;
7403 }
7404 case Intrinsic::clmul: {
7405 SDValue X = getValue(I.getArgOperand(0));
7406 SDValue Y = getValue(I.getArgOperand(1));
7407 setValue(&I, DAG.getNode(ISD::CLMUL, sdl, X.getValueType(), X, Y));
7408 return;
7409 }
7410 case Intrinsic::sadd_sat: {
7411 SDValue Op1 = getValue(I.getArgOperand(0));
7412 SDValue Op2 = getValue(I.getArgOperand(1));
7413 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7414 return;
7415 }
7416 case Intrinsic::uadd_sat: {
7417 SDValue Op1 = getValue(I.getArgOperand(0));
7418 SDValue Op2 = getValue(I.getArgOperand(1));
7419 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7420 return;
7421 }
7422 case Intrinsic::ssub_sat: {
7423 SDValue Op1 = getValue(I.getArgOperand(0));
7424 SDValue Op2 = getValue(I.getArgOperand(1));
7425 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7426 return;
7427 }
7428 case Intrinsic::usub_sat: {
7429 SDValue Op1 = getValue(I.getArgOperand(0));
7430 SDValue Op2 = getValue(I.getArgOperand(1));
7431 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7432 return;
7433 }
7434 case Intrinsic::sshl_sat:
7435 case Intrinsic::ushl_sat: {
7436 SDValue Op1 = getValue(I.getArgOperand(0));
7437 SDValue Op2 = getValue(I.getArgOperand(1));
7438
7439 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
7440 Op1.getValueType(), DAG.getDataLayout());
7441
7442 // Coerce the shift amount to the right type if we can. This exposes the
7443 // truncate or zext to optimization early.
7444 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
7445 assert(ShiftTy.getSizeInBits() >=
7447 "Unexpected shift type");
7448 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
7449 }
7450
7451 unsigned Opc =
7452 Intrinsic == Intrinsic::sshl_sat ? ISD::SSHLSAT : ISD::USHLSAT;
7453 setValue(&I, DAG.getNode(Opc, sdl, Op1.getValueType(), Op1, Op2));
7454 return;
7455 }
7456 case Intrinsic::smul_fix:
7457 case Intrinsic::umul_fix:
7458 case Intrinsic::smul_fix_sat:
7459 case Intrinsic::umul_fix_sat: {
7460 SDValue Op1 = getValue(I.getArgOperand(0));
7461 SDValue Op2 = getValue(I.getArgOperand(1));
7462 SDValue Op3 = getValue(I.getArgOperand(2));
7463 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7464 Op1.getValueType(), Op1, Op2, Op3));
7465 return;
7466 }
7467 case Intrinsic::sdiv_fix:
7468 case Intrinsic::udiv_fix:
7469 case Intrinsic::sdiv_fix_sat:
7470 case Intrinsic::udiv_fix_sat: {
7471 SDValue Op1 = getValue(I.getArgOperand(0));
7472 SDValue Op2 = getValue(I.getArgOperand(1));
7473 SDValue Op3 = getValue(I.getArgOperand(2));
7475 Op1, Op2, Op3, DAG, TLI));
7476 return;
7477 }
7478 case Intrinsic::smax: {
7479 SDValue Op1 = getValue(I.getArgOperand(0));
7480 SDValue Op2 = getValue(I.getArgOperand(1));
7481 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7482 return;
7483 }
7484 case Intrinsic::smin: {
7485 SDValue Op1 = getValue(I.getArgOperand(0));
7486 SDValue Op2 = getValue(I.getArgOperand(1));
7487 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7488 return;
7489 }
7490 case Intrinsic::umax: {
7491 SDValue Op1 = getValue(I.getArgOperand(0));
7492 SDValue Op2 = getValue(I.getArgOperand(1));
7493 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7494 return;
7495 }
7496 case Intrinsic::umin: {
7497 SDValue Op1 = getValue(I.getArgOperand(0));
7498 SDValue Op2 = getValue(I.getArgOperand(1));
7499 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7500 return;
7501 }
7502 case Intrinsic::abs: {
7503 SDValue Op1 = getValue(I.getArgOperand(0));
7504 bool IntMinIsPoison = cast<ConstantInt>(I.getArgOperand(1))->isOne();
7505 unsigned Opc = IntMinIsPoison ? ISD::ABS_MIN_POISON : ISD::ABS;
7506 setValue(&I, DAG.getNode(Opc, sdl, Op1.getValueType(), Op1));
7507 return;
7508 }
7509 case Intrinsic::scmp: {
7510 SDValue Op1 = getValue(I.getArgOperand(0));
7511 SDValue Op2 = getValue(I.getArgOperand(1));
7512 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7513 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7514 break;
7515 }
7516 case Intrinsic::ucmp: {
7517 SDValue Op1 = getValue(I.getArgOperand(0));
7518 SDValue Op2 = getValue(I.getArgOperand(1));
7519 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7520 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7521 break;
7522 }
7523 case Intrinsic::stackaddress:
7524 case Intrinsic::stacksave: {
7525 unsigned SDOpcode = Intrinsic == Intrinsic::stackaddress ? ISD::STACKADDRESS
7527 SDValue Op = getRoot();
7528 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7529 Res = DAG.getNode(SDOpcode, sdl, DAG.getVTList(VT, MVT::Other), Op);
7530 setValue(&I, Res);
7531 DAG.setRoot(Res.getValue(1));
7532 return;
7533 }
7534 case Intrinsic::stackrestore:
7535 Res = getValue(I.getArgOperand(0));
7536 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7537 return;
7538 case Intrinsic::get_dynamic_area_offset: {
7539 SDValue Op = getRoot();
7540 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7541 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7542 Op);
7543 DAG.setRoot(Op);
7544 setValue(&I, Res);
7545 return;
7546 }
7547 case Intrinsic::stackguard: {
7548 MachineFunction &MF = DAG.getMachineFunction();
7549 const Module &M = *MF.getFunction().getParent();
7550 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7551 SDValue Chain = getRoot();
7552 if (TLI.useLoadStackGuardNode(M)) {
7553 Res = getLoadStackGuard(DAG, sdl, Chain);
7554 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7555 } else {
7556 const Value *Global = TLI.getSDagStackGuard(M, DAG.getLibcalls());
7557 if (!Global) {
7558 LLVMContext &Ctx = *DAG.getContext();
7559 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
7560 setValue(&I, DAG.getPOISON(PtrTy));
7561 return;
7562 }
7563
7564 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7565 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7566 MachinePointerInfo(Global, 0), Align,
7568 }
7569 if (TLI.useStackGuardXorFP())
7570 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7571 DAG.setRoot(Chain);
7572 setValue(&I, Res);
7573 return;
7574 }
7575 case Intrinsic::stackprotector: {
7576 // Emit code into the DAG to store the stack guard onto the stack.
7577 MachineFunction &MF = DAG.getMachineFunction();
7578 MachineFrameInfo &MFI = MF.getFrameInfo();
7579 const Module &M = *MF.getFunction().getParent();
7580 SDValue Src, Chain = getRoot();
7581
7582 if (TLI.useLoadStackGuardNode(M))
7583 Src = getLoadStackGuard(DAG, sdl, Chain);
7584 else
7585 Src = getValue(I.getArgOperand(0)); // The guard's value.
7586
7587 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7588
7589 int FI = FuncInfo.StaticAllocaMap[Slot];
7590 MFI.setStackProtectorIndex(FI);
7591 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7592
7593 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7594
7595 // Store the stack protector onto the stack.
7596 Res = DAG.getStore(
7597 Chain, sdl, Src, FIN,
7598 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7599 MaybeAlign(), MachineMemOperand::MOVolatile);
7600 setValue(&I, Res);
7601 DAG.setRoot(Res);
7602 return;
7603 }
7604 case Intrinsic::objectsize:
7605 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7606
7607 case Intrinsic::is_constant:
7608 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7609
7610 case Intrinsic::annotation:
7611 case Intrinsic::ptr_annotation:
7612 case Intrinsic::launder_invariant_group:
7613 case Intrinsic::strip_invariant_group:
7614 // Drop the intrinsic, but forward the value
7615 setValue(&I, getValue(I.getOperand(0)));
7616 return;
7617
7618 case Intrinsic::type_test:
7619 case Intrinsic::public_type_test:
7620 reportFatalUsageError("llvm.type.test intrinsic must be lowered by the "
7621 "LowerTypeTests pass before code generation");
7622 return;
7623
7624 case Intrinsic::assume:
7625 case Intrinsic::experimental_noalias_scope_decl:
7626 case Intrinsic::var_annotation:
7627 case Intrinsic::sideeffect:
7628 // Discard annotate attributes, noalias scope declarations, assumptions, and
7629 // artificial side-effects.
7630 return;
7631
7632 case Intrinsic::codeview_annotation: {
7633 // Emit a label associated with this metadata.
7634 MachineFunction &MF = DAG.getMachineFunction();
7635 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7636 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7637 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7638 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7639 DAG.setRoot(Res);
7640 return;
7641 }
7642
7643 case Intrinsic::init_trampoline: {
7644 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7645
7646 SDValue Ops[6];
7647 Ops[0] = getRoot();
7648 Ops[1] = getValue(I.getArgOperand(0));
7649 Ops[2] = getValue(I.getArgOperand(1));
7650 Ops[3] = getValue(I.getArgOperand(2));
7651 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7652 Ops[5] = DAG.getSrcValue(F);
7653
7654 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7655
7656 DAG.setRoot(Res);
7657 return;
7658 }
7659 case Intrinsic::adjust_trampoline:
7660 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7661 TLI.getPointerTy(DAG.getDataLayout()),
7662 getValue(I.getArgOperand(0))));
7663 return;
7664 case Intrinsic::gcroot: {
7665 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7666 "only valid in functions with gc specified, enforced by Verifier");
7667 assert(GFI && "implied by previous");
7668 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7669 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7670
7671 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7672 GFI->addStackRoot(FI->getIndex(), TypeMap);
7673 return;
7674 }
7675 case Intrinsic::gcread:
7676 case Intrinsic::gcwrite:
7677 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7678 case Intrinsic::get_rounding:
7679 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7680 setValue(&I, Res);
7681 DAG.setRoot(Res.getValue(1));
7682 return;
7683
7684 case Intrinsic::expect:
7685 case Intrinsic::expect_with_probability:
7686 // Just replace __builtin_expect(exp, c) and
7687 // __builtin_expect_with_probability(exp, c, p) with EXP.
7688 setValue(&I, getValue(I.getArgOperand(0)));
7689 return;
7690
7691 case Intrinsic::ubsantrap:
7692 case Intrinsic::debugtrap:
7693 case Intrinsic::trap: {
7694 StringRef TrapFuncName =
7695 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7696 if (TrapFuncName.empty()) {
7697 switch (Intrinsic) {
7698 case Intrinsic::trap:
7699 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7700 break;
7701 case Intrinsic::debugtrap:
7702 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7703 break;
7704 case Intrinsic::ubsantrap:
7705 DAG.setRoot(DAG.getNode(
7706 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7707 DAG.getTargetConstant(
7708 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7709 MVT::i32)));
7710 break;
7711 default: llvm_unreachable("unknown trap intrinsic");
7712 }
7713 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7714 I.hasFnAttr(Attribute::NoMerge));
7715 return;
7716 }
7718 if (Intrinsic == Intrinsic::ubsantrap) {
7719 Value *Arg = I.getArgOperand(0);
7720 Args.emplace_back(Arg, getValue(Arg));
7721 }
7722
7723 TargetLowering::CallLoweringInfo CLI(DAG);
7724 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7725 CallingConv::C, I.getType(),
7726 DAG.getExternalSymbol(TrapFuncName.data(),
7727 TLI.getPointerTy(DAG.getDataLayout())),
7728 std::move(Args));
7729 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7730 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7731 DAG.setRoot(Result.second);
7732 return;
7733 }
7734
7735 case Intrinsic::allow_runtime_check:
7736 case Intrinsic::allow_ubsan_check:
7737 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7738 return;
7739
7740 case Intrinsic::uadd_with_overflow:
7741 case Intrinsic::sadd_with_overflow:
7742 case Intrinsic::usub_with_overflow:
7743 case Intrinsic::ssub_with_overflow:
7744 case Intrinsic::umul_with_overflow:
7745 case Intrinsic::smul_with_overflow: {
7747 switch (Intrinsic) {
7748 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7749 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7750 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7751 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7752 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7753 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7754 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7755 }
7756 SDValue Op1 = getValue(I.getArgOperand(0));
7757 SDValue Op2 = getValue(I.getArgOperand(1));
7758
7759 EVT ResultVT = Op1.getValueType();
7760 EVT OverflowVT = ResultVT.changeElementType(*Context, MVT::i1);
7761
7762 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7763 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7764 return;
7765 }
7766 case Intrinsic::prefetch: {
7767 SDValue Ops[5];
7768 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7770 Ops[0] = DAG.getRoot();
7771 Ops[1] = getValue(I.getArgOperand(0));
7772 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7773 MVT::i32);
7774 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7775 MVT::i32);
7776 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7777 MVT::i32);
7778 SDValue Result = DAG.getMemIntrinsicNode(
7779 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7780 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7781 /* align */ std::nullopt, Flags);
7782
7783 // Chain the prefetch in parallel with any pending loads, to stay out of
7784 // the way of later optimizations.
7785 PendingLoads.push_back(Result);
7786 Result = getRoot();
7787 DAG.setRoot(Result);
7788 return;
7789 }
7790 case Intrinsic::lifetime_start:
7791 case Intrinsic::lifetime_end: {
7792 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7793 // Stack coloring is not enabled in O0, discard region information.
7794 if (TM.getOptLevel() == CodeGenOptLevel::None)
7795 return;
7796
7797 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7798 if (!LifetimeObject)
7799 return;
7800
7801 // First check that the Alloca is static, otherwise it won't have a
7802 // valid frame index.
7803 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7804 if (SI == FuncInfo.StaticAllocaMap.end())
7805 return;
7806
7807 const int FrameIndex = SI->second;
7808 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7809 DAG.setRoot(Res);
7810 return;
7811 }
7812 case Intrinsic::pseudoprobe: {
7813 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7814 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7815 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7816 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7817 DAG.setRoot(Res);
7818 return;
7819 }
7820 case Intrinsic::invariant_start:
7821 // Discard region information.
7822 setValue(&I,
7823 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7824 return;
7825 case Intrinsic::invariant_end:
7826 // Discard region information.
7827 return;
7828 case Intrinsic::clear_cache: {
7829 SDValue InputChain = DAG.getRoot();
7830 SDValue StartVal = getValue(I.getArgOperand(0));
7831 SDValue EndVal = getValue(I.getArgOperand(1));
7832 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7833 {InputChain, StartVal, EndVal});
7834 setValue(&I, Res);
7835 DAG.setRoot(Res);
7836 return;
7837 }
7838 case Intrinsic::donothing:
7839 case Intrinsic::seh_try_begin:
7840 case Intrinsic::seh_scope_begin:
7841 case Intrinsic::seh_try_end:
7842 case Intrinsic::seh_scope_end:
7843 // ignore
7844 return;
7845 case Intrinsic::experimental_stackmap:
7846 visitStackmap(I);
7847 return;
7848 case Intrinsic::experimental_patchpoint_void:
7849 case Intrinsic::experimental_patchpoint:
7850 visitPatchpoint(I);
7851 return;
7852 case Intrinsic::experimental_gc_statepoint:
7854 return;
7855 case Intrinsic::experimental_gc_result:
7856 visitGCResult(cast<GCResultInst>(I));
7857 return;
7858 case Intrinsic::experimental_gc_relocate:
7859 visitGCRelocate(cast<GCRelocateInst>(I));
7860 return;
7861 case Intrinsic::instrprof_cover:
7862 llvm_unreachable("instrprof failed to lower a cover");
7863 case Intrinsic::instrprof_increment:
7864 llvm_unreachable("instrprof failed to lower an increment");
7865 case Intrinsic::instrprof_timestamp:
7866 llvm_unreachable("instrprof failed to lower a timestamp");
7867 case Intrinsic::instrprof_value_profile:
7868 llvm_unreachable("instrprof failed to lower a value profiling call");
7869 case Intrinsic::instrprof_mcdc_parameters:
7870 llvm_unreachable("instrprof failed to lower mcdc parameters");
7871 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7872 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7873 case Intrinsic::localescape: {
7874 MachineFunction &MF = DAG.getMachineFunction();
7875 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7876
7877 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7878 // is the same on all targets.
7879 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7880 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7881 if (isa<ConstantPointerNull>(Arg))
7882 continue; // Skip null pointers. They represent a hole in index space.
7883 AllocaInst *Slot = cast<AllocaInst>(Arg);
7884 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7885 "can only escape static allocas");
7886 int FI = FuncInfo.StaticAllocaMap[Slot];
7887 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7890 TII->get(TargetOpcode::LOCAL_ESCAPE))
7891 .addSym(FrameAllocSym)
7892 .addFrameIndex(FI);
7893 }
7894
7895 return;
7896 }
7897
7898 case Intrinsic::localrecover: {
7899 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7900 MachineFunction &MF = DAG.getMachineFunction();
7901
7902 // Get the symbol that defines the frame offset.
7903 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7904 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7905 unsigned IdxVal =
7906 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7907 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7909
7910 Value *FP = I.getArgOperand(1);
7911 SDValue FPVal = getValue(FP);
7912 EVT PtrVT = FPVal.getValueType();
7913
7914 // Create a MCSymbol for the label to avoid any target lowering
7915 // that would make this PC relative.
7916 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7917 SDValue OffsetVal =
7918 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7919
7920 // Add the offset to the FP.
7921 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7922 setValue(&I, Add);
7923
7924 return;
7925 }
7926
7927 case Intrinsic::fake_use: {
7928 Value *V = I.getArgOperand(0);
7929 SDValue Ops[2];
7930 // For Values not declared or previously used in this basic block, the
7931 // NodeMap will not have an entry, and `getValue` will assert if V has no
7932 // valid register value.
7933 auto FakeUseValue = [&]() -> SDValue {
7934 SDValue &N = NodeMap[V];
7935 if (N.getNode())
7936 return N;
7937
7938 // If there's a virtual register allocated and initialized for this
7939 // value, use it.
7940 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7941 return copyFromReg;
7942 // FIXME: Do we want to preserve constants? It seems pointless.
7943 if (isa<Constant>(V))
7944 return getValue(V);
7945 return SDValue();
7946 }();
7947 if (!FakeUseValue || FakeUseValue.isUndef())
7948 return;
7949 Ops[0] = getRoot();
7950 Ops[1] = FakeUseValue;
7951 // Also, do not translate a fake use with an undef operand, or any other
7952 // empty SDValues.
7953 if (!Ops[1] || Ops[1].isUndef())
7954 return;
7955 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7956 return;
7957 }
7958
7959 case Intrinsic::reloc_none: {
7960 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7961 StringRef SymbolName = cast<MDString>(MD)->getString();
7962 SDValue Ops[2] = {
7963 getRoot(),
7964 DAG.getTargetExternalSymbol(
7965 SymbolName.data(), TLI.getProgramPointerTy(DAG.getDataLayout()))};
7966 DAG.setRoot(DAG.getNode(ISD::RELOC_NONE, sdl, MVT::Other, Ops));
7967 return;
7968 }
7969
7970 case Intrinsic::cond_loop: {
7971 SDValue InputChain = DAG.getRoot();
7972 SDValue P = getValue(I.getArgOperand(0));
7973 Res = DAG.getNode(ISD::COND_LOOP, sdl, DAG.getVTList(MVT::Other),
7974 {InputChain, P});
7975 setValue(&I, Res);
7976 DAG.setRoot(Res);
7977 return;
7978 }
7979
7980 case Intrinsic::eh_exceptionpointer:
7981 case Intrinsic::eh_exceptioncode: {
7982 // Get the exception pointer vreg, copy from it, and resize it to fit.
7983 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7984 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7985 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7986 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7987 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7988 if (Intrinsic == Intrinsic::eh_exceptioncode)
7989 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7990 setValue(&I, N);
7991 return;
7992 }
7993 case Intrinsic::xray_customevent: {
7994 // Here we want to make sure that the intrinsic behaves as if it has a
7995 // specific calling convention.
7996 const auto &Triple = DAG.getTarget().getTargetTriple();
7997 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64 &&
7998 Triple.getArch() != Triple::hexagon)
7999 return;
8000
8002
8003 // We want to say that we always want the arguments in registers.
8004 SDValue LogEntryVal = getValue(I.getArgOperand(0));
8005 SDValue StrSizeVal = getValue(I.getArgOperand(1));
8006 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
8007 SDValue Chain = getRoot();
8008 Ops.push_back(LogEntryVal);
8009 Ops.push_back(StrSizeVal);
8010 Ops.push_back(Chain);
8011
8012 // We need to enforce the calling convention for the callsite, so that
8013 // argument ordering is enforced correctly, and that register allocation can
8014 // see that some registers may be assumed clobbered and have to preserve
8015 // them across calls to the intrinsic.
8016 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
8017 sdl, NodeTys, Ops);
8018 SDValue patchableNode = SDValue(MN, 0);
8019 DAG.setRoot(patchableNode);
8020 setValue(&I, patchableNode);
8021 return;
8022 }
8023 case Intrinsic::xray_typedevent: {
8024 // Here we want to make sure that the intrinsic behaves as if it has a
8025 // specific calling convention.
8026 const auto &Triple = DAG.getTarget().getTargetTriple();
8027 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64 &&
8028 Triple.getArch() != Triple::hexagon)
8029 return;
8030
8032
8033 // We want to say that we always want the arguments in registers.
8034 // It's unclear to me how manipulating the selection DAG here forces callers
8035 // to provide arguments in registers instead of on the stack.
8036 SDValue LogTypeId = getValue(I.getArgOperand(0));
8037 SDValue LogEntryVal = getValue(I.getArgOperand(1));
8038 SDValue StrSizeVal = getValue(I.getArgOperand(2));
8039 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
8040 SDValue Chain = getRoot();
8041 Ops.push_back(LogTypeId);
8042 Ops.push_back(LogEntryVal);
8043 Ops.push_back(StrSizeVal);
8044 Ops.push_back(Chain);
8045
8046 // We need to enforce the calling convention for the callsite, so that
8047 // argument ordering is enforced correctly, and that register allocation can
8048 // see that some registers may be assumed clobbered and have to preserve
8049 // them across calls to the intrinsic.
8050 MachineSDNode *MN = DAG.getMachineNode(
8051 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
8052 SDValue patchableNode = SDValue(MN, 0);
8053 DAG.setRoot(patchableNode);
8054 setValue(&I, patchableNode);
8055 return;
8056 }
8057 case Intrinsic::experimental_deoptimize:
8059 return;
8060 case Intrinsic::stepvector:
8061 visitStepVector(I);
8062 return;
8063 case Intrinsic::vector_reduce_fadd:
8064 case Intrinsic::vector_reduce_fmul:
8065 case Intrinsic::vector_reduce_add:
8066 case Intrinsic::vector_reduce_mul:
8067 case Intrinsic::vector_reduce_and:
8068 case Intrinsic::vector_reduce_or:
8069 case Intrinsic::vector_reduce_xor:
8070 case Intrinsic::vector_reduce_smax:
8071 case Intrinsic::vector_reduce_smin:
8072 case Intrinsic::vector_reduce_umax:
8073 case Intrinsic::vector_reduce_umin:
8074 case Intrinsic::vector_reduce_fmax:
8075 case Intrinsic::vector_reduce_fmin:
8076 case Intrinsic::vector_reduce_fmaximum:
8077 case Intrinsic::vector_reduce_fminimum:
8078 visitVectorReduce(I, Intrinsic);
8079 return;
8080
8081 case Intrinsic::icall_branch_funnel: {
8083 Ops.push_back(getValue(I.getArgOperand(0)));
8084
8085 int64_t Offset;
8087 I.getArgOperand(1), Offset, DAG.getDataLayout()));
8088 if (!Base)
8090 "llvm.icall.branch.funnel operand must be a GlobalValue");
8091 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
8092
8093 struct BranchFunnelTarget {
8094 int64_t Offset;
8096 };
8098
8099 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
8101 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
8102 if (ElemBase != Base)
8103 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
8104 "to the same GlobalValue");
8105
8106 SDValue Val = getValue(I.getArgOperand(Op + 1));
8107 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
8108 if (!GA)
8110 "llvm.icall.branch.funnel operand must be a GlobalValue");
8111 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
8112 GA->getGlobal(), sdl, Val.getValueType(),
8113 GA->getOffset())});
8114 }
8115 llvm::sort(Targets,
8116 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
8117 return T1.Offset < T2.Offset;
8118 });
8119
8120 for (auto &T : Targets) {
8121 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
8122 Ops.push_back(T.Target);
8123 }
8124
8125 Ops.push_back(DAG.getRoot()); // Chain
8126 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
8127 MVT::Other, Ops),
8128 0);
8129 DAG.setRoot(N);
8130 setValue(&I, N);
8131 HasTailCall = true;
8132 return;
8133 }
8134
8135 case Intrinsic::wasm_landingpad_index:
8136 // Information this intrinsic contained has been transferred to
8137 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
8138 // delete it now.
8139 return;
8140
8141 case Intrinsic::aarch64_settag:
8142 case Intrinsic::aarch64_settag_zero: {
8143 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8144 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
8146 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
8147 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
8148 ZeroMemory);
8149 DAG.setRoot(Val);
8150 setValue(&I, Val);
8151 return;
8152 }
8153 case Intrinsic::amdgcn_cs_chain: {
8154 // At this point we don't care if it's amdgpu_cs_chain or
8155 // amdgpu_cs_chain_preserve.
8157
8158 Type *RetTy = I.getType();
8159 assert(RetTy->isVoidTy() && "Should not return");
8160
8161 SDValue Callee = getValue(I.getOperand(0));
8162
8163 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
8164 // We'll also tack the value of the EXEC mask at the end.
8166 Args.reserve(3);
8167
8168 for (unsigned Idx : {2, 3, 1}) {
8169 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8170 I.getOperand(Idx)->getType());
8171 Arg.setAttributes(&I, Idx);
8172 Args.push_back(Arg);
8173 }
8174
8175 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
8176 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
8177 Args[2].IsInReg = true; // EXEC should be inreg
8178
8179 // Forward the flags and any additional arguments.
8180 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
8181 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8182 I.getOperand(Idx)->getType());
8183 Arg.setAttributes(&I, Idx);
8184 Args.push_back(Arg);
8185 }
8186
8187 TargetLowering::CallLoweringInfo CLI(DAG);
8188 CLI.setDebugLoc(getCurSDLoc())
8189 .setChain(getRoot())
8190 .setCallee(CC, RetTy, Callee, std::move(Args))
8191 .setNoReturn(true)
8192 .setTailCall(true)
8193 .setConvergent(I.isConvergent());
8194 CLI.CB = &I;
8195 std::pair<SDValue, SDValue> Result =
8196 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
8197 (void)Result;
8198 assert(!Result.first.getNode() && !Result.second.getNode() &&
8199 "Should've lowered as tail call");
8200
8201 HasTailCall = true;
8202 return;
8203 }
8204 case Intrinsic::amdgcn_call_whole_wave: {
8206 bool isTailCall = I.isTailCall();
8207
8208 // The first argument is the callee. Skip it when assembling the call args.
8209 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
8210 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
8211 I.getArgOperand(Idx)->getType());
8212 Arg.setAttributes(&I, Idx);
8213
8214 // If we have an explicit sret argument that is an Instruction, (i.e., it
8215 // might point to function-local memory), we can't meaningfully tail-call.
8216 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
8217 isTailCall = false;
8218
8219 Args.push_back(Arg);
8220 }
8221
8222 SDValue ConvControlToken;
8223 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8224 auto *Token = Bundle->Inputs[0].get();
8225 ConvControlToken = getValue(Token);
8226 }
8227
8228 TargetLowering::CallLoweringInfo CLI(DAG);
8229 CLI.setDebugLoc(getCurSDLoc())
8230 .setChain(getRoot())
8231 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8232 getValue(I.getArgOperand(0)), std::move(Args))
8233 .setTailCall(isTailCall && canTailCall(I))
8234 .setIsPreallocated(
8235 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8236 .setConvergent(I.isConvergent())
8237 .setConvergenceControlToken(ConvControlToken);
8238 CLI.CB = &I;
8239
8240 std::pair<SDValue, SDValue> Result =
8241 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8242
8243 if (Result.first.getNode())
8244 setValue(&I, Result.first);
8245 return;
8246 }
8247 case Intrinsic::ptrmask: {
8248 SDValue Ptr = getValue(I.getOperand(0));
8249 SDValue Mask = getValue(I.getOperand(1));
8250
8251 // On arm64_32, pointers are 32 bits when stored in memory, but
8252 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8253 // match the index type, but the pointer is 64 bits, so the mask must be
8254 // zero-extended up to 64 bits to match the pointer.
8255 EVT PtrVT =
8256 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8257 EVT MemVT =
8258 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8259 assert(PtrVT == Ptr.getValueType());
8260 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8261 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8262 // 128-bit, so we have to pad the mask with ones for unused bits.
8263 auto HighOnes = DAG.getNode(
8264 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8265 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8266 PtrVT, sdl));
8267 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8268 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8269 } else if (Mask.getValueType() != PtrVT)
8270 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8271
8272 assert(Mask.getValueType() == PtrVT);
8273 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8274 return;
8275 }
8276 case Intrinsic::threadlocal_address: {
8277 setValue(&I, getValue(I.getOperand(0)));
8278 return;
8279 }
8280 case Intrinsic::get_active_lane_mask: {
8281 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8282 SDValue Index = getValue(I.getOperand(0));
8283 SDValue TripCount = getValue(I.getOperand(1));
8284 EVT ElementVT = Index.getValueType();
8285
8286 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8287 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8288 TripCount));
8289 return;
8290 }
8291
8292 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8293 CCVT.getVectorElementCount());
8294
8295 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8296 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8297 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8298 SDValue VectorInduction = DAG.getNode(
8299 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8300 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8301 VectorTripCount, ISD::CondCode::SETULT);
8302 setValue(&I, SetCC);
8303 return;
8304 }
8305 case Intrinsic::experimental_get_vector_length: {
8306 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8307 "Expected positive VF");
8308 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8309 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8310
8311 SDValue Count = getValue(I.getOperand(0));
8312 EVT CountVT = Count.getValueType();
8313
8314 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8315 visitTargetIntrinsic(I, Intrinsic);
8316 return;
8317 }
8318
8319 // Expand to a umin between the trip count and the maximum elements the type
8320 // can hold.
8321 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8322
8323 // Extend the trip count to at least the result VT.
8324 if (CountVT.bitsLT(VT)) {
8325 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8326 CountVT = VT;
8327 }
8328
8329 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8330 ElementCount::get(VF, IsScalable));
8331
8332 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8333 // Clip to the result type if needed.
8334 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8335
8336 setValue(&I, Trunc);
8337 return;
8338 }
8339 case Intrinsic::vector_partial_reduce_add: {
8340 SDValue Acc = getValue(I.getOperand(0));
8341 SDValue Input = getValue(I.getOperand(1));
8342 setValue(&I,
8343 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8344 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8345 return;
8346 }
8347 case Intrinsic::vector_partial_reduce_fadd: {
8348 SDValue Acc = getValue(I.getOperand(0));
8349 SDValue Input = getValue(I.getOperand(1));
8350 setValue(&I, DAG.getNode(
8351 ISD::PARTIAL_REDUCE_FMLA, sdl, Acc.getValueType(), Acc,
8352 Input, DAG.getConstantFP(1.0, sdl, Input.getValueType())));
8353 return;
8354 }
8355 case Intrinsic::experimental_cttz_elts: {
8356 SDValue Op = getValue(I.getOperand(0));
8357 EVT OpVT = Op.getValueType();
8358 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8359 bool ZeroIsPoison =
8360 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8361 if (OpVT.getVectorElementType() != MVT::i1) {
8362 // Compare the input vector elements to zero & use to count trailing
8363 // zeros.
8364 SDValue AllZero = DAG.getConstant(0, sdl, OpVT);
8365 EVT I1OpVT = OpVT.changeVectorElementType(*DAG.getContext(), MVT::i1);
8366 Op = DAG.getSetCC(sdl, I1OpVT, Op, AllZero, ISD::SETNE);
8367 }
8368 setValue(&I, DAG.getNode(ZeroIsPoison ? ISD::CTTZ_ELTS_ZERO_POISON
8370 sdl, RetTy, Op));
8371 return;
8372 }
8373 case Intrinsic::vector_insert: {
8374 SDValue Vec = getValue(I.getOperand(0));
8375 SDValue SubVec = getValue(I.getOperand(1));
8376 SDValue Index = getValue(I.getOperand(2));
8377
8378 // The intrinsic's index type is i64, but the SDNode requires an index type
8379 // suitable for the target. Convert the index as required.
8380 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8381 if (Index.getValueType() != VectorIdxTy)
8382 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8383
8384 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8385 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8386 Index));
8387 return;
8388 }
8389 case Intrinsic::vector_extract: {
8390 SDValue Vec = getValue(I.getOperand(0));
8391 SDValue Index = getValue(I.getOperand(1));
8392 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8393
8394 // The intrinsic's index type is i64, but the SDNode requires an index type
8395 // suitable for the target. Convert the index as required.
8396 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8397 if (Index.getValueType() != VectorIdxTy)
8398 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8399
8400 setValue(&I,
8401 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8402 return;
8403 }
8404 case Intrinsic::experimental_vector_match: {
8405 SDValue Op1 = getValue(I.getOperand(0));
8406 SDValue Op2 = getValue(I.getOperand(1));
8407 SDValue Mask = getValue(I.getOperand(2));
8408 EVT Op1VT = Op1.getValueType();
8409 EVT Op2VT = Op2.getValueType();
8410 EVT ResVT = Mask.getValueType();
8411 unsigned SearchSize = Op2VT.getVectorNumElements();
8412
8413 // If the target has native support for this vector match operation, lower
8414 // the intrinsic untouched; otherwise, expand it below.
8415 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8416 visitTargetIntrinsic(I, Intrinsic);
8417 return;
8418 }
8419
8420 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8421
8422 for (unsigned i = 0; i < SearchSize; ++i) {
8423 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8424 Op2VT.getVectorElementType(), Op2,
8425 DAG.getVectorIdxConstant(i, sdl));
8426 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8427 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8428 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8429 }
8430
8431 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8432 return;
8433 }
8434 case Intrinsic::vector_reverse:
8435 visitVectorReverse(I);
8436 return;
8437 case Intrinsic::vector_splice_left:
8438 case Intrinsic::vector_splice_right:
8439 visitVectorSplice(I);
8440 return;
8441 case Intrinsic::callbr_landingpad:
8442 visitCallBrLandingPad(I);
8443 return;
8444 case Intrinsic::vector_interleave2:
8445 visitVectorInterleave(I, 2);
8446 return;
8447 case Intrinsic::vector_interleave3:
8448 visitVectorInterleave(I, 3);
8449 return;
8450 case Intrinsic::vector_interleave4:
8451 visitVectorInterleave(I, 4);
8452 return;
8453 case Intrinsic::vector_interleave5:
8454 visitVectorInterleave(I, 5);
8455 return;
8456 case Intrinsic::vector_interleave6:
8457 visitVectorInterleave(I, 6);
8458 return;
8459 case Intrinsic::vector_interleave7:
8460 visitVectorInterleave(I, 7);
8461 return;
8462 case Intrinsic::vector_interleave8:
8463 visitVectorInterleave(I, 8);
8464 return;
8465 case Intrinsic::vector_deinterleave2:
8466 visitVectorDeinterleave(I, 2);
8467 return;
8468 case Intrinsic::vector_deinterleave3:
8469 visitVectorDeinterleave(I, 3);
8470 return;
8471 case Intrinsic::vector_deinterleave4:
8472 visitVectorDeinterleave(I, 4);
8473 return;
8474 case Intrinsic::vector_deinterleave5:
8475 visitVectorDeinterleave(I, 5);
8476 return;
8477 case Intrinsic::vector_deinterleave6:
8478 visitVectorDeinterleave(I, 6);
8479 return;
8480 case Intrinsic::vector_deinterleave7:
8481 visitVectorDeinterleave(I, 7);
8482 return;
8483 case Intrinsic::vector_deinterleave8:
8484 visitVectorDeinterleave(I, 8);
8485 return;
8486 case Intrinsic::experimental_vector_compress:
8487 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8488 getValue(I.getArgOperand(0)).getValueType(),
8489 getValue(I.getArgOperand(0)),
8490 getValue(I.getArgOperand(1)),
8491 getValue(I.getArgOperand(2)), Flags));
8492 return;
8493 case Intrinsic::experimental_convergence_anchor:
8494 case Intrinsic::experimental_convergence_entry:
8495 case Intrinsic::experimental_convergence_loop:
8496 visitConvergenceControl(I, Intrinsic);
8497 return;
8498 case Intrinsic::experimental_vector_histogram_add: {
8499 visitVectorHistogram(I, Intrinsic);
8500 return;
8501 }
8502 case Intrinsic::experimental_vector_extract_last_active: {
8503 visitVectorExtractLastActive(I, Intrinsic);
8504 return;
8505 }
8506 case Intrinsic::loop_dependence_war_mask:
8507 setValue(&I,
8509 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8510 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8511 DAG.getConstant(0, sdl, MVT::i64)));
8512 return;
8513 case Intrinsic::loop_dependence_raw_mask:
8514 setValue(&I,
8516 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8517 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8518 DAG.getConstant(0, sdl, MVT::i64)));
8519 return;
8520 case Intrinsic::masked_udiv:
8521 setValue(&I,
8522 DAG.getNode(ISD::MASKED_UDIV, sdl, EVT::getEVT(I.getType()),
8523 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8524 getValue(I.getOperand(2))));
8525 return;
8526 case Intrinsic::masked_sdiv:
8527 setValue(&I,
8528 DAG.getNode(ISD::MASKED_SDIV, sdl, EVT::getEVT(I.getType()),
8529 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8530 getValue(I.getOperand(2))));
8531 return;
8532 case Intrinsic::masked_urem:
8533 setValue(&I,
8534 DAG.getNode(ISD::MASKED_UREM, sdl, EVT::getEVT(I.getType()),
8535 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8536 getValue(I.getOperand(2))));
8537 return;
8538 case Intrinsic::masked_srem:
8539 setValue(&I,
8540 DAG.getNode(ISD::MASKED_SREM, sdl, EVT::getEVT(I.getType()),
8541 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8542 getValue(I.getOperand(2))));
8543 return;
8544 }
8545}
8546
8547void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
8549 assert(Result.getNode()->getNumValues() == 2);
8550 SDValue OutChain = Result.getValue(1);
8551 assert(OutChain.getValueType() == MVT::Other);
8552
8553 // Instead of updating the root immediately, push the produced chain to the
8554 // appropriate list, deferring the update until the root is requested. In this
8555 // case, the nodes from the lists are chained using TokenFactor, indicating
8556 // that the operations are independent.
8557 //
8558 // In particular, the root is updated before any call that might access the
8559 // floating-point environment, except for constrained intrinsics.
8560 switch (EB) {
8563 PendingConstrainedFP.push_back(OutChain);
8564 break;
8566 PendingConstrainedFPStrict.push_back(OutChain);
8567 break;
8568 }
8569}
8570
8571void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8572 const ConstrainedFPIntrinsic &FPI) {
8573 SDLoc sdl = getCurSDLoc();
8574
8575 // We do not need to serialize constrained FP intrinsics against
8576 // each other or against (nonvolatile) loads, so they can be
8577 // chained like loads.
8579 SDValue Chain = getFPOperationRoot(EB);
8581 Opers.push_back(Chain);
8582 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8583 Opers.push_back(getValue(FPI.getArgOperand(I)));
8584
8585 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8586 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8587 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8588
8589 SDNodeFlags Flags;
8591 Flags.setNoFPExcept(true);
8592
8593 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8594 Flags.copyFMF(*FPOp);
8595
8596 unsigned Opcode;
8597 switch (FPI.getIntrinsicID()) {
8598 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8599#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8600 case Intrinsic::INTRINSIC: \
8601 Opcode = ISD::STRICT_##DAGN; \
8602 break;
8603#include "llvm/IR/ConstrainedOps.def"
8604 case Intrinsic::experimental_constrained_fmuladd: {
8605 Opcode = ISD::STRICT_FMA;
8606 // Break fmuladd into fmul and fadd.
8607 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8608 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8609 Opers.pop_back();
8610 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8611 pushFPOpOutChain(Mul, EB);
8612 Opcode = ISD::STRICT_FADD;
8613 Opers.clear();
8614 Opers.push_back(Mul.getValue(1));
8615 Opers.push_back(Mul.getValue(0));
8616 Opers.push_back(getValue(FPI.getArgOperand(2)));
8617 }
8618 break;
8619 }
8620 }
8621
8622 // A few strict DAG nodes carry additional operands that are not
8623 // set up by the default code above.
8624 switch (Opcode) {
8625 default: break;
8627 Opers.push_back(
8628 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8629 break;
8630 case ISD::STRICT_FSETCC:
8631 case ISD::STRICT_FSETCCS: {
8632 auto *FPCmp = dyn_cast<ConstrainedFPCmpIntrinsic>(&FPI);
8633 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8634 if (DAG.isKnownNeverNaN(Opers[1]) && DAG.isKnownNeverNaN(Opers[2]))
8635 Condition = getFCmpCodeWithoutNaN(Condition);
8636 Opers.push_back(DAG.getCondCode(Condition));
8637 break;
8638 }
8639 }
8640
8641 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8642 pushFPOpOutChain(Result, EB);
8643
8644 SDValue FPResult = Result.getValue(0);
8645 setValue(&FPI, FPResult);
8646}
8647
8648static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8649 std::optional<unsigned> ResOPC;
8650 switch (VPIntrin.getIntrinsicID()) {
8651 case Intrinsic::vp_ctlz: {
8652 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8653 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_POISON : ISD::VP_CTLZ;
8654 break;
8655 }
8656 case Intrinsic::vp_cttz: {
8657 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8658 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_POISON : ISD::VP_CTTZ;
8659 break;
8660 }
8661 case Intrinsic::vp_cttz_elts: {
8662 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8663 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_POISON : ISD::VP_CTTZ_ELTS;
8664 break;
8665 }
8666#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8667 case Intrinsic::VPID: \
8668 ResOPC = ISD::VPSD; \
8669 break;
8670#include "llvm/IR/VPIntrinsics.def"
8671 }
8672
8673 if (!ResOPC)
8675 "Inconsistency: no SDNode available for this VPIntrinsic!");
8676
8677 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8678 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8679 if (VPIntrin.getFastMathFlags().allowReassoc())
8680 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8681 : ISD::VP_REDUCE_FMUL;
8682 }
8683
8684 return *ResOPC;
8685}
8686
8687void SelectionDAGBuilder::visitVPLoad(
8688 const VPIntrinsic &VPIntrin, EVT VT,
8689 const SmallVectorImpl<SDValue> &OpValues) {
8690 SDLoc DL = getCurSDLoc();
8691 Value *PtrOperand = VPIntrin.getArgOperand(0);
8692 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8693 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8694 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8695 SDValue LD;
8696 // Do not serialize variable-length loads of constant memory with
8697 // anything.
8698 if (!Alignment)
8699 Alignment = DAG.getEVTAlign(VT);
8700 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8701 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8702 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8703 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8704 MachineMemOperand::Flags MMOFlags =
8705 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8706 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8707 MachinePointerInfo(PtrOperand), MMOFlags,
8708 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8709 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8710 MMO, false /*IsExpanding */);
8711 if (AddToChain)
8712 PendingLoads.push_back(LD.getValue(1));
8713 setValue(&VPIntrin, LD);
8714}
8715
8716void SelectionDAGBuilder::visitVPLoadFF(
8717 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8718 const SmallVectorImpl<SDValue> &OpValues) {
8719 assert(OpValues.size() == 3 && "Unexpected number of operands");
8720 SDLoc DL = getCurSDLoc();
8721 Value *PtrOperand = VPIntrin.getArgOperand(0);
8722 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8723 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8724 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8725 SDValue LD;
8726 // Do not serialize variable-length loads of constant memory with
8727 // anything.
8728 if (!Alignment)
8729 Alignment = DAG.getEVTAlign(VT);
8730 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8731 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8732 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8733 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8734 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8735 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8736 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8737 MMO);
8738 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8739 if (AddToChain)
8740 PendingLoads.push_back(LD.getValue(2));
8741 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8742}
8743
8744void SelectionDAGBuilder::visitVPGather(
8745 const VPIntrinsic &VPIntrin, EVT VT,
8746 const SmallVectorImpl<SDValue> &OpValues) {
8747 SDLoc DL = getCurSDLoc();
8748 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8749 Value *PtrOperand = VPIntrin.getArgOperand(0);
8750 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8751 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8752 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8753 SDValue LD;
8754 if (!Alignment)
8755 Alignment = DAG.getEVTAlign(VT.getScalarType());
8756 unsigned AS =
8757 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8758 MachineMemOperand::Flags MMOFlags =
8759 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8760 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8761 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8762 *Alignment, AAInfo, Ranges);
8763 SDValue Base, Index, Scale;
8764 bool UniformBase =
8765 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8766 VT.getScalarStoreSize());
8767 if (!UniformBase) {
8768 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8769 Index = getValue(PtrOperand);
8770 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8771 }
8772 EVT IdxVT = Index.getValueType();
8773 EVT EltTy = IdxVT.getVectorElementType();
8774 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8775 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8776 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8777 }
8778 LD = DAG.getGatherVP(
8779 DAG.getVTList(VT, MVT::Other), VT, DL,
8780 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8782 PendingLoads.push_back(LD.getValue(1));
8783 setValue(&VPIntrin, LD);
8784}
8785
8786void SelectionDAGBuilder::visitVPStore(
8787 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8788 SDLoc DL = getCurSDLoc();
8789 Value *PtrOperand = VPIntrin.getArgOperand(1);
8790 EVT VT = OpValues[0].getValueType();
8791 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8792 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8793 SDValue ST;
8794 if (!Alignment)
8795 Alignment = DAG.getEVTAlign(VT);
8796 SDValue Ptr = OpValues[1];
8797 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8798 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8799 MachineMemOperand::Flags MMOFlags =
8800 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8801 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8802 MachinePointerInfo(PtrOperand), MMOFlags,
8803 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8804 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8805 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8806 /* IsTruncating */ false, /*IsCompressing*/ false);
8807 DAG.setRoot(ST);
8808 setValue(&VPIntrin, ST);
8809}
8810
8811void SelectionDAGBuilder::visitVPScatter(
8812 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8813 SDLoc DL = getCurSDLoc();
8814 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8815 Value *PtrOperand = VPIntrin.getArgOperand(1);
8816 EVT VT = OpValues[0].getValueType();
8817 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8818 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8819 SDValue ST;
8820 if (!Alignment)
8821 Alignment = DAG.getEVTAlign(VT.getScalarType());
8822 unsigned AS =
8823 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8824 MachineMemOperand::Flags MMOFlags =
8825 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8826 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8827 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8828 *Alignment, AAInfo);
8829 SDValue Base, Index, Scale;
8830 bool UniformBase =
8831 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8832 VT.getScalarStoreSize());
8833 if (!UniformBase) {
8834 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8835 Index = getValue(PtrOperand);
8836 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8837 }
8838 EVT IdxVT = Index.getValueType();
8839 EVT EltTy = IdxVT.getVectorElementType();
8840 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8841 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8842 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8843 }
8844 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8845 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8846 OpValues[2], OpValues[3]},
8847 MMO, ISD::SIGNED_SCALED);
8848 DAG.setRoot(ST);
8849 setValue(&VPIntrin, ST);
8850}
8851
8852void SelectionDAGBuilder::visitVPStridedLoad(
8853 const VPIntrinsic &VPIntrin, EVT VT,
8854 const SmallVectorImpl<SDValue> &OpValues) {
8855 SDLoc DL = getCurSDLoc();
8856 Value *PtrOperand = VPIntrin.getArgOperand(0);
8857 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8858 if (!Alignment)
8859 Alignment = DAG.getEVTAlign(VT.getScalarType());
8860 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8861 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8862 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8863 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8864 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8865 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8866 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8867 MachineMemOperand::Flags MMOFlags =
8868 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8869 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8870 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8871 *Alignment, AAInfo, Ranges);
8872
8873 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8874 OpValues[2], OpValues[3], MMO,
8875 false /*IsExpanding*/);
8876
8877 if (AddToChain)
8878 PendingLoads.push_back(LD.getValue(1));
8879 setValue(&VPIntrin, LD);
8880}
8881
8882void SelectionDAGBuilder::visitVPStridedStore(
8883 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8884 SDLoc DL = getCurSDLoc();
8885 Value *PtrOperand = VPIntrin.getArgOperand(1);
8886 EVT VT = OpValues[0].getValueType();
8887 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8888 if (!Alignment)
8889 Alignment = DAG.getEVTAlign(VT.getScalarType());
8890 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8891 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8892 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8893 MachineMemOperand::Flags MMOFlags =
8894 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8895 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8896 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8897 *Alignment, AAInfo);
8898
8899 SDValue ST = DAG.getStridedStoreVP(
8900 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8901 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8902 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8903 /*IsCompressing*/ false);
8904
8905 DAG.setRoot(ST);
8906 setValue(&VPIntrin, ST);
8907}
8908
8909void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8910 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8911 SDLoc DL = getCurSDLoc();
8912
8913 ISD::CondCode Condition;
8915
8916 Value *Op1 = VPIntrin.getOperand(0);
8917 Value *Op2 = VPIntrin.getOperand(1);
8918 // #2 is the condition code
8919 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8920 SDValue EVL = getValue(VPIntrin.getOperand(4));
8921 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8922 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8923 "Unexpected target EVL type");
8924 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8925
8926 if (VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy()) {
8927 Condition = getFCmpCondCode(CondCode);
8928 SimplifyQuery SQ(DAG.getDataLayout(), &VPIntrin);
8929 if (isKnownNeverNaN(Op2, SQ) && isKnownNeverNaN(Op1, SQ))
8930 Condition = getFCmpCodeWithoutNaN(Condition);
8931 } else {
8932 Condition = getICmpCondCode(CondCode);
8933 }
8934
8935 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8936 VPIntrin.getType());
8937 setValue(&VPIntrin, DAG.getSetCCVP(DL, DestVT, getValue(Op1), getValue(Op2),
8938 Condition, MaskOp, EVL));
8939}
8940
8941void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8942 const VPIntrinsic &VPIntrin) {
8943 SDLoc DL = getCurSDLoc();
8944 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8945
8946 auto IID = VPIntrin.getIntrinsicID();
8947
8948 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8949 return visitVPCmp(*CmpI);
8950
8951 SmallVector<EVT, 4> ValueVTs;
8952 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8953 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8954 SDVTList VTs = DAG.getVTList(ValueVTs);
8955
8956 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8957
8958 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8959 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8960 "Unexpected target EVL type");
8961
8962 // Request operands.
8963 SmallVector<SDValue, 7> OpValues;
8964 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8965 auto Op = getValue(VPIntrin.getArgOperand(I));
8966 if (I == EVLParamPos)
8967 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8968 OpValues.push_back(Op);
8969 }
8970
8971 switch (Opcode) {
8972 default: {
8973 SDNodeFlags SDFlags;
8974 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8975 SDFlags.copyFMF(*FPMO);
8976 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8977 setValue(&VPIntrin, Result);
8978 break;
8979 }
8980 case ISD::VP_LOAD:
8981 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8982 break;
8983 case ISD::VP_LOAD_FF:
8984 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8985 break;
8986 case ISD::VP_GATHER:
8987 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8988 break;
8989 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8990 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8991 break;
8992 case ISD::VP_STORE:
8993 visitVPStore(VPIntrin, OpValues);
8994 break;
8995 case ISD::VP_SCATTER:
8996 visitVPScatter(VPIntrin, OpValues);
8997 break;
8998 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8999 visitVPStridedStore(VPIntrin, OpValues);
9000 break;
9001 case ISD::VP_FMULADD: {
9002 assert(OpValues.size() == 5 && "Unexpected number of operands");
9003 SDNodeFlags SDFlags;
9004 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
9005 SDFlags.copyFMF(*FPMO);
9006 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
9007 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
9008 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
9009 } else {
9010 SDValue Mul = DAG.getNode(
9011 ISD::VP_FMUL, DL, VTs,
9012 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
9013 SDValue Add =
9014 DAG.getNode(ISD::VP_FADD, DL, VTs,
9015 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
9016 setValue(&VPIntrin, Add);
9017 }
9018 break;
9019 }
9020 case ISD::VP_IS_FPCLASS: {
9021 const DataLayout DLayout = DAG.getDataLayout();
9022 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
9023 auto Constant = OpValues[1]->getAsZExtVal();
9024 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
9025 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
9026 {OpValues[0], Check, OpValues[2], OpValues[3]});
9027 setValue(&VPIntrin, V);
9028 return;
9029 }
9030 case ISD::VP_INTTOPTR: {
9031 SDValue N = OpValues[0];
9032 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
9033 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
9034 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
9035 OpValues[2]);
9036 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
9037 OpValues[2]);
9038 setValue(&VPIntrin, N);
9039 break;
9040 }
9041 case ISD::VP_PTRTOINT: {
9042 SDValue N = OpValues[0];
9043 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9044 VPIntrin.getType());
9045 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
9046 VPIntrin.getOperand(0)->getType());
9047 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
9048 OpValues[2]);
9049 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
9050 OpValues[2]);
9051 setValue(&VPIntrin, N);
9052 break;
9053 }
9054 case ISD::VP_ABS:
9055 case ISD::VP_CTLZ:
9056 case ISD::VP_CTLZ_ZERO_POISON:
9057 case ISD::VP_CTTZ:
9058 case ISD::VP_CTTZ_ZERO_POISON:
9059 case ISD::VP_CTTZ_ELTS_ZERO_POISON:
9060 case ISD::VP_CTTZ_ELTS: {
9061 SDValue Result =
9062 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
9063 setValue(&VPIntrin, Result);
9064 break;
9065 }
9066 }
9067}
9068
9070 const BasicBlock *EHPadBB,
9071 MCSymbol *&BeginLabel) {
9072 MachineFunction &MF = DAG.getMachineFunction();
9073
9074 // Insert a label before the invoke call to mark the try range. This can be
9075 // used to detect deletion of the invoke via the MachineModuleInfo.
9076 BeginLabel = MF.getContext().createTempSymbol();
9077
9078 // For SjLj, keep track of which landing pads go with which invokes
9079 // so as to maintain the ordering of pads in the LSDA.
9080 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
9081 if (CallSiteIndex) {
9082 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
9083 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
9084
9085 // Now that the call site is handled, stop tracking it.
9086 FuncInfo.setCurrentCallSite(0);
9087 }
9088
9089 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
9090}
9091
9092SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
9093 const BasicBlock *EHPadBB,
9094 MCSymbol *BeginLabel) {
9095 assert(BeginLabel && "BeginLabel should've been set");
9096
9098
9099 // Insert a label at the end of the invoke call to mark the try range. This
9100 // can be used to detect deletion of the invoke via the MachineModuleInfo.
9101 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
9102 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
9103
9104 // Inform MachineModuleInfo of range.
9106 // There is a platform (e.g. wasm) that uses funclet style IR but does not
9107 // actually use outlined funclets and their LSDA info style.
9108 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
9109 assert(II && "II should've been set");
9110 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
9111 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
9112 } else if (!isScopedEHPersonality(Pers)) {
9113 assert(EHPadBB);
9114 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
9115 }
9116
9117 return Chain;
9118}
9119
9120std::pair<SDValue, SDValue>
9122 const BasicBlock *EHPadBB) {
9123 MCSymbol *BeginLabel = nullptr;
9124
9125 if (EHPadBB) {
9126 // Both PendingLoads and PendingExports must be flushed here;
9127 // this call might not return.
9128 (void)getRoot();
9129 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
9130 CLI.setChain(getRoot());
9131 }
9132
9133 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9134 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
9135
9136 assert((CLI.IsTailCall || Result.second.getNode()) &&
9137 "Non-null chain expected with non-tail call!");
9138 assert((Result.second.getNode() || !Result.first.getNode()) &&
9139 "Null value expected with tail call!");
9140
9141 if (!Result.second.getNode()) {
9142 // As a special case, a null chain means that a tail call has been emitted
9143 // and the DAG root is already updated.
9144 HasTailCall = true;
9145
9146 // Since there's no actual continuation from this block, nothing can be
9147 // relying on us setting vregs for them.
9148 PendingExports.clear();
9149 } else {
9150 DAG.setRoot(Result.second);
9151 }
9152
9153 if (EHPadBB) {
9154 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
9155 BeginLabel));
9156 Result.second = getRoot();
9157 }
9158
9159 return Result;
9160}
9161
9163 bool isMustTailCall = CB.isMustTailCall();
9164
9165 // Avoid emitting tail calls in functions with the disable-tail-calls
9166 // attribute.
9167 const Function *Caller = CB.getParent()->getParent();
9168 if (!isMustTailCall &&
9169 Caller->getFnAttribute("disable-tail-calls").getValueAsBool())
9170 return false;
9171
9172 // We can't tail call inside a function with a swifterror argument. Lowering
9173 // does not support this yet. It would have to move into the swifterror
9174 // register before the call.
9175 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
9176 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
9177 return false;
9178
9179 // Check if target-independent constraints permit a tail call here.
9180 // Target-dependent constraints are checked within TLI->LowerCallTo.
9181 return isInTailCallPosition(CB, DAG.getTarget());
9182}
9183
9185 bool isTailCall, bool isMustTailCall,
9186 const BasicBlock *EHPadBB,
9187 const TargetLowering::PtrAuthInfo *PAI) {
9188 auto &DL = DAG.getDataLayout();
9189 FunctionType *FTy = CB.getFunctionType();
9190 Type *RetTy = CB.getType();
9191
9193 Args.reserve(CB.arg_size());
9194
9195 const Value *SwiftErrorVal = nullptr;
9196 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9197
9198 if (isTailCall)
9199 isTailCall = canTailCall(CB);
9200
9201 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
9202 const Value *V = *I;
9203
9204 // Skip empty types
9205 if (V->getType()->isEmptyTy())
9206 continue;
9207
9208 SDValue ArgNode = getValue(V);
9209 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
9210 Entry.setAttributes(&CB, I - CB.arg_begin());
9211
9212 // Use swifterror virtual register as input to the call.
9213 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
9214 SwiftErrorVal = V;
9215 // We find the virtual register for the actual swifterror argument.
9216 // Instead of using the Value, we use the virtual register instead.
9217 Entry.Node =
9218 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
9219 EVT(TLI.getPointerTy(DL)));
9220 }
9221
9222 Args.push_back(Entry);
9223
9224 // If we have an explicit sret argument that is an Instruction, (i.e., it
9225 // might point to function-local memory), we can't meaningfully tail-call.
9226 if (Entry.IsSRet && isa<Instruction>(V))
9227 isTailCall = false;
9228 }
9229
9230 // If call site has a cfguardtarget operand bundle, create and add an
9231 // additional ArgListEntry.
9232 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9233 Value *V = Bundle->Inputs[0];
9235 Entry.IsCFGuardTarget = true;
9236 Args.push_back(Entry);
9237 }
9238
9239 // Disable tail calls if there is an swifterror argument. Targets have not
9240 // been updated to support tail calls.
9241 if (TLI.supportSwiftError() && SwiftErrorVal)
9242 isTailCall = false;
9243
9244 ConstantInt *CFIType = nullptr;
9245 if (CB.isIndirectCall()) {
9246 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9247 if (!TLI.supportKCFIBundles())
9249 "Target doesn't support calls with kcfi operand bundles.");
9250 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9251 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9252 }
9253 }
9254
9255 SDValue ConvControlToken;
9256 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9257 auto *Token = Bundle->Inputs[0].get();
9258 ConvControlToken = getValue(Token);
9259 }
9260
9261 GlobalValue *DeactivationSymbol = nullptr;
9263 DeactivationSymbol = cast<GlobalValue>(Bundle->Inputs[0].get());
9264 }
9265
9268 .setChain(getRoot())
9269 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9270 .setTailCall(isTailCall)
9274 .setCFIType(CFIType)
9275 .setConvergenceControlToken(ConvControlToken)
9276 .setDeactivationSymbol(DeactivationSymbol);
9277
9278 // Set the pointer authentication info if we have it.
9279 if (PAI) {
9280 if (!TLI.supportPtrAuthBundles())
9282 "This target doesn't support calls with ptrauth operand bundles.");
9283 CLI.setPtrAuth(*PAI);
9284 }
9285
9286 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9287
9288 if (Result.first.getNode()) {
9289 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9290 Result.first = lowerNoFPClassToAssertNoFPClass(DAG, CB, Result.first);
9291 setValue(&CB, Result.first);
9292 }
9293
9294 // The last element of CLI.InVals has the SDValue for swifterror return.
9295 // Here we copy it to a virtual register and update SwiftErrorMap for
9296 // book-keeping.
9297 if (SwiftErrorVal && TLI.supportSwiftError()) {
9298 // Get the last element of InVals.
9299 SDValue Src = CLI.InVals.back();
9300 Register VReg =
9301 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9302 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9303 DAG.setRoot(CopyNode);
9304 }
9305}
9306
9307static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9308 SelectionDAGBuilder &Builder) {
9309 // Check to see if this load can be trivially constant folded, e.g. if the
9310 // input is from a string literal.
9311 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9312 // Cast pointer to the type we really want to load.
9313 Type *LoadTy =
9314 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9315 if (LoadVT.isVector())
9316 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9317 if (const Constant *LoadCst =
9318 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9319 LoadTy, Builder.DAG.getDataLayout()))
9320 return Builder.getValue(LoadCst);
9321 }
9322
9323 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9324 // still constant memory, the input chain can be the entry node.
9325 SDValue Root;
9326 bool ConstantMemory = false;
9327
9328 // Do not serialize (non-volatile) loads of constant memory with anything.
9329 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9330 Root = Builder.DAG.getEntryNode();
9331 ConstantMemory = true;
9332 } else {
9333 // Do not serialize non-volatile loads against each other.
9334 Root = Builder.DAG.getRoot();
9335 }
9336
9337 SDValue Ptr = Builder.getValue(PtrVal);
9338 SDValue LoadVal =
9339 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9340 MachinePointerInfo(PtrVal), Align(1));
9341
9342 if (!ConstantMemory)
9343 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9344 return LoadVal;
9345}
9346
9347/// Record the value for an instruction that produces an integer result,
9348/// converting the type where necessary.
9349void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9350 SDValue Value,
9351 bool IsSigned) {
9352 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9353 I.getType(), true);
9354 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9355 setValue(&I, Value);
9356}
9357
9358/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9359/// true and lower it. Otherwise return false, and it will be lowered like a
9360/// normal call.
9361/// The caller already checked that \p I calls the appropriate LibFunc with a
9362/// correct prototype.
9363bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9364 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9365 const Value *Size = I.getArgOperand(2);
9366 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9367 if (CSize && CSize->getZExtValue() == 0) {
9368 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9369 I.getType(), true);
9370 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9371 return true;
9372 }
9373
9374 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9375 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9376 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9377 getValue(Size), &I);
9378 if (Res.first.getNode()) {
9379 processIntegerCallValue(I, Res.first, true);
9380 PendingLoads.push_back(Res.second);
9381 return true;
9382 }
9383
9384 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9385 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9386 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9387 return false;
9388
9389 // If the target has a fast compare for the given size, it will return a
9390 // preferred load type for that size. Require that the load VT is legal and
9391 // that the target supports unaligned loads of that type. Otherwise, return
9392 // INVALID.
9393 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9394 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9395 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9396 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9397 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9398 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9399 // TODO: Check alignment of src and dest ptrs.
9400 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9401 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9402 if (!TLI.isTypeLegal(LVT) ||
9403 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9404 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9406 }
9407
9408 return LVT;
9409 };
9410
9411 // This turns into unaligned loads. We only do this if the target natively
9412 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9413 // we'll only produce a small number of byte loads.
9414 MVT LoadVT;
9415 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9416 switch (NumBitsToCompare) {
9417 default:
9418 return false;
9419 case 16:
9420 LoadVT = MVT::i16;
9421 break;
9422 case 32:
9423 LoadVT = MVT::i32;
9424 break;
9425 case 64:
9426 case 128:
9427 case 256:
9428 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9429 break;
9430 }
9431
9432 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9433 return false;
9434
9435 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9436 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9437
9438 // Bitcast to a wide integer type if the loads are vectors.
9439 if (LoadVT.isVector()) {
9440 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9441 LoadL = DAG.getBitcast(CmpVT, LoadL);
9442 LoadR = DAG.getBitcast(CmpVT, LoadR);
9443 }
9444
9445 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9446 processIntegerCallValue(I, Cmp, false);
9447 return true;
9448}
9449
9450/// See if we can lower a memchr call into an optimized form. If so, return
9451/// true and lower it. Otherwise return false, and it will be lowered like a
9452/// normal call.
9453/// The caller already checked that \p I calls the appropriate LibFunc with a
9454/// correct prototype.
9455bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9456 const Value *Src = I.getArgOperand(0);
9457 const Value *Char = I.getArgOperand(1);
9458 const Value *Length = I.getArgOperand(2);
9459
9460 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9461 std::pair<SDValue, SDValue> Res =
9462 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9463 getValue(Src), getValue(Char), getValue(Length),
9464 MachinePointerInfo(Src));
9465 if (Res.first.getNode()) {
9466 setValue(&I, Res.first);
9467 PendingLoads.push_back(Res.second);
9468 return true;
9469 }
9470
9471 return false;
9472}
9473
9474/// See if we can lower a memccpy call into an optimized form. If so, return
9475/// true and lower it, otherwise return false and it will be lowered like a
9476/// normal call.
9477/// The caller already checked that \p I calls the appropriate LibFunc with a
9478/// correct prototype.
9479bool SelectionDAGBuilder::visitMemCCpyCall(const CallInst &I) {
9480 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9481 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemccpy(
9482 DAG, getCurSDLoc(), DAG.getRoot(), getValue(I.getArgOperand(0)),
9483 getValue(I.getArgOperand(1)), getValue(I.getArgOperand(2)),
9484 getValue(I.getArgOperand(3)), &I);
9485
9486 if (Res.first) {
9487 processIntegerCallValue(I, Res.first, true);
9488 PendingLoads.push_back(Res.second);
9489 return true;
9490 }
9491 return false;
9492}
9493
9494/// See if we can lower a mempcpy call into an optimized form. If so, return
9495/// true and lower it. Otherwise return false, and it will be lowered like a
9496/// normal call.
9497/// The caller already checked that \p I calls the appropriate LibFunc with a
9498/// correct prototype.
9499bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9500 SDValue Dst = getValue(I.getArgOperand(0));
9501 SDValue Src = getValue(I.getArgOperand(1));
9502 SDValue Size = getValue(I.getArgOperand(2));
9503
9504 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9505 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9506
9507 SDLoc sdl = getCurSDLoc();
9508
9509 // In the mempcpy context we need to pass in a false value for isTailCall
9510 // because the return pointer needs to be adjusted by the size of
9511 // the copied memory.
9512 SDValue Root = getMemoryRoot();
9513 SDValue MC = DAG.getMemcpy(
9514 Root, sdl, Dst, Src, Size, DstAlign, SrcAlign, false, false,
9515 /*CI=*/nullptr, std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9516 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9517 assert(MC.getNode() != nullptr &&
9518 "** memcpy should not be lowered as TailCall in mempcpy context **");
9519 DAG.setRoot(MC);
9520
9521 // Check if Size needs to be truncated or extended.
9522 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9523
9524 // Adjust return pointer to point just past the last dst byte.
9525 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9526 setValue(&I, DstPlusSize);
9527 return true;
9528}
9529
9530/// See if we can lower a strcpy call into an optimized form. If so, return
9531/// true and lower it, otherwise return false and it will be lowered like a
9532/// normal call.
9533/// The caller already checked that \p I calls the appropriate LibFunc with a
9534/// correct prototype.
9535bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9536 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9537
9538 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9539 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrcpy(
9540 DAG, getCurSDLoc(), getRoot(), getValue(Arg0), getValue(Arg1),
9541 MachinePointerInfo(Arg0), MachinePointerInfo(Arg1), isStpcpy, &I);
9542 if (Res.first.getNode()) {
9543 setValue(&I, Res.first);
9544 DAG.setRoot(Res.second);
9545 return true;
9546 }
9547
9548 return false;
9549}
9550
9551/// See if we can lower a strcmp call into an optimized form. If so, return
9552/// true and lower it, otherwise return false and it will be lowered like a
9553/// normal call.
9554/// The caller already checked that \p I calls the appropriate LibFunc with a
9555/// correct prototype.
9556bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9557 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9558
9559 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9560 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrcmp(
9561 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), getValue(Arg1),
9562 MachinePointerInfo(Arg0), MachinePointerInfo(Arg1), &I);
9563 if (Res.first.getNode()) {
9564 processIntegerCallValue(I, Res.first, true);
9565 PendingLoads.push_back(Res.second);
9566 return true;
9567 }
9568
9569 return false;
9570}
9571
9572/// See if we can lower a strlen call into an optimized form. If so, return
9573/// true and lower it, otherwise return false and it will be lowered like a
9574/// normal call.
9575/// The caller already checked that \p I calls the appropriate LibFunc with a
9576/// correct prototype.
9577bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9578 const Value *Arg0 = I.getArgOperand(0);
9579
9580 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9581 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrlen(
9582 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), &I);
9583 if (Res.first.getNode()) {
9584 processIntegerCallValue(I, Res.first, false);
9585 PendingLoads.push_back(Res.second);
9586 return true;
9587 }
9588
9589 return false;
9590}
9591
9592/// See if we can lower a strnlen call into an optimized form. If so, return
9593/// true and lower it, otherwise return false and it will be lowered like a
9594/// normal call.
9595/// The caller already checked that \p I calls the appropriate LibFunc with a
9596/// correct prototype.
9597bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9598 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9599
9600 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9601 std::pair<SDValue, SDValue> Res =
9602 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9603 getValue(Arg0), getValue(Arg1),
9604 MachinePointerInfo(Arg0));
9605 if (Res.first.getNode()) {
9606 processIntegerCallValue(I, Res.first, false);
9607 PendingLoads.push_back(Res.second);
9608 return true;
9609 }
9610
9611 return false;
9612}
9613
9614/// See if we can lower a Strstr call into an optimized form. If so, return
9615/// true and lower it, otherwise return false and it will be lowered like a
9616/// normal call.
9617/// The caller already checked that \p I calls the appropriate LibFunc with a
9618/// correct prototype.
9619bool SelectionDAGBuilder::visitStrstrCall(const CallInst &I) {
9620 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9621 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9622 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrstr(
9623 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), getValue(Arg1), &I);
9624 if (Res.first) {
9625 processIntegerCallValue(I, Res.first, false);
9626 PendingLoads.push_back(Res.second);
9627 return true;
9628 }
9629 return false;
9630}
9631
9632/// See if we can lower a unary floating-point operation into an SDNode with
9633/// the specified Opcode. If so, return true and lower it, otherwise return
9634/// false and it will be lowered like a normal call.
9635/// The caller already checked that \p I calls the appropriate LibFunc with a
9636/// correct prototype.
9637bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9638 unsigned Opcode) {
9639 // We already checked this call's prototype; verify it doesn't modify errno.
9640 // Do not perform optimizations for call sites that require strict
9641 // floating-point semantics.
9642 if (!I.onlyReadsMemory() || I.isStrictFP())
9643 return false;
9644
9645 SDNodeFlags Flags;
9646 Flags.copyFMF(cast<FPMathOperator>(I));
9647
9648 SDValue Tmp = getValue(I.getArgOperand(0));
9649 setValue(&I,
9650 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9651 return true;
9652}
9653
9654/// See if we can lower a binary floating-point operation into an SDNode with
9655/// the specified Opcode. If so, return true and lower it. Otherwise return
9656/// false, and it will be lowered like a normal call.
9657/// The caller already checked that \p I calls the appropriate LibFunc with a
9658/// correct prototype.
9659bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9660 unsigned Opcode) {
9661 // We already checked this call's prototype; verify it doesn't modify errno.
9662 // Do not perform optimizations for call sites that require strict
9663 // floating-point semantics.
9664 if (!I.onlyReadsMemory() || I.isStrictFP())
9665 return false;
9666
9667 SDNodeFlags Flags;
9668 Flags.copyFMF(cast<FPMathOperator>(I));
9669
9670 SDValue Tmp0 = getValue(I.getArgOperand(0));
9671 SDValue Tmp1 = getValue(I.getArgOperand(1));
9672 EVT VT = Tmp0.getValueType();
9673 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9674 return true;
9675}
9676
9677void SelectionDAGBuilder::visitCall(const CallInst &I) {
9678 // Handle inline assembly differently.
9679 if (I.isInlineAsm()) {
9680 visitInlineAsm(I);
9681 return;
9682 }
9683
9685
9686 if (Function *F = I.getCalledFunction()) {
9687 if (F->isDeclaration()) {
9688 // Is this an LLVM intrinsic?
9689 if (unsigned IID = F->getIntrinsicID()) {
9690 visitIntrinsicCall(I, IID);
9691 return;
9692 }
9693 }
9694
9695 // Check for well-known libc/libm calls. If the function is internal, it
9696 // can't be a library call. Don't do the check if marked as nobuiltin for
9697 // some reason.
9698 // This code should not handle libcalls that are already canonicalized to
9699 // intrinsics by the middle-end.
9700 LibFunc Func;
9701 if (!I.isNoBuiltin() && !F->hasLocalLinkage() && F->hasName() &&
9702 LibInfo->getLibFunc(*F, Func) && LibInfo->hasOptimizedCodeGen(Func)) {
9703 switch (Func) {
9704 default: break;
9705 case LibFunc_bcmp:
9706 if (visitMemCmpBCmpCall(I))
9707 return;
9708 break;
9709 case LibFunc_copysign:
9710 case LibFunc_copysignf:
9711 case LibFunc_copysignl:
9712 // We already checked this call's prototype; verify it doesn't modify
9713 // errno.
9714 if (I.onlyReadsMemory()) {
9715 SDValue LHS = getValue(I.getArgOperand(0));
9716 SDValue RHS = getValue(I.getArgOperand(1));
9718 LHS.getValueType(), LHS, RHS));
9719 return;
9720 }
9721 break;
9722 case LibFunc_sin:
9723 case LibFunc_sinf:
9724 case LibFunc_sinl:
9725 if (visitUnaryFloatCall(I, ISD::FSIN))
9726 return;
9727 break;
9728 case LibFunc_cos:
9729 case LibFunc_cosf:
9730 case LibFunc_cosl:
9731 if (visitUnaryFloatCall(I, ISD::FCOS))
9732 return;
9733 break;
9734 case LibFunc_tan:
9735 case LibFunc_tanf:
9736 case LibFunc_tanl:
9737 if (visitUnaryFloatCall(I, ISD::FTAN))
9738 return;
9739 break;
9740 case LibFunc_asin:
9741 case LibFunc_asinf:
9742 case LibFunc_asinl:
9743 if (visitUnaryFloatCall(I, ISD::FASIN))
9744 return;
9745 break;
9746 case LibFunc_acos:
9747 case LibFunc_acosf:
9748 case LibFunc_acosl:
9749 if (visitUnaryFloatCall(I, ISD::FACOS))
9750 return;
9751 break;
9752 case LibFunc_atan:
9753 case LibFunc_atanf:
9754 case LibFunc_atanl:
9755 if (visitUnaryFloatCall(I, ISD::FATAN))
9756 return;
9757 break;
9758 case LibFunc_atan2:
9759 case LibFunc_atan2f:
9760 case LibFunc_atan2l:
9761 if (visitBinaryFloatCall(I, ISD::FATAN2))
9762 return;
9763 break;
9764 case LibFunc_sinh:
9765 case LibFunc_sinhf:
9766 case LibFunc_sinhl:
9767 if (visitUnaryFloatCall(I, ISD::FSINH))
9768 return;
9769 break;
9770 case LibFunc_cosh:
9771 case LibFunc_coshf:
9772 case LibFunc_coshl:
9773 if (visitUnaryFloatCall(I, ISD::FCOSH))
9774 return;
9775 break;
9776 case LibFunc_tanh:
9777 case LibFunc_tanhf:
9778 case LibFunc_tanhl:
9779 if (visitUnaryFloatCall(I, ISD::FTANH))
9780 return;
9781 break;
9782 case LibFunc_sqrt:
9783 case LibFunc_sqrtf:
9784 case LibFunc_sqrtl:
9785 case LibFunc_sqrt_finite:
9786 case LibFunc_sqrtf_finite:
9787 case LibFunc_sqrtl_finite:
9788 if (visitUnaryFloatCall(I, ISD::FSQRT))
9789 return;
9790 break;
9791 case LibFunc_log2:
9792 case LibFunc_log2f:
9793 case LibFunc_log2l:
9794 if (visitUnaryFloatCall(I, ISD::FLOG2))
9795 return;
9796 break;
9797 case LibFunc_exp2:
9798 case LibFunc_exp2f:
9799 case LibFunc_exp2l:
9800 if (visitUnaryFloatCall(I, ISD::FEXP2))
9801 return;
9802 break;
9803 case LibFunc_exp10:
9804 case LibFunc_exp10f:
9805 case LibFunc_exp10l:
9806 if (visitUnaryFloatCall(I, ISD::FEXP10))
9807 return;
9808 break;
9809 case LibFunc_ldexp:
9810 case LibFunc_ldexpf:
9811 case LibFunc_ldexpl:
9812 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9813 return;
9814 break;
9815 case LibFunc_strstr:
9816 if (visitStrstrCall(I))
9817 return;
9818 break;
9819 case LibFunc_memcmp:
9820 if (visitMemCmpBCmpCall(I))
9821 return;
9822 break;
9823 case LibFunc_memccpy:
9824 if (visitMemCCpyCall(I))
9825 return;
9826 break;
9827 case LibFunc_mempcpy:
9828 if (visitMemPCpyCall(I))
9829 return;
9830 break;
9831 case LibFunc_memchr:
9832 if (visitMemChrCall(I))
9833 return;
9834 break;
9835 case LibFunc_strcpy:
9836 if (visitStrCpyCall(I, false))
9837 return;
9838 break;
9839 case LibFunc_stpcpy:
9840 if (visitStrCpyCall(I, true))
9841 return;
9842 break;
9843 case LibFunc_strcmp:
9844 if (visitStrCmpCall(I))
9845 return;
9846 break;
9847 case LibFunc_strlen:
9848 if (visitStrLenCall(I))
9849 return;
9850 break;
9851 case LibFunc_strnlen:
9852 if (visitStrNLenCall(I))
9853 return;
9854 break;
9855 }
9856 }
9857 }
9858
9859 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9860 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9861 return;
9862 }
9863
9864 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9865 // have to do anything here to lower funclet bundles.
9866 // CFGuardTarget bundles are lowered in LowerCallTo.
9868 I, "calls",
9873
9874 SDValue Callee = getValue(I.getCalledOperand());
9875
9876 if (I.hasDeoptState())
9877 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9878 else
9879 // Check if we can potentially perform a tail call. More detailed checking
9880 // is be done within LowerCallTo, after more information about the call is
9881 // known.
9882 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9883}
9884
9886 const CallBase &CB, const BasicBlock *EHPadBB) {
9887 auto PAB = CB.getOperandBundle("ptrauth");
9888 const Value *CalleeV = CB.getCalledOperand();
9889
9890 // Gather the call ptrauth data from the operand bundle:
9891 // [ i32 <key>, i64 <discriminator> ]
9892 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9893 const Value *Discriminator = PAB->Inputs[1];
9894
9895 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9896 assert(Discriminator->getType()->isIntegerTy(64) &&
9897 "Invalid ptrauth discriminator");
9898
9899 // Look through ptrauth constants to find the raw callee.
9900 // Do a direct unauthenticated call if we found it and everything matches.
9901 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9902 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9903 DAG.getDataLayout()))
9904 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9905 CB.isMustTailCall(), EHPadBB);
9906
9907 // Functions should never be ptrauth-called directly.
9908 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9909
9910 // Otherwise, do an authenticated indirect call.
9911 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9912 getValue(Discriminator)};
9913
9914 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9915 EHPadBB, &PAI);
9916}
9917
9918namespace {
9919
9920/// AsmOperandInfo - This contains information for each constraint that we are
9921/// lowering.
9922class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9923public:
9924 /// CallOperand - If this is the result output operand or a clobber
9925 /// this is null, otherwise it is the incoming operand to the CallInst.
9926 /// This gets modified as the asm is processed.
9927 SDValue CallOperand;
9928
9929 /// AssignedRegs - If this is a register or register class operand, this
9930 /// contains the set of register corresponding to the operand.
9931 RegsForValue AssignedRegs;
9932
9933 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9934 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9935 }
9936
9937 /// Whether or not this operand accesses memory
9938 bool hasMemory(const TargetLowering &TLI) const {
9939 // Indirect operand accesses access memory.
9940 if (isIndirect)
9941 return true;
9942
9943 for (const auto &Code : Codes)
9945 return true;
9946
9947 return false;
9948 }
9949};
9950
9951
9952} // end anonymous namespace
9953
9954/// Make sure that the output operand \p OpInfo and its corresponding input
9955/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9956/// out).
9957static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9958 SDISelAsmOperandInfo &MatchingOpInfo,
9959 SelectionDAG &DAG) {
9960 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9961 return;
9962
9964 const auto &TLI = DAG.getTargetLoweringInfo();
9965
9966 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9967 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9968 OpInfo.ConstraintVT);
9969 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9970 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9971 MatchingOpInfo.ConstraintVT);
9972 const bool OutOpIsIntOrFP =
9973 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9974 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9975 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9976 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9977 // FIXME: error out in a more elegant fashion
9978 report_fatal_error("Unsupported asm: input constraint"
9979 " with a matching output constraint of"
9980 " incompatible type!");
9981 }
9982 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9983}
9984
9985/// Get a direct memory input to behave well as an indirect operand.
9986/// This may introduce stores, hence the need for a \p Chain.
9987/// \return The (possibly updated) chain.
9988static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9989 SDISelAsmOperandInfo &OpInfo,
9990 SelectionDAG &DAG) {
9991 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9992
9993 // If we don't have an indirect input, put it in the constpool if we can,
9994 // otherwise spill it to a stack slot.
9995 // TODO: This isn't quite right. We need to handle these according to
9996 // the addressing mode that the constraint wants. Also, this may take
9997 // an additional register for the computation and we don't want that
9998 // either.
9999
10000 // If the operand is a float, integer, or vector constant, spill to a
10001 // constant pool entry to get its address.
10002 const Value *OpVal = OpInfo.CallOperandVal;
10003 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
10005 OpInfo.CallOperand = DAG.getConstantPool(
10006 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
10007 return Chain;
10008 }
10009
10010 // Otherwise, create a stack slot and emit a store to it before the asm.
10011 Type *Ty = OpVal->getType();
10012 auto &DL = DAG.getDataLayout();
10013 TypeSize TySize = DL.getTypeAllocSize(Ty);
10016 int StackID = 0;
10017 if (TySize.isScalable())
10018 StackID = TFI->getStackIDForScalableVectors();
10019 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
10020 DL.getPrefTypeAlign(Ty), false,
10021 nullptr, StackID);
10022 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
10023 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
10025 TLI.getMemValueType(DL, Ty));
10026 OpInfo.CallOperand = StackSlot;
10027
10028 return Chain;
10029}
10030
10031/// GetRegistersForValue - Assign registers (virtual or physical) for the
10032/// specified operand. We prefer to assign virtual registers, to allow the
10033/// register allocator to handle the assignment process. However, if the asm
10034/// uses features that we can't model on machineinstrs, we have SDISel do the
10035/// allocation. This produces generally horrible, but correct, code.
10036///
10037/// OpInfo describes the operand
10038/// RefOpInfo describes the matching operand if any, the operand otherwise
10039static std::optional<unsigned>
10041 SDISelAsmOperandInfo &OpInfo,
10042 SDISelAsmOperandInfo &RefOpInfo) {
10043 LLVMContext &Context = *DAG.getContext();
10044 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10045
10049
10050 // No work to do for memory/address operands.
10051 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
10052 OpInfo.ConstraintType == TargetLowering::C_Address)
10053 return std::nullopt;
10054
10055 // If this is a constraint for a single physreg, or a constraint for a
10056 // register class, find it.
10057 unsigned AssignedReg;
10058 const TargetRegisterClass *RC;
10059 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
10060 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
10061 // RC is unset only on failure. Return immediately.
10062 if (!RC)
10063 return std::nullopt;
10064
10065 // Get the actual register value type. This is important, because the user
10066 // may have asked for (e.g.) the AX register in i32 type. We need to
10067 // remember that AX is actually i16 to get the right extension.
10068 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
10069
10070 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
10071 // If this is an FP operand in an integer register (or visa versa), or more
10072 // generally if the operand value disagrees with the register class we plan
10073 // to stick it in, fix the operand type.
10074 //
10075 // If this is an input value, the bitcast to the new type is done now.
10076 // Bitcast for output value is done at the end of visitInlineAsm().
10077 if ((OpInfo.Type == InlineAsm::isOutput ||
10078 OpInfo.Type == InlineAsm::isInput) &&
10079 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
10080 // Try to convert to the first EVT that the reg class contains. If the
10081 // types are identical size, use a bitcast to convert (e.g. two differing
10082 // vector types). Note: output bitcast is done at the end of
10083 // visitInlineAsm().
10084 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
10085 // Exclude indirect inputs while they are unsupported because the code
10086 // to perform the load is missing and thus OpInfo.CallOperand still
10087 // refers to the input address rather than the pointed-to value.
10088 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
10089 OpInfo.CallOperand =
10090 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
10091 OpInfo.ConstraintVT = RegVT;
10092 // If the operand is an FP value and we want it in integer registers,
10093 // use the corresponding integer type. This turns an f64 value into
10094 // i64, which can be passed with two i32 values on a 32-bit machine.
10095 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
10096 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
10097 if (OpInfo.Type == InlineAsm::isInput)
10098 OpInfo.CallOperand =
10099 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
10100 OpInfo.ConstraintVT = VT;
10101 }
10102 }
10103 }
10104
10105 // No need to allocate a matching input constraint since the constraint it's
10106 // matching to has already been allocated.
10107 if (OpInfo.isMatchingInputConstraint())
10108 return std::nullopt;
10109
10110 EVT ValueVT = OpInfo.ConstraintVT;
10111 if (OpInfo.ConstraintVT == MVT::Other)
10112 ValueVT = RegVT;
10113
10114 // Initialize NumRegs.
10115 unsigned NumRegs = 1;
10116 if (OpInfo.ConstraintVT != MVT::Other)
10117 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
10118
10119 // If this is a constraint for a specific physical register, like {r17},
10120 // assign it now.
10121
10122 // If this associated to a specific register, initialize iterator to correct
10123 // place. If virtual, make sure we have enough registers
10124
10125 // Initialize iterator if necessary
10128
10129 // Do not check for single registers.
10130 if (AssignedReg) {
10131 I = std::find(I, RC->end(), AssignedReg);
10132 if (I == RC->end()) {
10133 // RC does not contain the selected register, which indicates a
10134 // mismatch between the register and the required type/bitwidth.
10135 return {AssignedReg};
10136 }
10137 }
10138
10139 for (; NumRegs; --NumRegs, ++I) {
10140 assert(I != RC->end() && "Ran out of registers to allocate!");
10141 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
10142 Regs.push_back(R);
10143 }
10144
10145 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
10146 return std::nullopt;
10147}
10148
10149static unsigned
10151 const std::vector<SDValue> &AsmNodeOperands) {
10152 // Scan until we find the definition we already emitted of this operand.
10153 unsigned CurOp = InlineAsm::Op_FirstOperand;
10154 for (; OperandNo; --OperandNo) {
10155 // Advance to the next operand.
10156 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
10157 const InlineAsm::Flag F(OpFlag);
10158 assert(
10159 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
10160 "Skipped past definitions?");
10161 CurOp += F.getNumOperandRegisters() + 1;
10162 }
10163 return CurOp;
10164}
10165
10166namespace {
10167
10168class ExtraFlags {
10169 unsigned Flags = 0;
10170
10171public:
10172 explicit ExtraFlags(const CallBase &Call) {
10173 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10174 if (IA->hasSideEffects())
10176 if (IA->isAlignStack())
10178 if (IA->canThrow())
10180 if (Call.isConvergent())
10182 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
10183 }
10184
10185 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
10186 // Ideally, we would only check against memory constraints. However, the
10187 // meaning of an Other constraint can be target-specific and we can't easily
10188 // reason about it. Therefore, be conservative and set MayLoad/MayStore
10189 // for Other constraints as well.
10192 if (OpInfo.Type == InlineAsm::isInput)
10194 else if (OpInfo.Type == InlineAsm::isOutput)
10196 else if (OpInfo.Type == InlineAsm::isClobber)
10198 }
10199 }
10200
10201 unsigned get() const { return Flags; }
10202};
10203
10204} // end anonymous namespace
10205
10206static bool isFunction(SDValue Op) {
10207 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
10208 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10209 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10210
10211 // In normal "call dllimport func" instruction (non-inlineasm) it force
10212 // indirect access by specifing call opcode. And usually specially print
10213 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10214 // not do in this way now. (In fact, this is similar with "Data Access"
10215 // action). So here we ignore dllimport function.
10216 if (Fn && !Fn->hasDLLImportStorageClass())
10217 return true;
10218 }
10219 }
10220 return false;
10221}
10222
10223namespace {
10224
10225struct ConstraintDecisionInfo {
10226 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10227 std::vector<SDValue> AsmNodeOperands;
10228 SDValue Glue, Chain;
10229 bool HasSideEffect = false;
10230 MCSymbol *BeginLabel = nullptr;
10231
10232 SmallVector<char> Buffer;
10233 raw_svector_ostream ErrorMsg;
10234
10235 ConstraintDecisionInfo() : ErrorMsg(Buffer) {}
10236};
10237
10238} // end anonymous namespace
10239
10240/// Construct operand info objects.
10241static bool
10242constructOperandInfo(ConstraintDecisionInfo &Info,
10243 TargetLowering::AsmOperandInfoVector &TargetConstraints,
10244 SelectionDAGBuilder &Builder, const TargetLowering &TLI,
10245 ExtraFlags &ExtraInfo) {
10246 for (auto &T : TargetConstraints) {
10247 Info.ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10248 SDISelAsmOperandInfo &OpInfo = Info.ConstraintOperands.back();
10249
10250 if (OpInfo.CallOperandVal)
10251 OpInfo.CallOperand = Builder.getValue(OpInfo.CallOperandVal);
10252
10253 if (!Info.HasSideEffect)
10254 Info.HasSideEffect = OpInfo.hasMemory(TLI);
10255
10256 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10257 // FIXME: Could we compute this on OpInfo rather than T?
10258
10259 // Compute the constraint code and ConstraintType to use.
10261
10262 if (T.ConstraintType == TargetLowering::C_Immediate && OpInfo.CallOperand &&
10263 !isa<ConstantSDNode>(OpInfo.CallOperand)) {
10264 // We've delayed emitting a diagnostic like the "n" constraint because
10265 // inlining could cause an integer showing up.
10266 Info.ErrorMsg << "constraint '" << T.ConstraintCode
10267 << "' expects an integer constant expression";
10268 return true;
10269 }
10270
10271 ExtraInfo.update(T);
10272 }
10273
10274 return false;
10275}
10276
10277/// Compute which constraint option to use for each operand.
10278static void
10279computeConstraintToUse(ConstraintDecisionInfo &Info, const CallBase &Call,
10280 TargetLowering::AsmOperandInfoVector &TargetConstraints,
10281 SelectionDAGBuilder &Builder, const TargetLowering &TLI,
10282 const TargetMachine &TM, SelectionDAG &DAG) {
10283 const auto *IA = cast<InlineAsm>(Call.getCalledOperand());
10285 IA->collectAsmStrs(AsmStrs);
10286
10287 int OpNo = -1;
10288 for (SDISelAsmOperandInfo &OpInfo : Info.ConstraintOperands) {
10289 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10290 OpNo++;
10291
10292 // If this is an output operand with a matching input operand, look up the
10293 // matching input. If their types mismatch, e.g. one is an integer, the
10294 // other is floating point, or their sizes are different, flag it as an
10295 // error.
10296 if (OpInfo.hasMatchingInput()) {
10297 SDISelAsmOperandInfo &Input =
10298 Info.ConstraintOperands[OpInfo.MatchingInput];
10299 patchMatchingInput(OpInfo, Input, DAG);
10300 }
10301
10302 // Compute the constraint code and ConstraintType to use.
10303 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10304
10305 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10306 OpInfo.Type == InlineAsm::isClobber) ||
10307 OpInfo.ConstraintType == TargetLowering::C_Address)
10308 continue;
10309
10310 // In Linux PIC model, there are 4 cases about value/label addressing:
10311 //
10312 // 1: Function call or Label jmp inside the module.
10313 // 2: Data access (such as global variable, static variable) inside module.
10314 // 3: Function call or Label jmp outside the module.
10315 // 4: Data access (such as global variable) outside the module.
10316 //
10317 // Due to current llvm inline asm architecture designed to not "recognize"
10318 // the asm code, there are quite troubles for us to treat mem addressing
10319 // differently for same value/adress used in different instuctions.
10320 // For example, in pic model, call a func may in plt way or direclty
10321 // pc-related, but lea/mov a function adress may use got.
10322 //
10323 // Here we try to "recognize" function call for the case 1 and case 3 in
10324 // inline asm. And try to adjust the constraint for them.
10325 //
10326 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10327 // label, so here we don't handle jmp function label now, but we need to
10328 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10329 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10330 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10332 OpInfo.isIndirect = false;
10333 OpInfo.ConstraintType = TargetLowering::C_Address;
10334 }
10335
10336 // If this is a memory input, and if the operand is not indirect, do what we
10337 // need to provide an address for the memory input.
10338 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10339 !OpInfo.isIndirect) {
10340 assert((OpInfo.isMultipleAlternative ||
10341 (OpInfo.Type == InlineAsm::isInput)) &&
10342 "Can only indirectify direct input operands!");
10343
10344 // Memory operands really want the address of the value.
10345 Info.Chain = getAddressForMemoryInput(Info.Chain, Builder.getCurSDLoc(),
10346 OpInfo, DAG);
10347
10348 // There is no longer a Value* corresponding to this operand.
10349 OpInfo.CallOperandVal = nullptr;
10350
10351 // It is now an indirect operand.
10352 OpInfo.isIndirect = true;
10353 }
10354 }
10355}
10356
10357/// Prepare DAG-level operands. As part of this, assign virtual and physical
10358/// registers for inputs and output.
10359static bool prepareDAGLevelOperands(ConstraintDecisionInfo &Info,
10360 const CallBase &Call,
10361 SelectionDAGBuilder &Builder,
10362 const TargetLowering &TLI,
10363 SelectionDAG &DAG) {
10364 SDLoc DL = Builder.getCurSDLoc();
10365 for (SDISelAsmOperandInfo &OpInfo : Info.ConstraintOperands) {
10366 // Assign Registers.
10367 SDISelAsmOperandInfo &RefOpInfo =
10368 OpInfo.isMatchingInputConstraint()
10369 ? Info.ConstraintOperands[OpInfo.getMatchedOperand()]
10370 : OpInfo;
10371 const auto RegError = getRegistersForValue(DAG, DL, OpInfo, RefOpInfo);
10372 if (RegError) {
10373 const MachineFunction &MF = DAG.getMachineFunction();
10375 const char *RegName = TRI.getName(*RegError);
10376 Info.ErrorMsg << "register '" << RegName << "' allocated for constraint '"
10377 << OpInfo.ConstraintCode
10378 << "' does not match required type";
10379 return true;
10380 }
10381
10382 auto DetectWriteToReservedRegister = [&]() {
10383 const MachineFunction &MF = DAG.getMachineFunction();
10385
10386 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10387 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10388 Info.ErrorMsg << "write to reserved register '"
10389 << TRI.getRegAsmName(Reg) << "'";
10390 return true;
10391 }
10392 }
10393
10394 return false;
10395 };
10396 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10397 (OpInfo.Type == InlineAsm::isInput &&
10398 !OpInfo.isMatchingInputConstraint())) &&
10399 "Only address as input operand is allowed.");
10400
10401 switch (OpInfo.Type) {
10403 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10404 const InlineAsm::ConstraintCode ConstraintID =
10405 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10407 "Failed to convert memory constraint code to constraint id.");
10408
10409 // Add information to the INLINEASM node to know about this output.
10411 OpFlags.setMemConstraint(ConstraintID);
10412 Info.AsmNodeOperands.push_back(
10413 DAG.getTargetConstant(OpFlags, DL, MVT::i32));
10414 Info.AsmNodeOperands.push_back(OpInfo.CallOperand);
10415 } else {
10416 // Otherwise, this outputs to a register (directly for C_Register /
10417 // C_RegisterClass, and a target-defined fashion for
10418 // C_Immediate/C_Other). Find a register that we can use.
10419 if (OpInfo.AssignedRegs.Regs.empty()) {
10420 Info.ErrorMsg << "could not allocate output register for "
10421 << "constraint '" << OpInfo.ConstraintCode << "'";
10422 return true;
10423 }
10424
10425 if (DetectWriteToReservedRegister())
10426 return true;
10427
10428 // Add information to the INLINEASM node to know that this register is
10429 // set.
10430 OpInfo.AssignedRegs.AddInlineAsmOperands(
10431 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10433 false, 0, DL, DAG, Info.AsmNodeOperands);
10434 }
10435 break;
10436
10437 case InlineAsm::isInput:
10438 case InlineAsm::isLabel: {
10439 SDValue InOperandVal = OpInfo.CallOperand;
10440
10441 if (OpInfo.isMatchingInputConstraint()) {
10442 // If this is required to match an output register we have already set,
10443 // just use its register.
10444 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10445 Info.AsmNodeOperands);
10446 InlineAsm::Flag Flag(Info.AsmNodeOperands[CurOp]->getAsZExtVal());
10447 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10448 if (OpInfo.isIndirect) {
10449 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10450 Info.ErrorMsg << "inline asm not supported yet: cannot handle "
10451 << "tied indirect register inputs";
10452 return true;
10453 }
10454
10457 MachineRegisterInfo &MRI = MF.getRegInfo();
10459 auto *R = cast<RegisterSDNode>(Info.AsmNodeOperands[CurOp + 1]);
10460 Register TiedReg = R->getReg();
10461 MVT RegVT = R->getSimpleValueType(0);
10462 const TargetRegisterClass *RC =
10463 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10464 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10465 : TRI.getMinimalPhysRegClass(TiedReg);
10466 for (unsigned I = 0, E = Flag.getNumOperandRegisters(); I != E; ++I)
10467 Regs.push_back(MRI.createVirtualRegister(RC));
10468
10469 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10470
10471 // Use the produced MatchedRegs object to
10472 MatchedRegs.getCopyToRegs(InOperandVal, DAG, DL, Info.Chain,
10473 &Info.Glue, &Call);
10475 OpInfo.getMatchedOperand(), DL, DAG,
10476 Info.AsmNodeOperands);
10477 break;
10478 }
10479
10480 assert(Flag.isMemKind() && "Unknown matching constraint!");
10481 assert(Flag.getNumOperandRegisters() == 1 &&
10482 "Unexpected number of operands");
10483
10484 // Add information to the INLINEASM node to know about this input.
10485 // See InlineAsm.h isUseOperandTiedToDef.
10486 Flag.clearMemConstraint();
10487 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10488 Info.AsmNodeOperands.push_back(DAG.getTargetConstant(
10489 Flag, DL, TLI.getPointerTy(DAG.getDataLayout())));
10490 Info.AsmNodeOperands.push_back(Info.AsmNodeOperands[CurOp + 1]);
10491 break;
10492 }
10493
10494 // Treat indirect 'X' constraint as memory.
10495 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10496 OpInfo.isIndirect)
10497 OpInfo.ConstraintType = TargetLowering::C_Memory;
10498
10499 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10500 OpInfo.ConstraintType == TargetLowering::C_Other) {
10501 std::vector<SDValue> Ops;
10502 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10503 Ops, DAG);
10504 if (Ops.empty()) {
10505 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10506 if (isa<ConstantSDNode>(InOperandVal)) {
10507 Info.ErrorMsg << "value out of range for constraint '"
10508 << OpInfo.ConstraintCode << "'";
10509 return true;
10510 }
10511
10512 Info.ErrorMsg << "invalid operand for inline asm constraint '"
10513 << OpInfo.ConstraintCode << "'";
10514 return true;
10515 }
10516
10517 // Add information to the INLINEASM node to know about this input.
10518 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10519 Info.AsmNodeOperands.push_back(DAG.getTargetConstant(
10520 ResOpType, DL, TLI.getPointerTy(DAG.getDataLayout())));
10521 llvm::append_range(Info.AsmNodeOperands, Ops);
10522 break;
10523 }
10524
10525 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10526 assert((OpInfo.isIndirect ||
10527 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10528 "Operand must be indirect to be a mem!");
10529 assert(InOperandVal.getValueType() ==
10530 TLI.getPointerTy(DAG.getDataLayout()) &&
10531 "Memory operands expect pointer values");
10532
10533 const InlineAsm::ConstraintCode ConstraintID =
10534 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10536 "Failed to convert memory constraint code to constraint id.");
10537
10538 // Add information to the INLINEASM node to know about this input.
10540 ResOpType.setMemConstraint(ConstraintID);
10541 Info.AsmNodeOperands.push_back(
10542 DAG.getTargetConstant(ResOpType, DL, MVT::i32));
10543 Info.AsmNodeOperands.push_back(InOperandVal);
10544 break;
10545 }
10546
10547 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10548 const InlineAsm::ConstraintCode ConstraintID =
10549 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10551 "Failed to convert memory constraint code to constraint id.");
10552
10554
10555 SDValue AsmOp = InOperandVal;
10556 if (isFunction(InOperandVal)) {
10557 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10558 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10559 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
10560 InOperandVal.getValueType(),
10561 GA->getOffset());
10562 }
10563
10564 // Add information to the INLINEASM node to know about this input.
10565 ResOpType.setMemConstraint(ConstraintID);
10566
10567 Info.AsmNodeOperands.push_back(
10568 DAG.getTargetConstant(ResOpType, DL, MVT::i32));
10569 Info.AsmNodeOperands.push_back(AsmOp);
10570 break;
10571 }
10572
10573 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10574 OpInfo.ConstraintType != TargetLowering::C_Register) {
10575 Info.ErrorMsg << "unknown asm constraint '" << OpInfo.ConstraintCode
10576 << "'";
10577 return true;
10578 }
10579
10580 // TODO: Support this.
10581 if (OpInfo.isIndirect) {
10582 Info.ErrorMsg << "cannot handle indirect register inputs yet for "
10583 << "constraint '" << OpInfo.ConstraintCode << "'";
10584 return true;
10585 }
10586
10587 // Copy the input into the appropriate registers.
10588 if (OpInfo.AssignedRegs.Regs.empty()) {
10589 Info.ErrorMsg << "could not allocate input reg for constraint '"
10590 << OpInfo.ConstraintCode << "'";
10591 return true;
10592 }
10593
10594 if (DetectWriteToReservedRegister())
10595 return true;
10596
10597 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, DL, Info.Chain,
10598 &Info.Glue, &Call);
10599 OpInfo.AssignedRegs.AddInlineAsmOperands(
10600 InlineAsm::Kind::RegUse, false, 0, DL, DAG, Info.AsmNodeOperands);
10601 break;
10602 }
10603
10605 // Add the clobbered value to the operand list, so that the register
10606 // allocator is aware that the physreg got clobbered.
10607 if (!OpInfo.AssignedRegs.Regs.empty())
10608 OpInfo.AssignedRegs.AddInlineAsmOperands(
10609 InlineAsm::Kind::Clobber, false, 0, DL, DAG, Info.AsmNodeOperands);
10610 break;
10611 }
10612 }
10613
10614 return false;
10615}
10616
10617/// DetermineConstraints - Find the constraints to use for inline asm operands.
10618static bool
10619determineConstraints(ConstraintDecisionInfo &Info,
10620 TargetLowering::AsmOperandInfoVector &TargetConstraints,
10621 const CallBase &Call, SelectionDAGBuilder &Builder,
10622 const TargetLowering &TLI, const TargetMachine &TM,
10623 SelectionDAG &DAG, const BasicBlock *EHPadBB) {
10624 const auto *IA = cast<InlineAsm>(Call.getCalledOperand());
10625 ExtraFlags ExtraInfo(Call);
10626
10627 // First pass: Construct operand info objects.
10628 Info.HasSideEffect = IA->hasSideEffects();
10629 if (constructOperandInfo(Info, TargetConstraints, Builder, TLI, ExtraInfo))
10630 return true;
10631
10632 // We won't need to flush pending loads if this asm doesn't touch
10633 // memory and is nonvolatile.
10634 Info.Chain = Info.HasSideEffect ? Builder.getRoot() : DAG.getRoot();
10635
10636 bool IsCallBr = isa<CallBrInst>(Call);
10637 bool EmitEHLabels = isa<InvokeInst>(Call);
10638 if (IsCallBr || EmitEHLabels)
10639 // If this is a callbr or invoke we need to flush pending exports since
10640 // inlineasm_br and invoke are terminators.
10641 // We need to do this before nodes are glued to the inlineasm_br node.
10642 Info.Chain = Builder.getControlRoot();
10643
10644 if (EmitEHLabels)
10645 Info.Chain = Builder.lowerStartEH(Info.Chain, EHPadBB, Info.BeginLabel);
10646
10647 // Second pass: Compute which constraint option to use.
10648 computeConstraintToUse(Info, Call, TargetConstraints, Builder, TLI, TM, DAG);
10649
10650 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10651 Info.AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10652 Info.AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10653 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10654
10655 // If we have a !srcloc metadata node associated with it, we want to attach
10656 // this to the ultimately generated inline asm machineinstr. To do this, we
10657 // pass in the third operand as this (potentially null) inline asm MDNode.
10658 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10659 Info.AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10660
10661 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10662 // bits as operand 3.
10663 Info.AsmNodeOperands.push_back(
10664 DAG.getTargetConstant(ExtraInfo.get(), Builder.getCurSDLoc(),
10665 TLI.getPointerTy(DAG.getDataLayout())));
10666
10667 // Third pass: Prepare DAG-level operands
10668 return prepareDAGLevelOperands(Info, Call, Builder, TLI, DAG);
10669}
10670
10671/// visitInlineAsm - Handle a call to an InlineAsm object.
10672void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10673 const BasicBlock *EHPadBB) {
10674 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10676 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10677
10678 assert((!isa<InvokeInst>(Call) || EHPadBB) &&
10679 "InvokeInst must have an EHPadBB");
10680
10681 ConstraintDecisionInfo Info;
10682 if (determineConstraints(Info, TargetConstraints, Call, *this, TLI, TM, DAG,
10683 EHPadBB))
10684 return emitInlineAsmError(Call, Info.ErrorMsg.str());
10685
10686 SDValue Glue = Info.Glue;
10687 SDValue Chain = Info.Chain;
10688
10689 // Finish up input operands. Set the input chain and add the flag last.
10690 Info.AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10691 if (Glue.getNode())
10692 Info.AsmNodeOperands.push_back(Glue);
10693
10694 bool IsCallBr = isa<CallBrInst>(Call);
10695 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10696 Chain =
10697 DAG.getNode(ISDOpc, getCurSDLoc(), DAG.getVTList(MVT::Other, MVT::Glue),
10698 Info.AsmNodeOperands);
10699 Glue = Chain.getValue(1);
10700
10701 // Do additional work to generate outputs.
10702
10703 SmallVector<EVT, 1> ResultVTs;
10704 SmallVector<SDValue, 1> ResultValues;
10705 SmallVector<SDValue, 8> OutChains;
10706
10707 llvm::Type *CallResultType = Call.getType();
10708 ArrayRef<Type *> ResultTypes;
10709 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10710 ResultTypes = StructResult->elements();
10711 else if (!CallResultType->isVoidTy())
10712 ResultTypes = ArrayRef(CallResultType);
10713
10714 auto CurResultType = ResultTypes.begin();
10715 auto handleRegAssign = [&](SDValue V) {
10716 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10717 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10718 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10719 ++CurResultType;
10720 // If the type of the inline asm call site return value is different but has
10721 // same size as the type of the asm output bitcast it. One example of this
10722 // is for vectors with different width / number of elements. This can
10723 // happen for register classes that can contain multiple different value
10724 // types. The preg or vreg allocated may not have the same VT as was
10725 // expected.
10726 //
10727 // This can also happen for a return value that disagrees with the register
10728 // class it is put in, eg. a double in a general-purpose register on a
10729 // 32-bit machine.
10730 if (ResultVT != V.getValueType() &&
10731 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10732 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10733 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10734 V.getValueType().isInteger()) {
10735 // If a result value was tied to an input value, the computed result
10736 // may have a wider width than the expected result. Extract the
10737 // relevant portion.
10738 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10739 }
10740 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10741 ResultVTs.push_back(ResultVT);
10742 ResultValues.push_back(V);
10743 };
10744
10745 // Deal with output operands.
10746 for (SDISelAsmOperandInfo &OpInfo : Info.ConstraintOperands) {
10747 if (OpInfo.Type == InlineAsm::isOutput) {
10748 SDValue Val;
10749 // Skip trivial output operands.
10750 if (OpInfo.AssignedRegs.Regs.empty())
10751 continue;
10752
10753 switch (OpInfo.ConstraintType) {
10756 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10757 Chain, &Glue, &Call);
10758 break;
10761 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10762 OpInfo, DAG);
10763 break;
10765 break; // Already handled.
10767 break; // Silence warning.
10769 assert(false && "Unexpected unknown constraint");
10770 }
10771
10772 // Indirect output manifest as stores. Record output chains.
10773 if (OpInfo.isIndirect) {
10774 const Value *Ptr = OpInfo.CallOperandVal;
10775 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10776 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10777 MachinePointerInfo(Ptr));
10778 OutChains.push_back(Store);
10779 } else {
10780 // generate CopyFromRegs to associated registers.
10781 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10782 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10783 for (const SDValue &V : Val->op_values())
10784 handleRegAssign(V);
10785 } else
10786 handleRegAssign(Val);
10787 }
10788 }
10789 }
10790
10791 // Set results.
10792 if (!ResultValues.empty()) {
10793 assert(CurResultType == ResultTypes.end() &&
10794 "Mismatch in number of ResultTypes");
10795 assert(ResultValues.size() == ResultTypes.size() &&
10796 "Mismatch in number of output operands in asm result");
10797
10799 DAG.getVTList(ResultVTs), ResultValues);
10800 setValue(&Call, V);
10801 }
10802
10803 // Collect store chains.
10804 if (!OutChains.empty())
10805 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10806
10807 if (const auto *II = dyn_cast<InvokeInst>(&Call))
10808 Chain = lowerEndEH(Chain, II, EHPadBB, Info.BeginLabel);
10809
10810 // Only Update Root if inline assembly has a memory effect.
10811 if (ResultValues.empty() || Info.HasSideEffect || !OutChains.empty() ||
10812 IsCallBr || isa<InvokeInst>(Call))
10813 DAG.setRoot(Chain);
10814}
10815
10816void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10817 const Twine &Message) {
10818 LLVMContext &Ctx = *DAG.getContext();
10819 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10820
10821 // Make sure we leave the DAG in a valid state
10822 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10823 SmallVector<EVT, 1> ValueVTs;
10824 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10825
10826 if (ValueVTs.empty())
10827 return;
10828
10830 for (const EVT &VT : ValueVTs)
10831 Ops.push_back(DAG.getUNDEF(VT));
10832
10833 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10834}
10835
10836void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10837 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10838 MVT::Other, getRoot(),
10839 getValue(I.getArgOperand(0)),
10840 DAG.getSrcValue(I.getArgOperand(0))));
10841}
10842
10843void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10844 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10845 const DataLayout &DL = DAG.getDataLayout();
10846 SDValue V = DAG.getVAArg(
10847 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10848 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10849 DL.getABITypeAlign(I.getType()).value());
10850 DAG.setRoot(V.getValue(1));
10851
10852 if (I.getType()->isPointerTy())
10853 V = DAG.getPtrExtOrTrunc(
10854 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10855 setValue(&I, V);
10856}
10857
10858void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10859 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10860 MVT::Other, getRoot(),
10861 getValue(I.getArgOperand(0)),
10862 DAG.getSrcValue(I.getArgOperand(0))));
10863}
10864
10865void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10866 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10867 MVT::Other, getRoot(),
10868 getValue(I.getArgOperand(0)),
10869 getValue(I.getArgOperand(1)),
10870 DAG.getSrcValue(I.getArgOperand(0)),
10871 DAG.getSrcValue(I.getArgOperand(1))));
10872}
10873
10875 const Instruction &I,
10876 SDValue Op) {
10877 std::optional<ConstantRange> CR = getRange(I);
10878
10879 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10880 return Op;
10881
10882 APInt Hi = CR->getUnsignedMax();
10883 unsigned Bits = std::max(Hi.getActiveBits(),
10884 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10885
10886 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10887
10888 SDLoc SL = getCurSDLoc();
10889
10890 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10891 DAG.getValueType(SmallVT));
10892 unsigned NumVals = Op.getNode()->getNumValues();
10893 if (NumVals == 1)
10894 return ZExt;
10895
10897
10898 Ops.push_back(ZExt);
10899 for (unsigned I = 1; I != NumVals; ++I)
10900 Ops.push_back(Op.getValue(I));
10901
10902 return DAG.getMergeValues(Ops, SL);
10903}
10904
10906 SelectionDAG &DAG, const Instruction &I, SDValue Op) {
10907 FPClassTest Classes = getNoFPClass(I);
10908 if (Classes == fcNone)
10909 return Op;
10910
10911 SDLoc SL = getCurSDLoc();
10912 SDValue TestConst = DAG.getTargetConstant(Classes, SDLoc(), MVT::i32);
10913
10914 if (Op.getOpcode() != ISD::MERGE_VALUES) {
10915 return DAG.getNode(ISD::AssertNoFPClass, SL, Op.getValueType(), Op,
10916 TestConst);
10917 }
10918
10919 SmallVector<SDValue, 8> Ops(Op.getNumOperands());
10920 for (unsigned I = 0, E = Ops.size(); I != E; ++I) {
10921 SDValue MergeOp = Op.getOperand(I);
10922 Ops[I] = DAG.getNode(ISD::AssertNoFPClass, SL, MergeOp.getValueType(),
10923 MergeOp, TestConst);
10924 }
10925
10926 return DAG.getMergeValues(Ops, SL);
10927}
10928
10929/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10930/// the call being lowered.
10931///
10932/// This is a helper for lowering intrinsics that follow a target calling
10933/// convention or require stack pointer adjustment. Only a subset of the
10934/// intrinsic's operands need to participate in the calling convention.
10937 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10938 AttributeSet RetAttrs, bool IsPatchPoint) {
10940 Args.reserve(NumArgs);
10941
10942 // Populate the argument list.
10943 // Attributes for args start at offset 1, after the return attribute.
10944 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10945 ArgI != ArgE; ++ArgI) {
10946 const Value *V = Call->getOperand(ArgI);
10947
10948 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10949
10950 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10951 Entry.setAttributes(Call, ArgI);
10952 Args.push_back(Entry);
10953 }
10954
10956 .setChain(getRoot())
10957 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10958 RetAttrs)
10959 .setDiscardResult(Call->use_empty())
10960 .setIsPatchPoint(IsPatchPoint)
10962 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10963}
10964
10965/// Add a stack map intrinsic call's live variable operands to a stackmap
10966/// or patchpoint target node's operand list.
10967///
10968/// Constants are converted to TargetConstants purely as an optimization to
10969/// avoid constant materialization and register allocation.
10970///
10971/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10972/// generate addess computation nodes, and so FinalizeISel can convert the
10973/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10974/// address materialization and register allocation, but may also be required
10975/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10976/// alloca in the entry block, then the runtime may assume that the alloca's
10977/// StackMap location can be read immediately after compilation and that the
10978/// location is valid at any point during execution (this is similar to the
10979/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10980/// only available in a register, then the runtime would need to trap when
10981/// execution reaches the StackMap in order to read the alloca's location.
10982static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10984 SelectionDAGBuilder &Builder) {
10985 SelectionDAG &DAG = Builder.DAG;
10986 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10987 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10988
10989 // Things on the stack are pointer-typed, meaning that they are already
10990 // legal and can be emitted directly to target nodes.
10992 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10993 } else {
10994 // Otherwise emit a target independent node to be legalised.
10995 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10996 }
10997 }
10998}
10999
11000/// Lower llvm.experimental.stackmap.
11001void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
11002 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
11003 // [live variables...])
11004
11005 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
11006
11007 SDValue Chain, InGlue, Callee;
11009
11010 SDLoc DL = getCurSDLoc();
11012
11013 // The stackmap intrinsic only records the live variables (the arguments
11014 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
11015 // intrinsic, this won't be lowered to a function call. This means we don't
11016 // have to worry about calling conventions and target specific lowering code.
11017 // Instead we perform the call lowering right here.
11018 //
11019 // chain, flag = CALLSEQ_START(chain, 0, 0)
11020 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
11021 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
11022 //
11023 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
11024 InGlue = Chain.getValue(1);
11025
11026 // Add the STACKMAP operands, starting with DAG house-keeping.
11027 Ops.push_back(Chain);
11028 Ops.push_back(InGlue);
11029
11030 // Add the <id>, <numShadowBytes> operands.
11031 //
11032 // These do not require legalisation, and can be emitted directly to target
11033 // constant nodes.
11035 assert(ID.getValueType() == MVT::i64);
11036 SDValue IDConst =
11037 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
11038 Ops.push_back(IDConst);
11039
11040 SDValue Shad = getValue(CI.getArgOperand(1));
11041 assert(Shad.getValueType() == MVT::i32);
11042 SDValue ShadConst =
11043 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
11044 Ops.push_back(ShadConst);
11045
11046 // Add the live variables.
11047 addStackMapLiveVars(CI, 2, DL, Ops, *this);
11048
11049 // Create the STACKMAP node.
11050 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11051 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
11052 InGlue = Chain.getValue(1);
11053
11054 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
11055
11056 // Stackmaps don't generate values, so nothing goes into the NodeMap.
11057
11058 // Set the root to the target-lowered call chain.
11059 DAG.setRoot(Chain);
11060
11061 // Inform the Frame Information that we have a stackmap in this function.
11062 FuncInfo.MF->getFrameInfo().setHasStackMap();
11063}
11064
11065/// Lower llvm.experimental.patchpoint directly to its target opcode.
11066void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
11067 const BasicBlock *EHPadBB) {
11068 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
11069 // i32 <numBytes>,
11070 // i8* <target>,
11071 // i32 <numArgs>,
11072 // [Args...],
11073 // [live variables...])
11074
11076 bool IsAnyRegCC = CC == CallingConv::AnyReg;
11077 bool HasDef = !CB.getType()->isVoidTy();
11078 SDLoc dl = getCurSDLoc();
11080
11081 // Handle immediate and symbolic callees.
11082 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
11083 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
11084 /*isTarget=*/true);
11085 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
11086 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
11087 SDLoc(SymbolicCallee),
11088 SymbolicCallee->getValueType(0));
11089
11090 // Get the real number of arguments participating in the call <numArgs>
11092 unsigned NumArgs = NArgVal->getAsZExtVal();
11093
11094 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
11095 // Intrinsics include all meta-operands up to but not including CC.
11096 unsigned NumMetaOpers = PatchPointOpers::CCPos;
11097 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
11098 "Not enough arguments provided to the patchpoint intrinsic");
11099
11100 // For AnyRegCC the arguments are lowered later on manually.
11101 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
11102 Type *ReturnTy =
11103 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
11104
11105 TargetLowering::CallLoweringInfo CLI(DAG);
11106 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
11107 ReturnTy, CB.getAttributes().getRetAttrs(), true);
11108 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
11109
11110 SDNode *CallEnd = Result.second.getNode();
11111 if (CallEnd->getOpcode() == ISD::EH_LABEL)
11112 CallEnd = CallEnd->getOperand(0).getNode();
11113 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
11114 CallEnd = CallEnd->getOperand(0).getNode();
11115
11116 /// Get a call instruction from the call sequence chain.
11117 /// Tail calls are not allowed.
11118 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
11119 "Expected a callseq node.");
11120 SDNode *Call = CallEnd->getOperand(0).getNode();
11121 bool HasGlue = Call->getGluedNode();
11122
11123 // Replace the target specific call node with the patchable intrinsic.
11125
11126 // Push the chain.
11127 Ops.push_back(*(Call->op_begin()));
11128
11129 // Optionally, push the glue (if any).
11130 if (HasGlue)
11131 Ops.push_back(*(Call->op_end() - 1));
11132
11133 // Push the register mask info.
11134 if (HasGlue)
11135 Ops.push_back(*(Call->op_end() - 2));
11136 else
11137 Ops.push_back(*(Call->op_end() - 1));
11138
11139 // Add the <id> and <numBytes> constants.
11141 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
11143 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
11144
11145 // Add the callee.
11146 Ops.push_back(Callee);
11147
11148 // Adjust <numArgs> to account for any arguments that have been passed on the
11149 // stack instead.
11150 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
11151 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
11152 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
11153 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
11154
11155 // Add the calling convention
11156 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
11157
11158 // Add the arguments we omitted previously. The register allocator should
11159 // place these in any free register.
11160 if (IsAnyRegCC)
11161 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
11162 Ops.push_back(getValue(CB.getArgOperand(i)));
11163
11164 // Push the arguments from the call instruction.
11165 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
11166 Ops.append(Call->op_begin() + 2, e);
11167
11168 // Push live variables for the stack map.
11169 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
11170
11171 SDVTList NodeTys;
11172 if (IsAnyRegCC && HasDef) {
11173 // Create the return types based on the intrinsic definition
11174 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11175 SmallVector<EVT, 3> ValueVTs;
11176 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
11177 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
11178
11179 // There is always a chain and a glue type at the end
11180 ValueVTs.push_back(MVT::Other);
11181 ValueVTs.push_back(MVT::Glue);
11182 NodeTys = DAG.getVTList(ValueVTs);
11183 } else
11184 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11185
11186 // Replace the target specific call node with a PATCHPOINT node.
11187 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
11188
11189 // Update the NodeMap.
11190 if (HasDef) {
11191 if (IsAnyRegCC)
11192 setValue(&CB, SDValue(PPV.getNode(), 0));
11193 else
11194 setValue(&CB, Result.first);
11195 }
11196
11197 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
11198 // call sequence. Furthermore the location of the chain and glue can change
11199 // when the AnyReg calling convention is used and the intrinsic returns a
11200 // value.
11201 if (IsAnyRegCC && HasDef) {
11202 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
11203 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
11204 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
11205 } else
11206 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
11207 DAG.DeleteNode(Call);
11208
11209 // Inform the Frame Information that we have a patchpoint in this function.
11210 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
11211}
11212
11213void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
11214 unsigned Intrinsic) {
11215 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11216 SDValue Op1 = getValue(I.getArgOperand(0));
11217 SDValue Op2;
11218 if (I.arg_size() > 1)
11219 Op2 = getValue(I.getArgOperand(1));
11220 SDLoc dl = getCurSDLoc();
11221 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11222 SDValue Res;
11223 SDNodeFlags SDFlags;
11224 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
11225 SDFlags.copyFMF(*FPMO);
11226
11227 switch (Intrinsic) {
11228 case Intrinsic::vector_reduce_fadd:
11229 if (SDFlags.hasAllowReassociation())
11230 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
11231 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
11232 SDFlags);
11233 else
11234 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
11235 break;
11236 case Intrinsic::vector_reduce_fmul:
11237 if (SDFlags.hasAllowReassociation())
11238 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
11239 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
11240 SDFlags);
11241 else
11242 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
11243 break;
11244 case Intrinsic::vector_reduce_add:
11245 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
11246 break;
11247 case Intrinsic::vector_reduce_mul:
11248 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
11249 break;
11250 case Intrinsic::vector_reduce_and:
11251 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
11252 break;
11253 case Intrinsic::vector_reduce_or:
11254 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
11255 break;
11256 case Intrinsic::vector_reduce_xor:
11257 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
11258 break;
11259 case Intrinsic::vector_reduce_smax:
11260 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
11261 break;
11262 case Intrinsic::vector_reduce_smin:
11263 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
11264 break;
11265 case Intrinsic::vector_reduce_umax:
11266 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
11267 break;
11268 case Intrinsic::vector_reduce_umin:
11269 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
11270 break;
11271 case Intrinsic::vector_reduce_fmax:
11272 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
11273 break;
11274 case Intrinsic::vector_reduce_fmin:
11275 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
11276 break;
11277 case Intrinsic::vector_reduce_fmaximum:
11278 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
11279 break;
11280 case Intrinsic::vector_reduce_fminimum:
11281 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11282 break;
11283 default:
11284 llvm_unreachable("Unhandled vector reduce intrinsic");
11285 }
11286 setValue(&I, Res);
11287}
11288
11289/// Returns an AttributeList representing the attributes applied to the return
11290/// value of the given call.
11293 if (CLI.RetSExt)
11294 Attrs.push_back(Attribute::SExt);
11295 if (CLI.RetZExt)
11296 Attrs.push_back(Attribute::ZExt);
11297 if (CLI.IsInReg)
11298 Attrs.push_back(Attribute::InReg);
11299
11300 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11301 Attrs);
11302}
11303
11304/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11305/// implementation, which just calls LowerCall.
11306/// FIXME: When all targets are
11307/// migrated to using LowerCall, this hook should be integrated into SDISel.
11308std::pair<SDValue, SDValue>
11310 LLVMContext &Context = CLI.RetTy->getContext();
11311
11312 // Handle the incoming return values from the call.
11313 CLI.Ins.clear();
11314 SmallVector<Type *, 4> RetOrigTys;
11316 auto &DL = CLI.DAG.getDataLayout();
11317 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11318
11319 SmallVector<EVT, 4> RetVTs;
11320 if (CLI.RetTy != CLI.OrigRetTy) {
11321 assert(RetOrigTys.size() == 1 &&
11322 "Only supported for non-aggregate returns");
11323 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11324 } else {
11325 for (Type *Ty : RetOrigTys)
11326 RetVTs.push_back(getValueType(DL, Ty));
11327 }
11328
11329 if (CLI.IsPostTypeLegalization) {
11330 // If we are lowering a libcall after legalization, split the return type.
11331 SmallVector<Type *, 4> OldRetOrigTys;
11332 SmallVector<EVT, 4> OldRetVTs;
11333 SmallVector<TypeSize, 4> OldOffsets;
11334 RetOrigTys.swap(OldRetOrigTys);
11335 RetVTs.swap(OldRetVTs);
11336 Offsets.swap(OldOffsets);
11337
11338 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11339 EVT RetVT = OldRetVTs[i];
11340 uint64_t Offset = OldOffsets[i];
11341 MVT RegisterVT = getRegisterType(Context, RetVT);
11342 unsigned NumRegs = getNumRegisters(Context, RetVT);
11343 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11344 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11345 RetVTs.append(NumRegs, RegisterVT);
11346 for (unsigned j = 0; j != NumRegs; ++j)
11347 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11348 }
11349 }
11350
11352 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11353
11354 bool CanLowerReturn =
11356 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11357
11358 SDValue DemoteStackSlot;
11359 int DemoteStackIdx = -100;
11360 if (!CanLowerReturn) {
11361 // FIXME: equivalent assert?
11362 // assert(!CS.hasInAllocaArgument() &&
11363 // "sret demotion is incompatible with inalloca");
11364 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11365 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11367 DemoteStackIdx =
11368 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11369 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11370
11371 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11372 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11373 Entry.IsSRet = true;
11374 Entry.Alignment = Alignment;
11375 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11376 CLI.NumFixedArgs += 1;
11377 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11378 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11379
11380 // sret demotion isn't compatible with tail-calls, since the sret argument
11381 // points into the callers stack frame.
11382 CLI.IsTailCall = false;
11383 } else {
11384 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11385 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11386 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11387 ISD::ArgFlagsTy Flags;
11388 if (NeedsRegBlock) {
11389 Flags.setInConsecutiveRegs();
11390 if (I == RetVTs.size() - 1)
11391 Flags.setInConsecutiveRegsLast();
11392 }
11393 EVT VT = RetVTs[I];
11394 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11395 unsigned NumRegs =
11396 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11397 for (unsigned i = 0; i != NumRegs; ++i) {
11398 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11400 if (CLI.RetTy->isPointerTy()) {
11401 Ret.Flags.setPointer();
11403 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11404 }
11405 if (CLI.RetSExt)
11406 Ret.Flags.setSExt();
11407 if (CLI.RetZExt)
11408 Ret.Flags.setZExt();
11409 if (CLI.IsInReg)
11410 Ret.Flags.setInReg();
11411 CLI.Ins.push_back(Ret);
11412 }
11413 }
11414 }
11415
11416 // We push in swifterror return as the last element of CLI.Ins.
11417 ArgListTy &Args = CLI.getArgs();
11418 if (supportSwiftError()) {
11419 for (const ArgListEntry &Arg : Args) {
11420 if (Arg.IsSwiftError) {
11421 ISD::ArgFlagsTy Flags;
11422 Flags.setSwiftError();
11424 PointerType::getUnqual(Context),
11425 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11426 CLI.Ins.push_back(Ret);
11427 }
11428 }
11429 }
11430
11431 // Handle all of the outgoing arguments.
11432 CLI.Outs.clear();
11433 CLI.OutVals.clear();
11434 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11435 SmallVector<Type *, 4> OrigArgTys;
11436 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11437 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11438 Type *FinalType = Args[i].Ty;
11439 if (Args[i].IsByVal)
11440 FinalType = Args[i].IndirectType;
11441 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11442 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11443 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11444 ++Value) {
11445 Type *OrigArgTy = OrigArgTys[Value];
11446 Type *ArgTy = OrigArgTy;
11447 if (Args[i].Ty != Args[i].OrigTy) {
11448 assert(Value == 0 && "Only supported for non-aggregate arguments");
11449 ArgTy = Args[i].Ty;
11450 }
11451
11452 EVT VT = getValueType(DL, ArgTy);
11453 SDValue Op = SDValue(Args[i].Node.getNode(),
11454 Args[i].Node.getResNo() + Value);
11455 ISD::ArgFlagsTy Flags;
11456
11457 // Certain targets (such as MIPS), may have a different ABI alignment
11458 // for a type depending on the context. Give the target a chance to
11459 // specify the alignment it wants.
11460 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11461 Flags.setOrigAlign(OriginalAlignment);
11462
11463 if (i >= CLI.NumFixedArgs)
11464 Flags.setVarArg();
11465 if (ArgTy->isPointerTy()) {
11466 Flags.setPointer();
11467 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11468 }
11469 if (Args[i].IsZExt)
11470 Flags.setZExt();
11471 if (Args[i].IsSExt)
11472 Flags.setSExt();
11473 if (Args[i].IsNoExt)
11474 Flags.setNoExt();
11475 if (Args[i].IsInReg) {
11476 // If we are using vectorcall calling convention, a structure that is
11477 // passed InReg - is surely an HVA
11479 isa<StructType>(FinalType)) {
11480 // The first value of a structure is marked
11481 if (0 == Value)
11482 Flags.setHvaStart();
11483 Flags.setHva();
11484 }
11485 // Set InReg Flag
11486 Flags.setInReg();
11487 }
11488 if (Args[i].IsSRet)
11489 Flags.setSRet();
11490 if (Args[i].IsSwiftSelf)
11491 Flags.setSwiftSelf();
11492 if (Args[i].IsSwiftAsync)
11493 Flags.setSwiftAsync();
11494 if (Args[i].IsSwiftError)
11495 Flags.setSwiftError();
11496 if (Args[i].IsCFGuardTarget)
11497 Flags.setCFGuardTarget();
11498 if (Args[i].IsByVal)
11499 Flags.setByVal();
11500 if (Args[i].IsByRef)
11501 Flags.setByRef();
11502 if (Args[i].IsPreallocated) {
11503 Flags.setPreallocated();
11504 // Set the byval flag for CCAssignFn callbacks that don't know about
11505 // preallocated. This way we can know how many bytes we should've
11506 // allocated and how many bytes a callee cleanup function will pop. If
11507 // we port preallocated to more targets, we'll have to add custom
11508 // preallocated handling in the various CC lowering callbacks.
11509 Flags.setByVal();
11510 }
11511 if (Args[i].IsInAlloca) {
11512 Flags.setInAlloca();
11513 // Set the byval flag for CCAssignFn callbacks that don't know about
11514 // inalloca. This way we can know how many bytes we should've allocated
11515 // and how many bytes a callee cleanup function will pop. If we port
11516 // inalloca to more targets, we'll have to add custom inalloca handling
11517 // in the various CC lowering callbacks.
11518 Flags.setByVal();
11519 }
11520 Align MemAlign;
11521 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11522 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11523 Flags.setByValSize(FrameSize);
11524
11525 // info is not there but there are cases it cannot get right.
11526 if (auto MA = Args[i].Alignment)
11527 MemAlign = *MA;
11528 else
11529 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11530 } else if (auto MA = Args[i].Alignment) {
11531 MemAlign = *MA;
11532 } else {
11533 MemAlign = OriginalAlignment;
11534 }
11535 Flags.setMemAlign(MemAlign);
11536 if (Args[i].IsNest)
11537 Flags.setNest();
11538 if (NeedsRegBlock)
11539 Flags.setInConsecutiveRegs();
11540
11541 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11542 unsigned NumParts =
11543 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11544 SmallVector<SDValue, 4> Parts(NumParts);
11545 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11546
11547 if (Args[i].IsSExt)
11548 ExtendKind = ISD::SIGN_EXTEND;
11549 else if (Args[i].IsZExt)
11550 ExtendKind = ISD::ZERO_EXTEND;
11551
11552 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11553 // for now.
11554 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11556 assert((CLI.RetTy == Args[i].Ty ||
11557 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11559 Args[i].Ty->getPointerAddressSpace())) &&
11560 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11561 // Before passing 'returned' to the target lowering code, ensure that
11562 // either the register MVT and the actual EVT are the same size or that
11563 // the return value and argument are extended in the same way; in these
11564 // cases it's safe to pass the argument register value unchanged as the
11565 // return register value (although it's at the target's option whether
11566 // to do so)
11567 // TODO: allow code generation to take advantage of partially preserved
11568 // registers rather than clobbering the entire register when the
11569 // parameter extension method is not compatible with the return
11570 // extension method
11571 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11572 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11573 CLI.RetZExt == Args[i].IsZExt))
11574 Flags.setReturned();
11575 }
11576
11577 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11578 CLI.CallConv, ExtendKind);
11579
11580 for (unsigned j = 0; j != NumParts; ++j) {
11581 // if it isn't first piece, alignment must be 1
11582 // For scalable vectors the scalable part is currently handled
11583 // by individual targets, so we just use the known minimum size here.
11584 ISD::OutputArg MyFlags(
11585 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11586 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11587 if (NumParts > 1 && j == 0)
11588 MyFlags.Flags.setSplit();
11589 else if (j != 0) {
11590 MyFlags.Flags.setOrigAlign(Align(1));
11591 if (j == NumParts - 1)
11592 MyFlags.Flags.setSplitEnd();
11593 }
11594
11595 CLI.Outs.push_back(MyFlags);
11596 CLI.OutVals.push_back(Parts[j]);
11597 }
11598
11599 if (NeedsRegBlock && Value == NumValues - 1)
11600 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11601 }
11602 }
11603
11605 CLI.Chain = LowerCall(CLI, InVals);
11606
11607 // Update CLI.InVals to use outside of this function.
11608 CLI.InVals = InVals;
11609
11610 // Verify that the target's LowerCall behaved as expected.
11611 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11612 "LowerCall didn't return a valid chain!");
11613 assert((!CLI.IsTailCall || InVals.empty()) &&
11614 "LowerCall emitted a return value for a tail call!");
11615 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11616 "LowerCall didn't emit the correct number of values!");
11617
11618 // For a tail call, the return value is merely live-out and there aren't
11619 // any nodes in the DAG representing it. Return a special value to
11620 // indicate that a tail call has been emitted and no more Instructions
11621 // should be processed in the current block.
11622 if (CLI.IsTailCall) {
11623 CLI.DAG.setRoot(CLI.Chain);
11624 return std::make_pair(SDValue(), SDValue());
11625 }
11626
11627#ifndef NDEBUG
11628 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11629 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11630 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11631 "LowerCall emitted a value with the wrong type!");
11632 }
11633#endif
11634
11635 SmallVector<SDValue, 4> ReturnValues;
11636 if (!CanLowerReturn) {
11637 // The instruction result is the result of loading from the
11638 // hidden sret parameter.
11639 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11640
11641 unsigned NumValues = RetVTs.size();
11642 ReturnValues.resize(NumValues);
11643 SmallVector<SDValue, 4> Chains(NumValues);
11644
11645 // An aggregate return value cannot wrap around the address space, so
11646 // offsets to its parts don't wrap either.
11648 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11649 for (unsigned i = 0; i < NumValues; ++i) {
11651 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11653 SDValue L = CLI.DAG.getLoad(
11654 RetVTs[i], CLI.DL, CLI.Chain, Add,
11656 DemoteStackIdx, Offsets[i]),
11657 HiddenSRetAlign);
11658 ReturnValues[i] = L;
11659 Chains[i] = L.getValue(1);
11660 }
11661
11662 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11663 } else {
11664 // Collect the legal value parts into potentially illegal values
11665 // that correspond to the original function's return values.
11666 std::optional<ISD::NodeType> AssertOp;
11667 if (CLI.RetSExt)
11668 AssertOp = ISD::AssertSext;
11669 else if (CLI.RetZExt)
11670 AssertOp = ISD::AssertZext;
11671 unsigned CurReg = 0;
11672 for (EVT VT : RetVTs) {
11673 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11674 unsigned NumRegs =
11675 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11676
11677 ReturnValues.push_back(getCopyFromParts(
11678 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11679 CLI.Chain, CLI.CallConv, AssertOp));
11680 CurReg += NumRegs;
11681 }
11682
11683 // For a function returning void, there is no return value. We can't create
11684 // such a node, so we just return a null return value in that case. In
11685 // that case, nothing will actually look at the value.
11686 if (ReturnValues.empty())
11687 return std::make_pair(SDValue(), CLI.Chain);
11688 }
11689
11690 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11691 CLI.DAG.getVTList(RetVTs), ReturnValues);
11692 return std::make_pair(Res, CLI.Chain);
11693}
11694
11695/// Places new result values for the node in Results (their number
11696/// and types must exactly match those of the original return values of
11697/// the node), or leaves Results empty, which indicates that the node is not
11698/// to be custom lowered after all.
11701 SelectionDAG &DAG) const {
11702 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11703
11704 if (!Res.getNode())
11705 return;
11706
11707 // If the original node has one result, take the return value from
11708 // LowerOperation as is. It might not be result number 0.
11709 if (N->getNumValues() == 1) {
11710 Results.push_back(Res);
11711 return;
11712 }
11713
11714 // If the original node has multiple results, then the return node should
11715 // have the same number of results.
11716 assert((N->getNumValues() == Res->getNumValues()) &&
11717 "Lowering returned the wrong number of results!");
11718
11719 // Places new result values base on N result number.
11720 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11721 Results.push_back(Res.getValue(I));
11722}
11723
11725 llvm_unreachable("LowerOperation not implemented for this target!");
11726}
11727
11729 Register Reg,
11730 ISD::NodeType ExtendType) {
11732 assert((Op.getOpcode() != ISD::CopyFromReg ||
11733 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11734 "Copy from a reg to the same reg!");
11735 assert(!Reg.isPhysical() && "Is a physreg");
11736
11737 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11738 // If this is an InlineAsm we have to match the registers required, not the
11739 // notional registers required by the type.
11740
11741 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11742 std::nullopt); // This is not an ABI copy.
11743 SDValue Chain = DAG.getEntryNode();
11744
11745 if (ExtendType == ISD::ANY_EXTEND) {
11746 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11747 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11748 ExtendType = PreferredExtendIt->second;
11749 }
11750 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11751 PendingExports.push_back(Chain);
11752}
11753
11755
11756/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11757/// entry block, return true. This includes arguments used by switches, since
11758/// the switch may expand into multiple basic blocks.
11759static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11760 // With FastISel active, we may be splitting blocks, so force creation
11761 // of virtual registers for all non-dead arguments.
11762 if (FastISel)
11763 return A->use_empty();
11764
11765 const BasicBlock &Entry = A->getParent()->front();
11766 for (const User *U : A->users())
11767 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11768 return false; // Use not in entry block.
11769
11770 return true;
11771}
11772
11774 DenseMap<const Argument *,
11775 std::pair<const AllocaInst *, const StoreInst *>>;
11776
11777/// Scan the entry block of the function in FuncInfo for arguments that look
11778/// like copies into a local alloca. Record any copied arguments in
11779/// ArgCopyElisionCandidates.
11780static void
11782 FunctionLoweringInfo *FuncInfo,
11783 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11784 // Record the state of every static alloca used in the entry block. Argument
11785 // allocas are all used in the entry block, so we need approximately as many
11786 // entries as we have arguments.
11787 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11789 unsigned NumArgs = FuncInfo->Fn->arg_size();
11790 StaticAllocas.reserve(NumArgs * 2);
11791
11792 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11793 if (!V)
11794 return nullptr;
11795 V = V->stripPointerCasts();
11796 const auto *AI = dyn_cast<AllocaInst>(V);
11797 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11798 return nullptr;
11799 auto Iter = StaticAllocas.insert({AI, Unknown});
11800 return &Iter.first->second;
11801 };
11802
11803 // Look for stores of arguments to static allocas. Look through bitcasts and
11804 // GEPs to handle type coercions, as long as the alloca is fully initialized
11805 // by the store. Any non-store use of an alloca escapes it and any subsequent
11806 // unanalyzed store might write it.
11807 // FIXME: Handle structs initialized with multiple stores.
11808 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11809 // Look for stores, and handle non-store uses conservatively.
11810 const auto *SI = dyn_cast<StoreInst>(&I);
11811 if (!SI) {
11812 // We will look through cast uses, so ignore them completely.
11813 if (I.isCast())
11814 continue;
11815 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11816 // to allocas.
11817 if (I.isDebugOrPseudoInst())
11818 continue;
11819 // This is an unknown instruction. Assume it escapes or writes to all
11820 // static alloca operands.
11821 for (const Use &U : I.operands()) {
11822 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11823 *Info = StaticAllocaInfo::Clobbered;
11824 }
11825 continue;
11826 }
11827
11828 // If the stored value is a static alloca, mark it as escaped.
11829 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11830 *Info = StaticAllocaInfo::Clobbered;
11831
11832 // Check if the destination is a static alloca.
11833 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11834 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11835 if (!Info)
11836 continue;
11837 const AllocaInst *AI = cast<AllocaInst>(Dst);
11838
11839 // Skip allocas that have been initialized or clobbered.
11840 if (*Info != StaticAllocaInfo::Unknown)
11841 continue;
11842
11843 // Check if the stored value is an argument, and that this store fully
11844 // initializes the alloca.
11845 // If the argument type has padding bits we can't directly forward a pointer
11846 // as the upper bits may contain garbage.
11847 // Don't elide copies from the same argument twice.
11848 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11849 const auto *Arg = dyn_cast<Argument>(Val);
11850 std::optional<TypeSize> AllocaSize = AI->getAllocationSize(DL);
11851 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11852 Arg->getType()->isEmptyTy() || !AllocaSize ||
11853 DL.getTypeStoreSize(Arg->getType()) != *AllocaSize ||
11854 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11855 ArgCopyElisionCandidates.count(Arg)) {
11856 *Info = StaticAllocaInfo::Clobbered;
11857 continue;
11858 }
11859
11860 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11861 << '\n');
11862
11863 // Mark this alloca and store for argument copy elision.
11864 *Info = StaticAllocaInfo::Elidable;
11865 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11866
11867 // Stop scanning if we've seen all arguments. This will happen early in -O0
11868 // builds, which is useful, because -O0 builds have large entry blocks and
11869 // many allocas.
11870 if (ArgCopyElisionCandidates.size() == NumArgs)
11871 break;
11872 }
11873}
11874
11875/// Try to elide argument copies from memory into a local alloca. Succeeds if
11876/// ArgVal is a load from a suitable fixed stack object.
11879 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11880 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11881 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11882 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11883 // Check if this is a load from a fixed stack object.
11884 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11885 if (!LNode)
11886 return;
11887 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11888 if (!FINode)
11889 return;
11890
11891 // Check that the fixed stack object is the right size and alignment.
11892 // Look at the alignment that the user wrote on the alloca instead of looking
11893 // at the stack object.
11894 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11895 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11896 const AllocaInst *AI = ArgCopyIter->second.first;
11897 int FixedIndex = FINode->getIndex();
11898 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11899 int OldIndex = AllocaIndex;
11900 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11901 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11902 LLVM_DEBUG(
11903 dbgs() << " argument copy elision failed due to bad fixed stack "
11904 "object size\n");
11905 return;
11906 }
11907 Align RequiredAlignment = AI->getAlign();
11908 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11909 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11910 "greater than stack argument alignment ("
11911 << DebugStr(RequiredAlignment) << " vs "
11912 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11913 return;
11914 }
11915
11916 // Perform the elision. Delete the old stack object and replace its only use
11917 // in the variable info map. Mark the stack object as mutable and aliased.
11918 LLVM_DEBUG({
11919 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11920 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11921 << '\n';
11922 });
11923 MFI.RemoveStackObject(OldIndex);
11924 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11925 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11926 AllocaIndex = FixedIndex;
11927 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11928 for (SDValue ArgVal : ArgVals)
11929 Chains.push_back(ArgVal.getValue(1));
11930
11931 // Avoid emitting code for the store implementing the copy.
11932 const StoreInst *SI = ArgCopyIter->second.second;
11933 ElidedArgCopyInstrs.insert(SI);
11934
11935 // Check for uses of the argument again so that we can avoid exporting ArgVal
11936 // if it is't used by anything other than the store.
11937 for (const Value *U : Arg.users()) {
11938 if (U != SI) {
11939 ArgHasUses = true;
11940 break;
11941 }
11942 }
11943}
11944
11945void SelectionDAGISel::LowerArguments(const Function &F) {
11946 SelectionDAG &DAG = SDB->DAG;
11947 SDLoc dl = SDB->getCurSDLoc();
11948 const DataLayout &DL = DAG.getDataLayout();
11950
11951 // In Naked functions we aren't going to save any registers.
11952 if (F.hasFnAttribute(Attribute::Naked))
11953 return;
11954
11955 if (!FuncInfo->CanLowerReturn) {
11956 // Put in an sret pointer parameter before all the other parameters.
11957 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11958
11959 ISD::ArgFlagsTy Flags;
11960 Flags.setSRet();
11961 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11962 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11964 Ins.push_back(RetArg);
11965 }
11966
11967 // Look for stores of arguments to static allocas. Mark such arguments with a
11968 // flag to ask the target to give us the memory location of that argument if
11969 // available.
11970 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11972 ArgCopyElisionCandidates);
11973
11974 // Set up the incoming argument description vector.
11975 for (const Argument &Arg : F.args()) {
11976 unsigned ArgNo = Arg.getArgNo();
11978 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11979 bool isArgValueUsed = !Arg.use_empty();
11980 Type *FinalType = Arg.getType();
11981 if (Arg.hasAttribute(Attribute::ByVal))
11982 FinalType = Arg.getParamByValType();
11983 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11984 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11985 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11986 ++Value) {
11987 Type *ArgTy = Types[Value];
11988 EVT VT = TLI->getValueType(DL, ArgTy);
11989 ISD::ArgFlagsTy Flags;
11990
11991 if (ArgTy->isPointerTy()) {
11992 Flags.setPointer();
11993 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11994 }
11995 if (Arg.hasAttribute(Attribute::ZExt))
11996 Flags.setZExt();
11997 if (Arg.hasAttribute(Attribute::SExt))
11998 Flags.setSExt();
11999 if (Arg.hasAttribute(Attribute::InReg)) {
12000 // If we are using vectorcall calling convention, a structure that is
12001 // passed InReg - is surely an HVA
12002 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
12003 isa<StructType>(Arg.getType())) {
12004 // The first value of a structure is marked
12005 if (0 == Value)
12006 Flags.setHvaStart();
12007 Flags.setHva();
12008 }
12009 // Set InReg Flag
12010 Flags.setInReg();
12011 }
12012 if (Arg.hasAttribute(Attribute::StructRet))
12013 Flags.setSRet();
12014 if (Arg.hasAttribute(Attribute::SwiftSelf))
12015 Flags.setSwiftSelf();
12016 if (Arg.hasAttribute(Attribute::SwiftAsync))
12017 Flags.setSwiftAsync();
12018 if (Arg.hasAttribute(Attribute::SwiftError))
12019 Flags.setSwiftError();
12020 if (Arg.hasAttribute(Attribute::ByVal))
12021 Flags.setByVal();
12022 if (Arg.hasAttribute(Attribute::ByRef))
12023 Flags.setByRef();
12024 if (Arg.hasAttribute(Attribute::InAlloca)) {
12025 Flags.setInAlloca();
12026 // Set the byval flag for CCAssignFn callbacks that don't know about
12027 // inalloca. This way we can know how many bytes we should've allocated
12028 // and how many bytes a callee cleanup function will pop. If we port
12029 // inalloca to more targets, we'll have to add custom inalloca handling
12030 // in the various CC lowering callbacks.
12031 Flags.setByVal();
12032 }
12033 if (Arg.hasAttribute(Attribute::Preallocated)) {
12034 Flags.setPreallocated();
12035 // Set the byval flag for CCAssignFn callbacks that don't know about
12036 // preallocated. This way we can know how many bytes we should've
12037 // allocated and how many bytes a callee cleanup function will pop. If
12038 // we port preallocated to more targets, we'll have to add custom
12039 // preallocated handling in the various CC lowering callbacks.
12040 Flags.setByVal();
12041 }
12042
12043 // Certain targets (such as MIPS), may have a different ABI alignment
12044 // for a type depending on the context. Give the target a chance to
12045 // specify the alignment it wants.
12046 const Align OriginalAlignment(
12047 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
12048 Flags.setOrigAlign(OriginalAlignment);
12049
12050 Align MemAlign;
12051 Type *ArgMemTy = nullptr;
12052 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
12053 Flags.isByRef()) {
12054 if (!ArgMemTy)
12055 ArgMemTy = Arg.getPointeeInMemoryValueType();
12056
12057 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
12058
12059 // For in-memory arguments, size and alignment should be passed from FE.
12060 // BE will guess if this info is not there but there are cases it cannot
12061 // get right.
12062 if (auto ParamAlign = Arg.getParamStackAlign())
12063 MemAlign = *ParamAlign;
12064 else if ((ParamAlign = Arg.getParamAlign()))
12065 MemAlign = *ParamAlign;
12066 else
12067 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
12068 if (Flags.isByRef())
12069 Flags.setByRefSize(MemSize);
12070 else
12071 Flags.setByValSize(MemSize);
12072 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
12073 MemAlign = *ParamAlign;
12074 } else {
12075 MemAlign = OriginalAlignment;
12076 }
12077 Flags.setMemAlign(MemAlign);
12078
12079 if (Arg.hasAttribute(Attribute::Nest))
12080 Flags.setNest();
12081 if (NeedsRegBlock)
12082 Flags.setInConsecutiveRegs();
12083 if (ArgCopyElisionCandidates.count(&Arg))
12084 Flags.setCopyElisionCandidate();
12085 if (Arg.hasAttribute(Attribute::Returned))
12086 Flags.setReturned();
12087
12088 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
12089 *CurDAG->getContext(), F.getCallingConv(), VT);
12090 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
12091 *CurDAG->getContext(), F.getCallingConv(), VT);
12092 for (unsigned i = 0; i != NumRegs; ++i) {
12093 // For scalable vectors, use the minimum size; individual targets
12094 // are responsible for handling scalable vector arguments and
12095 // return values.
12096 ISD::InputArg MyFlags(
12097 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
12098 i * RegisterVT.getStoreSize().getKnownMinValue());
12099 if (NumRegs > 1 && i == 0)
12100 MyFlags.Flags.setSplit();
12101 // if it isn't first piece, alignment must be 1
12102 else if (i > 0) {
12103 MyFlags.Flags.setOrigAlign(Align(1));
12104 if (i == NumRegs - 1)
12105 MyFlags.Flags.setSplitEnd();
12106 }
12107 Ins.push_back(MyFlags);
12108 }
12109 if (NeedsRegBlock && Value == NumValues - 1)
12110 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
12111 }
12112 }
12113
12114 // Call the target to set up the argument values.
12116 SDValue NewRoot = TLI->LowerFormalArguments(
12117 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
12118
12119 // Verify that the target's LowerFormalArguments behaved as expected.
12120 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
12121 "LowerFormalArguments didn't return a valid chain!");
12122 assert(InVals.size() == Ins.size() &&
12123 "LowerFormalArguments didn't emit the correct number of values!");
12124 assert(all_of(InVals, [](SDValue InVal) { return InVal.getNode(); }) &&
12125 "LowerFormalArguments emitted a null value!");
12126
12127 // Update the DAG with the new chain value resulting from argument lowering.
12128 DAG.setRoot(NewRoot);
12129
12130 // Set up the argument values.
12131 unsigned i = 0;
12132 if (!FuncInfo->CanLowerReturn) {
12133 // Create a virtual register for the sret pointer, and put in a copy
12134 // from the sret argument into it.
12135 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
12136 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
12137 std::optional<ISD::NodeType> AssertOp;
12138 SDValue ArgValue =
12139 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
12140 F.getCallingConv(), AssertOp);
12141
12142 MachineFunction& MF = SDB->DAG.getMachineFunction();
12143 MachineRegisterInfo& RegInfo = MF.getRegInfo();
12144 Register SRetReg =
12145 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
12146 FuncInfo->DemoteRegister = SRetReg;
12147 NewRoot =
12148 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
12149 DAG.setRoot(NewRoot);
12150
12151 // i indexes lowered arguments. Bump it past the hidden sret argument.
12152 ++i;
12153 }
12154
12156 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
12157 for (const Argument &Arg : F.args()) {
12158 SmallVector<SDValue, 4> ArgValues;
12159 SmallVector<EVT, 4> ValueVTs;
12160 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
12161 unsigned NumValues = ValueVTs.size();
12162 if (NumValues == 0)
12163 continue;
12164
12165 bool ArgHasUses = !Arg.use_empty();
12166
12167 // Elide the copying store if the target loaded this argument from a
12168 // suitable fixed stack object.
12169 if (Ins[i].Flags.isCopyElisionCandidate()) {
12170 unsigned NumParts = 0;
12171 for (EVT VT : ValueVTs)
12172 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
12173 F.getCallingConv(), VT);
12174
12175 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
12176 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
12177 ArrayRef(&InVals[i], NumParts), ArgHasUses);
12178 }
12179
12180 // If this argument is unused then remember its value. It is used to generate
12181 // debugging information.
12182 bool isSwiftErrorArg =
12183 TLI->supportSwiftError() &&
12184 Arg.hasAttribute(Attribute::SwiftError);
12185 if (!ArgHasUses && !isSwiftErrorArg) {
12186 SDB->setUnusedArgValue(&Arg, InVals[i]);
12187
12188 // Also remember any frame index for use in FastISel.
12189 if (FrameIndexSDNode *FI =
12191 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12192 }
12193
12194 for (unsigned Val = 0; Val != NumValues; ++Val) {
12195 EVT VT = ValueVTs[Val];
12196 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
12197 F.getCallingConv(), VT);
12198 unsigned NumParts = TLI->getNumRegistersForCallingConv(
12199 *CurDAG->getContext(), F.getCallingConv(), VT);
12200
12201 // Even an apparent 'unused' swifterror argument needs to be returned. So
12202 // we do generate a copy for it that can be used on return from the
12203 // function.
12204 if (ArgHasUses || isSwiftErrorArg) {
12205 std::optional<ISD::NodeType> AssertOp;
12206 if (Arg.hasAttribute(Attribute::SExt))
12207 AssertOp = ISD::AssertSext;
12208 else if (Arg.hasAttribute(Attribute::ZExt))
12209 AssertOp = ISD::AssertZext;
12210
12211 SDValue OutVal =
12212 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
12213 NewRoot, F.getCallingConv(), AssertOp);
12214
12215 FPClassTest NoFPClass = Arg.getNoFPClass();
12216 if (NoFPClass != fcNone) {
12217 SDValue SDNoFPClass = DAG.getTargetConstant(
12218 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
12219 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
12220 OutVal, SDNoFPClass);
12221 }
12222 ArgValues.push_back(OutVal);
12223 }
12224
12225 i += NumParts;
12226 }
12227
12228 // We don't need to do anything else for unused arguments.
12229 if (ArgValues.empty())
12230 continue;
12231
12232 // Note down frame index.
12233 if (FrameIndexSDNode *FI =
12234 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
12235 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12236
12237 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
12238 SDB->getCurSDLoc());
12239
12240 SDB->setValue(&Arg, Res);
12241 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
12242 // We want to associate the argument with the frame index, among
12243 // involved operands, that correspond to the lowest address. The
12244 // getCopyFromParts function, called earlier, is swapping the order of
12245 // the operands to BUILD_PAIR depending on endianness. The result of
12246 // that swapping is that the least significant bits of the argument will
12247 // be in the first operand of the BUILD_PAIR node, and the most
12248 // significant bits will be in the second operand.
12249 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
12250 if (LoadSDNode *LNode =
12251 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
12252 if (FrameIndexSDNode *FI =
12253 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
12254 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12255 }
12256
12257 // Analyses past this point are naive and don't expect an assertion.
12258 if (Res.getOpcode() == ISD::AssertZext)
12259 Res = Res.getOperand(0);
12260
12261 // Update the SwiftErrorVRegDefMap.
12262 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
12263 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12264 if (Reg.isVirtual())
12265 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
12266 Reg);
12267 }
12268
12269 // If this argument is live outside of the entry block, insert a copy from
12270 // wherever we got it to the vreg that other BB's will reference it as.
12271 if (Res.getOpcode() == ISD::CopyFromReg) {
12272 // If we can, though, try to skip creating an unnecessary vreg.
12273 // FIXME: This isn't very clean... it would be nice to make this more
12274 // general.
12275 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12276 if (Reg.isVirtual()) {
12277 FuncInfo->ValueMap[&Arg] = Reg;
12278 continue;
12279 }
12280 }
12281 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12282 FuncInfo->InitializeRegForValue(&Arg);
12283 SDB->CopyToExportRegsIfNeeded(&Arg);
12284 }
12285 }
12286
12287 if (!Chains.empty()) {
12288 Chains.push_back(NewRoot);
12289 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12290 }
12291
12292 DAG.setRoot(NewRoot);
12293
12294 assert(i == InVals.size() && "Argument register count mismatch!");
12295
12296 // If any argument copy elisions occurred and we have debug info, update the
12297 // stale frame indices used in the dbg.declare variable info table.
12298 if (!ArgCopyElisionFrameIndexMap.empty()) {
12299 for (MachineFunction::VariableDbgInfo &VI :
12300 MF->getInStackSlotVariableDbgInfo()) {
12301 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12302 if (I != ArgCopyElisionFrameIndexMap.end())
12303 VI.updateStackSlot(I->second);
12304 }
12305 }
12306
12307 // Finally, if the target has anything special to do, allow it to do so.
12309}
12310
12311/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12312/// ensure constants are generated when needed. Remember the virtual registers
12313/// that need to be added to the Machine PHI nodes as input. We cannot just
12314/// directly add them, because expansion might result in multiple MBB's for one
12315/// BB. As such, the start of the BB might correspond to a different MBB than
12316/// the end.
12317void
12318SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12319 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12320
12321 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12322
12323 // Check PHI nodes in successors that expect a value to be available from this
12324 // block.
12325 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12326 if (!isa<PHINode>(SuccBB->begin())) continue;
12327 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12328
12329 // If this terminator has multiple identical successors (common for
12330 // switches), only handle each succ once.
12331 if (!SuccsHandled.insert(SuccMBB).second)
12332 continue;
12333
12335
12336 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12337 // nodes and Machine PHI nodes, but the incoming operands have not been
12338 // emitted yet.
12339 for (const PHINode &PN : SuccBB->phis()) {
12340 // Ignore dead phi's.
12341 if (PN.use_empty())
12342 continue;
12343
12344 // Skip empty types
12345 if (PN.getType()->isEmptyTy())
12346 continue;
12347
12348 Register Reg;
12349 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12350
12351 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12352 Register &RegOut = ConstantsOut[C];
12353 if (!RegOut) {
12354 RegOut = FuncInfo.CreateRegs(&PN);
12355 // We need to zero/sign extend ConstantInt phi operands to match
12356 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12357 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12358 if (auto *CI = dyn_cast<ConstantInt>(C))
12359 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12361 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12362 }
12363 Reg = RegOut;
12364 } else {
12365 auto I = FuncInfo.ValueMap.find(PHIOp);
12366 if (I != FuncInfo.ValueMap.end())
12367 Reg = I->second;
12368 else {
12369 assert(isa<AllocaInst>(PHIOp) &&
12370 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12371 "Didn't codegen value into a register!??");
12372 Reg = FuncInfo.CreateRegs(&PN);
12374 }
12375 }
12376
12377 // Remember that this register needs to added to the machine PHI node as
12378 // the input for this MBB.
12379 SmallVector<EVT, 4> ValueVTs;
12380 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12381 for (EVT VT : ValueVTs) {
12382 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12383 for (unsigned i = 0; i != NumRegisters; ++i)
12384 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12385 Reg += NumRegisters;
12386 }
12387 }
12388 }
12389
12390 ConstantsOut.clear();
12391}
12392
12393MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12395 if (++I == FuncInfo.MF->end())
12396 return nullptr;
12397 return &*I;
12398}
12399
12400/// During lowering new call nodes can be created (such as memset, etc.).
12401/// Those will become new roots of the current DAG, but complications arise
12402/// when they are tail calls. In such cases, the call lowering will update
12403/// the root, but the builder still needs to know that a tail call has been
12404/// lowered in order to avoid generating an additional return.
12405void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12406 // If the node is null, we do have a tail call.
12407 if (MaybeTC.getNode() != nullptr)
12408 DAG.setRoot(MaybeTC);
12409 else
12410 HasTailCall = true;
12411}
12412
12413void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12414 MachineBasicBlock *SwitchMBB,
12415 MachineBasicBlock *DefaultMBB) {
12416 MachineFunction *CurMF = FuncInfo.MF;
12417 MachineBasicBlock *NextMBB = nullptr;
12419 if (++BBI != FuncInfo.MF->end())
12420 NextMBB = &*BBI;
12421
12422 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12423
12424 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12425
12426 if (Size == 2 && W.MBB == SwitchMBB) {
12427 // If any two of the cases has the same destination, and if one value
12428 // is the same as the other, but has one bit unset that the other has set,
12429 // use bit manipulation to do two compares at once. For example:
12430 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12431 // TODO: This could be extended to merge any 2 cases in switches with 3
12432 // cases.
12433 // TODO: Handle cases where W.CaseBB != SwitchBB.
12434 CaseCluster &Small = *W.FirstCluster;
12435 CaseCluster &Big = *W.LastCluster;
12436
12437 if (Small.Low == Small.High && Big.Low == Big.High &&
12438 Small.MBB == Big.MBB) {
12439 const APInt &SmallValue = Small.Low->getValue();
12440 const APInt &BigValue = Big.Low->getValue();
12441
12442 // Check that there is only one bit different.
12443 APInt CommonBit = BigValue ^ SmallValue;
12444 if (CommonBit.isPowerOf2()) {
12445 SDValue CondLHS = getValue(Cond);
12446 EVT VT = CondLHS.getValueType();
12447 SDLoc DL = getCurSDLoc();
12448
12449 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12450 DAG.getConstant(CommonBit, DL, VT));
12451 SDValue Cond = DAG.getSetCC(
12452 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12453 ISD::SETEQ);
12454
12455 // Update successor info.
12456 // Both Small and Big will jump to Small.BB, so we sum up the
12457 // probabilities.
12458 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12459 if (BPI)
12460 addSuccessorWithProb(
12461 SwitchMBB, DefaultMBB,
12462 // The default destination is the first successor in IR.
12463 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12464 else
12465 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12466
12467 // Insert the true branch.
12468 SDValue BrCond =
12469 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12470 DAG.getBasicBlock(Small.MBB));
12471 // Insert the false branch.
12472 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12473 DAG.getBasicBlock(DefaultMBB));
12474
12475 DAG.setRoot(BrCond);
12476 return;
12477 }
12478 }
12479 }
12480
12481 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12482 // Here, we order cases by probability so the most likely case will be
12483 // checked first. However, two clusters can have the same probability in
12484 // which case their relative ordering is non-deterministic. So we use Low
12485 // as a tie-breaker as clusters are guaranteed to never overlap.
12486 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12487 [](const CaseCluster &a, const CaseCluster &b) {
12488 return a.Prob != b.Prob ?
12489 a.Prob > b.Prob :
12490 a.Low->getValue().slt(b.Low->getValue());
12491 });
12492
12493 // Rearrange the case blocks so that the last one falls through if possible
12494 // without changing the order of probabilities.
12495 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12496 --I;
12497 if (I->Prob > W.LastCluster->Prob)
12498 break;
12499 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12500 std::swap(*I, *W.LastCluster);
12501 break;
12502 }
12503 }
12504 }
12505
12506 // Compute total probability.
12507 BranchProbability DefaultProb = W.DefaultProb;
12508 BranchProbability UnhandledProbs = DefaultProb;
12509 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12510 UnhandledProbs += I->Prob;
12511
12512 MachineBasicBlock *CurMBB = W.MBB;
12513 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12514 bool FallthroughUnreachable = false;
12515 MachineBasicBlock *Fallthrough;
12516 if (I == W.LastCluster) {
12517 // For the last cluster, fall through to the default destination.
12518 Fallthrough = DefaultMBB;
12519 FallthroughUnreachable = isa<UnreachableInst>(
12520 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12521 } else {
12522 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12523 CurMF->insert(BBI, Fallthrough);
12524 // Put Cond in a virtual register to make it available from the new blocks.
12526 }
12527 UnhandledProbs -= I->Prob;
12528
12529 switch (I->Kind) {
12530 case CC_JumpTable: {
12531 // FIXME: Optimize away range check based on pivot comparisons.
12532 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12533 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12534
12535 // The jump block hasn't been inserted yet; insert it here.
12536 MachineBasicBlock *JumpMBB = JT->MBB;
12537 CurMF->insert(BBI, JumpMBB);
12538
12539 auto JumpProb = I->Prob;
12540 auto FallthroughProb = UnhandledProbs;
12541
12542 // If the default statement is a target of the jump table, we evenly
12543 // distribute the default probability to successors of CurMBB. Also
12544 // update the probability on the edge from JumpMBB to Fallthrough.
12545 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12546 SE = JumpMBB->succ_end();
12547 SI != SE; ++SI) {
12548 if (*SI == DefaultMBB) {
12549 JumpProb += DefaultProb / 2;
12550 FallthroughProb -= DefaultProb / 2;
12551 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12552 JumpMBB->normalizeSuccProbs();
12553 break;
12554 }
12555 }
12556
12557 // If the default clause is unreachable, propagate that knowledge into
12558 // JTH->FallthroughUnreachable which will use it to suppress the range
12559 // check.
12560 //
12561 // However, don't do this if we're doing branch target enforcement,
12562 // because a table branch _without_ a range check can be a tempting JOP
12563 // gadget - out-of-bounds inputs that are impossible in correct
12564 // execution become possible again if an attacker can influence the
12565 // control flow. So if an attacker doesn't already have a BTI bypass
12566 // available, we don't want them to be able to get one out of this
12567 // table branch.
12568 if (FallthroughUnreachable) {
12569 Function &CurFunc = CurMF->getFunction();
12570 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12571 JTH->FallthroughUnreachable = true;
12572 }
12573
12574 if (!JTH->FallthroughUnreachable)
12575 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12576 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12577 CurMBB->normalizeSuccProbs();
12578
12579 // The jump table header will be inserted in our current block, do the
12580 // range check, and fall through to our fallthrough block.
12581 JTH->HeaderBB = CurMBB;
12582 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12583
12584 // If we're in the right place, emit the jump table header right now.
12585 if (CurMBB == SwitchMBB) {
12586 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12587 JTH->Emitted = true;
12588 }
12589 break;
12590 }
12591 case CC_BitTests: {
12592 // FIXME: Optimize away range check based on pivot comparisons.
12593 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12594
12595 // The bit test blocks haven't been inserted yet; insert them here.
12596 for (BitTestCase &BTC : BTB->Cases)
12597 CurMF->insert(BBI, BTC.ThisBB);
12598
12599 // Fill in fields of the BitTestBlock.
12600 BTB->Parent = CurMBB;
12601 BTB->Default = Fallthrough;
12602
12603 BTB->DefaultProb = UnhandledProbs;
12604 // If the cases in bit test don't form a contiguous range, we evenly
12605 // distribute the probability on the edge to Fallthrough to two
12606 // successors of CurMBB.
12607 if (!BTB->ContiguousRange) {
12608 BTB->Prob += DefaultProb / 2;
12609 BTB->DefaultProb -= DefaultProb / 2;
12610 }
12611
12612 if (FallthroughUnreachable)
12613 BTB->FallthroughUnreachable = true;
12614
12615 // If we're in the right place, emit the bit test header right now.
12616 if (CurMBB == SwitchMBB) {
12617 visitBitTestHeader(*BTB, SwitchMBB);
12618 BTB->Emitted = true;
12619 }
12620 break;
12621 }
12622 case CC_Range: {
12623 const Value *RHS, *LHS, *MHS;
12624 ISD::CondCode CC;
12625 if (I->Low == I->High) {
12626 // Check Cond == I->Low.
12627 CC = ISD::SETEQ;
12628 LHS = Cond;
12629 RHS=I->Low;
12630 MHS = nullptr;
12631 } else {
12632 // Check I->Low <= Cond <= I->High.
12633 CC = ISD::SETLE;
12634 LHS = I->Low;
12635 MHS = Cond;
12636 RHS = I->High;
12637 }
12638
12639 // If Fallthrough is unreachable, fold away the comparison.
12640 if (FallthroughUnreachable)
12641 CC = ISD::SETTRUE;
12642
12643 // The false probability is the sum of all unhandled cases.
12644 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12645 getCurSDLoc(), I->Prob, UnhandledProbs);
12646
12647 if (CurMBB == SwitchMBB)
12648 visitSwitchCase(CB, SwitchMBB);
12649 else
12650 SL->SwitchCases.push_back(CB);
12651
12652 break;
12653 }
12654 }
12655 CurMBB = Fallthrough;
12656 }
12657}
12658
12659void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12660 const SwitchWorkListItem &W,
12661 Value *Cond,
12662 MachineBasicBlock *SwitchMBB) {
12663 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12664 "Clusters not sorted?");
12665 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12666
12667 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12668 SL->computeSplitWorkItemInfo(W);
12669
12670 // Use the first element on the right as pivot since we will make less-than
12671 // comparisons against it.
12672 CaseClusterIt PivotCluster = FirstRight;
12673 assert(PivotCluster > W.FirstCluster);
12674 assert(PivotCluster <= W.LastCluster);
12675
12676 CaseClusterIt FirstLeft = W.FirstCluster;
12677 CaseClusterIt LastRight = W.LastCluster;
12678
12679 const ConstantInt *Pivot = PivotCluster->Low;
12680
12681 // New blocks will be inserted immediately after the current one.
12683 ++BBI;
12684
12685 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12686 // we can branch to its destination directly if it's squeezed exactly in
12687 // between the known lower bound and Pivot - 1.
12688 MachineBasicBlock *LeftMBB;
12689 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12690 FirstLeft->Low == W.GE &&
12691 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12692 LeftMBB = FirstLeft->MBB;
12693 } else {
12694 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12695 FuncInfo.MF->insert(BBI, LeftMBB);
12696 WorkList.push_back(
12697 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12698 // Put Cond in a virtual register to make it available from the new blocks.
12700 }
12701
12702 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12703 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12704 // directly if RHS.High equals the current upper bound.
12705 MachineBasicBlock *RightMBB;
12706 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12707 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12708 RightMBB = FirstRight->MBB;
12709 } else {
12710 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12711 FuncInfo.MF->insert(BBI, RightMBB);
12712 WorkList.push_back(
12713 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12714 // Put Cond in a virtual register to make it available from the new blocks.
12716 }
12717
12718 // Create the CaseBlock record that will be used to lower the branch.
12719 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12720 getCurSDLoc(), LeftProb, RightProb);
12721
12722 if (W.MBB == SwitchMBB)
12723 visitSwitchCase(CB, SwitchMBB);
12724 else
12725 SL->SwitchCases.push_back(CB);
12726}
12727
12728// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12729// from the swith statement.
12731 BranchProbability PeeledCaseProb) {
12732 if (PeeledCaseProb == BranchProbability::getOne())
12734 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12735
12736 uint32_t Numerator = CaseProb.getNumerator();
12737 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12738 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12739}
12740
12741// Try to peel the top probability case if it exceeds the threshold.
12742// Return current MachineBasicBlock for the switch statement if the peeling
12743// does not occur.
12744// If the peeling is performed, return the newly created MachineBasicBlock
12745// for the peeled switch statement. Also update Clusters to remove the peeled
12746// case. PeeledCaseProb is the BranchProbability for the peeled case.
12747MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12748 const SwitchInst &SI, CaseClusterVector &Clusters,
12749 BranchProbability &PeeledCaseProb) {
12750 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12751 // Don't perform if there is only one cluster or optimizing for size.
12752 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12753 TM.getOptLevel() == CodeGenOptLevel::None ||
12754 SwitchMBB->getParent()->getFunction().hasMinSize())
12755 return SwitchMBB;
12756
12757 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12758 unsigned PeeledCaseIndex = 0;
12759 bool SwitchPeeled = false;
12760 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12761 CaseCluster &CC = Clusters[Index];
12762 if (CC.Prob < TopCaseProb)
12763 continue;
12764 TopCaseProb = CC.Prob;
12765 PeeledCaseIndex = Index;
12766 SwitchPeeled = true;
12767 }
12768 if (!SwitchPeeled)
12769 return SwitchMBB;
12770
12771 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12772 << TopCaseProb << "\n");
12773
12774 // Record the MBB for the peeled switch statement.
12775 MachineFunction::iterator BBI(SwitchMBB);
12776 ++BBI;
12777 MachineBasicBlock *PeeledSwitchMBB =
12778 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12779 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12780
12781 ExportFromCurrentBlock(SI.getCondition());
12782 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12783 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12784 nullptr, nullptr, TopCaseProb.getCompl()};
12785 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12786
12787 Clusters.erase(PeeledCaseIt);
12788 for (CaseCluster &CC : Clusters) {
12789 LLVM_DEBUG(
12790 dbgs() << "Scale the probablity for one cluster, before scaling: "
12791 << CC.Prob << "\n");
12792 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12793 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12794 }
12795 PeeledCaseProb = TopCaseProb;
12796 return PeeledSwitchMBB;
12797}
12798
12799void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12800 // Extract cases from the switch.
12801 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12802 CaseClusterVector Clusters;
12803 Clusters.reserve(SI.getNumCases());
12804 for (auto I : SI.cases()) {
12805 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12806 const ConstantInt *CaseVal = I.getCaseValue();
12807 BranchProbability Prob =
12808 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12809 : BranchProbability(1, SI.getNumCases() + 1);
12810 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12811 }
12812
12813 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12814
12815 // Cluster adjacent cases with the same destination. We do this at all
12816 // optimization levels because it's cheap to do and will make codegen faster
12817 // if there are many clusters.
12818 sortAndRangeify(Clusters);
12819
12820 // The branch probablity of the peeled case.
12821 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12822 MachineBasicBlock *PeeledSwitchMBB =
12823 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12824
12825 // If there is only the default destination, jump there directly.
12826 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12827 if (Clusters.empty()) {
12828 assert(PeeledSwitchMBB == SwitchMBB);
12829 SwitchMBB->addSuccessor(DefaultMBB);
12830 if (DefaultMBB != NextBlock(SwitchMBB)) {
12831 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12832 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12833 }
12834 return;
12835 }
12836
12837 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12838 DAG.getBFI());
12839 SL->findBitTestClusters(Clusters, &SI);
12840
12841 LLVM_DEBUG({
12842 dbgs() << "Case clusters: ";
12843 for (const CaseCluster &C : Clusters) {
12844 if (C.Kind == CC_JumpTable)
12845 dbgs() << "JT:";
12846 if (C.Kind == CC_BitTests)
12847 dbgs() << "BT:";
12848
12849 C.Low->getValue().print(dbgs(), true);
12850 if (C.Low != C.High) {
12851 dbgs() << '-';
12852 C.High->getValue().print(dbgs(), true);
12853 }
12854 dbgs() << ' ';
12855 }
12856 dbgs() << '\n';
12857 });
12858
12859 assert(!Clusters.empty());
12860 SwitchWorkList WorkList;
12861 CaseClusterIt First = Clusters.begin();
12862 CaseClusterIt Last = Clusters.end() - 1;
12863 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12864 // Scale the branchprobability for DefaultMBB if the peel occurs and
12865 // DefaultMBB is not replaced.
12866 if (PeeledCaseProb != BranchProbability::getZero() &&
12867 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12868 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12869 WorkList.push_back(
12870 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12871
12872 while (!WorkList.empty()) {
12873 SwitchWorkListItem W = WorkList.pop_back_val();
12874 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12875
12876 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12877 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12878 // For optimized builds, lower large range as a balanced binary tree.
12879 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12880 continue;
12881 }
12882
12883 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12884 }
12885}
12886
12887void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12888 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12889 auto DL = getCurSDLoc();
12890 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12891 setValue(&I, DAG.getStepVector(DL, ResultVT));
12892}
12893
12894void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12895 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12896 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12897
12898 SDLoc DL = getCurSDLoc();
12899 SDValue V = getValue(I.getOperand(0));
12900 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12901
12902 if (VT.isScalableVector()) {
12903 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12904 return;
12905 }
12906
12907 // Use VECTOR_SHUFFLE for the fixed-length vector
12908 // to maintain existing behavior.
12909 SmallVector<int, 8> Mask;
12910 unsigned NumElts = VT.getVectorMinNumElements();
12911 for (unsigned i = 0; i != NumElts; ++i)
12912 Mask.push_back(NumElts - 1 - i);
12913
12914 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12915}
12916
12917void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12918 unsigned Factor) {
12919 auto DL = getCurSDLoc();
12920 SDValue InVec = getValue(I.getOperand(0));
12921
12922 SmallVector<EVT, 4> ValueVTs;
12923 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12924 ValueVTs);
12925
12926 EVT OutVT = ValueVTs[0];
12927 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12928
12929 SmallVector<SDValue, 4> SubVecs(Factor);
12930 for (unsigned i = 0; i != Factor; ++i) {
12931 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12932 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12933 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12934 }
12935
12936 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12937 // from existing legalisation and combines.
12938 if (OutVT.isFixedLengthVector() && Factor == 2) {
12939 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12940 createStrideMask(0, 2, OutNumElts));
12941 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12942 createStrideMask(1, 2, OutNumElts));
12943 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12944 setValue(&I, Res);
12945 return;
12946 }
12947
12948 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12949 DAG.getVTList(ValueVTs), SubVecs);
12950 setValue(&I, Res);
12951}
12952
12953void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12954 unsigned Factor) {
12955 auto DL = getCurSDLoc();
12956 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12957 EVT InVT = getValue(I.getOperand(0)).getValueType();
12958 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12959
12960 SmallVector<SDValue, 8> InVecs(Factor);
12961 for (unsigned i = 0; i < Factor; ++i) {
12962 InVecs[i] = getValue(I.getOperand(i));
12963 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12964 "Expected VTs to be the same");
12965 }
12966
12967 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12968 // from existing legalisation and combines.
12969 if (OutVT.isFixedLengthVector() && Factor == 2) {
12970 unsigned NumElts = InVT.getVectorMinNumElements();
12971 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12972 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12973 createInterleaveMask(NumElts, 2)));
12974 return;
12975 }
12976
12977 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12978 SDValue Res =
12979 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12980
12982 for (unsigned i = 0; i < Factor; ++i)
12983 Results[i] = Res.getValue(i);
12984
12985 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12986 setValue(&I, Res);
12987}
12988
12989void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12990 SmallVector<EVT, 4> ValueVTs;
12991 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12992 ValueVTs);
12993 unsigned NumValues = ValueVTs.size();
12994 if (NumValues == 0) return;
12995
12996 SmallVector<SDValue, 4> Values(NumValues);
12997 SDValue Op = getValue(I.getOperand(0));
12998
12999 for (unsigned i = 0; i != NumValues; ++i)
13000 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
13001 SDValue(Op.getNode(), Op.getResNo() + i));
13002
13004 DAG.getVTList(ValueVTs), Values));
13005}
13006
13007void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
13008 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13009 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
13010
13011 SDLoc DL = getCurSDLoc();
13012 SDValue V1 = getValue(I.getOperand(0));
13013 SDValue V2 = getValue(I.getOperand(1));
13014 const bool IsLeft = I.getIntrinsicID() == Intrinsic::vector_splice_left;
13015
13016 // VECTOR_SHUFFLE doesn't support a scalable or non-constant mask.
13017 if (VT.isScalableVector() || !isa<ConstantInt>(I.getOperand(2))) {
13018 SDValue Offset = DAG.getZExtOrTrunc(
13019 getValue(I.getOperand(2)), DL, TLI.getVectorIdxTy(DAG.getDataLayout()));
13020 setValue(&I, DAG.getNode(IsLeft ? ISD::VECTOR_SPLICE_LEFT
13022 DL, VT, V1, V2, Offset));
13023 return;
13024 }
13025 uint64_t Imm = cast<ConstantInt>(I.getOperand(2))->getZExtValue();
13026
13027 unsigned NumElts = VT.getVectorNumElements();
13028
13029 uint64_t Idx = IsLeft ? Imm : NumElts - Imm;
13030
13031 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
13032 SmallVector<int, 8> Mask;
13033 for (unsigned i = 0; i < NumElts; ++i)
13034 Mask.push_back(Idx + i);
13035 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
13036}
13037
13038// Consider the following MIR after SelectionDAG, which produces output in
13039// phyregs in the first case or virtregs in the second case.
13040//
13041// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
13042// %5:gr32 = COPY $ebx
13043// %6:gr32 = COPY $edx
13044// %1:gr32 = COPY %6:gr32
13045// %0:gr32 = COPY %5:gr32
13046//
13047// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
13048// %1:gr32 = COPY %6:gr32
13049// %0:gr32 = COPY %5:gr32
13050//
13051// Given %0, we'd like to return $ebx in the first case and %5 in the second.
13052// Given %1, we'd like to return $edx in the first case and %6 in the second.
13053//
13054// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
13055// to a single virtreg (such as %0). The remaining outputs monotonically
13056// increase in virtreg number from there. If a callbr has no outputs, then it
13057// should not have a corresponding callbr landingpad; in fact, the callbr
13058// landingpad would not even be able to refer to such a callbr.
13061 // There is definitely at least one copy.
13062 assert(MI->getOpcode() == TargetOpcode::COPY &&
13063 "start of copy chain MUST be COPY");
13064 Reg = MI->getOperand(1).getReg();
13065
13066 // If the copied register in the first copy must be virtual.
13067 assert(Reg.isVirtual() && "expected COPY of virtual register");
13068 MI = MRI.def_begin(Reg)->getParent();
13069
13070 // There may be an optional second copy.
13071 if (MI->getOpcode() == TargetOpcode::COPY) {
13072 assert(Reg.isVirtual() && "expected COPY of virtual register");
13073 Reg = MI->getOperand(1).getReg();
13074 assert(Reg.isPhysical() && "expected COPY of physical register");
13075 } else {
13076 // The start of the chain must be an INLINEASM_BR.
13077 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
13078 "end of copy chain MUST be INLINEASM_BR");
13079 }
13080
13081 return Reg;
13082}
13083
13084// We must do this walk rather than the simpler
13085// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
13086// otherwise we will end up with copies of virtregs only valid along direct
13087// edges.
13088void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
13089 SmallVector<EVT, 8> ResultVTs;
13090 SmallVector<SDValue, 8> ResultValues;
13091 const auto *CBR =
13092 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
13093
13094 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13095 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
13096 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
13097
13098 Register InitialDef = FuncInfo.ValueMap[CBR];
13099 SDValue Chain = DAG.getRoot();
13100
13101 // Re-parse the asm constraints string.
13102 TargetLowering::AsmOperandInfoVector TargetConstraints =
13103 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
13104 for (auto &T : TargetConstraints) {
13105 SDISelAsmOperandInfo OpInfo(T);
13106 if (OpInfo.Type != InlineAsm::isOutput)
13107 continue;
13108
13109 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
13110 // individual constraint.
13111 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
13112
13113 switch (OpInfo.ConstraintType) {
13116 // Fill in OpInfo.AssignedRegs.Regs.
13117 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
13118
13119 // getRegistersForValue may produce 1 to many registers based on whether
13120 // the OpInfo.ConstraintVT is legal on the target or not.
13121 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
13122 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
13123 if (OriginalDef.isPhysical())
13124 FuncInfo.MBB->addLiveIn(OriginalDef);
13125 // Update the assigned registers to use the original defs.
13126 Reg = OriginalDef;
13127 }
13128
13129 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
13130 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
13131 ResultValues.push_back(V);
13132 ResultVTs.push_back(OpInfo.ConstraintVT);
13133 break;
13134 }
13136 SDValue Flag;
13137 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
13138 OpInfo, DAG);
13139 ++InitialDef;
13140 ResultValues.push_back(V);
13141 ResultVTs.push_back(OpInfo.ConstraintVT);
13142 break;
13143 }
13144 default:
13145 break;
13146 }
13147 }
13149 DAG.getVTList(ResultVTs), ResultValues);
13150 setValue(&I, V);
13151}
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:853
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:119
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:158
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:6020
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:334
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:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
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:1082
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:728
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
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:749
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1310
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:1217
uint64_t getZExtValue() const
Constant Vector Declarations.
Definition Constants.h:668
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:124
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:55
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:225
bool empty() const
Definition DenseMap.h:173
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:136
iterator end()
Definition DenseMap.h:143
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:286
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:178
Diagnostic information for inline asm reporting.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:64
An instruction for ordering other memory operations.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:869
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:736
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:724
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:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1433
LLVM_ABI StringRef getString() const
Definition Metadata.cpp:632
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.
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:118
bool contains(const KeyT &Key) const
Definition MapVector.h:148
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 isValid() const
Definition Register.h:112
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:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
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 Register getRegisterByName(const char *RegName, LLT Ty, const MachineFunction &MF) const
Return the register ID of the name passed in.
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:180
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:288
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
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:282
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
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:306
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:313
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:712
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.
@ 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:783
@ 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:857
@ 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:884
@ 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:997
@ 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:778
@ 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
@ CTLZ_ZERO_POISON
Definition ISDOpcodes.h:792
@ 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:979
@ CONVERGENCECTRL_GLUE
This does not correspond to any convergence control intrinsic.
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:848
@ 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.
@ 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:800
@ 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:974
@ 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:769
@ 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:854
@ 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:982
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:809
@ 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:930
@ 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,...
@ CTTZ_ZERO_POISON
Bit counting operators with a poisoned result for zero inputs.
Definition ISDOpcodes.h:791
@ 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:963
@ 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:949
@ 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:860
@ 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
@ ABS_MIN_POISON
ABS with a poison result for INT_MIN.
Definition ISDOpcodes.h:751
@ 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
LLVM_ABI 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:558
@ Length
Definition DWP.cpp:558
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
LLVM_ABI 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)
LLVM_ABI 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)
LLVM_ABI LLT getLLTForMVT(MVT Ty)
Get a rough equivalent of an LLT for a given MVT.
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
LLVM_ABI 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:209
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
LLVM_ABI 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:2300
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.
LLVM_ABI 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
LLVM_ABI 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
LLVM_ABI 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...
LLVM_ABI 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:862
#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:418
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
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:425
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:307
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:323
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:373
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:396
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:382
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:408
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:339
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:197
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:404
bool isFixedLengthVector() const
Definition ValueTypes.h:199
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:346
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:315
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:187
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:351
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:359
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:342
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.
LLVM_ABI void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)