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 && TLI.isZExtFree(Val, RegisterVT))
997 ExtendKind = ISD::ZERO_EXTEND;
998
999 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
1000 NumParts, RegisterVT, V, CallConv, ExtendKind);
1001 Part += NumParts;
1002 }
1003
1004 // Copy the parts into the registers.
1005 SmallVector<SDValue, 8> Chains(NumRegs);
1006 for (unsigned i = 0; i != NumRegs; ++i) {
1007 SDValue Part;
1008 if (!Glue) {
1009 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
1010 } else {
1011 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue);
1012 *Glue = Part.getValue(1);
1013 }
1014
1015 Chains[i] = Part.getValue(0);
1016 }
1017
1018 if (NumRegs == 1 || Glue)
1019 // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is
1020 // flagged to it. That is the CopyToReg nodes and the user are considered
1021 // a single scheduling unit. If we create a TokenFactor and return it as
1022 // chain, then the TokenFactor is both a predecessor (operand) of the
1023 // user as well as a successor (the TF operands are flagged to the user).
1024 // c1, f1 = CopyToReg
1025 // c2, f2 = CopyToReg
1026 // c3 = TokenFactor c1, c2
1027 // ...
1028 // = op c3, ..., f2
1029 Chain = Chains[NumRegs-1];
1030 else
1031 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1032}
1033
1035 unsigned MatchingIdx, const SDLoc &dl,
1036 SelectionDAG &DAG,
1037 std::vector<SDValue> &Ops) const {
1038 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1039
1040 InlineAsm::Flag Flag(Code, Regs.size());
1041 if (HasMatching)
1042 Flag.setMatchingOp(MatchingIdx);
1043 else if (!Regs.empty() && Regs.front().isVirtual()) {
1044 // Put the register class of the virtual registers in the flag word. That
1045 // way, later passes can recompute register class constraints for inline
1046 // assembly as well as normal instructions.
1047 // Don't do this for tied operands that can use the regclass information
1048 // from the def.
1050 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
1051 Flag.setRegClass(RC->getID());
1052 }
1053
1054 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
1055 Ops.push_back(Res);
1056
1057 if (Code == InlineAsm::Kind::Clobber) {
1058 // Clobbers should always have a 1:1 mapping with registers, and may
1059 // reference registers that have illegal (e.g. vector) types. Hence, we
1060 // shouldn't try to apply any sort of splitting logic to them.
1061 assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
1062 "No 1:1 mapping from clobbers to regs?");
1064 (void)SP;
1065 for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
1066 Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
1067 assert(
1068 (Regs[I] != SP ||
1070 "If we clobbered the stack pointer, MFI should know about it.");
1071 }
1072 return;
1073 }
1074
1075 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1076 MVT RegisterVT = RegVTs[Value];
1077 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1078 RegisterVT);
1079 for (unsigned i = 0; i != NumRegs; ++i) {
1080 assert(Reg < Regs.size() && "Mismatch in # registers expected");
1081 Register TheReg = Regs[Reg++];
1082 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1083 }
1084 }
1085}
1086
1090 unsigned I = 0;
1091 for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1092 unsigned RegCount = std::get<0>(CountAndVT);
1093 MVT RegisterVT = std::get<1>(CountAndVT);
1094 TypeSize RegisterSize = RegisterVT.getSizeInBits();
1095 for (unsigned E = I + RegCount; I != E; ++I)
1096 OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1097 }
1098 return OutVec;
1099}
1100
1102 AssumptionCache *ac, const TargetLibraryInfo *li,
1103 const TargetTransformInfo &TTI) {
1104 BatchAA = aa;
1105 AC = ac;
1106 GFI = gfi;
1107 LibInfo = li;
1108 Context = DAG.getContext();
1109 LPadToCallSiteMap.clear();
1110 this->TTI = &TTI;
1111 SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1112 AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
1113 *DAG.getMachineFunction().getFunction().getParent());
1114}
1115
1117 NodeMap.clear();
1118 UnusedArgNodeMap.clear();
1119 PendingLoads.clear();
1120 PendingExports.clear();
1121 PendingConstrainedFP.clear();
1122 PendingConstrainedFPStrict.clear();
1123 CurInst = nullptr;
1124 HasTailCall = false;
1125 SDNodeOrder = LowestSDNodeOrder;
1126 StatepointLowering.clear();
1127}
1128
1130 DanglingDebugInfoMap.clear();
1131}
1132
1133// Update DAG root to include dependencies on Pending chains.
1134SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1135 SDValue Root = DAG.getRoot();
1136
1137 if (Pending.empty())
1138 return Root;
1139
1140 // Add current root to PendingChains, unless we already indirectly
1141 // depend on it.
1142 if (Root.getOpcode() != ISD::EntryToken) {
1143 unsigned i = 0, e = Pending.size();
1144 for (; i != e; ++i) {
1145 assert(Pending[i].getNode()->getNumOperands() > 1);
1146 if (Pending[i].getNode()->getOperand(0) == Root)
1147 break; // Don't add the root if we already indirectly depend on it.
1148 }
1149
1150 if (i == e)
1151 Pending.push_back(Root);
1152 }
1153
1154 if (Pending.size() == 1)
1155 Root = Pending[0];
1156 else
1157 Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1158
1159 DAG.setRoot(Root);
1160 Pending.clear();
1161 return Root;
1162}
1163
1167
1169 // If the new exception behavior differs from that of the pending
1170 // ones, chain up them and update the root.
1171 switch (EB) {
1174 // Floating-point exceptions produced by such operations are not intended
1175 // to be observed, so the sequence of these operations does not need to be
1176 // preserved.
1177 //
1178 // They however must not be mixed with the instructions that have strict
1179 // exception behavior. Placing an operation with 'ebIgnore' behavior between
1180 // 'ebStrict' operations could distort the observed exception behavior.
1181 if (!PendingConstrainedFPStrict.empty()) {
1182 assert(PendingConstrainedFP.empty());
1183 updateRoot(PendingConstrainedFPStrict);
1184 }
1185 break;
1187 // Floating-point exception produced by these operations may be observed, so
1188 // they must be correctly chained. If trapping on FP exceptions is
1189 // disabled, the exceptions can be observed only by functions that read
1190 // exception flags, like 'llvm.get_fpenv' or 'fetestexcept'. It means that
1191 // the order of operations is not significant between barriers.
1192 //
1193 // If trapping is enabled, each operation becomes an implicit observation
1194 // point, so the operations must be sequenced according their original
1195 // source order.
1196 if (!PendingConstrainedFP.empty()) {
1197 assert(PendingConstrainedFPStrict.empty());
1198 updateRoot(PendingConstrainedFP);
1199 }
1200 // TODO: Add support for trapping-enabled scenarios.
1201 }
1202 return DAG.getRoot();
1203}
1204
1206 // Chain up all pending constrained intrinsics together with all
1207 // pending loads, by simply appending them to PendingLoads and
1208 // then calling getMemoryRoot().
1209 PendingLoads.reserve(PendingLoads.size() +
1210 PendingConstrainedFP.size() +
1211 PendingConstrainedFPStrict.size());
1212 PendingLoads.append(PendingConstrainedFP.begin(),
1213 PendingConstrainedFP.end());
1214 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1215 PendingConstrainedFPStrict.end());
1216 PendingConstrainedFP.clear();
1217 PendingConstrainedFPStrict.clear();
1218 return getMemoryRoot();
1219}
1220
1222 // We need to emit pending fpexcept.strict constrained intrinsics,
1223 // so append them to the PendingExports list.
1224 PendingExports.append(PendingConstrainedFPStrict.begin(),
1225 PendingConstrainedFPStrict.end());
1226 PendingConstrainedFPStrict.clear();
1227 return updateRoot(PendingExports);
1228}
1229
1231 DILocalVariable *Variable,
1233 DebugLoc DL) {
1234 assert(Variable && "Missing variable");
1235
1236 // Check if address has undef value.
1237 if (!Address || isa<UndefValue>(Address) ||
1238 (Address->use_empty() && !isa<Argument>(Address))) {
1239 LLVM_DEBUG(
1240 dbgs()
1241 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1242 return;
1243 }
1244
1245 bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1246
1247 SDValue &N = NodeMap[Address];
1248 if (!N.getNode() && isa<Argument>(Address))
1249 // Check unused arguments map.
1250 N = UnusedArgNodeMap[Address];
1251 SDDbgValue *SDV;
1252 if (N.getNode()) {
1253 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1254 Address = BCI->getOperand(0);
1255 // Parameters are handled specially.
1256 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1257 if (IsParameter && FINode) {
1258 // Byval parameter. We have a frame index at this point.
1259 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1260 /*IsIndirect*/ true, DL, SDNodeOrder);
1261 } else if (isa<Argument>(Address)) {
1262 // Address is an argument, so try to emit its dbg value using
1263 // virtual register info from the FuncInfo.ValueMap.
1264 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1265 FuncArgumentDbgValueKind::Declare, N);
1266 return;
1267 } else {
1268 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1269 true, DL, SDNodeOrder);
1270 }
1271 DAG.AddDbgValue(SDV, IsParameter);
1272 } else {
1273 // If Address is an argument then try to emit its dbg value using
1274 // virtual register info from the FuncInfo.ValueMap.
1275 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1276 FuncArgumentDbgValueKind::Declare, N)) {
1277 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1278 << " (could not emit func-arg dbg_value)\n");
1279 }
1280 }
1281}
1282
1284 // Add SDDbgValue nodes for any var locs here. Do so before updating
1285 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1286 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1287 // Add SDDbgValue nodes for any var locs here. Do so before updating
1288 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1289 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1290 It != End; ++It) {
1291 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1292 dropDanglingDebugInfo(Var, It->Expr);
1293 if (It->Values.isKillLocation(It->Expr)) {
1294 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1295 continue;
1296 }
1297 SmallVector<Value *> Values(It->Values.location_ops());
1298 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1299 It->Values.hasArgList())) {
1300 SmallVector<Value *, 4> Vals(It->Values.location_ops());
1302 FnVarLocs->getDILocalVariable(It->VariableID),
1303 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1304 }
1305 }
1306 }
1307
1308 // We must skip DbgVariableRecords if they've already been processed above as
1309 // we have just emitted the debug values resulting from assignment tracking
1310 // analysis, making any existing DbgVariableRecords redundant (and probably
1311 // less correct). We still need to process DbgLabelRecords. This does sink
1312 // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1313 // be important as it does so deterministcally and ordering between
1314 // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1315 // printing).
1316 bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1317 // Is there is any debug-info attached to this instruction, in the form of
1318 // DbgRecord non-instruction debug-info records.
1319 for (DbgRecord &DR : I.getDbgRecordRange()) {
1320 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1321 assert(DLR->getLabel() && "Missing label");
1322 SDDbgLabel *SDV =
1323 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1324 DAG.AddDbgLabel(SDV);
1325 continue;
1326 }
1327
1328 if (SkipDbgVariableRecords)
1329 continue;
1331 DILocalVariable *Variable = DVR.getVariable();
1334
1336 if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1337 continue;
1338 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1339 << "\n");
1341 DVR.getDebugLoc());
1342 continue;
1343 }
1344
1345 // A DbgVariableRecord with no locations is a kill location.
1347 if (Values.empty()) {
1349 SDNodeOrder);
1350 continue;
1351 }
1352
1353 // A DbgVariableRecord with an undef or absent location is also a kill
1354 // location.
1355 if (llvm::any_of(Values,
1356 [](Value *V) { return !V || isa<UndefValue>(V); })) {
1358 SDNodeOrder);
1359 continue;
1360 }
1361
1362 bool IsVariadic = DVR.hasArgList();
1363 if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1364 SDNodeOrder, IsVariadic)) {
1365 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1366 DVR.getDebugLoc(), SDNodeOrder);
1367 }
1368 }
1369}
1370
1372 visitDbgInfo(I);
1373
1374 // Set up outgoing PHI node register values before emitting the terminator.
1375 if (I.isTerminator()) {
1376 HandlePHINodesInSuccessorBlocks(I.getParent());
1377 }
1378
1379 ++SDNodeOrder;
1380 CurInst = &I;
1381
1382 // Set inserted listener only if required.
1383 bool NodeInserted = false;
1384 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1385 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1386 MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1387 if (PCSectionsMD || MMRA) {
1388 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1389 DAG, [&](SDNode *) { NodeInserted = true; });
1390 }
1391
1392 visit(I.getOpcode(), I);
1393
1394 if (!I.isTerminator() && !HasTailCall &&
1395 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1397
1398 // Handle metadata.
1399 if (PCSectionsMD || MMRA) {
1400 auto It = NodeMap.find(&I);
1401 if (It != NodeMap.end()) {
1402 if (PCSectionsMD)
1403 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1404 if (MMRA)
1405 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1406 } else if (NodeInserted) {
1407 // This should not happen; if it does, don't let it go unnoticed so we can
1408 // fix it. Relevant visit*() function is probably missing a setValue().
1409 errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1410 << I.getModule()->getName() << "]\n";
1411 LLVM_DEBUG(I.dump());
1412 assert(false);
1413 }
1414 }
1415
1416 CurInst = nullptr;
1417}
1418
1419void SelectionDAGBuilder::visitPHI(const PHINode &) {
1420 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1421}
1422
1423void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1424 // Note: this doesn't use InstVisitor, because it has to work with
1425 // ConstantExpr's in addition to instructions.
1426 switch (Opcode) {
1427 default: llvm_unreachable("Unknown instruction type encountered!");
1428 // Build the switch statement using the Instruction.def file.
1429#define HANDLE_INST(NUM, OPCODE, CLASS) \
1430 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1431#include "llvm/IR/Instruction.def"
1432 }
1433}
1434
1436 DILocalVariable *Variable,
1437 DebugLoc DL, unsigned Order,
1440 // For variadic dbg_values we will now insert poison.
1441 // FIXME: We can potentially recover these!
1443 for (const Value *V : Values) {
1444 auto *Poison = PoisonValue::get(V->getType());
1446 }
1447 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1448 /*IsIndirect=*/false, DL, Order,
1449 /*IsVariadic=*/true);
1450 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1451 return true;
1452}
1453
1455 DILocalVariable *Var,
1456 DIExpression *Expr,
1457 bool IsVariadic, DebugLoc DL,
1458 unsigned Order) {
1459 if (IsVariadic) {
1460 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1461 return;
1462 }
1463 // TODO: Dangling debug info will eventually either be resolved or produce
1464 // a poison DBG_VALUE. However in the resolution case, a gap may appear
1465 // between the original dbg.value location and its resolved DBG_VALUE,
1466 // which we should ideally fill with an extra poison DBG_VALUE.
1467 assert(Values.size() == 1);
1468 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1469}
1470
1472 const DIExpression *Expr) {
1473 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1474 DIVariable *DanglingVariable = DDI.getVariable();
1475 DIExpression *DanglingExpr = DDI.getExpression();
1476 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1477 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1478 << printDDI(nullptr, DDI) << "\n");
1479 return true;
1480 }
1481 return false;
1482 };
1483
1484 for (auto &DDIMI : DanglingDebugInfoMap) {
1485 DanglingDebugInfoVector &DDIV = DDIMI.second;
1486
1487 // If debug info is to be dropped, run it through final checks to see
1488 // whether it can be salvaged.
1489 for (auto &DDI : DDIV)
1490 if (isMatchingDbgValue(DDI))
1491 salvageUnresolvedDbgValue(DDIMI.first, DDI);
1492
1493 erase_if(DDIV, isMatchingDbgValue);
1494 }
1495}
1496
1497// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1498// generate the debug data structures now that we've seen its definition.
1500 SDValue Val) {
1501 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1502 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1503 return;
1504
1505 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1506 for (auto &DDI : DDIV) {
1507 DebugLoc DL = DDI.getDebugLoc();
1508 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1509 DILocalVariable *Variable = DDI.getVariable();
1510 DIExpression *Expr = DDI.getExpression();
1511 assert(Variable->isValidLocationForIntrinsic(DL) &&
1512 "Expected inlined-at fields to agree");
1513 SDDbgValue *SDV;
1514 if (Val.getNode()) {
1515 // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1516 // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1517 // we couldn't resolve it directly when examining the DbgValue intrinsic
1518 // in the first place we should not be more successful here). Unless we
1519 // have some test case that prove this to be correct we should avoid
1520 // calling EmitFuncArgumentDbgValue here.
1521 unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1522 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1523 FuncArgumentDbgValueKind::Value, Val)) {
1524 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1525 << printDDI(V, DDI) << "\n");
1526 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump());
1527 // Increase the SDNodeOrder for the DbgValue here to make sure it is
1528 // inserted after the definition of Val when emitting the instructions
1529 // after ISel. An alternative could be to teach
1530 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1531 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1532 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1533 << ValSDNodeOrder << "\n");
1534 SDV = getDbgValue(Val, Variable, Expr, DL,
1535 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1536 DAG.AddDbgValue(SDV, false);
1537 } else
1538 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1539 << printDDI(V, DDI)
1540 << " in EmitFuncArgumentDbgValue\n");
1541 } else {
1542 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1543 << "\n");
1544 auto Poison = PoisonValue::get(V->getType());
1545 auto SDV =
1546 DAG.getConstantDbgValue(Variable, Expr, Poison, DL, DbgSDNodeOrder);
1547 DAG.AddDbgValue(SDV, false);
1548 }
1549 }
1550 DDIV.clear();
1551}
1552
1554 DanglingDebugInfo &DDI) {
1555 // TODO: For the variadic implementation, instead of only checking the fail
1556 // state of `handleDebugValue`, we need know specifically which values were
1557 // invalid, so that we attempt to salvage only those values when processing
1558 // a DIArgList.
1559 const Value *OrigV = V;
1560 DILocalVariable *Var = DDI.getVariable();
1561 DIExpression *Expr = DDI.getExpression();
1562 DebugLoc DL = DDI.getDebugLoc();
1563 unsigned SDOrder = DDI.getSDNodeOrder();
1564
1565 // Currently we consider only dbg.value intrinsics -- we tell the salvager
1566 // that DW_OP_stack_value is desired.
1567 bool StackValue = true;
1568
1569 // Can this Value can be encoded without any further work?
1570 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1571 return;
1572
1573 // Attempt to salvage back through as many instructions as possible. Bail if
1574 // a non-instruction is seen, such as a constant expression or global
1575 // variable. FIXME: Further work could recover those too.
1576 while (isa<Instruction>(V)) {
1577 const Instruction &VAsInst = *cast<const Instruction>(V);
1578 // Temporary "0", awaiting real implementation.
1580 SmallVector<Value *, 4> AdditionalValues;
1581 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1582 Expr->getNumLocationOperands(), Ops,
1583 AdditionalValues);
1584 // If we cannot salvage any further, and haven't yet found a suitable debug
1585 // expression, bail out.
1586 if (!V)
1587 break;
1588
1589 // TODO: If AdditionalValues isn't empty, then the salvage can only be
1590 // represented with a DBG_VALUE_LIST, so we give up. When we have support
1591 // here for variadic dbg_values, remove that condition.
1592 if (!AdditionalValues.empty())
1593 break;
1594
1595 // New value and expr now represent this debuginfo.
1596 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1597
1598 // Some kind of simplification occurred: check whether the operand of the
1599 // salvaged debug expression can be encoded in this DAG.
1600 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1601 LLVM_DEBUG(
1602 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n"
1603 << *OrigV << "\nBy stripping back to:\n " << *V << "\n");
1604 return;
1605 }
1606 }
1607
1608 // This was the final opportunity to salvage this debug information, and it
1609 // couldn't be done. Place a poison DBG_VALUE at this location to terminate
1610 // any earlier variable location.
1611 assert(OrigV && "V shouldn't be null");
1612 auto *Poison = PoisonValue::get(OrigV->getType());
1613 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Poison, DL, SDNodeOrder);
1614 DAG.AddDbgValue(SDV, false);
1615 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n "
1616 << printDDI(OrigV, DDI) << "\n");
1617}
1618
1620 DIExpression *Expr,
1621 DebugLoc DbgLoc,
1622 unsigned Order) {
1626 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1627 /*IsVariadic*/ false);
1628}
1629
1631 DILocalVariable *Var,
1632 DIExpression *Expr, DebugLoc DbgLoc,
1633 unsigned Order, bool IsVariadic) {
1634 if (Values.empty())
1635 return true;
1636
1637 // Filter EntryValue locations out early.
1638 if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1639 return true;
1640
1641 SmallVector<SDDbgOperand> LocationOps;
1642 SmallVector<SDNode *> Dependencies;
1643 for (const Value *V : Values) {
1644 // Constant value.
1647 LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1648 continue;
1649 }
1650
1651 // Look through IntToPtr constants.
1652 if (auto *CE = dyn_cast<ConstantExpr>(V))
1653 if (CE->getOpcode() == Instruction::IntToPtr) {
1654 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1655 continue;
1656 }
1657
1658 // If the Value is a frame index, we can create a FrameIndex debug value
1659 // without relying on the DAG at all.
1660 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1661 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1662 if (SI != FuncInfo.StaticAllocaMap.end()) {
1663 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1664 continue;
1665 }
1666 }
1667
1668 // Do not use getValue() in here; we don't want to generate code at
1669 // this point if it hasn't been done yet.
1670 SDValue N = NodeMap[V];
1671 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1672 N = UnusedArgNodeMap[V];
1673
1674 if (N.getNode()) {
1675 // Only emit func arg dbg value for non-variadic dbg.values for now.
1676 if (!IsVariadic &&
1677 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1678 FuncArgumentDbgValueKind::Value, N))
1679 return true;
1680 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1681 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1682 // describe stack slot locations.
1683 //
1684 // Consider "int x = 0; int *px = &x;". There are two kinds of
1685 // interesting debug values here after optimization:
1686 //
1687 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
1688 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1689 //
1690 // Both describe the direct values of their associated variables.
1691 Dependencies.push_back(N.getNode());
1692 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1693 continue;
1694 }
1695 LocationOps.emplace_back(
1696 SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1697 continue;
1698 }
1699
1700 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1701 // Special rules apply for the first dbg.values of parameter variables in a
1702 // function. Identify them by the fact they reference Argument Values, that
1703 // they're parameters, and they are parameters of the current function. We
1704 // need to let them dangle until they get an SDNode.
1705 bool IsParamOfFunc =
1706 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1707 if (IsParamOfFunc)
1708 return false;
1709
1710 // The value is not used in this block yet (or it would have an SDNode).
1711 // We still want the value to appear for the user if possible -- if it has
1712 // an associated VReg, we can refer to that instead.
1713 auto VMI = FuncInfo.ValueMap.find(V);
1714 if (VMI != FuncInfo.ValueMap.end()) {
1715 Register Reg = VMI->second;
1716 // If this is a PHI node, it may be split up into several MI PHI nodes
1717 // (in FunctionLoweringInfo::set).
1718 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1719 V->getType(), std::nullopt);
1720 if (RFV.occupiesMultipleRegs()) {
1721 // FIXME: We could potentially support variadic dbg_values here.
1722 if (IsVariadic)
1723 return false;
1724 unsigned Offset = 0;
1725 unsigned BitsToDescribe = 0;
1726 if (auto VarSize = Var->getSizeInBits())
1727 BitsToDescribe = *VarSize;
1728 if (auto Fragment = Expr->getFragmentInfo())
1729 BitsToDescribe = Fragment->SizeInBits;
1730 for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1731 // Bail out if all bits are described already.
1732 if (Offset >= BitsToDescribe)
1733 break;
1734 // TODO: handle scalable vectors.
1735 unsigned RegisterSize = RegAndSize.second;
1736 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1737 ? BitsToDescribe - Offset
1738 : RegisterSize;
1739 auto FragmentExpr = DIExpression::createFragmentExpression(
1740 Expr, Offset, FragmentSize);
1741 if (!FragmentExpr)
1742 continue;
1743 SDDbgValue *SDV = DAG.getVRegDbgValue(
1744 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1745 DAG.AddDbgValue(SDV, false);
1746 Offset += RegisterSize;
1747 }
1748 return true;
1749 }
1750 // We can use simple vreg locations for variadic dbg_values as well.
1751 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1752 continue;
1753 }
1754 // We failed to create a SDDbgOperand for V.
1755 return false;
1756 }
1757
1758 // We have created a SDDbgOperand for each Value in Values.
1759 assert(!LocationOps.empty());
1760 SDDbgValue *SDV =
1761 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1762 /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1763 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1764 return true;
1765}
1766
1768 // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1769 for (auto &Pair : DanglingDebugInfoMap)
1770 for (auto &DDI : Pair.second)
1771 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1773}
1774
1775/// getCopyFromRegs - If there was virtual register allocated for the value V
1776/// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1779 SDValue Result;
1780
1781 if (It != FuncInfo.ValueMap.end()) {
1782 Register InReg = It->second;
1783
1784 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1785 DAG.getDataLayout(), InReg, Ty,
1786 std::nullopt); // This is not an ABI copy.
1787 SDValue Chain = DAG.getEntryNode();
1788 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1789 V);
1790 resolveDanglingDebugInfo(V, Result);
1791 }
1792
1793 return Result;
1794}
1795
1796/// getValue - Return an SDValue for the given Value.
1798 // If we already have an SDValue for this value, use it. It's important
1799 // to do this first, so that we don't create a CopyFromReg if we already
1800 // have a regular SDValue.
1801 SDValue &N = NodeMap[V];
1802 if (N.getNode()) return N;
1803
1804 // If there's a virtual register allocated and initialized for this
1805 // value, use it.
1806 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1807 return copyFromReg;
1808
1809 // Otherwise create a new SDValue and remember it.
1810 SDValue Val = getValueImpl(V);
1811 NodeMap[V] = Val;
1813 return Val;
1814}
1815
1816/// getNonRegisterValue - Return an SDValue for the given Value, but
1817/// don't look in FuncInfo.ValueMap for a virtual register.
1819 // If we already have an SDValue for this value, use it.
1820 SDValue &N = NodeMap[V];
1821 if (N.getNode()) {
1822 if (isIntOrFPConstant(N)) {
1823 // Remove the debug location from the node as the node is about to be used
1824 // in a location which may differ from the original debug location. This
1825 // is relevant to Constant and ConstantFP nodes because they can appear
1826 // as constant expressions inside PHI nodes.
1827 N->setDebugLoc(DebugLoc());
1828 }
1829 return N;
1830 }
1831
1832 // Otherwise create a new SDValue and remember it.
1833 SDValue Val = getValueImpl(V);
1834 NodeMap[V] = Val;
1836 return Val;
1837}
1838
1839/// getValueImpl - Helper function for getValue and getNonRegisterValue.
1840/// Create an SDValue for the given value.
1842 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1843
1844 if (const Constant *C = dyn_cast<Constant>(V)) {
1845 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1846
1847 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1848 SDLoc DL = getCurSDLoc();
1849
1850 // DAG.getConstant() may attempt to legalise the vector constant which can
1851 // significantly change the combines applied to the DAG. To reduce the
1852 // divergence when enabling ConstantInt based vectors we try to construct
1853 // the DAG in the same way as shufflevector based splats. TODO: The
1854 // divergence sometimes leads to better optimisations. Ideally we should
1855 // prevent DAG.getConstant() from legalising too early but there are some
1856 // degradations preventing this.
1857 if (VT.isScalableVector())
1858 return DAG.getNode(
1859 ISD::SPLAT_VECTOR, DL, VT,
1860 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1861 if (VT.isFixedLengthVector())
1862 return DAG.getSplatBuildVector(
1863 VT, DL,
1864 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1865 return DAG.getConstant(*CI, DL, VT);
1866 }
1867
1868 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1869 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1870
1871 if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1872 return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1873 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1874 getValue(CPA->getAddrDiscriminator()),
1875 getValue(CPA->getDiscriminator()));
1876 }
1877
1879 return DAG.getConstant(0, getCurSDLoc(), VT);
1880
1881 if (match(C, m_VScale()))
1882 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1883
1884 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1885 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1886
1887 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1888 return isa<PoisonValue>(C) ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
1889
1890 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1891 visit(CE->getOpcode(), *CE);
1892 SDValue N1 = NodeMap[V];
1893 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1894 return N1;
1895 }
1896
1898 SmallVector<SDValue, 4> Constants;
1899 for (const Use &U : C->operands()) {
1900 SDNode *Val = getValue(U).getNode();
1901 // If the operand is an empty aggregate, there are no values.
1902 if (!Val) continue;
1903 // Add each leaf value from the operand to the Constants list
1904 // to form a flattened list of all the values.
1905 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1906 Constants.push_back(SDValue(Val, i));
1907 }
1908
1909 return DAG.getMergeValues(Constants, getCurSDLoc());
1910 }
1911
1912 if (const ConstantDataSequential *CDS =
1915 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1916 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1917 // Add each leaf value from the operand to the Constants list
1918 // to form a flattened list of all the values.
1919 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1920 Ops.push_back(SDValue(Val, i));
1921 }
1922
1923 if (isa<ArrayType>(CDS->getType()))
1924 return DAG.getMergeValues(Ops, getCurSDLoc());
1925 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1926 }
1927
1928 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1930 "Unknown struct or array constant!");
1931
1932 SmallVector<EVT, 4> ValueVTs;
1933 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1934 unsigned NumElts = ValueVTs.size();
1935 if (NumElts == 0)
1936 return SDValue(); // empty struct
1937 SmallVector<SDValue, 4> Constants(NumElts);
1938 for (unsigned i = 0; i != NumElts; ++i) {
1939 EVT EltVT = ValueVTs[i];
1940 if (isa<UndefValue>(C))
1941 Constants[i] = DAG.getUNDEF(EltVT);
1942 else if (EltVT.isFloatingPoint())
1943 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1944 else
1945 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1946 }
1947
1948 return DAG.getMergeValues(Constants, getCurSDLoc());
1949 }
1950
1951 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1952 return DAG.getBlockAddress(BA, VT);
1953
1954 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1955 return getValue(Equiv->getGlobalValue());
1956
1957 if (const auto *NC = dyn_cast<NoCFIValue>(C))
1958 return getValue(NC->getGlobalValue());
1959
1960 if (VT == MVT::aarch64svcount) {
1961 assert(C->isNullValue() && "Can only zero this target type!");
1962 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1963 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1964 }
1965
1966 if (VT.isRISCVVectorTuple()) {
1967 assert(C->isNullValue() && "Can only zero this target type!");
1968 return DAG.getNode(
1970 DAG.getNode(
1972 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1973 VT.getSizeInBits().getKnownMinValue() / 8, true),
1974 DAG.getConstant(0, getCurSDLoc(), MVT::getIntegerVT(8))));
1975 }
1976
1977 VectorType *VecTy = cast<VectorType>(V->getType());
1978
1979 // Now that we know the number and type of the elements, get that number of
1980 // elements into the Ops array based on what kind of constant it is.
1981 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1983 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1984 for (unsigned i = 0; i != NumElements; ++i)
1985 Ops.push_back(getValue(CV->getOperand(i)));
1986
1987 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1988 }
1989
1991 EVT EltVT =
1992 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1993
1994 SDValue Op;
1995 if (EltVT.isFloatingPoint())
1996 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1997 else
1998 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1999
2000 return DAG.getSplat(VT, getCurSDLoc(), Op);
2001 }
2002
2003 llvm_unreachable("Unknown vector constant");
2004 }
2005
2006 // If this is a static alloca, generate it as the frameindex instead of
2007 // computation.
2008 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
2010 FuncInfo.StaticAllocaMap.find(AI);
2011 if (SI != FuncInfo.StaticAllocaMap.end())
2012 return DAG.getFrameIndex(
2013 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
2014 }
2015
2016 // If this is an instruction which fast-isel has deferred, select it now.
2017 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2018 Register InReg = FuncInfo.InitializeRegForValue(Inst);
2019 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
2020 Inst->getType(), std::nullopt);
2021 SDValue Chain = DAG.getEntryNode();
2022 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
2023 }
2024
2025 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
2026 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
2027
2028 if (const auto *BB = dyn_cast<BasicBlock>(V))
2029 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
2030
2031 llvm_unreachable("Can't get register for value!");
2032}
2033
2034void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
2036 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
2037 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
2038 bool IsSEH = isAsynchronousEHPersonality(Pers);
2039 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2040 if (IsSEH) {
2041 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2042 CatchPadMBB->setIsEHContTarget(true);
2044 } else
2045 CatchPadMBB->setIsEHScopeEntry();
2046 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2047 if (IsMSVCCXX || IsCoreCLR)
2048 CatchPadMBB->setIsEHFuncletEntry();
2049}
2050
2051void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2052 // Update machine-CFG edge.
2053 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2054 FuncInfo.MBB->addSuccessor(TargetMBB);
2055
2056 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2057 bool IsSEH = isAsynchronousEHPersonality(Pers);
2058 if (IsSEH) {
2059 // If this is not a fall-through branch or optimizations are switched off,
2060 // emit the branch.
2061 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2062 TM.getOptLevel() == CodeGenOptLevel::None)
2063 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2064 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2065 return;
2066 }
2067
2068 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2069 TargetMBB->setIsEHContTarget(true);
2070 DAG.getMachineFunction().setHasEHContTarget(true);
2071
2072 // Figure out the funclet membership for the catchret's successor.
2073 // This will be used by the FuncletLayout pass to determine how to order the
2074 // BB's.
2075 // A 'catchret' returns to the outer scope's color.
2076 Value *ParentPad = I.getCatchSwitchParentPad();
2077 const BasicBlock *SuccessorColor;
2078 if (isa<ConstantTokenNone>(ParentPad))
2079 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2080 else
2081 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2082 assert(SuccessorColor && "No parent funclet for catchret!");
2083 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2084 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2085
2086 // Create the terminator node.
2087 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2088 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2089 DAG.getBasicBlock(SuccessorColorMBB));
2090 DAG.setRoot(Ret);
2091}
2092
2093void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2094 // Don't emit any special code for the cleanuppad instruction. It just marks
2095 // the start of an EH scope/funclet.
2096 FuncInfo.MBB->setIsEHScopeEntry();
2097 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2098 if (Pers != EHPersonality::Wasm_CXX) {
2099 FuncInfo.MBB->setIsEHFuncletEntry();
2100 FuncInfo.MBB->setIsCleanupFuncletEntry();
2101 }
2102}
2103
2104/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2105/// many places it could ultimately go. In the IR, we have a single unwind
2106/// destination, but in the machine CFG, we enumerate all the possible blocks.
2107/// This function skips over imaginary basic blocks that hold catchswitch
2108/// instructions, and finds all the "real" machine
2109/// basic block destinations. As those destinations may not be successors of
2110/// EHPadBB, here we also calculate the edge probability to those destinations.
2111/// The passed-in Prob is the edge probability to EHPadBB.
2113 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2114 BranchProbability Prob,
2115 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2116 &UnwindDests) {
2117 EHPersonality Personality =
2119 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2120 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2121 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2122 bool IsSEH = isAsynchronousEHPersonality(Personality);
2123
2124 while (EHPadBB) {
2126 BasicBlock *NewEHPadBB = nullptr;
2127 if (isa<LandingPadInst>(Pad)) {
2128 // Stop on landingpads. They are not funclets.
2129 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2130 break;
2131 } else if (isa<CleanupPadInst>(Pad)) {
2132 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2133 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2134 // which always catches an exception.
2135 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2136 UnwindDests.back().first->setIsEHScopeEntry();
2137 // In Wasm, EH scopes are not funclets
2138 if (!IsWasmCXX)
2139 UnwindDests.back().first->setIsEHFuncletEntry();
2140 break;
2141 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2142 // Add the catchpad handlers to the possible destinations.
2143 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2144 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2145 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2146 if (IsMSVCCXX || IsCoreCLR)
2147 UnwindDests.back().first->setIsEHFuncletEntry();
2148 if (!IsSEH)
2149 UnwindDests.back().first->setIsEHScopeEntry();
2150 }
2151 NewEHPadBB = CatchSwitch->getUnwindDest();
2152 } else {
2153 continue;
2154 }
2155
2156 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2157 if (BPI && NewEHPadBB)
2158 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2159 EHPadBB = NewEHPadBB;
2160 }
2161}
2162
2163void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2164 // Update successor info.
2166 auto UnwindDest = I.getUnwindDest();
2167 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2168 BranchProbability UnwindDestProb =
2169 (BPI && UnwindDest)
2170 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2172 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2173 for (auto &UnwindDest : UnwindDests) {
2174 UnwindDest.first->setIsEHPad();
2175 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2176 }
2177 FuncInfo.MBB->normalizeSuccProbs();
2178
2179 // Create the terminator node.
2180 MachineBasicBlock *CleanupPadMBB =
2181 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2182 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
2183 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2184 DAG.setRoot(Ret);
2185}
2186
2187void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2188 report_fatal_error("visitCatchSwitch not yet implemented!");
2189}
2190
2191void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2192 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2193 auto &DL = DAG.getDataLayout();
2194 SDValue Chain = getControlRoot();
2197
2198 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2199 // lower
2200 //
2201 // %val = call <ty> @llvm.experimental.deoptimize()
2202 // ret <ty> %val
2203 //
2204 // differently.
2205 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2207 return;
2208 }
2209
2210 if (!FuncInfo.CanLowerReturn) {
2211 Register DemoteReg = FuncInfo.DemoteRegister;
2212
2213 // Emit a store of the return value through the virtual register.
2214 // Leave Outs empty so that LowerReturn won't try to load return
2215 // registers the usual way.
2216 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2217 SDValue RetPtr =
2218 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2219 SDValue RetOp = getValue(I.getOperand(0));
2220
2221 SmallVector<EVT, 4> ValueVTs, MemVTs;
2222 SmallVector<uint64_t, 4> Offsets;
2223 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2224 &Offsets, 0);
2225 unsigned NumValues = ValueVTs.size();
2226
2227 SmallVector<SDValue, 4> Chains(NumValues);
2228 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2229 for (unsigned i = 0; i != NumValues; ++i) {
2230 // An aggregate return value cannot wrap around the address space, so
2231 // offsets to its parts don't wrap either.
2232 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2233 TypeSize::getFixed(Offsets[i]));
2234
2235 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2236 if (MemVTs[i] != ValueVTs[i])
2237 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2238 Chains[i] = DAG.getStore(
2239 Chain, getCurSDLoc(), Val,
2240 // FIXME: better loc info would be nice.
2241 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2242 commonAlignment(BaseAlign, Offsets[i]));
2243 }
2244
2245 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2246 MVT::Other, Chains);
2247 } else if (I.getNumOperands() != 0) {
2249 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2250 unsigned NumValues = Types.size();
2251 if (NumValues) {
2252 SDValue RetOp = getValue(I.getOperand(0));
2253
2254 const Function *F = I.getParent()->getParent();
2255
2256 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2257 I.getOperand(0)->getType(), F->getCallingConv(),
2258 /*IsVarArg*/ false, DL);
2259
2260 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2261 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2262 ExtendKind = ISD::SIGN_EXTEND;
2263 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2264 ExtendKind = ISD::ZERO_EXTEND;
2265
2266 LLVMContext &Context = F->getContext();
2267 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2268
2269 for (unsigned j = 0; j != NumValues; ++j) {
2270 EVT VT = TLI.getValueType(DL, Types[j]);
2271
2272 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2273 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2274
2275 CallingConv::ID CC = F->getCallingConv();
2276
2277 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2278 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2279 SmallVector<SDValue, 4> Parts(NumParts);
2281 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2282 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2283
2284 // 'inreg' on function refers to return value
2285 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2286 if (RetInReg)
2287 Flags.setInReg();
2288
2289 if (I.getOperand(0)->getType()->isPointerTy()) {
2290 Flags.setPointer();
2291 Flags.setPointerAddrSpace(
2292 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2293 }
2294
2295 if (NeedsRegBlock) {
2296 Flags.setInConsecutiveRegs();
2297 if (j == NumValues - 1)
2298 Flags.setInConsecutiveRegsLast();
2299 }
2300
2301 // Propagate extension type if any
2302 if (ExtendKind == ISD::SIGN_EXTEND)
2303 Flags.setSExt();
2304 else if (ExtendKind == ISD::ZERO_EXTEND)
2305 Flags.setZExt();
2306 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2307 Flags.setNoExt();
2308
2309 for (unsigned i = 0; i < NumParts; ++i) {
2310 Outs.push_back(ISD::OutputArg(Flags,
2311 Parts[i].getValueType().getSimpleVT(),
2312 VT, Types[j], 0, 0));
2313 OutVals.push_back(Parts[i]);
2314 }
2315 }
2316 }
2317 }
2318
2319 // Push in swifterror virtual register as the last element of Outs. This makes
2320 // sure swifterror virtual register will be returned in the swifterror
2321 // physical register.
2322 const Function *F = I.getParent()->getParent();
2323 if (TLI.supportSwiftError() &&
2324 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2325 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2326 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2327 Flags.setSwiftError();
2328 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2329 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2330 PointerType::getUnqual(*DAG.getContext()),
2331 /*origidx=*/1, /*partOffs=*/0));
2332 // Create SDNode for the swifterror virtual register.
2333 OutVals.push_back(
2334 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2335 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2336 EVT(TLI.getPointerTy(DL))));
2337 }
2338
2339 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2340 CallingConv::ID CallConv =
2341 DAG.getMachineFunction().getFunction().getCallingConv();
2342 Chain = DAG.getTargetLoweringInfo().LowerReturn(
2343 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2344
2345 // Verify that the target's LowerReturn behaved as expected.
2346 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2347 "LowerReturn didn't return a valid chain!");
2348
2349 // Update the DAG with the new chain value resulting from return lowering.
2350 DAG.setRoot(Chain);
2351}
2352
2353/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2354/// created for it, emit nodes to copy the value into the virtual
2355/// registers.
2357 // Skip empty types
2358 if (V->getType()->isEmptyTy())
2359 return;
2360
2362 if (VMI != FuncInfo.ValueMap.end()) {
2363 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2364 "Unused value assigned virtual registers!");
2365 CopyValueToVirtualRegister(V, VMI->second);
2366 }
2367}
2368
2369/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2370/// the current basic block, add it to ValueMap now so that we'll get a
2371/// CopyTo/FromReg.
2373 // No need to export constants.
2374 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2375
2376 // Already exported?
2377 if (FuncInfo.isExportedInst(V)) return;
2378
2379 Register Reg = FuncInfo.InitializeRegForValue(V);
2381}
2382
2384 const BasicBlock *FromBB) {
2385 // The operands of the setcc have to be in this block. We don't know
2386 // how to export them from some other block.
2387 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2388 // Can export from current BB.
2389 if (VI->getParent() == FromBB)
2390 return true;
2391
2392 // Is already exported, noop.
2393 return FuncInfo.isExportedInst(V);
2394 }
2395
2396 // If this is an argument, we can export it if the BB is the entry block or
2397 // if it is already exported.
2398 if (isa<Argument>(V)) {
2399 if (FromBB->isEntryBlock())
2400 return true;
2401
2402 // Otherwise, can only export this if it is already exported.
2403 return FuncInfo.isExportedInst(V);
2404 }
2405
2406 // Otherwise, constants can always be exported.
2407 return true;
2408}
2409
2410/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2412SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2413 const MachineBasicBlock *Dst) const {
2415 const BasicBlock *SrcBB = Src->getBasicBlock();
2416 const BasicBlock *DstBB = Dst->getBasicBlock();
2417 if (!BPI) {
2418 // If BPI is not available, set the default probability as 1 / N, where N is
2419 // the number of successors.
2420 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2421 return BranchProbability(1, SuccSize);
2422 }
2423 return BPI->getEdgeProbability(SrcBB, DstBB);
2424}
2425
2426void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2427 MachineBasicBlock *Dst,
2428 BranchProbability Prob) {
2429 if (!FuncInfo.BPI)
2430 Src->addSuccessorWithoutProb(Dst);
2431 else {
2432 if (Prob.isUnknown())
2433 Prob = getEdgeProbability(Src, Dst);
2434 Src->addSuccessor(Dst, Prob);
2435 }
2436}
2437
2438static bool InBlock(const Value *V, const BasicBlock *BB) {
2439 if (const Instruction *I = dyn_cast<Instruction>(V))
2440 return I->getParent() == BB;
2441 return true;
2442}
2443
2444/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2445/// This function emits a branch and is used at the leaves of an OR or an
2446/// AND operator tree.
2447void
2450 MachineBasicBlock *FBB,
2451 MachineBasicBlock *CurBB,
2452 MachineBasicBlock *SwitchBB,
2453 BranchProbability TProb,
2454 BranchProbability FProb,
2455 bool InvertCond) {
2456 const BasicBlock *BB = CurBB->getBasicBlock();
2457
2458 // If the leaf of the tree is a comparison, merge the condition into
2459 // the caseblock.
2460 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2461 // The operands of the cmp have to be in this block. We don't know
2462 // how to export them from some other block. If this is the first block
2463 // of the sequence, no exporting is needed.
2464 if (CurBB == SwitchBB ||
2465 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2466 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2467 ISD::CondCode Condition;
2468 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2469 ICmpInst::Predicate Pred =
2470 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2471 Condition = getICmpCondCode(Pred);
2472 } else {
2473 const FCmpInst *FC = cast<FCmpInst>(Cond);
2474 FCmpInst::Predicate Pred =
2475 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2476 Condition = getFCmpCondCode(Pred);
2477 if (FC->hasNoNaNs() ||
2478 (isKnownNeverNaN(FC->getOperand(0),
2479 SimplifyQuery(DAG.getDataLayout(), FC)) &&
2480 isKnownNeverNaN(FC->getOperand(1),
2481 SimplifyQuery(DAG.getDataLayout(), FC))))
2482 Condition = getFCmpCodeWithoutNaN(Condition);
2483 }
2484
2485 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2486 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2487 SL->SwitchCases.push_back(CB);
2488 return;
2489 }
2490 }
2491
2492 // Create a CaseBlock record representing this branch.
2493 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2494 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2495 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2496 SL->SwitchCases.push_back(CB);
2497}
2498
2499// Collect dependencies on V recursively. This is used for the cost analysis in
2500// `shouldKeepJumpConditionsTogether`.
2504 unsigned Depth = 0) {
2505 // Return false if we have an incomplete count.
2507 return false;
2508
2509 auto *I = dyn_cast<Instruction>(V);
2510 if (I == nullptr)
2511 return true;
2512
2513 if (Necessary != nullptr) {
2514 // This instruction is necessary for the other side of the condition so
2515 // don't count it.
2516 if (Necessary->contains(I))
2517 return true;
2518 }
2519
2520 // Already added this dep.
2521 if (!Deps->try_emplace(I, false).second)
2522 return true;
2523
2524 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2525 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2526 Depth + 1))
2527 return false;
2528 return true;
2529}
2530
2533 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2535 if (I.getNumSuccessors() != 2)
2536 return false;
2537
2538 if (!I.isConditional())
2539 return false;
2540
2541 if (Params.BaseCost < 0)
2542 return false;
2543
2544 // Baseline cost.
2545 InstructionCost CostThresh = Params.BaseCost;
2546
2547 BranchProbabilityInfo *BPI = nullptr;
2548 if (Params.LikelyBias || Params.UnlikelyBias)
2549 BPI = FuncInfo.BPI;
2550 if (BPI != nullptr) {
2551 // See if we are either likely to get an early out or compute both lhs/rhs
2552 // of the condition.
2553 BasicBlock *IfFalse = I.getSuccessor(0);
2554 BasicBlock *IfTrue = I.getSuccessor(1);
2555
2556 std::optional<bool> Likely;
2557 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2558 Likely = true;
2559 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2560 Likely = false;
2561
2562 if (Likely) {
2563 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2564 // Its likely we will have to compute both lhs and rhs of condition
2565 CostThresh += Params.LikelyBias;
2566 else {
2567 if (Params.UnlikelyBias < 0)
2568 return false;
2569 // Its likely we will get an early out.
2570 CostThresh -= Params.UnlikelyBias;
2571 }
2572 }
2573 }
2574
2575 if (CostThresh <= 0)
2576 return false;
2577
2578 // Collect "all" instructions that lhs condition is dependent on.
2579 // Use map for stable iteration (to avoid non-determanism of iteration of
2580 // SmallPtrSet). The `bool` value is just a dummy.
2582 collectInstructionDeps(&LhsDeps, Lhs);
2583 // Collect "all" instructions that rhs condition is dependent on AND are
2584 // dependencies of lhs. This gives us an estimate on which instructions we
2585 // stand to save by splitting the condition.
2586 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2587 return false;
2588 // Add the compare instruction itself unless its a dependency on the LHS.
2589 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2590 if (!LhsDeps.contains(RhsI))
2591 RhsDeps.try_emplace(RhsI, false);
2592
2593 InstructionCost CostOfIncluding = 0;
2594 // See if this instruction will need to computed independently of whether RHS
2595 // is.
2596 Value *BrCond = I.getCondition();
2597 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2598 for (const auto *U : Ins->users()) {
2599 // If user is independent of RHS calculation we don't need to count it.
2600 if (auto *UIns = dyn_cast<Instruction>(U))
2601 if (UIns != BrCond && !RhsDeps.contains(UIns))
2602 return false;
2603 }
2604 return true;
2605 };
2606
2607 // Prune instructions from RHS Deps that are dependencies of unrelated
2608 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2609 // arbitrary and just meant to cap the how much time we spend in the pruning
2610 // loop. Its highly unlikely to come into affect.
2611 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2612 // Stop after a certain point. No incorrectness from including too many
2613 // instructions.
2614 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2615 const Instruction *ToDrop = nullptr;
2616 for (const auto &InsPair : RhsDeps) {
2617 if (!ShouldCountInsn(InsPair.first)) {
2618 ToDrop = InsPair.first;
2619 break;
2620 }
2621 }
2622 if (ToDrop == nullptr)
2623 break;
2624 RhsDeps.erase(ToDrop);
2625 }
2626
2627 for (const auto &InsPair : RhsDeps) {
2628 // Finally accumulate latency that we can only attribute to computing the
2629 // RHS condition. Use latency because we are essentially trying to calculate
2630 // the cost of the dependency chain.
2631 // Possible TODO: We could try to estimate ILP and make this more precise.
2632 CostOfIncluding += TTI->getInstructionCost(
2633 InsPair.first, TargetTransformInfo::TCK_Latency);
2634
2635 if (CostOfIncluding > CostThresh)
2636 return false;
2637 }
2638 return true;
2639}
2640
2643 MachineBasicBlock *FBB,
2644 MachineBasicBlock *CurBB,
2645 MachineBasicBlock *SwitchBB,
2647 BranchProbability TProb,
2648 BranchProbability FProb,
2649 bool InvertCond) {
2650 // Skip over not part of the tree and remember to invert op and operands at
2651 // next level.
2652 Value *NotCond;
2653 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2654 InBlock(NotCond, CurBB->getBasicBlock())) {
2655 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2656 !InvertCond);
2657 return;
2658 }
2659
2661 const Value *BOpOp0, *BOpOp1;
2662 // Compute the effective opcode for Cond, taking into account whether it needs
2663 // to be inverted, e.g.
2664 // and (not (or A, B)), C
2665 // gets lowered as
2666 // and (and (not A, not B), C)
2668 if (BOp) {
2669 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2670 ? Instruction::And
2671 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2672 ? Instruction::Or
2674 if (InvertCond) {
2675 if (BOpc == Instruction::And)
2676 BOpc = Instruction::Or;
2677 else if (BOpc == Instruction::Or)
2678 BOpc = Instruction::And;
2679 }
2680 }
2681
2682 // If this node is not part of the or/and tree, emit it as a branch.
2683 // Note that all nodes in the tree should have same opcode.
2684 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2685 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2686 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2687 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2688 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2689 TProb, FProb, InvertCond);
2690 return;
2691 }
2692
2693 // Create TmpBB after CurBB.
2694 MachineFunction::iterator BBI(CurBB);
2695 MachineFunction &MF = DAG.getMachineFunction();
2697 CurBB->getParent()->insert(++BBI, TmpBB);
2698
2699 if (Opc == Instruction::Or) {
2700 // Codegen X | Y as:
2701 // BB1:
2702 // jmp_if_X TBB
2703 // jmp TmpBB
2704 // TmpBB:
2705 // jmp_if_Y TBB
2706 // jmp FBB
2707 //
2708
2709 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2710 // The requirement is that
2711 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2712 // = TrueProb for original BB.
2713 // Assuming the original probabilities are A and B, one choice is to set
2714 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2715 // A/(1+B) and 2B/(1+B). This choice assumes that
2716 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2717 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2718 // TmpBB, but the math is more complicated.
2719
2720 auto NewTrueProb = TProb / 2;
2721 auto NewFalseProb = TProb / 2 + FProb;
2722 // Emit the LHS condition.
2723 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2724 NewFalseProb, InvertCond);
2725
2726 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2727 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2729 // Emit the RHS condition into TmpBB.
2730 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2731 Probs[1], InvertCond);
2732 } else {
2733 assert(Opc == Instruction::And && "Unknown merge op!");
2734 // Codegen X & Y as:
2735 // BB1:
2736 // jmp_if_X TmpBB
2737 // jmp FBB
2738 // TmpBB:
2739 // jmp_if_Y TBB
2740 // jmp FBB
2741 //
2742 // This requires creation of TmpBB after CurBB.
2743
2744 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2745 // The requirement is that
2746 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2747 // = FalseProb for original BB.
2748 // Assuming the original probabilities are A and B, one choice is to set
2749 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2750 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2751 // TrueProb for BB1 * FalseProb for TmpBB.
2752
2753 auto NewTrueProb = TProb + FProb / 2;
2754 auto NewFalseProb = FProb / 2;
2755 // Emit the LHS condition.
2756 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2757 NewFalseProb, InvertCond);
2758
2759 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2760 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2762 // Emit the RHS condition into TmpBB.
2763 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2764 Probs[1], InvertCond);
2765 }
2766}
2767
2768/// If the set of cases should be emitted as a series of branches, return true.
2769/// If we should emit this as a bunch of and/or'd together conditions, return
2770/// false.
2771bool
2772SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2773 if (Cases.size() != 2) return true;
2774
2775 // If this is two comparisons of the same values or'd or and'd together, they
2776 // will get folded into a single comparison, so don't emit two blocks.
2777 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2778 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2779 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2780 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2781 return false;
2782 }
2783
2784 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2785 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2786 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2787 Cases[0].CC == Cases[1].CC &&
2788 isa<Constant>(Cases[0].CmpRHS) &&
2789 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2790 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2791 return false;
2792 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2793 return false;
2794 }
2795
2796 return true;
2797}
2798
2799void SelectionDAGBuilder::visitBr(const BranchInst &I) {
2801
2802 // Update machine-CFG edges.
2803 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2804
2805 if (I.isUnconditional()) {
2806 // Update machine-CFG edges.
2807 BrMBB->addSuccessor(Succ0MBB);
2808
2809 // If this is not a fall-through branch or optimizations are switched off,
2810 // emit the branch.
2811 if (Succ0MBB != NextBlock(BrMBB) ||
2813 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2814 getControlRoot(), DAG.getBasicBlock(Succ0MBB));
2815 setValue(&I, Br);
2816 DAG.setRoot(Br);
2817 }
2818
2819 return;
2820 }
2821
2822 // If this condition is one of the special cases we handle, do special stuff
2823 // now.
2824 const Value *CondVal = I.getCondition();
2825 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2826
2827 // If this is a series of conditions that are or'd or and'd together, emit
2828 // this as a sequence of branches instead of setcc's with and/or operations.
2829 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2830 // unpredictable branches, and vector extracts because those jumps are likely
2831 // expensive for any target), this should improve performance.
2832 // For example, instead of something like:
2833 // cmp A, B
2834 // C = seteq
2835 // cmp D, E
2836 // F = setle
2837 // or C, F
2838 // jnz foo
2839 // Emit:
2840 // cmp A, B
2841 // je foo
2842 // cmp D, E
2843 // jle foo
2844 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2845 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2846 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2847 BOp->hasOneUse() && !IsUnpredictable) {
2848 Value *Vec;
2849 const Value *BOp0, *BOp1;
2851 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2852 Opcode = Instruction::And;
2853 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2854 Opcode = Instruction::Or;
2855
2856 if (Opcode &&
2857 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2858 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2860 FuncInfo, I, Opcode, BOp0, BOp1,
2861 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2862 Opcode, BOp0, BOp1))) {
2863 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2864 getEdgeProbability(BrMBB, Succ0MBB),
2865 getEdgeProbability(BrMBB, Succ1MBB),
2866 /*InvertCond=*/false);
2867 // If the compares in later blocks need to use values not currently
2868 // exported from this block, export them now. This block should always
2869 // be the first entry.
2870 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2871
2872 // Allow some cases to be rejected.
2873 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2874 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2875 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2876 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2877 }
2878
2879 // Emit the branch for this block.
2880 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2881 SL->SwitchCases.erase(SL->SwitchCases.begin());
2882 return;
2883 }
2884
2885 // Okay, we decided not to do this, remove any inserted MBB's and clear
2886 // SwitchCases.
2887 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2888 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2889
2890 SL->SwitchCases.clear();
2891 }
2892 }
2893
2894 // Create a CaseBlock record representing this branch.
2895 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2896 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2898 IsUnpredictable);
2899
2900 // Use visitSwitchCase to actually insert the fast branch sequence for this
2901 // cond branch.
2902 visitSwitchCase(CB, BrMBB);
2903}
2904
2905/// visitSwitchCase - Emits the necessary code to represent a single node in
2906/// the binary search tree resulting from lowering a switch instruction.
2908 MachineBasicBlock *SwitchBB) {
2909 SDValue Cond;
2910 SDValue CondLHS = getValue(CB.CmpLHS);
2911 SDLoc dl = CB.DL;
2912
2913 if (CB.CC == ISD::SETTRUE) {
2914 // Branch or fall through to TrueBB.
2915 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2916 SwitchBB->normalizeSuccProbs();
2917 if (CB.TrueBB != NextBlock(SwitchBB)) {
2918 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2919 DAG.getBasicBlock(CB.TrueBB)));
2920 }
2921 return;
2922 }
2923
2924 auto &TLI = DAG.getTargetLoweringInfo();
2925 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2926
2927 // Build the setcc now.
2928 if (!CB.CmpMHS) {
2929 // Fold "(X == true)" to X and "(X == false)" to !X to
2930 // handle common cases produced by branch lowering.
2931 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2932 CB.CC == ISD::SETEQ)
2933 Cond = CondLHS;
2934 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2935 CB.CC == ISD::SETEQ) {
2936 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2937 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2938 } else {
2939 SDValue CondRHS = getValue(CB.CmpRHS);
2940
2941 // If a pointer's DAG type is larger than its memory type then the DAG
2942 // values are zero-extended. This breaks signed comparisons so truncate
2943 // back to the underlying type before doing the compare.
2944 if (CondLHS.getValueType() != MemVT) {
2945 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2946 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2947 }
2948 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2949 }
2950 } else {
2951 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2952
2953 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2954 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2955
2956 SDValue CmpOp = getValue(CB.CmpMHS);
2957 EVT VT = CmpOp.getValueType();
2958
2959 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2960 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2961 ISD::SETLE);
2962 } else {
2963 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2964 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2965 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2966 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2967 }
2968 }
2969
2970 // Update successor info
2971 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2972 // TrueBB and FalseBB are always different unless the incoming IR is
2973 // degenerate. This only happens when running llc on weird IR.
2974 if (CB.TrueBB != CB.FalseBB)
2975 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2976 SwitchBB->normalizeSuccProbs();
2977
2978 // If the lhs block is the next block, invert the condition so that we can
2979 // fall through to the lhs instead of the rhs block.
2980 if (CB.TrueBB == NextBlock(SwitchBB)) {
2981 std::swap(CB.TrueBB, CB.FalseBB);
2982 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2983 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2984 }
2985
2986 SDNodeFlags Flags;
2988 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2989 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2990
2991 setValue(CurInst, BrCond);
2992
2993 // Insert the false branch. Do this even if it's a fall through branch,
2994 // this makes it easier to do DAG optimizations which require inverting
2995 // the branch condition.
2996 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2997 DAG.getBasicBlock(CB.FalseBB));
2998
2999 DAG.setRoot(BrCond);
3000}
3001
3002/// visitJumpTable - Emit JumpTable node in the current MBB
3004 // Emit the code for the jump table
3005 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3006 assert(JT.Reg && "Should lower JT Header first!");
3007 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
3008 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
3009 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
3010 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
3011 Index.getValue(1), Table, Index);
3012 DAG.setRoot(BrJumpTable);
3013}
3014
3015/// visitJumpTableHeader - This function emits necessary code to produce index
3016/// in the JumpTable from switch case.
3018 JumpTableHeader &JTH,
3019 MachineBasicBlock *SwitchBB) {
3020 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3021 const SDLoc &dl = *JT.SL;
3022
3023 // Subtract the lowest switch case value from the value being switched on.
3024 SDValue SwitchOp = getValue(JTH.SValue);
3025 EVT VT = SwitchOp.getValueType();
3026 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
3027 DAG.getConstant(JTH.First, dl, VT));
3028
3029 // The SDNode we just created, which holds the value being switched on minus
3030 // the smallest case value, needs to be copied to a virtual register so it
3031 // can be used as an index into the jump table in a subsequent basic block.
3032 // This value may be smaller or larger than the target's pointer type, and
3033 // therefore require extension or truncating.
3034 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3035 SwitchOp =
3036 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
3037
3038 Register JumpTableReg =
3039 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
3040 SDValue CopyTo =
3041 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3042 JT.Reg = JumpTableReg;
3043
3044 if (!JTH.FallthroughUnreachable) {
3045 // Emit the range check for the jump table, and branch to the default block
3046 // for the switch statement if the value being switched on exceeds the
3047 // largest case in the switch.
3048 SDValue CMP = DAG.getSetCC(
3049 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3050 Sub.getValueType()),
3051 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3052
3053 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3054 MVT::Other, CopyTo, CMP,
3055 DAG.getBasicBlock(JT.Default));
3056
3057 // Avoid emitting unnecessary branches to the next block.
3058 if (JT.MBB != NextBlock(SwitchBB))
3059 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3060 DAG.getBasicBlock(JT.MBB));
3061
3062 DAG.setRoot(BrCond);
3063 } else {
3064 // Avoid emitting unnecessary branches to the next block.
3065 if (JT.MBB != NextBlock(SwitchBB))
3066 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3067 DAG.getBasicBlock(JT.MBB)));
3068 else
3069 DAG.setRoot(CopyTo);
3070 }
3071}
3072
3073/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3074/// variable if there exists one.
3076 SDValue &Chain) {
3077 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3078 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3079 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3081 Value *Global =
3084 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3085 if (Global) {
3086 MachinePointerInfo MPInfo(Global);
3090 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3091 DAG.setNodeMemRefs(Node, {MemRef});
3092 }
3093 if (PtrTy != PtrMemTy)
3094 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3095 return SDValue(Node, 0);
3096}
3097
3098/// Codegen a new tail for a stack protector check ParentMBB which has had its
3099/// tail spliced into a stack protector check success bb.
3100///
3101/// For a high level explanation of how this fits into the stack protector
3102/// generation see the comment on the declaration of class
3103/// StackProtectorDescriptor.
3105 MachineBasicBlock *ParentBB) {
3106
3107 // First create the loads to the guard/stack slot for the comparison.
3108 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3109 auto &DL = DAG.getDataLayout();
3110 EVT PtrTy = TLI.getFrameIndexTy(DL);
3111 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3112
3113 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3114 int FI = MFI.getStackProtectorIndex();
3115
3116 SDValue Guard;
3117 SDLoc dl = getCurSDLoc();
3118 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3119 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3120 Align Align = DL.getPrefTypeAlign(
3121 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3122
3123 // Generate code to load the content of the guard slot.
3124 SDValue GuardVal = DAG.getLoad(
3125 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3126 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3128
3129 if (TLI.useStackGuardXorFP())
3130 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3131
3132 // If we're using function-based instrumentation, call the guard check
3133 // function
3135 // Get the guard check function from the target and verify it exists since
3136 // we're using function-based instrumentation
3137 const Function *GuardCheckFn =
3138 TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3139 assert(GuardCheckFn && "Guard check function is null");
3140
3141 // The target provides a guard check function to validate the guard value.
3142 // Generate a call to that function with the content of the guard slot as
3143 // argument.
3144 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3145 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3146
3148 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3149 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3150 Entry.IsInReg = true;
3151 Args.push_back(Entry);
3152
3155 .setChain(DAG.getEntryNode())
3156 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3157 getValue(GuardCheckFn), std::move(Args));
3158
3159 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3160 DAG.setRoot(Result.second);
3161 return;
3162 }
3163
3164 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3165 // Otherwise, emit a volatile load to retrieve the stack guard value.
3166 SDValue Chain = DAG.getEntryNode();
3167 if (TLI.useLoadStackGuardNode(M)) {
3168 Guard = getLoadStackGuard(DAG, dl, Chain);
3169 } else {
3170 if (const Value *IRGuard = TLI.getSDagStackGuard(M, DAG.getLibcalls())) {
3171 SDValue GuardPtr = getValue(IRGuard);
3172 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3173 MachinePointerInfo(IRGuard, 0), Align,
3175 } else {
3176 LLVMContext &Ctx = *DAG.getContext();
3177 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
3178 Guard = DAG.getPOISON(PtrMemTy);
3179 }
3180 }
3181
3182 // Perform the comparison via a getsetcc.
3183 SDValue Cmp = DAG.getSetCC(
3184 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3185 Guard, GuardVal, ISD::SETNE);
3186
3187 // If the guard/stackslot do not equal, branch to failure MBB.
3188 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
3189 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3190 // Otherwise branch to success MBB.
3191 SDValue Br = DAG.getNode(ISD::BR, dl,
3192 MVT::Other, BrCond,
3193 DAG.getBasicBlock(SPD.getSuccessMBB()));
3194
3195 DAG.setRoot(Br);
3196}
3197
3198/// Codegen the failure basic block for a stack protector check.
3199///
3200/// A failure stack protector machine basic block consists simply of a call to
3201/// __stack_chk_fail().
3202///
3203/// For a high level explanation of how this fits into the stack protector
3204/// generation see the comment on the declaration of class
3205/// StackProtectorDescriptor.
3208
3209 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3210 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3211 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3212 SDValue Chain;
3213
3214 // For -Oz builds with a guard check function, we use function-based
3215 // instrumentation. Otherwise, if we have a guard check function, we call it
3216 // in the failure block.
3217 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3218 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3219 // First create the loads to the guard/stack slot for the comparison.
3220 auto &DL = DAG.getDataLayout();
3221 EVT PtrTy = TLI.getFrameIndexTy(DL);
3222 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3223
3224 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3225 int FI = MFI.getStackProtectorIndex();
3226
3227 SDLoc dl = getCurSDLoc();
3228 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3229 Align Align = DL.getPrefTypeAlign(
3230 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3231
3232 // Generate code to load the content of the guard slot.
3233 SDValue GuardVal = DAG.getLoad(
3234 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3235 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3237
3238 if (TLI.useStackGuardXorFP())
3239 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3240
3241 // The target provides a guard check function to validate the guard value.
3242 // Generate a call to that function with the content of the guard slot as
3243 // argument.
3244 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3245 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3246
3248 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3249 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3250 Entry.IsInReg = true;
3251 Args.push_back(Entry);
3252
3255 .setChain(DAG.getEntryNode())
3256 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3257 getValue(GuardCheckFn), std::move(Args));
3258
3259 Chain = TLI.LowerCallTo(CLI).second;
3260 } else {
3262 CallOptions.setDiscardResult(true);
3263 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3264 {}, CallOptions, getCurSDLoc())
3265 .second;
3266 }
3267
3268 // Emit a trap instruction if we are required to do so.
3269 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3270 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3271 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3272
3273 DAG.setRoot(Chain);
3274}
3275
3276/// visitBitTestHeader - This function emits necessary code to produce value
3277/// suitable for "bit tests"
3279 MachineBasicBlock *SwitchBB) {
3280 SDLoc dl = getCurSDLoc();
3281
3282 // Subtract the minimum value.
3283 SDValue SwitchOp = getValue(B.SValue);
3284 EVT VT = SwitchOp.getValueType();
3285 SDValue RangeSub =
3286 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3287
3288 // Determine the type of the test operands.
3289 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3290 bool UsePtrType = false;
3291 if (!TLI.isTypeLegal(VT)) {
3292 UsePtrType = true;
3293 } else {
3294 for (const BitTestCase &Case : B.Cases)
3295 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3296 // Switch table case range are encoded into series of masks.
3297 // Just use pointer type, it's guaranteed to fit.
3298 UsePtrType = true;
3299 break;
3300 }
3301 }
3302 SDValue Sub = RangeSub;
3303 if (UsePtrType) {
3304 VT = TLI.getPointerTy(DAG.getDataLayout());
3305 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3306 }
3307
3308 B.RegVT = VT.getSimpleVT();
3309 B.Reg = FuncInfo.CreateReg(B.RegVT);
3310 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3311
3312 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3313
3314 if (!B.FallthroughUnreachable)
3315 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3316 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3317 SwitchBB->normalizeSuccProbs();
3318
3319 SDValue Root = CopyTo;
3320 if (!B.FallthroughUnreachable) {
3321 // Conditional branch to the default block.
3322 SDValue RangeCmp = DAG.getSetCC(dl,
3323 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3324 RangeSub.getValueType()),
3325 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3326 ISD::SETUGT);
3327
3328 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3329 DAG.getBasicBlock(B.Default));
3330 }
3331
3332 // Avoid emitting unnecessary branches to the next block.
3333 if (MBB != NextBlock(SwitchBB))
3334 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3335
3336 DAG.setRoot(Root);
3337}
3338
3339/// visitBitTestCase - this function produces one "bit test"
3341 MachineBasicBlock *NextMBB,
3342 BranchProbability BranchProbToNext,
3343 Register Reg, BitTestCase &B,
3344 MachineBasicBlock *SwitchBB) {
3345 SDLoc dl = getCurSDLoc();
3346 MVT VT = BB.RegVT;
3347 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3348 SDValue Cmp;
3349 unsigned PopCount = llvm::popcount(B.Mask);
3350 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3351 if (PopCount == 1) {
3352 // Testing for a single bit; just compare the shift count with what it
3353 // would need to be to shift a 1 bit in that position.
3354 Cmp = DAG.getSetCC(
3355 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3356 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3357 ISD::SETEQ);
3358 } else if (PopCount == BB.Range) {
3359 // There is only one zero bit in the range, test for it directly.
3360 Cmp = DAG.getSetCC(
3361 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3362 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3363 } else {
3364 // Make desired shift
3365 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3366 DAG.getConstant(1, dl, VT), ShiftOp);
3367
3368 // Emit bit tests and jumps
3369 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3370 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3371 Cmp = DAG.getSetCC(
3372 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3373 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3374 }
3375
3376 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3377 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3378 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3379 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3380 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3381 // one as they are relative probabilities (and thus work more like weights),
3382 // and hence we need to normalize them to let the sum of them become one.
3383 SwitchBB->normalizeSuccProbs();
3384
3385 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3386 MVT::Other, getControlRoot(),
3387 Cmp, DAG.getBasicBlock(B.TargetBB));
3388
3389 // Avoid emitting unnecessary branches to the next block.
3390 if (NextMBB != NextBlock(SwitchBB))
3391 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3392 DAG.getBasicBlock(NextMBB));
3393
3394 DAG.setRoot(BrAnd);
3395}
3396
3397void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3398 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3399
3400 // Retrieve successors. Look through artificial IR level blocks like
3401 // catchswitch for successors.
3402 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3403 const BasicBlock *EHPadBB = I.getSuccessor(1);
3404 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3405
3406 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3407 // have to do anything here to lower funclet bundles.
3408 failForInvalidBundles(I, "invokes",
3414
3415 const Value *Callee(I.getCalledOperand());
3416 const Function *Fn = dyn_cast<Function>(Callee);
3417 if (isa<InlineAsm>(Callee))
3418 visitInlineAsm(I, EHPadBB);
3419 else if (Fn && Fn->isIntrinsic()) {
3420 switch (Fn->getIntrinsicID()) {
3421 default:
3422 llvm_unreachable("Cannot invoke this intrinsic");
3423 case Intrinsic::donothing:
3424 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3425 case Intrinsic::seh_try_begin:
3426 case Intrinsic::seh_scope_begin:
3427 case Intrinsic::seh_try_end:
3428 case Intrinsic::seh_scope_end:
3429 if (EHPadMBB)
3430 // a block referenced by EH table
3431 // so dtor-funclet not removed by opts
3432 EHPadMBB->setMachineBlockAddressTaken();
3433 break;
3434 case Intrinsic::experimental_patchpoint_void:
3435 case Intrinsic::experimental_patchpoint:
3436 visitPatchpoint(I, EHPadBB);
3437 break;
3438 case Intrinsic::experimental_gc_statepoint:
3440 break;
3441 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3442 // but these intrinsics are special because they can be invoked, so we
3443 // manually lower it to a DAG node here.
3444 case Intrinsic::wasm_throw: {
3446 std::array<SDValue, 4> Ops = {
3447 getControlRoot(), // inchain for the terminator node
3448 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3450 getValue(I.getArgOperand(0)), // tag
3451 getValue(I.getArgOperand(1)) // thrown value
3452 };
3453 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3454 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3455 break;
3456 }
3457 case Intrinsic::wasm_rethrow: {
3458 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3459 std::array<SDValue, 2> Ops = {
3460 getControlRoot(), // inchain for the terminator node
3461 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3462 TLI.getPointerTy(DAG.getDataLayout()))};
3463 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3464 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3465 break;
3466 }
3467 }
3468 } else if (I.hasDeoptState()) {
3469 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3470 // Eventually we will support lowering the @llvm.experimental.deoptimize
3471 // intrinsic, and right now there are no plans to support other intrinsics
3472 // with deopt state.
3473 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3474 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3476 } else {
3477 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3478 }
3479
3480 // If the value of the invoke is used outside of its defining block, make it
3481 // available as a virtual register.
3482 // We already took care of the exported value for the statepoint instruction
3483 // during call to the LowerStatepoint.
3484 if (!isa<GCStatepointInst>(I)) {
3486 }
3487
3489 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3490 BranchProbability EHPadBBProb =
3491 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3493 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3494
3495 // Update successor info.
3496 addSuccessorWithProb(InvokeMBB, Return);
3497 for (auto &UnwindDest : UnwindDests) {
3498 UnwindDest.first->setIsEHPad();
3499 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3500 }
3501 InvokeMBB->normalizeSuccProbs();
3502
3503 // Drop into normal successor.
3504 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3505 DAG.getBasicBlock(Return)));
3506}
3507
3508/// The intrinsics currently supported by callbr are implicit control flow
3509/// intrinsics such as amdgcn.kill.
3510/// - they should be called (no "dontcall-" attributes)
3511/// - they do not touch memory on the target (= !TLI.getTgtMemIntrinsic())
3512/// - they do not need custom argument handling (no
3513/// TLI.CollectTargetIntrinsicOperands())
3514void SelectionDAGBuilder::visitCallBrIntrinsic(const CallBrInst &I) {
3515#ifndef NDEBUG
3517 DAG.getTargetLoweringInfo().getTgtMemIntrinsic(
3518 Infos, I, DAG.getMachineFunction(), I.getIntrinsicID());
3519 assert(Infos.empty() && "Intrinsic touches memory");
3520#endif
3521
3522 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
3523
3525 getTargetIntrinsicOperands(I, HasChain, OnlyLoad);
3526 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
3527
3528 // Create the node.
3529 SDValue Result =
3530 getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
3531 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
3532
3533 setValue(&I, Result);
3534}
3535
3536void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3537 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3538
3539 if (I.isInlineAsm()) {
3540 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3541 // have to do anything here to lower funclet bundles.
3542 failForInvalidBundles(I, "callbrs",
3544 visitInlineAsm(I);
3545 } else {
3546 assert(!I.hasOperandBundles() &&
3547 "Can't have operand bundles for intrinsics");
3548 visitCallBrIntrinsic(I);
3549 }
3551
3552 // Retrieve successors.
3553 SmallPtrSet<BasicBlock *, 8> Dests;
3554 Dests.insert(I.getDefaultDest());
3555 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3556
3557 // Update successor info.
3558 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3559 // TODO: For most of the cases where there is an intrinsic callbr, we're
3560 // having exactly one indirect target, which will be unreachable. As soon as
3561 // this changes, we might need to enhance
3562 // Target->setIsInlineAsmBrIndirectTarget or add something similar for
3563 // intrinsic indirect branches.
3564 if (I.isInlineAsm()) {
3565 for (BasicBlock *Dest : I.getIndirectDests()) {
3566 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
3567 Target->setIsInlineAsmBrIndirectTarget();
3568 // If we introduce a type of asm goto statement that is permitted to use
3569 // an indirect call instruction to jump to its labels, then we should add
3570 // a call to Target->setMachineBlockAddressTaken() here, to mark the
3571 // target block as requiring a BTI.
3572
3573 Target->setLabelMustBeEmitted();
3574 // Don't add duplicate machine successors.
3575 if (Dests.insert(Dest).second)
3576 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3577 }
3578 }
3579 CallBrMBB->normalizeSuccProbs();
3580
3581 // Drop into default successor.
3582 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3583 MVT::Other, getControlRoot(),
3584 DAG.getBasicBlock(Return)));
3585}
3586
3587void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3588 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3589}
3590
3591void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3592 assert(FuncInfo.MBB->isEHPad() &&
3593 "Call to landingpad not in landing pad!");
3594
3595 // If there aren't registers to copy the values into (e.g., during SjLj
3596 // exceptions), then don't bother to create these DAG nodes.
3597 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3598 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3599 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3600 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3601 return;
3602
3603 // If landingpad's return type is token type, we don't create DAG nodes
3604 // for its exception pointer and selector value. The extraction of exception
3605 // pointer or selector value from token type landingpads is not currently
3606 // supported.
3607 if (LP.getType()->isTokenTy())
3608 return;
3609
3610 SmallVector<EVT, 2> ValueVTs;
3611 SDLoc dl = getCurSDLoc();
3612 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3613 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3614
3615 // Get the two live-in registers as SDValues. The physregs have already been
3616 // copied into virtual registers.
3617 SDValue Ops[2];
3618 if (FuncInfo.ExceptionPointerVirtReg) {
3619 Ops[0] = DAG.getZExtOrTrunc(
3620 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3621 FuncInfo.ExceptionPointerVirtReg,
3622 TLI.getPointerTy(DAG.getDataLayout())),
3623 dl, ValueVTs[0]);
3624 } else {
3625 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3626 }
3627 Ops[1] = DAG.getZExtOrTrunc(
3628 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3629 FuncInfo.ExceptionSelectorVirtReg,
3630 TLI.getPointerTy(DAG.getDataLayout())),
3631 dl, ValueVTs[1]);
3632
3633 // Merge into one.
3634 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3635 DAG.getVTList(ValueVTs), Ops);
3636 setValue(&LP, Res);
3637}
3638
3641 // Update JTCases.
3642 for (JumpTableBlock &JTB : SL->JTCases)
3643 if (JTB.first.HeaderBB == First)
3644 JTB.first.HeaderBB = Last;
3645
3646 // Update BitTestCases.
3647 for (BitTestBlock &BTB : SL->BitTestCases)
3648 if (BTB.Parent == First)
3649 BTB.Parent = Last;
3650}
3651
3652void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3653 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3654
3655 // Update machine-CFG edges with unique successors.
3657 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3658 BasicBlock *BB = I.getSuccessor(i);
3659 bool Inserted = Done.insert(BB).second;
3660 if (!Inserted)
3661 continue;
3662
3663 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3664 addSuccessorWithProb(IndirectBrMBB, Succ);
3665 }
3666 IndirectBrMBB->normalizeSuccProbs();
3667
3669 MVT::Other, getControlRoot(),
3670 getValue(I.getAddress())));
3671}
3672
3673void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3674 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3675 DAG.getTarget().Options.NoTrapAfterNoreturn))
3676 return;
3677
3678 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3679}
3680
3681void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3682 SDNodeFlags Flags;
3683 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3684 Flags.copyFMF(*FPOp);
3685
3686 SDValue Op = getValue(I.getOperand(0));
3687 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3688 Op, Flags);
3689 setValue(&I, UnNodeValue);
3690}
3691
3692void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3693 SDNodeFlags Flags;
3694 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3695 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3696 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3697 }
3698 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3699 Flags.setExact(ExactOp->isExact());
3700 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3701 Flags.setDisjoint(DisjointOp->isDisjoint());
3702 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3703 Flags.copyFMF(*FPOp);
3704
3705 SDValue Op1 = getValue(I.getOperand(0));
3706 SDValue Op2 = getValue(I.getOperand(1));
3707 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3708 Op1, Op2, Flags);
3709 setValue(&I, BinNodeValue);
3710}
3711
3712void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3713 SDValue Op1 = getValue(I.getOperand(0));
3714 SDValue Op2 = getValue(I.getOperand(1));
3715
3716 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3717 Op1.getValueType(), DAG.getDataLayout());
3718
3719 // Coerce the shift amount to the right type if we can. This exposes the
3720 // truncate or zext to optimization early.
3721 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3723 "Unexpected shift type");
3724 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3725 }
3726
3727 bool nuw = false;
3728 bool nsw = false;
3729 bool exact = false;
3730
3731 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3732
3733 if (const OverflowingBinaryOperator *OFBinOp =
3735 nuw = OFBinOp->hasNoUnsignedWrap();
3736 nsw = OFBinOp->hasNoSignedWrap();
3737 }
3738 if (const PossiblyExactOperator *ExactOp =
3740 exact = ExactOp->isExact();
3741 }
3742 SDNodeFlags Flags;
3743 Flags.setExact(exact);
3744 Flags.setNoSignedWrap(nsw);
3745 Flags.setNoUnsignedWrap(nuw);
3746 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3747 Flags);
3748 setValue(&I, Res);
3749}
3750
3751void SelectionDAGBuilder::visitSDiv(const User &I) {
3752 SDValue Op1 = getValue(I.getOperand(0));
3753 SDValue Op2 = getValue(I.getOperand(1));
3754
3755 SDNodeFlags Flags;
3756 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3757 cast<PossiblyExactOperator>(&I)->isExact());
3758 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3759 Op2, Flags));
3760}
3761
3762void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3763 ICmpInst::Predicate predicate = I.getPredicate();
3764 SDValue Op1 = getValue(I.getOperand(0));
3765 SDValue Op2 = getValue(I.getOperand(1));
3766 ISD::CondCode Opcode = getICmpCondCode(predicate);
3767
3768 auto &TLI = DAG.getTargetLoweringInfo();
3769 EVT MemVT =
3770 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3771
3772 // If a pointer's DAG type is larger than its memory type then the DAG values
3773 // are zero-extended. This breaks signed comparisons so truncate back to the
3774 // underlying type before doing the compare.
3775 if (Op1.getValueType() != MemVT) {
3776 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3777 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3778 }
3779
3780 SDNodeFlags Flags;
3781 Flags.setSameSign(I.hasSameSign());
3782 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3783
3784 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3785 I.getType());
3786 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
3787}
3788
3789void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3790 FCmpInst::Predicate predicate = I.getPredicate();
3791 SDValue Op1 = getValue(I.getOperand(0));
3792 SDValue Op2 = getValue(I.getOperand(1));
3793
3794 ISD::CondCode Condition = getFCmpCondCode(predicate);
3795 auto *FPMO = cast<FPMathOperator>(&I);
3796 if (FPMO->hasNoNaNs() ||
3797 (DAG.isKnownNeverNaN(Op1) && DAG.isKnownNeverNaN(Op2)))
3798 Condition = getFCmpCodeWithoutNaN(Condition);
3799
3800 SDNodeFlags Flags;
3801 Flags.copyFMF(*FPMO);
3802 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3803
3804 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3805 I.getType());
3806 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition,
3807 /*Chian=*/{}, /*IsSignaling=*/false, Flags));
3808}
3809
3810// Check if the condition of the select has one use or two users that are both
3811// selects with the same condition.
3812static bool hasOnlySelectUsers(const Value *Cond) {
3813 return llvm::all_of(Cond->users(), [](const Value *V) {
3814 return isa<SelectInst>(V);
3815 });
3816}
3817
3818void SelectionDAGBuilder::visitSelect(const User &I) {
3819 SmallVector<EVT, 4> ValueVTs;
3820 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3821 ValueVTs);
3822 unsigned NumValues = ValueVTs.size();
3823 if (NumValues == 0) return;
3824
3825 SmallVector<SDValue, 4> Values(NumValues);
3826 SDValue Cond = getValue(I.getOperand(0));
3827 SDValue LHSVal = getValue(I.getOperand(1));
3828 SDValue RHSVal = getValue(I.getOperand(2));
3829 SmallVector<SDValue, 1> BaseOps(1, Cond);
3831 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3832
3833 bool IsUnaryAbs = false;
3834 bool Negate = false;
3835
3836 SDNodeFlags Flags;
3837 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3838 Flags.copyFMF(*FPOp);
3839
3840 Flags.setUnpredictable(
3841 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3842
3843 // Min/max matching is only viable if all output VTs are the same.
3844 if (all_equal(ValueVTs)) {
3845 EVT VT = ValueVTs[0];
3846 LLVMContext &Ctx = *DAG.getContext();
3847 auto &TLI = DAG.getTargetLoweringInfo();
3848
3849 // We care about the legality of the operation after it has been type
3850 // legalized.
3851 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3852 VT = TLI.getTypeToTransformTo(Ctx, VT);
3853
3854 // If the vselect is legal, assume we want to leave this as a vector setcc +
3855 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3856 // min/max is legal on the scalar type.
3857 bool UseScalarMinMax = VT.isVector() &&
3859
3860 // ValueTracking's select pattern matching does not account for -0.0,
3861 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3862 // -0.0 is less than +0.0.
3863 const Value *LHS, *RHS;
3864 auto SPR = matchSelectPattern(&I, LHS, RHS);
3866 switch (SPR.Flavor) {
3867 case SPF_UMAX: Opc = ISD::UMAX; break;
3868 case SPF_UMIN: Opc = ISD::UMIN; break;
3869 case SPF_SMAX: Opc = ISD::SMAX; break;
3870 case SPF_SMIN: Opc = ISD::SMIN; break;
3871 case SPF_FMINNUM:
3873 break;
3874
3875 switch (SPR.NaNBehavior) {
3876 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3877 case SPNB_RETURNS_NAN: break;
3878 case SPNB_RETURNS_OTHER:
3880 Flags.setNoSignedZeros(true);
3881 break;
3882 case SPNB_RETURNS_ANY:
3884 (UseScalarMinMax &&
3886 Opc = ISD::FMINNUM;
3887 break;
3888 }
3889 break;
3890 case SPF_FMAXNUM:
3892 break;
3893
3894 switch (SPR.NaNBehavior) {
3895 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3896 case SPNB_RETURNS_NAN: break;
3897 case SPNB_RETURNS_OTHER:
3899 Flags.setNoSignedZeros(true);
3900 break;
3901 case SPNB_RETURNS_ANY:
3903 (UseScalarMinMax &&
3905 Opc = ISD::FMAXNUM;
3906 break;
3907 }
3908 break;
3909 case SPF_NABS:
3910 Negate = true;
3911 [[fallthrough]];
3912 case SPF_ABS:
3913 IsUnaryAbs = true;
3914 Opc = ISD::ABS;
3915 break;
3916 default: break;
3917 }
3918
3919 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3920 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3921 (UseScalarMinMax &&
3923 // If the underlying comparison instruction is used by any other
3924 // instruction, the consumed instructions won't be destroyed, so it is
3925 // not profitable to convert to a min/max.
3927 OpCode = Opc;
3928 LHSVal = getValue(LHS);
3929 RHSVal = getValue(RHS);
3930 BaseOps.clear();
3931 }
3932
3933 if (IsUnaryAbs) {
3934 OpCode = Opc;
3935 LHSVal = getValue(LHS);
3936 BaseOps.clear();
3937 }
3938 }
3939
3940 if (IsUnaryAbs) {
3941 for (unsigned i = 0; i != NumValues; ++i) {
3942 SDLoc dl = getCurSDLoc();
3943 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3944 Values[i] =
3945 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3946 if (Negate)
3947 Values[i] = DAG.getNegative(Values[i], dl, VT);
3948 }
3949 } else {
3950 for (unsigned i = 0; i != NumValues; ++i) {
3951 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3952 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3953 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3954 Values[i] = DAG.getNode(
3955 OpCode, getCurSDLoc(),
3956 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3957 }
3958 }
3959
3961 DAG.getVTList(ValueVTs), Values));
3962}
3963
3964void SelectionDAGBuilder::visitTrunc(const User &I) {
3965 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3966 SDValue N = getValue(I.getOperand(0));
3967 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3968 I.getType());
3969 SDNodeFlags Flags;
3970 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3971 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
3972 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
3973 }
3974
3975 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
3976}
3977
3978void SelectionDAGBuilder::visitZExt(const User &I) {
3979 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3980 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3981 SDValue N = getValue(I.getOperand(0));
3982 auto &TLI = DAG.getTargetLoweringInfo();
3983 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3984
3985 SDNodeFlags Flags;
3986 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3987 Flags.setNonNeg(PNI->hasNonNeg());
3988
3989 // Eagerly use nonneg information to canonicalize towards sign_extend if
3990 // that is the target's preference.
3991 // TODO: Let the target do this later.
3992 if (Flags.hasNonNeg() &&
3993 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3994 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3995 return;
3996 }
3997
3998 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3999}
4000
4001void SelectionDAGBuilder::visitSExt(const User &I) {
4002 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
4003 // SExt also can't be a cast to bool for same reason. So, nothing much to do
4004 SDValue N = getValue(I.getOperand(0));
4005 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4006 I.getType());
4007 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
4008}
4009
4010void SelectionDAGBuilder::visitFPTrunc(const User &I) {
4011 // FPTrunc is never a no-op cast, no need to check
4012 SDValue N = getValue(I.getOperand(0));
4013 SDLoc dl = getCurSDLoc();
4014 SDNodeFlags Flags;
4015 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4016 Flags.copyFMF(*FPOp);
4017 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4018 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4019 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
4020 DAG.getTargetConstant(
4021 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
4022 Flags));
4023}
4024
4025void SelectionDAGBuilder::visitFPExt(const User &I) {
4026 // FPExt is never a no-op cast, no need to check
4027 SDValue N = getValue(I.getOperand(0));
4028 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4029 I.getType());
4030 SDNodeFlags Flags;
4031 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4032 Flags.copyFMF(*FPOp);
4033 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N, Flags));
4034}
4035
4036void SelectionDAGBuilder::visitFPToUI(const User &I) {
4037 // FPToUI is never a no-op cast, no need to check
4038 SDValue N = getValue(I.getOperand(0));
4039 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4040 I.getType());
4041 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
4042}
4043
4044void SelectionDAGBuilder::visitFPToSI(const User &I) {
4045 // FPToSI is never a no-op cast, no need to check
4046 SDValue N = getValue(I.getOperand(0));
4047 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4048 I.getType());
4049 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
4050}
4051
4052void SelectionDAGBuilder::visitUIToFP(const User &I) {
4053 // UIToFP is never a no-op cast, no need to check
4054 SDValue N = getValue(I.getOperand(0));
4055 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4056 I.getType());
4057 SDNodeFlags Flags;
4058 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
4059 Flags.setNonNeg(PNI->hasNonNeg());
4060
4061 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4062}
4063
4064void SelectionDAGBuilder::visitSIToFP(const User &I) {
4065 // SIToFP is never a no-op cast, no need to check
4066 SDValue N = getValue(I.getOperand(0));
4067 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4068 I.getType());
4069 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
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 Values[i] = L;
4789 }
4790
4791 if (!ConstantMemory) {
4792 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4793 ArrayRef(Chains.data(), ChainI));
4794 if (isVolatile)
4795 DAG.setRoot(Chain);
4796 else
4797 PendingLoads.push_back(Chain);
4798 }
4799
4800 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4801 DAG.getVTList(ValueVTs), Values));
4802}
4803
4804void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4805 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4806 "call visitStoreToSwiftError when backend supports swifterror");
4807
4808 SmallVector<EVT, 4> ValueVTs;
4809 SmallVector<uint64_t, 4> Offsets;
4810 const Value *SrcV = I.getOperand(0);
4811 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4812 SrcV->getType(), ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4813 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4814 "expect a single EVT for swifterror");
4815
4816 SDValue Src = getValue(SrcV);
4817 // Create a virtual register, then update the virtual register.
4818 Register VReg =
4819 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4820 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4821 // Chain can be getRoot or getControlRoot.
4822 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4823 SDValue(Src.getNode(), Src.getResNo()));
4824 DAG.setRoot(CopyNode);
4825}
4826
4827void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4828 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4829 "call visitLoadFromSwiftError when backend supports swifterror");
4830
4831 assert(!I.isVolatile() &&
4832 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4833 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4834 "Support volatile, non temporal, invariant for load_from_swift_error");
4835
4836 const Value *SV = I.getOperand(0);
4837 Type *Ty = I.getType();
4838 assert(
4839 (!BatchAA ||
4840 !BatchAA->pointsToConstantMemory(MemoryLocation(
4841 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4842 I.getAAMetadata()))) &&
4843 "load_from_swift_error should not be constant memory");
4844
4845 SmallVector<EVT, 4> ValueVTs;
4846 SmallVector<uint64_t, 4> Offsets;
4847 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4848 ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4849 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4850 "expect a single EVT for swifterror");
4851
4852 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4853 SDValue L = DAG.getCopyFromReg(
4854 getRoot(), getCurSDLoc(),
4855 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4856
4857 setValue(&I, L);
4858}
4859
4860void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4861 if (I.isAtomic())
4862 return visitAtomicStore(I);
4863
4864 const Value *SrcV = I.getOperand(0);
4865 const Value *PtrV = I.getOperand(1);
4866
4867 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4868 if (TLI.supportSwiftError()) {
4869 // Swifterror values can come from either a function parameter with
4870 // swifterror attribute or an alloca with swifterror attribute.
4871 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4872 if (Arg->hasSwiftErrorAttr())
4873 return visitStoreToSwiftError(I);
4874 }
4875
4876 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4877 if (Alloca->isSwiftError())
4878 return visitStoreToSwiftError(I);
4879 }
4880 }
4881
4882 SmallVector<EVT, 4> ValueVTs, MemVTs;
4884 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4885 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4886 unsigned NumValues = ValueVTs.size();
4887 if (NumValues == 0)
4888 return;
4889
4890 // Get the lowered operands. Note that we do this after
4891 // checking if NumResults is zero, because with zero results
4892 // the operands won't have values in the map.
4893 SDValue Src = getValue(SrcV);
4894 SDValue Ptr = getValue(PtrV);
4895
4896 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4897 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4898 SDLoc dl = getCurSDLoc();
4899 Align Alignment = I.getAlign();
4900 AAMDNodes AAInfo = I.getAAMetadata();
4901
4902 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4903
4904 unsigned ChainI = 0;
4905 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4906 // See visitLoad comments.
4907 if (ChainI == MaxParallelChains) {
4908 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4909 ArrayRef(Chains.data(), ChainI));
4910 Root = Chain;
4911 ChainI = 0;
4912 }
4913
4914 // TODO: MachinePointerInfo only supports a fixed length offset.
4915 MachinePointerInfo PtrInfo =
4916 !Offsets[i].isScalable() || Offsets[i].isZero()
4917 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4918 : MachinePointerInfo();
4919
4920 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4921 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4922 if (MemVTs[i] != ValueVTs[i])
4923 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4924 SDValue St =
4925 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4926 Chains[ChainI] = St;
4927 }
4928
4929 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4930 ArrayRef(Chains.data(), ChainI));
4931 setValue(&I, StoreNode);
4932 DAG.setRoot(StoreNode);
4933}
4934
4935void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4936 bool IsCompressing) {
4937 SDLoc sdl = getCurSDLoc();
4938
4939 Value *Src0Operand = I.getArgOperand(0);
4940 Value *PtrOperand = I.getArgOperand(1);
4941 Value *MaskOperand = I.getArgOperand(2);
4942 Align Alignment = I.getParamAlign(1).valueOrOne();
4943
4944 SDValue Ptr = getValue(PtrOperand);
4945 SDValue Src0 = getValue(Src0Operand);
4946 SDValue Mask = getValue(MaskOperand);
4947 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4948
4949 EVT VT = Src0.getValueType();
4950
4951 auto MMOFlags = MachineMemOperand::MOStore;
4952 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4954
4955 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4956 MachinePointerInfo(PtrOperand), MMOFlags,
4957 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4958
4959 const auto &TLI = DAG.getTargetLoweringInfo();
4960
4961 SDValue StoreNode =
4962 !IsCompressing && TTI->hasConditionalLoadStoreForType(
4963 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4964 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4965 Mask)
4966 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4967 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4968 IsCompressing);
4969 DAG.setRoot(StoreNode);
4970 setValue(&I, StoreNode);
4971}
4972
4973// Get a uniform base for the Gather/Scatter intrinsic.
4974// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4975// We try to represent it as a base pointer + vector of indices.
4976// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4977// The first operand of the GEP may be a single pointer or a vector of pointers
4978// Example:
4979// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4980// or
4981// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4982// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4983//
4984// When the first GEP operand is a single pointer - it is the uniform base we
4985// are looking for. If first operand of the GEP is a splat vector - we
4986// extract the splat value and use it as a uniform base.
4987// In all other cases the function returns 'false'.
4988static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4989 SDValue &Scale, SelectionDAGBuilder *SDB,
4990 const BasicBlock *CurBB, uint64_t ElemSize) {
4991 SelectionDAG& DAG = SDB->DAG;
4992 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4993 const DataLayout &DL = DAG.getDataLayout();
4994
4995 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4996
4997 // Handle splat constant pointer.
4998 if (auto *C = dyn_cast<Constant>(Ptr)) {
4999 C = C->getSplatValue();
5000 if (!C)
5001 return false;
5002
5003 Base = SDB->getValue(C);
5004
5005 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
5006 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
5007 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
5008 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
5009 return true;
5010 }
5011
5013 if (!GEP || GEP->getParent() != CurBB)
5014 return false;
5015
5016 if (GEP->getNumOperands() != 2)
5017 return false;
5018
5019 const Value *BasePtr = GEP->getPointerOperand();
5020 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
5021
5022 // Make sure the base is scalar and the index is a vector.
5023 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
5024 return false;
5025
5026 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
5027 if (ScaleVal.isScalable())
5028 return false;
5029
5030 // Target may not support the required addressing mode.
5031 if (ScaleVal != 1 &&
5032 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
5033 return false;
5034
5035 Base = SDB->getValue(BasePtr);
5036 Index = SDB->getValue(IndexVal);
5037
5038 Scale =
5039 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
5040 return true;
5041}
5042
5043void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
5044 SDLoc sdl = getCurSDLoc();
5045
5046 // llvm.masked.scatter.*(Src0, Ptrs, Mask)
5047 const Value *Ptr = I.getArgOperand(1);
5048 SDValue Src0 = getValue(I.getArgOperand(0));
5049 SDValue Mask = getValue(I.getArgOperand(2));
5050 EVT VT = Src0.getValueType();
5051 Align Alignment = I.getParamAlign(1).valueOrOne();
5052 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5053
5054 SDValue Base;
5055 SDValue Index;
5056 SDValue Scale;
5057 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5058 I.getParent(), VT.getScalarStoreSize());
5059
5060 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5061 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5062 MachinePointerInfo(AS), MachineMemOperand::MOStore,
5063 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
5064 if (!UniformBase) {
5065 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5066 Index = getValue(Ptr);
5067 Scale =
5068 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5069 }
5070
5071 EVT IdxVT = Index.getValueType();
5072 EVT EltTy = IdxVT.getVectorElementType();
5073 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5074 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5075 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5076 }
5077
5078 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
5079 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
5080 Ops, MMO, ISD::SIGNED_SCALED, false);
5081 DAG.setRoot(Scatter);
5082 setValue(&I, Scatter);
5083}
5084
5085void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5086 SDLoc sdl = getCurSDLoc();
5087
5088 Value *PtrOperand = I.getArgOperand(0);
5089 Value *MaskOperand = I.getArgOperand(1);
5090 Value *Src0Operand = I.getArgOperand(2);
5091 Align Alignment = I.getParamAlign(0).valueOrOne();
5092
5093 SDValue Ptr = getValue(PtrOperand);
5094 SDValue Src0 = getValue(Src0Operand);
5095 SDValue Mask = getValue(MaskOperand);
5096 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5097
5098 EVT VT = Src0.getValueType();
5099 AAMDNodes AAInfo = I.getAAMetadata();
5100 const MDNode *Ranges = getRangeMetadata(I);
5101
5102 // Do not serialize masked loads of constant memory with anything.
5103 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5104 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5105
5106 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5107
5108 auto MMOFlags = MachineMemOperand::MOLoad;
5109 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5111 if (I.hasMetadata(LLVMContext::MD_invariant_load))
5113
5114 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5115 MachinePointerInfo(PtrOperand), MMOFlags,
5116 VT.getStoreSize(), Alignment, AAInfo, Ranges);
5117
5118 const auto &TLI = DAG.getTargetLoweringInfo();
5119
5120 // The Load/Res may point to different values and both of them are output
5121 // variables.
5122 SDValue Load;
5123 SDValue Res;
5124 if (!IsExpanding &&
5125 TTI->hasConditionalLoadStoreForType(Src0Operand->getType(),
5126 /*IsStore=*/false))
5127 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5128 else
5129 Res = Load =
5130 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5131 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5132 if (AddToChain)
5133 PendingLoads.push_back(Load.getValue(1));
5134 setValue(&I, Res);
5135}
5136
5137void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5138 SDLoc sdl = getCurSDLoc();
5139
5140 // @llvm.masked.gather.*(Ptrs, Mask, Src0)
5141 const Value *Ptr = I.getArgOperand(0);
5142 SDValue Src0 = getValue(I.getArgOperand(2));
5143 SDValue Mask = getValue(I.getArgOperand(1));
5144
5145 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5146 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5147 Align Alignment = I.getParamAlign(0).valueOrOne();
5148
5149 const MDNode *Ranges = getRangeMetadata(I);
5150
5151 SDValue Root = DAG.getRoot();
5152 SDValue Base;
5153 SDValue Index;
5154 SDValue Scale;
5155 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5156 I.getParent(), VT.getScalarStoreSize());
5157 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5158 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5159 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5160 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5161 Ranges);
5162
5163 if (!UniformBase) {
5164 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5165 Index = getValue(Ptr);
5166 Scale =
5167 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5168 }
5169
5170 EVT IdxVT = Index.getValueType();
5171 EVT EltTy = IdxVT.getVectorElementType();
5172 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5173 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5174 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5175 }
5176
5177 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5178 SDValue Gather =
5179 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5181
5182 PendingLoads.push_back(Gather.getValue(1));
5183 setValue(&I, Gather);
5184}
5185
5186void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5187 SDLoc dl = getCurSDLoc();
5188 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5189 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5190 SyncScope::ID SSID = I.getSyncScopeID();
5191
5192 SDValue InChain = getRoot();
5193
5194 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5195 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5196
5197 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5198 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5199
5200 MachineFunction &MF = DAG.getMachineFunction();
5201 MachineMemOperand *MMO = MF.getMachineMemOperand(
5202 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5203 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5204 FailureOrdering);
5205
5207 dl, MemVT, VTs, InChain,
5208 getValue(I.getPointerOperand()),
5209 getValue(I.getCompareOperand()),
5210 getValue(I.getNewValOperand()), MMO);
5211
5212 SDValue OutChain = L.getValue(2);
5213
5214 setValue(&I, L);
5215 DAG.setRoot(OutChain);
5216}
5217
5218void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5219 SDLoc dl = getCurSDLoc();
5221 switch (I.getOperation()) {
5222 default: llvm_unreachable("Unknown atomicrmw operation");
5240 break;
5243 break;
5246 break;
5249 break;
5252 break;
5255 break;
5256 }
5257 AtomicOrdering Ordering = I.getOrdering();
5258 SyncScope::ID SSID = I.getSyncScopeID();
5259
5260 SDValue InChain = getRoot();
5261
5262 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5263 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5264 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5265
5266 MachineFunction &MF = DAG.getMachineFunction();
5267 MachineMemOperand *MMO = MF.getMachineMemOperand(
5268 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5269 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
5270
5271 SDValue L =
5272 DAG.getAtomic(NT, dl, MemVT, InChain,
5273 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5274 MMO);
5275
5276 SDValue OutChain = L.getValue(1);
5277
5278 setValue(&I, L);
5279 DAG.setRoot(OutChain);
5280}
5281
5282void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5283 SDLoc dl = getCurSDLoc();
5284 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5285 SDValue Ops[3];
5286 Ops[0] = getRoot();
5287 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5288 TLI.getFenceOperandTy(DAG.getDataLayout()));
5289 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5290 TLI.getFenceOperandTy(DAG.getDataLayout()));
5291 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5292 setValue(&I, N);
5293 DAG.setRoot(N);
5294}
5295
5296void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5297 SDLoc dl = getCurSDLoc();
5298 AtomicOrdering Order = I.getOrdering();
5299 SyncScope::ID SSID = I.getSyncScopeID();
5300
5301 SDValue InChain = getRoot();
5302
5303 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5304 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5305 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5306
5307 if (!TLI.supportsUnalignedAtomics() &&
5308 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5309 report_fatal_error("Cannot generate unaligned atomic load");
5310
5311 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5312
5313 const MDNode *Ranges = getRangeMetadata(I);
5314 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5315 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5316 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5317
5318 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5319
5320 SDValue Ptr = getValue(I.getPointerOperand());
5321 SDValue L =
5322 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5323
5324 SDValue OutChain = L.getValue(1);
5325 if (MemVT != VT)
5326 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5327
5328 setValue(&I, L);
5329 DAG.setRoot(OutChain);
5330}
5331
5332void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5333 SDLoc dl = getCurSDLoc();
5334
5335 AtomicOrdering Ordering = I.getOrdering();
5336 SyncScope::ID SSID = I.getSyncScopeID();
5337
5338 SDValue InChain = getRoot();
5339
5340 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5341 EVT MemVT =
5342 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5343
5344 if (!TLI.supportsUnalignedAtomics() &&
5345 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5346 report_fatal_error("Cannot generate unaligned atomic store");
5347
5348 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5349
5350 MachineFunction &MF = DAG.getMachineFunction();
5351 MachineMemOperand *MMO = MF.getMachineMemOperand(
5352 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5353 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5354
5355 SDValue Val = getValue(I.getValueOperand());
5356 if (Val.getValueType() != MemVT)
5357 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5358 SDValue Ptr = getValue(I.getPointerOperand());
5359
5360 SDValue OutChain =
5361 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5362
5363 setValue(&I, OutChain);
5364 DAG.setRoot(OutChain);
5365}
5366
5367/// Check if this intrinsic call depends on the chain (1st return value)
5368/// and if it only *loads* memory.
5369/// Ignore the callsite's attributes. A specific call site may be marked with
5370/// readnone, but the lowering code will expect the chain based on the
5371/// definition.
5372std::pair<bool, bool>
5373SelectionDAGBuilder::getTargetIntrinsicCallProperties(const CallBase &I) {
5374 const Function *F = I.getCalledFunction();
5375 bool HasChain = !F->doesNotAccessMemory();
5376 bool OnlyLoad =
5377 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5378
5379 return {HasChain, OnlyLoad};
5380}
5381
5382SmallVector<SDValue, 8> SelectionDAGBuilder::getTargetIntrinsicOperands(
5383 const CallBase &I, bool HasChain, bool OnlyLoad,
5384 TargetLowering::IntrinsicInfo *TgtMemIntrinsicInfo) {
5385 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5386
5387 // Build the operand list.
5389 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5390 if (OnlyLoad) {
5391 // We don't need to serialize loads against other loads.
5392 Ops.push_back(DAG.getRoot());
5393 } else {
5394 Ops.push_back(getRoot());
5395 }
5396 }
5397
5398 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5399 if (!TgtMemIntrinsicInfo || TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_VOID ||
5400 TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_W_CHAIN)
5401 Ops.push_back(DAG.getTargetConstant(I.getIntrinsicID(), getCurSDLoc(),
5402 TLI.getPointerTy(DAG.getDataLayout())));
5403
5404 // Add all operands of the call to the operand list.
5405 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5406 const Value *Arg = I.getArgOperand(i);
5407 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5408 Ops.push_back(getValue(Arg));
5409 continue;
5410 }
5411
5412 // Use TargetConstant instead of a regular constant for immarg.
5413 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5414 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5415 assert(CI->getBitWidth() <= 64 &&
5416 "large intrinsic immediates not handled");
5417 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5418 } else {
5419 Ops.push_back(
5420 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5421 }
5422 }
5423
5424 if (std::optional<OperandBundleUse> Bundle =
5425 I.getOperandBundle(LLVMContext::OB_deactivation_symbol)) {
5426 auto *Sym = Bundle->Inputs[0].get();
5427 SDValue SDSym = getValue(Sym);
5428 SDSym = DAG.getDeactivationSymbol(cast<GlobalValue>(Sym));
5429 Ops.push_back(SDSym);
5430 }
5431
5432 if (std::optional<OperandBundleUse> Bundle =
5433 I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5434 Value *Token = Bundle->Inputs[0].get();
5435 SDValue ConvControlToken = getValue(Token);
5436 assert(Ops.back().getValueType() != MVT::Glue &&
5437 "Did not expect another glue node here.");
5438 ConvControlToken =
5439 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5440 Ops.push_back(ConvControlToken);
5441 }
5442
5443 return Ops;
5444}
5445
5446SDVTList SelectionDAGBuilder::getTargetIntrinsicVTList(const CallBase &I,
5447 bool HasChain) {
5448 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5449
5450 SmallVector<EVT, 4> ValueVTs;
5451 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5452
5453 if (HasChain)
5454 ValueVTs.push_back(MVT::Other);
5455
5456 return DAG.getVTList(ValueVTs);
5457}
5458
5459/// Get an INTRINSIC node for a target intrinsic which does not touch memory.
5460SDValue SelectionDAGBuilder::getTargetNonMemIntrinsicNode(
5461 const Type &IntrinsicVT, bool HasChain, ArrayRef<SDValue> Ops,
5462 const SDVTList &VTs) {
5463 if (!HasChain)
5464 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5465 if (!IntrinsicVT.isVoidTy())
5466 return DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5467 return DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5468}
5469
5470/// Set root, convert return type if necessary and check alignment.
5471SDValue SelectionDAGBuilder::handleTargetIntrinsicRet(const CallBase &I,
5472 bool HasChain,
5473 bool OnlyLoad,
5474 SDValue Result) {
5475 if (HasChain) {
5476 SDValue Chain = Result.getValue(Result.getNode()->getNumValues() - 1);
5477 if (OnlyLoad)
5478 PendingLoads.push_back(Chain);
5479 else
5480 DAG.setRoot(Chain);
5481 }
5482
5483 if (I.getType()->isVoidTy())
5484 return Result;
5485
5486 if (MaybeAlign Alignment = I.getRetAlign(); InsertAssertAlign && Alignment) {
5487 // Insert `assertalign` node if there's an alignment.
5488 Result = DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5489 } else if (!isa<VectorType>(I.getType())) {
5490 Result = lowerRangeToAssertZExt(DAG, I, Result);
5491 }
5492
5493 return Result;
5494}
5495
5496/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5497/// node.
5498void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5499 unsigned Intrinsic) {
5500 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
5501
5502 // Infos is set by getTgtMemIntrinsic.
5504 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5505 TLI.getTgtMemIntrinsic(Infos, I, DAG.getMachineFunction(), Intrinsic);
5506 // Use the first (primary) info determines the node opcode.
5507 TargetLowering::IntrinsicInfo *Info = !Infos.empty() ? &Infos[0] : nullptr;
5508
5510 getTargetIntrinsicOperands(I, HasChain, OnlyLoad, Info);
5511 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
5512
5513 // Propagate fast-math-flags from IR to node(s).
5514 SDNodeFlags Flags;
5515 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5516 Flags.copyFMF(*FPMO);
5517 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5518
5519 // Create the node.
5521
5522 // In some cases, custom collection of operands from CallInst I may be needed.
5524 if (!Infos.empty()) {
5525 // This is target intrinsic that touches memory
5526 // Create MachineMemOperands for each memory access described by the target.
5527 MachineFunction &MF = DAG.getMachineFunction();
5529 for (const auto &Info : Infos) {
5530 // TODO: We currently just fallback to address space 0 if
5531 // getTgtMemIntrinsic didn't yield anything useful.
5532 MachinePointerInfo MPI;
5533 if (Info.ptrVal)
5534 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5535 else if (Info.fallbackAddressSpace)
5536 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5537 EVT MemVT = Info.memVT;
5538 LocationSize Size = LocationSize::precise(Info.size);
5539 if (Size.hasValue() && !Size.getValue())
5541 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5542 MachineMemOperand *MMO = MF.getMachineMemOperand(
5543 MPI, Info.flags, Size, Alignment, I.getAAMetadata(),
5544 /*Ranges=*/nullptr, Info.ssid, Info.order, Info.failureOrder);
5545 MMOs.push_back(MMO);
5546 }
5547
5548 Result = DAG.getMemIntrinsicNode(Info->opc, getCurSDLoc(), VTs, Ops,
5549 Info->memVT, MMOs);
5550 } else {
5551 Result = getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
5552 }
5553
5554 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
5555
5556 setValue(&I, Result);
5557}
5558
5559/// GetSignificand - Get the significand and build it into a floating-point
5560/// number with exponent of 1:
5561///
5562/// Op = (Op & 0x007fffff) | 0x3f800000;
5563///
5564/// where Op is the hexadecimal representation of floating point value.
5566 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5567 DAG.getConstant(0x007fffff, dl, MVT::i32));
5568 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5569 DAG.getConstant(0x3f800000, dl, MVT::i32));
5570 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5571}
5572
5573/// GetExponent - Get the exponent:
5574///
5575/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5576///
5577/// where Op is the hexadecimal representation of floating point value.
5579 const TargetLowering &TLI, const SDLoc &dl) {
5580 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5581 DAG.getConstant(0x7f800000, dl, MVT::i32));
5582 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5583 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5584 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5585 DAG.getConstant(127, dl, MVT::i32));
5586 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5587}
5588
5589/// getF32Constant - Get 32-bit floating point constant.
5590static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt,
5591 const SDLoc &dl) {
5592 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5593 MVT::f32);
5594}
5595
5597 SelectionDAG &DAG) {
5598 // TODO: What fast-math-flags should be set on the floating-point nodes?
5599
5600 // IntegerPartOfX = ((int32_t)(t0);
5601 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5602
5603 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5604 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5605 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5606
5607 // IntegerPartOfX <<= 23;
5608 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5609 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5610
5611 SDValue TwoToFractionalPartOfX;
5612 if (LimitFloatPrecision <= 6) {
5613 // For floating-point precision of 6:
5614 //
5615 // TwoToFractionalPartOfX =
5616 // 0.997535578f +
5617 // (0.735607626f + 0.252464424f * x) * x;
5618 //
5619 // error 0.0144103317, which is 6 bits
5620 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5621 getF32Constant(DAG, 0x3e814304, dl));
5622 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5623 getF32Constant(DAG, 0x3f3c50c8, dl));
5624 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5625 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5626 getF32Constant(DAG, 0x3f7f5e7e, dl));
5627 } else if (LimitFloatPrecision <= 12) {
5628 // For floating-point precision of 12:
5629 //
5630 // TwoToFractionalPartOfX =
5631 // 0.999892986f +
5632 // (0.696457318f +
5633 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5634 //
5635 // error 0.000107046256, which is 13 to 14 bits
5636 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5637 getF32Constant(DAG, 0x3da235e3, dl));
5638 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5639 getF32Constant(DAG, 0x3e65b8f3, dl));
5640 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5641 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5642 getF32Constant(DAG, 0x3f324b07, dl));
5643 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5644 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5645 getF32Constant(DAG, 0x3f7ff8fd, dl));
5646 } else { // LimitFloatPrecision <= 18
5647 // For floating-point precision of 18:
5648 //
5649 // TwoToFractionalPartOfX =
5650 // 0.999999982f +
5651 // (0.693148872f +
5652 // (0.240227044f +
5653 // (0.554906021e-1f +
5654 // (0.961591928e-2f +
5655 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5656 // error 2.47208000*10^(-7), which is better than 18 bits
5657 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5658 getF32Constant(DAG, 0x3924b03e, dl));
5659 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5660 getF32Constant(DAG, 0x3ab24b87, dl));
5661 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5662 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5663 getF32Constant(DAG, 0x3c1d8c17, dl));
5664 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5665 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5666 getF32Constant(DAG, 0x3d634a1d, dl));
5667 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5668 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5669 getF32Constant(DAG, 0x3e75fe14, dl));
5670 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5671 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5672 getF32Constant(DAG, 0x3f317234, dl));
5673 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5674 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5675 getF32Constant(DAG, 0x3f800000, dl));
5676 }
5677
5678 // Add the exponent into the result in integer domain.
5679 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5680 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5681 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5682}
5683
5684/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5685/// limited-precision mode.
5687 const TargetLowering &TLI, SDNodeFlags Flags) {
5688 if (Op.getValueType() == MVT::f32 &&
5690
5691 // Put the exponent in the right bit position for later addition to the
5692 // final result:
5693 //
5694 // t0 = Op * log2(e)
5695
5696 // TODO: What fast-math-flags should be set here?
5697 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5698 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5699 return getLimitedPrecisionExp2(t0, dl, DAG);
5700 }
5701
5702 // No special expansion.
5703 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5704}
5705
5706/// expandLog - Lower a log intrinsic. Handles the special sequences for
5707/// limited-precision mode.
5709 const TargetLowering &TLI, SDNodeFlags Flags) {
5710 // TODO: What fast-math-flags should be set on the floating-point nodes?
5711
5712 if (Op.getValueType() == MVT::f32 &&
5714 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5715
5716 // Scale the exponent by log(2).
5717 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5718 SDValue LogOfExponent =
5719 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5720 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5721
5722 // Get the significand and build it into a floating-point number with
5723 // exponent of 1.
5724 SDValue X = GetSignificand(DAG, Op1, dl);
5725
5726 SDValue LogOfMantissa;
5727 if (LimitFloatPrecision <= 6) {
5728 // For floating-point precision of 6:
5729 //
5730 // LogofMantissa =
5731 // -1.1609546f +
5732 // (1.4034025f - 0.23903021f * x) * x;
5733 //
5734 // error 0.0034276066, which is better than 8 bits
5735 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5736 getF32Constant(DAG, 0xbe74c456, dl));
5737 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5738 getF32Constant(DAG, 0x3fb3a2b1, dl));
5739 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5740 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5741 getF32Constant(DAG, 0x3f949a29, dl));
5742 } else if (LimitFloatPrecision <= 12) {
5743 // For floating-point precision of 12:
5744 //
5745 // LogOfMantissa =
5746 // -1.7417939f +
5747 // (2.8212026f +
5748 // (-1.4699568f +
5749 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5750 //
5751 // error 0.000061011436, which is 14 bits
5752 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5753 getF32Constant(DAG, 0xbd67b6d6, dl));
5754 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5755 getF32Constant(DAG, 0x3ee4f4b8, dl));
5756 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5757 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5758 getF32Constant(DAG, 0x3fbc278b, dl));
5759 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5760 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5761 getF32Constant(DAG, 0x40348e95, dl));
5762 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5763 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5764 getF32Constant(DAG, 0x3fdef31a, dl));
5765 } else { // LimitFloatPrecision <= 18
5766 // For floating-point precision of 18:
5767 //
5768 // LogOfMantissa =
5769 // -2.1072184f +
5770 // (4.2372794f +
5771 // (-3.7029485f +
5772 // (2.2781945f +
5773 // (-0.87823314f +
5774 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5775 //
5776 // error 0.0000023660568, which is better than 18 bits
5777 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5778 getF32Constant(DAG, 0xbc91e5ac, dl));
5779 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5780 getF32Constant(DAG, 0x3e4350aa, dl));
5781 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5782 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5783 getF32Constant(DAG, 0x3f60d3e3, dl));
5784 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5785 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5786 getF32Constant(DAG, 0x4011cdf0, dl));
5787 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5788 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5789 getF32Constant(DAG, 0x406cfd1c, dl));
5790 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5791 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5792 getF32Constant(DAG, 0x408797cb, dl));
5793 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5794 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5795 getF32Constant(DAG, 0x4006dcab, dl));
5796 }
5797
5798 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5799 }
5800
5801 // No special expansion.
5802 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5803}
5804
5805/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5806/// limited-precision mode.
5808 const TargetLowering &TLI, SDNodeFlags Flags) {
5809 // TODO: What fast-math-flags should be set on the floating-point nodes?
5810
5811 if (Op.getValueType() == MVT::f32 &&
5813 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5814
5815 // Get the exponent.
5816 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5817
5818 // Get the significand and build it into a floating-point number with
5819 // exponent of 1.
5820 SDValue X = GetSignificand(DAG, Op1, dl);
5821
5822 // Different possible minimax approximations of significand in
5823 // floating-point for various degrees of accuracy over [1,2].
5824 SDValue Log2ofMantissa;
5825 if (LimitFloatPrecision <= 6) {
5826 // For floating-point precision of 6:
5827 //
5828 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5829 //
5830 // error 0.0049451742, which is more than 7 bits
5831 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5832 getF32Constant(DAG, 0xbeb08fe0, dl));
5833 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5834 getF32Constant(DAG, 0x40019463, dl));
5835 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5836 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5837 getF32Constant(DAG, 0x3fd6633d, dl));
5838 } else if (LimitFloatPrecision <= 12) {
5839 // For floating-point precision of 12:
5840 //
5841 // Log2ofMantissa =
5842 // -2.51285454f +
5843 // (4.07009056f +
5844 // (-2.12067489f +
5845 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5846 //
5847 // error 0.0000876136000, which is better than 13 bits
5848 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5849 getF32Constant(DAG, 0xbda7262e, dl));
5850 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5851 getF32Constant(DAG, 0x3f25280b, dl));
5852 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5853 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5854 getF32Constant(DAG, 0x4007b923, dl));
5855 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5856 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5857 getF32Constant(DAG, 0x40823e2f, dl));
5858 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5859 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5860 getF32Constant(DAG, 0x4020d29c, dl));
5861 } else { // LimitFloatPrecision <= 18
5862 // For floating-point precision of 18:
5863 //
5864 // Log2ofMantissa =
5865 // -3.0400495f +
5866 // (6.1129976f +
5867 // (-5.3420409f +
5868 // (3.2865683f +
5869 // (-1.2669343f +
5870 // (0.27515199f -
5871 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5872 //
5873 // error 0.0000018516, which is better than 18 bits
5874 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5875 getF32Constant(DAG, 0xbcd2769e, dl));
5876 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5877 getF32Constant(DAG, 0x3e8ce0b9, dl));
5878 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5879 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5880 getF32Constant(DAG, 0x3fa22ae7, dl));
5881 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5882 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5883 getF32Constant(DAG, 0x40525723, dl));
5884 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5885 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5886 getF32Constant(DAG, 0x40aaf200, dl));
5887 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5888 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5889 getF32Constant(DAG, 0x40c39dad, dl));
5890 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5891 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5892 getF32Constant(DAG, 0x4042902c, dl));
5893 }
5894
5895 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5896 }
5897
5898 // No special expansion.
5899 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5900}
5901
5902/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5903/// limited-precision mode.
5905 const TargetLowering &TLI, SDNodeFlags Flags) {
5906 // TODO: What fast-math-flags should be set on the floating-point nodes?
5907
5908 if (Op.getValueType() == MVT::f32 &&
5910 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5911
5912 // Scale the exponent by log10(2) [0.30102999f].
5913 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5914 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5915 getF32Constant(DAG, 0x3e9a209a, dl));
5916
5917 // Get the significand and build it into a floating-point number with
5918 // exponent of 1.
5919 SDValue X = GetSignificand(DAG, Op1, dl);
5920
5921 SDValue Log10ofMantissa;
5922 if (LimitFloatPrecision <= 6) {
5923 // For floating-point precision of 6:
5924 //
5925 // Log10ofMantissa =
5926 // -0.50419619f +
5927 // (0.60948995f - 0.10380950f * x) * x;
5928 //
5929 // error 0.0014886165, which is 6 bits
5930 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5931 getF32Constant(DAG, 0xbdd49a13, dl));
5932 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5933 getF32Constant(DAG, 0x3f1c0789, dl));
5934 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5935 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5936 getF32Constant(DAG, 0x3f011300, dl));
5937 } else if (LimitFloatPrecision <= 12) {
5938 // For floating-point precision of 12:
5939 //
5940 // Log10ofMantissa =
5941 // -0.64831180f +
5942 // (0.91751397f +
5943 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5944 //
5945 // error 0.00019228036, which is better than 12 bits
5946 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5947 getF32Constant(DAG, 0x3d431f31, dl));
5948 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5949 getF32Constant(DAG, 0x3ea21fb2, dl));
5950 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5951 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5952 getF32Constant(DAG, 0x3f6ae232, dl));
5953 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5954 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5955 getF32Constant(DAG, 0x3f25f7c3, dl));
5956 } else { // LimitFloatPrecision <= 18
5957 // For floating-point precision of 18:
5958 //
5959 // Log10ofMantissa =
5960 // -0.84299375f +
5961 // (1.5327582f +
5962 // (-1.0688956f +
5963 // (0.49102474f +
5964 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5965 //
5966 // error 0.0000037995730, which is better than 18 bits
5967 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5968 getF32Constant(DAG, 0x3c5d51ce, dl));
5969 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5970 getF32Constant(DAG, 0x3e00685a, dl));
5971 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5972 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5973 getF32Constant(DAG, 0x3efb6798, dl));
5974 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5975 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5976 getF32Constant(DAG, 0x3f88d192, dl));
5977 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5978 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5979 getF32Constant(DAG, 0x3fc4316c, dl));
5980 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5981 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5982 getF32Constant(DAG, 0x3f57ce70, dl));
5983 }
5984
5985 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5986 }
5987
5988 // No special expansion.
5989 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5990}
5991
5992/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5993/// limited-precision mode.
5995 const TargetLowering &TLI, SDNodeFlags Flags) {
5996 if (Op.getValueType() == MVT::f32 &&
5998 return getLimitedPrecisionExp2(Op, dl, DAG);
5999
6000 // No special expansion.
6001 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
6002}
6003
6004/// visitPow - Lower a pow intrinsic. Handles the special sequences for
6005/// limited-precision mode with x == 10.0f.
6007 SelectionDAG &DAG, const TargetLowering &TLI,
6008 SDNodeFlags Flags) {
6009 bool IsExp10 = false;
6010 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
6013 APFloat Ten(10.0f);
6014 IsExp10 = LHSC->isExactlyValue(Ten);
6015 }
6016 }
6017
6018 // TODO: What fast-math-flags should be set on the FMUL node?
6019 if (IsExp10) {
6020 // Put the exponent in the right bit position for later addition to the
6021 // final result:
6022 //
6023 // #define LOG2OF10 3.3219281f
6024 // t0 = Op * LOG2OF10;
6025 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
6026 getF32Constant(DAG, 0x40549a78, dl));
6027 return getLimitedPrecisionExp2(t0, dl, DAG);
6028 }
6029
6030 // No special expansion.
6031 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
6032}
6033
6034/// ExpandPowI - Expand a llvm.powi intrinsic.
6036 SelectionDAG &DAG) {
6037 // If RHS is a constant, we can expand this out to a multiplication tree if
6038 // it's beneficial on the target, otherwise we end up lowering to a call to
6039 // __powidf2 (for example).
6041 unsigned Val = RHSC->getSExtValue();
6042
6043 // powi(x, 0) -> 1.0
6044 if (Val == 0)
6045 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
6046
6048 Val, DAG.shouldOptForSize())) {
6049 // Get the exponent as a positive value.
6050 if ((int)Val < 0)
6051 Val = -Val;
6052 // We use the simple binary decomposition method to generate the multiply
6053 // sequence. There are more optimal ways to do this (for example,
6054 // powi(x,15) generates one more multiply than it should), but this has
6055 // the benefit of being both really simple and much better than a libcall.
6056 SDValue Res; // Logically starts equal to 1.0
6057 SDValue CurSquare = LHS;
6058 // TODO: Intrinsics should have fast-math-flags that propagate to these
6059 // nodes.
6060 while (Val) {
6061 if (Val & 1) {
6062 if (Res.getNode())
6063 Res =
6064 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
6065 else
6066 Res = CurSquare; // 1.0*CurSquare.
6067 }
6068
6069 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
6070 CurSquare, CurSquare);
6071 Val >>= 1;
6072 }
6073
6074 // If the original was negative, invert the result, producing 1/(x*x*x).
6075 if (RHSC->getSExtValue() < 0)
6076 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
6077 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
6078 return Res;
6079 }
6080 }
6081
6082 // Otherwise, expand to a libcall.
6083 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
6084}
6085
6086static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
6087 SDValue LHS, SDValue RHS, SDValue Scale,
6088 SelectionDAG &DAG, const TargetLowering &TLI) {
6089 EVT VT = LHS.getValueType();
6090 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
6091 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
6092 LLVMContext &Ctx = *DAG.getContext();
6093
6094 // If the type is legal but the operation isn't, this node might survive all
6095 // the way to operation legalization. If we end up there and we do not have
6096 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
6097 // node.
6098
6099 // Coax the legalizer into expanding the node during type legalization instead
6100 // by bumping the size by one bit. This will force it to Promote, enabling the
6101 // early expansion and avoiding the need to expand later.
6102
6103 // We don't have to do this if Scale is 0; that can always be expanded, unless
6104 // it's a saturating signed operation. Those can experience true integer
6105 // division overflow, a case which we must avoid.
6106
6107 // FIXME: We wouldn't have to do this (or any of the early
6108 // expansion/promotion) if it was possible to expand a libcall of an
6109 // illegal type during operation legalization. But it's not, so things
6110 // get a bit hacky.
6111 unsigned ScaleInt = Scale->getAsZExtVal();
6112 if ((ScaleInt > 0 || (Saturating && Signed)) &&
6113 (TLI.isTypeLegal(VT) ||
6114 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6116 Opcode, VT, ScaleInt);
6117 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6118 EVT PromVT;
6119 if (VT.isScalarInteger())
6120 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6121 else if (VT.isVector()) {
6122 PromVT = VT.getVectorElementType();
6123 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6124 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6125 } else
6126 llvm_unreachable("Wrong VT for DIVFIX?");
6127 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6128 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6129 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6130 // For saturating operations, we need to shift up the LHS to get the
6131 // proper saturation width, and then shift down again afterwards.
6132 if (Saturating)
6133 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6134 DAG.getConstant(1, DL, ShiftTy));
6135 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6136 if (Saturating)
6137 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6138 DAG.getConstant(1, DL, ShiftTy));
6139 return DAG.getZExtOrTrunc(Res, DL, VT);
6140 }
6141 }
6142
6143 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6144}
6145
6146// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6147// bitcasted, or split argument. Returns a list of <Register, size in bits>
6148static void
6149getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6150 const SDValue &N) {
6151 switch (N.getOpcode()) {
6152 case ISD::CopyFromReg: {
6153 SDValue Op = N.getOperand(1);
6154 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6155 Op.getValueType().getSizeInBits());
6156 return;
6157 }
6158 case ISD::BITCAST:
6159 case ISD::AssertZext:
6160 case ISD::AssertSext:
6161 case ISD::TRUNCATE:
6162 getUnderlyingArgRegs(Regs, N.getOperand(0));
6163 return;
6164 case ISD::BUILD_PAIR:
6165 case ISD::BUILD_VECTOR:
6167 for (SDValue Op : N->op_values())
6168 getUnderlyingArgRegs(Regs, Op);
6169 return;
6170 default:
6171 return;
6172 }
6173}
6174
6175/// If the DbgValueInst is a dbg_value of a function argument, create the
6176/// corresponding DBG_VALUE machine instruction for it now. At the end of
6177/// instruction selection, they will be inserted to the entry BB.
6178/// We don't currently support this for variadic dbg_values, as they shouldn't
6179/// appear for function arguments or in the prologue.
6180bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6181 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6182 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6183 const Argument *Arg = dyn_cast<Argument>(V);
6184 if (!Arg)
6185 return false;
6186
6187 MachineFunction &MF = DAG.getMachineFunction();
6188 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6189
6190 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6191 // we've been asked to pursue.
6192 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6193 bool Indirect) {
6194 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6195 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6196 // pointing at the VReg, which will be patched up later.
6197 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6199 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6200 /* isKill */ false, /* isDead */ false,
6201 /* isUndef */ false, /* isEarlyClobber */ false,
6202 /* SubReg */ 0, /* isDebug */ true)});
6203
6204 auto *NewDIExpr = FragExpr;
6205 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6206 // the DIExpression.
6207 if (Indirect)
6208 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6210 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6211 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6212 } else {
6213 // Create a completely standard DBG_VALUE.
6214 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6215 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6216 }
6217 };
6218
6219 if (Kind == FuncArgumentDbgValueKind::Value) {
6220 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6221 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6222 // the entry block.
6223 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6224 if (!IsInEntryBlock)
6225 return false;
6226
6227 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6228 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6229 // variable that also is a param.
6230 //
6231 // Although, if we are at the top of the entry block already, we can still
6232 // emit using ArgDbgValue. This might catch some situations when the
6233 // dbg.value refers to an argument that isn't used in the entry block, so
6234 // any CopyToReg node would be optimized out and the only way to express
6235 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6236 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6237 // we should only emit as ArgDbgValue if the Variable is an argument to the
6238 // current function, and the dbg.value intrinsic is found in the entry
6239 // block.
6240 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6241 !DL->getInlinedAt();
6242 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6243 if (!IsInPrologue && !VariableIsFunctionInputArg)
6244 return false;
6245
6246 // Here we assume that a function argument on IR level only can be used to
6247 // describe one input parameter on source level. If we for example have
6248 // source code like this
6249 //
6250 // struct A { long x, y; };
6251 // void foo(struct A a, long b) {
6252 // ...
6253 // b = a.x;
6254 // ...
6255 // }
6256 //
6257 // and IR like this
6258 //
6259 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6260 // entry:
6261 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6262 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6263 // call void @llvm.dbg.value(metadata i32 %b, "b",
6264 // ...
6265 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6266 // ...
6267 //
6268 // then the last dbg.value is describing a parameter "b" using a value that
6269 // is an argument. But since we already has used %a1 to describe a parameter
6270 // we should not handle that last dbg.value here (that would result in an
6271 // incorrect hoisting of the DBG_VALUE to the function entry).
6272 // Notice that we allow one dbg.value per IR level argument, to accommodate
6273 // for the situation with fragments above.
6274 // If there is no node for the value being handled, we return true to skip
6275 // the normal generation of debug info, as it would kill existing debug
6276 // info for the parameter in case of duplicates.
6277 if (VariableIsFunctionInputArg) {
6278 unsigned ArgNo = Arg->getArgNo();
6279 if (ArgNo >= FuncInfo.DescribedArgs.size())
6280 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6281 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6282 return !NodeMap[V].getNode();
6283 FuncInfo.DescribedArgs.set(ArgNo);
6284 }
6285 }
6286
6287 bool IsIndirect = false;
6288 std::optional<MachineOperand> Op;
6289 // Some arguments' frame index is recorded during argument lowering.
6290 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6291 if (FI != std::numeric_limits<int>::max())
6293
6295 if (!Op && N.getNode()) {
6296 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6297 Register Reg;
6298 if (ArgRegsAndSizes.size() == 1)
6299 Reg = ArgRegsAndSizes.front().first;
6300
6301 if (Reg && Reg.isVirtual()) {
6302 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6303 Register PR = RegInfo.getLiveInPhysReg(Reg);
6304 if (PR)
6305 Reg = PR;
6306 }
6307 if (Reg) {
6309 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6310 }
6311 }
6312
6313 if (!Op && N.getNode()) {
6314 // Check if frame index is available.
6315 SDValue LCandidate = peekThroughBitcasts(N);
6316 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6317 if (FrameIndexSDNode *FINode =
6318 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6319 Op = MachineOperand::CreateFI(FINode->getIndex());
6320 }
6321
6322 if (!Op) {
6323 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6324 auto splitMultiRegDbgValue =
6325 [&](ArrayRef<std::pair<Register, TypeSize>> SplitRegs) -> bool {
6326 unsigned Offset = 0;
6327 for (const auto &[Reg, RegSizeInBits] : SplitRegs) {
6328 // FIXME: Scalable sizes are not supported in fragment expressions.
6329 if (RegSizeInBits.isScalable())
6330 return false;
6331
6332 // If the expression is already a fragment, the current register
6333 // offset+size might extend beyond the fragment. In this case, only
6334 // the register bits that are inside the fragment are relevant.
6335 int RegFragmentSizeInBits = RegSizeInBits.getFixedValue();
6336 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6337 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6338 // The register is entirely outside the expression fragment,
6339 // so is irrelevant for debug info.
6340 if (Offset >= ExprFragmentSizeInBits)
6341 break;
6342 // The register is partially outside the expression fragment, only
6343 // the low bits within the fragment are relevant for debug info.
6344 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6345 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6346 }
6347 }
6348
6349 auto FragmentExpr = DIExpression::createFragmentExpression(
6350 Expr, Offset, RegFragmentSizeInBits);
6351 Offset += RegSizeInBits.getFixedValue();
6352 // If a valid fragment expression cannot be created, the variable's
6353 // correct value cannot be determined and so it is set as poison.
6354 if (!FragmentExpr) {
6355 SDDbgValue *SDV = DAG.getConstantDbgValue(
6356 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6357 DAG.AddDbgValue(SDV, false);
6358 continue;
6359 }
6360 MachineInstr *NewMI = MakeVRegDbgValue(
6361 Reg, *FragmentExpr, Kind != FuncArgumentDbgValueKind::Value);
6362 FuncInfo.ArgDbgValues.push_back(NewMI);
6363 }
6364
6365 return true;
6366 };
6367
6368 // Check if ValueMap has reg number.
6370 VMI = FuncInfo.ValueMap.find(V);
6371 if (VMI != FuncInfo.ValueMap.end()) {
6372 const auto &TLI = DAG.getTargetLoweringInfo();
6373 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6374 V->getType(), std::nullopt);
6375 if (RFV.occupiesMultipleRegs())
6376 return splitMultiRegDbgValue(RFV.getRegsAndSizes());
6377
6378 Op = MachineOperand::CreateReg(VMI->second, false);
6379 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6380 } else if (ArgRegsAndSizes.size() > 1) {
6381 // This was split due to the calling convention, and no virtual register
6382 // mapping exists for the value.
6383 return splitMultiRegDbgValue(ArgRegsAndSizes);
6384 }
6385 }
6386
6387 if (!Op)
6388 return false;
6389
6390 assert(Variable->isValidLocationForIntrinsic(DL) &&
6391 "Expected inlined-at fields to agree");
6392 MachineInstr *NewMI = nullptr;
6393
6394 if (Op->isReg())
6395 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6396 else
6397 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6398 Variable, Expr);
6399
6400 // Otherwise, use ArgDbgValues.
6401 FuncInfo.ArgDbgValues.push_back(NewMI);
6402 return true;
6403}
6404
6405/// Return the appropriate SDDbgValue based on N.
6406SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6407 DILocalVariable *Variable,
6408 DIExpression *Expr,
6409 const DebugLoc &dl,
6410 unsigned DbgSDNodeOrder) {
6411 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6412 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6413 // stack slot locations.
6414 //
6415 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6416 // debug values here after optimization:
6417 //
6418 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6419 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6420 //
6421 // Both describe the direct values of their associated variables.
6422 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6423 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6424 }
6425 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6426 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6427}
6428
6429static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6430 switch (Intrinsic) {
6431 case Intrinsic::smul_fix:
6432 return ISD::SMULFIX;
6433 case Intrinsic::umul_fix:
6434 return ISD::UMULFIX;
6435 case Intrinsic::smul_fix_sat:
6436 return ISD::SMULFIXSAT;
6437 case Intrinsic::umul_fix_sat:
6438 return ISD::UMULFIXSAT;
6439 case Intrinsic::sdiv_fix:
6440 return ISD::SDIVFIX;
6441 case Intrinsic::udiv_fix:
6442 return ISD::UDIVFIX;
6443 case Intrinsic::sdiv_fix_sat:
6444 return ISD::SDIVFIXSAT;
6445 case Intrinsic::udiv_fix_sat:
6446 return ISD::UDIVFIXSAT;
6447 default:
6448 llvm_unreachable("Unhandled fixed point intrinsic");
6449 }
6450}
6451
6452/// Given a @llvm.call.preallocated.setup, return the corresponding
6453/// preallocated call.
6454static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6455 assert(cast<CallBase>(PreallocatedSetup)
6457 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6458 "expected call_preallocated_setup Value");
6459 for (const auto *U : PreallocatedSetup->users()) {
6460 auto *UseCall = cast<CallBase>(U);
6461 const Function *Fn = UseCall->getCalledFunction();
6462 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6463 return UseCall;
6464 }
6465 }
6466 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6467}
6468
6469/// If DI is a debug value with an EntryValue expression, lower it using the
6470/// corresponding physical register of the associated Argument value
6471/// (guaranteed to exist by the verifier).
6472bool SelectionDAGBuilder::visitEntryValueDbgValue(
6473 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6474 DIExpression *Expr, DebugLoc DbgLoc) {
6475 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6476 return false;
6477
6478 // These properties are guaranteed by the verifier.
6479 const Argument *Arg = cast<Argument>(Values[0]);
6480 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6481
6482 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6483 if (ArgIt == FuncInfo.ValueMap.end()) {
6484 LLVM_DEBUG(
6485 dbgs() << "Dropping dbg.value: expression is entry_value but "
6486 "couldn't find an associated register for the Argument\n");
6487 return true;
6488 }
6489 Register ArgVReg = ArgIt->getSecond();
6490
6491 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6492 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6493 SDDbgValue *SDV = DAG.getVRegDbgValue(
6494 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6495 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6496 return true;
6497 }
6498 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6499 "couldn't find a physical register\n");
6500 return true;
6501}
6502
6503/// Lower the call to the specified intrinsic function.
6504void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6505 unsigned Intrinsic) {
6506 SDLoc sdl = getCurSDLoc();
6507 switch (Intrinsic) {
6508 case Intrinsic::experimental_convergence_anchor:
6509 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6510 break;
6511 case Intrinsic::experimental_convergence_entry:
6512 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6513 break;
6514 case Intrinsic::experimental_convergence_loop: {
6515 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6516 auto *Token = Bundle->Inputs[0].get();
6517 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6518 getValue(Token)));
6519 break;
6520 }
6521 }
6522}
6523
6524void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6525 unsigned IntrinsicID) {
6526 // For now, we're only lowering an 'add' histogram.
6527 // We can add others later, e.g. saturating adds, min/max.
6528 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6529 "Tried to lower unsupported histogram type");
6530 SDLoc sdl = getCurSDLoc();
6531 Value *Ptr = I.getOperand(0);
6532 SDValue Inc = getValue(I.getOperand(1));
6533 SDValue Mask = getValue(I.getOperand(2));
6534
6535 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6536 DataLayout TargetDL = DAG.getDataLayout();
6537 EVT VT = Inc.getValueType();
6538 Align Alignment = DAG.getEVTAlign(VT);
6539
6540 const MDNode *Ranges = getRangeMetadata(I);
6541
6542 SDValue Root = DAG.getRoot();
6543 SDValue Base;
6544 SDValue Index;
6545 SDValue Scale;
6546 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6547 I.getParent(), VT.getScalarStoreSize());
6548
6549 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6550
6551 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6552 MachinePointerInfo(AS),
6554 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6555
6556 if (!UniformBase) {
6557 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6558 Index = getValue(Ptr);
6559 Scale =
6560 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6561 }
6562
6563 EVT IdxVT = Index.getValueType();
6564
6565 // Avoid using e.g. i32 as index type when the increment must be performed
6566 // on i64's.
6567 bool MustExtendIndex = VT.getScalarSizeInBits() > IdxVT.getScalarSizeInBits();
6568 EVT EltTy = MustExtendIndex ? VT : IdxVT.getVectorElementType();
6569 if (MustExtendIndex || TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6570 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
6571 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6572 }
6573
6574 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6575
6576 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6577 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6578 Ops, MMO, ISD::SIGNED_SCALED);
6579
6580 setValue(&I, Histogram);
6581 DAG.setRoot(Histogram);
6582}
6583
6584void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6585 unsigned Intrinsic) {
6586 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6587 "Tried lowering invalid vector extract last");
6588 SDLoc sdl = getCurSDLoc();
6589 const DataLayout &Layout = DAG.getDataLayout();
6590 SDValue Data = getValue(I.getOperand(0));
6591 SDValue Mask = getValue(I.getOperand(1));
6592
6593 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6594 EVT ResVT = TLI.getValueType(Layout, I.getType());
6595
6596 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6597 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6598 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6599
6600 Value *Default = I.getOperand(2);
6602 SDValue PassThru = getValue(Default);
6603 EVT BoolVT = Mask.getValueType().getScalarType();
6604 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6605 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6606 }
6607
6608 setValue(&I, Result);
6609}
6610
6611/// Lower the call to the specified intrinsic function.
6612void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6613 unsigned Intrinsic) {
6614 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6615 SDLoc sdl = getCurSDLoc();
6616 DebugLoc dl = getCurDebugLoc();
6617 SDValue Res;
6618
6619 SDNodeFlags Flags;
6620 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6621 Flags.copyFMF(*FPOp);
6622
6623 switch (Intrinsic) {
6624 default:
6625 // By default, turn this into a target intrinsic node.
6626 visitTargetIntrinsic(I, Intrinsic);
6627 return;
6628 case Intrinsic::vscale: {
6629 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6630 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6631 return;
6632 }
6633 case Intrinsic::vastart: visitVAStart(I); return;
6634 case Intrinsic::vaend: visitVAEnd(I); return;
6635 case Intrinsic::vacopy: visitVACopy(I); return;
6636 case Intrinsic::returnaddress:
6637 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6638 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6639 getValue(I.getArgOperand(0))));
6640 return;
6641 case Intrinsic::addressofreturnaddress:
6642 setValue(&I,
6643 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6644 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6645 return;
6646 case Intrinsic::sponentry:
6647 setValue(&I,
6648 DAG.getNode(ISD::SPONENTRY, sdl,
6649 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6650 return;
6651 case Intrinsic::frameaddress:
6652 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6653 TLI.getFrameIndexTy(DAG.getDataLayout()),
6654 getValue(I.getArgOperand(0))));
6655 return;
6656 case Intrinsic::read_volatile_register:
6657 case Intrinsic::read_register: {
6658 Value *Reg = I.getArgOperand(0);
6659 SDValue Chain = getRoot();
6661 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6662 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6663 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6664 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6665 setValue(&I, Res);
6666 DAG.setRoot(Res.getValue(1));
6667 return;
6668 }
6669 case Intrinsic::write_register: {
6670 Value *Reg = I.getArgOperand(0);
6671 Value *RegValue = I.getArgOperand(1);
6672 SDValue Chain = getRoot();
6674 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6675 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6676 RegName, getValue(RegValue)));
6677 return;
6678 }
6679 case Intrinsic::memcpy:
6680 case Intrinsic::memcpy_inline: {
6681 const auto &MCI = cast<MemCpyInst>(I);
6682 SDValue Dst = getValue(I.getArgOperand(0));
6683 SDValue Src = getValue(I.getArgOperand(1));
6684 SDValue Size = getValue(I.getArgOperand(2));
6685 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6686 "memcpy_inline needs constant size");
6687 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6688 Align DstAlign = MCI.getDestAlign().valueOrOne();
6689 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6690 Align Alignment = std::min(DstAlign, SrcAlign);
6691 bool isVol = MCI.isVolatile();
6692 // FIXME: Support passing different dest/src alignments to the memcpy DAG
6693 // node.
6694 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6695 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol,
6696 MCI.isForceInlined(), &I, std::nullopt,
6697 MachinePointerInfo(I.getArgOperand(0)),
6698 MachinePointerInfo(I.getArgOperand(1)),
6699 I.getAAMetadata(), BatchAA);
6700 updateDAGForMaybeTailCall(MC);
6701 return;
6702 }
6703 case Intrinsic::memset:
6704 case Intrinsic::memset_inline: {
6705 const auto &MSII = cast<MemSetInst>(I);
6706 SDValue Dst = getValue(I.getArgOperand(0));
6707 SDValue Value = getValue(I.getArgOperand(1));
6708 SDValue Size = getValue(I.getArgOperand(2));
6709 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6710 "memset_inline needs constant size");
6711 // @llvm.memset defines 0 and 1 to both mean no alignment.
6712 Align DstAlign = MSII.getDestAlign().valueOrOne();
6713 bool isVol = MSII.isVolatile();
6714 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6715 SDValue MC = DAG.getMemset(
6716 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6717 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6718 updateDAGForMaybeTailCall(MC);
6719 return;
6720 }
6721 case Intrinsic::memmove: {
6722 const auto &MMI = cast<MemMoveInst>(I);
6723 SDValue Op1 = getValue(I.getArgOperand(0));
6724 SDValue Op2 = getValue(I.getArgOperand(1));
6725 SDValue Op3 = getValue(I.getArgOperand(2));
6726 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6727 Align DstAlign = MMI.getDestAlign().valueOrOne();
6728 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6729 Align Alignment = std::min(DstAlign, SrcAlign);
6730 bool isVol = MMI.isVolatile();
6731 // FIXME: Support passing different dest/src alignments to the memmove DAG
6732 // node.
6733 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6734 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, &I,
6735 /* OverrideTailCall */ std::nullopt,
6736 MachinePointerInfo(I.getArgOperand(0)),
6737 MachinePointerInfo(I.getArgOperand(1)),
6738 I.getAAMetadata(), BatchAA);
6739 updateDAGForMaybeTailCall(MM);
6740 return;
6741 }
6742 case Intrinsic::memcpy_element_unordered_atomic: {
6743 auto &MI = cast<AnyMemCpyInst>(I);
6744 SDValue Dst = getValue(MI.getRawDest());
6745 SDValue Src = getValue(MI.getRawSource());
6746 SDValue Length = getValue(MI.getLength());
6747
6748 Type *LengthTy = MI.getLength()->getType();
6749 unsigned ElemSz = MI.getElementSizeInBytes();
6750 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6751 SDValue MC =
6752 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6753 isTC, MachinePointerInfo(MI.getRawDest()),
6754 MachinePointerInfo(MI.getRawSource()));
6755 updateDAGForMaybeTailCall(MC);
6756 return;
6757 }
6758 case Intrinsic::memmove_element_unordered_atomic: {
6759 auto &MI = cast<AnyMemMoveInst>(I);
6760 SDValue Dst = getValue(MI.getRawDest());
6761 SDValue Src = getValue(MI.getRawSource());
6762 SDValue Length = getValue(MI.getLength());
6763
6764 Type *LengthTy = MI.getLength()->getType();
6765 unsigned ElemSz = MI.getElementSizeInBytes();
6766 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6767 SDValue MC =
6768 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6769 isTC, MachinePointerInfo(MI.getRawDest()),
6770 MachinePointerInfo(MI.getRawSource()));
6771 updateDAGForMaybeTailCall(MC);
6772 return;
6773 }
6774 case Intrinsic::memset_element_unordered_atomic: {
6775 auto &MI = cast<AnyMemSetInst>(I);
6776 SDValue Dst = getValue(MI.getRawDest());
6777 SDValue Val = getValue(MI.getValue());
6778 SDValue Length = getValue(MI.getLength());
6779
6780 Type *LengthTy = MI.getLength()->getType();
6781 unsigned ElemSz = MI.getElementSizeInBytes();
6782 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6783 SDValue MC =
6784 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6785 isTC, MachinePointerInfo(MI.getRawDest()));
6786 updateDAGForMaybeTailCall(MC);
6787 return;
6788 }
6789 case Intrinsic::call_preallocated_setup: {
6790 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6791 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6792 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6793 getRoot(), SrcValue);
6794 setValue(&I, Res);
6795 DAG.setRoot(Res);
6796 return;
6797 }
6798 case Intrinsic::call_preallocated_arg: {
6799 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6800 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6801 SDValue Ops[3];
6802 Ops[0] = getRoot();
6803 Ops[1] = SrcValue;
6804 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6805 MVT::i32); // arg index
6806 SDValue Res = DAG.getNode(
6808 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6809 setValue(&I, Res);
6810 DAG.setRoot(Res.getValue(1));
6811 return;
6812 }
6813
6814 case Intrinsic::eh_typeid_for: {
6815 // Find the type id for the given typeinfo.
6816 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6817 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6818 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6819 setValue(&I, Res);
6820 return;
6821 }
6822
6823 case Intrinsic::eh_return_i32:
6824 case Intrinsic::eh_return_i64:
6825 DAG.getMachineFunction().setCallsEHReturn(true);
6826 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6827 MVT::Other,
6829 getValue(I.getArgOperand(0)),
6830 getValue(I.getArgOperand(1))));
6831 return;
6832 case Intrinsic::eh_unwind_init:
6833 DAG.getMachineFunction().setCallsUnwindInit(true);
6834 return;
6835 case Intrinsic::eh_dwarf_cfa:
6836 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6837 TLI.getPointerTy(DAG.getDataLayout()),
6838 getValue(I.getArgOperand(0))));
6839 return;
6840 case Intrinsic::eh_sjlj_callsite: {
6841 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6842 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6843
6844 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6845 return;
6846 }
6847 case Intrinsic::eh_sjlj_functioncontext: {
6848 // Get and store the index of the function context.
6849 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6850 AllocaInst *FnCtx =
6851 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6852 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6854 return;
6855 }
6856 case Intrinsic::eh_sjlj_setjmp: {
6857 SDValue Ops[2];
6858 Ops[0] = getRoot();
6859 Ops[1] = getValue(I.getArgOperand(0));
6860 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6861 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6862 setValue(&I, Op.getValue(0));
6863 DAG.setRoot(Op.getValue(1));
6864 return;
6865 }
6866 case Intrinsic::eh_sjlj_longjmp:
6867 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6868 getRoot(), getValue(I.getArgOperand(0))));
6869 return;
6870 case Intrinsic::eh_sjlj_setup_dispatch:
6871 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6872 getRoot()));
6873 return;
6874 case Intrinsic::masked_gather:
6875 visitMaskedGather(I);
6876 return;
6877 case Intrinsic::masked_load:
6878 visitMaskedLoad(I);
6879 return;
6880 case Intrinsic::masked_scatter:
6881 visitMaskedScatter(I);
6882 return;
6883 case Intrinsic::masked_store:
6884 visitMaskedStore(I);
6885 return;
6886 case Intrinsic::masked_expandload:
6887 visitMaskedLoad(I, true /* IsExpanding */);
6888 return;
6889 case Intrinsic::masked_compressstore:
6890 visitMaskedStore(I, true /* IsCompressing */);
6891 return;
6892 case Intrinsic::powi:
6893 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6894 getValue(I.getArgOperand(1)), DAG));
6895 return;
6896 case Intrinsic::log:
6897 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6898 return;
6899 case Intrinsic::log2:
6900 setValue(&I,
6901 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6902 return;
6903 case Intrinsic::log10:
6904 setValue(&I,
6905 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6906 return;
6907 case Intrinsic::exp:
6908 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6909 return;
6910 case Intrinsic::exp2:
6911 setValue(&I,
6912 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6913 return;
6914 case Intrinsic::pow:
6915 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6916 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6917 return;
6918 case Intrinsic::sqrt:
6919 case Intrinsic::fabs:
6920 case Intrinsic::sin:
6921 case Intrinsic::cos:
6922 case Intrinsic::tan:
6923 case Intrinsic::asin:
6924 case Intrinsic::acos:
6925 case Intrinsic::atan:
6926 case Intrinsic::sinh:
6927 case Intrinsic::cosh:
6928 case Intrinsic::tanh:
6929 case Intrinsic::exp10:
6930 case Intrinsic::floor:
6931 case Intrinsic::ceil:
6932 case Intrinsic::trunc:
6933 case Intrinsic::rint:
6934 case Intrinsic::nearbyint:
6935 case Intrinsic::round:
6936 case Intrinsic::roundeven:
6937 case Intrinsic::canonicalize: {
6938 unsigned Opcode;
6939 // clang-format off
6940 switch (Intrinsic) {
6941 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6942 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6943 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6944 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6945 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6946 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6947 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6948 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6949 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6950 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6951 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6952 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6953 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6954 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6955 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6956 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6957 case Intrinsic::rint: Opcode = ISD::FRINT; break;
6958 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6959 case Intrinsic::round: Opcode = ISD::FROUND; break;
6960 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6961 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6962 }
6963 // clang-format on
6964
6965 setValue(&I, DAG.getNode(Opcode, sdl,
6966 getValue(I.getArgOperand(0)).getValueType(),
6967 getValue(I.getArgOperand(0)), Flags));
6968 return;
6969 }
6970 case Intrinsic::atan2:
6971 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
6972 getValue(I.getArgOperand(0)).getValueType(),
6973 getValue(I.getArgOperand(0)),
6974 getValue(I.getArgOperand(1)), Flags));
6975 return;
6976 case Intrinsic::lround:
6977 case Intrinsic::llround:
6978 case Intrinsic::lrint:
6979 case Intrinsic::llrint: {
6980 unsigned Opcode;
6981 // clang-format off
6982 switch (Intrinsic) {
6983 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6984 case Intrinsic::lround: Opcode = ISD::LROUND; break;
6985 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6986 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
6987 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
6988 }
6989 // clang-format on
6990
6991 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6992 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6993 getValue(I.getArgOperand(0))));
6994 return;
6995 }
6996 case Intrinsic::minnum:
6997 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6998 getValue(I.getArgOperand(0)).getValueType(),
6999 getValue(I.getArgOperand(0)),
7000 getValue(I.getArgOperand(1)), Flags));
7001 return;
7002 case Intrinsic::maxnum:
7003 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
7004 getValue(I.getArgOperand(0)).getValueType(),
7005 getValue(I.getArgOperand(0)),
7006 getValue(I.getArgOperand(1)), Flags));
7007 return;
7008 case Intrinsic::minimum:
7009 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
7010 getValue(I.getArgOperand(0)).getValueType(),
7011 getValue(I.getArgOperand(0)),
7012 getValue(I.getArgOperand(1)), Flags));
7013 return;
7014 case Intrinsic::maximum:
7015 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
7016 getValue(I.getArgOperand(0)).getValueType(),
7017 getValue(I.getArgOperand(0)),
7018 getValue(I.getArgOperand(1)), Flags));
7019 return;
7020 case Intrinsic::minimumnum:
7021 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
7022 getValue(I.getArgOperand(0)).getValueType(),
7023 getValue(I.getArgOperand(0)),
7024 getValue(I.getArgOperand(1)), Flags));
7025 return;
7026 case Intrinsic::maximumnum:
7027 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
7028 getValue(I.getArgOperand(0)).getValueType(),
7029 getValue(I.getArgOperand(0)),
7030 getValue(I.getArgOperand(1)), Flags));
7031 return;
7032 case Intrinsic::copysign:
7033 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
7034 getValue(I.getArgOperand(0)).getValueType(),
7035 getValue(I.getArgOperand(0)),
7036 getValue(I.getArgOperand(1)), Flags));
7037 return;
7038 case Intrinsic::ldexp:
7039 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
7040 getValue(I.getArgOperand(0)).getValueType(),
7041 getValue(I.getArgOperand(0)),
7042 getValue(I.getArgOperand(1)), Flags));
7043 return;
7044 case Intrinsic::modf:
7045 case Intrinsic::sincos:
7046 case Intrinsic::sincospi:
7047 case Intrinsic::frexp: {
7048 unsigned Opcode;
7049 switch (Intrinsic) {
7050 default:
7051 llvm_unreachable("unexpected intrinsic");
7052 case Intrinsic::sincos:
7053 Opcode = ISD::FSINCOS;
7054 break;
7055 case Intrinsic::sincospi:
7056 Opcode = ISD::FSINCOSPI;
7057 break;
7058 case Intrinsic::modf:
7059 Opcode = ISD::FMODF;
7060 break;
7061 case Intrinsic::frexp:
7062 Opcode = ISD::FFREXP;
7063 break;
7064 }
7065 SmallVector<EVT, 2> ValueVTs;
7066 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
7067 SDVTList VTs = DAG.getVTList(ValueVTs);
7068 setValue(
7069 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
7070 return;
7071 }
7072 case Intrinsic::arithmetic_fence: {
7073 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
7074 getValue(I.getArgOperand(0)).getValueType(),
7075 getValue(I.getArgOperand(0)), Flags));
7076 return;
7077 }
7078 case Intrinsic::fma:
7079 setValue(&I, DAG.getNode(
7080 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
7081 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
7082 getValue(I.getArgOperand(2)), Flags));
7083 return;
7084#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
7085 case Intrinsic::INTRINSIC:
7086#include "llvm/IR/ConstrainedOps.def"
7087 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
7088 return;
7089#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
7090#include "llvm/IR/VPIntrinsics.def"
7091 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
7092 return;
7093 case Intrinsic::fptrunc_round: {
7094 // Get the last argument, the metadata and convert it to an integer in the
7095 // call
7096 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7097 std::optional<RoundingMode> RoundMode =
7098 convertStrToRoundingMode(cast<MDString>(MD)->getString());
7099
7100 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7101
7102 // Propagate fast-math-flags from IR to node(s).
7103 SDNodeFlags Flags;
7104 Flags.copyFMF(*cast<FPMathOperator>(&I));
7105 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
7106
7108 Result = DAG.getNode(
7109 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
7110 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
7111 setValue(&I, Result);
7112
7113 return;
7114 }
7115 case Intrinsic::fmuladd: {
7116 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7117 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
7118 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
7119 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7120 getValue(I.getArgOperand(0)).getValueType(),
7121 getValue(I.getArgOperand(0)),
7122 getValue(I.getArgOperand(1)),
7123 getValue(I.getArgOperand(2)), Flags));
7124 } else if (TLI.isOperationLegalOrCustom(ISD::FMULADD, VT)) {
7125 // TODO: Support splitting the vector.
7126 setValue(&I, DAG.getNode(ISD::FMULADD, sdl,
7127 getValue(I.getArgOperand(0)).getValueType(),
7128 getValue(I.getArgOperand(0)),
7129 getValue(I.getArgOperand(1)),
7130 getValue(I.getArgOperand(2)), Flags));
7131 } else {
7132 // TODO: Intrinsic calls should have fast-math-flags.
7133 SDValue Mul = DAG.getNode(
7134 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7135 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7136 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7137 getValue(I.getArgOperand(0)).getValueType(),
7138 Mul, getValue(I.getArgOperand(2)), Flags);
7139 setValue(&I, Add);
7140 }
7141 return;
7142 }
7143 case Intrinsic::fptosi_sat: {
7144 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7145 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7146 getValue(I.getArgOperand(0)),
7147 DAG.getValueType(VT.getScalarType())));
7148 return;
7149 }
7150 case Intrinsic::fptoui_sat: {
7151 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7152 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7153 getValue(I.getArgOperand(0)),
7154 DAG.getValueType(VT.getScalarType())));
7155 return;
7156 }
7157 case Intrinsic::convert_from_arbitrary_fp: {
7158 // Extract format metadata and convert to semantics enum.
7159 EVT DstVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7160 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7161 StringRef FormatStr = cast<MDString>(MD)->getString();
7162 const fltSemantics *SrcSem =
7164 if (!SrcSem) {
7165 DAG.getContext()->emitError(
7166 "convert_from_arbitrary_fp: not implemented format '" + FormatStr +
7167 "'");
7168 setValue(&I, DAG.getPOISON(DstVT));
7169 return;
7170 }
7172
7173 SDValue IntVal = getValue(I.getArgOperand(0));
7174
7175 // Emit ISD::CONVERT_FROM_ARBITRARY_FP node.
7176 SDValue SemConst =
7177 DAG.getTargetConstant(static_cast<int>(SemEnum), sdl, MVT::i32);
7178 setValue(&I, DAG.getNode(ISD::CONVERT_FROM_ARBITRARY_FP, sdl, DstVT, IntVal,
7179 SemConst));
7180 return;
7181 }
7182 case Intrinsic::set_rounding:
7183 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7184 {getRoot(), getValue(I.getArgOperand(0))});
7185 setValue(&I, Res);
7186 DAG.setRoot(Res.getValue(0));
7187 return;
7188 case Intrinsic::is_fpclass: {
7189 const DataLayout DLayout = DAG.getDataLayout();
7190 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7191 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7192 FPClassTest Test = static_cast<FPClassTest>(
7193 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7194 MachineFunction &MF = DAG.getMachineFunction();
7195 const Function &F = MF.getFunction();
7196 SDValue Op = getValue(I.getArgOperand(0));
7197 SDNodeFlags Flags;
7198 Flags.setNoFPExcept(
7199 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7200 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7201 // expansion can use illegal types. Making expansion early allows
7202 // legalizing these types prior to selection.
7203 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7204 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7205 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7206 setValue(&I, Result);
7207 return;
7208 }
7209
7210 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7211 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7212 setValue(&I, V);
7213 return;
7214 }
7215 case Intrinsic::get_fpenv: {
7216 const DataLayout DLayout = DAG.getDataLayout();
7217 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7218 Align TempAlign = DAG.getEVTAlign(EnvVT);
7219 SDValue Chain = getRoot();
7220 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7221 // and temporary storage in stack.
7222 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7223 Res = DAG.getNode(
7224 ISD::GET_FPENV, sdl,
7225 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7226 MVT::Other),
7227 Chain);
7228 } else {
7229 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7230 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7231 auto MPI =
7232 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7233 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7235 TempAlign);
7236 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7237 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7238 }
7239 setValue(&I, Res);
7240 DAG.setRoot(Res.getValue(1));
7241 return;
7242 }
7243 case Intrinsic::set_fpenv: {
7244 const DataLayout DLayout = DAG.getDataLayout();
7245 SDValue Env = getValue(I.getArgOperand(0));
7246 EVT EnvVT = Env.getValueType();
7247 Align TempAlign = DAG.getEVTAlign(EnvVT);
7248 SDValue Chain = getRoot();
7249 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7250 // environment from memory.
7251 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7252 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7253 } else {
7254 // Allocate space in stack, copy environment bits into it and use this
7255 // memory in SET_FPENV_MEM.
7256 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7257 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7258 auto MPI =
7259 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7260 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7262 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7264 TempAlign);
7265 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7266 }
7267 DAG.setRoot(Chain);
7268 return;
7269 }
7270 case Intrinsic::reset_fpenv:
7271 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7272 return;
7273 case Intrinsic::get_fpmode:
7274 Res = DAG.getNode(
7275 ISD::GET_FPMODE, sdl,
7276 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7277 MVT::Other),
7278 DAG.getRoot());
7279 setValue(&I, Res);
7280 DAG.setRoot(Res.getValue(1));
7281 return;
7282 case Intrinsic::set_fpmode:
7283 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7284 getValue(I.getArgOperand(0)));
7285 DAG.setRoot(Res);
7286 return;
7287 case Intrinsic::reset_fpmode: {
7288 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7289 DAG.setRoot(Res);
7290 return;
7291 }
7292 case Intrinsic::pcmarker: {
7293 SDValue Tmp = getValue(I.getArgOperand(0));
7294 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7295 return;
7296 }
7297 case Intrinsic::readcyclecounter: {
7298 SDValue Op = getRoot();
7299 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7300 DAG.getVTList(MVT::i64, MVT::Other), Op);
7301 setValue(&I, Res);
7302 DAG.setRoot(Res.getValue(1));
7303 return;
7304 }
7305 case Intrinsic::readsteadycounter: {
7306 SDValue Op = getRoot();
7307 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7308 DAG.getVTList(MVT::i64, MVT::Other), Op);
7309 setValue(&I, Res);
7310 DAG.setRoot(Res.getValue(1));
7311 return;
7312 }
7313 case Intrinsic::bitreverse:
7314 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7315 getValue(I.getArgOperand(0)).getValueType(),
7316 getValue(I.getArgOperand(0))));
7317 return;
7318 case Intrinsic::bswap:
7319 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7320 getValue(I.getArgOperand(0)).getValueType(),
7321 getValue(I.getArgOperand(0))));
7322 return;
7323 case Intrinsic::cttz: {
7324 SDValue Arg = getValue(I.getArgOperand(0));
7325 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7326 EVT Ty = Arg.getValueType();
7327 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
7328 sdl, Ty, Arg));
7329 return;
7330 }
7331 case Intrinsic::ctlz: {
7332 SDValue Arg = getValue(I.getArgOperand(0));
7333 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7334 EVT Ty = Arg.getValueType();
7335 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
7336 sdl, Ty, Arg));
7337 return;
7338 }
7339 case Intrinsic::ctpop: {
7340 SDValue Arg = getValue(I.getArgOperand(0));
7341 EVT Ty = Arg.getValueType();
7342 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7343 return;
7344 }
7345 case Intrinsic::fshl:
7346 case Intrinsic::fshr: {
7347 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7348 SDValue X = getValue(I.getArgOperand(0));
7349 SDValue Y = getValue(I.getArgOperand(1));
7350 SDValue Z = getValue(I.getArgOperand(2));
7351 EVT VT = X.getValueType();
7352
7353 if (X == Y) {
7354 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7355 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7356 } else {
7357 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7358 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7359 }
7360 return;
7361 }
7362 case Intrinsic::clmul: {
7363 SDValue X = getValue(I.getArgOperand(0));
7364 SDValue Y = getValue(I.getArgOperand(1));
7365 setValue(&I, DAG.getNode(ISD::CLMUL, sdl, X.getValueType(), X, Y));
7366 return;
7367 }
7368 case Intrinsic::sadd_sat: {
7369 SDValue Op1 = getValue(I.getArgOperand(0));
7370 SDValue Op2 = getValue(I.getArgOperand(1));
7371 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7372 return;
7373 }
7374 case Intrinsic::uadd_sat: {
7375 SDValue Op1 = getValue(I.getArgOperand(0));
7376 SDValue Op2 = getValue(I.getArgOperand(1));
7377 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7378 return;
7379 }
7380 case Intrinsic::ssub_sat: {
7381 SDValue Op1 = getValue(I.getArgOperand(0));
7382 SDValue Op2 = getValue(I.getArgOperand(1));
7383 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7384 return;
7385 }
7386 case Intrinsic::usub_sat: {
7387 SDValue Op1 = getValue(I.getArgOperand(0));
7388 SDValue Op2 = getValue(I.getArgOperand(1));
7389 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7390 return;
7391 }
7392 case Intrinsic::sshl_sat:
7393 case Intrinsic::ushl_sat: {
7394 SDValue Op1 = getValue(I.getArgOperand(0));
7395 SDValue Op2 = getValue(I.getArgOperand(1));
7396
7397 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
7398 Op1.getValueType(), DAG.getDataLayout());
7399
7400 // Coerce the shift amount to the right type if we can. This exposes the
7401 // truncate or zext to optimization early.
7402 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
7403 assert(ShiftTy.getSizeInBits() >=
7405 "Unexpected shift type");
7406 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
7407 }
7408
7409 unsigned Opc =
7410 Intrinsic == Intrinsic::sshl_sat ? ISD::SSHLSAT : ISD::USHLSAT;
7411 setValue(&I, DAG.getNode(Opc, sdl, Op1.getValueType(), Op1, Op2));
7412 return;
7413 }
7414 case Intrinsic::smul_fix:
7415 case Intrinsic::umul_fix:
7416 case Intrinsic::smul_fix_sat:
7417 case Intrinsic::umul_fix_sat: {
7418 SDValue Op1 = getValue(I.getArgOperand(0));
7419 SDValue Op2 = getValue(I.getArgOperand(1));
7420 SDValue Op3 = getValue(I.getArgOperand(2));
7421 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7422 Op1.getValueType(), Op1, Op2, Op3));
7423 return;
7424 }
7425 case Intrinsic::sdiv_fix:
7426 case Intrinsic::udiv_fix:
7427 case Intrinsic::sdiv_fix_sat:
7428 case Intrinsic::udiv_fix_sat: {
7429 SDValue Op1 = getValue(I.getArgOperand(0));
7430 SDValue Op2 = getValue(I.getArgOperand(1));
7431 SDValue Op3 = getValue(I.getArgOperand(2));
7433 Op1, Op2, Op3, DAG, TLI));
7434 return;
7435 }
7436 case Intrinsic::smax: {
7437 SDValue Op1 = getValue(I.getArgOperand(0));
7438 SDValue Op2 = getValue(I.getArgOperand(1));
7439 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7440 return;
7441 }
7442 case Intrinsic::smin: {
7443 SDValue Op1 = getValue(I.getArgOperand(0));
7444 SDValue Op2 = getValue(I.getArgOperand(1));
7445 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7446 return;
7447 }
7448 case Intrinsic::umax: {
7449 SDValue Op1 = getValue(I.getArgOperand(0));
7450 SDValue Op2 = getValue(I.getArgOperand(1));
7451 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7452 return;
7453 }
7454 case Intrinsic::umin: {
7455 SDValue Op1 = getValue(I.getArgOperand(0));
7456 SDValue Op2 = getValue(I.getArgOperand(1));
7457 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7458 return;
7459 }
7460 case Intrinsic::abs: {
7461 // TODO: Preserve "int min is poison" arg in SDAG?
7462 SDValue Op1 = getValue(I.getArgOperand(0));
7463 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7464 return;
7465 }
7466 case Intrinsic::scmp: {
7467 SDValue Op1 = getValue(I.getArgOperand(0));
7468 SDValue Op2 = getValue(I.getArgOperand(1));
7469 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7470 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7471 break;
7472 }
7473 case Intrinsic::ucmp: {
7474 SDValue Op1 = getValue(I.getArgOperand(0));
7475 SDValue Op2 = getValue(I.getArgOperand(1));
7476 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7477 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7478 break;
7479 }
7480 case Intrinsic::stackaddress:
7481 case Intrinsic::stacksave: {
7482 unsigned SDOpcode = Intrinsic == Intrinsic::stackaddress ? ISD::STACKADDRESS
7484 SDValue Op = getRoot();
7485 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7486 Res = DAG.getNode(SDOpcode, sdl, DAG.getVTList(VT, MVT::Other), Op);
7487 setValue(&I, Res);
7488 DAG.setRoot(Res.getValue(1));
7489 return;
7490 }
7491 case Intrinsic::stackrestore:
7492 Res = getValue(I.getArgOperand(0));
7493 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7494 return;
7495 case Intrinsic::get_dynamic_area_offset: {
7496 SDValue Op = getRoot();
7497 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7498 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7499 Op);
7500 DAG.setRoot(Op);
7501 setValue(&I, Res);
7502 return;
7503 }
7504 case Intrinsic::stackguard: {
7505 MachineFunction &MF = DAG.getMachineFunction();
7506 const Module &M = *MF.getFunction().getParent();
7507 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7508 SDValue Chain = getRoot();
7509 if (TLI.useLoadStackGuardNode(M)) {
7510 Res = getLoadStackGuard(DAG, sdl, Chain);
7511 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7512 } else {
7513 const Value *Global = TLI.getSDagStackGuard(M, DAG.getLibcalls());
7514 if (!Global) {
7515 LLVMContext &Ctx = *DAG.getContext();
7516 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
7517 setValue(&I, DAG.getPOISON(PtrTy));
7518 return;
7519 }
7520
7521 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7522 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7523 MachinePointerInfo(Global, 0), Align,
7525 }
7526 if (TLI.useStackGuardXorFP())
7527 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7528 DAG.setRoot(Chain);
7529 setValue(&I, Res);
7530 return;
7531 }
7532 case Intrinsic::stackprotector: {
7533 // Emit code into the DAG to store the stack guard onto the stack.
7534 MachineFunction &MF = DAG.getMachineFunction();
7535 MachineFrameInfo &MFI = MF.getFrameInfo();
7536 const Module &M = *MF.getFunction().getParent();
7537 SDValue Src, Chain = getRoot();
7538
7539 if (TLI.useLoadStackGuardNode(M))
7540 Src = getLoadStackGuard(DAG, sdl, Chain);
7541 else
7542 Src = getValue(I.getArgOperand(0)); // The guard's value.
7543
7544 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7545
7546 int FI = FuncInfo.StaticAllocaMap[Slot];
7547 MFI.setStackProtectorIndex(FI);
7548 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7549
7550 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7551
7552 // Store the stack protector onto the stack.
7553 Res = DAG.getStore(
7554 Chain, sdl, Src, FIN,
7555 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7556 MaybeAlign(), MachineMemOperand::MOVolatile);
7557 setValue(&I, Res);
7558 DAG.setRoot(Res);
7559 return;
7560 }
7561 case Intrinsic::objectsize:
7562 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7563
7564 case Intrinsic::is_constant:
7565 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7566
7567 case Intrinsic::annotation:
7568 case Intrinsic::ptr_annotation:
7569 case Intrinsic::launder_invariant_group:
7570 case Intrinsic::strip_invariant_group:
7571 // Drop the intrinsic, but forward the value
7572 setValue(&I, getValue(I.getOperand(0)));
7573 return;
7574
7575 case Intrinsic::type_test:
7576 case Intrinsic::public_type_test:
7577 reportFatalUsageError("llvm.type.test intrinsic must be lowered by the "
7578 "LowerTypeTests pass before code generation");
7579 return;
7580
7581 case Intrinsic::assume:
7582 case Intrinsic::experimental_noalias_scope_decl:
7583 case Intrinsic::var_annotation:
7584 case Intrinsic::sideeffect:
7585 // Discard annotate attributes, noalias scope declarations, assumptions, and
7586 // artificial side-effects.
7587 return;
7588
7589 case Intrinsic::codeview_annotation: {
7590 // Emit a label associated with this metadata.
7591 MachineFunction &MF = DAG.getMachineFunction();
7592 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7593 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7594 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7595 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7596 DAG.setRoot(Res);
7597 return;
7598 }
7599
7600 case Intrinsic::init_trampoline: {
7601 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7602
7603 SDValue Ops[6];
7604 Ops[0] = getRoot();
7605 Ops[1] = getValue(I.getArgOperand(0));
7606 Ops[2] = getValue(I.getArgOperand(1));
7607 Ops[3] = getValue(I.getArgOperand(2));
7608 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7609 Ops[5] = DAG.getSrcValue(F);
7610
7611 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7612
7613 DAG.setRoot(Res);
7614 return;
7615 }
7616 case Intrinsic::adjust_trampoline:
7617 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7618 TLI.getPointerTy(DAG.getDataLayout()),
7619 getValue(I.getArgOperand(0))));
7620 return;
7621 case Intrinsic::gcroot: {
7622 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7623 "only valid in functions with gc specified, enforced by Verifier");
7624 assert(GFI && "implied by previous");
7625 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7626 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7627
7628 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7629 GFI->addStackRoot(FI->getIndex(), TypeMap);
7630 return;
7631 }
7632 case Intrinsic::gcread:
7633 case Intrinsic::gcwrite:
7634 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7635 case Intrinsic::get_rounding:
7636 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7637 setValue(&I, Res);
7638 DAG.setRoot(Res.getValue(1));
7639 return;
7640
7641 case Intrinsic::expect:
7642 case Intrinsic::expect_with_probability:
7643 // Just replace __builtin_expect(exp, c) and
7644 // __builtin_expect_with_probability(exp, c, p) with EXP.
7645 setValue(&I, getValue(I.getArgOperand(0)));
7646 return;
7647
7648 case Intrinsic::ubsantrap:
7649 case Intrinsic::debugtrap:
7650 case Intrinsic::trap: {
7651 StringRef TrapFuncName =
7652 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7653 if (TrapFuncName.empty()) {
7654 switch (Intrinsic) {
7655 case Intrinsic::trap:
7656 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7657 break;
7658 case Intrinsic::debugtrap:
7659 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7660 break;
7661 case Intrinsic::ubsantrap:
7662 DAG.setRoot(DAG.getNode(
7663 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7664 DAG.getTargetConstant(
7665 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7666 MVT::i32)));
7667 break;
7668 default: llvm_unreachable("unknown trap intrinsic");
7669 }
7670 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7671 I.hasFnAttr(Attribute::NoMerge));
7672 return;
7673 }
7675 if (Intrinsic == Intrinsic::ubsantrap) {
7676 Value *Arg = I.getArgOperand(0);
7677 Args.emplace_back(Arg, getValue(Arg));
7678 }
7679
7680 TargetLowering::CallLoweringInfo CLI(DAG);
7681 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7682 CallingConv::C, I.getType(),
7683 DAG.getExternalSymbol(TrapFuncName.data(),
7684 TLI.getPointerTy(DAG.getDataLayout())),
7685 std::move(Args));
7686 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7687 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7688 DAG.setRoot(Result.second);
7689 return;
7690 }
7691
7692 case Intrinsic::allow_runtime_check:
7693 case Intrinsic::allow_ubsan_check:
7694 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7695 return;
7696
7697 case Intrinsic::uadd_with_overflow:
7698 case Intrinsic::sadd_with_overflow:
7699 case Intrinsic::usub_with_overflow:
7700 case Intrinsic::ssub_with_overflow:
7701 case Intrinsic::umul_with_overflow:
7702 case Intrinsic::smul_with_overflow: {
7704 switch (Intrinsic) {
7705 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7706 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7707 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7708 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7709 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7710 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7711 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7712 }
7713 SDValue Op1 = getValue(I.getArgOperand(0));
7714 SDValue Op2 = getValue(I.getArgOperand(1));
7715
7716 EVT ResultVT = Op1.getValueType();
7717 EVT OverflowVT = ResultVT.changeElementType(*Context, MVT::i1);
7718
7719 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7720 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7721 return;
7722 }
7723 case Intrinsic::prefetch: {
7724 SDValue Ops[5];
7725 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7727 Ops[0] = DAG.getRoot();
7728 Ops[1] = getValue(I.getArgOperand(0));
7729 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7730 MVT::i32);
7731 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7732 MVT::i32);
7733 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7734 MVT::i32);
7735 SDValue Result = DAG.getMemIntrinsicNode(
7736 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7737 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7738 /* align */ std::nullopt, Flags);
7739
7740 // Chain the prefetch in parallel with any pending loads, to stay out of
7741 // the way of later optimizations.
7742 PendingLoads.push_back(Result);
7743 Result = getRoot();
7744 DAG.setRoot(Result);
7745 return;
7746 }
7747 case Intrinsic::lifetime_start:
7748 case Intrinsic::lifetime_end: {
7749 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7750 // Stack coloring is not enabled in O0, discard region information.
7751 if (TM.getOptLevel() == CodeGenOptLevel::None)
7752 return;
7753
7754 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7755 if (!LifetimeObject)
7756 return;
7757
7758 // First check that the Alloca is static, otherwise it won't have a
7759 // valid frame index.
7760 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7761 if (SI == FuncInfo.StaticAllocaMap.end())
7762 return;
7763
7764 const int FrameIndex = SI->second;
7765 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7766 DAG.setRoot(Res);
7767 return;
7768 }
7769 case Intrinsic::pseudoprobe: {
7770 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7771 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7772 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7773 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7774 DAG.setRoot(Res);
7775 return;
7776 }
7777 case Intrinsic::invariant_start:
7778 // Discard region information.
7779 setValue(&I,
7780 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7781 return;
7782 case Intrinsic::invariant_end:
7783 // Discard region information.
7784 return;
7785 case Intrinsic::clear_cache: {
7786 SDValue InputChain = DAG.getRoot();
7787 SDValue StartVal = getValue(I.getArgOperand(0));
7788 SDValue EndVal = getValue(I.getArgOperand(1));
7789 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7790 {InputChain, StartVal, EndVal});
7791 setValue(&I, Res);
7792 DAG.setRoot(Res);
7793 return;
7794 }
7795 case Intrinsic::donothing:
7796 case Intrinsic::seh_try_begin:
7797 case Intrinsic::seh_scope_begin:
7798 case Intrinsic::seh_try_end:
7799 case Intrinsic::seh_scope_end:
7800 // ignore
7801 return;
7802 case Intrinsic::experimental_stackmap:
7803 visitStackmap(I);
7804 return;
7805 case Intrinsic::experimental_patchpoint_void:
7806 case Intrinsic::experimental_patchpoint:
7807 visitPatchpoint(I);
7808 return;
7809 case Intrinsic::experimental_gc_statepoint:
7811 return;
7812 case Intrinsic::experimental_gc_result:
7813 visitGCResult(cast<GCResultInst>(I));
7814 return;
7815 case Intrinsic::experimental_gc_relocate:
7816 visitGCRelocate(cast<GCRelocateInst>(I));
7817 return;
7818 case Intrinsic::instrprof_cover:
7819 llvm_unreachable("instrprof failed to lower a cover");
7820 case Intrinsic::instrprof_increment:
7821 llvm_unreachable("instrprof failed to lower an increment");
7822 case Intrinsic::instrprof_timestamp:
7823 llvm_unreachable("instrprof failed to lower a timestamp");
7824 case Intrinsic::instrprof_value_profile:
7825 llvm_unreachable("instrprof failed to lower a value profiling call");
7826 case Intrinsic::instrprof_mcdc_parameters:
7827 llvm_unreachable("instrprof failed to lower mcdc parameters");
7828 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7829 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7830 case Intrinsic::localescape: {
7831 MachineFunction &MF = DAG.getMachineFunction();
7832 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7833
7834 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7835 // is the same on all targets.
7836 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7837 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7838 if (isa<ConstantPointerNull>(Arg))
7839 continue; // Skip null pointers. They represent a hole in index space.
7840 AllocaInst *Slot = cast<AllocaInst>(Arg);
7841 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7842 "can only escape static allocas");
7843 int FI = FuncInfo.StaticAllocaMap[Slot];
7844 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7846 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7847 TII->get(TargetOpcode::LOCAL_ESCAPE))
7848 .addSym(FrameAllocSym)
7849 .addFrameIndex(FI);
7850 }
7851
7852 return;
7853 }
7854
7855 case Intrinsic::localrecover: {
7856 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7857 MachineFunction &MF = DAG.getMachineFunction();
7858
7859 // Get the symbol that defines the frame offset.
7860 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7861 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7862 unsigned IdxVal =
7863 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7864 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7866
7867 Value *FP = I.getArgOperand(1);
7868 SDValue FPVal = getValue(FP);
7869 EVT PtrVT = FPVal.getValueType();
7870
7871 // Create a MCSymbol for the label to avoid any target lowering
7872 // that would make this PC relative.
7873 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7874 SDValue OffsetVal =
7875 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7876
7877 // Add the offset to the FP.
7878 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7879 setValue(&I, Add);
7880
7881 return;
7882 }
7883
7884 case Intrinsic::fake_use: {
7885 Value *V = I.getArgOperand(0);
7886 SDValue Ops[2];
7887 // For Values not declared or previously used in this basic block, the
7888 // NodeMap will not have an entry, and `getValue` will assert if V has no
7889 // valid register value.
7890 auto FakeUseValue = [&]() -> SDValue {
7891 SDValue &N = NodeMap[V];
7892 if (N.getNode())
7893 return N;
7894
7895 // If there's a virtual register allocated and initialized for this
7896 // value, use it.
7897 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7898 return copyFromReg;
7899 // FIXME: Do we want to preserve constants? It seems pointless.
7900 if (isa<Constant>(V))
7901 return getValue(V);
7902 return SDValue();
7903 }();
7904 if (!FakeUseValue || FakeUseValue.isUndef())
7905 return;
7906 Ops[0] = getRoot();
7907 Ops[1] = FakeUseValue;
7908 // Also, do not translate a fake use with an undef operand, or any other
7909 // empty SDValues.
7910 if (!Ops[1] || Ops[1].isUndef())
7911 return;
7912 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7913 return;
7914 }
7915
7916 case Intrinsic::reloc_none: {
7917 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7918 StringRef SymbolName = cast<MDString>(MD)->getString();
7919 SDValue Ops[2] = {
7920 getRoot(),
7921 DAG.getTargetExternalSymbol(
7922 SymbolName.data(), TLI.getProgramPointerTy(DAG.getDataLayout()))};
7923 DAG.setRoot(DAG.getNode(ISD::RELOC_NONE, sdl, MVT::Other, Ops));
7924 return;
7925 }
7926
7927 case Intrinsic::cond_loop: {
7928 SDValue InputChain = DAG.getRoot();
7929 SDValue P = getValue(I.getArgOperand(0));
7930 Res = DAG.getNode(ISD::COND_LOOP, sdl, DAG.getVTList(MVT::Other),
7931 {InputChain, P});
7932 setValue(&I, Res);
7933 DAG.setRoot(Res);
7934 return;
7935 }
7936
7937 case Intrinsic::eh_exceptionpointer:
7938 case Intrinsic::eh_exceptioncode: {
7939 // Get the exception pointer vreg, copy from it, and resize it to fit.
7940 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7941 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7942 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7943 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7944 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7945 if (Intrinsic == Intrinsic::eh_exceptioncode)
7946 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7947 setValue(&I, N);
7948 return;
7949 }
7950 case Intrinsic::xray_customevent: {
7951 // Here we want to make sure that the intrinsic behaves as if it has a
7952 // specific calling convention.
7953 const auto &Triple = DAG.getTarget().getTargetTriple();
7954 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7955 return;
7956
7958
7959 // We want to say that we always want the arguments in registers.
7960 SDValue LogEntryVal = getValue(I.getArgOperand(0));
7961 SDValue StrSizeVal = getValue(I.getArgOperand(1));
7962 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7963 SDValue Chain = getRoot();
7964 Ops.push_back(LogEntryVal);
7965 Ops.push_back(StrSizeVal);
7966 Ops.push_back(Chain);
7967
7968 // We need to enforce the calling convention for the callsite, so that
7969 // argument ordering is enforced correctly, and that register allocation can
7970 // see that some registers may be assumed clobbered and have to preserve
7971 // them across calls to the intrinsic.
7972 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7973 sdl, NodeTys, Ops);
7974 SDValue patchableNode = SDValue(MN, 0);
7975 DAG.setRoot(patchableNode);
7976 setValue(&I, patchableNode);
7977 return;
7978 }
7979 case Intrinsic::xray_typedevent: {
7980 // Here we want to make sure that the intrinsic behaves as if it has a
7981 // specific calling convention.
7982 const auto &Triple = DAG.getTarget().getTargetTriple();
7983 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7984 return;
7985
7987
7988 // We want to say that we always want the arguments in registers.
7989 // It's unclear to me how manipulating the selection DAG here forces callers
7990 // to provide arguments in registers instead of on the stack.
7991 SDValue LogTypeId = getValue(I.getArgOperand(0));
7992 SDValue LogEntryVal = getValue(I.getArgOperand(1));
7993 SDValue StrSizeVal = getValue(I.getArgOperand(2));
7994 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7995 SDValue Chain = getRoot();
7996 Ops.push_back(LogTypeId);
7997 Ops.push_back(LogEntryVal);
7998 Ops.push_back(StrSizeVal);
7999 Ops.push_back(Chain);
8000
8001 // We need to enforce the calling convention for the callsite, so that
8002 // argument ordering is enforced correctly, and that register allocation can
8003 // see that some registers may be assumed clobbered and have to preserve
8004 // them across calls to the intrinsic.
8005 MachineSDNode *MN = DAG.getMachineNode(
8006 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
8007 SDValue patchableNode = SDValue(MN, 0);
8008 DAG.setRoot(patchableNode);
8009 setValue(&I, patchableNode);
8010 return;
8011 }
8012 case Intrinsic::experimental_deoptimize:
8014 return;
8015 case Intrinsic::stepvector:
8016 visitStepVector(I);
8017 return;
8018 case Intrinsic::vector_reduce_fadd:
8019 case Intrinsic::vector_reduce_fmul:
8020 case Intrinsic::vector_reduce_add:
8021 case Intrinsic::vector_reduce_mul:
8022 case Intrinsic::vector_reduce_and:
8023 case Intrinsic::vector_reduce_or:
8024 case Intrinsic::vector_reduce_xor:
8025 case Intrinsic::vector_reduce_smax:
8026 case Intrinsic::vector_reduce_smin:
8027 case Intrinsic::vector_reduce_umax:
8028 case Intrinsic::vector_reduce_umin:
8029 case Intrinsic::vector_reduce_fmax:
8030 case Intrinsic::vector_reduce_fmin:
8031 case Intrinsic::vector_reduce_fmaximum:
8032 case Intrinsic::vector_reduce_fminimum:
8033 visitVectorReduce(I, Intrinsic);
8034 return;
8035
8036 case Intrinsic::icall_branch_funnel: {
8038 Ops.push_back(getValue(I.getArgOperand(0)));
8039
8040 int64_t Offset;
8042 I.getArgOperand(1), Offset, DAG.getDataLayout()));
8043 if (!Base)
8045 "llvm.icall.branch.funnel operand must be a GlobalValue");
8046 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
8047
8048 struct BranchFunnelTarget {
8049 int64_t Offset;
8051 };
8053
8054 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
8056 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
8057 if (ElemBase != Base)
8058 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
8059 "to the same GlobalValue");
8060
8061 SDValue Val = getValue(I.getArgOperand(Op + 1));
8062 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
8063 if (!GA)
8065 "llvm.icall.branch.funnel operand must be a GlobalValue");
8066 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
8067 GA->getGlobal(), sdl, Val.getValueType(),
8068 GA->getOffset())});
8069 }
8070 llvm::sort(Targets,
8071 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
8072 return T1.Offset < T2.Offset;
8073 });
8074
8075 for (auto &T : Targets) {
8076 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
8077 Ops.push_back(T.Target);
8078 }
8079
8080 Ops.push_back(DAG.getRoot()); // Chain
8081 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
8082 MVT::Other, Ops),
8083 0);
8084 DAG.setRoot(N);
8085 setValue(&I, N);
8086 HasTailCall = true;
8087 return;
8088 }
8089
8090 case Intrinsic::wasm_landingpad_index:
8091 // Information this intrinsic contained has been transferred to
8092 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
8093 // delete it now.
8094 return;
8095
8096 case Intrinsic::aarch64_settag:
8097 case Intrinsic::aarch64_settag_zero: {
8098 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8099 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
8101 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
8102 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
8103 ZeroMemory);
8104 DAG.setRoot(Val);
8105 setValue(&I, Val);
8106 return;
8107 }
8108 case Intrinsic::amdgcn_cs_chain: {
8109 // At this point we don't care if it's amdgpu_cs_chain or
8110 // amdgpu_cs_chain_preserve.
8112
8113 Type *RetTy = I.getType();
8114 assert(RetTy->isVoidTy() && "Should not return");
8115
8116 SDValue Callee = getValue(I.getOperand(0));
8117
8118 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
8119 // We'll also tack the value of the EXEC mask at the end.
8121 Args.reserve(3);
8122
8123 for (unsigned Idx : {2, 3, 1}) {
8124 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8125 I.getOperand(Idx)->getType());
8126 Arg.setAttributes(&I, Idx);
8127 Args.push_back(Arg);
8128 }
8129
8130 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
8131 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
8132 Args[2].IsInReg = true; // EXEC should be inreg
8133
8134 // Forward the flags and any additional arguments.
8135 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
8136 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8137 I.getOperand(Idx)->getType());
8138 Arg.setAttributes(&I, Idx);
8139 Args.push_back(Arg);
8140 }
8141
8142 TargetLowering::CallLoweringInfo CLI(DAG);
8143 CLI.setDebugLoc(getCurSDLoc())
8144 .setChain(getRoot())
8145 .setCallee(CC, RetTy, Callee, std::move(Args))
8146 .setNoReturn(true)
8147 .setTailCall(true)
8148 .setConvergent(I.isConvergent());
8149 CLI.CB = &I;
8150 std::pair<SDValue, SDValue> Result =
8151 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
8152 (void)Result;
8153 assert(!Result.first.getNode() && !Result.second.getNode() &&
8154 "Should've lowered as tail call");
8155
8156 HasTailCall = true;
8157 return;
8158 }
8159 case Intrinsic::amdgcn_call_whole_wave: {
8161 bool isTailCall = I.isTailCall();
8162
8163 // The first argument is the callee. Skip it when assembling the call args.
8164 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
8165 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
8166 I.getArgOperand(Idx)->getType());
8167 Arg.setAttributes(&I, Idx);
8168
8169 // If we have an explicit sret argument that is an Instruction, (i.e., it
8170 // might point to function-local memory), we can't meaningfully tail-call.
8171 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
8172 isTailCall = false;
8173
8174 Args.push_back(Arg);
8175 }
8176
8177 SDValue ConvControlToken;
8178 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8179 auto *Token = Bundle->Inputs[0].get();
8180 ConvControlToken = getValue(Token);
8181 }
8182
8183 TargetLowering::CallLoweringInfo CLI(DAG);
8184 CLI.setDebugLoc(getCurSDLoc())
8185 .setChain(getRoot())
8186 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8187 getValue(I.getArgOperand(0)), std::move(Args))
8188 .setTailCall(isTailCall && canTailCall(I))
8189 .setIsPreallocated(
8190 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8191 .setConvergent(I.isConvergent())
8192 .setConvergenceControlToken(ConvControlToken);
8193 CLI.CB = &I;
8194
8195 std::pair<SDValue, SDValue> Result =
8196 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8197
8198 if (Result.first.getNode())
8199 setValue(&I, Result.first);
8200 return;
8201 }
8202 case Intrinsic::ptrmask: {
8203 SDValue Ptr = getValue(I.getOperand(0));
8204 SDValue Mask = getValue(I.getOperand(1));
8205
8206 // On arm64_32, pointers are 32 bits when stored in memory, but
8207 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8208 // match the index type, but the pointer is 64 bits, so the mask must be
8209 // zero-extended up to 64 bits to match the pointer.
8210 EVT PtrVT =
8211 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8212 EVT MemVT =
8213 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8214 assert(PtrVT == Ptr.getValueType());
8215 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8216 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8217 // 128-bit, so we have to pad the mask with ones for unused bits.
8218 auto HighOnes = DAG.getNode(
8219 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8220 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8221 PtrVT, sdl));
8222 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8223 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8224 } else if (Mask.getValueType() != PtrVT)
8225 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8226
8227 assert(Mask.getValueType() == PtrVT);
8228 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8229 return;
8230 }
8231 case Intrinsic::threadlocal_address: {
8232 setValue(&I, getValue(I.getOperand(0)));
8233 return;
8234 }
8235 case Intrinsic::get_active_lane_mask: {
8236 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8237 SDValue Index = getValue(I.getOperand(0));
8238 SDValue TripCount = getValue(I.getOperand(1));
8239 EVT ElementVT = Index.getValueType();
8240
8241 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8242 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8243 TripCount));
8244 return;
8245 }
8246
8247 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8248 CCVT.getVectorElementCount());
8249
8250 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8251 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8252 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8253 SDValue VectorInduction = DAG.getNode(
8254 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8255 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8256 VectorTripCount, ISD::CondCode::SETULT);
8257 setValue(&I, SetCC);
8258 return;
8259 }
8260 case Intrinsic::experimental_get_vector_length: {
8261 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8262 "Expected positive VF");
8263 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8264 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8265
8266 SDValue Count = getValue(I.getOperand(0));
8267 EVT CountVT = Count.getValueType();
8268
8269 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8270 visitTargetIntrinsic(I, Intrinsic);
8271 return;
8272 }
8273
8274 // Expand to a umin between the trip count and the maximum elements the type
8275 // can hold.
8276 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8277
8278 // Extend the trip count to at least the result VT.
8279 if (CountVT.bitsLT(VT)) {
8280 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8281 CountVT = VT;
8282 }
8283
8284 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8285 ElementCount::get(VF, IsScalable));
8286
8287 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8288 // Clip to the result type if needed.
8289 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8290
8291 setValue(&I, Trunc);
8292 return;
8293 }
8294 case Intrinsic::vector_partial_reduce_add: {
8295 SDValue Acc = getValue(I.getOperand(0));
8296 SDValue Input = getValue(I.getOperand(1));
8297 setValue(&I,
8298 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8299 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8300 return;
8301 }
8302 case Intrinsic::vector_partial_reduce_fadd: {
8303 SDValue Acc = getValue(I.getOperand(0));
8304 SDValue Input = getValue(I.getOperand(1));
8305 setValue(&I, DAG.getNode(
8306 ISD::PARTIAL_REDUCE_FMLA, sdl, Acc.getValueType(), Acc,
8307 Input, DAG.getConstantFP(1.0, sdl, Input.getValueType())));
8308 return;
8309 }
8310 case Intrinsic::experimental_cttz_elts: {
8311 auto DL = getCurSDLoc();
8312 SDValue Op = getValue(I.getOperand(0));
8313 EVT OpVT = Op.getValueType();
8314
8315 if (!TLI.shouldExpandCttzElements(OpVT)) {
8316 visitTargetIntrinsic(I, Intrinsic);
8317 return;
8318 }
8319
8320 if (OpVT.getScalarType() != MVT::i1) {
8321 // Compare the input vector elements to zero & use to count trailing zeros
8322 SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8323 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8324 OpVT.getVectorElementCount());
8325 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8326 }
8327
8328 // If the zero-is-poison flag is set, we can assume the upper limit
8329 // of the result is VF-1.
8330 bool ZeroIsPoison =
8331 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8332 ConstantRange VScaleRange(1, true); // Dummy value.
8333 if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8334 VScaleRange = getVScaleRange(I.getCaller(), 64);
8335 unsigned EltWidth = TLI.getBitWidthForCttzElements(
8336 I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8337
8338 MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8339
8340 // Create the new vector type & get the vector length
8341 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8342 OpVT.getVectorElementCount());
8343
8344 SDValue VL =
8345 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8346
8347 SDValue StepVec = DAG.getStepVector(DL, NewVT);
8348 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8349 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8350 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8351 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8352 SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8353 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8354
8355 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8356 SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
8357
8358 setValue(&I, Ret);
8359 return;
8360 }
8361 case Intrinsic::vector_insert: {
8362 SDValue Vec = getValue(I.getOperand(0));
8363 SDValue SubVec = getValue(I.getOperand(1));
8364 SDValue Index = getValue(I.getOperand(2));
8365
8366 // The intrinsic's index type is i64, but the SDNode requires an index type
8367 // suitable for the target. Convert the index as required.
8368 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8369 if (Index.getValueType() != VectorIdxTy)
8370 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8371
8372 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8373 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8374 Index));
8375 return;
8376 }
8377 case Intrinsic::vector_extract: {
8378 SDValue Vec = getValue(I.getOperand(0));
8379 SDValue Index = getValue(I.getOperand(1));
8380 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8381
8382 // The intrinsic's index type is i64, but the SDNode requires an index type
8383 // suitable for the target. Convert the index as required.
8384 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8385 if (Index.getValueType() != VectorIdxTy)
8386 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8387
8388 setValue(&I,
8389 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8390 return;
8391 }
8392 case Intrinsic::experimental_vector_match: {
8393 SDValue Op1 = getValue(I.getOperand(0));
8394 SDValue Op2 = getValue(I.getOperand(1));
8395 SDValue Mask = getValue(I.getOperand(2));
8396 EVT Op1VT = Op1.getValueType();
8397 EVT Op2VT = Op2.getValueType();
8398 EVT ResVT = Mask.getValueType();
8399 unsigned SearchSize = Op2VT.getVectorNumElements();
8400
8401 // If the target has native support for this vector match operation, lower
8402 // the intrinsic untouched; otherwise, expand it below.
8403 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8404 visitTargetIntrinsic(I, Intrinsic);
8405 return;
8406 }
8407
8408 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8409
8410 for (unsigned i = 0; i < SearchSize; ++i) {
8411 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8412 Op2VT.getVectorElementType(), Op2,
8413 DAG.getVectorIdxConstant(i, sdl));
8414 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8415 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8416 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8417 }
8418
8419 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8420 return;
8421 }
8422 case Intrinsic::vector_reverse:
8423 visitVectorReverse(I);
8424 return;
8425 case Intrinsic::vector_splice_left:
8426 case Intrinsic::vector_splice_right:
8427 visitVectorSplice(I);
8428 return;
8429 case Intrinsic::callbr_landingpad:
8430 visitCallBrLandingPad(I);
8431 return;
8432 case Intrinsic::vector_interleave2:
8433 visitVectorInterleave(I, 2);
8434 return;
8435 case Intrinsic::vector_interleave3:
8436 visitVectorInterleave(I, 3);
8437 return;
8438 case Intrinsic::vector_interleave4:
8439 visitVectorInterleave(I, 4);
8440 return;
8441 case Intrinsic::vector_interleave5:
8442 visitVectorInterleave(I, 5);
8443 return;
8444 case Intrinsic::vector_interleave6:
8445 visitVectorInterleave(I, 6);
8446 return;
8447 case Intrinsic::vector_interleave7:
8448 visitVectorInterleave(I, 7);
8449 return;
8450 case Intrinsic::vector_interleave8:
8451 visitVectorInterleave(I, 8);
8452 return;
8453 case Intrinsic::vector_deinterleave2:
8454 visitVectorDeinterleave(I, 2);
8455 return;
8456 case Intrinsic::vector_deinterleave3:
8457 visitVectorDeinterleave(I, 3);
8458 return;
8459 case Intrinsic::vector_deinterleave4:
8460 visitVectorDeinterleave(I, 4);
8461 return;
8462 case Intrinsic::vector_deinterleave5:
8463 visitVectorDeinterleave(I, 5);
8464 return;
8465 case Intrinsic::vector_deinterleave6:
8466 visitVectorDeinterleave(I, 6);
8467 return;
8468 case Intrinsic::vector_deinterleave7:
8469 visitVectorDeinterleave(I, 7);
8470 return;
8471 case Intrinsic::vector_deinterleave8:
8472 visitVectorDeinterleave(I, 8);
8473 return;
8474 case Intrinsic::experimental_vector_compress:
8475 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8476 getValue(I.getArgOperand(0)).getValueType(),
8477 getValue(I.getArgOperand(0)),
8478 getValue(I.getArgOperand(1)),
8479 getValue(I.getArgOperand(2)), Flags));
8480 return;
8481 case Intrinsic::experimental_convergence_anchor:
8482 case Intrinsic::experimental_convergence_entry:
8483 case Intrinsic::experimental_convergence_loop:
8484 visitConvergenceControl(I, Intrinsic);
8485 return;
8486 case Intrinsic::experimental_vector_histogram_add: {
8487 visitVectorHistogram(I, Intrinsic);
8488 return;
8489 }
8490 case Intrinsic::experimental_vector_extract_last_active: {
8491 visitVectorExtractLastActive(I, Intrinsic);
8492 return;
8493 }
8494 case Intrinsic::loop_dependence_war_mask:
8495 setValue(&I,
8497 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8498 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8499 DAG.getConstant(0, sdl, MVT::i64)));
8500 return;
8501 case Intrinsic::loop_dependence_raw_mask:
8502 setValue(&I,
8504 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8505 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8506 DAG.getConstant(0, sdl, MVT::i64)));
8507 return;
8508 }
8509}
8510
8511void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
8513 assert(Result.getNode()->getNumValues() == 2);
8514 SDValue OutChain = Result.getValue(1);
8515 assert(OutChain.getValueType() == MVT::Other);
8516
8517 // Instead of updating the root immediately, push the produced chain to the
8518 // appropriate list, deferring the update until the root is requested. In this
8519 // case, the nodes from the lists are chained using TokenFactor, indicating
8520 // that the operations are independent.
8521 //
8522 // In particular, the root is updated before any call that might access the
8523 // floating-point environment, except for constrained intrinsics.
8524 switch (EB) {
8527 PendingConstrainedFP.push_back(OutChain);
8528 break;
8530 PendingConstrainedFPStrict.push_back(OutChain);
8531 break;
8532 }
8533}
8534
8535void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8536 const ConstrainedFPIntrinsic &FPI) {
8537 SDLoc sdl = getCurSDLoc();
8538
8539 // We do not need to serialize constrained FP intrinsics against
8540 // each other or against (nonvolatile) loads, so they can be
8541 // chained like loads.
8543 SDValue Chain = getFPOperationRoot(EB);
8545 Opers.push_back(Chain);
8546 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8547 Opers.push_back(getValue(FPI.getArgOperand(I)));
8548
8549 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8550 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8551 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8552
8553 SDNodeFlags Flags;
8555 Flags.setNoFPExcept(true);
8556
8557 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8558 Flags.copyFMF(*FPOp);
8559
8560 unsigned Opcode;
8561 switch (FPI.getIntrinsicID()) {
8562 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8563#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8564 case Intrinsic::INTRINSIC: \
8565 Opcode = ISD::STRICT_##DAGN; \
8566 break;
8567#include "llvm/IR/ConstrainedOps.def"
8568 case Intrinsic::experimental_constrained_fmuladd: {
8569 Opcode = ISD::STRICT_FMA;
8570 // Break fmuladd into fmul and fadd.
8571 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8572 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8573 Opers.pop_back();
8574 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8575 pushFPOpOutChain(Mul, EB);
8576 Opcode = ISD::STRICT_FADD;
8577 Opers.clear();
8578 Opers.push_back(Mul.getValue(1));
8579 Opers.push_back(Mul.getValue(0));
8580 Opers.push_back(getValue(FPI.getArgOperand(2)));
8581 }
8582 break;
8583 }
8584 }
8585
8586 // A few strict DAG nodes carry additional operands that are not
8587 // set up by the default code above.
8588 switch (Opcode) {
8589 default: break;
8591 Opers.push_back(
8592 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8593 break;
8594 case ISD::STRICT_FSETCC:
8595 case ISD::STRICT_FSETCCS: {
8596 auto *FPCmp = dyn_cast<ConstrainedFPCmpIntrinsic>(&FPI);
8597 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8598 if (DAG.isKnownNeverNaN(Opers[1]) && DAG.isKnownNeverNaN(Opers[2]))
8599 Condition = getFCmpCodeWithoutNaN(Condition);
8600 Opers.push_back(DAG.getCondCode(Condition));
8601 break;
8602 }
8603 }
8604
8605 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8606 pushFPOpOutChain(Result, EB);
8607
8608 SDValue FPResult = Result.getValue(0);
8609 setValue(&FPI, FPResult);
8610}
8611
8612static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8613 std::optional<unsigned> ResOPC;
8614 switch (VPIntrin.getIntrinsicID()) {
8615 case Intrinsic::vp_ctlz: {
8616 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8617 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8618 break;
8619 }
8620 case Intrinsic::vp_cttz: {
8621 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8622 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8623 break;
8624 }
8625 case Intrinsic::vp_cttz_elts: {
8626 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8627 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8628 break;
8629 }
8630#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8631 case Intrinsic::VPID: \
8632 ResOPC = ISD::VPSD; \
8633 break;
8634#include "llvm/IR/VPIntrinsics.def"
8635 }
8636
8637 if (!ResOPC)
8639 "Inconsistency: no SDNode available for this VPIntrinsic!");
8640
8641 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8642 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8643 if (VPIntrin.getFastMathFlags().allowReassoc())
8644 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8645 : ISD::VP_REDUCE_FMUL;
8646 }
8647
8648 return *ResOPC;
8649}
8650
8651void SelectionDAGBuilder::visitVPLoad(
8652 const VPIntrinsic &VPIntrin, EVT VT,
8653 const SmallVectorImpl<SDValue> &OpValues) {
8654 SDLoc DL = getCurSDLoc();
8655 Value *PtrOperand = VPIntrin.getArgOperand(0);
8656 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8657 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8658 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8659 SDValue LD;
8660 // Do not serialize variable-length loads of constant memory with
8661 // anything.
8662 if (!Alignment)
8663 Alignment = DAG.getEVTAlign(VT);
8664 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8665 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8666 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8667 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8668 MachineMemOperand::Flags MMOFlags =
8669 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8670 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8671 MachinePointerInfo(PtrOperand), MMOFlags,
8672 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8673 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8674 MMO, false /*IsExpanding */);
8675 if (AddToChain)
8676 PendingLoads.push_back(LD.getValue(1));
8677 setValue(&VPIntrin, LD);
8678}
8679
8680void SelectionDAGBuilder::visitVPLoadFF(
8681 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8682 const SmallVectorImpl<SDValue> &OpValues) {
8683 assert(OpValues.size() == 3 && "Unexpected number of operands");
8684 SDLoc DL = getCurSDLoc();
8685 Value *PtrOperand = VPIntrin.getArgOperand(0);
8686 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8687 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8688 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8689 SDValue LD;
8690 // Do not serialize variable-length loads of constant memory with
8691 // anything.
8692 if (!Alignment)
8693 Alignment = DAG.getEVTAlign(VT);
8694 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8695 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8696 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8697 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8698 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8699 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8700 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8701 MMO);
8702 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8703 if (AddToChain)
8704 PendingLoads.push_back(LD.getValue(2));
8705 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8706}
8707
8708void SelectionDAGBuilder::visitVPGather(
8709 const VPIntrinsic &VPIntrin, EVT VT,
8710 const SmallVectorImpl<SDValue> &OpValues) {
8711 SDLoc DL = getCurSDLoc();
8712 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8713 Value *PtrOperand = VPIntrin.getArgOperand(0);
8714 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8715 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8716 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8717 SDValue LD;
8718 if (!Alignment)
8719 Alignment = DAG.getEVTAlign(VT.getScalarType());
8720 unsigned AS =
8721 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8722 MachineMemOperand::Flags MMOFlags =
8723 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8724 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8725 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8726 *Alignment, AAInfo, Ranges);
8727 SDValue Base, Index, Scale;
8728 bool UniformBase =
8729 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8730 VT.getScalarStoreSize());
8731 if (!UniformBase) {
8732 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8733 Index = getValue(PtrOperand);
8734 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8735 }
8736 EVT IdxVT = Index.getValueType();
8737 EVT EltTy = IdxVT.getVectorElementType();
8738 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8739 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8740 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8741 }
8742 LD = DAG.getGatherVP(
8743 DAG.getVTList(VT, MVT::Other), VT, DL,
8744 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8746 PendingLoads.push_back(LD.getValue(1));
8747 setValue(&VPIntrin, LD);
8748}
8749
8750void SelectionDAGBuilder::visitVPStore(
8751 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8752 SDLoc DL = getCurSDLoc();
8753 Value *PtrOperand = VPIntrin.getArgOperand(1);
8754 EVT VT = OpValues[0].getValueType();
8755 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8756 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8757 SDValue ST;
8758 if (!Alignment)
8759 Alignment = DAG.getEVTAlign(VT);
8760 SDValue Ptr = OpValues[1];
8761 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8762 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8763 MachineMemOperand::Flags MMOFlags =
8764 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8765 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8766 MachinePointerInfo(PtrOperand), MMOFlags,
8767 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8768 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8769 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8770 /* IsTruncating */ false, /*IsCompressing*/ false);
8771 DAG.setRoot(ST);
8772 setValue(&VPIntrin, ST);
8773}
8774
8775void SelectionDAGBuilder::visitVPScatter(
8776 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8777 SDLoc DL = getCurSDLoc();
8778 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8779 Value *PtrOperand = VPIntrin.getArgOperand(1);
8780 EVT VT = OpValues[0].getValueType();
8781 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8782 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8783 SDValue ST;
8784 if (!Alignment)
8785 Alignment = DAG.getEVTAlign(VT.getScalarType());
8786 unsigned AS =
8787 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8788 MachineMemOperand::Flags MMOFlags =
8789 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8790 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8791 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8792 *Alignment, AAInfo);
8793 SDValue Base, Index, Scale;
8794 bool UniformBase =
8795 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8796 VT.getScalarStoreSize());
8797 if (!UniformBase) {
8798 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8799 Index = getValue(PtrOperand);
8800 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8801 }
8802 EVT IdxVT = Index.getValueType();
8803 EVT EltTy = IdxVT.getVectorElementType();
8804 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8805 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8806 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8807 }
8808 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8809 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8810 OpValues[2], OpValues[3]},
8811 MMO, ISD::SIGNED_SCALED);
8812 DAG.setRoot(ST);
8813 setValue(&VPIntrin, ST);
8814}
8815
8816void SelectionDAGBuilder::visitVPStridedLoad(
8817 const VPIntrinsic &VPIntrin, EVT VT,
8818 const SmallVectorImpl<SDValue> &OpValues) {
8819 SDLoc DL = getCurSDLoc();
8820 Value *PtrOperand = VPIntrin.getArgOperand(0);
8821 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8822 if (!Alignment)
8823 Alignment = DAG.getEVTAlign(VT.getScalarType());
8824 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8825 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8826 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8827 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8828 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8829 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8830 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8831 MachineMemOperand::Flags MMOFlags =
8832 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8833 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8834 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8835 *Alignment, AAInfo, Ranges);
8836
8837 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8838 OpValues[2], OpValues[3], MMO,
8839 false /*IsExpanding*/);
8840
8841 if (AddToChain)
8842 PendingLoads.push_back(LD.getValue(1));
8843 setValue(&VPIntrin, LD);
8844}
8845
8846void SelectionDAGBuilder::visitVPStridedStore(
8847 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8848 SDLoc DL = getCurSDLoc();
8849 Value *PtrOperand = VPIntrin.getArgOperand(1);
8850 EVT VT = OpValues[0].getValueType();
8851 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8852 if (!Alignment)
8853 Alignment = DAG.getEVTAlign(VT.getScalarType());
8854 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8855 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8856 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8857 MachineMemOperand::Flags MMOFlags =
8858 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8859 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8860 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8861 *Alignment, AAInfo);
8862
8863 SDValue ST = DAG.getStridedStoreVP(
8864 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8865 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8866 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8867 /*IsCompressing*/ false);
8868
8869 DAG.setRoot(ST);
8870 setValue(&VPIntrin, ST);
8871}
8872
8873void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8874 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8875 SDLoc DL = getCurSDLoc();
8876
8877 ISD::CondCode Condition;
8879 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8880 Condition = IsFP ? getFCmpCondCode(CondCode) : getICmpCondCode(CondCode);
8881
8882 SDValue Op1 = getValue(VPIntrin.getOperand(0));
8883 SDValue Op2 = getValue(VPIntrin.getOperand(1));
8884 // #2 is the condition code
8885 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8886 SDValue EVL = getValue(VPIntrin.getOperand(4));
8887 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8888 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8889 "Unexpected target EVL type");
8890 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8891
8892 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8893 VPIntrin.getType());
8894 if (DAG.isKnownNeverNaN(Op1) && DAG.isKnownNeverNaN(Op2))
8895 Condition = getFCmpCodeWithoutNaN(Condition);
8896 setValue(&VPIntrin,
8897 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8898}
8899
8900void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8901 const VPIntrinsic &VPIntrin) {
8902 SDLoc DL = getCurSDLoc();
8903 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8904
8905 auto IID = VPIntrin.getIntrinsicID();
8906
8907 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8908 return visitVPCmp(*CmpI);
8909
8910 SmallVector<EVT, 4> ValueVTs;
8911 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8912 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8913 SDVTList VTs = DAG.getVTList(ValueVTs);
8914
8915 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8916
8917 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8918 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8919 "Unexpected target EVL type");
8920
8921 // Request operands.
8922 SmallVector<SDValue, 7> OpValues;
8923 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8924 auto Op = getValue(VPIntrin.getArgOperand(I));
8925 if (I == EVLParamPos)
8926 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8927 OpValues.push_back(Op);
8928 }
8929
8930 switch (Opcode) {
8931 default: {
8932 SDNodeFlags SDFlags;
8933 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8934 SDFlags.copyFMF(*FPMO);
8935 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8936 setValue(&VPIntrin, Result);
8937 break;
8938 }
8939 case ISD::VP_LOAD:
8940 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8941 break;
8942 case ISD::VP_LOAD_FF:
8943 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8944 break;
8945 case ISD::VP_GATHER:
8946 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8947 break;
8948 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8949 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8950 break;
8951 case ISD::VP_STORE:
8952 visitVPStore(VPIntrin, OpValues);
8953 break;
8954 case ISD::VP_SCATTER:
8955 visitVPScatter(VPIntrin, OpValues);
8956 break;
8957 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8958 visitVPStridedStore(VPIntrin, OpValues);
8959 break;
8960 case ISD::VP_FMULADD: {
8961 assert(OpValues.size() == 5 && "Unexpected number of operands");
8962 SDNodeFlags SDFlags;
8963 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8964 SDFlags.copyFMF(*FPMO);
8965 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8966 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8967 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8968 } else {
8969 SDValue Mul = DAG.getNode(
8970 ISD::VP_FMUL, DL, VTs,
8971 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8972 SDValue Add =
8973 DAG.getNode(ISD::VP_FADD, DL, VTs,
8974 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8975 setValue(&VPIntrin, Add);
8976 }
8977 break;
8978 }
8979 case ISD::VP_IS_FPCLASS: {
8980 const DataLayout DLayout = DAG.getDataLayout();
8981 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8982 auto Constant = OpValues[1]->getAsZExtVal();
8983 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8984 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8985 {OpValues[0], Check, OpValues[2], OpValues[3]});
8986 setValue(&VPIntrin, V);
8987 return;
8988 }
8989 case ISD::VP_INTTOPTR: {
8990 SDValue N = OpValues[0];
8991 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8992 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8993 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8994 OpValues[2]);
8995 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8996 OpValues[2]);
8997 setValue(&VPIntrin, N);
8998 break;
8999 }
9000 case ISD::VP_PTRTOINT: {
9001 SDValue N = OpValues[0];
9002 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9003 VPIntrin.getType());
9004 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
9005 VPIntrin.getOperand(0)->getType());
9006 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
9007 OpValues[2]);
9008 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
9009 OpValues[2]);
9010 setValue(&VPIntrin, N);
9011 break;
9012 }
9013 case ISD::VP_ABS:
9014 case ISD::VP_CTLZ:
9015 case ISD::VP_CTLZ_ZERO_UNDEF:
9016 case ISD::VP_CTTZ:
9017 case ISD::VP_CTTZ_ZERO_UNDEF:
9018 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
9019 case ISD::VP_CTTZ_ELTS: {
9020 SDValue Result =
9021 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
9022 setValue(&VPIntrin, Result);
9023 break;
9024 }
9025 }
9026}
9027
9028SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
9029 const BasicBlock *EHPadBB,
9030 MCSymbol *&BeginLabel) {
9031 MachineFunction &MF = DAG.getMachineFunction();
9032
9033 // Insert a label before the invoke call to mark the try range. This can be
9034 // used to detect deletion of the invoke via the MachineModuleInfo.
9035 BeginLabel = MF.getContext().createTempSymbol();
9036
9037 // For SjLj, keep track of which landing pads go with which invokes
9038 // so as to maintain the ordering of pads in the LSDA.
9039 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
9040 if (CallSiteIndex) {
9041 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
9042 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
9043
9044 // Now that the call site is handled, stop tracking it.
9045 FuncInfo.setCurrentCallSite(0);
9046 }
9047
9048 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
9049}
9050
9051SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
9052 const BasicBlock *EHPadBB,
9053 MCSymbol *BeginLabel) {
9054 assert(BeginLabel && "BeginLabel should've been set");
9055
9056 MachineFunction &MF = DAG.getMachineFunction();
9057
9058 // Insert a label at the end of the invoke call to mark the try range. This
9059 // can be used to detect deletion of the invoke via the MachineModuleInfo.
9060 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
9061 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
9062
9063 // Inform MachineModuleInfo of range.
9064 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
9065 // There is a platform (e.g. wasm) that uses funclet style IR but does not
9066 // actually use outlined funclets and their LSDA info style.
9067 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
9068 assert(II && "II should've been set");
9069 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
9070 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
9071 } else if (!isScopedEHPersonality(Pers)) {
9072 assert(EHPadBB);
9073 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
9074 }
9075
9076 return Chain;
9077}
9078
9079std::pair<SDValue, SDValue>
9081 const BasicBlock *EHPadBB) {
9082 MCSymbol *BeginLabel = nullptr;
9083
9084 if (EHPadBB) {
9085 // Both PendingLoads and PendingExports must be flushed here;
9086 // this call might not return.
9087 (void)getRoot();
9088 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
9089 CLI.setChain(getRoot());
9090 }
9091
9092 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9093 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
9094
9095 assert((CLI.IsTailCall || Result.second.getNode()) &&
9096 "Non-null chain expected with non-tail call!");
9097 assert((Result.second.getNode() || !Result.first.getNode()) &&
9098 "Null value expected with tail call!");
9099
9100 if (!Result.second.getNode()) {
9101 // As a special case, a null chain means that a tail call has been emitted
9102 // and the DAG root is already updated.
9103 HasTailCall = true;
9104
9105 // Since there's no actual continuation from this block, nothing can be
9106 // relying on us setting vregs for them.
9107 PendingExports.clear();
9108 } else {
9109 DAG.setRoot(Result.second);
9110 }
9111
9112 if (EHPadBB) {
9113 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
9114 BeginLabel));
9115 Result.second = getRoot();
9116 }
9117
9118 return Result;
9119}
9120
9122 bool isMustTailCall = CB.isMustTailCall();
9123
9124 // Avoid emitting tail calls in functions with the disable-tail-calls
9125 // attribute.
9126 const Function *Caller = CB.getParent()->getParent();
9127 if (!isMustTailCall &&
9128 Caller->getFnAttribute("disable-tail-calls").getValueAsBool())
9129 return false;
9130
9131 // We can't tail call inside a function with a swifterror argument. Lowering
9132 // does not support this yet. It would have to move into the swifterror
9133 // register before the call.
9134 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
9135 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
9136 return false;
9137
9138 // Check if target-independent constraints permit a tail call here.
9139 // Target-dependent constraints are checked within TLI->LowerCallTo.
9140 return isInTailCallPosition(CB, DAG.getTarget());
9141}
9142
9144 bool isTailCall, bool isMustTailCall,
9145 const BasicBlock *EHPadBB,
9146 const TargetLowering::PtrAuthInfo *PAI) {
9147 auto &DL = DAG.getDataLayout();
9148 FunctionType *FTy = CB.getFunctionType();
9149 Type *RetTy = CB.getType();
9150
9152 Args.reserve(CB.arg_size());
9153
9154 const Value *SwiftErrorVal = nullptr;
9155 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9156
9157 if (isTailCall)
9158 isTailCall = canTailCall(CB);
9159
9160 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
9161 const Value *V = *I;
9162
9163 // Skip empty types
9164 if (V->getType()->isEmptyTy())
9165 continue;
9166
9167 SDValue ArgNode = getValue(V);
9168 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
9169 Entry.setAttributes(&CB, I - CB.arg_begin());
9170
9171 // Use swifterror virtual register as input to the call.
9172 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
9173 SwiftErrorVal = V;
9174 // We find the virtual register for the actual swifterror argument.
9175 // Instead of using the Value, we use the virtual register instead.
9176 Entry.Node =
9177 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
9178 EVT(TLI.getPointerTy(DL)));
9179 }
9180
9181 Args.push_back(Entry);
9182
9183 // If we have an explicit sret argument that is an Instruction, (i.e., it
9184 // might point to function-local memory), we can't meaningfully tail-call.
9185 if (Entry.IsSRet && isa<Instruction>(V))
9186 isTailCall = false;
9187 }
9188
9189 // If call site has a cfguardtarget operand bundle, create and add an
9190 // additional ArgListEntry.
9191 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9192 Value *V = Bundle->Inputs[0];
9194 Entry.IsCFGuardTarget = true;
9195 Args.push_back(Entry);
9196 }
9197
9198 // Disable tail calls if there is an swifterror argument. Targets have not
9199 // been updated to support tail calls.
9200 if (TLI.supportSwiftError() && SwiftErrorVal)
9201 isTailCall = false;
9202
9203 ConstantInt *CFIType = nullptr;
9204 if (CB.isIndirectCall()) {
9205 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9206 if (!TLI.supportKCFIBundles())
9208 "Target doesn't support calls with kcfi operand bundles.");
9209 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9210 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9211 }
9212 }
9213
9214 SDValue ConvControlToken;
9215 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9216 auto *Token = Bundle->Inputs[0].get();
9217 ConvControlToken = getValue(Token);
9218 }
9219
9220 GlobalValue *DeactivationSymbol = nullptr;
9222 DeactivationSymbol = cast<GlobalValue>(Bundle->Inputs[0].get());
9223 }
9224
9227 .setChain(getRoot())
9228 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9229 .setTailCall(isTailCall)
9233 .setCFIType(CFIType)
9234 .setConvergenceControlToken(ConvControlToken)
9235 .setDeactivationSymbol(DeactivationSymbol);
9236
9237 // Set the pointer authentication info if we have it.
9238 if (PAI) {
9239 if (!TLI.supportPtrAuthBundles())
9241 "This target doesn't support calls with ptrauth operand bundles.");
9242 CLI.setPtrAuth(*PAI);
9243 }
9244
9245 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9246
9247 if (Result.first.getNode()) {
9248 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9249 Result.first = lowerNoFPClassToAssertNoFPClass(DAG, CB, Result.first);
9250 setValue(&CB, Result.first);
9251 }
9252
9253 // The last element of CLI.InVals has the SDValue for swifterror return.
9254 // Here we copy it to a virtual register and update SwiftErrorMap for
9255 // book-keeping.
9256 if (SwiftErrorVal && TLI.supportSwiftError()) {
9257 // Get the last element of InVals.
9258 SDValue Src = CLI.InVals.back();
9259 Register VReg =
9260 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9261 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9262 DAG.setRoot(CopyNode);
9263 }
9264}
9265
9266static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9267 SelectionDAGBuilder &Builder) {
9268 // Check to see if this load can be trivially constant folded, e.g. if the
9269 // input is from a string literal.
9270 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9271 // Cast pointer to the type we really want to load.
9272 Type *LoadTy =
9273 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9274 if (LoadVT.isVector())
9275 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9276 if (const Constant *LoadCst =
9277 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9278 LoadTy, Builder.DAG.getDataLayout()))
9279 return Builder.getValue(LoadCst);
9280 }
9281
9282 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9283 // still constant memory, the input chain can be the entry node.
9284 SDValue Root;
9285 bool ConstantMemory = false;
9286
9287 // Do not serialize (non-volatile) loads of constant memory with anything.
9288 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9289 Root = Builder.DAG.getEntryNode();
9290 ConstantMemory = true;
9291 } else {
9292 // Do not serialize non-volatile loads against each other.
9293 Root = Builder.DAG.getRoot();
9294 }
9295
9296 SDValue Ptr = Builder.getValue(PtrVal);
9297 SDValue LoadVal =
9298 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9299 MachinePointerInfo(PtrVal), Align(1));
9300
9301 if (!ConstantMemory)
9302 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9303 return LoadVal;
9304}
9305
9306/// Record the value for an instruction that produces an integer result,
9307/// converting the type where necessary.
9308void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9309 SDValue Value,
9310 bool IsSigned) {
9311 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9312 I.getType(), true);
9313 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9314 setValue(&I, Value);
9315}
9316
9317/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9318/// true and lower it. Otherwise return false, and it will be lowered like a
9319/// normal call.
9320/// The caller already checked that \p I calls the appropriate LibFunc with a
9321/// correct prototype.
9322bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9323 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9324 const Value *Size = I.getArgOperand(2);
9325 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9326 if (CSize && CSize->getZExtValue() == 0) {
9327 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9328 I.getType(), true);
9329 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9330 return true;
9331 }
9332
9333 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9334 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9335 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9336 getValue(Size), &I);
9337 if (Res.first.getNode()) {
9338 processIntegerCallValue(I, Res.first, true);
9339 PendingLoads.push_back(Res.second);
9340 return true;
9341 }
9342
9343 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9344 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9345 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9346 return false;
9347
9348 // If the target has a fast compare for the given size, it will return a
9349 // preferred load type for that size. Require that the load VT is legal and
9350 // that the target supports unaligned loads of that type. Otherwise, return
9351 // INVALID.
9352 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9353 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9354 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9355 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9356 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9357 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9358 // TODO: Check alignment of src and dest ptrs.
9359 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9360 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9361 if (!TLI.isTypeLegal(LVT) ||
9362 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9363 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9365 }
9366
9367 return LVT;
9368 };
9369
9370 // This turns into unaligned loads. We only do this if the target natively
9371 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9372 // we'll only produce a small number of byte loads.
9373 MVT LoadVT;
9374 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9375 switch (NumBitsToCompare) {
9376 default:
9377 return false;
9378 case 16:
9379 LoadVT = MVT::i16;
9380 break;
9381 case 32:
9382 LoadVT = MVT::i32;
9383 break;
9384 case 64:
9385 case 128:
9386 case 256:
9387 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9388 break;
9389 }
9390
9391 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9392 return false;
9393
9394 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9395 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9396
9397 // Bitcast to a wide integer type if the loads are vectors.
9398 if (LoadVT.isVector()) {
9399 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9400 LoadL = DAG.getBitcast(CmpVT, LoadL);
9401 LoadR = DAG.getBitcast(CmpVT, LoadR);
9402 }
9403
9404 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9405 processIntegerCallValue(I, Cmp, false);
9406 return true;
9407}
9408
9409/// See if we can lower a memchr call into an optimized form. If so, return
9410/// true and lower it. Otherwise return false, and it will be lowered like a
9411/// normal call.
9412/// The caller already checked that \p I calls the appropriate LibFunc with a
9413/// correct prototype.
9414bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9415 const Value *Src = I.getArgOperand(0);
9416 const Value *Char = I.getArgOperand(1);
9417 const Value *Length = I.getArgOperand(2);
9418
9419 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9420 std::pair<SDValue, SDValue> Res =
9421 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9422 getValue(Src), getValue(Char), getValue(Length),
9423 MachinePointerInfo(Src));
9424 if (Res.first.getNode()) {
9425 setValue(&I, Res.first);
9426 PendingLoads.push_back(Res.second);
9427 return true;
9428 }
9429
9430 return false;
9431}
9432
9433/// See if we can lower a memccpy call into an optimized form. If so, return
9434/// true and lower it, otherwise return false and it will be lowered like a
9435/// normal call.
9436/// The caller already checked that \p I calls the appropriate LibFunc with a
9437/// correct prototype.
9438bool SelectionDAGBuilder::visitMemCCpyCall(const CallInst &I) {
9439 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9440 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemccpy(
9441 DAG, getCurSDLoc(), DAG.getRoot(), getValue(I.getArgOperand(0)),
9442 getValue(I.getArgOperand(1)), getValue(I.getArgOperand(2)),
9443 getValue(I.getArgOperand(3)), &I);
9444
9445 if (Res.first) {
9446 processIntegerCallValue(I, Res.first, true);
9447 PendingLoads.push_back(Res.second);
9448 return true;
9449 }
9450 return false;
9451}
9452
9453/// See if we can lower a mempcpy call into an optimized form. If so, return
9454/// true and lower it. Otherwise return false, and it will be lowered like a
9455/// normal call.
9456/// The caller already checked that \p I calls the appropriate LibFunc with a
9457/// correct prototype.
9458bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9459 SDValue Dst = getValue(I.getArgOperand(0));
9460 SDValue Src = getValue(I.getArgOperand(1));
9461 SDValue Size = getValue(I.getArgOperand(2));
9462
9463 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9464 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9465 // DAG::getMemcpy needs Alignment to be defined.
9466 Align Alignment = std::min(DstAlign, SrcAlign);
9467
9468 SDLoc sdl = getCurSDLoc();
9469
9470 // In the mempcpy context we need to pass in a false value for isTailCall
9471 // because the return pointer needs to be adjusted by the size of
9472 // the copied memory.
9473 SDValue Root = getMemoryRoot();
9474 SDValue MC = DAG.getMemcpy(
9475 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9476 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9477 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9478 assert(MC.getNode() != nullptr &&
9479 "** memcpy should not be lowered as TailCall in mempcpy context **");
9480 DAG.setRoot(MC);
9481
9482 // Check if Size needs to be truncated or extended.
9483 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9484
9485 // Adjust return pointer to point just past the last dst byte.
9486 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9487 setValue(&I, DstPlusSize);
9488 return true;
9489}
9490
9491/// See if we can lower a strcpy call into an optimized form. If so, return
9492/// true and lower it, otherwise return false and it will be lowered like a
9493/// normal call.
9494/// The caller already checked that \p I calls the appropriate LibFunc with a
9495/// correct prototype.
9496bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9497 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9498
9499 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9500 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrcpy(
9501 DAG, getCurSDLoc(), getRoot(), getValue(Arg0), getValue(Arg1),
9502 MachinePointerInfo(Arg0), MachinePointerInfo(Arg1), isStpcpy, &I);
9503 if (Res.first.getNode()) {
9504 setValue(&I, Res.first);
9505 DAG.setRoot(Res.second);
9506 return true;
9507 }
9508
9509 return false;
9510}
9511
9512/// See if we can lower a strcmp call into an optimized form. If so, return
9513/// true and lower it, otherwise return false and it will be lowered like a
9514/// normal call.
9515/// The caller already checked that \p I calls the appropriate LibFunc with a
9516/// correct prototype.
9517bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9518 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9519
9520 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9521 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrcmp(
9522 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), getValue(Arg1),
9523 MachinePointerInfo(Arg0), MachinePointerInfo(Arg1), &I);
9524 if (Res.first.getNode()) {
9525 processIntegerCallValue(I, Res.first, true);
9526 PendingLoads.push_back(Res.second);
9527 return true;
9528 }
9529
9530 return false;
9531}
9532
9533/// See if we can lower a strlen call into an optimized form. If so, return
9534/// true and lower it, otherwise return false and it will be lowered like a
9535/// normal call.
9536/// The caller already checked that \p I calls the appropriate LibFunc with a
9537/// correct prototype.
9538bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9539 const Value *Arg0 = I.getArgOperand(0);
9540
9541 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9542 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrlen(
9543 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), &I);
9544 if (Res.first.getNode()) {
9545 processIntegerCallValue(I, Res.first, false);
9546 PendingLoads.push_back(Res.second);
9547 return true;
9548 }
9549
9550 return false;
9551}
9552
9553/// See if we can lower a strnlen call into an optimized form. If so, return
9554/// true and lower it, otherwise return false and it will be lowered like a
9555/// normal call.
9556/// The caller already checked that \p I calls the appropriate LibFunc with a
9557/// correct prototype.
9558bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9559 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9560
9561 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9562 std::pair<SDValue, SDValue> Res =
9563 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9564 getValue(Arg0), getValue(Arg1),
9565 MachinePointerInfo(Arg0));
9566 if (Res.first.getNode()) {
9567 processIntegerCallValue(I, Res.first, false);
9568 PendingLoads.push_back(Res.second);
9569 return true;
9570 }
9571
9572 return false;
9573}
9574
9575/// See if we can lower a Strstr call into an optimized form. If so, return
9576/// true and lower it, otherwise return false and it will be lowered like a
9577/// normal call.
9578/// The caller already checked that \p I calls the appropriate LibFunc with a
9579/// correct prototype.
9580bool SelectionDAGBuilder::visitStrstrCall(const CallInst &I) {
9581 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9582 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9583 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrstr(
9584 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), getValue(Arg1), &I);
9585 if (Res.first) {
9586 processIntegerCallValue(I, Res.first, false);
9587 PendingLoads.push_back(Res.second);
9588 return true;
9589 }
9590 return false;
9591}
9592
9593/// See if we can lower a unary floating-point operation into an SDNode with
9594/// the specified Opcode. If so, return true and lower it, otherwise return
9595/// false and it will be lowered like a normal call.
9596/// The caller already checked that \p I calls the appropriate LibFunc with a
9597/// correct prototype.
9598bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9599 unsigned Opcode) {
9600 // We already checked this call's prototype; verify it doesn't modify errno.
9601 // Do not perform optimizations for call sites that require strict
9602 // floating-point semantics.
9603 if (!I.onlyReadsMemory() || I.isStrictFP())
9604 return false;
9605
9606 SDNodeFlags Flags;
9607 Flags.copyFMF(cast<FPMathOperator>(I));
9608
9609 SDValue Tmp = getValue(I.getArgOperand(0));
9610 setValue(&I,
9611 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9612 return true;
9613}
9614
9615/// See if we can lower a binary floating-point operation into an SDNode with
9616/// the specified Opcode. If so, return true and lower it. Otherwise return
9617/// false, and it will be lowered like a normal call.
9618/// The caller already checked that \p I calls the appropriate LibFunc with a
9619/// correct prototype.
9620bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9621 unsigned Opcode) {
9622 // We already checked this call's prototype; verify it doesn't modify errno.
9623 // Do not perform optimizations for call sites that require strict
9624 // floating-point semantics.
9625 if (!I.onlyReadsMemory() || I.isStrictFP())
9626 return false;
9627
9628 SDNodeFlags Flags;
9629 Flags.copyFMF(cast<FPMathOperator>(I));
9630
9631 SDValue Tmp0 = getValue(I.getArgOperand(0));
9632 SDValue Tmp1 = getValue(I.getArgOperand(1));
9633 EVT VT = Tmp0.getValueType();
9634 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9635 return true;
9636}
9637
9638void SelectionDAGBuilder::visitCall(const CallInst &I) {
9639 // Handle inline assembly differently.
9640 if (I.isInlineAsm()) {
9641 visitInlineAsm(I);
9642 return;
9643 }
9644
9646
9647 if (Function *F = I.getCalledFunction()) {
9648 if (F->isDeclaration()) {
9649 // Is this an LLVM intrinsic?
9650 if (unsigned IID = F->getIntrinsicID()) {
9651 visitIntrinsicCall(I, IID);
9652 return;
9653 }
9654 }
9655
9656 // Check for well-known libc/libm calls. If the function is internal, it
9657 // can't be a library call. Don't do the check if marked as nobuiltin for
9658 // some reason.
9659 // This code should not handle libcalls that are already canonicalized to
9660 // intrinsics by the middle-end.
9661 LibFunc Func;
9662 if (!I.isNoBuiltin() && !F->hasLocalLinkage() && F->hasName() &&
9663 LibInfo->getLibFunc(*F, Func) && LibInfo->hasOptimizedCodeGen(Func)) {
9664 switch (Func) {
9665 default: break;
9666 case LibFunc_bcmp:
9667 if (visitMemCmpBCmpCall(I))
9668 return;
9669 break;
9670 case LibFunc_copysign:
9671 case LibFunc_copysignf:
9672 case LibFunc_copysignl:
9673 // We already checked this call's prototype; verify it doesn't modify
9674 // errno.
9675 if (I.onlyReadsMemory()) {
9676 SDValue LHS = getValue(I.getArgOperand(0));
9677 SDValue RHS = getValue(I.getArgOperand(1));
9679 LHS.getValueType(), LHS, RHS));
9680 return;
9681 }
9682 break;
9683 case LibFunc_sin:
9684 case LibFunc_sinf:
9685 case LibFunc_sinl:
9686 if (visitUnaryFloatCall(I, ISD::FSIN))
9687 return;
9688 break;
9689 case LibFunc_cos:
9690 case LibFunc_cosf:
9691 case LibFunc_cosl:
9692 if (visitUnaryFloatCall(I, ISD::FCOS))
9693 return;
9694 break;
9695 case LibFunc_tan:
9696 case LibFunc_tanf:
9697 case LibFunc_tanl:
9698 if (visitUnaryFloatCall(I, ISD::FTAN))
9699 return;
9700 break;
9701 case LibFunc_asin:
9702 case LibFunc_asinf:
9703 case LibFunc_asinl:
9704 if (visitUnaryFloatCall(I, ISD::FASIN))
9705 return;
9706 break;
9707 case LibFunc_acos:
9708 case LibFunc_acosf:
9709 case LibFunc_acosl:
9710 if (visitUnaryFloatCall(I, ISD::FACOS))
9711 return;
9712 break;
9713 case LibFunc_atan:
9714 case LibFunc_atanf:
9715 case LibFunc_atanl:
9716 if (visitUnaryFloatCall(I, ISD::FATAN))
9717 return;
9718 break;
9719 case LibFunc_atan2:
9720 case LibFunc_atan2f:
9721 case LibFunc_atan2l:
9722 if (visitBinaryFloatCall(I, ISD::FATAN2))
9723 return;
9724 break;
9725 case LibFunc_sinh:
9726 case LibFunc_sinhf:
9727 case LibFunc_sinhl:
9728 if (visitUnaryFloatCall(I, ISD::FSINH))
9729 return;
9730 break;
9731 case LibFunc_cosh:
9732 case LibFunc_coshf:
9733 case LibFunc_coshl:
9734 if (visitUnaryFloatCall(I, ISD::FCOSH))
9735 return;
9736 break;
9737 case LibFunc_tanh:
9738 case LibFunc_tanhf:
9739 case LibFunc_tanhl:
9740 if (visitUnaryFloatCall(I, ISD::FTANH))
9741 return;
9742 break;
9743 case LibFunc_sqrt:
9744 case LibFunc_sqrtf:
9745 case LibFunc_sqrtl:
9746 case LibFunc_sqrt_finite:
9747 case LibFunc_sqrtf_finite:
9748 case LibFunc_sqrtl_finite:
9749 if (visitUnaryFloatCall(I, ISD::FSQRT))
9750 return;
9751 break;
9752 case LibFunc_log2:
9753 case LibFunc_log2f:
9754 case LibFunc_log2l:
9755 if (visitUnaryFloatCall(I, ISD::FLOG2))
9756 return;
9757 break;
9758 case LibFunc_exp2:
9759 case LibFunc_exp2f:
9760 case LibFunc_exp2l:
9761 if (visitUnaryFloatCall(I, ISD::FEXP2))
9762 return;
9763 break;
9764 case LibFunc_exp10:
9765 case LibFunc_exp10f:
9766 case LibFunc_exp10l:
9767 if (visitUnaryFloatCall(I, ISD::FEXP10))
9768 return;
9769 break;
9770 case LibFunc_ldexp:
9771 case LibFunc_ldexpf:
9772 case LibFunc_ldexpl:
9773 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9774 return;
9775 break;
9776 case LibFunc_strstr:
9777 if (visitStrstrCall(I))
9778 return;
9779 break;
9780 case LibFunc_memcmp:
9781 if (visitMemCmpBCmpCall(I))
9782 return;
9783 break;
9784 case LibFunc_memccpy:
9785 if (visitMemCCpyCall(I))
9786 return;
9787 break;
9788 case LibFunc_mempcpy:
9789 if (visitMemPCpyCall(I))
9790 return;
9791 break;
9792 case LibFunc_memchr:
9793 if (visitMemChrCall(I))
9794 return;
9795 break;
9796 case LibFunc_strcpy:
9797 if (visitStrCpyCall(I, false))
9798 return;
9799 break;
9800 case LibFunc_stpcpy:
9801 if (visitStrCpyCall(I, true))
9802 return;
9803 break;
9804 case LibFunc_strcmp:
9805 if (visitStrCmpCall(I))
9806 return;
9807 break;
9808 case LibFunc_strlen:
9809 if (visitStrLenCall(I))
9810 return;
9811 break;
9812 case LibFunc_strnlen:
9813 if (visitStrNLenCall(I))
9814 return;
9815 break;
9816 }
9817 }
9818 }
9819
9820 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9821 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9822 return;
9823 }
9824
9825 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9826 // have to do anything here to lower funclet bundles.
9827 // CFGuardTarget bundles are lowered in LowerCallTo.
9829 I, "calls",
9834
9835 SDValue Callee = getValue(I.getCalledOperand());
9836
9837 if (I.hasDeoptState())
9838 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9839 else
9840 // Check if we can potentially perform a tail call. More detailed checking
9841 // is be done within LowerCallTo, after more information about the call is
9842 // known.
9843 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9844}
9845
9847 const CallBase &CB, const BasicBlock *EHPadBB) {
9848 auto PAB = CB.getOperandBundle("ptrauth");
9849 const Value *CalleeV = CB.getCalledOperand();
9850
9851 // Gather the call ptrauth data from the operand bundle:
9852 // [ i32 <key>, i64 <discriminator> ]
9853 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9854 const Value *Discriminator = PAB->Inputs[1];
9855
9856 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9857 assert(Discriminator->getType()->isIntegerTy(64) &&
9858 "Invalid ptrauth discriminator");
9859
9860 // Look through ptrauth constants to find the raw callee.
9861 // Do a direct unauthenticated call if we found it and everything matches.
9862 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9863 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9864 DAG.getDataLayout()))
9865 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9866 CB.isMustTailCall(), EHPadBB);
9867
9868 // Functions should never be ptrauth-called directly.
9869 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9870
9871 // Otherwise, do an authenticated indirect call.
9872 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9873 getValue(Discriminator)};
9874
9875 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9876 EHPadBB, &PAI);
9877}
9878
9879namespace {
9880
9881/// AsmOperandInfo - This contains information for each constraint that we are
9882/// lowering.
9883class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9884public:
9885 /// CallOperand - If this is the result output operand or a clobber
9886 /// this is null, otherwise it is the incoming operand to the CallInst.
9887 /// This gets modified as the asm is processed.
9888 SDValue CallOperand;
9889
9890 /// AssignedRegs - If this is a register or register class operand, this
9891 /// contains the set of register corresponding to the operand.
9892 RegsForValue AssignedRegs;
9893
9894 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9895 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9896 }
9897
9898 /// Whether or not this operand accesses memory
9899 bool hasMemory(const TargetLowering &TLI) const {
9900 // Indirect operand accesses access memory.
9901 if (isIndirect)
9902 return true;
9903
9904 for (const auto &Code : Codes)
9906 return true;
9907
9908 return false;
9909 }
9910};
9911
9912
9913} // end anonymous namespace
9914
9915/// Make sure that the output operand \p OpInfo and its corresponding input
9916/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9917/// out).
9918static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9919 SDISelAsmOperandInfo &MatchingOpInfo,
9920 SelectionDAG &DAG) {
9921 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9922 return;
9923
9925 const auto &TLI = DAG.getTargetLoweringInfo();
9926
9927 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9928 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9929 OpInfo.ConstraintVT);
9930 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9931 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9932 MatchingOpInfo.ConstraintVT);
9933 const bool OutOpIsIntOrFP =
9934 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9935 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9936 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9937 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9938 // FIXME: error out in a more elegant fashion
9939 report_fatal_error("Unsupported asm: input constraint"
9940 " with a matching output constraint of"
9941 " incompatible type!");
9942 }
9943 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9944}
9945
9946/// Get a direct memory input to behave well as an indirect operand.
9947/// This may introduce stores, hence the need for a \p Chain.
9948/// \return The (possibly updated) chain.
9949static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9950 SDISelAsmOperandInfo &OpInfo,
9951 SelectionDAG &DAG) {
9952 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9953
9954 // If we don't have an indirect input, put it in the constpool if we can,
9955 // otherwise spill it to a stack slot.
9956 // TODO: This isn't quite right. We need to handle these according to
9957 // the addressing mode that the constraint wants. Also, this may take
9958 // an additional register for the computation and we don't want that
9959 // either.
9960
9961 // If the operand is a float, integer, or vector constant, spill to a
9962 // constant pool entry to get its address.
9963 const Value *OpVal = OpInfo.CallOperandVal;
9964 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9966 OpInfo.CallOperand = DAG.getConstantPool(
9967 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9968 return Chain;
9969 }
9970
9971 // Otherwise, create a stack slot and emit a store to it before the asm.
9972 Type *Ty = OpVal->getType();
9973 auto &DL = DAG.getDataLayout();
9974 TypeSize TySize = DL.getTypeAllocSize(Ty);
9977 int StackID = 0;
9978 if (TySize.isScalable())
9979 StackID = TFI->getStackIDForScalableVectors();
9980 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9981 DL.getPrefTypeAlign(Ty), false,
9982 nullptr, StackID);
9983 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9984 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9986 TLI.getMemValueType(DL, Ty));
9987 OpInfo.CallOperand = StackSlot;
9988
9989 return Chain;
9990}
9991
9992/// GetRegistersForValue - Assign registers (virtual or physical) for the
9993/// specified operand. We prefer to assign virtual registers, to allow the
9994/// register allocator to handle the assignment process. However, if the asm
9995/// uses features that we can't model on machineinstrs, we have SDISel do the
9996/// allocation. This produces generally horrible, but correct, code.
9997///
9998/// OpInfo describes the operand
9999/// RefOpInfo describes the matching operand if any, the operand otherwise
10000static std::optional<unsigned>
10002 SDISelAsmOperandInfo &OpInfo,
10003 SDISelAsmOperandInfo &RefOpInfo) {
10004 LLVMContext &Context = *DAG.getContext();
10005 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10006
10010
10011 // No work to do for memory/address operands.
10012 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
10013 OpInfo.ConstraintType == TargetLowering::C_Address)
10014 return std::nullopt;
10015
10016 // If this is a constraint for a single physreg, or a constraint for a
10017 // register class, find it.
10018 unsigned AssignedReg;
10019 const TargetRegisterClass *RC;
10020 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
10021 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
10022 // RC is unset only on failure. Return immediately.
10023 if (!RC)
10024 return std::nullopt;
10025
10026 // Get the actual register value type. This is important, because the user
10027 // may have asked for (e.g.) the AX register in i32 type. We need to
10028 // remember that AX is actually i16 to get the right extension.
10029 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
10030
10031 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
10032 // If this is an FP operand in an integer register (or visa versa), or more
10033 // generally if the operand value disagrees with the register class we plan
10034 // to stick it in, fix the operand type.
10035 //
10036 // If this is an input value, the bitcast to the new type is done now.
10037 // Bitcast for output value is done at the end of visitInlineAsm().
10038 if ((OpInfo.Type == InlineAsm::isOutput ||
10039 OpInfo.Type == InlineAsm::isInput) &&
10040 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
10041 // Try to convert to the first EVT that the reg class contains. If the
10042 // types are identical size, use a bitcast to convert (e.g. two differing
10043 // vector types). Note: output bitcast is done at the end of
10044 // visitInlineAsm().
10045 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
10046 // Exclude indirect inputs while they are unsupported because the code
10047 // to perform the load is missing and thus OpInfo.CallOperand still
10048 // refers to the input address rather than the pointed-to value.
10049 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
10050 OpInfo.CallOperand =
10051 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
10052 OpInfo.ConstraintVT = RegVT;
10053 // If the operand is an FP value and we want it in integer registers,
10054 // use the corresponding integer type. This turns an f64 value into
10055 // i64, which can be passed with two i32 values on a 32-bit machine.
10056 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
10057 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
10058 if (OpInfo.Type == InlineAsm::isInput)
10059 OpInfo.CallOperand =
10060 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
10061 OpInfo.ConstraintVT = VT;
10062 }
10063 }
10064 }
10065
10066 // No need to allocate a matching input constraint since the constraint it's
10067 // matching to has already been allocated.
10068 if (OpInfo.isMatchingInputConstraint())
10069 return std::nullopt;
10070
10071 EVT ValueVT = OpInfo.ConstraintVT;
10072 if (OpInfo.ConstraintVT == MVT::Other)
10073 ValueVT = RegVT;
10074
10075 // Initialize NumRegs.
10076 unsigned NumRegs = 1;
10077 if (OpInfo.ConstraintVT != MVT::Other)
10078 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
10079
10080 // If this is a constraint for a specific physical register, like {r17},
10081 // assign it now.
10082
10083 // If this associated to a specific register, initialize iterator to correct
10084 // place. If virtual, make sure we have enough registers
10085
10086 // Initialize iterator if necessary
10089
10090 // Do not check for single registers.
10091 if (AssignedReg) {
10092 I = std::find(I, RC->end(), AssignedReg);
10093 if (I == RC->end()) {
10094 // RC does not contain the selected register, which indicates a
10095 // mismatch between the register and the required type/bitwidth.
10096 return {AssignedReg};
10097 }
10098 }
10099
10100 for (; NumRegs; --NumRegs, ++I) {
10101 assert(I != RC->end() && "Ran out of registers to allocate!");
10102 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
10103 Regs.push_back(R);
10104 }
10105
10106 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
10107 return std::nullopt;
10108}
10109
10110static unsigned
10112 const std::vector<SDValue> &AsmNodeOperands) {
10113 // Scan until we find the definition we already emitted of this operand.
10114 unsigned CurOp = InlineAsm::Op_FirstOperand;
10115 for (; OperandNo; --OperandNo) {
10116 // Advance to the next operand.
10117 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
10118 const InlineAsm::Flag F(OpFlag);
10119 assert(
10120 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
10121 "Skipped past definitions?");
10122 CurOp += F.getNumOperandRegisters() + 1;
10123 }
10124 return CurOp;
10125}
10126
10127namespace {
10128
10129class ExtraFlags {
10130 unsigned Flags = 0;
10131
10132public:
10133 explicit ExtraFlags(const CallBase &Call) {
10134 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10135 if (IA->hasSideEffects())
10137 if (IA->isAlignStack())
10139 if (IA->canThrow())
10141 if (Call.isConvergent())
10143 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
10144 }
10145
10146 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
10147 // Ideally, we would only check against memory constraints. However, the
10148 // meaning of an Other constraint can be target-specific and we can't easily
10149 // reason about it. Therefore, be conservative and set MayLoad/MayStore
10150 // for Other constraints as well.
10153 if (OpInfo.Type == InlineAsm::isInput)
10155 else if (OpInfo.Type == InlineAsm::isOutput)
10157 else if (OpInfo.Type == InlineAsm::isClobber)
10159 }
10160 }
10161
10162 unsigned get() const { return Flags; }
10163};
10164
10165} // end anonymous namespace
10166
10167static bool isFunction(SDValue Op) {
10168 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
10169 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10170 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10171
10172 // In normal "call dllimport func" instruction (non-inlineasm) it force
10173 // indirect access by specifing call opcode. And usually specially print
10174 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10175 // not do in this way now. (In fact, this is similar with "Data Access"
10176 // action). So here we ignore dllimport function.
10177 if (Fn && !Fn->hasDLLImportStorageClass())
10178 return true;
10179 }
10180 }
10181 return false;
10182}
10183
10184/// visitInlineAsm - Handle a call to an InlineAsm object.
10185void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10186 const BasicBlock *EHPadBB) {
10187 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10188
10189 /// ConstraintOperands - Information about all of the constraints.
10190 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10191
10192 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10194 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10195
10196 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
10197 // AsmDialect, MayLoad, MayStore).
10198 bool HasSideEffect = IA->hasSideEffects();
10199 ExtraFlags ExtraInfo(Call);
10200
10201 for (auto &T : TargetConstraints) {
10202 ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10203 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
10204
10205 if (OpInfo.CallOperandVal)
10206 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
10207
10208 if (!HasSideEffect)
10209 HasSideEffect = OpInfo.hasMemory(TLI);
10210
10211 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10212 // FIXME: Could we compute this on OpInfo rather than T?
10213
10214 // Compute the constraint code and ConstraintType to use.
10216
10217 if (T.ConstraintType == TargetLowering::C_Immediate &&
10218 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
10219 // We've delayed emitting a diagnostic like the "n" constraint because
10220 // inlining could cause an integer showing up.
10221 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
10222 "' expects an integer constant "
10223 "expression");
10224
10225 ExtraInfo.update(T);
10226 }
10227
10228 // We won't need to flush pending loads if this asm doesn't touch
10229 // memory and is nonvolatile.
10230 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
10231
10232 bool EmitEHLabels = isa<InvokeInst>(Call);
10233 if (EmitEHLabels) {
10234 assert(EHPadBB && "InvokeInst must have an EHPadBB");
10235 }
10236 bool IsCallBr = isa<CallBrInst>(Call);
10237
10238 if (IsCallBr || EmitEHLabels) {
10239 // If this is a callbr or invoke we need to flush pending exports since
10240 // inlineasm_br and invoke are terminators.
10241 // We need to do this before nodes are glued to the inlineasm_br node.
10242 Chain = getControlRoot();
10243 }
10244
10245 MCSymbol *BeginLabel = nullptr;
10246 if (EmitEHLabels) {
10247 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
10248 }
10249
10250 int OpNo = -1;
10251 SmallVector<StringRef> AsmStrs;
10252 IA->collectAsmStrs(AsmStrs);
10253
10254 // Second pass over the constraints: compute which constraint option to use.
10255 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10256 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10257 OpNo++;
10258
10259 // If this is an output operand with a matching input operand, look up the
10260 // matching input. If their types mismatch, e.g. one is an integer, the
10261 // other is floating point, or their sizes are different, flag it as an
10262 // error.
10263 if (OpInfo.hasMatchingInput()) {
10264 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
10265 patchMatchingInput(OpInfo, Input, DAG);
10266 }
10267
10268 // Compute the constraint code and ConstraintType to use.
10269 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10270
10271 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10272 OpInfo.Type == InlineAsm::isClobber) ||
10273 OpInfo.ConstraintType == TargetLowering::C_Address)
10274 continue;
10275
10276 // In Linux PIC model, there are 4 cases about value/label addressing:
10277 //
10278 // 1: Function call or Label jmp inside the module.
10279 // 2: Data access (such as global variable, static variable) inside module.
10280 // 3: Function call or Label jmp outside the module.
10281 // 4: Data access (such as global variable) outside the module.
10282 //
10283 // Due to current llvm inline asm architecture designed to not "recognize"
10284 // the asm code, there are quite troubles for us to treat mem addressing
10285 // differently for same value/adress used in different instuctions.
10286 // For example, in pic model, call a func may in plt way or direclty
10287 // pc-related, but lea/mov a function adress may use got.
10288 //
10289 // Here we try to "recognize" function call for the case 1 and case 3 in
10290 // inline asm. And try to adjust the constraint for them.
10291 //
10292 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10293 // label, so here we don't handle jmp function label now, but we need to
10294 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10295 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10296 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10297 TM.getCodeModel() != CodeModel::Large) {
10298 OpInfo.isIndirect = false;
10299 OpInfo.ConstraintType = TargetLowering::C_Address;
10300 }
10301
10302 // If this is a memory input, and if the operand is not indirect, do what we
10303 // need to provide an address for the memory input.
10304 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10305 !OpInfo.isIndirect) {
10306 assert((OpInfo.isMultipleAlternative ||
10307 (OpInfo.Type == InlineAsm::isInput)) &&
10308 "Can only indirectify direct input operands!");
10309
10310 // Memory operands really want the address of the value.
10311 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
10312
10313 // There is no longer a Value* corresponding to this operand.
10314 OpInfo.CallOperandVal = nullptr;
10315
10316 // It is now an indirect operand.
10317 OpInfo.isIndirect = true;
10318 }
10319
10320 }
10321
10322 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10323 std::vector<SDValue> AsmNodeOperands;
10324 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10325 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10326 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10327
10328 // If we have a !srcloc metadata node associated with it, we want to attach
10329 // this to the ultimately generated inline asm machineinstr. To do this, we
10330 // pass in the third operand as this (potentially null) inline asm MDNode.
10331 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10332 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10333
10334 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10335 // bits as operand 3.
10336 AsmNodeOperands.push_back(DAG.getTargetConstant(
10337 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10338
10339 // Third pass: Loop over operands to prepare DAG-level operands.. As part of
10340 // this, assign virtual and physical registers for inputs and otput.
10341 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10342 // Assign Registers.
10343 SDISelAsmOperandInfo &RefOpInfo =
10344 OpInfo.isMatchingInputConstraint()
10345 ? ConstraintOperands[OpInfo.getMatchedOperand()]
10346 : OpInfo;
10347 const auto RegError =
10348 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
10349 if (RegError) {
10350 const MachineFunction &MF = DAG.getMachineFunction();
10351 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10352 const char *RegName = TRI.getName(*RegError);
10353 emitInlineAsmError(Call, "register '" + Twine(RegName) +
10354 "' allocated for constraint '" +
10355 Twine(OpInfo.ConstraintCode) +
10356 "' does not match required type");
10357 return;
10358 }
10359
10360 auto DetectWriteToReservedRegister = [&]() {
10361 const MachineFunction &MF = DAG.getMachineFunction();
10362 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10363 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10364 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10365 const char *RegName = TRI.getName(Reg);
10366 emitInlineAsmError(Call, "write to reserved register '" +
10367 Twine(RegName) + "'");
10368 return true;
10369 }
10370 }
10371 return false;
10372 };
10373 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10374 (OpInfo.Type == InlineAsm::isInput &&
10375 !OpInfo.isMatchingInputConstraint())) &&
10376 "Only address as input operand is allowed.");
10377
10378 switch (OpInfo.Type) {
10380 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10381 const InlineAsm::ConstraintCode ConstraintID =
10382 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10384 "Failed to convert memory constraint code to constraint id.");
10385
10386 // Add information to the INLINEASM node to know about this output.
10387 InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1);
10388 OpFlags.setMemConstraint(ConstraintID);
10389 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
10390 MVT::i32));
10391 AsmNodeOperands.push_back(OpInfo.CallOperand);
10392 } else {
10393 // Otherwise, this outputs to a register (directly for C_Register /
10394 // C_RegisterClass, and a target-defined fashion for
10395 // C_Immediate/C_Other). Find a register that we can use.
10396 if (OpInfo.AssignedRegs.Regs.empty()) {
10397 emitInlineAsmError(
10398 Call, "couldn't allocate output register for constraint '" +
10399 Twine(OpInfo.ConstraintCode) + "'");
10400 return;
10401 }
10402
10403 if (DetectWriteToReservedRegister())
10404 return;
10405
10406 // Add information to the INLINEASM node to know that this register is
10407 // set.
10408 OpInfo.AssignedRegs.AddInlineAsmOperands(
10409 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10411 false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
10412 }
10413 break;
10414
10415 case InlineAsm::isInput:
10416 case InlineAsm::isLabel: {
10417 SDValue InOperandVal = OpInfo.CallOperand;
10418
10419 if (OpInfo.isMatchingInputConstraint()) {
10420 // If this is required to match an output register we have already set,
10421 // just use its register.
10422 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10423 AsmNodeOperands);
10424 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
10425 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10426 if (OpInfo.isIndirect) {
10427 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10428 emitInlineAsmError(Call, "inline asm not supported yet: "
10429 "don't know how to handle tied "
10430 "indirect register inputs");
10431 return;
10432 }
10433
10435 MachineFunction &MF = DAG.getMachineFunction();
10436 MachineRegisterInfo &MRI = MF.getRegInfo();
10437 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10438 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
10439 Register TiedReg = R->getReg();
10440 MVT RegVT = R->getSimpleValueType(0);
10441 const TargetRegisterClass *RC =
10442 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10443 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10444 : TRI.getMinimalPhysRegClass(TiedReg);
10445 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
10446 Regs.push_back(MRI.createVirtualRegister(RC));
10447
10448 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10449
10450 SDLoc dl = getCurSDLoc();
10451 // Use the produced MatchedRegs object to
10452 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
10453 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
10454 OpInfo.getMatchedOperand(), dl, DAG,
10455 AsmNodeOperands);
10456 break;
10457 }
10458
10459 assert(Flag.isMemKind() && "Unknown matching constraint!");
10460 assert(Flag.getNumOperandRegisters() == 1 &&
10461 "Unexpected number of operands");
10462 // Add information to the INLINEASM node to know about this input.
10463 // See InlineAsm.h isUseOperandTiedToDef.
10464 Flag.clearMemConstraint();
10465 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10466 AsmNodeOperands.push_back(DAG.getTargetConstant(
10467 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10468 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
10469 break;
10470 }
10471
10472 // Treat indirect 'X' constraint as memory.
10473 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10474 OpInfo.isIndirect)
10475 OpInfo.ConstraintType = TargetLowering::C_Memory;
10476
10477 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10478 OpInfo.ConstraintType == TargetLowering::C_Other) {
10479 std::vector<SDValue> Ops;
10480 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10481 Ops, DAG);
10482 if (Ops.empty()) {
10483 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10484 if (isa<ConstantSDNode>(InOperandVal)) {
10485 emitInlineAsmError(Call, "value out of range for constraint '" +
10486 Twine(OpInfo.ConstraintCode) + "'");
10487 return;
10488 }
10489
10490 emitInlineAsmError(Call,
10491 "invalid operand for inline asm constraint '" +
10492 Twine(OpInfo.ConstraintCode) + "'");
10493 return;
10494 }
10495
10496 // Add information to the INLINEASM node to know about this input.
10497 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10498 AsmNodeOperands.push_back(DAG.getTargetConstant(
10499 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10500 llvm::append_range(AsmNodeOperands, Ops);
10501 break;
10502 }
10503
10504 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10505 assert((OpInfo.isIndirect ||
10506 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10507 "Operand must be indirect to be a mem!");
10508 assert(InOperandVal.getValueType() ==
10509 TLI.getPointerTy(DAG.getDataLayout()) &&
10510 "Memory operands expect pointer values");
10511
10512 const InlineAsm::ConstraintCode ConstraintID =
10513 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10515 "Failed to convert memory constraint code to constraint id.");
10516
10517 // Add information to the INLINEASM node to know about this input.
10518 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10519 ResOpType.setMemConstraint(ConstraintID);
10520 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10521 getCurSDLoc(),
10522 MVT::i32));
10523 AsmNodeOperands.push_back(InOperandVal);
10524 break;
10525 }
10526
10527 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10528 const InlineAsm::ConstraintCode ConstraintID =
10529 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10531 "Failed to convert memory constraint code to constraint id.");
10532
10533 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10534
10535 SDValue AsmOp = InOperandVal;
10536 if (isFunction(InOperandVal)) {
10537 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10538 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10539 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10540 InOperandVal.getValueType(),
10541 GA->getOffset());
10542 }
10543
10544 // Add information to the INLINEASM node to know about this input.
10545 ResOpType.setMemConstraint(ConstraintID);
10546
10547 AsmNodeOperands.push_back(
10548 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10549
10550 AsmNodeOperands.push_back(AsmOp);
10551 break;
10552 }
10553
10554 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10555 OpInfo.ConstraintType != TargetLowering::C_Register) {
10556 emitInlineAsmError(Call, "unknown asm constraint '" +
10557 Twine(OpInfo.ConstraintCode) + "'");
10558 return;
10559 }
10560
10561 // TODO: Support this.
10562 if (OpInfo.isIndirect) {
10563 emitInlineAsmError(
10564 Call, "Don't know how to handle indirect register inputs yet "
10565 "for constraint '" +
10566 Twine(OpInfo.ConstraintCode) + "'");
10567 return;
10568 }
10569
10570 // Copy the input into the appropriate registers.
10571 if (OpInfo.AssignedRegs.Regs.empty()) {
10572 emitInlineAsmError(Call,
10573 "couldn't allocate input reg for constraint '" +
10574 Twine(OpInfo.ConstraintCode) + "'");
10575 return;
10576 }
10577
10578 if (DetectWriteToReservedRegister())
10579 return;
10580
10581 SDLoc dl = getCurSDLoc();
10582
10583 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10584 &Call);
10585
10586 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10587 0, dl, DAG, AsmNodeOperands);
10588 break;
10589 }
10591 // Add the clobbered value to the operand list, so that the register
10592 // allocator is aware that the physreg got clobbered.
10593 if (!OpInfo.AssignedRegs.Regs.empty())
10595 false, 0, getCurSDLoc(), DAG,
10596 AsmNodeOperands);
10597 break;
10598 }
10599 }
10600
10601 // Finish up input operands. Set the input chain and add the flag last.
10602 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10603 if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10604
10605 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10606 Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10607 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10608 Glue = Chain.getValue(1);
10609
10610 // Do additional work to generate outputs.
10611
10612 SmallVector<EVT, 1> ResultVTs;
10613 SmallVector<SDValue, 1> ResultValues;
10614 SmallVector<SDValue, 8> OutChains;
10615
10616 llvm::Type *CallResultType = Call.getType();
10617 ArrayRef<Type *> ResultTypes;
10618 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10619 ResultTypes = StructResult->elements();
10620 else if (!CallResultType->isVoidTy())
10621 ResultTypes = ArrayRef(CallResultType);
10622
10623 auto CurResultType = ResultTypes.begin();
10624 auto handleRegAssign = [&](SDValue V) {
10625 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10626 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10627 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10628 ++CurResultType;
10629 // If the type of the inline asm call site return value is different but has
10630 // same size as the type of the asm output bitcast it. One example of this
10631 // is for vectors with different width / number of elements. This can
10632 // happen for register classes that can contain multiple different value
10633 // types. The preg or vreg allocated may not have the same VT as was
10634 // expected.
10635 //
10636 // This can also happen for a return value that disagrees with the register
10637 // class it is put in, eg. a double in a general-purpose register on a
10638 // 32-bit machine.
10639 if (ResultVT != V.getValueType() &&
10640 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10641 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10642 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10643 V.getValueType().isInteger()) {
10644 // If a result value was tied to an input value, the computed result
10645 // may have a wider width than the expected result. Extract the
10646 // relevant portion.
10647 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10648 }
10649 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10650 ResultVTs.push_back(ResultVT);
10651 ResultValues.push_back(V);
10652 };
10653
10654 // Deal with output operands.
10655 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10656 if (OpInfo.Type == InlineAsm::isOutput) {
10657 SDValue Val;
10658 // Skip trivial output operands.
10659 if (OpInfo.AssignedRegs.Regs.empty())
10660 continue;
10661
10662 switch (OpInfo.ConstraintType) {
10665 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10666 Chain, &Glue, &Call);
10667 break;
10670 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10671 OpInfo, DAG);
10672 break;
10674 break; // Already handled.
10676 break; // Silence warning.
10678 assert(false && "Unexpected unknown constraint");
10679 }
10680
10681 // Indirect output manifest as stores. Record output chains.
10682 if (OpInfo.isIndirect) {
10683 const Value *Ptr = OpInfo.CallOperandVal;
10684 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10685 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10686 MachinePointerInfo(Ptr));
10687 OutChains.push_back(Store);
10688 } else {
10689 // generate CopyFromRegs to associated registers.
10690 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10691 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10692 for (const SDValue &V : Val->op_values())
10693 handleRegAssign(V);
10694 } else
10695 handleRegAssign(Val);
10696 }
10697 }
10698 }
10699
10700 // Set results.
10701 if (!ResultValues.empty()) {
10702 assert(CurResultType == ResultTypes.end() &&
10703 "Mismatch in number of ResultTypes");
10704 assert(ResultValues.size() == ResultTypes.size() &&
10705 "Mismatch in number of output operands in asm result");
10706
10708 DAG.getVTList(ResultVTs), ResultValues);
10709 setValue(&Call, V);
10710 }
10711
10712 // Collect store chains.
10713 if (!OutChains.empty())
10714 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10715
10716 if (EmitEHLabels) {
10717 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10718 }
10719
10720 // Only Update Root if inline assembly has a memory effect.
10721 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10722 EmitEHLabels)
10723 DAG.setRoot(Chain);
10724}
10725
10726void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10727 const Twine &Message) {
10728 LLVMContext &Ctx = *DAG.getContext();
10729 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10730
10731 // Make sure we leave the DAG in a valid state
10732 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10733 SmallVector<EVT, 1> ValueVTs;
10734 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10735
10736 if (ValueVTs.empty())
10737 return;
10738
10740 for (const EVT &VT : ValueVTs)
10741 Ops.push_back(DAG.getUNDEF(VT));
10742
10743 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10744}
10745
10746void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10747 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10748 MVT::Other, getRoot(),
10749 getValue(I.getArgOperand(0)),
10750 DAG.getSrcValue(I.getArgOperand(0))));
10751}
10752
10753void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10754 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10755 const DataLayout &DL = DAG.getDataLayout();
10756 SDValue V = DAG.getVAArg(
10757 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10758 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10759 DL.getABITypeAlign(I.getType()).value());
10760 DAG.setRoot(V.getValue(1));
10761
10762 if (I.getType()->isPointerTy())
10763 V = DAG.getPtrExtOrTrunc(
10764 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10765 setValue(&I, V);
10766}
10767
10768void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10769 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10770 MVT::Other, getRoot(),
10771 getValue(I.getArgOperand(0)),
10772 DAG.getSrcValue(I.getArgOperand(0))));
10773}
10774
10775void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10776 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10777 MVT::Other, getRoot(),
10778 getValue(I.getArgOperand(0)),
10779 getValue(I.getArgOperand(1)),
10780 DAG.getSrcValue(I.getArgOperand(0)),
10781 DAG.getSrcValue(I.getArgOperand(1))));
10782}
10783
10785 const Instruction &I,
10786 SDValue Op) {
10787 std::optional<ConstantRange> CR = getRange(I);
10788
10789 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10790 return Op;
10791
10792 APInt Lo = CR->getUnsignedMin();
10793 if (!Lo.isMinValue())
10794 return Op;
10795
10796 APInt Hi = CR->getUnsignedMax();
10797 unsigned Bits = std::max(Hi.getActiveBits(),
10798 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10799
10800 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10801
10802 SDLoc SL = getCurSDLoc();
10803
10804 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10805 DAG.getValueType(SmallVT));
10806 unsigned NumVals = Op.getNode()->getNumValues();
10807 if (NumVals == 1)
10808 return ZExt;
10809
10811
10812 Ops.push_back(ZExt);
10813 for (unsigned I = 1; I != NumVals; ++I)
10814 Ops.push_back(Op.getValue(I));
10815
10816 return DAG.getMergeValues(Ops, SL);
10817}
10818
10820 SelectionDAG &DAG, const Instruction &I, SDValue Op) {
10821 FPClassTest Classes = getNoFPClass(I);
10822 if (Classes == fcNone)
10823 return Op;
10824
10825 SDLoc SL = getCurSDLoc();
10826 SDValue TestConst = DAG.getTargetConstant(Classes, SDLoc(), MVT::i32);
10827
10828 if (Op.getOpcode() != ISD::MERGE_VALUES) {
10829 return DAG.getNode(ISD::AssertNoFPClass, SL, Op.getValueType(), Op,
10830 TestConst);
10831 }
10832
10833 SmallVector<SDValue, 8> Ops(Op.getNumOperands());
10834 for (unsigned I = 0, E = Ops.size(); I != E; ++I) {
10835 SDValue MergeOp = Op.getOperand(I);
10836 Ops[I] = DAG.getNode(ISD::AssertNoFPClass, SL, MergeOp.getValueType(),
10837 MergeOp, TestConst);
10838 }
10839
10840 return DAG.getMergeValues(Ops, SL);
10841}
10842
10843/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10844/// the call being lowered.
10845///
10846/// This is a helper for lowering intrinsics that follow a target calling
10847/// convention or require stack pointer adjustment. Only a subset of the
10848/// intrinsic's operands need to participate in the calling convention.
10851 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10852 AttributeSet RetAttrs, bool IsPatchPoint) {
10854 Args.reserve(NumArgs);
10855
10856 // Populate the argument list.
10857 // Attributes for args start at offset 1, after the return attribute.
10858 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10859 ArgI != ArgE; ++ArgI) {
10860 const Value *V = Call->getOperand(ArgI);
10861
10862 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10863
10864 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10865 Entry.setAttributes(Call, ArgI);
10866 Args.push_back(Entry);
10867 }
10868
10870 .setChain(getRoot())
10871 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10872 RetAttrs)
10873 .setDiscardResult(Call->use_empty())
10874 .setIsPatchPoint(IsPatchPoint)
10876 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10877}
10878
10879/// Add a stack map intrinsic call's live variable operands to a stackmap
10880/// or patchpoint target node's operand list.
10881///
10882/// Constants are converted to TargetConstants purely as an optimization to
10883/// avoid constant materialization and register allocation.
10884///
10885/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10886/// generate addess computation nodes, and so FinalizeISel can convert the
10887/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10888/// address materialization and register allocation, but may also be required
10889/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10890/// alloca in the entry block, then the runtime may assume that the alloca's
10891/// StackMap location can be read immediately after compilation and that the
10892/// location is valid at any point during execution (this is similar to the
10893/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10894/// only available in a register, then the runtime would need to trap when
10895/// execution reaches the StackMap in order to read the alloca's location.
10896static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10898 SelectionDAGBuilder &Builder) {
10899 SelectionDAG &DAG = Builder.DAG;
10900 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10901 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10902
10903 // Things on the stack are pointer-typed, meaning that they are already
10904 // legal and can be emitted directly to target nodes.
10906 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10907 } else {
10908 // Otherwise emit a target independent node to be legalised.
10909 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10910 }
10911 }
10912}
10913
10914/// Lower llvm.experimental.stackmap.
10915void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10916 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10917 // [live variables...])
10918
10919 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10920
10921 SDValue Chain, InGlue, Callee;
10923
10924 SDLoc DL = getCurSDLoc();
10926
10927 // The stackmap intrinsic only records the live variables (the arguments
10928 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10929 // intrinsic, this won't be lowered to a function call. This means we don't
10930 // have to worry about calling conventions and target specific lowering code.
10931 // Instead we perform the call lowering right here.
10932 //
10933 // chain, flag = CALLSEQ_START(chain, 0, 0)
10934 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10935 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10936 //
10937 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10938 InGlue = Chain.getValue(1);
10939
10940 // Add the STACKMAP operands, starting with DAG house-keeping.
10941 Ops.push_back(Chain);
10942 Ops.push_back(InGlue);
10943
10944 // Add the <id>, <numShadowBytes> operands.
10945 //
10946 // These do not require legalisation, and can be emitted directly to target
10947 // constant nodes.
10949 assert(ID.getValueType() == MVT::i64);
10950 SDValue IDConst =
10951 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10952 Ops.push_back(IDConst);
10953
10954 SDValue Shad = getValue(CI.getArgOperand(1));
10955 assert(Shad.getValueType() == MVT::i32);
10956 SDValue ShadConst =
10957 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
10958 Ops.push_back(ShadConst);
10959
10960 // Add the live variables.
10961 addStackMapLiveVars(CI, 2, DL, Ops, *this);
10962
10963 // Create the STACKMAP node.
10964 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10965 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10966 InGlue = Chain.getValue(1);
10967
10968 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10969
10970 // Stackmaps don't generate values, so nothing goes into the NodeMap.
10971
10972 // Set the root to the target-lowered call chain.
10973 DAG.setRoot(Chain);
10974
10975 // Inform the Frame Information that we have a stackmap in this function.
10976 FuncInfo.MF->getFrameInfo().setHasStackMap();
10977}
10978
10979/// Lower llvm.experimental.patchpoint directly to its target opcode.
10980void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10981 const BasicBlock *EHPadBB) {
10982 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10983 // i32 <numBytes>,
10984 // i8* <target>,
10985 // i32 <numArgs>,
10986 // [Args...],
10987 // [live variables...])
10988
10990 bool IsAnyRegCC = CC == CallingConv::AnyReg;
10991 bool HasDef = !CB.getType()->isVoidTy();
10992 SDLoc dl = getCurSDLoc();
10994
10995 // Handle immediate and symbolic callees.
10996 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10997 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10998 /*isTarget=*/true);
10999 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
11000 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
11001 SDLoc(SymbolicCallee),
11002 SymbolicCallee->getValueType(0));
11003
11004 // Get the real number of arguments participating in the call <numArgs>
11006 unsigned NumArgs = NArgVal->getAsZExtVal();
11007
11008 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
11009 // Intrinsics include all meta-operands up to but not including CC.
11010 unsigned NumMetaOpers = PatchPointOpers::CCPos;
11011 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
11012 "Not enough arguments provided to the patchpoint intrinsic");
11013
11014 // For AnyRegCC the arguments are lowered later on manually.
11015 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
11016 Type *ReturnTy =
11017 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
11018
11019 TargetLowering::CallLoweringInfo CLI(DAG);
11020 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
11021 ReturnTy, CB.getAttributes().getRetAttrs(), true);
11022 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
11023
11024 SDNode *CallEnd = Result.second.getNode();
11025 if (CallEnd->getOpcode() == ISD::EH_LABEL)
11026 CallEnd = CallEnd->getOperand(0).getNode();
11027 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
11028 CallEnd = CallEnd->getOperand(0).getNode();
11029
11030 /// Get a call instruction from the call sequence chain.
11031 /// Tail calls are not allowed.
11032 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
11033 "Expected a callseq node.");
11034 SDNode *Call = CallEnd->getOperand(0).getNode();
11035 bool HasGlue = Call->getGluedNode();
11036
11037 // Replace the target specific call node with the patchable intrinsic.
11039
11040 // Push the chain.
11041 Ops.push_back(*(Call->op_begin()));
11042
11043 // Optionally, push the glue (if any).
11044 if (HasGlue)
11045 Ops.push_back(*(Call->op_end() - 1));
11046
11047 // Push the register mask info.
11048 if (HasGlue)
11049 Ops.push_back(*(Call->op_end() - 2));
11050 else
11051 Ops.push_back(*(Call->op_end() - 1));
11052
11053 // Add the <id> and <numBytes> constants.
11055 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
11057 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
11058
11059 // Add the callee.
11060 Ops.push_back(Callee);
11061
11062 // Adjust <numArgs> to account for any arguments that have been passed on the
11063 // stack instead.
11064 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
11065 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
11066 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
11067 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
11068
11069 // Add the calling convention
11070 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
11071
11072 // Add the arguments we omitted previously. The register allocator should
11073 // place these in any free register.
11074 if (IsAnyRegCC)
11075 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
11076 Ops.push_back(getValue(CB.getArgOperand(i)));
11077
11078 // Push the arguments from the call instruction.
11079 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
11080 Ops.append(Call->op_begin() + 2, e);
11081
11082 // Push live variables for the stack map.
11083 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
11084
11085 SDVTList NodeTys;
11086 if (IsAnyRegCC && HasDef) {
11087 // Create the return types based on the intrinsic definition
11088 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11089 SmallVector<EVT, 3> ValueVTs;
11090 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
11091 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
11092
11093 // There is always a chain and a glue type at the end
11094 ValueVTs.push_back(MVT::Other);
11095 ValueVTs.push_back(MVT::Glue);
11096 NodeTys = DAG.getVTList(ValueVTs);
11097 } else
11098 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11099
11100 // Replace the target specific call node with a PATCHPOINT node.
11101 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
11102
11103 // Update the NodeMap.
11104 if (HasDef) {
11105 if (IsAnyRegCC)
11106 setValue(&CB, SDValue(PPV.getNode(), 0));
11107 else
11108 setValue(&CB, Result.first);
11109 }
11110
11111 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
11112 // call sequence. Furthermore the location of the chain and glue can change
11113 // when the AnyReg calling convention is used and the intrinsic returns a
11114 // value.
11115 if (IsAnyRegCC && HasDef) {
11116 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
11117 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
11118 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
11119 } else
11120 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
11121 DAG.DeleteNode(Call);
11122
11123 // Inform the Frame Information that we have a patchpoint in this function.
11124 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
11125}
11126
11127void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
11128 unsigned Intrinsic) {
11129 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11130 SDValue Op1 = getValue(I.getArgOperand(0));
11131 SDValue Op2;
11132 if (I.arg_size() > 1)
11133 Op2 = getValue(I.getArgOperand(1));
11134 SDLoc dl = getCurSDLoc();
11135 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11136 SDValue Res;
11137 SDNodeFlags SDFlags;
11138 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
11139 SDFlags.copyFMF(*FPMO);
11140
11141 switch (Intrinsic) {
11142 case Intrinsic::vector_reduce_fadd:
11143 if (SDFlags.hasAllowReassociation())
11144 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
11145 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
11146 SDFlags);
11147 else
11148 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
11149 break;
11150 case Intrinsic::vector_reduce_fmul:
11151 if (SDFlags.hasAllowReassociation())
11152 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
11153 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
11154 SDFlags);
11155 else
11156 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
11157 break;
11158 case Intrinsic::vector_reduce_add:
11159 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
11160 break;
11161 case Intrinsic::vector_reduce_mul:
11162 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
11163 break;
11164 case Intrinsic::vector_reduce_and:
11165 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
11166 break;
11167 case Intrinsic::vector_reduce_or:
11168 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
11169 break;
11170 case Intrinsic::vector_reduce_xor:
11171 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
11172 break;
11173 case Intrinsic::vector_reduce_smax:
11174 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
11175 break;
11176 case Intrinsic::vector_reduce_smin:
11177 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
11178 break;
11179 case Intrinsic::vector_reduce_umax:
11180 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
11181 break;
11182 case Intrinsic::vector_reduce_umin:
11183 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
11184 break;
11185 case Intrinsic::vector_reduce_fmax:
11186 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
11187 break;
11188 case Intrinsic::vector_reduce_fmin:
11189 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
11190 break;
11191 case Intrinsic::vector_reduce_fmaximum:
11192 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
11193 break;
11194 case Intrinsic::vector_reduce_fminimum:
11195 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11196 break;
11197 default:
11198 llvm_unreachable("Unhandled vector reduce intrinsic");
11199 }
11200 setValue(&I, Res);
11201}
11202
11203/// Returns an AttributeList representing the attributes applied to the return
11204/// value of the given call.
11207 if (CLI.RetSExt)
11208 Attrs.push_back(Attribute::SExt);
11209 if (CLI.RetZExt)
11210 Attrs.push_back(Attribute::ZExt);
11211 if (CLI.IsInReg)
11212 Attrs.push_back(Attribute::InReg);
11213
11214 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11215 Attrs);
11216}
11217
11218/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11219/// implementation, which just calls LowerCall.
11220/// FIXME: When all targets are
11221/// migrated to using LowerCall, this hook should be integrated into SDISel.
11222std::pair<SDValue, SDValue>
11224 LLVMContext &Context = CLI.RetTy->getContext();
11225
11226 // Handle the incoming return values from the call.
11227 CLI.Ins.clear();
11228 SmallVector<Type *, 4> RetOrigTys;
11230 auto &DL = CLI.DAG.getDataLayout();
11231 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11232
11233 SmallVector<EVT, 4> RetVTs;
11234 if (CLI.RetTy != CLI.OrigRetTy) {
11235 assert(RetOrigTys.size() == 1 &&
11236 "Only supported for non-aggregate returns");
11237 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11238 } else {
11239 for (Type *Ty : RetOrigTys)
11240 RetVTs.push_back(getValueType(DL, Ty));
11241 }
11242
11243 if (CLI.IsPostTypeLegalization) {
11244 // If we are lowering a libcall after legalization, split the return type.
11245 SmallVector<Type *, 4> OldRetOrigTys;
11246 SmallVector<EVT, 4> OldRetVTs;
11247 SmallVector<TypeSize, 4> OldOffsets;
11248 RetOrigTys.swap(OldRetOrigTys);
11249 RetVTs.swap(OldRetVTs);
11250 Offsets.swap(OldOffsets);
11251
11252 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11253 EVT RetVT = OldRetVTs[i];
11254 uint64_t Offset = OldOffsets[i];
11255 MVT RegisterVT = getRegisterType(Context, RetVT);
11256 unsigned NumRegs = getNumRegisters(Context, RetVT);
11257 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11258 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11259 RetVTs.append(NumRegs, RegisterVT);
11260 for (unsigned j = 0; j != NumRegs; ++j)
11261 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11262 }
11263 }
11264
11266 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11267
11268 bool CanLowerReturn =
11270 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11271
11272 SDValue DemoteStackSlot;
11273 int DemoteStackIdx = -100;
11274 if (!CanLowerReturn) {
11275 // FIXME: equivalent assert?
11276 // assert(!CS.hasInAllocaArgument() &&
11277 // "sret demotion is incompatible with inalloca");
11278 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11279 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11281 DemoteStackIdx =
11282 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11283 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11284
11285 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11286 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11287 Entry.IsSRet = true;
11288 Entry.Alignment = Alignment;
11289 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11290 CLI.NumFixedArgs += 1;
11291 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11292 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11293
11294 // sret demotion isn't compatible with tail-calls, since the sret argument
11295 // points into the callers stack frame.
11296 CLI.IsTailCall = false;
11297 } else {
11298 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11299 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11300 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11301 ISD::ArgFlagsTy Flags;
11302 if (NeedsRegBlock) {
11303 Flags.setInConsecutiveRegs();
11304 if (I == RetVTs.size() - 1)
11305 Flags.setInConsecutiveRegsLast();
11306 }
11307 EVT VT = RetVTs[I];
11308 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11309 unsigned NumRegs =
11310 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11311 for (unsigned i = 0; i != NumRegs; ++i) {
11312 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11314 if (CLI.RetTy->isPointerTy()) {
11315 Ret.Flags.setPointer();
11317 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11318 }
11319 if (CLI.RetSExt)
11320 Ret.Flags.setSExt();
11321 if (CLI.RetZExt)
11322 Ret.Flags.setZExt();
11323 if (CLI.IsInReg)
11324 Ret.Flags.setInReg();
11325 CLI.Ins.push_back(Ret);
11326 }
11327 }
11328 }
11329
11330 // We push in swifterror return as the last element of CLI.Ins.
11331 ArgListTy &Args = CLI.getArgs();
11332 if (supportSwiftError()) {
11333 for (const ArgListEntry &Arg : Args) {
11334 if (Arg.IsSwiftError) {
11335 ISD::ArgFlagsTy Flags;
11336 Flags.setSwiftError();
11338 PointerType::getUnqual(Context),
11339 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11340 CLI.Ins.push_back(Ret);
11341 }
11342 }
11343 }
11344
11345 // Handle all of the outgoing arguments.
11346 CLI.Outs.clear();
11347 CLI.OutVals.clear();
11348 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11349 SmallVector<Type *, 4> OrigArgTys;
11350 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11351 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11352 Type *FinalType = Args[i].Ty;
11353 if (Args[i].IsByVal)
11354 FinalType = Args[i].IndirectType;
11355 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11356 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11357 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11358 ++Value) {
11359 Type *OrigArgTy = OrigArgTys[Value];
11360 Type *ArgTy = OrigArgTy;
11361 if (Args[i].Ty != Args[i].OrigTy) {
11362 assert(Value == 0 && "Only supported for non-aggregate arguments");
11363 ArgTy = Args[i].Ty;
11364 }
11365
11366 EVT VT = getValueType(DL, ArgTy);
11367 SDValue Op = SDValue(Args[i].Node.getNode(),
11368 Args[i].Node.getResNo() + Value);
11369 ISD::ArgFlagsTy Flags;
11370
11371 // Certain targets (such as MIPS), may have a different ABI alignment
11372 // for a type depending on the context. Give the target a chance to
11373 // specify the alignment it wants.
11374 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11375 Flags.setOrigAlign(OriginalAlignment);
11376
11377 if (i >= CLI.NumFixedArgs)
11378 Flags.setVarArg();
11379 if (ArgTy->isPointerTy()) {
11380 Flags.setPointer();
11381 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11382 }
11383 if (Args[i].IsZExt)
11384 Flags.setZExt();
11385 if (Args[i].IsSExt)
11386 Flags.setSExt();
11387 if (Args[i].IsNoExt)
11388 Flags.setNoExt();
11389 if (Args[i].IsInReg) {
11390 // If we are using vectorcall calling convention, a structure that is
11391 // passed InReg - is surely an HVA
11393 isa<StructType>(FinalType)) {
11394 // The first value of a structure is marked
11395 if (0 == Value)
11396 Flags.setHvaStart();
11397 Flags.setHva();
11398 }
11399 // Set InReg Flag
11400 Flags.setInReg();
11401 }
11402 if (Args[i].IsSRet)
11403 Flags.setSRet();
11404 if (Args[i].IsSwiftSelf)
11405 Flags.setSwiftSelf();
11406 if (Args[i].IsSwiftAsync)
11407 Flags.setSwiftAsync();
11408 if (Args[i].IsSwiftError)
11409 Flags.setSwiftError();
11410 if (Args[i].IsCFGuardTarget)
11411 Flags.setCFGuardTarget();
11412 if (Args[i].IsByVal)
11413 Flags.setByVal();
11414 if (Args[i].IsByRef)
11415 Flags.setByRef();
11416 if (Args[i].IsPreallocated) {
11417 Flags.setPreallocated();
11418 // Set the byval flag for CCAssignFn callbacks that don't know about
11419 // preallocated. This way we can know how many bytes we should've
11420 // allocated and how many bytes a callee cleanup function will pop. If
11421 // we port preallocated to more targets, we'll have to add custom
11422 // preallocated handling in the various CC lowering callbacks.
11423 Flags.setByVal();
11424 }
11425 if (Args[i].IsInAlloca) {
11426 Flags.setInAlloca();
11427 // Set the byval flag for CCAssignFn callbacks that don't know about
11428 // inalloca. This way we can know how many bytes we should've allocated
11429 // and how many bytes a callee cleanup function will pop. If we port
11430 // inalloca to more targets, we'll have to add custom inalloca handling
11431 // in the various CC lowering callbacks.
11432 Flags.setByVal();
11433 }
11434 Align MemAlign;
11435 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11436 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11437 Flags.setByValSize(FrameSize);
11438
11439 // info is not there but there are cases it cannot get right.
11440 if (auto MA = Args[i].Alignment)
11441 MemAlign = *MA;
11442 else
11443 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11444 } else if (auto MA = Args[i].Alignment) {
11445 MemAlign = *MA;
11446 } else {
11447 MemAlign = OriginalAlignment;
11448 }
11449 Flags.setMemAlign(MemAlign);
11450 if (Args[i].IsNest)
11451 Flags.setNest();
11452 if (NeedsRegBlock)
11453 Flags.setInConsecutiveRegs();
11454
11455 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11456 unsigned NumParts =
11457 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11458 SmallVector<SDValue, 4> Parts(NumParts);
11459 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11460
11461 if (Args[i].IsSExt)
11462 ExtendKind = ISD::SIGN_EXTEND;
11463 else if (Args[i].IsZExt)
11464 ExtendKind = ISD::ZERO_EXTEND;
11465
11466 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11467 // for now.
11468 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11470 assert((CLI.RetTy == Args[i].Ty ||
11471 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11473 Args[i].Ty->getPointerAddressSpace())) &&
11474 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11475 // Before passing 'returned' to the target lowering code, ensure that
11476 // either the register MVT and the actual EVT are the same size or that
11477 // the return value and argument are extended in the same way; in these
11478 // cases it's safe to pass the argument register value unchanged as the
11479 // return register value (although it's at the target's option whether
11480 // to do so)
11481 // TODO: allow code generation to take advantage of partially preserved
11482 // registers rather than clobbering the entire register when the
11483 // parameter extension method is not compatible with the return
11484 // extension method
11485 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11486 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11487 CLI.RetZExt == Args[i].IsZExt))
11488 Flags.setReturned();
11489 }
11490
11491 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11492 CLI.CallConv, ExtendKind);
11493
11494 for (unsigned j = 0; j != NumParts; ++j) {
11495 // if it isn't first piece, alignment must be 1
11496 // For scalable vectors the scalable part is currently handled
11497 // by individual targets, so we just use the known minimum size here.
11498 ISD::OutputArg MyFlags(
11499 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11500 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11501 if (NumParts > 1 && j == 0)
11502 MyFlags.Flags.setSplit();
11503 else if (j != 0) {
11504 MyFlags.Flags.setOrigAlign(Align(1));
11505 if (j == NumParts - 1)
11506 MyFlags.Flags.setSplitEnd();
11507 }
11508
11509 CLI.Outs.push_back(MyFlags);
11510 CLI.OutVals.push_back(Parts[j]);
11511 }
11512
11513 if (NeedsRegBlock && Value == NumValues - 1)
11514 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11515 }
11516 }
11517
11519 CLI.Chain = LowerCall(CLI, InVals);
11520
11521 // Update CLI.InVals to use outside of this function.
11522 CLI.InVals = InVals;
11523
11524 // Verify that the target's LowerCall behaved as expected.
11525 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11526 "LowerCall didn't return a valid chain!");
11527 assert((!CLI.IsTailCall || InVals.empty()) &&
11528 "LowerCall emitted a return value for a tail call!");
11529 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11530 "LowerCall didn't emit the correct number of values!");
11531
11532 // For a tail call, the return value is merely live-out and there aren't
11533 // any nodes in the DAG representing it. Return a special value to
11534 // indicate that a tail call has been emitted and no more Instructions
11535 // should be processed in the current block.
11536 if (CLI.IsTailCall) {
11537 CLI.DAG.setRoot(CLI.Chain);
11538 return std::make_pair(SDValue(), SDValue());
11539 }
11540
11541#ifndef NDEBUG
11542 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11543 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11544 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11545 "LowerCall emitted a value with the wrong type!");
11546 }
11547#endif
11548
11549 SmallVector<SDValue, 4> ReturnValues;
11550 if (!CanLowerReturn) {
11551 // The instruction result is the result of loading from the
11552 // hidden sret parameter.
11553 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11554
11555 unsigned NumValues = RetVTs.size();
11556 ReturnValues.resize(NumValues);
11557 SmallVector<SDValue, 4> Chains(NumValues);
11558
11559 // An aggregate return value cannot wrap around the address space, so
11560 // offsets to its parts don't wrap either.
11562 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11563 for (unsigned i = 0; i < NumValues; ++i) {
11565 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11567 SDValue L = CLI.DAG.getLoad(
11568 RetVTs[i], CLI.DL, CLI.Chain, Add,
11570 DemoteStackIdx, Offsets[i]),
11571 HiddenSRetAlign);
11572 ReturnValues[i] = L;
11573 Chains[i] = L.getValue(1);
11574 }
11575
11576 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11577 } else {
11578 // Collect the legal value parts into potentially illegal values
11579 // that correspond to the original function's return values.
11580 std::optional<ISD::NodeType> AssertOp;
11581 if (CLI.RetSExt)
11582 AssertOp = ISD::AssertSext;
11583 else if (CLI.RetZExt)
11584 AssertOp = ISD::AssertZext;
11585 unsigned CurReg = 0;
11586 for (EVT VT : RetVTs) {
11587 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11588 unsigned NumRegs =
11589 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11590
11591 ReturnValues.push_back(getCopyFromParts(
11592 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11593 CLI.Chain, CLI.CallConv, AssertOp));
11594 CurReg += NumRegs;
11595 }
11596
11597 // For a function returning void, there is no return value. We can't create
11598 // such a node, so we just return a null return value in that case. In
11599 // that case, nothing will actually look at the value.
11600 if (ReturnValues.empty())
11601 return std::make_pair(SDValue(), CLI.Chain);
11602 }
11603
11604 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11605 CLI.DAG.getVTList(RetVTs), ReturnValues);
11606 return std::make_pair(Res, CLI.Chain);
11607}
11608
11609/// Places new result values for the node in Results (their number
11610/// and types must exactly match those of the original return values of
11611/// the node), or leaves Results empty, which indicates that the node is not
11612/// to be custom lowered after all.
11615 SelectionDAG &DAG) const {
11616 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11617
11618 if (!Res.getNode())
11619 return;
11620
11621 // If the original node has one result, take the return value from
11622 // LowerOperation as is. It might not be result number 0.
11623 if (N->getNumValues() == 1) {
11624 Results.push_back(Res);
11625 return;
11626 }
11627
11628 // If the original node has multiple results, then the return node should
11629 // have the same number of results.
11630 assert((N->getNumValues() == Res->getNumValues()) &&
11631 "Lowering returned the wrong number of results!");
11632
11633 // Places new result values base on N result number.
11634 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11635 Results.push_back(Res.getValue(I));
11636}
11637
11639 llvm_unreachable("LowerOperation not implemented for this target!");
11640}
11641
11643 Register Reg,
11644 ISD::NodeType ExtendType) {
11646 assert((Op.getOpcode() != ISD::CopyFromReg ||
11647 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11648 "Copy from a reg to the same reg!");
11649 assert(!Reg.isPhysical() && "Is a physreg");
11650
11651 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11652 // If this is an InlineAsm we have to match the registers required, not the
11653 // notional registers required by the type.
11654
11655 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11656 std::nullopt); // This is not an ABI copy.
11657 SDValue Chain = DAG.getEntryNode();
11658
11659 if (ExtendType == ISD::ANY_EXTEND) {
11660 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11661 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11662 ExtendType = PreferredExtendIt->second;
11663 }
11664 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11665 PendingExports.push_back(Chain);
11666}
11667
11669
11670/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11671/// entry block, return true. This includes arguments used by switches, since
11672/// the switch may expand into multiple basic blocks.
11673static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11674 // With FastISel active, we may be splitting blocks, so force creation
11675 // of virtual registers for all non-dead arguments.
11676 if (FastISel)
11677 return A->use_empty();
11678
11679 const BasicBlock &Entry = A->getParent()->front();
11680 for (const User *U : A->users())
11681 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11682 return false; // Use not in entry block.
11683
11684 return true;
11685}
11686
11688 DenseMap<const Argument *,
11689 std::pair<const AllocaInst *, const StoreInst *>>;
11690
11691/// Scan the entry block of the function in FuncInfo for arguments that look
11692/// like copies into a local alloca. Record any copied arguments in
11693/// ArgCopyElisionCandidates.
11694static void
11696 FunctionLoweringInfo *FuncInfo,
11697 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11698 // Record the state of every static alloca used in the entry block. Argument
11699 // allocas are all used in the entry block, so we need approximately as many
11700 // entries as we have arguments.
11701 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11703 unsigned NumArgs = FuncInfo->Fn->arg_size();
11704 StaticAllocas.reserve(NumArgs * 2);
11705
11706 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11707 if (!V)
11708 return nullptr;
11709 V = V->stripPointerCasts();
11710 const auto *AI = dyn_cast<AllocaInst>(V);
11711 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11712 return nullptr;
11713 auto Iter = StaticAllocas.insert({AI, Unknown});
11714 return &Iter.first->second;
11715 };
11716
11717 // Look for stores of arguments to static allocas. Look through bitcasts and
11718 // GEPs to handle type coercions, as long as the alloca is fully initialized
11719 // by the store. Any non-store use of an alloca escapes it and any subsequent
11720 // unanalyzed store might write it.
11721 // FIXME: Handle structs initialized with multiple stores.
11722 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11723 // Look for stores, and handle non-store uses conservatively.
11724 const auto *SI = dyn_cast<StoreInst>(&I);
11725 if (!SI) {
11726 // We will look through cast uses, so ignore them completely.
11727 if (I.isCast())
11728 continue;
11729 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11730 // to allocas.
11731 if (I.isDebugOrPseudoInst())
11732 continue;
11733 // This is an unknown instruction. Assume it escapes or writes to all
11734 // static alloca operands.
11735 for (const Use &U : I.operands()) {
11736 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11737 *Info = StaticAllocaInfo::Clobbered;
11738 }
11739 continue;
11740 }
11741
11742 // If the stored value is a static alloca, mark it as escaped.
11743 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11744 *Info = StaticAllocaInfo::Clobbered;
11745
11746 // Check if the destination is a static alloca.
11747 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11748 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11749 if (!Info)
11750 continue;
11751 const AllocaInst *AI = cast<AllocaInst>(Dst);
11752
11753 // Skip allocas that have been initialized or clobbered.
11754 if (*Info != StaticAllocaInfo::Unknown)
11755 continue;
11756
11757 // Check if the stored value is an argument, and that this store fully
11758 // initializes the alloca.
11759 // If the argument type has padding bits we can't directly forward a pointer
11760 // as the upper bits may contain garbage.
11761 // Don't elide copies from the same argument twice.
11762 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11763 const auto *Arg = dyn_cast<Argument>(Val);
11764 std::optional<TypeSize> AllocaSize = AI->getAllocationSize(DL);
11765 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11766 Arg->getType()->isEmptyTy() || !AllocaSize ||
11767 DL.getTypeStoreSize(Arg->getType()) != *AllocaSize ||
11768 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11769 ArgCopyElisionCandidates.count(Arg)) {
11770 *Info = StaticAllocaInfo::Clobbered;
11771 continue;
11772 }
11773
11774 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11775 << '\n');
11776
11777 // Mark this alloca and store for argument copy elision.
11778 *Info = StaticAllocaInfo::Elidable;
11779 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11780
11781 // Stop scanning if we've seen all arguments. This will happen early in -O0
11782 // builds, which is useful, because -O0 builds have large entry blocks and
11783 // many allocas.
11784 if (ArgCopyElisionCandidates.size() == NumArgs)
11785 break;
11786 }
11787}
11788
11789/// Try to elide argument copies from memory into a local alloca. Succeeds if
11790/// ArgVal is a load from a suitable fixed stack object.
11793 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11794 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11795 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11796 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11797 // Check if this is a load from a fixed stack object.
11798 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11799 if (!LNode)
11800 return;
11801 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11802 if (!FINode)
11803 return;
11804
11805 // Check that the fixed stack object is the right size and alignment.
11806 // Look at the alignment that the user wrote on the alloca instead of looking
11807 // at the stack object.
11808 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11809 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11810 const AllocaInst *AI = ArgCopyIter->second.first;
11811 int FixedIndex = FINode->getIndex();
11812 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11813 int OldIndex = AllocaIndex;
11814 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11815 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11816 LLVM_DEBUG(
11817 dbgs() << " argument copy elision failed due to bad fixed stack "
11818 "object size\n");
11819 return;
11820 }
11821 Align RequiredAlignment = AI->getAlign();
11822 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11823 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11824 "greater than stack argument alignment ("
11825 << DebugStr(RequiredAlignment) << " vs "
11826 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11827 return;
11828 }
11829
11830 // Perform the elision. Delete the old stack object and replace its only use
11831 // in the variable info map. Mark the stack object as mutable and aliased.
11832 LLVM_DEBUG({
11833 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11834 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11835 << '\n';
11836 });
11837 MFI.RemoveStackObject(OldIndex);
11838 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11839 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11840 AllocaIndex = FixedIndex;
11841 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11842 for (SDValue ArgVal : ArgVals)
11843 Chains.push_back(ArgVal.getValue(1));
11844
11845 // Avoid emitting code for the store implementing the copy.
11846 const StoreInst *SI = ArgCopyIter->second.second;
11847 ElidedArgCopyInstrs.insert(SI);
11848
11849 // Check for uses of the argument again so that we can avoid exporting ArgVal
11850 // if it is't used by anything other than the store.
11851 for (const Value *U : Arg.users()) {
11852 if (U != SI) {
11853 ArgHasUses = true;
11854 break;
11855 }
11856 }
11857}
11858
11859void SelectionDAGISel::LowerArguments(const Function &F) {
11860 SelectionDAG &DAG = SDB->DAG;
11861 SDLoc dl = SDB->getCurSDLoc();
11862 const DataLayout &DL = DAG.getDataLayout();
11864
11865 // In Naked functions we aren't going to save any registers.
11866 if (F.hasFnAttribute(Attribute::Naked))
11867 return;
11868
11869 if (!FuncInfo->CanLowerReturn) {
11870 // Put in an sret pointer parameter before all the other parameters.
11871 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11872
11873 ISD::ArgFlagsTy Flags;
11874 Flags.setSRet();
11875 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11876 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11878 Ins.push_back(RetArg);
11879 }
11880
11881 // Look for stores of arguments to static allocas. Mark such arguments with a
11882 // flag to ask the target to give us the memory location of that argument if
11883 // available.
11884 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11886 ArgCopyElisionCandidates);
11887
11888 // Set up the incoming argument description vector.
11889 for (const Argument &Arg : F.args()) {
11890 unsigned ArgNo = Arg.getArgNo();
11892 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11893 bool isArgValueUsed = !Arg.use_empty();
11894 Type *FinalType = Arg.getType();
11895 if (Arg.hasAttribute(Attribute::ByVal))
11896 FinalType = Arg.getParamByValType();
11897 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11898 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11899 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11900 ++Value) {
11901 Type *ArgTy = Types[Value];
11902 EVT VT = TLI->getValueType(DL, ArgTy);
11903 ISD::ArgFlagsTy Flags;
11904
11905 if (ArgTy->isPointerTy()) {
11906 Flags.setPointer();
11907 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11908 }
11909 if (Arg.hasAttribute(Attribute::ZExt))
11910 Flags.setZExt();
11911 if (Arg.hasAttribute(Attribute::SExt))
11912 Flags.setSExt();
11913 if (Arg.hasAttribute(Attribute::InReg)) {
11914 // If we are using vectorcall calling convention, a structure that is
11915 // passed InReg - is surely an HVA
11916 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11917 isa<StructType>(Arg.getType())) {
11918 // The first value of a structure is marked
11919 if (0 == Value)
11920 Flags.setHvaStart();
11921 Flags.setHva();
11922 }
11923 // Set InReg Flag
11924 Flags.setInReg();
11925 }
11926 if (Arg.hasAttribute(Attribute::StructRet))
11927 Flags.setSRet();
11928 if (Arg.hasAttribute(Attribute::SwiftSelf))
11929 Flags.setSwiftSelf();
11930 if (Arg.hasAttribute(Attribute::SwiftAsync))
11931 Flags.setSwiftAsync();
11932 if (Arg.hasAttribute(Attribute::SwiftError))
11933 Flags.setSwiftError();
11934 if (Arg.hasAttribute(Attribute::ByVal))
11935 Flags.setByVal();
11936 if (Arg.hasAttribute(Attribute::ByRef))
11937 Flags.setByRef();
11938 if (Arg.hasAttribute(Attribute::InAlloca)) {
11939 Flags.setInAlloca();
11940 // Set the byval flag for CCAssignFn callbacks that don't know about
11941 // inalloca. This way we can know how many bytes we should've allocated
11942 // and how many bytes a callee cleanup function will pop. If we port
11943 // inalloca to more targets, we'll have to add custom inalloca handling
11944 // in the various CC lowering callbacks.
11945 Flags.setByVal();
11946 }
11947 if (Arg.hasAttribute(Attribute::Preallocated)) {
11948 Flags.setPreallocated();
11949 // Set the byval flag for CCAssignFn callbacks that don't know about
11950 // preallocated. This way we can know how many bytes we should've
11951 // allocated and how many bytes a callee cleanup function will pop. If
11952 // we port preallocated to more targets, we'll have to add custom
11953 // preallocated handling in the various CC lowering callbacks.
11954 Flags.setByVal();
11955 }
11956
11957 // Certain targets (such as MIPS), may have a different ABI alignment
11958 // for a type depending on the context. Give the target a chance to
11959 // specify the alignment it wants.
11960 const Align OriginalAlignment(
11961 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
11962 Flags.setOrigAlign(OriginalAlignment);
11963
11964 Align MemAlign;
11965 Type *ArgMemTy = nullptr;
11966 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11967 Flags.isByRef()) {
11968 if (!ArgMemTy)
11969 ArgMemTy = Arg.getPointeeInMemoryValueType();
11970
11971 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11972
11973 // For in-memory arguments, size and alignment should be passed from FE.
11974 // BE will guess if this info is not there but there are cases it cannot
11975 // get right.
11976 if (auto ParamAlign = Arg.getParamStackAlign())
11977 MemAlign = *ParamAlign;
11978 else if ((ParamAlign = Arg.getParamAlign()))
11979 MemAlign = *ParamAlign;
11980 else
11981 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
11982 if (Flags.isByRef())
11983 Flags.setByRefSize(MemSize);
11984 else
11985 Flags.setByValSize(MemSize);
11986 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11987 MemAlign = *ParamAlign;
11988 } else {
11989 MemAlign = OriginalAlignment;
11990 }
11991 Flags.setMemAlign(MemAlign);
11992
11993 if (Arg.hasAttribute(Attribute::Nest))
11994 Flags.setNest();
11995 if (NeedsRegBlock)
11996 Flags.setInConsecutiveRegs();
11997 if (ArgCopyElisionCandidates.count(&Arg))
11998 Flags.setCopyElisionCandidate();
11999 if (Arg.hasAttribute(Attribute::Returned))
12000 Flags.setReturned();
12001
12002 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
12003 *CurDAG->getContext(), F.getCallingConv(), VT);
12004 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
12005 *CurDAG->getContext(), F.getCallingConv(), VT);
12006 for (unsigned i = 0; i != NumRegs; ++i) {
12007 // For scalable vectors, use the minimum size; individual targets
12008 // are responsible for handling scalable vector arguments and
12009 // return values.
12010 ISD::InputArg MyFlags(
12011 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
12012 i * RegisterVT.getStoreSize().getKnownMinValue());
12013 if (NumRegs > 1 && i == 0)
12014 MyFlags.Flags.setSplit();
12015 // if it isn't first piece, alignment must be 1
12016 else if (i > 0) {
12017 MyFlags.Flags.setOrigAlign(Align(1));
12018 if (i == NumRegs - 1)
12019 MyFlags.Flags.setSplitEnd();
12020 }
12021 Ins.push_back(MyFlags);
12022 }
12023 if (NeedsRegBlock && Value == NumValues - 1)
12024 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
12025 }
12026 }
12027
12028 // Call the target to set up the argument values.
12030 SDValue NewRoot = TLI->LowerFormalArguments(
12031 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
12032
12033 // Verify that the target's LowerFormalArguments behaved as expected.
12034 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
12035 "LowerFormalArguments didn't return a valid chain!");
12036 assert(InVals.size() == Ins.size() &&
12037 "LowerFormalArguments didn't emit the correct number of values!");
12038 LLVM_DEBUG({
12039 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
12040 assert(InVals[i].getNode() &&
12041 "LowerFormalArguments emitted a null value!");
12042 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
12043 "LowerFormalArguments emitted a value with the wrong type!");
12044 }
12045 });
12046
12047 // Update the DAG with the new chain value resulting from argument lowering.
12048 DAG.setRoot(NewRoot);
12049
12050 // Set up the argument values.
12051 unsigned i = 0;
12052 if (!FuncInfo->CanLowerReturn) {
12053 // Create a virtual register for the sret pointer, and put in a copy
12054 // from the sret argument into it.
12055 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
12056 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
12057 std::optional<ISD::NodeType> AssertOp;
12058 SDValue ArgValue =
12059 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
12060 F.getCallingConv(), AssertOp);
12061
12062 MachineFunction& MF = SDB->DAG.getMachineFunction();
12063 MachineRegisterInfo& RegInfo = MF.getRegInfo();
12064 Register SRetReg =
12065 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
12066 FuncInfo->DemoteRegister = SRetReg;
12067 NewRoot =
12068 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
12069 DAG.setRoot(NewRoot);
12070
12071 // i indexes lowered arguments. Bump it past the hidden sret argument.
12072 ++i;
12073 }
12074
12076 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
12077 for (const Argument &Arg : F.args()) {
12078 SmallVector<SDValue, 4> ArgValues;
12079 SmallVector<EVT, 4> ValueVTs;
12080 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
12081 unsigned NumValues = ValueVTs.size();
12082 if (NumValues == 0)
12083 continue;
12084
12085 bool ArgHasUses = !Arg.use_empty();
12086
12087 // Elide the copying store if the target loaded this argument from a
12088 // suitable fixed stack object.
12089 if (Ins[i].Flags.isCopyElisionCandidate()) {
12090 unsigned NumParts = 0;
12091 for (EVT VT : ValueVTs)
12092 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
12093 F.getCallingConv(), VT);
12094
12095 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
12096 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
12097 ArrayRef(&InVals[i], NumParts), ArgHasUses);
12098 }
12099
12100 // If this argument is unused then remember its value. It is used to generate
12101 // debugging information.
12102 bool isSwiftErrorArg =
12103 TLI->supportSwiftError() &&
12104 Arg.hasAttribute(Attribute::SwiftError);
12105 if (!ArgHasUses && !isSwiftErrorArg) {
12106 SDB->setUnusedArgValue(&Arg, InVals[i]);
12107
12108 // Also remember any frame index for use in FastISel.
12109 if (FrameIndexSDNode *FI =
12111 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12112 }
12113
12114 for (unsigned Val = 0; Val != NumValues; ++Val) {
12115 EVT VT = ValueVTs[Val];
12116 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
12117 F.getCallingConv(), VT);
12118 unsigned NumParts = TLI->getNumRegistersForCallingConv(
12119 *CurDAG->getContext(), F.getCallingConv(), VT);
12120
12121 // Even an apparent 'unused' swifterror argument needs to be returned. So
12122 // we do generate a copy for it that can be used on return from the
12123 // function.
12124 if (ArgHasUses || isSwiftErrorArg) {
12125 std::optional<ISD::NodeType> AssertOp;
12126 if (Arg.hasAttribute(Attribute::SExt))
12127 AssertOp = ISD::AssertSext;
12128 else if (Arg.hasAttribute(Attribute::ZExt))
12129 AssertOp = ISD::AssertZext;
12130
12131 SDValue OutVal =
12132 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
12133 NewRoot, F.getCallingConv(), AssertOp);
12134
12135 FPClassTest NoFPClass = Arg.getNoFPClass();
12136 if (NoFPClass != fcNone) {
12137 SDValue SDNoFPClass = DAG.getTargetConstant(
12138 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
12139 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
12140 OutVal, SDNoFPClass);
12141 }
12142 ArgValues.push_back(OutVal);
12143 }
12144
12145 i += NumParts;
12146 }
12147
12148 // We don't need to do anything else for unused arguments.
12149 if (ArgValues.empty())
12150 continue;
12151
12152 // Note down frame index.
12153 if (FrameIndexSDNode *FI =
12154 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
12155 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12156
12157 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
12158 SDB->getCurSDLoc());
12159
12160 SDB->setValue(&Arg, Res);
12161 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
12162 // We want to associate the argument with the frame index, among
12163 // involved operands, that correspond to the lowest address. The
12164 // getCopyFromParts function, called earlier, is swapping the order of
12165 // the operands to BUILD_PAIR depending on endianness. The result of
12166 // that swapping is that the least significant bits of the argument will
12167 // be in the first operand of the BUILD_PAIR node, and the most
12168 // significant bits will be in the second operand.
12169 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
12170 if (LoadSDNode *LNode =
12171 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
12172 if (FrameIndexSDNode *FI =
12173 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
12174 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12175 }
12176
12177 // Analyses past this point are naive and don't expect an assertion.
12178 if (Res.getOpcode() == ISD::AssertZext)
12179 Res = Res.getOperand(0);
12180
12181 // Update the SwiftErrorVRegDefMap.
12182 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
12183 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12184 if (Reg.isVirtual())
12185 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
12186 Reg);
12187 }
12188
12189 // If this argument is live outside of the entry block, insert a copy from
12190 // wherever we got it to the vreg that other BB's will reference it as.
12191 if (Res.getOpcode() == ISD::CopyFromReg) {
12192 // If we can, though, try to skip creating an unnecessary vreg.
12193 // FIXME: This isn't very clean... it would be nice to make this more
12194 // general.
12195 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12196 if (Reg.isVirtual()) {
12197 FuncInfo->ValueMap[&Arg] = Reg;
12198 continue;
12199 }
12200 }
12201 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12202 FuncInfo->InitializeRegForValue(&Arg);
12203 SDB->CopyToExportRegsIfNeeded(&Arg);
12204 }
12205 }
12206
12207 if (!Chains.empty()) {
12208 Chains.push_back(NewRoot);
12209 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12210 }
12211
12212 DAG.setRoot(NewRoot);
12213
12214 assert(i == InVals.size() && "Argument register count mismatch!");
12215
12216 // If any argument copy elisions occurred and we have debug info, update the
12217 // stale frame indices used in the dbg.declare variable info table.
12218 if (!ArgCopyElisionFrameIndexMap.empty()) {
12219 for (MachineFunction::VariableDbgInfo &VI :
12220 MF->getInStackSlotVariableDbgInfo()) {
12221 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12222 if (I != ArgCopyElisionFrameIndexMap.end())
12223 VI.updateStackSlot(I->second);
12224 }
12225 }
12226
12227 // Finally, if the target has anything special to do, allow it to do so.
12229}
12230
12231/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12232/// ensure constants are generated when needed. Remember the virtual registers
12233/// that need to be added to the Machine PHI nodes as input. We cannot just
12234/// directly add them, because expansion might result in multiple MBB's for one
12235/// BB. As such, the start of the BB might correspond to a different MBB than
12236/// the end.
12237void
12238SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12239 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12240
12241 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12242
12243 // Check PHI nodes in successors that expect a value to be available from this
12244 // block.
12245 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12246 if (!isa<PHINode>(SuccBB->begin())) continue;
12247 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12248
12249 // If this terminator has multiple identical successors (common for
12250 // switches), only handle each succ once.
12251 if (!SuccsHandled.insert(SuccMBB).second)
12252 continue;
12253
12255
12256 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12257 // nodes and Machine PHI nodes, but the incoming operands have not been
12258 // emitted yet.
12259 for (const PHINode &PN : SuccBB->phis()) {
12260 // Ignore dead phi's.
12261 if (PN.use_empty())
12262 continue;
12263
12264 // Skip empty types
12265 if (PN.getType()->isEmptyTy())
12266 continue;
12267
12268 Register Reg;
12269 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12270
12271 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12272 Register &RegOut = ConstantsOut[C];
12273 if (!RegOut) {
12274 RegOut = FuncInfo.CreateRegs(&PN);
12275 // We need to zero/sign extend ConstantInt phi operands to match
12276 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12277 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12278 if (auto *CI = dyn_cast<ConstantInt>(C))
12279 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12281 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12282 }
12283 Reg = RegOut;
12284 } else {
12286 FuncInfo.ValueMap.find(PHIOp);
12287 if (I != FuncInfo.ValueMap.end())
12288 Reg = I->second;
12289 else {
12290 assert(isa<AllocaInst>(PHIOp) &&
12291 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12292 "Didn't codegen value into a register!??");
12293 Reg = FuncInfo.CreateRegs(&PN);
12295 }
12296 }
12297
12298 // Remember that this register needs to added to the machine PHI node as
12299 // the input for this MBB.
12300 SmallVector<EVT, 4> ValueVTs;
12301 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12302 for (EVT VT : ValueVTs) {
12303 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12304 for (unsigned i = 0; i != NumRegisters; ++i)
12305 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12306 Reg += NumRegisters;
12307 }
12308 }
12309 }
12310
12311 ConstantsOut.clear();
12312}
12313
12314MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12316 if (++I == FuncInfo.MF->end())
12317 return nullptr;
12318 return &*I;
12319}
12320
12321/// During lowering new call nodes can be created (such as memset, etc.).
12322/// Those will become new roots of the current DAG, but complications arise
12323/// when they are tail calls. In such cases, the call lowering will update
12324/// the root, but the builder still needs to know that a tail call has been
12325/// lowered in order to avoid generating an additional return.
12326void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12327 // If the node is null, we do have a tail call.
12328 if (MaybeTC.getNode() != nullptr)
12329 DAG.setRoot(MaybeTC);
12330 else
12331 HasTailCall = true;
12332}
12333
12334void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12335 MachineBasicBlock *SwitchMBB,
12336 MachineBasicBlock *DefaultMBB) {
12337 MachineFunction *CurMF = FuncInfo.MF;
12338 MachineBasicBlock *NextMBB = nullptr;
12340 if (++BBI != FuncInfo.MF->end())
12341 NextMBB = &*BBI;
12342
12343 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12344
12345 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12346
12347 if (Size == 2 && W.MBB == SwitchMBB) {
12348 // If any two of the cases has the same destination, and if one value
12349 // is the same as the other, but has one bit unset that the other has set,
12350 // use bit manipulation to do two compares at once. For example:
12351 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12352 // TODO: This could be extended to merge any 2 cases in switches with 3
12353 // cases.
12354 // TODO: Handle cases where W.CaseBB != SwitchBB.
12355 CaseCluster &Small = *W.FirstCluster;
12356 CaseCluster &Big = *W.LastCluster;
12357
12358 if (Small.Low == Small.High && Big.Low == Big.High &&
12359 Small.MBB == Big.MBB) {
12360 const APInt &SmallValue = Small.Low->getValue();
12361 const APInt &BigValue = Big.Low->getValue();
12362
12363 // Check that there is only one bit different.
12364 APInt CommonBit = BigValue ^ SmallValue;
12365 if (CommonBit.isPowerOf2()) {
12366 SDValue CondLHS = getValue(Cond);
12367 EVT VT = CondLHS.getValueType();
12368 SDLoc DL = getCurSDLoc();
12369
12370 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12371 DAG.getConstant(CommonBit, DL, VT));
12372 SDValue Cond = DAG.getSetCC(
12373 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12374 ISD::SETEQ);
12375
12376 // Update successor info.
12377 // Both Small and Big will jump to Small.BB, so we sum up the
12378 // probabilities.
12379 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12380 if (BPI)
12381 addSuccessorWithProb(
12382 SwitchMBB, DefaultMBB,
12383 // The default destination is the first successor in IR.
12384 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12385 else
12386 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12387
12388 // Insert the true branch.
12389 SDValue BrCond =
12390 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12391 DAG.getBasicBlock(Small.MBB));
12392 // Insert the false branch.
12393 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12394 DAG.getBasicBlock(DefaultMBB));
12395
12396 DAG.setRoot(BrCond);
12397 return;
12398 }
12399 }
12400 }
12401
12402 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12403 // Here, we order cases by probability so the most likely case will be
12404 // checked first. However, two clusters can have the same probability in
12405 // which case their relative ordering is non-deterministic. So we use Low
12406 // as a tie-breaker as clusters are guaranteed to never overlap.
12407 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12408 [](const CaseCluster &a, const CaseCluster &b) {
12409 return a.Prob != b.Prob ?
12410 a.Prob > b.Prob :
12411 a.Low->getValue().slt(b.Low->getValue());
12412 });
12413
12414 // Rearrange the case blocks so that the last one falls through if possible
12415 // without changing the order of probabilities.
12416 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12417 --I;
12418 if (I->Prob > W.LastCluster->Prob)
12419 break;
12420 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12421 std::swap(*I, *W.LastCluster);
12422 break;
12423 }
12424 }
12425 }
12426
12427 // Compute total probability.
12428 BranchProbability DefaultProb = W.DefaultProb;
12429 BranchProbability UnhandledProbs = DefaultProb;
12430 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12431 UnhandledProbs += I->Prob;
12432
12433 MachineBasicBlock *CurMBB = W.MBB;
12434 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12435 bool FallthroughUnreachable = false;
12436 MachineBasicBlock *Fallthrough;
12437 if (I == W.LastCluster) {
12438 // For the last cluster, fall through to the default destination.
12439 Fallthrough = DefaultMBB;
12440 FallthroughUnreachable = isa<UnreachableInst>(
12441 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12442 } else {
12443 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12444 CurMF->insert(BBI, Fallthrough);
12445 // Put Cond in a virtual register to make it available from the new blocks.
12447 }
12448 UnhandledProbs -= I->Prob;
12449
12450 switch (I->Kind) {
12451 case CC_JumpTable: {
12452 // FIXME: Optimize away range check based on pivot comparisons.
12453 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12454 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12455
12456 // The jump block hasn't been inserted yet; insert it here.
12457 MachineBasicBlock *JumpMBB = JT->MBB;
12458 CurMF->insert(BBI, JumpMBB);
12459
12460 auto JumpProb = I->Prob;
12461 auto FallthroughProb = UnhandledProbs;
12462
12463 // If the default statement is a target of the jump table, we evenly
12464 // distribute the default probability to successors of CurMBB. Also
12465 // update the probability on the edge from JumpMBB to Fallthrough.
12466 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12467 SE = JumpMBB->succ_end();
12468 SI != SE; ++SI) {
12469 if (*SI == DefaultMBB) {
12470 JumpProb += DefaultProb / 2;
12471 FallthroughProb -= DefaultProb / 2;
12472 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12473 JumpMBB->normalizeSuccProbs();
12474 break;
12475 }
12476 }
12477
12478 // If the default clause is unreachable, propagate that knowledge into
12479 // JTH->FallthroughUnreachable which will use it to suppress the range
12480 // check.
12481 //
12482 // However, don't do this if we're doing branch target enforcement,
12483 // because a table branch _without_ a range check can be a tempting JOP
12484 // gadget - out-of-bounds inputs that are impossible in correct
12485 // execution become possible again if an attacker can influence the
12486 // control flow. So if an attacker doesn't already have a BTI bypass
12487 // available, we don't want them to be able to get one out of this
12488 // table branch.
12489 if (FallthroughUnreachable) {
12490 Function &CurFunc = CurMF->getFunction();
12491 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12492 JTH->FallthroughUnreachable = true;
12493 }
12494
12495 if (!JTH->FallthroughUnreachable)
12496 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12497 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12498 CurMBB->normalizeSuccProbs();
12499
12500 // The jump table header will be inserted in our current block, do the
12501 // range check, and fall through to our fallthrough block.
12502 JTH->HeaderBB = CurMBB;
12503 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12504
12505 // If we're in the right place, emit the jump table header right now.
12506 if (CurMBB == SwitchMBB) {
12507 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12508 JTH->Emitted = true;
12509 }
12510 break;
12511 }
12512 case CC_BitTests: {
12513 // FIXME: Optimize away range check based on pivot comparisons.
12514 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12515
12516 // The bit test blocks haven't been inserted yet; insert them here.
12517 for (BitTestCase &BTC : BTB->Cases)
12518 CurMF->insert(BBI, BTC.ThisBB);
12519
12520 // Fill in fields of the BitTestBlock.
12521 BTB->Parent = CurMBB;
12522 BTB->Default = Fallthrough;
12523
12524 BTB->DefaultProb = UnhandledProbs;
12525 // If the cases in bit test don't form a contiguous range, we evenly
12526 // distribute the probability on the edge to Fallthrough to two
12527 // successors of CurMBB.
12528 if (!BTB->ContiguousRange) {
12529 BTB->Prob += DefaultProb / 2;
12530 BTB->DefaultProb -= DefaultProb / 2;
12531 }
12532
12533 if (FallthroughUnreachable)
12534 BTB->FallthroughUnreachable = true;
12535
12536 // If we're in the right place, emit the bit test header right now.
12537 if (CurMBB == SwitchMBB) {
12538 visitBitTestHeader(*BTB, SwitchMBB);
12539 BTB->Emitted = true;
12540 }
12541 break;
12542 }
12543 case CC_Range: {
12544 const Value *RHS, *LHS, *MHS;
12545 ISD::CondCode CC;
12546 if (I->Low == I->High) {
12547 // Check Cond == I->Low.
12548 CC = ISD::SETEQ;
12549 LHS = Cond;
12550 RHS=I->Low;
12551 MHS = nullptr;
12552 } else {
12553 // Check I->Low <= Cond <= I->High.
12554 CC = ISD::SETLE;
12555 LHS = I->Low;
12556 MHS = Cond;
12557 RHS = I->High;
12558 }
12559
12560 // If Fallthrough is unreachable, fold away the comparison.
12561 if (FallthroughUnreachable)
12562 CC = ISD::SETTRUE;
12563
12564 // The false probability is the sum of all unhandled cases.
12565 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12566 getCurSDLoc(), I->Prob, UnhandledProbs);
12567
12568 if (CurMBB == SwitchMBB)
12569 visitSwitchCase(CB, SwitchMBB);
12570 else
12571 SL->SwitchCases.push_back(CB);
12572
12573 break;
12574 }
12575 }
12576 CurMBB = Fallthrough;
12577 }
12578}
12579
12580void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12581 const SwitchWorkListItem &W,
12582 Value *Cond,
12583 MachineBasicBlock *SwitchMBB) {
12584 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12585 "Clusters not sorted?");
12586 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12587
12588 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12589 SL->computeSplitWorkItemInfo(W);
12590
12591 // Use the first element on the right as pivot since we will make less-than
12592 // comparisons against it.
12593 CaseClusterIt PivotCluster = FirstRight;
12594 assert(PivotCluster > W.FirstCluster);
12595 assert(PivotCluster <= W.LastCluster);
12596
12597 CaseClusterIt FirstLeft = W.FirstCluster;
12598 CaseClusterIt LastRight = W.LastCluster;
12599
12600 const ConstantInt *Pivot = PivotCluster->Low;
12601
12602 // New blocks will be inserted immediately after the current one.
12604 ++BBI;
12605
12606 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12607 // we can branch to its destination directly if it's squeezed exactly in
12608 // between the known lower bound and Pivot - 1.
12609 MachineBasicBlock *LeftMBB;
12610 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12611 FirstLeft->Low == W.GE &&
12612 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12613 LeftMBB = FirstLeft->MBB;
12614 } else {
12615 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12616 FuncInfo.MF->insert(BBI, LeftMBB);
12617 WorkList.push_back(
12618 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12619 // Put Cond in a virtual register to make it available from the new blocks.
12621 }
12622
12623 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12624 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12625 // directly if RHS.High equals the current upper bound.
12626 MachineBasicBlock *RightMBB;
12627 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12628 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12629 RightMBB = FirstRight->MBB;
12630 } else {
12631 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12632 FuncInfo.MF->insert(BBI, RightMBB);
12633 WorkList.push_back(
12634 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12635 // Put Cond in a virtual register to make it available from the new blocks.
12637 }
12638
12639 // Create the CaseBlock record that will be used to lower the branch.
12640 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12641 getCurSDLoc(), LeftProb, RightProb);
12642
12643 if (W.MBB == SwitchMBB)
12644 visitSwitchCase(CB, SwitchMBB);
12645 else
12646 SL->SwitchCases.push_back(CB);
12647}
12648
12649// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12650// from the swith statement.
12652 BranchProbability PeeledCaseProb) {
12653 if (PeeledCaseProb == BranchProbability::getOne())
12655 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12656
12657 uint32_t Numerator = CaseProb.getNumerator();
12658 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12659 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12660}
12661
12662// Try to peel the top probability case if it exceeds the threshold.
12663// Return current MachineBasicBlock for the switch statement if the peeling
12664// does not occur.
12665// If the peeling is performed, return the newly created MachineBasicBlock
12666// for the peeled switch statement. Also update Clusters to remove the peeled
12667// case. PeeledCaseProb is the BranchProbability for the peeled case.
12668MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12669 const SwitchInst &SI, CaseClusterVector &Clusters,
12670 BranchProbability &PeeledCaseProb) {
12671 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12672 // Don't perform if there is only one cluster or optimizing for size.
12673 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12674 TM.getOptLevel() == CodeGenOptLevel::None ||
12675 SwitchMBB->getParent()->getFunction().hasMinSize())
12676 return SwitchMBB;
12677
12678 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12679 unsigned PeeledCaseIndex = 0;
12680 bool SwitchPeeled = false;
12681 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12682 CaseCluster &CC = Clusters[Index];
12683 if (CC.Prob < TopCaseProb)
12684 continue;
12685 TopCaseProb = CC.Prob;
12686 PeeledCaseIndex = Index;
12687 SwitchPeeled = true;
12688 }
12689 if (!SwitchPeeled)
12690 return SwitchMBB;
12691
12692 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12693 << TopCaseProb << "\n");
12694
12695 // Record the MBB for the peeled switch statement.
12696 MachineFunction::iterator BBI(SwitchMBB);
12697 ++BBI;
12698 MachineBasicBlock *PeeledSwitchMBB =
12699 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12700 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12701
12702 ExportFromCurrentBlock(SI.getCondition());
12703 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12704 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12705 nullptr, nullptr, TopCaseProb.getCompl()};
12706 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12707
12708 Clusters.erase(PeeledCaseIt);
12709 for (CaseCluster &CC : Clusters) {
12710 LLVM_DEBUG(
12711 dbgs() << "Scale the probablity for one cluster, before scaling: "
12712 << CC.Prob << "\n");
12713 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12714 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12715 }
12716 PeeledCaseProb = TopCaseProb;
12717 return PeeledSwitchMBB;
12718}
12719
12720void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12721 // Extract cases from the switch.
12722 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12723 CaseClusterVector Clusters;
12724 Clusters.reserve(SI.getNumCases());
12725 for (auto I : SI.cases()) {
12726 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12727 const ConstantInt *CaseVal = I.getCaseValue();
12728 BranchProbability Prob =
12729 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12730 : BranchProbability(1, SI.getNumCases() + 1);
12731 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12732 }
12733
12734 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12735
12736 // Cluster adjacent cases with the same destination. We do this at all
12737 // optimization levels because it's cheap to do and will make codegen faster
12738 // if there are many clusters.
12739 sortAndRangeify(Clusters);
12740
12741 // The branch probablity of the peeled case.
12742 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12743 MachineBasicBlock *PeeledSwitchMBB =
12744 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12745
12746 // If there is only the default destination, jump there directly.
12747 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12748 if (Clusters.empty()) {
12749 assert(PeeledSwitchMBB == SwitchMBB);
12750 SwitchMBB->addSuccessor(DefaultMBB);
12751 if (DefaultMBB != NextBlock(SwitchMBB)) {
12752 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12753 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12754 }
12755 return;
12756 }
12757
12758 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12759 DAG.getBFI());
12760 SL->findBitTestClusters(Clusters, &SI);
12761
12762 LLVM_DEBUG({
12763 dbgs() << "Case clusters: ";
12764 for (const CaseCluster &C : Clusters) {
12765 if (C.Kind == CC_JumpTable)
12766 dbgs() << "JT:";
12767 if (C.Kind == CC_BitTests)
12768 dbgs() << "BT:";
12769
12770 C.Low->getValue().print(dbgs(), true);
12771 if (C.Low != C.High) {
12772 dbgs() << '-';
12773 C.High->getValue().print(dbgs(), true);
12774 }
12775 dbgs() << ' ';
12776 }
12777 dbgs() << '\n';
12778 });
12779
12780 assert(!Clusters.empty());
12781 SwitchWorkList WorkList;
12782 CaseClusterIt First = Clusters.begin();
12783 CaseClusterIt Last = Clusters.end() - 1;
12784 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12785 // Scale the branchprobability for DefaultMBB if the peel occurs and
12786 // DefaultMBB is not replaced.
12787 if (PeeledCaseProb != BranchProbability::getZero() &&
12788 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12789 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12790 WorkList.push_back(
12791 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12792
12793 while (!WorkList.empty()) {
12794 SwitchWorkListItem W = WorkList.pop_back_val();
12795 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12796
12797 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12798 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12799 // For optimized builds, lower large range as a balanced binary tree.
12800 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12801 continue;
12802 }
12803
12804 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12805 }
12806}
12807
12808void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12809 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12810 auto DL = getCurSDLoc();
12811 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12812 setValue(&I, DAG.getStepVector(DL, ResultVT));
12813}
12814
12815void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12816 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12817 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12818
12819 SDLoc DL = getCurSDLoc();
12820 SDValue V = getValue(I.getOperand(0));
12821 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12822
12823 if (VT.isScalableVector()) {
12824 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12825 return;
12826 }
12827
12828 // Use VECTOR_SHUFFLE for the fixed-length vector
12829 // to maintain existing behavior.
12830 SmallVector<int, 8> Mask;
12831 unsigned NumElts = VT.getVectorMinNumElements();
12832 for (unsigned i = 0; i != NumElts; ++i)
12833 Mask.push_back(NumElts - 1 - i);
12834
12835 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12836}
12837
12838void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12839 unsigned Factor) {
12840 auto DL = getCurSDLoc();
12841 SDValue InVec = getValue(I.getOperand(0));
12842
12843 SmallVector<EVT, 4> ValueVTs;
12844 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12845 ValueVTs);
12846
12847 EVT OutVT = ValueVTs[0];
12848 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12849
12850 SmallVector<SDValue, 4> SubVecs(Factor);
12851 for (unsigned i = 0; i != Factor; ++i) {
12852 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12853 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12854 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12855 }
12856
12857 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12858 // from existing legalisation and combines.
12859 if (OutVT.isFixedLengthVector() && Factor == 2) {
12860 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12861 createStrideMask(0, 2, OutNumElts));
12862 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12863 createStrideMask(1, 2, OutNumElts));
12864 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12865 setValue(&I, Res);
12866 return;
12867 }
12868
12869 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12870 DAG.getVTList(ValueVTs), SubVecs);
12871 setValue(&I, Res);
12872}
12873
12874void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12875 unsigned Factor) {
12876 auto DL = getCurSDLoc();
12877 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12878 EVT InVT = getValue(I.getOperand(0)).getValueType();
12879 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12880
12881 SmallVector<SDValue, 8> InVecs(Factor);
12882 for (unsigned i = 0; i < Factor; ++i) {
12883 InVecs[i] = getValue(I.getOperand(i));
12884 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12885 "Expected VTs to be the same");
12886 }
12887
12888 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12889 // from existing legalisation and combines.
12890 if (OutVT.isFixedLengthVector() && Factor == 2) {
12891 unsigned NumElts = InVT.getVectorMinNumElements();
12892 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12893 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12894 createInterleaveMask(NumElts, 2)));
12895 return;
12896 }
12897
12898 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12899 SDValue Res =
12900 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12901
12903 for (unsigned i = 0; i < Factor; ++i)
12904 Results[i] = Res.getValue(i);
12905
12906 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12907 setValue(&I, Res);
12908}
12909
12910void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12911 SmallVector<EVT, 4> ValueVTs;
12912 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12913 ValueVTs);
12914 unsigned NumValues = ValueVTs.size();
12915 if (NumValues == 0) return;
12916
12917 SmallVector<SDValue, 4> Values(NumValues);
12918 SDValue Op = getValue(I.getOperand(0));
12919
12920 for (unsigned i = 0; i != NumValues; ++i)
12921 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12922 SDValue(Op.getNode(), Op.getResNo() + i));
12923
12925 DAG.getVTList(ValueVTs), Values));
12926}
12927
12928void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12929 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12930 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12931
12932 SDLoc DL = getCurSDLoc();
12933 SDValue V1 = getValue(I.getOperand(0));
12934 SDValue V2 = getValue(I.getOperand(1));
12935 const bool IsLeft = I.getIntrinsicID() == Intrinsic::vector_splice_left;
12936
12937 // VECTOR_SHUFFLE doesn't support a scalable or non-constant mask.
12938 if (VT.isScalableVector() || !isa<ConstantInt>(I.getOperand(2))) {
12939 SDValue Offset = DAG.getZExtOrTrunc(
12940 getValue(I.getOperand(2)), DL, TLI.getVectorIdxTy(DAG.getDataLayout()));
12941 setValue(&I, DAG.getNode(IsLeft ? ISD::VECTOR_SPLICE_LEFT
12943 DL, VT, V1, V2, Offset));
12944 return;
12945 }
12946 uint64_t Imm = cast<ConstantInt>(I.getOperand(2))->getZExtValue();
12947
12948 unsigned NumElts = VT.getVectorNumElements();
12949
12950 uint64_t Idx = IsLeft ? Imm : NumElts - Imm;
12951
12952 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12953 SmallVector<int, 8> Mask;
12954 for (unsigned i = 0; i < NumElts; ++i)
12955 Mask.push_back(Idx + i);
12956 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12957}
12958
12959// Consider the following MIR after SelectionDAG, which produces output in
12960// phyregs in the first case or virtregs in the second case.
12961//
12962// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12963// %5:gr32 = COPY $ebx
12964// %6:gr32 = COPY $edx
12965// %1:gr32 = COPY %6:gr32
12966// %0:gr32 = COPY %5:gr32
12967//
12968// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12969// %1:gr32 = COPY %6:gr32
12970// %0:gr32 = COPY %5:gr32
12971//
12972// Given %0, we'd like to return $ebx in the first case and %5 in the second.
12973// Given %1, we'd like to return $edx in the first case and %6 in the second.
12974//
12975// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12976// to a single virtreg (such as %0). The remaining outputs monotonically
12977// increase in virtreg number from there. If a callbr has no outputs, then it
12978// should not have a corresponding callbr landingpad; in fact, the callbr
12979// landingpad would not even be able to refer to such a callbr.
12982 // There is definitely at least one copy.
12983 assert(MI->getOpcode() == TargetOpcode::COPY &&
12984 "start of copy chain MUST be COPY");
12985 Reg = MI->getOperand(1).getReg();
12986
12987 // If the copied register in the first copy must be virtual.
12988 assert(Reg.isVirtual() && "expected COPY of virtual register");
12989 MI = MRI.def_begin(Reg)->getParent();
12990
12991 // There may be an optional second copy.
12992 if (MI->getOpcode() == TargetOpcode::COPY) {
12993 assert(Reg.isVirtual() && "expected COPY of virtual register");
12994 Reg = MI->getOperand(1).getReg();
12995 assert(Reg.isPhysical() && "expected COPY of physical register");
12996 } else {
12997 // The start of the chain must be an INLINEASM_BR.
12998 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12999 "end of copy chain MUST be INLINEASM_BR");
13000 }
13001
13002 return Reg;
13003}
13004
13005// We must do this walk rather than the simpler
13006// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
13007// otherwise we will end up with copies of virtregs only valid along direct
13008// edges.
13009void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
13010 SmallVector<EVT, 8> ResultVTs;
13011 SmallVector<SDValue, 8> ResultValues;
13012 const auto *CBR =
13013 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
13014
13015 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13016 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
13017 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
13018
13019 Register InitialDef = FuncInfo.ValueMap[CBR];
13020 SDValue Chain = DAG.getRoot();
13021
13022 // Re-parse the asm constraints string.
13023 TargetLowering::AsmOperandInfoVector TargetConstraints =
13024 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
13025 for (auto &T : TargetConstraints) {
13026 SDISelAsmOperandInfo OpInfo(T);
13027 if (OpInfo.Type != InlineAsm::isOutput)
13028 continue;
13029
13030 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
13031 // individual constraint.
13032 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
13033
13034 switch (OpInfo.ConstraintType) {
13037 // Fill in OpInfo.AssignedRegs.Regs.
13038 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
13039
13040 // getRegistersForValue may produce 1 to many registers based on whether
13041 // the OpInfo.ConstraintVT is legal on the target or not.
13042 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
13043 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
13044 if (OriginalDef.isPhysical())
13045 FuncInfo.MBB->addLiveIn(OriginalDef);
13046 // Update the assigned registers to use the original defs.
13047 Reg = OriginalDef;
13048 }
13049
13050 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
13051 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
13052 ResultValues.push_back(V);
13053 ResultVTs.push_back(OpInfo.ConstraintVT);
13054 break;
13055 }
13057 SDValue Flag;
13058 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
13059 OpInfo, DAG);
13060 ++InitialDef;
13061 ResultValues.push_back(V);
13062 ResultVTs.push_back(OpInfo.ConstraintVT);
13063 break;
13064 }
13065 default:
13066 break;
13067 }
13068 }
13070 DAG.getVTList(ResultVTs), ResultValues);
13071 setValue(&I, V);
13072}
return SDValue()
static unsigned getIntrinsicID(const SDNode *N)
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Function Alias Analysis Results
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI)
Returns an AttributeList representing the attributes applied to the return value of the given call.
Definition FastISel.cpp:942
#define Check(C,...)
static Value * getCondition(Instruction *I)
Hexagon Common GEP
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
static void getRegistersForValue(MachineFunction &MF, MachineIRBuilder &MIRBuilder, GISelAsmOperandInfo &OpInfo, GISelAsmOperandInfo &RefOpInfo)
Assign virtual/physical registers for the specified register operand.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
lazy value info
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static const Function * getCalledFunction(const Value *V)
This file provides utility analysis objects describing memory locations.
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
#define T
#define T1
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static unsigned getAddressSpace(const Value *V, unsigned MaxLookup)
MachineInstr unsigned OpIdx
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t High
uint64_t IntrinsicInst * II
OptimizedStructLayoutField Field
#define P(N)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static bool hasOnlySelectUsers(const Value *Cond)
static SDValue getLoadStackGuard(SelectionDAG &DAG, const SDLoc &DL, SDValue &Chain)
Create a LOAD_STACK_GUARD node, and let it carry the target specific global variable if there exists ...
static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index, SDValue &Scale, SelectionDAGBuilder *SDB, const BasicBlock *CurBB, uint64_t ElemSize)
static void failForInvalidBundles(const CallBase &I, StringRef Name, ArrayRef< uint32_t > AllowedBundles)
static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx, const SDLoc &DL, SmallVectorImpl< SDValue > &Ops, SelectionDAGBuilder &Builder)
Add a stack map intrinsic call's live variable operands to a stackmap or patchpoint target node's ope...
static const unsigned MaxParallelChains
static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
visitPow - Lower a pow intrinsic.
static const CallBase * FindPreallocatedCall(const Value *PreallocatedSetup)
Given a @llvm.call.preallocated.setup, return the corresponding preallocated call.
static cl::opt< unsigned > SwitchPeelThreshold("switch-peel-threshold", cl::Hidden, cl::init(66), cl::desc("Set the case probability threshold for peeling the case from a " "switch statement. A value greater than 100 will void this " "optimization"))
static cl::opt< bool > InsertAssertAlign("insert-assert-align", cl::init(true), cl::desc("Insert the experimental `assertalign` node."), cl::ReallyHidden)
static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin)
static bool handleDanglingVariadicDebugInfo(SelectionDAG &DAG, DILocalVariable *Variable, DebugLoc DL, unsigned Order, SmallVectorImpl< Value * > &Values, DIExpression *Expression)
static unsigned findMatchingInlineAsmOperand(unsigned OperandNo, const std::vector< SDValue > &AsmNodeOperands)
static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo, SDISelAsmOperandInfo &MatchingOpInfo, SelectionDAG &DAG)
Make sure that the output operand OpInfo and its corresponding input operand MatchingOpInfo have comp...
static void findUnwindDestinations(FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB, BranchProbability Prob, SmallVectorImpl< std::pair< MachineBasicBlock *, BranchProbability > > &UnwindDests)
When an invoke or a cleanupret unwinds to the next EH pad, there are many places it could ultimately ...
static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic)
static BranchProbability scaleCaseProbality(BranchProbability CaseProb, BranchProbability PeeledCaseProb)
static SDValue expandExp2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp2 - Lower an exp2 intrinsic.
static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue Scale, SelectionDAG &DAG, const TargetLowering &TLI)
static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt, const SDLoc &dl)
getF32Constant - Get 32-bit floating point constant.
static SDValue widenVectorToPartType(SelectionDAG &DAG, SDValue Val, const SDLoc &DL, EVT PartVT)
static SDValue expandLog10(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog10 - Lower a log10 intrinsic.
DenseMap< const Argument *, std::pair< const AllocaInst *, const StoreInst * > > ArgCopyElisionMapTy
static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv)
getCopyToPartsVector - Create a series of nodes that contain the specified value split into legal par...
static void getUnderlyingArgRegs(SmallVectorImpl< std::pair< Register, TypeSize > > &Regs, const SDValue &N)
static void getCopyToParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv=std::nullopt, ISD::NodeType ExtendKind=ISD::ANY_EXTEND)
getCopyToParts - Create a series of nodes that contain the specified value split into legal parts.
static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, SelectionDAGBuilder &Builder)
static SDValue expandLog2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog2 - Lower a log2 intrinsic.
static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location, SDISelAsmOperandInfo &OpInfo, SelectionDAG &DAG)
Get a direct memory input to behave well as an indirect operand.
static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel)
isOnlyUsedInEntryBlock - If the specified argument is only used in the entry block,...
static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V, const Twine &ErrMsg)
static bool collectInstructionDeps(SmallMapVector< const Instruction *, bool, 8 > *Deps, const Value *V, SmallMapVector< const Instruction *, bool, 8 > *Necessary=nullptr, unsigned Depth=0)
static void findArgumentCopyElisionCandidates(const DataLayout &DL, FunctionLoweringInfo *FuncInfo, ArgCopyElisionMapTy &ArgCopyElisionCandidates)
Scan the entry block of the function in FuncInfo for arguments that look like copies into a local all...
static bool isFunction(SDValue Op)
static SDValue GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, const SDLoc &dl)
GetExponent - Get the exponent:
static Register FollowCopyChain(MachineRegisterInfo &MRI, Register Reg)
static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS, SelectionDAG &DAG)
ExpandPowI - Expand a llvm.powi intrinsic.
static SDValue expandLog(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog - Lower a log intrinsic.
static SDValue getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC=std::nullopt, std::optional< ISD::NodeType > AssertOp=std::nullopt)
getCopyFromParts - Create a value that contains the specified legal parts combined into the value the...
static SDValue getLimitedPrecisionExp2(SDValue t0, const SDLoc &dl, SelectionDAG &DAG)
static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl)
GetSignificand - Get the significand and build it into a floating-point number with exponent of 1:
static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp - Lower an exp intrinsic.
static const MDNode * getRangeMetadata(const Instruction &I)
static cl::opt< unsigned, true > LimitFPPrecision("limit-float-precision", cl::desc("Generate low-precision inline sequences " "for some float libcalls"), cl::location(LimitFloatPrecision), cl::Hidden, cl::init(0))
static void tryToElideArgumentCopy(FunctionLoweringInfo &FuncInfo, SmallVectorImpl< SDValue > &Chains, DenseMap< int, int > &ArgCopyElisionFrameIndexMap, SmallPtrSetImpl< const Instruction * > &ElidedArgCopyInstrs, ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg, ArrayRef< SDValue > ArgVals, bool &ArgHasUses)
Try to elide argument copies from memory into a local alloca.
static unsigned LimitFloatPrecision
LimitFloatPrecision - Generate low-precision inline sequences for some float libcalls (6,...
static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC)
getCopyFromPartsVector - Create a value that contains the specified legal parts combined into the val...
static bool InBlock(const Value *V, const BasicBlock *BB)
static FPClassTest getNoFPClass(const Instruction &I)
static LLVM_ATTRIBUTE_ALWAYS_INLINE MVT::SimpleValueType getSimpleVT(const uint8_t *MatcherTable, size_t &MatcherIndex)
getSimpleVT - Decode a value in MatcherTable, if it's a VBR encoded value, use GetVBR to decode it.
This file defines the SmallPtrSet class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
uint16_t RegSizeInBits(const MCRegisterInfo &MRI, MCRegister RegNo)
Value * RHS
Value * LHS
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
static LLVM_ABI Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition APFloat.cpp:145
static LLVM_ABI const fltSemantics * getArbitraryFPSemantics(StringRef Format)
Returns the fltSemantics for a given arbitrary FP format string, or nullptr if invalid.
Definition APFloat.cpp:6089
Class for arbitrary precision integers.
Definition APInt.h:78
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
an instruction to allocate memory on the stack
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
LLVM_ABI std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Check if an argument has a given attribute.
Definition Function.cpp:338
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
A cache of @llvm.assume calls within a function.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h: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 if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This class represents a no-op cast from one type to another.
The address of a basic block.
Definition Constants.h:904
Conditional or Unconditional Branch instruction.
Analysis providing branch probability information.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static uint32_t getDenominator()
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
BranchProbability getCompl() const
static BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
CallingConv::ID getCallingConv() const
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI bool isTailCall() const
Tests if this call site is marked as a tail call.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:598
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1130
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
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:1037
uint64_t getZExtValue() const
Constant Vector Declarations.
Definition Constants.h:522
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:215
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
LLVM_ABI iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
A debug info location.
Definition DebugLoc.h:123
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:67
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
bool empty() const
Definition DenseMap.h:109
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:114
Diagnostic information for inline asm reporting.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:67
An instruction for ordering other memory operations.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:802
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
BranchProbabilityInfo * BPI
MachineBasicBlock * getMBB(const BasicBlock *BB) const
DenseMap< const AllocaInst *, int > StaticAllocaMap
StaticAllocaMap - Keep track of frame indices for fixed sized allocas in the entry block.
const LiveOutInfo * GetLiveOutRegInfo(Register Reg)
GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the register is a PHI destinat...
MachineBasicBlock * MBB
MBB - The current block.
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Type * getParamType(unsigned i) const
Parameter type accessors.
Type * getReturnType() const
Data structure describing the variable locations in a function.
const BasicBlock & getEntryBlock() const
Definition Function.h: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:741
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:729
Garbage collection metadata for a single function.
Definition GCMetadata.h:80
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
bool isInBounds() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
bool hasDLLImportStorageClass() const
Module * getParent()
Get the module that this global value is contained inside of...
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
This instruction inserts a struct field of array element value into an aggregate value.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
@ MIN_INT_BITS
Minimum number of bits that can be specified.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
A helper class to return the specified delimiter string after the first invocation of operator String...
An instruction for reading from memory.
static LocationSize precise(uint64_t Value)
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI MCSymbol * getOrCreateFrameAllocSymbol(const Twine &FuncName, unsigned Idx)
Gets a symbol that will be defined to the final stack offset of a local variable after codegen.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1080
Machine Value Type.
@ INVALID_SIMPLE_VALUE_TYPE
uint64_t getScalarSizeInBits() const
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
ElementCount getVectorElementCount() const
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
bool bitsGE(MVT VT) const
Return true if this has no less bits than VT.
bool isScalarInteger() const
Return true if this is an integer, not including vectors.
static MVT getVectorVT(MVT VT, unsigned NumElements)
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
static MVT getIntegerVT(unsigned BitWidth)
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void setIsEHContTarget(bool V=true)
Indicates if this is a target of Windows EH Continuation Guard.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
MachineInstrBundleIterator< MachineInstr > iterator
void setIsEHScopeEntry(bool V=true)
Indicates if this is the entry block of an EH scope, i.e., the block that that used to have a catchpa...
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
int getStackProtectorIndex() const
Return the index for the stack protector object.
void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased)
Set "maybe pointed to by an LLVM IR value" for an object.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
void setFunctionContextIndex(int I)
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site)
Map the begin label for a call site.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD)
Record annotations associated with a particular label.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
void setHasEHContTarget(bool V)
void addInvoke(MachineBasicBlock *LandingPad, MCSymbol *BeginLabel, MCSymbol *EndLabel)
Provide the begin and end labels of an invoke style call and associate it with a try landing pad bloc...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
Representation of each machine instruction.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
def_iterator def_begin(Register RegNo) const
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI MCRegister getLiveInPhysReg(Register VReg) const
getLiveInPhysReg - If VReg is a live-in virtual register, return the corresponding live-in physical r...
An SDNode that represents everything that will be needed to construct a MachineInstr.
bool contains(const KeyT &Key) const
Definition MapVector.h:146
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:116
static MemoryLocation getAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location after Ptr, while remaining within the underlying objec...
Metadata wrapper in the Value hierarchy.
Definition Metadata.h:184
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
Resume the propagation of an exception.
Return a value (possibly void), from a function.
Holds the information from a dbg_label node through SDISel.
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getOpcode() const
SelectionDAGBuilder - This is the common target-independent lowering implementation that is parameter...
SDValue getValue(const Value *V)
getValue - Return an SDValue for the given Value.
DenseMap< const Constant *, Register > ConstantsOut
void addDanglingDebugInfo(SmallVectorImpl< Value * > &Values, DILocalVariable *Var, DIExpression *Expr, bool IsVariadic, DebugLoc DL, unsigned Order)
Register a dbg_value which relies on a Value which we have not yet seen.
void visitDbgInfo(const Instruction &I)
void clearDanglingDebugInfo()
Clear the dangling debug information map.
void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall, bool IsMustTailCall, const BasicBlock *EHPadBB=nullptr, const TargetLowering::PtrAuthInfo *PAI=nullptr)
void clear()
Clear out the current SelectionDAG and the associated state and prepare this SelectionDAGBuilder obje...
void visitBitTestHeader(SwitchCG::BitTestBlock &B, MachineBasicBlock *SwitchBB)
visitBitTestHeader - This function emits necessary code to produce value suitable for "bit tests"
void LowerStatepoint(const GCStatepointInst &I, const BasicBlock *EHPadBB=nullptr)
std::unique_ptr< SDAGSwitchLowering > SL
SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, SDValue Op)
bool HasTailCall
This is set to true if a call in the current block has been translated as a tail call.
bool ShouldEmitAsBranches(const std::vector< SwitchCG::CaseBlock > &Cases)
If the set of cases should be emitted as a series of branches, return true.
void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
EmitBranchForMergedCondition - Helper method for FindMergedConditions.
void LowerDeoptimizeCall(const CallInst *CI)
void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB)
SwiftErrorValueTracking & SwiftError
Information about the swifterror values used throughout the function.
SDValue getNonRegisterValue(const Value *V)
getNonRegisterValue - Return an SDValue for the given Value, but don't look in FuncInfo....
const TargetTransformInfo * TTI
DenseMap< MachineBasicBlock *, SmallVector< unsigned, 4 > > LPadToCallSiteMap
Map a landing pad to the call site indexes.
SDValue lowerNoFPClassToAssertNoFPClass(SelectionDAG &DAG, const Instruction &I, SDValue Op)
void handleDebugDeclare(Value *Address, DILocalVariable *Variable, DIExpression *Expression, DebugLoc DL)
bool shouldKeepJumpConditionsTogether(const FunctionLoweringInfo &FuncInfo, const BranchInst &I, Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs, TargetLoweringBase::CondMergingParams Params) const
StatepointLoweringState StatepointLowering
State used while lowering a statepoint sequence (gc_statepoint, gc_relocate, and gc_result).
void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB, BranchProbability BranchProbToNext, Register Reg, SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB)
visitBitTestCase - this function produces one "bit test"
bool canTailCall(const CallBase &CB) const
void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, AttributeSet RetAttrs, bool IsPatchPoint)
Populate a CallLowerinInfo (into CLI) based on the properties of the call being lowered.
void CopyValueToVirtualRegister(const Value *V, Register Reg, ISD::NodeType ExtendType=ISD::ANY_EXTEND)
void salvageUnresolvedDbgValue(const Value *V, DanglingDebugInfo &DDI)
For the given dangling debuginfo record, perform last-ditch efforts to resolve the debuginfo to somet...
SmallVector< SDValue, 8 > PendingLoads
Loads are not emitted to the program immediately.
GCFunctionInfo * GFI
Garbage collection metadata for the function.
void init(GCFunctionInfo *gfi, BatchAAResults *BatchAA, AssumptionCache *AC, const TargetLibraryInfo *li, const TargetTransformInfo &TTI)
SDValue getRoot()
Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) items.
void ExportFromCurrentBlock(const Value *V)
ExportFromCurrentBlock - If this condition isn't known to be exported from the current basic block,...
void resolveOrClearDbgInfo()
Evict any dangling debug information, attempting to salvage it first.
std::pair< SDValue, SDValue > lowerInvokable(TargetLowering::CallLoweringInfo &CLI, const BasicBlock *EHPadBB=nullptr)
SDValue getMemoryRoot()
Return the current virtual root of the Selection DAG, flushing any PendingLoad items.
void resolveDanglingDebugInfo(const Value *V, SDValue Val)
If we saw an earlier dbg_value referring to V, generate the debug data structures now that we've seen...
void visit(const Instruction &I)
void dropDanglingDebugInfo(const DILocalVariable *Variable, const DIExpression *Expr)
If we have dangling debug info that describes Variable, or an overlapping part of variable considerin...
SDValue getCopyFromRegs(const Value *V, Type *Ty)
If there was virtual register allocated for the value V emit CopyFromReg of the specified type Ty.
void CopyToExportRegsIfNeeded(const Value *V)
CopyToExportRegsIfNeeded - If the given value has virtual registers created for it,...
void handleKillDebugValue(DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order)
Create a record for a kill location debug intrinsic.
void visitJumpTable(SwitchCG::JumpTable &JT)
visitJumpTable - Emit JumpTable node in the current MBB
SDValue getFPOperationRoot(fp::ExceptionBehavior EB)
Return the current virtual root of the Selection DAG, flushing PendingConstrainedFP or PendingConstra...
void visitJumpTableHeader(SwitchCG::JumpTable &JT, SwitchCG::JumpTableHeader &JTH, MachineBasicBlock *SwitchBB)
visitJumpTableHeader - This function emits necessary code to produce index in the JumpTable from swit...
void LowerCallSiteWithPtrAuthBundle(const CallBase &CB, const BasicBlock *EHPadBB)
static const unsigned LowestSDNodeOrder
Lowest valid SDNodeOrder.
FunctionLoweringInfo & FuncInfo
Information about the function as a whole.
void setValue(const Value *V, SDValue NewN)
void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, Instruction::BinaryOps Opc, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
const TargetLibraryInfo * LibInfo
bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB)
void visitSPDescriptorParent(StackProtectorDescriptor &SPD, MachineBasicBlock *ParentBB)
Codegen a new tail for a stack protector check ParentMBB which has had its tail spliced into a stack ...
bool handleDebugValue(ArrayRef< const Value * > Values, DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order, bool IsVariadic)
For a given list of Values, attempt to create and record a SDDbgValue in the SelectionDAG.
SDValue getControlRoot()
Similar to getRoot, but instead of flushing all the PendingLoad items, flush all the PendingExports (...
void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last)
When an MBB was split during scheduling, update the references that need to refer to the last resulti...
SDValue getValueImpl(const Value *V)
getValueImpl - Helper function for getValue and getNonRegisterValue.
void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB)
visitSwitchCase - Emits the necessary code to represent a single node in the binary search tree resul...
void visitSPDescriptorFailure(StackProtectorDescriptor &SPD)
Codegen the failure basic block for a stack protector check.
std::unique_ptr< FunctionLoweringInfo > FuncInfo
SmallPtrSet< const Instruction *, 4 > ElidedArgCopyInstrs
const TargetLowering * TLI
MachineRegisterInfo * RegInfo
std::unique_ptr< SwiftErrorValueTracking > SwiftError
virtual void emitFunctionEntryCode()
std::unique_ptr< SelectionDAGBuilder > SDB
virtual std::pair< SDValue, SDValue > 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 getExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT, unsigned Opcode)
Convert Op, which must be of integer type, to the integer type VT, by either any/sign/zero-extending ...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
const TargetSubtargetInfo & getSubtarget() const
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
const TargetLowering & getTargetLoweringInfo() const
static constexpr unsigned MaxRecursionDepth
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
const DataLayout & getDataLayout() const
SDValue getTargetFrameIndex(int FI, EVT VT)
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
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.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void swap(SmallVectorImpl &RHS)
void resize(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Encapsulates all of the information needed to generate a stack protector check, and signals to isel w...
MachineBasicBlock * getSuccessMBB()
MachineBasicBlock * getFailureMBB()
MachineBasicBlock * getParentMBB()
bool shouldEmitFunctionBasedCheckStackProtector() const
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:137
Multiway switch.
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Provides information about what library functions are available for the current target.
virtual Align getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Returns the desired alignment for ByVal or InAlloca aggregate function arguments in the caller parame...
virtual bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, EVT) const
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
EVT getMemValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Function * getSSPStackGuardCheck(const Module &M, const LibcallLoweringInfo &Libcalls) const
If the target has a standard stack protection check function that performs validation and error handl...
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
virtual bool useStackGuardXorFP() const
If this function returns true, stack protection checks should XOR the frame pointer (or whichever poi...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
virtual bool isLegalScaleForGatherScatter(uint64_t Scale, uint64_t ElemSize) const
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
virtual unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain targets require unusual breakdowns of certain types.
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const
Return the number of registers that this ValueType will eventually require.
unsigned getBitWidthForCttzElements(Type *RetTy, ElementCount EC, bool ZeroIsPoison, const ConstantRange *VScaleRange) const
Return the minimum number of bits required to hold the maximum possible number of trailing zero vecto...
virtual bool shouldExtendGSIndex(EVT VT, EVT &EltTy) const
Returns true if the index type for a masked gather/scatter requires extending.
virtual unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
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.
MachineMemOperand::Flags getLoadMemOperandFlags(const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC=nullptr, const TargetLibraryInfo *LibInfo=nullptr) const
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
MVT getProgramPointerTy(const DataLayout &DL) const
Return the type for code pointers, which is determined by the program address space specified through...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
virtual bool shouldExpandVectorMatch(EVT VT, unsigned SearchSize) const
Return true if the @llvm.experimental.vector.match intrinsic should be expanded for vector type ‘VT’ ...
virtual 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 shouldExpandCttzElements(EVT VT) const
Return true if the @llvm.experimental.cttz.elts intrinsic should be expanded using generic code in Se...
virtual bool signExtendConstant(const ConstantInt *C) const
Return true if this constant should be sign extended when promoting to a larger type.
virtual Value * getSDagStackGuard(const Module &M, const LibcallLoweringInfo &Libcalls) const
Return the variable that's previously inserted by insertSSPDeclarations, if any, otherwise return nul...
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
virtual Register getExceptionPointerRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception address on entry to an ...
bool supportsUnalignedAtomics() const
Whether the target supports unaligned atomic operations.
std::vector< ArgListEntry > ArgListTy
bool isBeneficialToExpandPowI(int64_t Exponent, bool OptForSize) const
Return true if it is beneficial to expand an @llvm.powi.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual Register getExceptionSelectorRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception typeid on entry to a la...
virtual MVT getPointerMemTy(const DataLayout &DL, uint32_t AS=0) const
Return the in-memory pointer type for the given address space, defaults to the pointer type from the ...
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
virtual MVT getVPExplicitVectorLengthTy() const
Returns the type to be used for the EVL/AVL operand of VP nodes: ISD::VP_ADD, ISD::VP_SUB,...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool supportKCFIBundles() const
Return true if the target supports kcfi operand bundles.
virtual bool supportPtrAuthBundles() const
Return true if the target supports ptrauth operand bundles.
virtual bool supportSwiftError() const
Return true if the target supports swifterror attribute.
virtual SDValue visitMaskedLoad(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue &NewLoad, SDValue Ptr, SDValue PassThru, SDValue Mask) const
virtual SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, const SDLoc &DL) const
virtual EVT getTypeForExtReturn(LLVMContext &Context, EVT VT, ISD::NodeType) const
Return the type that should be used to zero or sign extend a zeroext/signext integer return value.
virtual InlineAsm::ConstraintCode getInlineAsmMemConstraint(StringRef ConstraintCode) const
std::vector< AsmOperandInfo > AsmOperandInfoVector
SDValue expandIS_FPCLASS(EVT ResultVT, SDValue Op, FPClassTest Test, SDNodeFlags Flags, const SDLoc &DL, SelectionDAG &DAG) const
Expand check for floating point class.
virtual SDValue prepareVolatileOrAtomicLoad(SDValue Chain, const SDLoc &DL, SelectionDAG &DAG) const
This callback is used to prepare for a volatile or atomic load.
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
virtual bool splitValueIntoRegisterParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, std::optional< CallingConv::ID > CC) const
Target-specific splitting of values into parts that fit a register storing a legal type.
virtual SDValue joinRegisterPartsIntoValue(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, std::optional< CallingConv::ID > CC) const
Target-specific combining of register parts into its original value.
virtual SDValue LowerCall(CallLoweringInfo &, SmallVectorImpl< SDValue > &) const
This hook must be implemented to lower calls into the specified DAG.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual SDValue LowerAsmOutputForConstraint(SDValue &Chain, SDValue &Glue, const SDLoc &DL, const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, const CallBase &Call) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const
This callback is invoked for operations that are unsupported by the target, which are registered to u...
virtual bool functionArgumentNeedsConsecutiveRegisters(Type *Ty, CallingConv::ID CallConv, bool isVarArg, const DataLayout &DL) const
For some targets, an LLVM struct type must be broken down into multiple simple types,...
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo,...
virtual void CollectTargetIntrinsicOperands(const CallInst &I, SmallVectorImpl< SDValue > &Ops, SelectionDAG &DAG) const
virtual SDValue visitMaskedStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue Ptr, SDValue Val, SDValue Mask) const
virtual bool useLoadStackGuardNode(const Module &M) const
If this function returns true, SelectionDAGBuilder emits a LOAD_STACK_GUARD node when it is lowering ...
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::LibcallImpl LibcallImpl, EVT RetVT, ArrayRef< SDValue > Ops, MakeLibCallOptions CallOptions, const SDLoc &dl, SDValue Chain=SDValue()) const
Returns a pair of (return value, chain).
virtual void LowerOperationWrapper(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const
This callback is invoked by the type legalizer to legalize nodes with an illegal operand type but leg...
virtual bool isInlineAsmTargetBranch(const SmallVectorImpl< StringRef > &AsmStrs, unsigned OpNo) const
On x86, return true if the operand with index OpNo is a CALL or JUMP instruction, which can use eithe...
virtual MVT getJumpTableRegTy(const DataLayout &DL) const
virtual bool CanLowerReturn(CallingConv::ID, MachineFunction &, bool, const SmallVectorImpl< ISD::OutputArg > &, LLVMContext &, const Type *RetTy) const
This hook should be implemented to check whether the return values described by the Outs array can fi...
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
unsigned getID() const
Return the register class ID number.
iterator begin() const
begin/end - Return all of the registers in this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
@ TCK_Latency
The latency of instruction.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
Definition Type.cpp:180
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:293
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:234
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:300
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_iterator op_begin()
Definition User.h: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:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:440
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:259
iterator_range< user_iterator > users()
Definition Value.h:427
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:713
bool use_empty() const
Definition Value.h:347
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
const ParentTy * getParent() const
Definition ilist_node.h:34
A raw_ostream that writes to an std::string.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ STACKRESTORE
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain.
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:788
@ CONVERGENCECTRL_ANCHOR
The llvm.experimental.convergence.* intrinsics.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:511
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ SET_FPENV
Sets the current floating-point environment.
@ LOOP_DEPENDENCE_RAW_MASK
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ COND_LOOP
COND_LOOP is a conditional branch to self, used for implementing efficient conditional traps.
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition ISDOpcodes.h:168
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:600
@ STACKADDRESS
STACKADDRESS - Represents the llvm.stackaddress intrinsic.
Definition ISDOpcodes.h:127
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:779
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:394
@ VAEND
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE.
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ RESET_FPENV
Set floating-point environment to default state.
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:400
@ SET_FPMODE
Sets the current dynamic floating-point control modes.
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:853
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:518
@ VECTOR_FIND_LAST_ACTIVE
Finds the index of the last active mask element Operands: Mask.
@ FMODF
FMODF - Decomposes the operand into integral and fractional parts, each having the same type and sign...
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ FSINCOSPI
FSINCOSPI - Compute both the sine and cosine times pi more accurately than FSINCOS(pi*x),...
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ EH_SJLJ_SETUP_DISPATCH
OUTCHAIN = EH_SJLJ_SETUP_DISPATCH(INCHAIN) The target initializes the dispatch table here.
Definition ISDOpcodes.h:172
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:880
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:584
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:747
@ ATOMIC_FENCE
OUTCHAIN = ATOMIC_FENCE(INCHAIN, ordering, scope) This corresponds to the fence instruction.
@ RESET_FPMODE
Sets default dynamic floating-point control modes.
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:528
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition ISDOpcodes.h:515
@ FAKE_USE
FAKE_USE represents a use of the operand but does not do anything.
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:993
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:774
@ INIT_TRAMPOLINE
INIT_TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:407
@ CONVERT_FROM_ARBITRARY_FP
CONVERT_FROM_ARBITRARY_FP - This operator converts from an arbitrary floating-point represented as an...
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ATOMIC_LOAD_USUB_SAT
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:156
@ ANNOTATION_LABEL
ANNOTATION_LABEL - Represents a mid basic block label used by annotations.
@ SET_ROUNDING
Set rounding mode.
Definition ISDOpcodes.h:975
@ CONVERGENCECTRL_GLUE
This does not correspond to any convergence control intrinsic.
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:844
@ PREALLOCATED_SETUP
PREALLOCATED_SETUP - This has 2 operands: an input chain and a SRCVALUE with the preallocated call Va...
@ READSTEADYCOUNTER
READSTEADYCOUNTER - This corresponds to the readfixedcounter intrinsic.
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition ISDOpcodes.h:117
@ CONVERGENCECTRL_ENTRY
@ BR
Control flow instructions. These all have token chains.
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:787
@ PARTIAL_REDUCE_FMLA
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ PREALLOCATED_ARG
PREALLOCATED_ARG - This has 3 operands: an input chain, a SRCVALUE with the preallocated call Value,...
@ BRIND
BRIND - Indirect branch.
@ BR_JT
BR_JT - Jumptable branch.
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:635
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:541
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:548
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:796
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:672
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:970
@ CLEANUPRET
CLEANUPRET - Represents a return from a cleanup block funclet.
@ ATOMIC_LOAD_FMAXIMUM
@ GET_FPMODE
Reads the current dynamic floating-point control modes.
@ GET_FPENV
Gets the current floating-point environment.
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:765
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ PtrAuthGlobalAddress
A ptrauth constant.
Definition ISDOpcodes.h:100
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:614
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:139
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:576
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:850
@ DEBUGTRAP
DEBUGTRAP - Trap intended to get the attention of a debugger.
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ LOCAL_RECOVER
LOCAL_RECOVER - Represents the llvm.localrecover intrinsic.
Definition ISDOpcodes.h:135
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ UBSANTRAP
UBSANTRAP - Trap with an immediate describing the kind of sanitizer failure.
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ PATCHPOINT
The llvm.experimental.patchpoint.
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ ATOMIC_LOAD_FMINIMUM
@ DYNAMIC_STACKALLOC
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary.
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
Definition ISDOpcodes.h:653
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:727
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:640
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:413
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:978
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:805
@ PCMARKER
PCMARKER - This corresponds to the pcmarker intrinsic.
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:150
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ ATOMIC_LOAD_UDEC_WRAP
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:500
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:926
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ RELOC_NONE
Issue a no-op relocation against a given symbol at the current location.
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:739
@ TRAP
TRAP - Trapping instruction.
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:735
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:657
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition ISDOpcodes.h:427
@ STACKMAP
The llvm.experimental.stackmap intrinsic.
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:959
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:699
@ SPONENTRY
SPONENTRY - Represents the llvm.sponentry intrinsic.
Definition ISDOpcodes.h:122
@ CLEAR_CACHE
llvm.clear_cache intrinsic Operands: Input Chain, Start Addres, End Address Outputs: Output Chain
@ CONVERGENCECTRL_LOOP
@ INLINEASM
INLINEASM - Represents an inline asm block.
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:945
@ VECREDUCE_FMINIMUM
@ EH_SJLJ_SETJMP
RESULT, OUTCHAIN = EH_SJLJ_SETJMP(INCHAIN, buffer) This corresponds to the eh.sjlj....
Definition ISDOpcodes.h:162
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:856
@ BRCOND
BRCOND - Conditional branch.
@ VECREDUCE_SEQ_FMUL
@ CATCHRET
CATCHRET - Represents a return from a catch block funclet.
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:534
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:624
@ GET_DYNAMIC_AREA_OFFSET
GET_DYNAMIC_AREA_OFFSET - get offset from native SP to the address of the most recent dynamic alloca.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ ADJUST_TRAMPOLINE
ADJUST_TRAMPOLINE - This corresponds to the adjust_trampoline intrinsic.
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
@ LOOP_DEPENDENCE_WAR_MASK
The llvm.loop.dependence.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Offsets
Offsets in bytes from the start of the input buffer.
std::pair< JumpTableHeader, JumpTable > JumpTableBlock
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::vector< CaseCluster > CaseClusterVector
@ CC_Range
A cluster of adjacent case labels with the same destination, or just one case.
@ CC_JumpTable
A cluster of cases suitable for jump table lowering.
@ CC_BitTests
A cluster of cases suitable for bit test lowering.
SmallVector< SwitchWorkListItem, 4 > SwitchWorkList
CaseClusterVector::iterator CaseClusterIt
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebStrict
This corresponds to "fpexcept.strict".
Definition FPEnv.h:42
@ ebMayTrap
This corresponds to "fpexcept.maytrap".
Definition FPEnv.h:41
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
constexpr float log2ef
Definition MathExtras.h:51
constexpr double e
constexpr float ln2f
Definition MathExtras.h:49
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp: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:1739
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:1669
LLVM_ABI void GetReturnInfo(CallingConv::ID CC, Type *ReturnType, AttributeList attr, SmallVectorImpl< ISD::OutputArg > &Outs, const TargetLowering &TLI, const DataLayout &DL)
Given an LLVM IR type and return type attributes, compute the return value EVTs and flags,...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs=nullptr, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition Analysis.cpp:119
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
auto successors(const MachineBasicBlock *BB)
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
static ConstantRange getRange(Value *Op, SCCPSolver &Solver, const SmallPtrSetImpl< Value * > &InsertedValues)
Helper for getting ranges from Solver.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
auto cast_or_null(const Y &Val)
Definition Casting.h:714
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:546
gep_type_iterator gep_type_end(const User *GEP)
constexpr auto equal_to(T &&Arg)
Functor variant of std::equal_to that can be used as a UnaryPredicate in functional algorithms like a...
Definition STLExtras.h:2173
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
Definition STLExtras.h:1152
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
void ComputeValueTypes(const DataLayout &DL, Type *Ty, SmallVectorImpl< Type * > &Types, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
Given an LLVM IR type, compute non-aggregate subtypes.
Definition Analysis.cpp:72
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
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:854
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
generic_gep_type_iterator<> gep_type_iterator
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
auto succ_size(const MachineBasicBlock *BB)
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition STLExtras.h:300
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred)
getFCmpCondCode - Return the ISD condition code corresponding to the given LLVM IR floating-point con...
Definition Analysis.cpp: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:2292
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Global
Append to llvm.global_dtors.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
LLVM_ABI llvm::SmallVector< int, 16 > createInterleaveMask(unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ And
Bitwise or logical AND of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:539
DWARFExpression::Operation Op
ISD::CondCode getFCmpCodeWithoutNaN(ISD::CondCode CC)
getFCmpCodeWithoutNaN - Given an ISD condition code comparing floats, return the equivalent code if w...
Definition Analysis.cpp:225
ArrayRef(const T &OneElt) -> ArrayRef< T >
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
LLVM_ABI std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition FPEnv.cpp:25
gep_type_iterator gep_type_begin(const User *GEP)
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2192
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:1947
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:2166
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
unsigned ComputeLinearIndex(Type *Ty, const unsigned *Indices, const unsigned *IndicesEnd, unsigned CurIndex=0)
Compute the linearized index of a member in a nested aggregate/struct/array.
Definition Analysis.cpp:33
T bit_floor(T Value)
Returns the largest integral power of two no greater than Value if Value is nonzero.
Definition bit.h:330
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
#define NC
Definition regutils.h:42
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:403
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
uint64_t getScalarStoreSize() const
Definition ValueTypes.h:410
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:292
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:308
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
ElementCount getVectorElementCount() const
Definition ValueTypes.h:358
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:381
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:367
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:393
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
EVT changeVectorElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:98
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:324
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
bool isRISCVVectorTuple() const
Return true if this is a vector value type.
Definition ValueTypes.h:187
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:389
bool isFixedLengthVector() const
Definition ValueTypes.h:189
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:331
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:300
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:182
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:336
EVT changeElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a type whose attributes match ourselves with the exception of the element type that i...
Definition ValueTypes.h:121
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:165
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:344
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
void setPointerAddrSpace(unsigned AS)
InputArg - This struct carries flags and type information about a single incoming (formal) argument o...
static const unsigned NoArgIndex
Sentinel value for implicit machine-level input arguments.
OutputArg - This struct carries flags and a value for a single outgoing (actual) argument or outgoing...
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber/label.
Definition InlineAsm.h:128
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:264
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
A lightweight accessor for an operand bundle meant to be passed around by value.
This struct represents the registers (physical or virtual) that a particular set of values is assigne...
SmallVector< std::pair< Register, TypeSize >, 4 > getRegsAndSizes() const
Return a list of registers and their sizes.
RegsForValue()=default
SmallVector< unsigned, 4 > RegCount
This list holds the number of registers for each value.
SmallVector< EVT, 4 > ValueVTs
The value types of the values, which may not be legal, and may need be promoted or synthesized from o...
SmallVector< Register, 4 > Regs
This list holds the registers assigned to the values.
void AddInlineAsmOperands(InlineAsm::Kind Code, bool HasMatching, unsigned MatchingIdx, const SDLoc &dl, SelectionDAG &DAG, std::vector< SDValue > &Ops) const
Add this value to the specified inlineasm node operand list.
SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr) const
Emit a series of CopyFromReg nodes that copies from this value and returns the result as a ValueVTs v...
SmallVector< MVT, 4 > RegVTs
The value types of the registers.
void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr, ISD::NodeType PreferredExtendType=ISD::ANY_EXTEND) const
Emit a series of CopyToReg nodes that copies the specified value into the registers specified by this...
std::optional< CallingConv::ID > CallConv
Records if this value needs to be treated in an ABI dependant manner, different to normal type legali...
bool occupiesMultipleRegs() const
Check if the total RegCount is greater than one.
These are IR-level optimization flags that may be propagated to SDNodes.
void copyFMF(const FPMathOperator &FPMO)
Propagate the fast-math-flags from an IR FPMathOperator.
void setUnpredictable(bool b)
bool hasAllowReassociation() const
void setNoUnsignedWrap(bool b)
void setNoSignedWrap(bool b)
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:276
This structure is used to communicate between SelectionDAGBuilder and SDISel for the code generation ...
SDLoc DL
The debug location of the instruction this CaseBlock was produced from.
static CaseCluster range(const ConstantInt *Low, const ConstantInt *High, MachineBasicBlock *MBB, BranchProbability Prob)
Register Reg
The virtual register containing the index of the jump table entry to jump to.
MachineBasicBlock * Default
The MBB of the default bb, which is a successor of the range check MBB.
unsigned JTI
The JumpTableIndex for this jump table in the function.
MachineBasicBlock * MBB
The MBB into which to emit the code for the indirect jump.
std::optional< SDLoc > SL
The debug location of the instruction this JumpTable was produced from.
This contains information for each constraint that we are lowering.
TargetLowering::ConstraintType ConstraintType
Information about the constraint code, e.g.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setConvergent(bool Value=true)
CallLoweringInfo & setDeactivationSymbol(GlobalValue *Sym)
CallLoweringInfo & setCFIType(const ConstantInt *Type)
SmallVector< ISD::InputArg, 32 > Ins
Type * OrigRetTy
Original unlegalized return type.
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setIsPatchPoint(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setIsPreallocated(bool Value=true)
CallLoweringInfo & setConvergenceControlToken(SDValue Token)
SmallVector< ISD::OutputArg, 32 > Outs
Type * RetTy
Same as OrigRetTy, or partially legalized for soft float libcalls.
CallLoweringInfo & setChain(SDValue InChain)
CallLoweringInfo & setPtrAuth(PtrAuthInfo Value)
CallLoweringInfo & setCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList, AttributeSet ResultAttrs={})
This structure is used to pass arguments to makeLibCall function.
MakeLibCallOptions & setDiscardResult(bool Value=true)
This structure contains the information necessary for lowering pointer-authenticating indirect calls.
void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)