LLVM 22.0.0git
SelectionDAGBuilder.cpp
Go to the documentation of this file.
1//===- SelectionDAGBuilder.cpp - Selection-DAG building -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements routines for translating from LLVM IR into SelectionDAG IR.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SelectionDAGBuilder.h"
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/Twine.h"
26#include "llvm/Analysis/Loads.h"
57#include "llvm/IR/Argument.h"
58#include "llvm/IR/Attributes.h"
59#include "llvm/IR/BasicBlock.h"
60#include "llvm/IR/CFG.h"
61#include "llvm/IR/CallingConv.h"
62#include "llvm/IR/Constant.h"
64#include "llvm/IR/Constants.h"
65#include "llvm/IR/DataLayout.h"
66#include "llvm/IR/DebugInfo.h"
71#include "llvm/IR/Function.h"
73#include "llvm/IR/InlineAsm.h"
74#include "llvm/IR/InstrTypes.h"
77#include "llvm/IR/Intrinsics.h"
78#include "llvm/IR/IntrinsicsAArch64.h"
79#include "llvm/IR/IntrinsicsAMDGPU.h"
80#include "llvm/IR/IntrinsicsWebAssembly.h"
81#include "llvm/IR/LLVMContext.h"
83#include "llvm/IR/Metadata.h"
84#include "llvm/IR/Module.h"
85#include "llvm/IR/Operator.h"
87#include "llvm/IR/Statepoint.h"
88#include "llvm/IR/Type.h"
89#include "llvm/IR/User.h"
90#include "llvm/IR/Value.h"
91#include "llvm/MC/MCContext.h"
96#include "llvm/Support/Debug.h"
104#include <cstddef>
105#include <limits>
106#include <optional>
107#include <tuple>
108
109using namespace llvm;
110using namespace PatternMatch;
111using namespace SwitchCG;
112
113#define DEBUG_TYPE "isel"
114
115/// LimitFloatPrecision - Generate low-precision inline sequences for
116/// some float libcalls (6, 8 or 12 bits).
117static unsigned LimitFloatPrecision;
118
119static cl::opt<bool>
120 InsertAssertAlign("insert-assert-align", cl::init(true),
121 cl::desc("Insert the experimental `assertalign` node."),
123
125 LimitFPPrecision("limit-float-precision",
126 cl::desc("Generate low-precision inline sequences "
127 "for some float libcalls"),
129 cl::init(0));
130
132 "switch-peel-threshold", cl::Hidden, cl::init(66),
133 cl::desc("Set the case probability threshold for peeling the case from a "
134 "switch statement. A value greater than 100 will void this "
135 "optimization"));
136
137// Limit the width of DAG chains. This is important in general to prevent
138// DAG-based analysis from blowing up. For example, alias analysis and
139// load clustering may not complete in reasonable time. It is difficult to
140// recognize and avoid this situation within each individual analysis, and
141// future analyses are likely to have the same behavior. Limiting DAG width is
142// the safe approach and will be especially important with global DAGs.
143//
144// MaxParallelChains default is arbitrarily high to avoid affecting
145// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
146// sequence over this should have been converted to llvm.memcpy by the
147// frontend. It is easy to induce this behavior with .ll code such as:
148// %buffer = alloca [4096 x i8]
149// %data = load [4096 x i8]* %argPtr
150// store [4096 x i8] %data, [4096 x i8]* %buffer
151static const unsigned MaxParallelChains = 64;
152
154 const SDValue *Parts, unsigned NumParts,
155 MVT PartVT, EVT ValueVT, const Value *V,
156 SDValue InChain,
157 std::optional<CallingConv::ID> CC);
158
159/// getCopyFromParts - Create a value that contains the specified legal parts
160/// combined into the value they represent. If the parts combine to a type
161/// larger than ValueVT then AssertOp can be used to specify whether the extra
162/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
163/// (ISD::AssertSext).
164static SDValue
165getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts,
166 unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V,
167 SDValue InChain,
168 std::optional<CallingConv::ID> CC = std::nullopt,
169 std::optional<ISD::NodeType> AssertOp = std::nullopt) {
170 // Let the target assemble the parts if it wants to
171 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
172 if (SDValue Val = TLI.joinRegisterPartsIntoValue(DAG, DL, Parts, NumParts,
173 PartVT, ValueVT, CC))
174 return Val;
175
176 if (ValueVT.isVector())
177 return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT, V,
178 InChain, CC);
179
180 assert(NumParts > 0 && "No parts to assemble!");
181 SDValue Val = Parts[0];
182
183 if (NumParts > 1) {
184 // Assemble the value from multiple parts.
185 if (ValueVT.isInteger()) {
186 unsigned PartBits = PartVT.getSizeInBits();
187 unsigned ValueBits = ValueVT.getSizeInBits();
188
189 // Assemble the power of 2 part.
190 unsigned RoundParts = llvm::bit_floor(NumParts);
191 unsigned RoundBits = PartBits * RoundParts;
192 EVT RoundVT = RoundBits == ValueBits ?
193 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
194 SDValue Lo, Hi;
195
196 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
197
198 if (RoundParts > 2) {
199 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, PartVT, HalfVT, V,
200 InChain);
201 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, RoundParts / 2,
202 PartVT, HalfVT, V, InChain);
203 } else {
204 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
205 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
206 }
207
208 if (DAG.getDataLayout().isBigEndian())
209 std::swap(Lo, Hi);
210
211 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
212
213 if (RoundParts < NumParts) {
214 // Assemble the trailing non-power-of-2 part.
215 unsigned OddParts = NumParts - RoundParts;
216 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
217 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts, OddParts, PartVT,
218 OddVT, V, InChain, CC);
219
220 // Combine the round and odd parts.
221 Lo = Val;
222 if (DAG.getDataLayout().isBigEndian())
223 std::swap(Lo, Hi);
224 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
225 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
226 Hi = DAG.getNode(
227 ISD::SHL, DL, TotalVT, Hi,
228 DAG.getShiftAmountConstant(Lo.getValueSizeInBits(), TotalVT, DL));
229 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
230 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
231 }
232 } else if (PartVT.isFloatingPoint()) {
233 // FP split into multiple FP parts (for ppcf128)
234 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
235 "Unexpected split");
236 SDValue Lo, Hi;
237 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
238 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
239 if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
240 std::swap(Lo, Hi);
241 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
242 } else {
243 // FP split into integer parts (soft fp)
244 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
245 !PartVT.isVector() && "Unexpected split");
246 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
247 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V,
248 InChain, CC);
249 }
250 }
251
252 // There is now one part, held in Val. Correct it to match ValueVT.
253 // PartEVT is the type of the register class that holds the value.
254 // ValueVT is the type of the inline asm operation.
255 EVT PartEVT = Val.getValueType();
256
257 if (PartEVT == ValueVT)
258 return Val;
259
260 if (PartEVT.isInteger() && ValueVT.isFloatingPoint() &&
261 ValueVT.bitsLT(PartEVT)) {
262 // For an FP value in an integer part, we need to truncate to the right
263 // width first.
264 PartEVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
265 Val = DAG.getNode(ISD::TRUNCATE, DL, PartEVT, Val);
266 }
267
268 // Handle types that have the same size.
269 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
270 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
271
272 // Handle types with different sizes.
273 if (PartEVT.isInteger() && ValueVT.isInteger()) {
274 if (ValueVT.bitsLT(PartEVT)) {
275 // For a truncate, see if we have any information to
276 // indicate whether the truncated bits will always be
277 // zero or sign-extension.
278 if (AssertOp)
279 Val = DAG.getNode(*AssertOp, DL, PartEVT, Val,
280 DAG.getValueType(ValueVT));
281 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
282 }
283 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
284 }
285
286 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
287 // FP_ROUND's are always exact here.
288 if (ValueVT.bitsLT(Val.getValueType())) {
289
290 SDValue NoChange =
292
293 if (DAG.getMachineFunction().getFunction().getAttributes().hasFnAttr(
294 llvm::Attribute::StrictFP)) {
295 return DAG.getNode(ISD::STRICT_FP_ROUND, DL,
296 DAG.getVTList(ValueVT, MVT::Other), InChain, Val,
297 NoChange);
298 }
299
300 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, NoChange);
301 }
302
303 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
304 }
305
306 // Handle MMX to a narrower integer type by bitcasting MMX to integer and
307 // then truncating.
308 if (PartEVT == MVT::x86mmx && ValueVT.isInteger() &&
309 ValueVT.bitsLT(PartEVT)) {
310 Val = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Val);
311 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
312 }
313
314 report_fatal_error("Unknown mismatch in getCopyFromParts!");
315}
316
318 const Twine &ErrMsg) {
320 if (!I)
321 return Ctx.emitError(ErrMsg);
322
323 if (const CallInst *CI = dyn_cast<CallInst>(I))
324 if (CI->isInlineAsm()) {
325 return Ctx.diagnose(DiagnosticInfoInlineAsm(
326 *CI, ErrMsg + ", possible invalid constraint for vector type"));
327 }
328
329 return Ctx.emitError(I, ErrMsg);
330}
331
332/// getCopyFromPartsVector - Create a value that contains the specified legal
333/// parts combined into the value they represent. If the parts combine to a
334/// type larger than ValueVT then AssertOp can be used to specify whether the
335/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
336/// ValueVT (ISD::AssertSext).
338 const SDValue *Parts, unsigned NumParts,
339 MVT PartVT, EVT ValueVT, const Value *V,
340 SDValue InChain,
341 std::optional<CallingConv::ID> CallConv) {
342 assert(ValueVT.isVector() && "Not a vector value");
343 assert(NumParts > 0 && "No parts to assemble!");
344 const bool IsABIRegCopy = CallConv.has_value();
345
346 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
347 SDValue Val = Parts[0];
348
349 // Handle a multi-element vector.
350 if (NumParts > 1) {
351 EVT IntermediateVT;
352 MVT RegisterVT;
353 unsigned NumIntermediates;
354 unsigned NumRegs;
355
356 if (IsABIRegCopy) {
358 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT,
359 NumIntermediates, RegisterVT);
360 } else {
361 NumRegs =
362 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
363 NumIntermediates, RegisterVT);
364 }
365
366 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
367 NumParts = NumRegs; // Silence a compiler warning.
368 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
369 assert(RegisterVT.getSizeInBits() ==
370 Parts[0].getSimpleValueType().getSizeInBits() &&
371 "Part type sizes don't match!");
372
373 // Assemble the parts into intermediate operands.
374 SmallVector<SDValue, 8> Ops(NumIntermediates);
375 if (NumIntermediates == NumParts) {
376 // If the register was not expanded, truncate or copy the value,
377 // as appropriate.
378 for (unsigned i = 0; i != NumParts; ++i)
379 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, PartVT, IntermediateVT,
380 V, InChain, CallConv);
381 } else if (NumParts > 0) {
382 // If the intermediate type was expanded, build the intermediate
383 // operands from the parts.
384 assert(NumParts % NumIntermediates == 0 &&
385 "Must expand into a divisible number of parts!");
386 unsigned Factor = NumParts / NumIntermediates;
387 for (unsigned i = 0; i != NumIntermediates; ++i)
388 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, PartVT,
389 IntermediateVT, V, InChain, CallConv);
390 }
391
392 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
393 // intermediate operands.
394 EVT BuiltVectorTy =
395 IntermediateVT.isVector()
397 *DAG.getContext(), IntermediateVT.getScalarType(),
398 IntermediateVT.getVectorElementCount() * NumParts)
400 IntermediateVT.getScalarType(),
401 NumIntermediates);
402 Val = DAG.getNode(IntermediateVT.isVector() ? ISD::CONCAT_VECTORS
404 DL, BuiltVectorTy, Ops);
405 }
406
407 // There is now one part, held in Val. Correct it to match ValueVT.
408 EVT PartEVT = Val.getValueType();
409
410 if (PartEVT == ValueVT)
411 return Val;
412
413 if (PartEVT.isVector()) {
414 // Vector/Vector bitcast.
415 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
416 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
417
418 // If the parts vector has more elements than the value vector, then we
419 // have a vector widening case (e.g. <2 x float> -> <4 x float>).
420 // Extract the elements we want.
421 if (PartEVT.getVectorElementCount() != ValueVT.getVectorElementCount()) {
424 (PartEVT.getVectorElementCount().isScalable() ==
425 ValueVT.getVectorElementCount().isScalable()) &&
426 "Cannot narrow, it would be a lossy transformation");
427 PartEVT =
429 ValueVT.getVectorElementCount());
430 Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, PartEVT, Val,
431 DAG.getVectorIdxConstant(0, DL));
432 if (PartEVT == ValueVT)
433 return Val;
434 if (PartEVT.isInteger() && ValueVT.isFloatingPoint())
435 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
436
437 // Vector/Vector bitcast (e.g. <2 x bfloat> -> <2 x half>).
438 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
439 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
440 }
441
442 // Promoted vector extract
443 return DAG.getAnyExtOrTrunc(Val, DL, ValueVT);
444 }
445
446 // Trivial bitcast if the types are the same size and the destination
447 // vector type is legal.
448 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
449 TLI.isTypeLegal(ValueVT))
450 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
451
452 if (ValueVT.getVectorNumElements() != 1) {
453 // Certain ABIs require that vectors are passed as integers. For vectors
454 // are the same size, this is an obvious bitcast.
455 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) {
456 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
457 } else if (ValueVT.bitsLT(PartEVT)) {
458 const uint64_t ValueSize = ValueVT.getFixedSizeInBits();
459 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
460 // Drop the extra bits.
461 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
462 return DAG.getBitcast(ValueVT, Val);
463 }
464
466 *DAG.getContext(), V, "non-trivial scalar-to-vector conversion");
467 return DAG.getUNDEF(ValueVT);
468 }
469
470 // Handle cases such as i8 -> <1 x i1>
471 EVT ValueSVT = ValueVT.getVectorElementType();
472 if (ValueVT.getVectorNumElements() == 1 && ValueSVT != PartEVT) {
473 unsigned ValueSize = ValueSVT.getSizeInBits();
474 if (ValueSize == PartEVT.getSizeInBits()) {
475 Val = DAG.getNode(ISD::BITCAST, DL, ValueSVT, Val);
476 } else if (ValueSVT.isFloatingPoint() && PartEVT.isInteger()) {
477 // It's possible a scalar floating point type gets softened to integer and
478 // then promoted to a larger integer. If PartEVT is the larger integer
479 // we need to truncate it and then bitcast to the FP type.
480 assert(ValueSVT.bitsLT(PartEVT) && "Unexpected types");
481 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
482 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
483 Val = DAG.getBitcast(ValueSVT, Val);
484 } else {
485 Val = ValueVT.isFloatingPoint()
486 ? DAG.getFPExtendOrRound(Val, DL, ValueSVT)
487 : DAG.getAnyExtOrTrunc(Val, DL, ValueSVT);
488 }
489 }
490
491 return DAG.getBuildVector(ValueVT, DL, Val);
492}
493
494static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl,
495 SDValue Val, SDValue *Parts, unsigned NumParts,
496 MVT PartVT, const Value *V,
497 std::optional<CallingConv::ID> CallConv);
498
499/// getCopyToParts - Create a series of nodes that contain the specified value
500/// split into legal parts. If the parts contain more bits than Val, then, for
501/// integers, ExtendKind can be used to specify how to generate the extra bits.
502static void
504 unsigned NumParts, MVT PartVT, const Value *V,
505 std::optional<CallingConv::ID> CallConv = std::nullopt,
506 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
507 // Let the target split the parts if it wants to
508 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
509 if (TLI.splitValueIntoRegisterParts(DAG, DL, Val, Parts, NumParts, PartVT,
510 CallConv))
511 return;
512 EVT ValueVT = Val.getValueType();
513
514 // Handle the vector case separately.
515 if (ValueVT.isVector())
516 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V,
517 CallConv);
518
519 unsigned OrigNumParts = NumParts;
521 "Copying to an illegal type!");
522
523 if (NumParts == 0)
524 return;
525
526 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
527 EVT PartEVT = PartVT;
528 if (PartEVT == ValueVT) {
529 assert(NumParts == 1 && "No-op copy with multiple parts!");
530 Parts[0] = Val;
531 return;
532 }
533
534 unsigned PartBits = PartVT.getSizeInBits();
535 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
536 // If the parts cover more bits than the value has, promote the value.
537 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
538 assert(NumParts == 1 && "Do not know what to promote to!");
539 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
540 } else {
541 if (ValueVT.isFloatingPoint()) {
542 // FP values need to be bitcast, then extended if they are being put
543 // into a larger container.
544 ValueVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
545 Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
546 }
547 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
548 ValueVT.isInteger() &&
549 "Unknown mismatch!");
550 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
551 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
552 if (PartVT == MVT::x86mmx)
553 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
554 }
555 } else if (PartBits == ValueVT.getSizeInBits()) {
556 // Different types of the same size.
557 assert(NumParts == 1 && PartEVT != ValueVT);
558 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
559 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
560 // If the parts cover less bits than value has, truncate the value.
561 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
562 ValueVT.isInteger() &&
563 "Unknown mismatch!");
564 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
565 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
566 if (PartVT == MVT::x86mmx)
567 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
568 }
569
570 // The value may have changed - recompute ValueVT.
571 ValueVT = Val.getValueType();
572 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
573 "Failed to tile the value with PartVT!");
574
575 if (NumParts == 1) {
576 if (PartEVT != ValueVT) {
578 "scalar-to-vector conversion failed");
579 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
580 }
581
582 Parts[0] = Val;
583 return;
584 }
585
586 // Expand the value into multiple parts.
587 if (NumParts & (NumParts - 1)) {
588 // The number of parts is not a power of 2. Split off and copy the tail.
589 assert(PartVT.isInteger() && ValueVT.isInteger() &&
590 "Do not know what to expand to!");
591 unsigned RoundParts = llvm::bit_floor(NumParts);
592 unsigned RoundBits = RoundParts * PartBits;
593 unsigned OddParts = NumParts - RoundParts;
594 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
595 DAG.getShiftAmountConstant(RoundBits, ValueVT, DL));
596
597 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V,
598 CallConv);
599
600 if (DAG.getDataLayout().isBigEndian())
601 // The odd parts were reversed by getCopyToParts - unreverse them.
602 std::reverse(Parts + RoundParts, Parts + NumParts);
603
604 NumParts = RoundParts;
605 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
606 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
607 }
608
609 // The number of parts is a power of 2. Repeatedly bisect the value using
610 // EXTRACT_ELEMENT.
611 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
613 ValueVT.getSizeInBits()),
614 Val);
615
616 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
617 for (unsigned i = 0; i < NumParts; i += StepSize) {
618 unsigned ThisBits = StepSize * PartBits / 2;
619 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
620 SDValue &Part0 = Parts[i];
621 SDValue &Part1 = Parts[i+StepSize/2];
622
623 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
624 ThisVT, Part0, DAG.getIntPtrConstant(1, DL));
625 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
626 ThisVT, Part0, DAG.getIntPtrConstant(0, DL));
627
628 if (ThisBits == PartBits && ThisVT != PartVT) {
629 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
630 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
631 }
632 }
633 }
634
635 if (DAG.getDataLayout().isBigEndian())
636 std::reverse(Parts, Parts + OrigNumParts);
637}
638
640 const SDLoc &DL, EVT PartVT) {
641 if (!PartVT.isVector())
642 return SDValue();
643
644 EVT ValueVT = Val.getValueType();
645 EVT PartEVT = PartVT.getVectorElementType();
646 EVT ValueEVT = ValueVT.getVectorElementType();
647 ElementCount PartNumElts = PartVT.getVectorElementCount();
648 ElementCount ValueNumElts = ValueVT.getVectorElementCount();
649
650 // We only support widening vectors with equivalent element types and
651 // fixed/scalable properties. If a target needs to widen a fixed-length type
652 // to a scalable one, it should be possible to use INSERT_SUBVECTOR below.
653 if (ElementCount::isKnownLE(PartNumElts, ValueNumElts) ||
654 PartNumElts.isScalable() != ValueNumElts.isScalable())
655 return SDValue();
656
657 // Have a try for bf16 because some targets share its ABI with fp16.
658 if (ValueEVT == MVT::bf16 && PartEVT == MVT::f16) {
660 "Cannot widen to illegal type");
661 Val = DAG.getNode(ISD::BITCAST, DL,
662 ValueVT.changeVectorElementType(MVT::f16), Val);
663 } else if (PartEVT != ValueEVT) {
664 return SDValue();
665 }
666
667 // Widening a scalable vector to another scalable vector is done by inserting
668 // the vector into a larger undef one.
669 if (PartNumElts.isScalable())
670 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT),
671 Val, DAG.getVectorIdxConstant(0, DL));
672
673 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
674 // undef elements.
676 DAG.ExtractVectorElements(Val, Ops);
677 SDValue EltUndef = DAG.getUNDEF(PartEVT);
678 Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef);
679
680 // FIXME: Use CONCAT for 2x -> 4x.
681 return DAG.getBuildVector(PartVT, DL, Ops);
682}
683
684/// getCopyToPartsVector - Create a series of nodes that contain the specified
685/// value split into legal parts.
686static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL,
687 SDValue Val, SDValue *Parts, unsigned NumParts,
688 MVT PartVT, const Value *V,
689 std::optional<CallingConv::ID> CallConv) {
690 EVT ValueVT = Val.getValueType();
691 assert(ValueVT.isVector() && "Not a vector");
692 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
693 const bool IsABIRegCopy = CallConv.has_value();
694
695 if (NumParts == 1) {
696 EVT PartEVT = PartVT;
697 if (PartEVT == ValueVT) {
698 // Nothing to do.
699 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
700 // Bitconvert vector->vector case.
701 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
702 } else if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, PartVT)) {
703 Val = Widened;
704 } else if (PartVT.isVector() &&
706 ValueVT.getVectorElementType()) &&
707 PartEVT.getVectorElementCount() ==
708 ValueVT.getVectorElementCount()) {
709
710 // Promoted vector extract
711 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
712 } else if (PartEVT.isVector() &&
713 PartEVT.getVectorElementType() !=
714 ValueVT.getVectorElementType() &&
715 TLI.getTypeAction(*DAG.getContext(), ValueVT) ==
717 // Combination of widening and promotion.
718 EVT WidenVT =
720 PartVT.getVectorElementCount());
721 SDValue Widened = widenVectorToPartType(DAG, Val, DL, WidenVT);
722 Val = DAG.getAnyExtOrTrunc(Widened, DL, PartVT);
723 } else {
724 // Don't extract an integer from a float vector. This can happen if the
725 // FP type gets softened to integer and then promoted. The promotion
726 // prevents it from being picked up by the earlier bitcast case.
727 if (ValueVT.getVectorElementCount().isScalar() &&
728 (!ValueVT.isFloatingPoint() || !PartVT.isInteger())) {
729 // If we reach this condition and PartVT is FP, this means that
730 // ValueVT is also FP and both have a different size, otherwise we
731 // would have bitcasted them. Producing an EXTRACT_VECTOR_ELT here
732 // would be invalid since that would mean the smaller FP type has to
733 // be extended to the larger one.
734 if (PartVT.isFloatingPoint()) {
735 Val = DAG.getBitcast(ValueVT.getScalarType(), Val);
736 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
737 } else
738 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val,
739 DAG.getVectorIdxConstant(0, DL));
740 } else {
741 uint64_t ValueSize = ValueVT.getFixedSizeInBits();
742 assert(PartVT.getFixedSizeInBits() > ValueSize &&
743 "lossy conversion of vector to scalar type");
744 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
745 Val = DAG.getBitcast(IntermediateType, Val);
746 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
747 }
748 }
749
750 assert(Val.getValueType() == PartVT && "Unexpected vector part value type");
751 Parts[0] = Val;
752 return;
753 }
754
755 // Handle a multi-element vector.
756 EVT IntermediateVT;
757 MVT RegisterVT;
758 unsigned NumIntermediates;
759 unsigned NumRegs;
760 if (IsABIRegCopy) {
762 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT, NumIntermediates,
763 RegisterVT);
764 } else {
765 NumRegs =
766 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
767 NumIntermediates, RegisterVT);
768 }
769
770 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
771 NumParts = NumRegs; // Silence a compiler warning.
772 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
773
774 assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() &&
775 "Mixing scalable and fixed vectors when copying in parts");
776
777 std::optional<ElementCount> DestEltCnt;
778
779 if (IntermediateVT.isVector())
780 DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates;
781 else
782 DestEltCnt = ElementCount::getFixed(NumIntermediates);
783
784 EVT BuiltVectorTy = EVT::getVectorVT(
785 *DAG.getContext(), IntermediateVT.getScalarType(), *DestEltCnt);
786
787 if (ValueVT == BuiltVectorTy) {
788 // Nothing to do.
789 } else if (ValueVT.getSizeInBits() == BuiltVectorTy.getSizeInBits()) {
790 // Bitconvert vector->vector case.
791 Val = DAG.getNode(ISD::BITCAST, DL, BuiltVectorTy, Val);
792 } else {
793 if (BuiltVectorTy.getVectorElementType().bitsGT(
794 ValueVT.getVectorElementType())) {
795 // Integer promotion.
796 ValueVT = EVT::getVectorVT(*DAG.getContext(),
797 BuiltVectorTy.getVectorElementType(),
798 ValueVT.getVectorElementCount());
799 Val = DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
800 }
801
802 if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy)) {
803 Val = Widened;
804 }
805 }
806
807 assert(Val.getValueType() == BuiltVectorTy && "Unexpected vector value type");
808
809 // Split the vector into intermediate operands.
810 SmallVector<SDValue, 8> Ops(NumIntermediates);
811 for (unsigned i = 0; i != NumIntermediates; ++i) {
812 if (IntermediateVT.isVector()) {
813 // This does something sensible for scalable vectors - see the
814 // definition of EXTRACT_SUBVECTOR for further details.
815 unsigned IntermediateNumElts = IntermediateVT.getVectorMinNumElements();
816 Ops[i] =
817 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val,
818 DAG.getVectorIdxConstant(i * IntermediateNumElts, DL));
819 } else {
820 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val,
821 DAG.getVectorIdxConstant(i, DL));
822 }
823 }
824
825 // Split the intermediate operands into legal parts.
826 if (NumParts == NumIntermediates) {
827 // If the register was not expanded, promote or copy the value,
828 // as appropriate.
829 for (unsigned i = 0; i != NumParts; ++i)
830 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V, CallConv);
831 } else if (NumParts > 0) {
832 // If the intermediate type was expanded, split each the value into
833 // legal parts.
834 assert(NumIntermediates != 0 && "division by zero");
835 assert(NumParts % NumIntermediates == 0 &&
836 "Must expand into a divisible number of parts!");
837 unsigned Factor = NumParts / NumIntermediates;
838 for (unsigned i = 0; i != NumIntermediates; ++i)
839 getCopyToParts(DAG, DL, Ops[i], &Parts[i * Factor], Factor, PartVT, V,
840 CallConv);
841 }
842}
843
844static void failForInvalidBundles(const CallBase &I, StringRef Name,
845 ArrayRef<uint32_t> AllowedBundles) {
846 if (I.hasOperandBundlesOtherThan(AllowedBundles)) {
847 ListSeparator LS;
848 std::string Error;
850 for (unsigned i = 0, e = I.getNumOperandBundles(); i != e; ++i) {
851 OperandBundleUse U = I.getOperandBundleAt(i);
852 if (!is_contained(AllowedBundles, U.getTagID()))
853 OS << LS << U.getTagName();
854 }
856 Twine("cannot lower ", Name)
857 .concat(Twine(" with arbitrary operand bundles: ", Error)));
858 }
859}
860
862 EVT valuevt, std::optional<CallingConv::ID> CC)
863 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs),
864 RegCount(1, regs.size()), CallConv(CC) {}
865
867 const DataLayout &DL, Register Reg, Type *Ty,
868 std::optional<CallingConv::ID> CC) {
869 ComputeValueVTs(TLI, DL, Ty, ValueVTs);
870
871 CallConv = CC;
872
873 for (EVT ValueVT : ValueVTs) {
874 unsigned NumRegs =
876 ? TLI.getNumRegistersForCallingConv(Context, *CC, ValueVT)
877 : TLI.getNumRegisters(Context, ValueVT);
878 MVT RegisterVT =
880 ? TLI.getRegisterTypeForCallingConv(Context, *CC, ValueVT)
881 : TLI.getRegisterType(Context, ValueVT);
882 for (unsigned i = 0; i != NumRegs; ++i)
883 Regs.push_back(Reg + i);
884 RegVTs.push_back(RegisterVT);
885 RegCount.push_back(NumRegs);
886 Reg = Reg.id() + NumRegs;
887 }
888}
889
891 FunctionLoweringInfo &FuncInfo,
892 const SDLoc &dl, SDValue &Chain,
893 SDValue *Glue, const Value *V) const {
894 // A Value with type {} or [0 x %t] needs no registers.
895 if (ValueVTs.empty())
896 return SDValue();
897
898 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
899
900 // Assemble the legal parts into the final values.
901 SmallVector<SDValue, 4> Values(ValueVTs.size());
903 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
904 // Copy the legal parts from the registers.
905 EVT ValueVT = ValueVTs[Value];
906 unsigned NumRegs = RegCount[Value];
907 MVT RegisterVT = isABIMangled()
909 *DAG.getContext(), *CallConv, RegVTs[Value])
910 : RegVTs[Value];
911
912 Parts.resize(NumRegs);
913 for (unsigned i = 0; i != NumRegs; ++i) {
914 SDValue P;
915 if (!Glue) {
916 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
917 } else {
918 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Glue);
919 *Glue = P.getValue(2);
920 }
921
922 Chain = P.getValue(1);
923 Parts[i] = P;
924
925 // If the source register was virtual and if we know something about it,
926 // add an assert node.
927 if (!Regs[Part + i].isVirtual() || !RegisterVT.isInteger())
928 continue;
929
931 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
932 if (!LOI)
933 continue;
934
935 unsigned RegSize = RegisterVT.getScalarSizeInBits();
936 unsigned NumSignBits = LOI->NumSignBits;
937 unsigned NumZeroBits = LOI->Known.countMinLeadingZeros();
938
939 if (NumZeroBits == RegSize) {
940 // The current value is a zero.
941 // Explicitly express that as it would be easier for
942 // optimizations to kick in.
943 Parts[i] = DAG.getConstant(0, dl, RegisterVT);
944 continue;
945 }
946
947 // FIXME: We capture more information than the dag can represent. For
948 // now, just use the tightest assertzext/assertsext possible.
949 bool isSExt;
950 EVT FromVT(MVT::Other);
951 if (NumZeroBits) {
952 FromVT = EVT::getIntegerVT(*DAG.getContext(), RegSize - NumZeroBits);
953 isSExt = false;
954 } else if (NumSignBits > 1) {
955 FromVT =
956 EVT::getIntegerVT(*DAG.getContext(), RegSize - NumSignBits + 1);
957 isSExt = true;
958 } else {
959 continue;
960 }
961 // Add an assertion node.
962 assert(FromVT != MVT::Other);
963 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
964 RegisterVT, P, DAG.getValueType(FromVT));
965 }
966
967 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), NumRegs,
968 RegisterVT, ValueVT, V, Chain, CallConv);
969 Part += NumRegs;
970 Parts.clear();
971 }
972
973 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values);
974}
975
977 const SDLoc &dl, SDValue &Chain, SDValue *Glue,
978 const Value *V,
979 ISD::NodeType PreferredExtendType) const {
980 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
981 ISD::NodeType ExtendKind = PreferredExtendType;
982
983 // Get the list of the values's legal parts.
984 unsigned NumRegs = Regs.size();
985 SmallVector<SDValue, 8> Parts(NumRegs);
986 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
987 unsigned NumParts = RegCount[Value];
988
989 MVT RegisterVT = isABIMangled()
991 *DAG.getContext(), *CallConv, RegVTs[Value])
992 : RegVTs[Value];
993
994 if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT))
995 ExtendKind = ISD::ZERO_EXTEND;
996
997 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
998 NumParts, RegisterVT, V, CallConv, ExtendKind);
999 Part += NumParts;
1000 }
1001
1002 // Copy the parts into the registers.
1003 SmallVector<SDValue, 8> Chains(NumRegs);
1004 for (unsigned i = 0; i != NumRegs; ++i) {
1005 SDValue Part;
1006 if (!Glue) {
1007 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
1008 } else {
1009 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue);
1010 *Glue = Part.getValue(1);
1011 }
1012
1013 Chains[i] = Part.getValue(0);
1014 }
1015
1016 if (NumRegs == 1 || Glue)
1017 // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is
1018 // flagged to it. That is the CopyToReg nodes and the user are considered
1019 // a single scheduling unit. If we create a TokenFactor and return it as
1020 // chain, then the TokenFactor is both a predecessor (operand) of the
1021 // user as well as a successor (the TF operands are flagged to the user).
1022 // c1, f1 = CopyToReg
1023 // c2, f2 = CopyToReg
1024 // c3 = TokenFactor c1, c2
1025 // ...
1026 // = op c3, ..., f2
1027 Chain = Chains[NumRegs-1];
1028 else
1029 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1030}
1031
1033 unsigned MatchingIdx, const SDLoc &dl,
1034 SelectionDAG &DAG,
1035 std::vector<SDValue> &Ops) const {
1036 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1037
1038 InlineAsm::Flag Flag(Code, Regs.size());
1039 if (HasMatching)
1040 Flag.setMatchingOp(MatchingIdx);
1041 else if (!Regs.empty() && Regs.front().isVirtual()) {
1042 // Put the register class of the virtual registers in the flag word. That
1043 // way, later passes can recompute register class constraints for inline
1044 // assembly as well as normal instructions.
1045 // Don't do this for tied operands that can use the regclass information
1046 // from the def.
1048 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
1049 Flag.setRegClass(RC->getID());
1050 }
1051
1052 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
1053 Ops.push_back(Res);
1054
1055 if (Code == InlineAsm::Kind::Clobber) {
1056 // Clobbers should always have a 1:1 mapping with registers, and may
1057 // reference registers that have illegal (e.g. vector) types. Hence, we
1058 // shouldn't try to apply any sort of splitting logic to them.
1059 assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
1060 "No 1:1 mapping from clobbers to regs?");
1062 (void)SP;
1063 for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
1064 Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
1065 assert(
1066 (Regs[I] != SP ||
1068 "If we clobbered the stack pointer, MFI should know about it.");
1069 }
1070 return;
1071 }
1072
1073 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1074 MVT RegisterVT = RegVTs[Value];
1075 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1076 RegisterVT);
1077 for (unsigned i = 0; i != NumRegs; ++i) {
1078 assert(Reg < Regs.size() && "Mismatch in # registers expected");
1079 Register TheReg = Regs[Reg++];
1080 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1081 }
1082 }
1083}
1084
1088 unsigned I = 0;
1089 for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1090 unsigned RegCount = std::get<0>(CountAndVT);
1091 MVT RegisterVT = std::get<1>(CountAndVT);
1092 TypeSize RegisterSize = RegisterVT.getSizeInBits();
1093 for (unsigned E = I + RegCount; I != E; ++I)
1094 OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1095 }
1096 return OutVec;
1097}
1098
1100 AssumptionCache *ac,
1101 const TargetLibraryInfo *li) {
1102 BatchAA = aa;
1103 AC = ac;
1104 GFI = gfi;
1105 LibInfo = li;
1106 Context = DAG.getContext();
1107 LPadToCallSiteMap.clear();
1108 SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1109 AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
1110 *DAG.getMachineFunction().getFunction().getParent());
1111}
1112
1114 NodeMap.clear();
1115 UnusedArgNodeMap.clear();
1116 PendingLoads.clear();
1117 PendingExports.clear();
1118 PendingConstrainedFP.clear();
1119 PendingConstrainedFPStrict.clear();
1120 CurInst = nullptr;
1121 HasTailCall = false;
1122 SDNodeOrder = LowestSDNodeOrder;
1123 StatepointLowering.clear();
1124}
1125
1127 DanglingDebugInfoMap.clear();
1128}
1129
1130// Update DAG root to include dependencies on Pending chains.
1131SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1132 SDValue Root = DAG.getRoot();
1133
1134 if (Pending.empty())
1135 return Root;
1136
1137 // Add current root to PendingChains, unless we already indirectly
1138 // depend on it.
1139 if (Root.getOpcode() != ISD::EntryToken) {
1140 unsigned i = 0, e = Pending.size();
1141 for (; i != e; ++i) {
1142 assert(Pending[i].getNode()->getNumOperands() > 1);
1143 if (Pending[i].getNode()->getOperand(0) == Root)
1144 break; // Don't add the root if we already indirectly depend on it.
1145 }
1146
1147 if (i == e)
1148 Pending.push_back(Root);
1149 }
1150
1151 if (Pending.size() == 1)
1152 Root = Pending[0];
1153 else
1154 Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1155
1156 DAG.setRoot(Root);
1157 Pending.clear();
1158 return Root;
1159}
1160
1164
1166 // If the new exception behavior differs from that of the pending
1167 // ones, chain up them and update the root.
1168 switch (EB) {
1171 // Floating-point exceptions produced by such operations are not intended
1172 // to be observed, so the sequence of these operations does not need to be
1173 // preserved.
1174 //
1175 // They however must not be mixed with the instructions that have strict
1176 // exception behavior. Placing an operation with 'ebIgnore' behavior between
1177 // 'ebStrict' operations could distort the observed exception behavior.
1178 if (!PendingConstrainedFPStrict.empty()) {
1179 assert(PendingConstrainedFP.empty());
1180 updateRoot(PendingConstrainedFPStrict);
1181 }
1182 break;
1184 // Floating-point exception produced by these operations may be observed, so
1185 // they must be correctly chained. If trapping on FP exceptions is
1186 // disabled, the exceptions can be observed only by functions that read
1187 // exception flags, like 'llvm.get_fpenv' or 'fetestexcept'. It means that
1188 // the order of operations is not significant between barriers.
1189 //
1190 // If trapping is enabled, each operation becomes an implicit observation
1191 // point, so the operations must be sequenced according their original
1192 // source order.
1193 if (!PendingConstrainedFP.empty()) {
1194 assert(PendingConstrainedFPStrict.empty());
1195 updateRoot(PendingConstrainedFP);
1196 }
1197 // TODO: Add support for trapping-enabled scenarios.
1198 }
1199 return DAG.getRoot();
1200}
1201
1203 // Chain up all pending constrained intrinsics together with all
1204 // pending loads, by simply appending them to PendingLoads and
1205 // then calling getMemoryRoot().
1206 PendingLoads.reserve(PendingLoads.size() +
1207 PendingConstrainedFP.size() +
1208 PendingConstrainedFPStrict.size());
1209 PendingLoads.append(PendingConstrainedFP.begin(),
1210 PendingConstrainedFP.end());
1211 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1212 PendingConstrainedFPStrict.end());
1213 PendingConstrainedFP.clear();
1214 PendingConstrainedFPStrict.clear();
1215 return getMemoryRoot();
1216}
1217
1219 // We need to emit pending fpexcept.strict constrained intrinsics,
1220 // so append them to the PendingExports list.
1221 PendingExports.append(PendingConstrainedFPStrict.begin(),
1222 PendingConstrainedFPStrict.end());
1223 PendingConstrainedFPStrict.clear();
1224 return updateRoot(PendingExports);
1225}
1226
1228 DILocalVariable *Variable,
1230 DebugLoc DL) {
1231 assert(Variable && "Missing variable");
1232
1233 // Check if address has undef value.
1234 if (!Address || isa<UndefValue>(Address) ||
1235 (Address->use_empty() && !isa<Argument>(Address))) {
1236 LLVM_DEBUG(
1237 dbgs()
1238 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1239 return;
1240 }
1241
1242 bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1243
1244 SDValue &N = NodeMap[Address];
1245 if (!N.getNode() && isa<Argument>(Address))
1246 // Check unused arguments map.
1247 N = UnusedArgNodeMap[Address];
1248 SDDbgValue *SDV;
1249 if (N.getNode()) {
1250 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1251 Address = BCI->getOperand(0);
1252 // Parameters are handled specially.
1253 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1254 if (IsParameter && FINode) {
1255 // Byval parameter. We have a frame index at this point.
1256 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1257 /*IsIndirect*/ true, DL, SDNodeOrder);
1258 } else if (isa<Argument>(Address)) {
1259 // Address is an argument, so try to emit its dbg value using
1260 // virtual register info from the FuncInfo.ValueMap.
1261 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1262 FuncArgumentDbgValueKind::Declare, N);
1263 return;
1264 } else {
1265 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1266 true, DL, SDNodeOrder);
1267 }
1268 DAG.AddDbgValue(SDV, IsParameter);
1269 } else {
1270 // If Address is an argument then try to emit its dbg value using
1271 // virtual register info from the FuncInfo.ValueMap.
1272 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1273 FuncArgumentDbgValueKind::Declare, N)) {
1274 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1275 << " (could not emit func-arg dbg_value)\n");
1276 }
1277 }
1278}
1279
1281 // Add SDDbgValue nodes for any var locs here. Do so before updating
1282 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1283 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1284 // Add SDDbgValue nodes for any var locs here. Do so before updating
1285 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1286 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1287 It != End; ++It) {
1288 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1289 dropDanglingDebugInfo(Var, It->Expr);
1290 if (It->Values.isKillLocation(It->Expr)) {
1291 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1292 continue;
1293 }
1294 SmallVector<Value *> Values(It->Values.location_ops());
1295 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1296 It->Values.hasArgList())) {
1297 SmallVector<Value *, 4> Vals(It->Values.location_ops());
1299 FnVarLocs->getDILocalVariable(It->VariableID),
1300 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1301 }
1302 }
1303 }
1304
1305 // We must skip DbgVariableRecords if they've already been processed above as
1306 // we have just emitted the debug values resulting from assignment tracking
1307 // analysis, making any existing DbgVariableRecords redundant (and probably
1308 // less correct). We still need to process DbgLabelRecords. This does sink
1309 // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1310 // be important as it does so deterministcally and ordering between
1311 // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1312 // printing).
1313 bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1314 // Is there is any debug-info attached to this instruction, in the form of
1315 // DbgRecord non-instruction debug-info records.
1316 for (DbgRecord &DR : I.getDbgRecordRange()) {
1317 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1318 assert(DLR->getLabel() && "Missing label");
1319 SDDbgLabel *SDV =
1320 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1321 DAG.AddDbgLabel(SDV);
1322 continue;
1323 }
1324
1325 if (SkipDbgVariableRecords)
1326 continue;
1328 DILocalVariable *Variable = DVR.getVariable();
1331
1333 if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1334 continue;
1335 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1336 << "\n");
1338 DVR.getDebugLoc());
1339 continue;
1340 }
1341
1342 // A DbgVariableRecord with no locations is a kill location.
1344 if (Values.empty()) {
1346 SDNodeOrder);
1347 continue;
1348 }
1349
1350 // A DbgVariableRecord with an undef or absent location is also a kill
1351 // location.
1352 if (llvm::any_of(Values,
1353 [](Value *V) { return !V || isa<UndefValue>(V); })) {
1355 SDNodeOrder);
1356 continue;
1357 }
1358
1359 bool IsVariadic = DVR.hasArgList();
1360 if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1361 SDNodeOrder, IsVariadic)) {
1362 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1363 DVR.getDebugLoc(), SDNodeOrder);
1364 }
1365 }
1366}
1367
1369 visitDbgInfo(I);
1370
1371 // Set up outgoing PHI node register values before emitting the terminator.
1372 if (I.isTerminator()) {
1373 HandlePHINodesInSuccessorBlocks(I.getParent());
1374 }
1375
1376 ++SDNodeOrder;
1377 CurInst = &I;
1378
1379 // Set inserted listener only if required.
1380 bool NodeInserted = false;
1381 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1382 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1383 MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1384 if (PCSectionsMD || MMRA) {
1385 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1386 DAG, [&](SDNode *) { NodeInserted = true; });
1387 }
1388
1389 visit(I.getOpcode(), I);
1390
1391 if (!I.isTerminator() && !HasTailCall &&
1392 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1394
1395 // Handle metadata.
1396 if (PCSectionsMD || MMRA) {
1397 auto It = NodeMap.find(&I);
1398 if (It != NodeMap.end()) {
1399 if (PCSectionsMD)
1400 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1401 if (MMRA)
1402 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1403 } else if (NodeInserted) {
1404 // This should not happen; if it does, don't let it go unnoticed so we can
1405 // fix it. Relevant visit*() function is probably missing a setValue().
1406 errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1407 << I.getModule()->getName() << "]\n";
1408 LLVM_DEBUG(I.dump());
1409 assert(false);
1410 }
1411 }
1412
1413 CurInst = nullptr;
1414}
1415
1416void SelectionDAGBuilder::visitPHI(const PHINode &) {
1417 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1418}
1419
1420void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1421 // Note: this doesn't use InstVisitor, because it has to work with
1422 // ConstantExpr's in addition to instructions.
1423 switch (Opcode) {
1424 default: llvm_unreachable("Unknown instruction type encountered!");
1425 // Build the switch statement using the Instruction.def file.
1426#define HANDLE_INST(NUM, OPCODE, CLASS) \
1427 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1428#include "llvm/IR/Instruction.def"
1429 }
1430}
1431
1433 DILocalVariable *Variable,
1434 DebugLoc DL, unsigned Order,
1437 // For variadic dbg_values we will now insert poison.
1438 // FIXME: We can potentially recover these!
1440 for (const Value *V : Values) {
1441 auto *Poison = PoisonValue::get(V->getType());
1443 }
1444 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1445 /*IsIndirect=*/false, DL, Order,
1446 /*IsVariadic=*/true);
1447 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1448 return true;
1449}
1450
1452 DILocalVariable *Var,
1453 DIExpression *Expr,
1454 bool IsVariadic, DebugLoc DL,
1455 unsigned Order) {
1456 if (IsVariadic) {
1457 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1458 return;
1459 }
1460 // TODO: Dangling debug info will eventually either be resolved or produce
1461 // a poison DBG_VALUE. However in the resolution case, a gap may appear
1462 // between the original dbg.value location and its resolved DBG_VALUE,
1463 // which we should ideally fill with an extra poison DBG_VALUE.
1464 assert(Values.size() == 1);
1465 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1466}
1467
1469 const DIExpression *Expr) {
1470 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1471 DIVariable *DanglingVariable = DDI.getVariable();
1472 DIExpression *DanglingExpr = DDI.getExpression();
1473 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1474 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1475 << printDDI(nullptr, DDI) << "\n");
1476 return true;
1477 }
1478 return false;
1479 };
1480
1481 for (auto &DDIMI : DanglingDebugInfoMap) {
1482 DanglingDebugInfoVector &DDIV = DDIMI.second;
1483
1484 // If debug info is to be dropped, run it through final checks to see
1485 // whether it can be salvaged.
1486 for (auto &DDI : DDIV)
1487 if (isMatchingDbgValue(DDI))
1488 salvageUnresolvedDbgValue(DDIMI.first, DDI);
1489
1490 erase_if(DDIV, isMatchingDbgValue);
1491 }
1492}
1493
1494// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1495// generate the debug data structures now that we've seen its definition.
1497 SDValue Val) {
1498 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1499 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1500 return;
1501
1502 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1503 for (auto &DDI : DDIV) {
1504 DebugLoc DL = DDI.getDebugLoc();
1505 unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1506 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1507 DILocalVariable *Variable = DDI.getVariable();
1508 DIExpression *Expr = DDI.getExpression();
1509 assert(Variable->isValidLocationForIntrinsic(DL) &&
1510 "Expected inlined-at fields to agree");
1511 SDDbgValue *SDV;
1512 if (Val.getNode()) {
1513 // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1514 // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1515 // we couldn't resolve it directly when examining the DbgValue intrinsic
1516 // in the first place we should not be more successful here). Unless we
1517 // have some test case that prove this to be correct we should avoid
1518 // calling EmitFuncArgumentDbgValue here.
1519 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1520 FuncArgumentDbgValueKind::Value, Val)) {
1521 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1522 << printDDI(V, DDI) << "\n");
1523 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump());
1524 // Increase the SDNodeOrder for the DbgValue here to make sure it is
1525 // inserted after the definition of Val when emitting the instructions
1526 // after ISel. An alternative could be to teach
1527 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1528 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1529 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1530 << ValSDNodeOrder << "\n");
1531 SDV = getDbgValue(Val, Variable, Expr, DL,
1532 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1533 DAG.AddDbgValue(SDV, false);
1534 } else
1535 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1536 << printDDI(V, DDI)
1537 << " in EmitFuncArgumentDbgValue\n");
1538 } else {
1539 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1540 << "\n");
1541 auto Poison = PoisonValue::get(V->getType());
1542 auto SDV =
1543 DAG.getConstantDbgValue(Variable, Expr, Poison, DL, DbgSDNodeOrder);
1544 DAG.AddDbgValue(SDV, false);
1545 }
1546 }
1547 DDIV.clear();
1548}
1549
1551 DanglingDebugInfo &DDI) {
1552 // TODO: For the variadic implementation, instead of only checking the fail
1553 // state of `handleDebugValue`, we need know specifically which values were
1554 // invalid, so that we attempt to salvage only those values when processing
1555 // a DIArgList.
1556 const Value *OrigV = V;
1557 DILocalVariable *Var = DDI.getVariable();
1558 DIExpression *Expr = DDI.getExpression();
1559 DebugLoc DL = DDI.getDebugLoc();
1560 unsigned SDOrder = DDI.getSDNodeOrder();
1561
1562 // Currently we consider only dbg.value intrinsics -- we tell the salvager
1563 // that DW_OP_stack_value is desired.
1564 bool StackValue = true;
1565
1566 // Can this Value can be encoded without any further work?
1567 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1568 return;
1569
1570 // Attempt to salvage back through as many instructions as possible. Bail if
1571 // a non-instruction is seen, such as a constant expression or global
1572 // variable. FIXME: Further work could recover those too.
1573 while (isa<Instruction>(V)) {
1574 const Instruction &VAsInst = *cast<const Instruction>(V);
1575 // Temporary "0", awaiting real implementation.
1577 SmallVector<Value *, 4> AdditionalValues;
1578 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1579 Expr->getNumLocationOperands(), Ops,
1580 AdditionalValues);
1581 // If we cannot salvage any further, and haven't yet found a suitable debug
1582 // expression, bail out.
1583 if (!V)
1584 break;
1585
1586 // TODO: If AdditionalValues isn't empty, then the salvage can only be
1587 // represented with a DBG_VALUE_LIST, so we give up. When we have support
1588 // here for variadic dbg_values, remove that condition.
1589 if (!AdditionalValues.empty())
1590 break;
1591
1592 // New value and expr now represent this debuginfo.
1593 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1594
1595 // Some kind of simplification occurred: check whether the operand of the
1596 // salvaged debug expression can be encoded in this DAG.
1597 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1598 LLVM_DEBUG(
1599 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n"
1600 << *OrigV << "\nBy stripping back to:\n " << *V << "\n");
1601 return;
1602 }
1603 }
1604
1605 // This was the final opportunity to salvage this debug information, and it
1606 // couldn't be done. Place a poison DBG_VALUE at this location to terminate
1607 // any earlier variable location.
1608 assert(OrigV && "V shouldn't be null");
1609 auto *Poison = PoisonValue::get(OrigV->getType());
1610 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Poison, DL, SDNodeOrder);
1611 DAG.AddDbgValue(SDV, false);
1612 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n "
1613 << printDDI(OrigV, DDI) << "\n");
1614}
1615
1617 DIExpression *Expr,
1618 DebugLoc DbgLoc,
1619 unsigned Order) {
1623 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1624 /*IsVariadic*/ false);
1625}
1626
1628 DILocalVariable *Var,
1629 DIExpression *Expr, DebugLoc DbgLoc,
1630 unsigned Order, bool IsVariadic) {
1631 if (Values.empty())
1632 return true;
1633
1634 // Filter EntryValue locations out early.
1635 if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1636 return true;
1637
1638 SmallVector<SDDbgOperand> LocationOps;
1639 SmallVector<SDNode *> Dependencies;
1640 for (const Value *V : Values) {
1641 // Constant value.
1644 LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1645 continue;
1646 }
1647
1648 // Look through IntToPtr constants.
1649 if (auto *CE = dyn_cast<ConstantExpr>(V))
1650 if (CE->getOpcode() == Instruction::IntToPtr) {
1651 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1652 continue;
1653 }
1654
1655 // If the Value is a frame index, we can create a FrameIndex debug value
1656 // without relying on the DAG at all.
1657 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1658 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1659 if (SI != FuncInfo.StaticAllocaMap.end()) {
1660 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1661 continue;
1662 }
1663 }
1664
1665 // Do not use getValue() in here; we don't want to generate code at
1666 // this point if it hasn't been done yet.
1667 SDValue N = NodeMap[V];
1668 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1669 N = UnusedArgNodeMap[V];
1670
1671 if (N.getNode()) {
1672 // Only emit func arg dbg value for non-variadic dbg.values for now.
1673 if (!IsVariadic &&
1674 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1675 FuncArgumentDbgValueKind::Value, N))
1676 return true;
1677 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1678 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1679 // describe stack slot locations.
1680 //
1681 // Consider "int x = 0; int *px = &x;". There are two kinds of
1682 // interesting debug values here after optimization:
1683 //
1684 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
1685 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1686 //
1687 // Both describe the direct values of their associated variables.
1688 Dependencies.push_back(N.getNode());
1689 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1690 continue;
1691 }
1692 LocationOps.emplace_back(
1693 SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1694 continue;
1695 }
1696
1697 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1698 // Special rules apply for the first dbg.values of parameter variables in a
1699 // function. Identify them by the fact they reference Argument Values, that
1700 // they're parameters, and they are parameters of the current function. We
1701 // need to let them dangle until they get an SDNode.
1702 bool IsParamOfFunc =
1703 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1704 if (IsParamOfFunc)
1705 return false;
1706
1707 // The value is not used in this block yet (or it would have an SDNode).
1708 // We still want the value to appear for the user if possible -- if it has
1709 // an associated VReg, we can refer to that instead.
1710 auto VMI = FuncInfo.ValueMap.find(V);
1711 if (VMI != FuncInfo.ValueMap.end()) {
1712 Register Reg = VMI->second;
1713 // If this is a PHI node, it may be split up into several MI PHI nodes
1714 // (in FunctionLoweringInfo::set).
1715 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1716 V->getType(), std::nullopt);
1717 if (RFV.occupiesMultipleRegs()) {
1718 // FIXME: We could potentially support variadic dbg_values here.
1719 if (IsVariadic)
1720 return false;
1721 unsigned Offset = 0;
1722 unsigned BitsToDescribe = 0;
1723 if (auto VarSize = Var->getSizeInBits())
1724 BitsToDescribe = *VarSize;
1725 if (auto Fragment = Expr->getFragmentInfo())
1726 BitsToDescribe = Fragment->SizeInBits;
1727 for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1728 // Bail out if all bits are described already.
1729 if (Offset >= BitsToDescribe)
1730 break;
1731 // TODO: handle scalable vectors.
1732 unsigned RegisterSize = RegAndSize.second;
1733 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1734 ? BitsToDescribe - Offset
1735 : RegisterSize;
1736 auto FragmentExpr = DIExpression::createFragmentExpression(
1737 Expr, Offset, FragmentSize);
1738 if (!FragmentExpr)
1739 continue;
1740 SDDbgValue *SDV = DAG.getVRegDbgValue(
1741 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1742 DAG.AddDbgValue(SDV, false);
1743 Offset += RegisterSize;
1744 }
1745 return true;
1746 }
1747 // We can use simple vreg locations for variadic dbg_values as well.
1748 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1749 continue;
1750 }
1751 // We failed to create a SDDbgOperand for V.
1752 return false;
1753 }
1754
1755 // We have created a SDDbgOperand for each Value in Values.
1756 assert(!LocationOps.empty());
1757 SDDbgValue *SDV =
1758 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1759 /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1760 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1761 return true;
1762}
1763
1765 // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1766 for (auto &Pair : DanglingDebugInfoMap)
1767 for (auto &DDI : Pair.second)
1768 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1770}
1771
1772/// getCopyFromRegs - If there was virtual register allocated for the value V
1773/// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1776 SDValue Result;
1777
1778 if (It != FuncInfo.ValueMap.end()) {
1779 Register InReg = It->second;
1780
1781 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1782 DAG.getDataLayout(), InReg, Ty,
1783 std::nullopt); // This is not an ABI copy.
1784 SDValue Chain = DAG.getEntryNode();
1785 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1786 V);
1787 resolveDanglingDebugInfo(V, Result);
1788 }
1789
1790 return Result;
1791}
1792
1793/// getValue - Return an SDValue for the given Value.
1795 // If we already have an SDValue for this value, use it. It's important
1796 // to do this first, so that we don't create a CopyFromReg if we already
1797 // have a regular SDValue.
1798 SDValue &N = NodeMap[V];
1799 if (N.getNode()) return N;
1800
1801 // If there's a virtual register allocated and initialized for this
1802 // value, use it.
1803 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1804 return copyFromReg;
1805
1806 // Otherwise create a new SDValue and remember it.
1807 SDValue Val = getValueImpl(V);
1808 NodeMap[V] = Val;
1810 return Val;
1811}
1812
1813/// getNonRegisterValue - Return an SDValue for the given Value, but
1814/// don't look in FuncInfo.ValueMap for a virtual register.
1816 // If we already have an SDValue for this value, use it.
1817 SDValue &N = NodeMap[V];
1818 if (N.getNode()) {
1819 if (isIntOrFPConstant(N)) {
1820 // Remove the debug location from the node as the node is about to be used
1821 // in a location which may differ from the original debug location. This
1822 // is relevant to Constant and ConstantFP nodes because they can appear
1823 // as constant expressions inside PHI nodes.
1824 N->setDebugLoc(DebugLoc());
1825 }
1826 return N;
1827 }
1828
1829 // Otherwise create a new SDValue and remember it.
1830 SDValue Val = getValueImpl(V);
1831 NodeMap[V] = Val;
1833 return Val;
1834}
1835
1836/// getValueImpl - Helper function for getValue and getNonRegisterValue.
1837/// Create an SDValue for the given value.
1839 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1840
1841 if (const Constant *C = dyn_cast<Constant>(V)) {
1842 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1843
1844 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1845 SDLoc DL = getCurSDLoc();
1846
1847 // DAG.getConstant() may attempt to legalise the vector constant which can
1848 // significantly change the combines applied to the DAG. To reduce the
1849 // divergence when enabling ConstantInt based vectors we try to construct
1850 // the DAG in the same way as shufflevector based splats. TODO: The
1851 // divergence sometimes leads to better optimisations. Ideally we should
1852 // prevent DAG.getConstant() from legalising too early but there are some
1853 // degradations preventing this.
1854 if (VT.isScalableVector())
1855 return DAG.getNode(
1856 ISD::SPLAT_VECTOR, DL, VT,
1857 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1858 if (VT.isFixedLengthVector())
1859 return DAG.getSplatBuildVector(
1860 VT, DL,
1861 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1862 return DAG.getConstant(*CI, DL, VT);
1863 }
1864
1865 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1866 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1867
1868 if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1869 return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1870 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1871 getValue(CPA->getAddrDiscriminator()),
1872 getValue(CPA->getDiscriminator()));
1873 }
1874
1876 return DAG.getConstant(0, getCurSDLoc(), VT);
1877
1878 if (match(C, m_VScale()))
1879 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1880
1881 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1882 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1883
1884 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1885 return isa<PoisonValue>(C) ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
1886
1887 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1888 visit(CE->getOpcode(), *CE);
1889 SDValue N1 = NodeMap[V];
1890 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1891 return N1;
1892 }
1893
1895 SmallVector<SDValue, 4> Constants;
1896 for (const Use &U : C->operands()) {
1897 SDNode *Val = getValue(U).getNode();
1898 // If the operand is an empty aggregate, there are no values.
1899 if (!Val) continue;
1900 // Add each leaf value from the operand to the Constants list
1901 // to form a flattened list of all the values.
1902 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1903 Constants.push_back(SDValue(Val, i));
1904 }
1905
1906 return DAG.getMergeValues(Constants, getCurSDLoc());
1907 }
1908
1909 if (const ConstantDataSequential *CDS =
1912 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1913 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1914 // Add each leaf value from the operand to the Constants list
1915 // to form a flattened list of all the values.
1916 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1917 Ops.push_back(SDValue(Val, i));
1918 }
1919
1920 if (isa<ArrayType>(CDS->getType()))
1921 return DAG.getMergeValues(Ops, getCurSDLoc());
1922 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1923 }
1924
1925 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1927 "Unknown struct or array constant!");
1928
1929 SmallVector<EVT, 4> ValueVTs;
1930 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1931 unsigned NumElts = ValueVTs.size();
1932 if (NumElts == 0)
1933 return SDValue(); // empty struct
1934 SmallVector<SDValue, 4> Constants(NumElts);
1935 for (unsigned i = 0; i != NumElts; ++i) {
1936 EVT EltVT = ValueVTs[i];
1937 if (isa<UndefValue>(C))
1938 Constants[i] = DAG.getUNDEF(EltVT);
1939 else if (EltVT.isFloatingPoint())
1940 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1941 else
1942 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1943 }
1944
1945 return DAG.getMergeValues(Constants, getCurSDLoc());
1946 }
1947
1948 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1949 return DAG.getBlockAddress(BA, VT);
1950
1951 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1952 return getValue(Equiv->getGlobalValue());
1953
1954 if (const auto *NC = dyn_cast<NoCFIValue>(C))
1955 return getValue(NC->getGlobalValue());
1956
1957 if (VT == MVT::aarch64svcount) {
1958 assert(C->isNullValue() && "Can only zero this target type!");
1959 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1960 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1961 }
1962
1963 if (VT.isRISCVVectorTuple()) {
1964 assert(C->isNullValue() && "Can only zero this target type!");
1965 return DAG.getNode(
1966 ISD::BITCAST, getCurSDLoc(), VT,
1967 DAG.getNode(
1969 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1970 VT.getSizeInBits().getKnownMinValue() / 8, true),
1971 DAG.getConstant(0, getCurSDLoc(), MVT::getIntegerVT(8))));
1972 }
1973
1974 VectorType *VecTy = cast<VectorType>(V->getType());
1975
1976 // Now that we know the number and type of the elements, get that number of
1977 // elements into the Ops array based on what kind of constant it is.
1978 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1980 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1981 for (unsigned i = 0; i != NumElements; ++i)
1982 Ops.push_back(getValue(CV->getOperand(i)));
1983
1984 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1985 }
1986
1988 EVT EltVT =
1989 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1990
1991 SDValue Op;
1992 if (EltVT.isFloatingPoint())
1993 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1994 else
1995 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1996
1997 return DAG.getSplat(VT, getCurSDLoc(), Op);
1998 }
1999
2000 llvm_unreachable("Unknown vector constant");
2001 }
2002
2003 // If this is a static alloca, generate it as the frameindex instead of
2004 // computation.
2005 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
2007 FuncInfo.StaticAllocaMap.find(AI);
2008 if (SI != FuncInfo.StaticAllocaMap.end())
2009 return DAG.getFrameIndex(
2010 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
2011 }
2012
2013 // If this is an instruction which fast-isel has deferred, select it now.
2014 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2015 Register InReg = FuncInfo.InitializeRegForValue(Inst);
2016
2017 std::optional<CallingConv::ID> CallConv;
2018 auto *CB = dyn_cast<CallBase>(Inst);
2019 if (CB && !CB->isInlineAsm())
2020 CallConv = CB->getCallingConv();
2021
2022 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
2023 Inst->getType(), CallConv);
2024 SDValue Chain = DAG.getEntryNode();
2025 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
2026 }
2027
2028 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
2029 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
2030
2031 if (const auto *BB = dyn_cast<BasicBlock>(V))
2032 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
2033
2034 llvm_unreachable("Can't get register for value!");
2035}
2036
2037void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
2039 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
2040 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
2041 bool IsSEH = isAsynchronousEHPersonality(Pers);
2042 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2043 if (IsSEH) {
2044 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2045 CatchPadMBB->setIsEHContTarget(true);
2047 } else
2048 CatchPadMBB->setIsEHScopeEntry();
2049 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2050 if (IsMSVCCXX || IsCoreCLR)
2051 CatchPadMBB->setIsEHFuncletEntry();
2052}
2053
2054void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2055 // Update machine-CFG edge.
2056 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2057 FuncInfo.MBB->addSuccessor(TargetMBB);
2058
2059 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2060 bool IsSEH = isAsynchronousEHPersonality(Pers);
2061 if (IsSEH) {
2062 // If this is not a fall-through branch or optimizations are switched off,
2063 // emit the branch.
2064 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2065 TM.getOptLevel() == CodeGenOptLevel::None)
2066 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2067 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2068 return;
2069 }
2070
2071 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2072 TargetMBB->setIsEHContTarget(true);
2073 DAG.getMachineFunction().setHasEHContTarget(true);
2074
2075 // Figure out the funclet membership for the catchret's successor.
2076 // This will be used by the FuncletLayout pass to determine how to order the
2077 // BB's.
2078 // A 'catchret' returns to the outer scope's color.
2079 Value *ParentPad = I.getCatchSwitchParentPad();
2080 const BasicBlock *SuccessorColor;
2081 if (isa<ConstantTokenNone>(ParentPad))
2082 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2083 else
2084 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2085 assert(SuccessorColor && "No parent funclet for catchret!");
2086 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2087 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2088
2089 // Create the terminator node.
2090 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2091 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2092 DAG.getBasicBlock(SuccessorColorMBB));
2093 DAG.setRoot(Ret);
2094}
2095
2096void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2097 // Don't emit any special code for the cleanuppad instruction. It just marks
2098 // the start of an EH scope/funclet.
2099 FuncInfo.MBB->setIsEHScopeEntry();
2100 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2101 if (Pers != EHPersonality::Wasm_CXX) {
2102 FuncInfo.MBB->setIsEHFuncletEntry();
2103 FuncInfo.MBB->setIsCleanupFuncletEntry();
2104 }
2105}
2106
2107/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2108/// many places it could ultimately go. In the IR, we have a single unwind
2109/// destination, but in the machine CFG, we enumerate all the possible blocks.
2110/// This function skips over imaginary basic blocks that hold catchswitch
2111/// instructions, and finds all the "real" machine
2112/// basic block destinations. As those destinations may not be successors of
2113/// EHPadBB, here we also calculate the edge probability to those destinations.
2114/// The passed-in Prob is the edge probability to EHPadBB.
2116 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2117 BranchProbability Prob,
2118 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2119 &UnwindDests) {
2120 EHPersonality Personality =
2122 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2123 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2124 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2125 bool IsSEH = isAsynchronousEHPersonality(Personality);
2126
2127 while (EHPadBB) {
2129 BasicBlock *NewEHPadBB = nullptr;
2130 if (isa<LandingPadInst>(Pad)) {
2131 // Stop on landingpads. They are not funclets.
2132 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2133 break;
2134 } else if (isa<CleanupPadInst>(Pad)) {
2135 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2136 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2137 // which always catches an exception.
2138 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2139 UnwindDests.back().first->setIsEHScopeEntry();
2140 // In Wasm, EH scopes are not funclets
2141 if (!IsWasmCXX)
2142 UnwindDests.back().first->setIsEHFuncletEntry();
2143 break;
2144 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2145 // Add the catchpad handlers to the possible destinations.
2146 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2147 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2148 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2149 if (IsMSVCCXX || IsCoreCLR)
2150 UnwindDests.back().first->setIsEHFuncletEntry();
2151 if (!IsSEH)
2152 UnwindDests.back().first->setIsEHScopeEntry();
2153 }
2154 NewEHPadBB = CatchSwitch->getUnwindDest();
2155 } else {
2156 continue;
2157 }
2158
2159 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2160 if (BPI && NewEHPadBB)
2161 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2162 EHPadBB = NewEHPadBB;
2163 }
2164}
2165
2166void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2167 // Update successor info.
2169 auto UnwindDest = I.getUnwindDest();
2170 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2171 BranchProbability UnwindDestProb =
2172 (BPI && UnwindDest)
2173 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2175 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2176 for (auto &UnwindDest : UnwindDests) {
2177 UnwindDest.first->setIsEHPad();
2178 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2179 }
2180 FuncInfo.MBB->normalizeSuccProbs();
2181
2182 // Create the terminator node.
2183 MachineBasicBlock *CleanupPadMBB =
2184 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2185 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
2186 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2187 DAG.setRoot(Ret);
2188}
2189
2190void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2191 report_fatal_error("visitCatchSwitch not yet implemented!");
2192}
2193
2194void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2195 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2196 auto &DL = DAG.getDataLayout();
2197 SDValue Chain = getControlRoot();
2200
2201 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2202 // lower
2203 //
2204 // %val = call <ty> @llvm.experimental.deoptimize()
2205 // ret <ty> %val
2206 //
2207 // differently.
2208 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2210 return;
2211 }
2212
2213 if (!FuncInfo.CanLowerReturn) {
2214 Register DemoteReg = FuncInfo.DemoteRegister;
2215
2216 // Emit a store of the return value through the virtual register.
2217 // Leave Outs empty so that LowerReturn won't try to load return
2218 // registers the usual way.
2219 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2220 SDValue RetPtr =
2221 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2222 SDValue RetOp = getValue(I.getOperand(0));
2223
2224 SmallVector<EVT, 4> ValueVTs, MemVTs;
2225 SmallVector<uint64_t, 4> Offsets;
2226 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2227 &Offsets, 0);
2228 unsigned NumValues = ValueVTs.size();
2229
2230 SmallVector<SDValue, 4> Chains(NumValues);
2231 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2232 for (unsigned i = 0; i != NumValues; ++i) {
2233 // An aggregate return value cannot wrap around the address space, so
2234 // offsets to its parts don't wrap either.
2235 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2236 TypeSize::getFixed(Offsets[i]));
2237
2238 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2239 if (MemVTs[i] != ValueVTs[i])
2240 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2241 Chains[i] = DAG.getStore(
2242 Chain, getCurSDLoc(), Val,
2243 // FIXME: better loc info would be nice.
2244 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2245 commonAlignment(BaseAlign, Offsets[i]));
2246 }
2247
2248 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2249 MVT::Other, Chains);
2250 } else if (I.getNumOperands() != 0) {
2252 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2253 unsigned NumValues = Types.size();
2254 if (NumValues) {
2255 SDValue RetOp = getValue(I.getOperand(0));
2256
2257 const Function *F = I.getParent()->getParent();
2258
2259 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2260 I.getOperand(0)->getType(), F->getCallingConv(),
2261 /*IsVarArg*/ false, DL);
2262
2263 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2264 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2265 ExtendKind = ISD::SIGN_EXTEND;
2266 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2267 ExtendKind = ISD::ZERO_EXTEND;
2268
2269 LLVMContext &Context = F->getContext();
2270 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2271
2272 for (unsigned j = 0; j != NumValues; ++j) {
2273 EVT VT = TLI.getValueType(DL, Types[j]);
2274
2275 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2276 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2277
2278 CallingConv::ID CC = F->getCallingConv();
2279
2280 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2281 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2282 SmallVector<SDValue, 4> Parts(NumParts);
2284 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2285 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2286
2287 // 'inreg' on function refers to return value
2288 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2289 if (RetInReg)
2290 Flags.setInReg();
2291
2292 if (I.getOperand(0)->getType()->isPointerTy()) {
2293 Flags.setPointer();
2294 Flags.setPointerAddrSpace(
2295 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2296 }
2297
2298 if (NeedsRegBlock) {
2299 Flags.setInConsecutiveRegs();
2300 if (j == NumValues - 1)
2301 Flags.setInConsecutiveRegsLast();
2302 }
2303
2304 // Propagate extension type if any
2305 if (ExtendKind == ISD::SIGN_EXTEND)
2306 Flags.setSExt();
2307 else if (ExtendKind == ISD::ZERO_EXTEND)
2308 Flags.setZExt();
2309 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2310 Flags.setNoExt();
2311
2312 for (unsigned i = 0; i < NumParts; ++i) {
2313 Outs.push_back(ISD::OutputArg(Flags,
2314 Parts[i].getValueType().getSimpleVT(),
2315 VT, Types[j], 0, 0));
2316 OutVals.push_back(Parts[i]);
2317 }
2318 }
2319 }
2320 }
2321
2322 // Push in swifterror virtual register as the last element of Outs. This makes
2323 // sure swifterror virtual register will be returned in the swifterror
2324 // physical register.
2325 const Function *F = I.getParent()->getParent();
2326 if (TLI.supportSwiftError() &&
2327 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2328 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2329 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2330 Flags.setSwiftError();
2331 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2332 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2333 PointerType::getUnqual(*DAG.getContext()),
2334 /*origidx=*/1, /*partOffs=*/0));
2335 // Create SDNode for the swifterror virtual register.
2336 OutVals.push_back(
2337 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2338 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2339 EVT(TLI.getPointerTy(DL))));
2340 }
2341
2342 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2343 CallingConv::ID CallConv =
2344 DAG.getMachineFunction().getFunction().getCallingConv();
2345 Chain = DAG.getTargetLoweringInfo().LowerReturn(
2346 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2347
2348 // Verify that the target's LowerReturn behaved as expected.
2349 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2350 "LowerReturn didn't return a valid chain!");
2351
2352 // Update the DAG with the new chain value resulting from return lowering.
2353 DAG.setRoot(Chain);
2354}
2355
2356/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2357/// created for it, emit nodes to copy the value into the virtual
2358/// registers.
2360 // Skip empty types
2361 if (V->getType()->isEmptyTy())
2362 return;
2363
2365 if (VMI != FuncInfo.ValueMap.end()) {
2366 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2367 "Unused value assigned virtual registers!");
2368 CopyValueToVirtualRegister(V, VMI->second);
2369 }
2370}
2371
2372/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2373/// the current basic block, add it to ValueMap now so that we'll get a
2374/// CopyTo/FromReg.
2376 // No need to export constants.
2377 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2378
2379 // Already exported?
2380 if (FuncInfo.isExportedInst(V)) return;
2381
2382 Register Reg = FuncInfo.InitializeRegForValue(V);
2384}
2385
2387 const BasicBlock *FromBB) {
2388 // The operands of the setcc have to be in this block. We don't know
2389 // how to export them from some other block.
2390 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2391 // Can export from current BB.
2392 if (VI->getParent() == FromBB)
2393 return true;
2394
2395 // Is already exported, noop.
2396 return FuncInfo.isExportedInst(V);
2397 }
2398
2399 // If this is an argument, we can export it if the BB is the entry block or
2400 // if it is already exported.
2401 if (isa<Argument>(V)) {
2402 if (FromBB->isEntryBlock())
2403 return true;
2404
2405 // Otherwise, can only export this if it is already exported.
2406 return FuncInfo.isExportedInst(V);
2407 }
2408
2409 // Otherwise, constants can always be exported.
2410 return true;
2411}
2412
2413/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2415SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2416 const MachineBasicBlock *Dst) const {
2418 const BasicBlock *SrcBB = Src->getBasicBlock();
2419 const BasicBlock *DstBB = Dst->getBasicBlock();
2420 if (!BPI) {
2421 // If BPI is not available, set the default probability as 1 / N, where N is
2422 // the number of successors.
2423 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2424 return BranchProbability(1, SuccSize);
2425 }
2426 return BPI->getEdgeProbability(SrcBB, DstBB);
2427}
2428
2429void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2430 MachineBasicBlock *Dst,
2431 BranchProbability Prob) {
2432 if (!FuncInfo.BPI)
2433 Src->addSuccessorWithoutProb(Dst);
2434 else {
2435 if (Prob.isUnknown())
2436 Prob = getEdgeProbability(Src, Dst);
2437 Src->addSuccessor(Dst, Prob);
2438 }
2439}
2440
2441static bool InBlock(const Value *V, const BasicBlock *BB) {
2442 if (const Instruction *I = dyn_cast<Instruction>(V))
2443 return I->getParent() == BB;
2444 return true;
2445}
2446
2447/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2448/// This function emits a branch and is used at the leaves of an OR or an
2449/// AND operator tree.
2450void
2453 MachineBasicBlock *FBB,
2454 MachineBasicBlock *CurBB,
2455 MachineBasicBlock *SwitchBB,
2456 BranchProbability TProb,
2457 BranchProbability FProb,
2458 bool InvertCond) {
2459 const BasicBlock *BB = CurBB->getBasicBlock();
2460
2461 // If the leaf of the tree is a comparison, merge the condition into
2462 // the caseblock.
2463 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2464 // The operands of the cmp have to be in this block. We don't know
2465 // how to export them from some other block. If this is the first block
2466 // of the sequence, no exporting is needed.
2467 if (CurBB == SwitchBB ||
2468 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2469 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2470 ISD::CondCode Condition;
2471 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2472 ICmpInst::Predicate Pred =
2473 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2474 Condition = getICmpCondCode(Pred);
2475 } else {
2476 const FCmpInst *FC = cast<FCmpInst>(Cond);
2477 FCmpInst::Predicate Pred =
2478 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2479 Condition = getFCmpCondCode(Pred);
2480 if (TM.Options.NoNaNsFPMath)
2481 Condition = getFCmpCodeWithoutNaN(Condition);
2482 }
2483
2484 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2485 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2486 SL->SwitchCases.push_back(CB);
2487 return;
2488 }
2489 }
2490
2491 // Create a CaseBlock record representing this branch.
2492 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2493 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2494 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2495 SL->SwitchCases.push_back(CB);
2496}
2497
2498// Collect dependencies on V recursively. This is used for the cost analysis in
2499// `shouldKeepJumpConditionsTogether`.
2503 unsigned Depth = 0) {
2504 // Return false if we have an incomplete count.
2506 return false;
2507
2508 auto *I = dyn_cast<Instruction>(V);
2509 if (I == nullptr)
2510 return true;
2511
2512 if (Necessary != nullptr) {
2513 // This instruction is necessary for the other side of the condition so
2514 // don't count it.
2515 if (Necessary->contains(I))
2516 return true;
2517 }
2518
2519 // Already added this dep.
2520 if (!Deps->try_emplace(I, false).second)
2521 return true;
2522
2523 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2524 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2525 Depth + 1))
2526 return false;
2527 return true;
2528}
2529
2532 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2534 if (I.getNumSuccessors() != 2)
2535 return false;
2536
2537 if (!I.isConditional())
2538 return false;
2539
2540 if (Params.BaseCost < 0)
2541 return false;
2542
2543 // Baseline cost.
2544 InstructionCost CostThresh = Params.BaseCost;
2545
2546 BranchProbabilityInfo *BPI = nullptr;
2547 if (Params.LikelyBias || Params.UnlikelyBias)
2548 BPI = FuncInfo.BPI;
2549 if (BPI != nullptr) {
2550 // See if we are either likely to get an early out or compute both lhs/rhs
2551 // of the condition.
2552 BasicBlock *IfFalse = I.getSuccessor(0);
2553 BasicBlock *IfTrue = I.getSuccessor(1);
2554
2555 std::optional<bool> Likely;
2556 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2557 Likely = true;
2558 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2559 Likely = false;
2560
2561 if (Likely) {
2562 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2563 // Its likely we will have to compute both lhs and rhs of condition
2564 CostThresh += Params.LikelyBias;
2565 else {
2566 if (Params.UnlikelyBias < 0)
2567 return false;
2568 // Its likely we will get an early out.
2569 CostThresh -= Params.UnlikelyBias;
2570 }
2571 }
2572 }
2573
2574 if (CostThresh <= 0)
2575 return false;
2576
2577 // Collect "all" instructions that lhs condition is dependent on.
2578 // Use map for stable iteration (to avoid non-determanism of iteration of
2579 // SmallPtrSet). The `bool` value is just a dummy.
2581 collectInstructionDeps(&LhsDeps, Lhs);
2582 // Collect "all" instructions that rhs condition is dependent on AND are
2583 // dependencies of lhs. This gives us an estimate on which instructions we
2584 // stand to save by splitting the condition.
2585 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2586 return false;
2587 // Add the compare instruction itself unless its a dependency on the LHS.
2588 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2589 if (!LhsDeps.contains(RhsI))
2590 RhsDeps.try_emplace(RhsI, false);
2591
2592 const auto &TLI = DAG.getTargetLoweringInfo();
2593 const auto &TTI =
2594 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
2595
2596 InstructionCost CostOfIncluding = 0;
2597 // See if this instruction will need to computed independently of whether RHS
2598 // is.
2599 Value *BrCond = I.getCondition();
2600 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2601 for (const auto *U : Ins->users()) {
2602 // If user is independent of RHS calculation we don't need to count it.
2603 if (auto *UIns = dyn_cast<Instruction>(U))
2604 if (UIns != BrCond && !RhsDeps.contains(UIns))
2605 return false;
2606 }
2607 return true;
2608 };
2609
2610 // Prune instructions from RHS Deps that are dependencies of unrelated
2611 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2612 // arbitrary and just meant to cap the how much time we spend in the pruning
2613 // loop. Its highly unlikely to come into affect.
2614 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2615 // Stop after a certain point. No incorrectness from including too many
2616 // instructions.
2617 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2618 const Instruction *ToDrop = nullptr;
2619 for (const auto &InsPair : RhsDeps) {
2620 if (!ShouldCountInsn(InsPair.first)) {
2621 ToDrop = InsPair.first;
2622 break;
2623 }
2624 }
2625 if (ToDrop == nullptr)
2626 break;
2627 RhsDeps.erase(ToDrop);
2628 }
2629
2630 for (const auto &InsPair : RhsDeps) {
2631 // Finally accumulate latency that we can only attribute to computing the
2632 // RHS condition. Use latency because we are essentially trying to calculate
2633 // the cost of the dependency chain.
2634 // Possible TODO: We could try to estimate ILP and make this more precise.
2635 CostOfIncluding +=
2636 TTI.getInstructionCost(InsPair.first, TargetTransformInfo::TCK_Latency);
2637
2638 if (CostOfIncluding > CostThresh)
2639 return false;
2640 }
2641 return true;
2642}
2643
2646 MachineBasicBlock *FBB,
2647 MachineBasicBlock *CurBB,
2648 MachineBasicBlock *SwitchBB,
2650 BranchProbability TProb,
2651 BranchProbability FProb,
2652 bool InvertCond) {
2653 // Skip over not part of the tree and remember to invert op and operands at
2654 // next level.
2655 Value *NotCond;
2656 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2657 InBlock(NotCond, CurBB->getBasicBlock())) {
2658 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2659 !InvertCond);
2660 return;
2661 }
2662
2664 const Value *BOpOp0, *BOpOp1;
2665 // Compute the effective opcode for Cond, taking into account whether it needs
2666 // to be inverted, e.g.
2667 // and (not (or A, B)), C
2668 // gets lowered as
2669 // and (and (not A, not B), C)
2671 if (BOp) {
2672 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2673 ? Instruction::And
2674 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2675 ? Instruction::Or
2677 if (InvertCond) {
2678 if (BOpc == Instruction::And)
2679 BOpc = Instruction::Or;
2680 else if (BOpc == Instruction::Or)
2681 BOpc = Instruction::And;
2682 }
2683 }
2684
2685 // If this node is not part of the or/and tree, emit it as a branch.
2686 // Note that all nodes in the tree should have same opcode.
2687 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2688 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2689 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2690 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2691 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2692 TProb, FProb, InvertCond);
2693 return;
2694 }
2695
2696 // Create TmpBB after CurBB.
2697 MachineFunction::iterator BBI(CurBB);
2698 MachineFunction &MF = DAG.getMachineFunction();
2700 CurBB->getParent()->insert(++BBI, TmpBB);
2701
2702 if (Opc == Instruction::Or) {
2703 // Codegen X | Y as:
2704 // BB1:
2705 // jmp_if_X TBB
2706 // jmp TmpBB
2707 // TmpBB:
2708 // jmp_if_Y TBB
2709 // jmp FBB
2710 //
2711
2712 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2713 // The requirement is that
2714 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2715 // = TrueProb for original BB.
2716 // Assuming the original probabilities are A and B, one choice is to set
2717 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2718 // A/(1+B) and 2B/(1+B). This choice assumes that
2719 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2720 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2721 // TmpBB, but the math is more complicated.
2722
2723 auto NewTrueProb = TProb / 2;
2724 auto NewFalseProb = TProb / 2 + FProb;
2725 // Emit the LHS condition.
2726 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2727 NewFalseProb, InvertCond);
2728
2729 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2730 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2732 // Emit the RHS condition into TmpBB.
2733 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2734 Probs[1], InvertCond);
2735 } else {
2736 assert(Opc == Instruction::And && "Unknown merge op!");
2737 // Codegen X & Y as:
2738 // BB1:
2739 // jmp_if_X TmpBB
2740 // jmp FBB
2741 // TmpBB:
2742 // jmp_if_Y TBB
2743 // jmp FBB
2744 //
2745 // This requires creation of TmpBB after CurBB.
2746
2747 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2748 // The requirement is that
2749 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2750 // = FalseProb for original BB.
2751 // Assuming the original probabilities are A and B, one choice is to set
2752 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2753 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2754 // TrueProb for BB1 * FalseProb for TmpBB.
2755
2756 auto NewTrueProb = TProb + FProb / 2;
2757 auto NewFalseProb = FProb / 2;
2758 // Emit the LHS condition.
2759 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2760 NewFalseProb, InvertCond);
2761
2762 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2763 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2765 // Emit the RHS condition into TmpBB.
2766 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2767 Probs[1], InvertCond);
2768 }
2769}
2770
2771/// If the set of cases should be emitted as a series of branches, return true.
2772/// If we should emit this as a bunch of and/or'd together conditions, return
2773/// false.
2774bool
2775SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2776 if (Cases.size() != 2) return true;
2777
2778 // If this is two comparisons of the same values or'd or and'd together, they
2779 // will get folded into a single comparison, so don't emit two blocks.
2780 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2781 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2782 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2783 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2784 return false;
2785 }
2786
2787 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2788 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2789 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2790 Cases[0].CC == Cases[1].CC &&
2791 isa<Constant>(Cases[0].CmpRHS) &&
2792 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2793 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2794 return false;
2795 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2796 return false;
2797 }
2798
2799 return true;
2800}
2801
2802void SelectionDAGBuilder::visitBr(const BranchInst &I) {
2804
2805 // Update machine-CFG edges.
2806 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2807
2808 if (I.isUnconditional()) {
2809 // Update machine-CFG edges.
2810 BrMBB->addSuccessor(Succ0MBB);
2811
2812 // If this is not a fall-through branch or optimizations are switched off,
2813 // emit the branch.
2814 if (Succ0MBB != NextBlock(BrMBB) ||
2816 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2817 getControlRoot(), DAG.getBasicBlock(Succ0MBB));
2818 setValue(&I, Br);
2819 DAG.setRoot(Br);
2820 }
2821
2822 return;
2823 }
2824
2825 // If this condition is one of the special cases we handle, do special stuff
2826 // now.
2827 const Value *CondVal = I.getCondition();
2828 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2829
2830 // If this is a series of conditions that are or'd or and'd together, emit
2831 // this as a sequence of branches instead of setcc's with and/or operations.
2832 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2833 // unpredictable branches, and vector extracts because those jumps are likely
2834 // expensive for any target), this should improve performance.
2835 // For example, instead of something like:
2836 // cmp A, B
2837 // C = seteq
2838 // cmp D, E
2839 // F = setle
2840 // or C, F
2841 // jnz foo
2842 // Emit:
2843 // cmp A, B
2844 // je foo
2845 // cmp D, E
2846 // jle foo
2847 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2848 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2849 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2850 BOp->hasOneUse() && !IsUnpredictable) {
2851 Value *Vec;
2852 const Value *BOp0, *BOp1;
2854 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2855 Opcode = Instruction::And;
2856 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2857 Opcode = Instruction::Or;
2858
2859 if (Opcode &&
2860 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2861 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2863 FuncInfo, I, Opcode, BOp0, BOp1,
2864 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2865 Opcode, BOp0, BOp1))) {
2866 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2867 getEdgeProbability(BrMBB, Succ0MBB),
2868 getEdgeProbability(BrMBB, Succ1MBB),
2869 /*InvertCond=*/false);
2870 // If the compares in later blocks need to use values not currently
2871 // exported from this block, export them now. This block should always
2872 // be the first entry.
2873 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2874
2875 // Allow some cases to be rejected.
2876 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2877 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2878 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2879 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2880 }
2881
2882 // Emit the branch for this block.
2883 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2884 SL->SwitchCases.erase(SL->SwitchCases.begin());
2885 return;
2886 }
2887
2888 // Okay, we decided not to do this, remove any inserted MBB's and clear
2889 // SwitchCases.
2890 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2891 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2892
2893 SL->SwitchCases.clear();
2894 }
2895 }
2896
2897 // Create a CaseBlock record representing this branch.
2898 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2899 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2901 IsUnpredictable);
2902
2903 // Use visitSwitchCase to actually insert the fast branch sequence for this
2904 // cond branch.
2905 visitSwitchCase(CB, BrMBB);
2906}
2907
2908/// visitSwitchCase - Emits the necessary code to represent a single node in
2909/// the binary search tree resulting from lowering a switch instruction.
2911 MachineBasicBlock *SwitchBB) {
2912 SDValue Cond;
2913 SDValue CondLHS = getValue(CB.CmpLHS);
2914 SDLoc dl = CB.DL;
2915
2916 if (CB.CC == ISD::SETTRUE) {
2917 // Branch or fall through to TrueBB.
2918 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2919 SwitchBB->normalizeSuccProbs();
2920 if (CB.TrueBB != NextBlock(SwitchBB)) {
2921 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2922 DAG.getBasicBlock(CB.TrueBB)));
2923 }
2924 return;
2925 }
2926
2927 auto &TLI = DAG.getTargetLoweringInfo();
2928 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2929
2930 // Build the setcc now.
2931 if (!CB.CmpMHS) {
2932 // Fold "(X == true)" to X and "(X == false)" to !X to
2933 // handle common cases produced by branch lowering.
2934 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2935 CB.CC == ISD::SETEQ)
2936 Cond = CondLHS;
2937 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2938 CB.CC == ISD::SETEQ) {
2939 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2940 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2941 } else {
2942 SDValue CondRHS = getValue(CB.CmpRHS);
2943
2944 // If a pointer's DAG type is larger than its memory type then the DAG
2945 // values are zero-extended. This breaks signed comparisons so truncate
2946 // back to the underlying type before doing the compare.
2947 if (CondLHS.getValueType() != MemVT) {
2948 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2949 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2950 }
2951 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2952 }
2953 } else {
2954 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2955
2956 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2957 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2958
2959 SDValue CmpOp = getValue(CB.CmpMHS);
2960 EVT VT = CmpOp.getValueType();
2961
2962 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2963 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2964 ISD::SETLE);
2965 } else {
2966 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2967 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2968 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2969 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2970 }
2971 }
2972
2973 // Update successor info
2974 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2975 // TrueBB and FalseBB are always different unless the incoming IR is
2976 // degenerate. This only happens when running llc on weird IR.
2977 if (CB.TrueBB != CB.FalseBB)
2978 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2979 SwitchBB->normalizeSuccProbs();
2980
2981 // If the lhs block is the next block, invert the condition so that we can
2982 // fall through to the lhs instead of the rhs block.
2983 if (CB.TrueBB == NextBlock(SwitchBB)) {
2984 std::swap(CB.TrueBB, CB.FalseBB);
2985 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2986 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2987 }
2988
2989 SDNodeFlags Flags;
2991 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2992 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2993
2994 setValue(CurInst, BrCond);
2995
2996 // Insert the false branch. Do this even if it's a fall through branch,
2997 // this makes it easier to do DAG optimizations which require inverting
2998 // the branch condition.
2999 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3000 DAG.getBasicBlock(CB.FalseBB));
3001
3002 DAG.setRoot(BrCond);
3003}
3004
3005/// visitJumpTable - Emit JumpTable node in the current MBB
3007 // Emit the code for the jump table
3008 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3009 assert(JT.Reg && "Should lower JT Header first!");
3010 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
3011 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
3012 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
3013 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
3014 Index.getValue(1), Table, Index);
3015 DAG.setRoot(BrJumpTable);
3016}
3017
3018/// visitJumpTableHeader - This function emits necessary code to produce index
3019/// in the JumpTable from switch case.
3021 JumpTableHeader &JTH,
3022 MachineBasicBlock *SwitchBB) {
3023 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3024 const SDLoc &dl = *JT.SL;
3025
3026 // Subtract the lowest switch case value from the value being switched on.
3027 SDValue SwitchOp = getValue(JTH.SValue);
3028 EVT VT = SwitchOp.getValueType();
3029 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
3030 DAG.getConstant(JTH.First, dl, VT));
3031
3032 // The SDNode we just created, which holds the value being switched on minus
3033 // the smallest case value, needs to be copied to a virtual register so it
3034 // can be used as an index into the jump table in a subsequent basic block.
3035 // This value may be smaller or larger than the target's pointer type, and
3036 // therefore require extension or truncating.
3037 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3038 SwitchOp =
3039 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
3040
3041 Register JumpTableReg =
3042 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
3043 SDValue CopyTo =
3044 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3045 JT.Reg = JumpTableReg;
3046
3047 if (!JTH.FallthroughUnreachable) {
3048 // Emit the range check for the jump table, and branch to the default block
3049 // for the switch statement if the value being switched on exceeds the
3050 // largest case in the switch.
3051 SDValue CMP = DAG.getSetCC(
3052 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3053 Sub.getValueType()),
3054 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3055
3056 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3057 MVT::Other, CopyTo, CMP,
3058 DAG.getBasicBlock(JT.Default));
3059
3060 // Avoid emitting unnecessary branches to the next block.
3061 if (JT.MBB != NextBlock(SwitchBB))
3062 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3063 DAG.getBasicBlock(JT.MBB));
3064
3065 DAG.setRoot(BrCond);
3066 } else {
3067 // Avoid emitting unnecessary branches to the next block.
3068 if (JT.MBB != NextBlock(SwitchBB))
3069 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3070 DAG.getBasicBlock(JT.MBB)));
3071 else
3072 DAG.setRoot(CopyTo);
3073 }
3074}
3075
3076/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3077/// variable if there exists one.
3079 SDValue &Chain) {
3080 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3081 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3082 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3086 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3087 if (Global) {
3088 MachinePointerInfo MPInfo(Global);
3092 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3093 DAG.setNodeMemRefs(Node, {MemRef});
3094 }
3095 if (PtrTy != PtrMemTy)
3096 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3097 return SDValue(Node, 0);
3098}
3099
3100/// Codegen a new tail for a stack protector check ParentMBB which has had its
3101/// tail spliced into a stack protector check success bb.
3102///
3103/// For a high level explanation of how this fits into the stack protector
3104/// generation see the comment on the declaration of class
3105/// StackProtectorDescriptor.
3107 MachineBasicBlock *ParentBB) {
3108
3109 // First create the loads to the guard/stack slot for the comparison.
3110 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3111 auto &DL = DAG.getDataLayout();
3112 EVT PtrTy = TLI.getFrameIndexTy(DL);
3113 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3114
3115 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3116 int FI = MFI.getStackProtectorIndex();
3117
3118 SDValue Guard;
3119 SDLoc dl = getCurSDLoc();
3120 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3121 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3122 Align Align = DL.getPrefTypeAlign(
3123 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3124
3125 // Generate code to load the content of the guard slot.
3126 SDValue GuardVal = DAG.getLoad(
3127 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3128 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3130
3131 if (TLI.useStackGuardXorFP())
3132 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3133
3134 // If we're using function-based instrumentation, call the guard check
3135 // function
3137 // Get the guard check function from the target and verify it exists since
3138 // we're using function-based instrumentation
3139 const Function *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3140 assert(GuardCheckFn && "Guard check function is null");
3141
3142 // The target provides a guard check function to validate the guard value.
3143 // Generate a call to that function with the content of the guard slot as
3144 // argument.
3145 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3146 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3147
3149 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3150 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3151 Entry.IsInReg = true;
3152 Args.push_back(Entry);
3153
3156 .setChain(DAG.getEntryNode())
3157 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3158 getValue(GuardCheckFn), std::move(Args));
3159
3160 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3161 DAG.setRoot(Result.second);
3162 return;
3163 }
3164
3165 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3166 // Otherwise, emit a volatile load to retrieve the stack guard value.
3167 SDValue Chain = DAG.getEntryNode();
3168 if (TLI.useLoadStackGuardNode(M)) {
3169 Guard = getLoadStackGuard(DAG, dl, Chain);
3170 } else {
3171 if (const Value *IRGuard = TLI.getSDagStackGuard(M)) {
3172 SDValue GuardPtr = getValue(IRGuard);
3173 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3174 MachinePointerInfo(IRGuard, 0), Align,
3176 } else {
3177 LLVMContext &Ctx = *DAG.getContext();
3178 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
3179 Guard = DAG.getPOISON(PtrMemTy);
3180 }
3181 }
3182
3183 // Perform the comparison via a getsetcc.
3184 SDValue Cmp = DAG.getSetCC(
3185 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3186 Guard, GuardVal, ISD::SETNE);
3187
3188 // If the guard/stackslot do not equal, branch to failure MBB.
3189 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3190 MVT::Other, GuardVal.getOperand(0),
3191 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3192 // Otherwise branch to success MBB.
3193 SDValue Br = DAG.getNode(ISD::BR, dl,
3194 MVT::Other, BrCond,
3195 DAG.getBasicBlock(SPD.getSuccessMBB()));
3196
3197 DAG.setRoot(Br);
3198}
3199
3200/// Codegen the failure basic block for a stack protector check.
3201///
3202/// A failure stack protector machine basic block consists simply of a call to
3203/// __stack_chk_fail().
3204///
3205/// For a high level explanation of how this fits into the stack protector
3206/// generation see the comment on the declaration of class
3207/// StackProtectorDescriptor.
3210
3211 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3212 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3213 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3214 SDValue Chain;
3215
3216 // For -Oz builds with a guard check function, we use function-based
3217 // instrumentation. Otherwise, if we have a guard check function, we call it
3218 // in the failure block.
3219 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3220 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3221 // First create the loads to the guard/stack slot for the comparison.
3222 auto &DL = DAG.getDataLayout();
3223 EVT PtrTy = TLI.getFrameIndexTy(DL);
3224 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3225
3226 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3227 int FI = MFI.getStackProtectorIndex();
3228
3229 SDLoc dl = getCurSDLoc();
3230 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3231 Align Align = DL.getPrefTypeAlign(
3232 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3233
3234 // Generate code to load the content of the guard slot.
3235 SDValue GuardVal = DAG.getLoad(
3236 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3237 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3239
3240 if (TLI.useStackGuardXorFP())
3241 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3242
3243 // The target provides a guard check function to validate the guard value.
3244 // Generate a call to that function with the content of the guard slot as
3245 // argument.
3246 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3247 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3248
3250 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3251 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3252 Entry.IsInReg = true;
3253 Args.push_back(Entry);
3254
3257 .setChain(DAG.getEntryNode())
3258 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3259 getValue(GuardCheckFn), std::move(Args));
3260
3261 Chain = TLI.LowerCallTo(CLI).second;
3262 } else {
3264 CallOptions.setDiscardResult(true);
3265 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3266 {}, CallOptions, getCurSDLoc())
3267 .second;
3268 }
3269
3270 // Emit a trap instruction if we are required to do so.
3271 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3272 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3273 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3274
3275 DAG.setRoot(Chain);
3276}
3277
3278/// visitBitTestHeader - This function emits necessary code to produce value
3279/// suitable for "bit tests"
3281 MachineBasicBlock *SwitchBB) {
3282 SDLoc dl = getCurSDLoc();
3283
3284 // Subtract the minimum value.
3285 SDValue SwitchOp = getValue(B.SValue);
3286 EVT VT = SwitchOp.getValueType();
3287 SDValue RangeSub =
3288 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3289
3290 // Determine the type of the test operands.
3291 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3292 bool UsePtrType = false;
3293 if (!TLI.isTypeLegal(VT)) {
3294 UsePtrType = true;
3295 } else {
3296 for (const BitTestCase &Case : B.Cases)
3297 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3298 // Switch table case range are encoded into series of masks.
3299 // Just use pointer type, it's guaranteed to fit.
3300 UsePtrType = true;
3301 break;
3302 }
3303 }
3304 SDValue Sub = RangeSub;
3305 if (UsePtrType) {
3306 VT = TLI.getPointerTy(DAG.getDataLayout());
3307 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3308 }
3309
3310 B.RegVT = VT.getSimpleVT();
3311 B.Reg = FuncInfo.CreateReg(B.RegVT);
3312 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3313
3314 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3315
3316 if (!B.FallthroughUnreachable)
3317 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3318 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3319 SwitchBB->normalizeSuccProbs();
3320
3321 SDValue Root = CopyTo;
3322 if (!B.FallthroughUnreachable) {
3323 // Conditional branch to the default block.
3324 SDValue RangeCmp = DAG.getSetCC(dl,
3325 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3326 RangeSub.getValueType()),
3327 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3328 ISD::SETUGT);
3329
3330 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3331 DAG.getBasicBlock(B.Default));
3332 }
3333
3334 // Avoid emitting unnecessary branches to the next block.
3335 if (MBB != NextBlock(SwitchBB))
3336 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3337
3338 DAG.setRoot(Root);
3339}
3340
3341/// visitBitTestCase - this function produces one "bit test"
3343 MachineBasicBlock *NextMBB,
3344 BranchProbability BranchProbToNext,
3345 Register Reg, BitTestCase &B,
3346 MachineBasicBlock *SwitchBB) {
3347 SDLoc dl = getCurSDLoc();
3348 MVT VT = BB.RegVT;
3349 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3350 SDValue Cmp;
3351 unsigned PopCount = llvm::popcount(B.Mask);
3352 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3353 if (PopCount == 1) {
3354 // Testing for a single bit; just compare the shift count with what it
3355 // would need to be to shift a 1 bit in that position.
3356 Cmp = DAG.getSetCC(
3357 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3358 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3359 ISD::SETEQ);
3360 } else if (PopCount == BB.Range) {
3361 // There is only one zero bit in the range, test for it directly.
3362 Cmp = DAG.getSetCC(
3363 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3364 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3365 } else {
3366 // Make desired shift
3367 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3368 DAG.getConstant(1, dl, VT), ShiftOp);
3369
3370 // Emit bit tests and jumps
3371 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3372 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3373 Cmp = DAG.getSetCC(
3374 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3375 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3376 }
3377
3378 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3379 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3380 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3381 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3382 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3383 // one as they are relative probabilities (and thus work more like weights),
3384 // and hence we need to normalize them to let the sum of them become one.
3385 SwitchBB->normalizeSuccProbs();
3386
3387 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3388 MVT::Other, getControlRoot(),
3389 Cmp, DAG.getBasicBlock(B.TargetBB));
3390
3391 // Avoid emitting unnecessary branches to the next block.
3392 if (NextMBB != NextBlock(SwitchBB))
3393 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3394 DAG.getBasicBlock(NextMBB));
3395
3396 DAG.setRoot(BrAnd);
3397}
3398
3399void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3400 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3401
3402 // Retrieve successors. Look through artificial IR level blocks like
3403 // catchswitch for successors.
3404 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3405 const BasicBlock *EHPadBB = I.getSuccessor(1);
3406 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3407
3408 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3409 // have to do anything here to lower funclet bundles.
3410 failForInvalidBundles(I, "invokes",
3416
3417 const Value *Callee(I.getCalledOperand());
3418 const Function *Fn = dyn_cast<Function>(Callee);
3419 if (isa<InlineAsm>(Callee))
3420 visitInlineAsm(I, EHPadBB);
3421 else if (Fn && Fn->isIntrinsic()) {
3422 switch (Fn->getIntrinsicID()) {
3423 default:
3424 llvm_unreachable("Cannot invoke this intrinsic");
3425 case Intrinsic::donothing:
3426 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3427 case Intrinsic::seh_try_begin:
3428 case Intrinsic::seh_scope_begin:
3429 case Intrinsic::seh_try_end:
3430 case Intrinsic::seh_scope_end:
3431 if (EHPadMBB)
3432 // a block referenced by EH table
3433 // so dtor-funclet not removed by opts
3434 EHPadMBB->setMachineBlockAddressTaken();
3435 break;
3436 case Intrinsic::experimental_patchpoint_void:
3437 case Intrinsic::experimental_patchpoint:
3438 visitPatchpoint(I, EHPadBB);
3439 break;
3440 case Intrinsic::experimental_gc_statepoint:
3442 break;
3443 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3444 // but these intrinsics are special because they can be invoked, so we
3445 // manually lower it to a DAG node here.
3446 case Intrinsic::wasm_throw: {
3448 std::array<SDValue, 4> Ops = {
3449 getControlRoot(), // inchain for the terminator node
3450 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3452 getValue(I.getArgOperand(0)), // tag
3453 getValue(I.getArgOperand(1)) // thrown value
3454 };
3455 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3456 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3457 break;
3458 }
3459 case Intrinsic::wasm_rethrow: {
3460 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3461 std::array<SDValue, 2> Ops = {
3462 getControlRoot(), // inchain for the terminator node
3463 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3464 TLI.getPointerTy(DAG.getDataLayout()))};
3465 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3466 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3467 break;
3468 }
3469 }
3470 } else if (I.hasDeoptState()) {
3471 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3472 // Eventually we will support lowering the @llvm.experimental.deoptimize
3473 // intrinsic, and right now there are no plans to support other intrinsics
3474 // with deopt state.
3475 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3476 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3478 } else {
3479 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3480 }
3481
3482 // If the value of the invoke is used outside of its defining block, make it
3483 // available as a virtual register.
3484 // We already took care of the exported value for the statepoint instruction
3485 // during call to the LowerStatepoint.
3486 if (!isa<GCStatepointInst>(I)) {
3488 }
3489
3491 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3492 BranchProbability EHPadBBProb =
3493 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3495 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3496
3497 // Update successor info.
3498 addSuccessorWithProb(InvokeMBB, Return);
3499 for (auto &UnwindDest : UnwindDests) {
3500 UnwindDest.first->setIsEHPad();
3501 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3502 }
3503 InvokeMBB->normalizeSuccProbs();
3504
3505 // Drop into normal successor.
3506 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3507 DAG.getBasicBlock(Return)));
3508}
3509
3510void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3511 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3512
3513 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3514 // have to do anything here to lower funclet bundles.
3515 failForInvalidBundles(I, "callbrs",
3517
3518 assert(I.isInlineAsm() && "Only know how to handle inlineasm callbr");
3519 visitInlineAsm(I);
3521
3522 // Retrieve successors.
3523 SmallPtrSet<BasicBlock *, 8> Dests;
3524 Dests.insert(I.getDefaultDest());
3525 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3526
3527 // Update successor info.
3528 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3529 for (unsigned i = 0, e = I.getNumIndirectDests(); i < e; ++i) {
3530 BasicBlock *Dest = I.getIndirectDest(i);
3531 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
3532 Target->setIsInlineAsmBrIndirectTarget();
3533 // If we introduce a type of asm goto statement that is permitted to use an
3534 // indirect call instruction to jump to its labels, then we should add a
3535 // call to Target->setMachineBlockAddressTaken() here, to mark the target
3536 // block as requiring a BTI.
3537
3538 Target->setLabelMustBeEmitted();
3539 // Don't add duplicate machine successors.
3540 if (Dests.insert(Dest).second)
3541 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3542 }
3543 CallBrMBB->normalizeSuccProbs();
3544
3545 // Drop into default successor.
3546 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3547 MVT::Other, getControlRoot(),
3548 DAG.getBasicBlock(Return)));
3549}
3550
3551void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3552 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3553}
3554
3555void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3556 assert(FuncInfo.MBB->isEHPad() &&
3557 "Call to landingpad not in landing pad!");
3558
3559 // If there aren't registers to copy the values into (e.g., during SjLj
3560 // exceptions), then don't bother to create these DAG nodes.
3561 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3562 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3563 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3564 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3565 return;
3566
3567 // If landingpad's return type is token type, we don't create DAG nodes
3568 // for its exception pointer and selector value. The extraction of exception
3569 // pointer or selector value from token type landingpads is not currently
3570 // supported.
3571 if (LP.getType()->isTokenTy())
3572 return;
3573
3574 SmallVector<EVT, 2> ValueVTs;
3575 SDLoc dl = getCurSDLoc();
3576 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3577 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3578
3579 // Get the two live-in registers as SDValues. The physregs have already been
3580 // copied into virtual registers.
3581 SDValue Ops[2];
3582 if (FuncInfo.ExceptionPointerVirtReg) {
3583 Ops[0] = DAG.getZExtOrTrunc(
3584 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3585 FuncInfo.ExceptionPointerVirtReg,
3586 TLI.getPointerTy(DAG.getDataLayout())),
3587 dl, ValueVTs[0]);
3588 } else {
3589 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3590 }
3591 Ops[1] = DAG.getZExtOrTrunc(
3592 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3593 FuncInfo.ExceptionSelectorVirtReg,
3594 TLI.getPointerTy(DAG.getDataLayout())),
3595 dl, ValueVTs[1]);
3596
3597 // Merge into one.
3598 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3599 DAG.getVTList(ValueVTs), Ops);
3600 setValue(&LP, Res);
3601}
3602
3605 // Update JTCases.
3606 for (JumpTableBlock &JTB : SL->JTCases)
3607 if (JTB.first.HeaderBB == First)
3608 JTB.first.HeaderBB = Last;
3609
3610 // Update BitTestCases.
3611 for (BitTestBlock &BTB : SL->BitTestCases)
3612 if (BTB.Parent == First)
3613 BTB.Parent = Last;
3614}
3615
3616void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3617 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3618
3619 // Update machine-CFG edges with unique successors.
3621 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3622 BasicBlock *BB = I.getSuccessor(i);
3623 bool Inserted = Done.insert(BB).second;
3624 if (!Inserted)
3625 continue;
3626
3627 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3628 addSuccessorWithProb(IndirectBrMBB, Succ);
3629 }
3630 IndirectBrMBB->normalizeSuccProbs();
3631
3632 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
3633 MVT::Other, getControlRoot(),
3634 getValue(I.getAddress())));
3635}
3636
3637void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3638 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3639 DAG.getTarget().Options.NoTrapAfterNoreturn))
3640 return;
3641
3642 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3643}
3644
3645void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3646 SDNodeFlags Flags;
3647 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3648 Flags.copyFMF(*FPOp);
3649
3650 SDValue Op = getValue(I.getOperand(0));
3651 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3652 Op, Flags);
3653 setValue(&I, UnNodeValue);
3654}
3655
3656void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3657 SDNodeFlags Flags;
3658 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3659 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3660 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3661 }
3662 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3663 Flags.setExact(ExactOp->isExact());
3664 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3665 Flags.setDisjoint(DisjointOp->isDisjoint());
3666 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3667 Flags.copyFMF(*FPOp);
3668
3669 SDValue Op1 = getValue(I.getOperand(0));
3670 SDValue Op2 = getValue(I.getOperand(1));
3671 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3672 Op1, Op2, Flags);
3673 setValue(&I, BinNodeValue);
3674}
3675
3676void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3677 SDValue Op1 = getValue(I.getOperand(0));
3678 SDValue Op2 = getValue(I.getOperand(1));
3679
3680 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3681 Op1.getValueType(), DAG.getDataLayout());
3682
3683 // Coerce the shift amount to the right type if we can. This exposes the
3684 // truncate or zext to optimization early.
3685 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3687 "Unexpected shift type");
3688 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3689 }
3690
3691 bool nuw = false;
3692 bool nsw = false;
3693 bool exact = false;
3694
3695 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3696
3697 if (const OverflowingBinaryOperator *OFBinOp =
3699 nuw = OFBinOp->hasNoUnsignedWrap();
3700 nsw = OFBinOp->hasNoSignedWrap();
3701 }
3702 if (const PossiblyExactOperator *ExactOp =
3704 exact = ExactOp->isExact();
3705 }
3706 SDNodeFlags Flags;
3707 Flags.setExact(exact);
3708 Flags.setNoSignedWrap(nsw);
3709 Flags.setNoUnsignedWrap(nuw);
3710 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3711 Flags);
3712 setValue(&I, Res);
3713}
3714
3715void SelectionDAGBuilder::visitSDiv(const User &I) {
3716 SDValue Op1 = getValue(I.getOperand(0));
3717 SDValue Op2 = getValue(I.getOperand(1));
3718
3719 SDNodeFlags Flags;
3720 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3721 cast<PossiblyExactOperator>(&I)->isExact());
3722 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3723 Op2, Flags));
3724}
3725
3726void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3727 ICmpInst::Predicate predicate = I.getPredicate();
3728 SDValue Op1 = getValue(I.getOperand(0));
3729 SDValue Op2 = getValue(I.getOperand(1));
3730 ISD::CondCode Opcode = getICmpCondCode(predicate);
3731
3732 auto &TLI = DAG.getTargetLoweringInfo();
3733 EVT MemVT =
3734 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3735
3736 // If a pointer's DAG type is larger than its memory type then the DAG values
3737 // are zero-extended. This breaks signed comparisons so truncate back to the
3738 // underlying type before doing the compare.
3739 if (Op1.getValueType() != MemVT) {
3740 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3741 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3742 }
3743
3744 SDNodeFlags Flags;
3745 Flags.setSameSign(I.hasSameSign());
3746 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3747
3748 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3749 I.getType());
3750 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
3751}
3752
3753void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3754 FCmpInst::Predicate predicate = I.getPredicate();
3755 SDValue Op1 = getValue(I.getOperand(0));
3756 SDValue Op2 = getValue(I.getOperand(1));
3757
3758 ISD::CondCode Condition = getFCmpCondCode(predicate);
3759 auto *FPMO = cast<FPMathOperator>(&I);
3760 if (FPMO->hasNoNaNs() || TM.Options.NoNaNsFPMath)
3761 Condition = getFCmpCodeWithoutNaN(Condition);
3762
3763 SDNodeFlags Flags;
3764 Flags.copyFMF(*FPMO);
3765 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3766
3767 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3768 I.getType());
3769 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
3770}
3771
3772// Check if the condition of the select has one use or two users that are both
3773// selects with the same condition.
3774static bool hasOnlySelectUsers(const Value *Cond) {
3775 return llvm::all_of(Cond->users(), [](const Value *V) {
3776 return isa<SelectInst>(V);
3777 });
3778}
3779
3780void SelectionDAGBuilder::visitSelect(const User &I) {
3781 SmallVector<EVT, 4> ValueVTs;
3782 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3783 ValueVTs);
3784 unsigned NumValues = ValueVTs.size();
3785 if (NumValues == 0) return;
3786
3787 SmallVector<SDValue, 4> Values(NumValues);
3788 SDValue Cond = getValue(I.getOperand(0));
3789 SDValue LHSVal = getValue(I.getOperand(1));
3790 SDValue RHSVal = getValue(I.getOperand(2));
3791 SmallVector<SDValue, 1> BaseOps(1, Cond);
3793 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3794
3795 bool IsUnaryAbs = false;
3796 bool Negate = false;
3797
3798 SDNodeFlags Flags;
3799 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3800 Flags.copyFMF(*FPOp);
3801
3802 Flags.setUnpredictable(
3803 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3804
3805 // Min/max matching is only viable if all output VTs are the same.
3806 if (all_equal(ValueVTs)) {
3807 EVT VT = ValueVTs[0];
3808 LLVMContext &Ctx = *DAG.getContext();
3809 auto &TLI = DAG.getTargetLoweringInfo();
3810
3811 // We care about the legality of the operation after it has been type
3812 // legalized.
3813 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3814 VT = TLI.getTypeToTransformTo(Ctx, VT);
3815
3816 // If the vselect is legal, assume we want to leave this as a vector setcc +
3817 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3818 // min/max is legal on the scalar type.
3819 bool UseScalarMinMax = VT.isVector() &&
3821
3822 // ValueTracking's select pattern matching does not account for -0.0,
3823 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3824 // -0.0 is less than +0.0.
3825 const Value *LHS, *RHS;
3826 auto SPR = matchSelectPattern(&I, LHS, RHS);
3828 switch (SPR.Flavor) {
3829 case SPF_UMAX: Opc = ISD::UMAX; break;
3830 case SPF_UMIN: Opc = ISD::UMIN; break;
3831 case SPF_SMAX: Opc = ISD::SMAX; break;
3832 case SPF_SMIN: Opc = ISD::SMIN; break;
3833 case SPF_FMINNUM:
3834 switch (SPR.NaNBehavior) {
3835 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3836 case SPNB_RETURNS_NAN: break;
3837 case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break;
3838 case SPNB_RETURNS_ANY:
3839 if (TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT) ||
3840 (UseScalarMinMax &&
3841 TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT.getScalarType())))
3842 Opc = ISD::FMINNUM;
3843 break;
3844 }
3845 break;
3846 case SPF_FMAXNUM:
3847 switch (SPR.NaNBehavior) {
3848 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3849 case SPNB_RETURNS_NAN: break;
3850 case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break;
3851 case SPNB_RETURNS_ANY:
3852 if (TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT) ||
3853 (UseScalarMinMax &&
3854 TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT.getScalarType())))
3855 Opc = ISD::FMAXNUM;
3856 break;
3857 }
3858 break;
3859 case SPF_NABS:
3860 Negate = true;
3861 [[fallthrough]];
3862 case SPF_ABS:
3863 IsUnaryAbs = true;
3864 Opc = ISD::ABS;
3865 break;
3866 default: break;
3867 }
3868
3869 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3870 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3871 (UseScalarMinMax &&
3873 // If the underlying comparison instruction is used by any other
3874 // instruction, the consumed instructions won't be destroyed, so it is
3875 // not profitable to convert to a min/max.
3877 OpCode = Opc;
3878 LHSVal = getValue(LHS);
3879 RHSVal = getValue(RHS);
3880 BaseOps.clear();
3881 }
3882
3883 if (IsUnaryAbs) {
3884 OpCode = Opc;
3885 LHSVal = getValue(LHS);
3886 BaseOps.clear();
3887 }
3888 }
3889
3890 if (IsUnaryAbs) {
3891 for (unsigned i = 0; i != NumValues; ++i) {
3892 SDLoc dl = getCurSDLoc();
3893 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3894 Values[i] =
3895 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3896 if (Negate)
3897 Values[i] = DAG.getNegative(Values[i], dl, VT);
3898 }
3899 } else {
3900 for (unsigned i = 0; i != NumValues; ++i) {
3901 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3902 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3903 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3904 Values[i] = DAG.getNode(
3905 OpCode, getCurSDLoc(),
3906 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3907 }
3908 }
3909
3911 DAG.getVTList(ValueVTs), Values));
3912}
3913
3914void SelectionDAGBuilder::visitTrunc(const User &I) {
3915 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3916 SDValue N = getValue(I.getOperand(0));
3917 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3918 I.getType());
3919 SDNodeFlags Flags;
3920 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3921 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
3922 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
3923 }
3924
3925 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
3926}
3927
3928void SelectionDAGBuilder::visitZExt(const User &I) {
3929 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3930 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3931 SDValue N = getValue(I.getOperand(0));
3932 auto &TLI = DAG.getTargetLoweringInfo();
3933 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3934
3935 SDNodeFlags Flags;
3936 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3937 Flags.setNonNeg(PNI->hasNonNeg());
3938
3939 // Eagerly use nonneg information to canonicalize towards sign_extend if
3940 // that is the target's preference.
3941 // TODO: Let the target do this later.
3942 if (Flags.hasNonNeg() &&
3943 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3944 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3945 return;
3946 }
3947
3948 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3949}
3950
3951void SelectionDAGBuilder::visitSExt(const User &I) {
3952 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3953 // SExt also can't be a cast to bool for same reason. So, nothing much to do
3954 SDValue N = getValue(I.getOperand(0));
3955 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3956 I.getType());
3957 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3958}
3959
3960void SelectionDAGBuilder::visitFPTrunc(const User &I) {
3961 // FPTrunc is never a no-op cast, no need to check
3962 SDValue N = getValue(I.getOperand(0));
3963 SDLoc dl = getCurSDLoc();
3964 SDNodeFlags Flags;
3965 if (auto *TruncInst = dyn_cast<FPMathOperator>(&I))
3966 Flags.copyFMF(*TruncInst);
3967 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3968 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3969 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
3970 DAG.getTargetConstant(
3971 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
3972 Flags));
3973}
3974
3975void SelectionDAGBuilder::visitFPExt(const User &I) {
3976 // FPExt is never a no-op cast, no need to check
3977 SDValue N = getValue(I.getOperand(0));
3978 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3979 I.getType());
3980 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
3981}
3982
3983void SelectionDAGBuilder::visitFPToUI(const User &I) {
3984 // FPToUI is never a no-op cast, no need to check
3985 SDValue N = getValue(I.getOperand(0));
3986 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3987 I.getType());
3988 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
3989}
3990
3991void SelectionDAGBuilder::visitFPToSI(const User &I) {
3992 // FPToSI is never a no-op cast, no need to check
3993 SDValue N = getValue(I.getOperand(0));
3994 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3995 I.getType());
3996 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
3997}
3998
3999void SelectionDAGBuilder::visitUIToFP(const User &I) {
4000 // UIToFP is never a no-op cast, no need to check
4001 SDValue N = getValue(I.getOperand(0));
4002 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4003 I.getType());
4004 SDNodeFlags Flags;
4005 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
4006 Flags.setNonNeg(PNI->hasNonNeg());
4007
4008 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4009}
4010
4011void SelectionDAGBuilder::visitSIToFP(const User &I) {
4012 // SIToFP is never a no-op cast, no need to check
4013 SDValue N = getValue(I.getOperand(0));
4014 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4015 I.getType());
4016 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
4017}
4018
4019void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
4020 SDValue N = getValue(I.getOperand(0));
4021 // By definition the type of the ptrtoaddr must be equal to the address type.
4022 const auto &TLI = DAG.getTargetLoweringInfo();
4023 EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4024 // The address width must be smaller or equal to the pointer representation
4025 // width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
4026 N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
4027 setValue(&I, N);
4028}
4029
4030void SelectionDAGBuilder::visitPtrToInt(const User &I) {
4031 // What to do depends on the size of the integer and the size of the pointer.
4032 // We can either truncate, zero extend, or no-op, accordingly.
4033 SDValue N = getValue(I.getOperand(0));
4034 auto &TLI = DAG.getTargetLoweringInfo();
4035 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4036 I.getType());
4037 EVT PtrMemVT =
4038 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
4039 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4040 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
4041 setValue(&I, N);
4042}
4043
4044void SelectionDAGBuilder::visitIntToPtr(const User &I) {
4045 // What to do depends on the size of the integer and the size of the pointer.
4046 // We can either truncate, zero extend, or no-op, accordingly.
4047 SDValue N = getValue(I.getOperand(0));
4048 auto &TLI = DAG.getTargetLoweringInfo();
4049 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4050 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4051 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4052 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4053 setValue(&I, N);
4054}
4055
4056void SelectionDAGBuilder::visitBitCast(const User &I) {
4057 SDValue N = getValue(I.getOperand(0));
4058 SDLoc dl = getCurSDLoc();
4059 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4060 I.getType());
4061
4062 // BitCast assures us that source and destination are the same size so this is
4063 // either a BITCAST or a no-op.
4064 if (DestVT != N.getValueType())
4065 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
4066 DestVT, N)); // convert types.
4067 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4068 // might fold any kind of constant expression to an integer constant and that
4069 // is not what we are looking for. Only recognize a bitcast of a genuine
4070 // constant integer as an opaque constant.
4071 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4072 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4073 /*isOpaque*/true));
4074 else
4075 setValue(&I, N); // noop cast.
4076}
4077
4078void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4079 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4080 const Value *SV = I.getOperand(0);
4081 SDValue N = getValue(SV);
4082 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4083
4084 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4085 unsigned DestAS = I.getType()->getPointerAddressSpace();
4086
4087 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4088 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4089
4090 setValue(&I, N);
4091}
4092
4093void SelectionDAGBuilder::visitInsertElement(const User &I) {
4094 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4095 SDValue InVec = getValue(I.getOperand(0));
4096 SDValue InVal = getValue(I.getOperand(1));
4097 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4098 TLI.getVectorIdxTy(DAG.getDataLayout()));
4100 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4101 InVec, InVal, InIdx));
4102}
4103
4104void SelectionDAGBuilder::visitExtractElement(const User &I) {
4105 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4106 SDValue InVec = getValue(I.getOperand(0));
4107 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4108 TLI.getVectorIdxTy(DAG.getDataLayout()));
4110 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4111 InVec, InIdx));
4112}
4113
4114void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4115 SDValue Src1 = getValue(I.getOperand(0));
4116 SDValue Src2 = getValue(I.getOperand(1));
4117 ArrayRef<int> Mask;
4118 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4119 Mask = SVI->getShuffleMask();
4120 else
4121 Mask = cast<ConstantExpr>(I).getShuffleMask();
4122 SDLoc DL = getCurSDLoc();
4123 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4124 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4125 EVT SrcVT = Src1.getValueType();
4126
4127 if (all_of(Mask, [](int Elem) { return Elem == 0; }) &&
4128 VT.isScalableVector()) {
4129 // Canonical splat form of first element of first input vector.
4130 SDValue FirstElt =
4131 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4132 DAG.getVectorIdxConstant(0, DL));
4133 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4134 return;
4135 }
4136
4137 // For now, we only handle splats for scalable vectors.
4138 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4139 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4140 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4141
4142 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4143 unsigned MaskNumElts = Mask.size();
4144
4145 if (SrcNumElts == MaskNumElts) {
4146 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4147 return;
4148 }
4149
4150 // Normalize the shuffle vector since mask and vector length don't match.
4151 if (SrcNumElts < MaskNumElts) {
4152 // Mask is longer than the source vectors. We can use concatenate vector to
4153 // make the mask and vectors lengths match.
4154
4155 if (MaskNumElts % SrcNumElts == 0) {
4156 // Mask length is a multiple of the source vector length.
4157 // Check if the shuffle is some kind of concatenation of the input
4158 // vectors.
4159 unsigned NumConcat = MaskNumElts / SrcNumElts;
4160 bool IsConcat = true;
4161 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4162 for (unsigned i = 0; i != MaskNumElts; ++i) {
4163 int Idx = Mask[i];
4164 if (Idx < 0)
4165 continue;
4166 // Ensure the indices in each SrcVT sized piece are sequential and that
4167 // the same source is used for the whole piece.
4168 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4169 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4170 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4171 IsConcat = false;
4172 break;
4173 }
4174 // Remember which source this index came from.
4175 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4176 }
4177
4178 // The shuffle is concatenating multiple vectors together. Just emit
4179 // a CONCAT_VECTORS operation.
4180 if (IsConcat) {
4181 SmallVector<SDValue, 8> ConcatOps;
4182 for (auto Src : ConcatSrcs) {
4183 if (Src < 0)
4184 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4185 else if (Src == 0)
4186 ConcatOps.push_back(Src1);
4187 else
4188 ConcatOps.push_back(Src2);
4189 }
4190 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4191 return;
4192 }
4193 }
4194
4195 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4196 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4197 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4198 PaddedMaskNumElts);
4199
4200 // Pad both vectors with undefs to make them the same length as the mask.
4201 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4202
4203 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4204 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4205 MOps1[0] = Src1;
4206 MOps2[0] = Src2;
4207
4208 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4209 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4210
4211 // Readjust mask for new input vector length.
4212 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4213 for (unsigned i = 0; i != MaskNumElts; ++i) {
4214 int Idx = Mask[i];
4215 if (Idx >= (int)SrcNumElts)
4216 Idx -= SrcNumElts - PaddedMaskNumElts;
4217 MappedOps[i] = Idx;
4218 }
4219
4220 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4221
4222 // If the concatenated vector was padded, extract a subvector with the
4223 // correct number of elements.
4224 if (MaskNumElts != PaddedMaskNumElts)
4225 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4226 DAG.getVectorIdxConstant(0, DL));
4227
4228 setValue(&I, Result);
4229 return;
4230 }
4231
4232 assert(SrcNumElts > MaskNumElts);
4233
4234 // Analyze the access pattern of the vector to see if we can extract
4235 // two subvectors and do the shuffle.
4236 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4237 bool CanExtract = true;
4238 for (int Idx : Mask) {
4239 unsigned Input = 0;
4240 if (Idx < 0)
4241 continue;
4242
4243 if (Idx >= (int)SrcNumElts) {
4244 Input = 1;
4245 Idx -= SrcNumElts;
4246 }
4247
4248 // If all the indices come from the same MaskNumElts sized portion of
4249 // the sources we can use extract. Also make sure the extract wouldn't
4250 // extract past the end of the source.
4251 int NewStartIdx = alignDown(Idx, MaskNumElts);
4252 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4253 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4254 CanExtract = false;
4255 // Make sure we always update StartIdx as we use it to track if all
4256 // elements are undef.
4257 StartIdx[Input] = NewStartIdx;
4258 }
4259
4260 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4261 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4262 return;
4263 }
4264 if (CanExtract) {
4265 // Extract appropriate subvector and generate a vector shuffle
4266 for (unsigned Input = 0; Input < 2; ++Input) {
4267 SDValue &Src = Input == 0 ? Src1 : Src2;
4268 if (StartIdx[Input] < 0)
4269 Src = DAG.getUNDEF(VT);
4270 else {
4271 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4272 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4273 }
4274 }
4275
4276 // Calculate new mask.
4277 SmallVector<int, 8> MappedOps(Mask);
4278 for (int &Idx : MappedOps) {
4279 if (Idx >= (int)SrcNumElts)
4280 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4281 else if (Idx >= 0)
4282 Idx -= StartIdx[0];
4283 }
4284
4285 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4286 return;
4287 }
4288
4289 // We can't use either concat vectors or extract subvectors so fall back to
4290 // replacing the shuffle with extract and build vector.
4291 // to insert and build vector.
4292 EVT EltVT = VT.getVectorElementType();
4294 for (int Idx : Mask) {
4295 SDValue Res;
4296
4297 if (Idx < 0) {
4298 Res = DAG.getUNDEF(EltVT);
4299 } else {
4300 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4301 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4302
4303 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4304 DAG.getVectorIdxConstant(Idx, DL));
4305 }
4306
4307 Ops.push_back(Res);
4308 }
4309
4310 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4311}
4312
4313void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4314 ArrayRef<unsigned> Indices = I.getIndices();
4315 const Value *Op0 = I.getOperand(0);
4316 const Value *Op1 = I.getOperand(1);
4317 Type *AggTy = I.getType();
4318 Type *ValTy = Op1->getType();
4319 bool IntoUndef = isa<UndefValue>(Op0);
4320 bool FromUndef = isa<UndefValue>(Op1);
4321
4322 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4323
4324 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4325 SmallVector<EVT, 4> AggValueVTs;
4326 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4327 SmallVector<EVT, 4> ValValueVTs;
4328 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4329
4330 unsigned NumAggValues = AggValueVTs.size();
4331 unsigned NumValValues = ValValueVTs.size();
4332 SmallVector<SDValue, 4> Values(NumAggValues);
4333
4334 // Ignore an insertvalue that produces an empty object
4335 if (!NumAggValues) {
4336 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4337 return;
4338 }
4339
4340 SDValue Agg = getValue(Op0);
4341 unsigned i = 0;
4342 // Copy the beginning value(s) from the original aggregate.
4343 for (; i != LinearIndex; ++i)
4344 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4345 SDValue(Agg.getNode(), Agg.getResNo() + i);
4346 // Copy values from the inserted value(s).
4347 if (NumValValues) {
4348 SDValue Val = getValue(Op1);
4349 for (; i != LinearIndex + NumValValues; ++i)
4350 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4351 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4352 }
4353 // Copy remaining value(s) from the original aggregate.
4354 for (; i != NumAggValues; ++i)
4355 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4356 SDValue(Agg.getNode(), Agg.getResNo() + i);
4357
4359 DAG.getVTList(AggValueVTs), Values));
4360}
4361
4362void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4363 ArrayRef<unsigned> Indices = I.getIndices();
4364 const Value *Op0 = I.getOperand(0);
4365 Type *AggTy = Op0->getType();
4366 Type *ValTy = I.getType();
4367 bool OutOfUndef = isa<UndefValue>(Op0);
4368
4369 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4370
4371 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4372 SmallVector<EVT, 4> ValValueVTs;
4373 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4374
4375 unsigned NumValValues = ValValueVTs.size();
4376
4377 // Ignore a extractvalue that produces an empty object
4378 if (!NumValValues) {
4379 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4380 return;
4381 }
4382
4383 SmallVector<SDValue, 4> Values(NumValValues);
4384
4385 SDValue Agg = getValue(Op0);
4386 // Copy out the selected value(s).
4387 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4388 Values[i - LinearIndex] =
4389 OutOfUndef ?
4390 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4391 SDValue(Agg.getNode(), Agg.getResNo() + i);
4392
4394 DAG.getVTList(ValValueVTs), Values));
4395}
4396
4397void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4398 Value *Op0 = I.getOperand(0);
4399 // Note that the pointer operand may be a vector of pointers. Take the scalar
4400 // element which holds a pointer.
4401 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4402 SDValue N = getValue(Op0);
4403 SDLoc dl = getCurSDLoc();
4404 auto &TLI = DAG.getTargetLoweringInfo();
4405 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4406
4407 // For a vector GEP, keep the prefix scalar as long as possible, then
4408 // convert any scalars encountered after the first vector operand to vectors.
4409 bool IsVectorGEP = I.getType()->isVectorTy();
4410 ElementCount VectorElementCount =
4411 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4413
4415 GTI != E; ++GTI) {
4416 const Value *Idx = GTI.getOperand();
4417 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4418 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4419 if (Field) {
4420 // N = N + Offset
4421 uint64_t Offset =
4422 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4423
4424 // In an inbounds GEP with an offset that is nonnegative even when
4425 // interpreted as signed, assume there is no unsigned overflow.
4426 SDNodeFlags Flags;
4427 if (NW.hasNoUnsignedWrap() ||
4428 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4430 Flags.setInBounds(NW.isInBounds());
4431
4432 N = DAG.getMemBasePlusOffset(
4433 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4434 }
4435 } else {
4436 // IdxSize is the width of the arithmetic according to IR semantics.
4437 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4438 // (and fix up the result later).
4439 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4440 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4441 TypeSize ElementSize =
4442 GTI.getSequentialElementStride(DAG.getDataLayout());
4443 // We intentionally mask away the high bits here; ElementSize may not
4444 // fit in IdxTy.
4445 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4446 /*isSigned=*/false, /*implicitTrunc=*/true);
4447 bool ElementScalable = ElementSize.isScalable();
4448
4449 // If this is a scalar constant or a splat vector of constants,
4450 // handle it quickly.
4451 const auto *C = dyn_cast<Constant>(Idx);
4452 if (C && isa<VectorType>(C->getType()))
4453 C = C->getSplatValue();
4454
4455 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4456 if (CI && CI->isZero())
4457 continue;
4458 if (CI && !ElementScalable) {
4459 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4460 LLVMContext &Context = *DAG.getContext();
4461 SDValue OffsVal;
4462 if (N.getValueType().isVector())
4463 OffsVal = DAG.getConstant(
4464 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4465 else
4466 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4467
4468 // In an inbounds GEP with an offset that is nonnegative even when
4469 // interpreted as signed, assume there is no unsigned overflow.
4470 SDNodeFlags Flags;
4471 if (NW.hasNoUnsignedWrap() ||
4472 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4473 Flags.setNoUnsignedWrap(true);
4474 Flags.setInBounds(NW.isInBounds());
4475
4476 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4477
4478 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4479 continue;
4480 }
4481
4482 // N = N + Idx * ElementMul;
4483 SDValue IdxN = getValue(Idx);
4484
4485 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4486 if (N.getValueType().isVector()) {
4487 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4488 VectorElementCount);
4489 IdxN = DAG.getSplat(VT, dl, IdxN);
4490 } else {
4491 EVT VT =
4492 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4493 N = DAG.getSplat(VT, dl, N);
4494 }
4495 }
4496
4497 // If the index is smaller or larger than intptr_t, truncate or extend
4498 // it.
4499 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4500
4501 SDNodeFlags ScaleFlags;
4502 // The multiplication of an index by the type size does not wrap the
4503 // pointer index type in a signed sense (mul nsw).
4505
4506 // The multiplication of an index by the type size does not wrap the
4507 // pointer index type in an unsigned sense (mul nuw).
4508 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4509
4510 if (ElementScalable) {
4511 EVT VScaleTy = N.getValueType().getScalarType();
4512 SDValue VScale = DAG.getNode(
4513 ISD::VSCALE, dl, VScaleTy,
4514 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4515 if (N.getValueType().isVector())
4516 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4517 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4518 ScaleFlags);
4519 } else {
4520 // If this is a multiply by a power of two, turn it into a shl
4521 // immediately. This is a very common case.
4522 if (ElementMul != 1) {
4523 if (ElementMul.isPowerOf2()) {
4524 unsigned Amt = ElementMul.logBase2();
4525 IdxN = DAG.getNode(
4526 ISD::SHL, dl, N.getValueType(), IdxN,
4527 DAG.getShiftAmountConstant(Amt, N.getValueType(), dl),
4528 ScaleFlags);
4529 } else {
4530 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4531 IdxN.getValueType());
4532 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4533 ScaleFlags);
4534 }
4535 }
4536 }
4537
4538 // The successive addition of the current address, truncated to the
4539 // pointer index type and interpreted as an unsigned number, and each
4540 // offset, also interpreted as an unsigned number, does not wrap the
4541 // pointer index type (add nuw).
4542 SDNodeFlags AddFlags;
4543 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4544 AddFlags.setInBounds(NW.isInBounds());
4545
4546 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4547 }
4548 }
4549
4550 if (IsVectorGEP && !N.getValueType().isVector()) {
4551 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4552 N = DAG.getSplat(VT, dl, N);
4553 }
4554
4555 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4556 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4557 if (IsVectorGEP) {
4558 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4559 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4560 }
4561
4562 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4563 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4564
4565 setValue(&I, N);
4566}
4567
4568void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4569 // If this is a fixed sized alloca in the entry block of the function,
4570 // allocate it statically on the stack.
4571 if (FuncInfo.StaticAllocaMap.count(&I))
4572 return; // getValue will auto-populate this.
4573
4574 SDLoc dl = getCurSDLoc();
4575 Type *Ty = I.getAllocatedType();
4576 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4577 auto &DL = DAG.getDataLayout();
4578 TypeSize TySize = DL.getTypeAllocSize(Ty);
4579 MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign());
4580
4581 SDValue AllocSize = getValue(I.getArraySize());
4582
4583 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4584 if (AllocSize.getValueType() != IntPtr)
4585 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4586
4587 if (TySize.isScalable())
4588 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4589 DAG.getVScale(dl, IntPtr,
4590 APInt(IntPtr.getScalarSizeInBits(),
4591 TySize.getKnownMinValue())));
4592 else {
4593 SDValue TySizeValue =
4594 DAG.getConstant(TySize.getFixedValue(), dl, MVT::getIntegerVT(64));
4595 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4596 DAG.getZExtOrTrunc(TySizeValue, dl, IntPtr));
4597 }
4598
4599 // Handle alignment. If the requested alignment is less than or equal to
4600 // the stack alignment, ignore it. If the size is greater than or equal to
4601 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4602 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4603 if (*Alignment <= StackAlign)
4604 Alignment = std::nullopt;
4605
4606 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4607 // Round the size of the allocation up to the stack alignment size
4608 // by add SA-1 to the size. This doesn't overflow because we're computing
4609 // an address inside an alloca.
4610 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4611 DAG.getConstant(StackAlignMask, dl, IntPtr),
4613
4614 // Mask out the low bits for alignment purposes.
4615 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4616 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4617
4618 SDValue Ops[] = {
4619 getRoot(), AllocSize,
4620 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4621 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4622 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4623 setValue(&I, DSA);
4624 DAG.setRoot(DSA.getValue(1));
4625
4626 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4627}
4628
4629static const MDNode *getRangeMetadata(const Instruction &I) {
4630 return I.getMetadata(LLVMContext::MD_range);
4631}
4632
4633static std::optional<ConstantRange> getRange(const Instruction &I) {
4634 if (const auto *CB = dyn_cast<CallBase>(&I))
4635 if (std::optional<ConstantRange> CR = CB->getRange())
4636 return CR;
4637 if (const MDNode *Range = getRangeMetadata(I))
4639 return std::nullopt;
4640}
4641
4642void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4643 if (I.isAtomic())
4644 return visitAtomicLoad(I);
4645
4646 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4647 const Value *SV = I.getOperand(0);
4648 if (TLI.supportSwiftError()) {
4649 // Swifterror values can come from either a function parameter with
4650 // swifterror attribute or an alloca with swifterror attribute.
4651 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4652 if (Arg->hasSwiftErrorAttr())
4653 return visitLoadFromSwiftError(I);
4654 }
4655
4656 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4657 if (Alloca->isSwiftError())
4658 return visitLoadFromSwiftError(I);
4659 }
4660 }
4661
4662 SDValue Ptr = getValue(SV);
4663
4664 Type *Ty = I.getType();
4665 SmallVector<EVT, 4> ValueVTs, MemVTs;
4667 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4668 unsigned NumValues = ValueVTs.size();
4669 if (NumValues == 0)
4670 return;
4671
4672 Align Alignment = I.getAlign();
4673 AAMDNodes AAInfo = I.getAAMetadata();
4674 const MDNode *Ranges = getRangeMetadata(I);
4675 bool isVolatile = I.isVolatile();
4676 MachineMemOperand::Flags MMOFlags =
4677 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4678
4679 SDValue Root;
4680 bool ConstantMemory = false;
4681 if (isVolatile)
4682 // Serialize volatile loads with other side effects.
4683 Root = getRoot();
4684 else if (NumValues > MaxParallelChains)
4685 Root = getMemoryRoot();
4686 else if (BatchAA &&
4687 BatchAA->pointsToConstantMemory(MemoryLocation(
4688 SV,
4689 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4690 AAInfo))) {
4691 // Do not serialize (non-volatile) loads of constant memory with anything.
4692 Root = DAG.getEntryNode();
4693 ConstantMemory = true;
4695 } else {
4696 // Do not serialize non-volatile loads against each other.
4697 Root = DAG.getRoot();
4698 }
4699
4700 SDLoc dl = getCurSDLoc();
4701
4702 if (isVolatile)
4703 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4704
4705 SmallVector<SDValue, 4> Values(NumValues);
4706 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4707
4708 unsigned ChainI = 0;
4709 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4710 // Serializing loads here may result in excessive register pressure, and
4711 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4712 // could recover a bit by hoisting nodes upward in the chain by recognizing
4713 // they are side-effect free or do not alias. The optimizer should really
4714 // avoid this case by converting large object/array copies to llvm.memcpy
4715 // (MaxParallelChains should always remain as failsafe).
4716 if (ChainI == MaxParallelChains) {
4717 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4718 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4719 ArrayRef(Chains.data(), ChainI));
4720 Root = Chain;
4721 ChainI = 0;
4722 }
4723
4724 // TODO: MachinePointerInfo only supports a fixed length offset.
4725 MachinePointerInfo PtrInfo =
4726 !Offsets[i].isScalable() || Offsets[i].isZero()
4727 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4728 : MachinePointerInfo();
4729
4730 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4731 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4732 MMOFlags, AAInfo, Ranges);
4733 Chains[ChainI] = L.getValue(1);
4734
4735 if (MemVTs[i] != ValueVTs[i])
4736 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4737
4738 Values[i] = L;
4739 }
4740
4741 if (!ConstantMemory) {
4742 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4743 ArrayRef(Chains.data(), ChainI));
4744 if (isVolatile)
4745 DAG.setRoot(Chain);
4746 else
4747 PendingLoads.push_back(Chain);
4748 }
4749
4750 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4751 DAG.getVTList(ValueVTs), Values));
4752}
4753
4754void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4755 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4756 "call visitStoreToSwiftError when backend supports swifterror");
4757
4758 SmallVector<EVT, 4> ValueVTs;
4759 SmallVector<uint64_t, 4> Offsets;
4760 const Value *SrcV = I.getOperand(0);
4761 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4762 SrcV->getType(), ValueVTs, &Offsets, 0);
4763 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4764 "expect a single EVT for swifterror");
4765
4766 SDValue Src = getValue(SrcV);
4767 // Create a virtual register, then update the virtual register.
4768 Register VReg =
4769 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4770 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4771 // Chain can be getRoot or getControlRoot.
4772 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4773 SDValue(Src.getNode(), Src.getResNo()));
4774 DAG.setRoot(CopyNode);
4775}
4776
4777void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4778 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4779 "call visitLoadFromSwiftError when backend supports swifterror");
4780
4781 assert(!I.isVolatile() &&
4782 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4783 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4784 "Support volatile, non temporal, invariant for load_from_swift_error");
4785
4786 const Value *SV = I.getOperand(0);
4787 Type *Ty = I.getType();
4788 assert(
4789 (!BatchAA ||
4790 !BatchAA->pointsToConstantMemory(MemoryLocation(
4791 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4792 I.getAAMetadata()))) &&
4793 "load_from_swift_error should not be constant memory");
4794
4795 SmallVector<EVT, 4> ValueVTs;
4796 SmallVector<uint64_t, 4> Offsets;
4797 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4798 ValueVTs, &Offsets, 0);
4799 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4800 "expect a single EVT for swifterror");
4801
4802 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4803 SDValue L = DAG.getCopyFromReg(
4804 getRoot(), getCurSDLoc(),
4805 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4806
4807 setValue(&I, L);
4808}
4809
4810void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4811 if (I.isAtomic())
4812 return visitAtomicStore(I);
4813
4814 const Value *SrcV = I.getOperand(0);
4815 const Value *PtrV = I.getOperand(1);
4816
4817 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4818 if (TLI.supportSwiftError()) {
4819 // Swifterror values can come from either a function parameter with
4820 // swifterror attribute or an alloca with swifterror attribute.
4821 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4822 if (Arg->hasSwiftErrorAttr())
4823 return visitStoreToSwiftError(I);
4824 }
4825
4826 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4827 if (Alloca->isSwiftError())
4828 return visitStoreToSwiftError(I);
4829 }
4830 }
4831
4832 SmallVector<EVT, 4> ValueVTs, MemVTs;
4834 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4835 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4836 unsigned NumValues = ValueVTs.size();
4837 if (NumValues == 0)
4838 return;
4839
4840 // Get the lowered operands. Note that we do this after
4841 // checking if NumResults is zero, because with zero results
4842 // the operands won't have values in the map.
4843 SDValue Src = getValue(SrcV);
4844 SDValue Ptr = getValue(PtrV);
4845
4846 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4847 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4848 SDLoc dl = getCurSDLoc();
4849 Align Alignment = I.getAlign();
4850 AAMDNodes AAInfo = I.getAAMetadata();
4851
4852 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4853
4854 unsigned ChainI = 0;
4855 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4856 // See visitLoad comments.
4857 if (ChainI == MaxParallelChains) {
4858 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4859 ArrayRef(Chains.data(), ChainI));
4860 Root = Chain;
4861 ChainI = 0;
4862 }
4863
4864 // TODO: MachinePointerInfo only supports a fixed length offset.
4865 MachinePointerInfo PtrInfo =
4866 !Offsets[i].isScalable() || Offsets[i].isZero()
4867 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4868 : MachinePointerInfo();
4869
4870 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4871 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4872 if (MemVTs[i] != ValueVTs[i])
4873 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4874 SDValue St =
4875 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4876 Chains[ChainI] = St;
4877 }
4878
4879 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4880 ArrayRef(Chains.data(), ChainI));
4881 setValue(&I, StoreNode);
4882 DAG.setRoot(StoreNode);
4883}
4884
4885void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4886 bool IsCompressing) {
4887 SDLoc sdl = getCurSDLoc();
4888
4889 Value *Src0Operand = I.getArgOperand(0);
4890 Value *PtrOperand = I.getArgOperand(1);
4891 Value *MaskOperand = I.getArgOperand(2);
4892 Align Alignment = I.getParamAlign(1).valueOrOne();
4893
4894 SDValue Ptr = getValue(PtrOperand);
4895 SDValue Src0 = getValue(Src0Operand);
4896 SDValue Mask = getValue(MaskOperand);
4897 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4898
4899 EVT VT = Src0.getValueType();
4900
4901 auto MMOFlags = MachineMemOperand::MOStore;
4902 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4904
4905 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4906 MachinePointerInfo(PtrOperand), MMOFlags,
4907 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4908
4909 const auto &TLI = DAG.getTargetLoweringInfo();
4910 const auto &TTI =
4911 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
4912 SDValue StoreNode =
4913 !IsCompressing && TTI.hasConditionalLoadStoreForType(
4914 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4915 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4916 Mask)
4917 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4918 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4919 IsCompressing);
4920 DAG.setRoot(StoreNode);
4921 setValue(&I, StoreNode);
4922}
4923
4924// Get a uniform base for the Gather/Scatter intrinsic.
4925// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4926// We try to represent it as a base pointer + vector of indices.
4927// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4928// The first operand of the GEP may be a single pointer or a vector of pointers
4929// Example:
4930// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4931// or
4932// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4933// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4934//
4935// When the first GEP operand is a single pointer - it is the uniform base we
4936// are looking for. If first operand of the GEP is a splat vector - we
4937// extract the splat value and use it as a uniform base.
4938// In all other cases the function returns 'false'.
4939static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4940 SDValue &Scale, SelectionDAGBuilder *SDB,
4941 const BasicBlock *CurBB, uint64_t ElemSize) {
4942 SelectionDAG& DAG = SDB->DAG;
4943 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4944 const DataLayout &DL = DAG.getDataLayout();
4945
4946 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4947
4948 // Handle splat constant pointer.
4949 if (auto *C = dyn_cast<Constant>(Ptr)) {
4950 C = C->getSplatValue();
4951 if (!C)
4952 return false;
4953
4954 Base = SDB->getValue(C);
4955
4956 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
4957 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
4958 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
4959 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4960 return true;
4961 }
4962
4964 if (!GEP || GEP->getParent() != CurBB)
4965 return false;
4966
4967 if (GEP->getNumOperands() != 2)
4968 return false;
4969
4970 const Value *BasePtr = GEP->getPointerOperand();
4971 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
4972
4973 // Make sure the base is scalar and the index is a vector.
4974 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
4975 return false;
4976
4977 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
4978 if (ScaleVal.isScalable())
4979 return false;
4980
4981 // Target may not support the required addressing mode.
4982 if (ScaleVal != 1 &&
4983 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
4984 return false;
4985
4986 Base = SDB->getValue(BasePtr);
4987 Index = SDB->getValue(IndexVal);
4988
4989 Scale =
4990 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4991 return true;
4992}
4993
4994void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
4995 SDLoc sdl = getCurSDLoc();
4996
4997 // llvm.masked.scatter.*(Src0, Ptrs, Mask)
4998 const Value *Ptr = I.getArgOperand(1);
4999 SDValue Src0 = getValue(I.getArgOperand(0));
5000 SDValue Mask = getValue(I.getArgOperand(2));
5001 EVT VT = Src0.getValueType();
5002 Align Alignment = I.getParamAlign(1).valueOrOne();
5003 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5004
5005 SDValue Base;
5006 SDValue Index;
5007 SDValue Scale;
5008 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5009 I.getParent(), VT.getScalarStoreSize());
5010
5011 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5012 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5013 MachinePointerInfo(AS), MachineMemOperand::MOStore,
5014 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
5015 if (!UniformBase) {
5016 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5017 Index = getValue(Ptr);
5018 Scale =
5019 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5020 }
5021
5022 EVT IdxVT = Index.getValueType();
5023 EVT EltTy = IdxVT.getVectorElementType();
5024 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5025 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5026 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5027 }
5028
5029 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
5030 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
5031 Ops, MMO, ISD::SIGNED_SCALED, false);
5032 DAG.setRoot(Scatter);
5033 setValue(&I, Scatter);
5034}
5035
5036void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5037 SDLoc sdl = getCurSDLoc();
5038
5039 Value *PtrOperand = I.getArgOperand(0);
5040 Value *MaskOperand = I.getArgOperand(1);
5041 Value *Src0Operand = I.getArgOperand(2);
5042 Align Alignment = I.getParamAlign(0).valueOrOne();
5043
5044 SDValue Ptr = getValue(PtrOperand);
5045 SDValue Src0 = getValue(Src0Operand);
5046 SDValue Mask = getValue(MaskOperand);
5047 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5048
5049 EVT VT = Src0.getValueType();
5050 AAMDNodes AAInfo = I.getAAMetadata();
5051 const MDNode *Ranges = getRangeMetadata(I);
5052
5053 // Do not serialize masked loads of constant memory with anything.
5054 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5055 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5056
5057 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5058
5059 auto MMOFlags = MachineMemOperand::MOLoad;
5060 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5062
5063 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5064 MachinePointerInfo(PtrOperand), MMOFlags,
5065 LocationSize::beforeOrAfterPointer(), Alignment, AAInfo, Ranges);
5066
5067 const auto &TLI = DAG.getTargetLoweringInfo();
5068 const auto &TTI =
5069 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
5070 // The Load/Res may point to different values and both of them are output
5071 // variables.
5072 SDValue Load;
5073 SDValue Res;
5074 if (!IsExpanding && TTI.hasConditionalLoadStoreForType(Src0Operand->getType(),
5075 /*IsStore=*/false))
5076 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5077 else
5078 Res = Load =
5079 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5080 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5081 if (AddToChain)
5082 PendingLoads.push_back(Load.getValue(1));
5083 setValue(&I, Res);
5084}
5085
5086void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5087 SDLoc sdl = getCurSDLoc();
5088
5089 // @llvm.masked.gather.*(Ptrs, Mask, Src0)
5090 const Value *Ptr = I.getArgOperand(0);
5091 SDValue Src0 = getValue(I.getArgOperand(2));
5092 SDValue Mask = getValue(I.getArgOperand(1));
5093
5094 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5095 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5096 Align Alignment = I.getParamAlign(0).valueOrOne();
5097
5098 const MDNode *Ranges = getRangeMetadata(I);
5099
5100 SDValue Root = DAG.getRoot();
5101 SDValue Base;
5102 SDValue Index;
5103 SDValue Scale;
5104 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5105 I.getParent(), VT.getScalarStoreSize());
5106 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5107 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5108 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5109 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5110 Ranges);
5111
5112 if (!UniformBase) {
5113 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5114 Index = getValue(Ptr);
5115 Scale =
5116 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5117 }
5118
5119 EVT IdxVT = Index.getValueType();
5120 EVT EltTy = IdxVT.getVectorElementType();
5121 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5122 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5123 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5124 }
5125
5126 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5127 SDValue Gather =
5128 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5130
5131 PendingLoads.push_back(Gather.getValue(1));
5132 setValue(&I, Gather);
5133}
5134
5135void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5136 SDLoc dl = getCurSDLoc();
5137 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5138 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5139 SyncScope::ID SSID = I.getSyncScopeID();
5140
5141 SDValue InChain = getRoot();
5142
5143 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5144 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5145
5146 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5147 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5148
5149 MachineFunction &MF = DAG.getMachineFunction();
5150 MachineMemOperand *MMO = MF.getMachineMemOperand(
5151 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5152 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5153 FailureOrdering);
5154
5155 SDValue L = DAG.getAtomicCmpSwap(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS,
5156 dl, MemVT, VTs, InChain,
5157 getValue(I.getPointerOperand()),
5158 getValue(I.getCompareOperand()),
5159 getValue(I.getNewValOperand()), MMO);
5160
5161 SDValue OutChain = L.getValue(2);
5162
5163 setValue(&I, L);
5164 DAG.setRoot(OutChain);
5165}
5166
5167void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5168 SDLoc dl = getCurSDLoc();
5170 switch (I.getOperation()) {
5171 default: llvm_unreachable("Unknown atomicrmw operation");
5172 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
5173 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
5174 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
5175 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
5176 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
5177 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
5178 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
5179 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
5180 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
5181 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
5182 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
5183 case AtomicRMWInst::FAdd: NT = ISD::ATOMIC_LOAD_FADD; break;
5184 case AtomicRMWInst::FSub: NT = ISD::ATOMIC_LOAD_FSUB; break;
5185 case AtomicRMWInst::FMax: NT = ISD::ATOMIC_LOAD_FMAX; break;
5186 case AtomicRMWInst::FMin: NT = ISD::ATOMIC_LOAD_FMIN; break;
5188 NT = ISD::ATOMIC_LOAD_FMAXIMUM;
5189 break;
5191 NT = ISD::ATOMIC_LOAD_FMINIMUM;
5192 break;
5194 NT = ISD::ATOMIC_LOAD_UINC_WRAP;
5195 break;
5197 NT = ISD::ATOMIC_LOAD_UDEC_WRAP;
5198 break;
5200 NT = ISD::ATOMIC_LOAD_USUB_COND;
5201 break;
5203 NT = ISD::ATOMIC_LOAD_USUB_SAT;
5204 break;
5205 }
5206 AtomicOrdering Ordering = I.getOrdering();
5207 SyncScope::ID SSID = I.getSyncScopeID();
5208
5209 SDValue InChain = getRoot();
5210
5211 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5212 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5213 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5214
5215 MachineFunction &MF = DAG.getMachineFunction();
5216 MachineMemOperand *MMO = MF.getMachineMemOperand(
5217 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5218 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
5219
5220 SDValue L =
5221 DAG.getAtomic(NT, dl, MemVT, InChain,
5222 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5223 MMO);
5224
5225 SDValue OutChain = L.getValue(1);
5226
5227 setValue(&I, L);
5228 DAG.setRoot(OutChain);
5229}
5230
5231void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5232 SDLoc dl = getCurSDLoc();
5233 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5234 SDValue Ops[3];
5235 Ops[0] = getRoot();
5236 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5237 TLI.getFenceOperandTy(DAG.getDataLayout()));
5238 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5239 TLI.getFenceOperandTy(DAG.getDataLayout()));
5240 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5241 setValue(&I, N);
5242 DAG.setRoot(N);
5243}
5244
5245void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5246 SDLoc dl = getCurSDLoc();
5247 AtomicOrdering Order = I.getOrdering();
5248 SyncScope::ID SSID = I.getSyncScopeID();
5249
5250 SDValue InChain = getRoot();
5251
5252 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5253 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5254 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5255
5256 if (!TLI.supportsUnalignedAtomics() &&
5257 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5258 report_fatal_error("Cannot generate unaligned atomic load");
5259
5260 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5261
5262 const MDNode *Ranges = getRangeMetadata(I);
5263 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5264 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5265 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5266
5267 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5268
5269 SDValue Ptr = getValue(I.getPointerOperand());
5270 SDValue L =
5271 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5272
5273 SDValue OutChain = L.getValue(1);
5274 if (MemVT != VT)
5275 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5276
5277 setValue(&I, L);
5278 DAG.setRoot(OutChain);
5279}
5280
5281void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5282 SDLoc dl = getCurSDLoc();
5283
5284 AtomicOrdering Ordering = I.getOrdering();
5285 SyncScope::ID SSID = I.getSyncScopeID();
5286
5287 SDValue InChain = getRoot();
5288
5289 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5290 EVT MemVT =
5291 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5292
5293 if (!TLI.supportsUnalignedAtomics() &&
5294 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5295 report_fatal_error("Cannot generate unaligned atomic store");
5296
5297 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5298
5299 MachineFunction &MF = DAG.getMachineFunction();
5300 MachineMemOperand *MMO = MF.getMachineMemOperand(
5301 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5302 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5303
5304 SDValue Val = getValue(I.getValueOperand());
5305 if (Val.getValueType() != MemVT)
5306 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5307 SDValue Ptr = getValue(I.getPointerOperand());
5308
5309 SDValue OutChain =
5310 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5311
5312 setValue(&I, OutChain);
5313 DAG.setRoot(OutChain);
5314}
5315
5316/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5317/// node.
5318void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5319 unsigned Intrinsic) {
5320 // Ignore the callsite's attributes. A specific call site may be marked with
5321 // readnone, but the lowering code will expect the chain based on the
5322 // definition.
5323 const Function *F = I.getCalledFunction();
5324 bool HasChain = !F->doesNotAccessMemory();
5325 bool OnlyLoad =
5326 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5327
5328 // Build the operand list.
5330 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5331 if (OnlyLoad) {
5332 // We don't need to serialize loads against other loads.
5333 Ops.push_back(DAG.getRoot());
5334 } else {
5335 Ops.push_back(getRoot());
5336 }
5337 }
5338
5339 // Info is set by getTgtMemIntrinsic
5340 TargetLowering::IntrinsicInfo Info;
5341 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5342 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I,
5343 DAG.getMachineFunction(),
5344 Intrinsic);
5345
5346 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5347 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
5349 Ops.push_back(DAG.getTargetConstant(Intrinsic, getCurSDLoc(),
5350 TLI.getPointerTy(DAG.getDataLayout())));
5351
5352 // Add all operands of the call to the operand list.
5353 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5354 const Value *Arg = I.getArgOperand(i);
5355 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5356 Ops.push_back(getValue(Arg));
5357 continue;
5358 }
5359
5360 // Use TargetConstant instead of a regular constant for immarg.
5361 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5362 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5363 assert(CI->getBitWidth() <= 64 &&
5364 "large intrinsic immediates not handled");
5365 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5366 } else {
5367 Ops.push_back(
5368 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5369 }
5370 }
5371
5372 SmallVector<EVT, 4> ValueVTs;
5373 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5374
5375 if (HasChain)
5376 ValueVTs.push_back(MVT::Other);
5377
5378 SDVTList VTs = DAG.getVTList(ValueVTs);
5379
5380 // Propagate fast-math-flags from IR to node(s).
5381 SDNodeFlags Flags;
5382 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5383 Flags.copyFMF(*FPMO);
5384 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5385
5386 // Create the node.
5388
5389 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5390 auto *Token = Bundle->Inputs[0].get();
5391 SDValue ConvControlToken = getValue(Token);
5392 assert(Ops.back().getValueType() != MVT::Glue &&
5393 "Did not expected another glue node here.");
5394 ConvControlToken =
5395 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5396 Ops.push_back(ConvControlToken);
5397 }
5398
5399 // In some cases, custom collection of operands from CallInst I may be needed.
5401 if (IsTgtIntrinsic) {
5402 // This is target intrinsic that touches memory
5403 //
5404 // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic
5405 // didn't yield anything useful.
5406 MachinePointerInfo MPI;
5407 if (Info.ptrVal)
5408 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5409 else if (Info.fallbackAddressSpace)
5410 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5411 EVT MemVT = Info.memVT;
5412 LocationSize Size = LocationSize::precise(Info.size);
5413 if (Size.hasValue() && !Size.getValue())
5415 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5416 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5417 MPI, Info.flags, Size, Alignment, I.getAAMetadata(), /*Ranges=*/nullptr,
5418 Info.ssid, Info.order, Info.failureOrder);
5419 Result =
5420 DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops, MemVT, MMO);
5421 } else if (!HasChain) {
5422 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5423 } else if (!I.getType()->isVoidTy()) {
5424 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5425 } else {
5426 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5427 }
5428
5429 if (HasChain) {
5430 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
5431 if (OnlyLoad)
5432 PendingLoads.push_back(Chain);
5433 else
5434 DAG.setRoot(Chain);
5435 }
5436
5437 if (!I.getType()->isVoidTy()) {
5438 if (!isa<VectorType>(I.getType()))
5439 Result = lowerRangeToAssertZExt(DAG, I, Result);
5440
5441 MaybeAlign Alignment = I.getRetAlign();
5442
5443 // Insert `assertalign` node if there's an alignment.
5444 if (InsertAssertAlign && Alignment) {
5445 Result =
5446 DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5447 }
5448 }
5449
5450 setValue(&I, Result);
5451}
5452
5453/// GetSignificand - Get the significand and build it into a floating-point
5454/// number with exponent of 1:
5455///
5456/// Op = (Op & 0x007fffff) | 0x3f800000;
5457///
5458/// where Op is the hexadecimal representation of floating point value.
5460 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5461 DAG.getConstant(0x007fffff, dl, MVT::i32));
5462 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5463 DAG.getConstant(0x3f800000, dl, MVT::i32));
5464 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5465}
5466
5467/// GetExponent - Get the exponent:
5468///
5469/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5470///
5471/// where Op is the hexadecimal representation of floating point value.
5473 const TargetLowering &TLI, const SDLoc &dl) {
5474 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5475 DAG.getConstant(0x7f800000, dl, MVT::i32));
5476 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5477 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5478 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5479 DAG.getConstant(127, dl, MVT::i32));
5480 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5481}
5482
5483/// getF32Constant - Get 32-bit floating point constant.
5485 const SDLoc &dl) {
5486 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5487 MVT::f32);
5488}
5489
5491 SelectionDAG &DAG) {
5492 // TODO: What fast-math-flags should be set on the floating-point nodes?
5493
5494 // IntegerPartOfX = ((int32_t)(t0);
5495 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5496
5497 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5498 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5499 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5500
5501 // IntegerPartOfX <<= 23;
5502 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5503 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5504
5505 SDValue TwoToFractionalPartOfX;
5506 if (LimitFloatPrecision <= 6) {
5507 // For floating-point precision of 6:
5508 //
5509 // TwoToFractionalPartOfX =
5510 // 0.997535578f +
5511 // (0.735607626f + 0.252464424f * x) * x;
5512 //
5513 // error 0.0144103317, which is 6 bits
5514 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5515 getF32Constant(DAG, 0x3e814304, dl));
5516 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5517 getF32Constant(DAG, 0x3f3c50c8, dl));
5518 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5519 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5520 getF32Constant(DAG, 0x3f7f5e7e, dl));
5521 } else if (LimitFloatPrecision <= 12) {
5522 // For floating-point precision of 12:
5523 //
5524 // TwoToFractionalPartOfX =
5525 // 0.999892986f +
5526 // (0.696457318f +
5527 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5528 //
5529 // error 0.000107046256, which is 13 to 14 bits
5530 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5531 getF32Constant(DAG, 0x3da235e3, dl));
5532 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5533 getF32Constant(DAG, 0x3e65b8f3, dl));
5534 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5535 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5536 getF32Constant(DAG, 0x3f324b07, dl));
5537 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5538 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5539 getF32Constant(DAG, 0x3f7ff8fd, dl));
5540 } else { // LimitFloatPrecision <= 18
5541 // For floating-point precision of 18:
5542 //
5543 // TwoToFractionalPartOfX =
5544 // 0.999999982f +
5545 // (0.693148872f +
5546 // (0.240227044f +
5547 // (0.554906021e-1f +
5548 // (0.961591928e-2f +
5549 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5550 // error 2.47208000*10^(-7), which is better than 18 bits
5551 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5552 getF32Constant(DAG, 0x3924b03e, dl));
5553 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5554 getF32Constant(DAG, 0x3ab24b87, dl));
5555 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5556 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5557 getF32Constant(DAG, 0x3c1d8c17, dl));
5558 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5559 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5560 getF32Constant(DAG, 0x3d634a1d, dl));
5561 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5562 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5563 getF32Constant(DAG, 0x3e75fe14, dl));
5564 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5565 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5566 getF32Constant(DAG, 0x3f317234, dl));
5567 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5568 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5569 getF32Constant(DAG, 0x3f800000, dl));
5570 }
5571
5572 // Add the exponent into the result in integer domain.
5573 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5574 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5575 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5576}
5577
5578/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5579/// limited-precision mode.
5581 const TargetLowering &TLI, SDNodeFlags Flags) {
5582 if (Op.getValueType() == MVT::f32 &&
5584
5585 // Put the exponent in the right bit position for later addition to the
5586 // final result:
5587 //
5588 // t0 = Op * log2(e)
5589
5590 // TODO: What fast-math-flags should be set here?
5591 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5592 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5593 return getLimitedPrecisionExp2(t0, dl, DAG);
5594 }
5595
5596 // No special expansion.
5597 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5598}
5599
5600/// expandLog - Lower a log intrinsic. Handles the special sequences for
5601/// limited-precision mode.
5603 const TargetLowering &TLI, SDNodeFlags Flags) {
5604 // TODO: What fast-math-flags should be set on the floating-point nodes?
5605
5606 if (Op.getValueType() == MVT::f32 &&
5608 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5609
5610 // Scale the exponent by log(2).
5611 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5612 SDValue LogOfExponent =
5613 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5614 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5615
5616 // Get the significand and build it into a floating-point number with
5617 // exponent of 1.
5618 SDValue X = GetSignificand(DAG, Op1, dl);
5619
5620 SDValue LogOfMantissa;
5621 if (LimitFloatPrecision <= 6) {
5622 // For floating-point precision of 6:
5623 //
5624 // LogofMantissa =
5625 // -1.1609546f +
5626 // (1.4034025f - 0.23903021f * x) * x;
5627 //
5628 // error 0.0034276066, which is better than 8 bits
5629 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5630 getF32Constant(DAG, 0xbe74c456, dl));
5631 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5632 getF32Constant(DAG, 0x3fb3a2b1, dl));
5633 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5634 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5635 getF32Constant(DAG, 0x3f949a29, dl));
5636 } else if (LimitFloatPrecision <= 12) {
5637 // For floating-point precision of 12:
5638 //
5639 // LogOfMantissa =
5640 // -1.7417939f +
5641 // (2.8212026f +
5642 // (-1.4699568f +
5643 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5644 //
5645 // error 0.000061011436, which is 14 bits
5646 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5647 getF32Constant(DAG, 0xbd67b6d6, dl));
5648 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5649 getF32Constant(DAG, 0x3ee4f4b8, dl));
5650 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5651 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5652 getF32Constant(DAG, 0x3fbc278b, dl));
5653 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5654 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5655 getF32Constant(DAG, 0x40348e95, dl));
5656 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5657 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5658 getF32Constant(DAG, 0x3fdef31a, dl));
5659 } else { // LimitFloatPrecision <= 18
5660 // For floating-point precision of 18:
5661 //
5662 // LogOfMantissa =
5663 // -2.1072184f +
5664 // (4.2372794f +
5665 // (-3.7029485f +
5666 // (2.2781945f +
5667 // (-0.87823314f +
5668 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5669 //
5670 // error 0.0000023660568, which is better than 18 bits
5671 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5672 getF32Constant(DAG, 0xbc91e5ac, dl));
5673 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5674 getF32Constant(DAG, 0x3e4350aa, dl));
5675 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5676 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5677 getF32Constant(DAG, 0x3f60d3e3, dl));
5678 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5679 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5680 getF32Constant(DAG, 0x4011cdf0, dl));
5681 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5682 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5683 getF32Constant(DAG, 0x406cfd1c, dl));
5684 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5685 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5686 getF32Constant(DAG, 0x408797cb, dl));
5687 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5688 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5689 getF32Constant(DAG, 0x4006dcab, dl));
5690 }
5691
5692 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5693 }
5694
5695 // No special expansion.
5696 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5697}
5698
5699/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5700/// limited-precision mode.
5702 const TargetLowering &TLI, SDNodeFlags Flags) {
5703 // TODO: What fast-math-flags should be set on the floating-point nodes?
5704
5705 if (Op.getValueType() == MVT::f32 &&
5707 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5708
5709 // Get the exponent.
5710 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5711
5712 // Get the significand and build it into a floating-point number with
5713 // exponent of 1.
5714 SDValue X = GetSignificand(DAG, Op1, dl);
5715
5716 // Different possible minimax approximations of significand in
5717 // floating-point for various degrees of accuracy over [1,2].
5718 SDValue Log2ofMantissa;
5719 if (LimitFloatPrecision <= 6) {
5720 // For floating-point precision of 6:
5721 //
5722 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5723 //
5724 // error 0.0049451742, which is more than 7 bits
5725 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5726 getF32Constant(DAG, 0xbeb08fe0, dl));
5727 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5728 getF32Constant(DAG, 0x40019463, dl));
5729 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5730 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5731 getF32Constant(DAG, 0x3fd6633d, dl));
5732 } else if (LimitFloatPrecision <= 12) {
5733 // For floating-point precision of 12:
5734 //
5735 // Log2ofMantissa =
5736 // -2.51285454f +
5737 // (4.07009056f +
5738 // (-2.12067489f +
5739 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5740 //
5741 // error 0.0000876136000, which is better than 13 bits
5742 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5743 getF32Constant(DAG, 0xbda7262e, dl));
5744 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5745 getF32Constant(DAG, 0x3f25280b, dl));
5746 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5747 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5748 getF32Constant(DAG, 0x4007b923, dl));
5749 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5750 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5751 getF32Constant(DAG, 0x40823e2f, dl));
5752 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5753 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5754 getF32Constant(DAG, 0x4020d29c, dl));
5755 } else { // LimitFloatPrecision <= 18
5756 // For floating-point precision of 18:
5757 //
5758 // Log2ofMantissa =
5759 // -3.0400495f +
5760 // (6.1129976f +
5761 // (-5.3420409f +
5762 // (3.2865683f +
5763 // (-1.2669343f +
5764 // (0.27515199f -
5765 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5766 //
5767 // error 0.0000018516, which is better than 18 bits
5768 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5769 getF32Constant(DAG, 0xbcd2769e, dl));
5770 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5771 getF32Constant(DAG, 0x3e8ce0b9, dl));
5772 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5773 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5774 getF32Constant(DAG, 0x3fa22ae7, dl));
5775 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5776 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5777 getF32Constant(DAG, 0x40525723, dl));
5778 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5779 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5780 getF32Constant(DAG, 0x40aaf200, dl));
5781 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5782 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5783 getF32Constant(DAG, 0x40c39dad, dl));
5784 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5785 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5786 getF32Constant(DAG, 0x4042902c, dl));
5787 }
5788
5789 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5790 }
5791
5792 // No special expansion.
5793 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5794}
5795
5796/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5797/// limited-precision mode.
5799 const TargetLowering &TLI, SDNodeFlags Flags) {
5800 // TODO: What fast-math-flags should be set on the floating-point nodes?
5801
5802 if (Op.getValueType() == MVT::f32 &&
5804 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5805
5806 // Scale the exponent by log10(2) [0.30102999f].
5807 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5808 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5809 getF32Constant(DAG, 0x3e9a209a, dl));
5810
5811 // Get the significand and build it into a floating-point number with
5812 // exponent of 1.
5813 SDValue X = GetSignificand(DAG, Op1, dl);
5814
5815 SDValue Log10ofMantissa;
5816 if (LimitFloatPrecision <= 6) {
5817 // For floating-point precision of 6:
5818 //
5819 // Log10ofMantissa =
5820 // -0.50419619f +
5821 // (0.60948995f - 0.10380950f * x) * x;
5822 //
5823 // error 0.0014886165, which is 6 bits
5824 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5825 getF32Constant(DAG, 0xbdd49a13, dl));
5826 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5827 getF32Constant(DAG, 0x3f1c0789, dl));
5828 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5829 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5830 getF32Constant(DAG, 0x3f011300, dl));
5831 } else if (LimitFloatPrecision <= 12) {
5832 // For floating-point precision of 12:
5833 //
5834 // Log10ofMantissa =
5835 // -0.64831180f +
5836 // (0.91751397f +
5837 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5838 //
5839 // error 0.00019228036, which is better than 12 bits
5840 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5841 getF32Constant(DAG, 0x3d431f31, dl));
5842 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5843 getF32Constant(DAG, 0x3ea21fb2, dl));
5844 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5845 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5846 getF32Constant(DAG, 0x3f6ae232, dl));
5847 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5848 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5849 getF32Constant(DAG, 0x3f25f7c3, dl));
5850 } else { // LimitFloatPrecision <= 18
5851 // For floating-point precision of 18:
5852 //
5853 // Log10ofMantissa =
5854 // -0.84299375f +
5855 // (1.5327582f +
5856 // (-1.0688956f +
5857 // (0.49102474f +
5858 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5859 //
5860 // error 0.0000037995730, which is better than 18 bits
5861 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5862 getF32Constant(DAG, 0x3c5d51ce, dl));
5863 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5864 getF32Constant(DAG, 0x3e00685a, dl));
5865 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5866 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5867 getF32Constant(DAG, 0x3efb6798, dl));
5868 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5869 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5870 getF32Constant(DAG, 0x3f88d192, dl));
5871 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5872 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5873 getF32Constant(DAG, 0x3fc4316c, dl));
5874 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5875 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5876 getF32Constant(DAG, 0x3f57ce70, dl));
5877 }
5878
5879 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5880 }
5881
5882 // No special expansion.
5883 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5884}
5885
5886/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5887/// limited-precision mode.
5889 const TargetLowering &TLI, SDNodeFlags Flags) {
5890 if (Op.getValueType() == MVT::f32 &&
5892 return getLimitedPrecisionExp2(Op, dl, DAG);
5893
5894 // No special expansion.
5895 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
5896}
5897
5898/// visitPow - Lower a pow intrinsic. Handles the special sequences for
5899/// limited-precision mode with x == 10.0f.
5901 SelectionDAG &DAG, const TargetLowering &TLI,
5902 SDNodeFlags Flags) {
5903 bool IsExp10 = false;
5904 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
5907 APFloat Ten(10.0f);
5908 IsExp10 = LHSC->isExactlyValue(Ten);
5909 }
5910 }
5911
5912 // TODO: What fast-math-flags should be set on the FMUL node?
5913 if (IsExp10) {
5914 // Put the exponent in the right bit position for later addition to the
5915 // final result:
5916 //
5917 // #define LOG2OF10 3.3219281f
5918 // t0 = Op * LOG2OF10;
5919 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
5920 getF32Constant(DAG, 0x40549a78, dl));
5921 return getLimitedPrecisionExp2(t0, dl, DAG);
5922 }
5923
5924 // No special expansion.
5925 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
5926}
5927
5928/// ExpandPowI - Expand a llvm.powi intrinsic.
5930 SelectionDAG &DAG) {
5931 // If RHS is a constant, we can expand this out to a multiplication tree if
5932 // it's beneficial on the target, otherwise we end up lowering to a call to
5933 // __powidf2 (for example).
5935 unsigned Val = RHSC->getSExtValue();
5936
5937 // powi(x, 0) -> 1.0
5938 if (Val == 0)
5939 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
5940
5942 Val, DAG.shouldOptForSize())) {
5943 // Get the exponent as a positive value.
5944 if ((int)Val < 0)
5945 Val = -Val;
5946 // We use the simple binary decomposition method to generate the multiply
5947 // sequence. There are more optimal ways to do this (for example,
5948 // powi(x,15) generates one more multiply than it should), but this has
5949 // the benefit of being both really simple and much better than a libcall.
5950 SDValue Res; // Logically starts equal to 1.0
5951 SDValue CurSquare = LHS;
5952 // TODO: Intrinsics should have fast-math-flags that propagate to these
5953 // nodes.
5954 while (Val) {
5955 if (Val & 1) {
5956 if (Res.getNode())
5957 Res =
5958 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
5959 else
5960 Res = CurSquare; // 1.0*CurSquare.
5961 }
5962
5963 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
5964 CurSquare, CurSquare);
5965 Val >>= 1;
5966 }
5967
5968 // If the original was negative, invert the result, producing 1/(x*x*x).
5969 if (RHSC->getSExtValue() < 0)
5970 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
5971 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
5972 return Res;
5973 }
5974 }
5975
5976 // Otherwise, expand to a libcall.
5977 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
5978}
5979
5980static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
5981 SDValue LHS, SDValue RHS, SDValue Scale,
5982 SelectionDAG &DAG, const TargetLowering &TLI) {
5983 EVT VT = LHS.getValueType();
5984 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
5985 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
5986 LLVMContext &Ctx = *DAG.getContext();
5987
5988 // If the type is legal but the operation isn't, this node might survive all
5989 // the way to operation legalization. If we end up there and we do not have
5990 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
5991 // node.
5992
5993 // Coax the legalizer into expanding the node during type legalization instead
5994 // by bumping the size by one bit. This will force it to Promote, enabling the
5995 // early expansion and avoiding the need to expand later.
5996
5997 // We don't have to do this if Scale is 0; that can always be expanded, unless
5998 // it's a saturating signed operation. Those can experience true integer
5999 // division overflow, a case which we must avoid.
6000
6001 // FIXME: We wouldn't have to do this (or any of the early
6002 // expansion/promotion) if it was possible to expand a libcall of an
6003 // illegal type during operation legalization. But it's not, so things
6004 // get a bit hacky.
6005 unsigned ScaleInt = Scale->getAsZExtVal();
6006 if ((ScaleInt > 0 || (Saturating && Signed)) &&
6007 (TLI.isTypeLegal(VT) ||
6008 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6010 Opcode, VT, ScaleInt);
6011 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6012 EVT PromVT;
6013 if (VT.isScalarInteger())
6014 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6015 else if (VT.isVector()) {
6016 PromVT = VT.getVectorElementType();
6017 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6018 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6019 } else
6020 llvm_unreachable("Wrong VT for DIVFIX?");
6021 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6022 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6023 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6024 // For saturating operations, we need to shift up the LHS to get the
6025 // proper saturation width, and then shift down again afterwards.
6026 if (Saturating)
6027 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6028 DAG.getConstant(1, DL, ShiftTy));
6029 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6030 if (Saturating)
6031 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6032 DAG.getConstant(1, DL, ShiftTy));
6033 return DAG.getZExtOrTrunc(Res, DL, VT);
6034 }
6035 }
6036
6037 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6038}
6039
6040// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6041// bitcasted, or split argument. Returns a list of <Register, size in bits>
6042static void
6043getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6044 const SDValue &N) {
6045 switch (N.getOpcode()) {
6046 case ISD::CopyFromReg: {
6047 SDValue Op = N.getOperand(1);
6048 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6049 Op.getValueType().getSizeInBits());
6050 return;
6051 }
6052 case ISD::BITCAST:
6053 case ISD::AssertZext:
6054 case ISD::AssertSext:
6055 case ISD::TRUNCATE:
6056 getUnderlyingArgRegs(Regs, N.getOperand(0));
6057 return;
6058 case ISD::BUILD_PAIR:
6059 case ISD::BUILD_VECTOR:
6061 for (SDValue Op : N->op_values())
6062 getUnderlyingArgRegs(Regs, Op);
6063 return;
6064 default:
6065 return;
6066 }
6067}
6068
6069/// If the DbgValueInst is a dbg_value of a function argument, create the
6070/// corresponding DBG_VALUE machine instruction for it now. At the end of
6071/// instruction selection, they will be inserted to the entry BB.
6072/// We don't currently support this for variadic dbg_values, as they shouldn't
6073/// appear for function arguments or in the prologue.
6074bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6075 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6076 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6077 const Argument *Arg = dyn_cast<Argument>(V);
6078 if (!Arg)
6079 return false;
6080
6081 MachineFunction &MF = DAG.getMachineFunction();
6082 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6083
6084 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6085 // we've been asked to pursue.
6086 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6087 bool Indirect) {
6088 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6089 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6090 // pointing at the VReg, which will be patched up later.
6091 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6093 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6094 /* isKill */ false, /* isDead */ false,
6095 /* isUndef */ false, /* isEarlyClobber */ false,
6096 /* SubReg */ 0, /* isDebug */ true)});
6097
6098 auto *NewDIExpr = FragExpr;
6099 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6100 // the DIExpression.
6101 if (Indirect)
6102 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6104 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6105 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6106 } else {
6107 // Create a completely standard DBG_VALUE.
6108 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6109 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6110 }
6111 };
6112
6113 if (Kind == FuncArgumentDbgValueKind::Value) {
6114 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6115 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6116 // the entry block.
6117 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6118 if (!IsInEntryBlock)
6119 return false;
6120
6121 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6122 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6123 // variable that also is a param.
6124 //
6125 // Although, if we are at the top of the entry block already, we can still
6126 // emit using ArgDbgValue. This might catch some situations when the
6127 // dbg.value refers to an argument that isn't used in the entry block, so
6128 // any CopyToReg node would be optimized out and the only way to express
6129 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6130 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6131 // we should only emit as ArgDbgValue if the Variable is an argument to the
6132 // current function, and the dbg.value intrinsic is found in the entry
6133 // block.
6134 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6135 !DL->getInlinedAt();
6136 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6137 if (!IsInPrologue && !VariableIsFunctionInputArg)
6138 return false;
6139
6140 // Here we assume that a function argument on IR level only can be used to
6141 // describe one input parameter on source level. If we for example have
6142 // source code like this
6143 //
6144 // struct A { long x, y; };
6145 // void foo(struct A a, long b) {
6146 // ...
6147 // b = a.x;
6148 // ...
6149 // }
6150 //
6151 // and IR like this
6152 //
6153 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6154 // entry:
6155 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6156 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6157 // call void @llvm.dbg.value(metadata i32 %b, "b",
6158 // ...
6159 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6160 // ...
6161 //
6162 // then the last dbg.value is describing a parameter "b" using a value that
6163 // is an argument. But since we already has used %a1 to describe a parameter
6164 // we should not handle that last dbg.value here (that would result in an
6165 // incorrect hoisting of the DBG_VALUE to the function entry).
6166 // Notice that we allow one dbg.value per IR level argument, to accommodate
6167 // for the situation with fragments above.
6168 // If there is no node for the value being handled, we return true to skip
6169 // the normal generation of debug info, as it would kill existing debug
6170 // info for the parameter in case of duplicates.
6171 if (VariableIsFunctionInputArg) {
6172 unsigned ArgNo = Arg->getArgNo();
6173 if (ArgNo >= FuncInfo.DescribedArgs.size())
6174 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6175 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6176 return !NodeMap[V].getNode();
6177 FuncInfo.DescribedArgs.set(ArgNo);
6178 }
6179 }
6180
6181 bool IsIndirect = false;
6182 std::optional<MachineOperand> Op;
6183 // Some arguments' frame index is recorded during argument lowering.
6184 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6185 if (FI != std::numeric_limits<int>::max())
6187
6189 if (!Op && N.getNode()) {
6190 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6191 Register Reg;
6192 if (ArgRegsAndSizes.size() == 1)
6193 Reg = ArgRegsAndSizes.front().first;
6194
6195 if (Reg && Reg.isVirtual()) {
6196 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6197 Register PR = RegInfo.getLiveInPhysReg(Reg);
6198 if (PR)
6199 Reg = PR;
6200 }
6201 if (Reg) {
6203 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6204 }
6205 }
6206
6207 if (!Op && N.getNode()) {
6208 // Check if frame index is available.
6209 SDValue LCandidate = peekThroughBitcasts(N);
6210 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6211 if (FrameIndexSDNode *FINode =
6212 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6213 Op = MachineOperand::CreateFI(FINode->getIndex());
6214 }
6215
6216 if (!Op) {
6217 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6218 auto splitMultiRegDbgValue = [&](ArrayRef<std::pair<Register, TypeSize>>
6219 SplitRegs) {
6220 unsigned Offset = 0;
6221 for (const auto &RegAndSize : SplitRegs) {
6222 // If the expression is already a fragment, the current register
6223 // offset+size might extend beyond the fragment. In this case, only
6224 // the register bits that are inside the fragment are relevant.
6225 int RegFragmentSizeInBits = RegAndSize.second;
6226 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6227 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6228 // The register is entirely outside the expression fragment,
6229 // so is irrelevant for debug info.
6230 if (Offset >= ExprFragmentSizeInBits)
6231 break;
6232 // The register is partially outside the expression fragment, only
6233 // the low bits within the fragment are relevant for debug info.
6234 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6235 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6236 }
6237 }
6238
6239 auto FragmentExpr = DIExpression::createFragmentExpression(
6240 Expr, Offset, RegFragmentSizeInBits);
6241 Offset += RegAndSize.second;
6242 // If a valid fragment expression cannot be created, the variable's
6243 // correct value cannot be determined and so it is set as poison.
6244 if (!FragmentExpr) {
6245 SDDbgValue *SDV = DAG.getConstantDbgValue(
6246 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6247 DAG.AddDbgValue(SDV, false);
6248 continue;
6249 }
6250 MachineInstr *NewMI =
6251 MakeVRegDbgValue(RegAndSize.first, *FragmentExpr,
6252 Kind != FuncArgumentDbgValueKind::Value);
6253 FuncInfo.ArgDbgValues.push_back(NewMI);
6254 }
6255 };
6256
6257 // Check if ValueMap has reg number.
6259 VMI = FuncInfo.ValueMap.find(V);
6260 if (VMI != FuncInfo.ValueMap.end()) {
6261 const auto &TLI = DAG.getTargetLoweringInfo();
6262 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6263 V->getType(), std::nullopt);
6264 if (RFV.occupiesMultipleRegs()) {
6265 splitMultiRegDbgValue(RFV.getRegsAndSizes());
6266 return true;
6267 }
6268
6269 Op = MachineOperand::CreateReg(VMI->second, false);
6270 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6271 } else if (ArgRegsAndSizes.size() > 1) {
6272 // This was split due to the calling convention, and no virtual register
6273 // mapping exists for the value.
6274 splitMultiRegDbgValue(ArgRegsAndSizes);
6275 return true;
6276 }
6277 }
6278
6279 if (!Op)
6280 return false;
6281
6282 assert(Variable->isValidLocationForIntrinsic(DL) &&
6283 "Expected inlined-at fields to agree");
6284 MachineInstr *NewMI = nullptr;
6285
6286 if (Op->isReg())
6287 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6288 else
6289 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6290 Variable, Expr);
6291
6292 // Otherwise, use ArgDbgValues.
6293 FuncInfo.ArgDbgValues.push_back(NewMI);
6294 return true;
6295}
6296
6297/// Return the appropriate SDDbgValue based on N.
6298SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6299 DILocalVariable *Variable,
6300 DIExpression *Expr,
6301 const DebugLoc &dl,
6302 unsigned DbgSDNodeOrder) {
6303 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6304 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6305 // stack slot locations.
6306 //
6307 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6308 // debug values here after optimization:
6309 //
6310 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6311 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6312 //
6313 // Both describe the direct values of their associated variables.
6314 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6315 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6316 }
6317 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6318 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6319}
6320
6321static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6322 switch (Intrinsic) {
6323 case Intrinsic::smul_fix:
6324 return ISD::SMULFIX;
6325 case Intrinsic::umul_fix:
6326 return ISD::UMULFIX;
6327 case Intrinsic::smul_fix_sat:
6328 return ISD::SMULFIXSAT;
6329 case Intrinsic::umul_fix_sat:
6330 return ISD::UMULFIXSAT;
6331 case Intrinsic::sdiv_fix:
6332 return ISD::SDIVFIX;
6333 case Intrinsic::udiv_fix:
6334 return ISD::UDIVFIX;
6335 case Intrinsic::sdiv_fix_sat:
6336 return ISD::SDIVFIXSAT;
6337 case Intrinsic::udiv_fix_sat:
6338 return ISD::UDIVFIXSAT;
6339 default:
6340 llvm_unreachable("Unhandled fixed point intrinsic");
6341 }
6342}
6343
6344/// Given a @llvm.call.preallocated.setup, return the corresponding
6345/// preallocated call.
6346static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6347 assert(cast<CallBase>(PreallocatedSetup)
6349 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6350 "expected call_preallocated_setup Value");
6351 for (const auto *U : PreallocatedSetup->users()) {
6352 auto *UseCall = cast<CallBase>(U);
6353 const Function *Fn = UseCall->getCalledFunction();
6354 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6355 return UseCall;
6356 }
6357 }
6358 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6359}
6360
6361/// If DI is a debug value with an EntryValue expression, lower it using the
6362/// corresponding physical register of the associated Argument value
6363/// (guaranteed to exist by the verifier).
6364bool SelectionDAGBuilder::visitEntryValueDbgValue(
6365 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6366 DIExpression *Expr, DebugLoc DbgLoc) {
6367 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6368 return false;
6369
6370 // These properties are guaranteed by the verifier.
6371 const Argument *Arg = cast<Argument>(Values[0]);
6372 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6373
6374 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6375 if (ArgIt == FuncInfo.ValueMap.end()) {
6376 LLVM_DEBUG(
6377 dbgs() << "Dropping dbg.value: expression is entry_value but "
6378 "couldn't find an associated register for the Argument\n");
6379 return true;
6380 }
6381 Register ArgVReg = ArgIt->getSecond();
6382
6383 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6384 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6385 SDDbgValue *SDV = DAG.getVRegDbgValue(
6386 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6387 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6388 return true;
6389 }
6390 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6391 "couldn't find a physical register\n");
6392 return true;
6393}
6394
6395/// Lower the call to the specified intrinsic function.
6396void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6397 unsigned Intrinsic) {
6398 SDLoc sdl = getCurSDLoc();
6399 switch (Intrinsic) {
6400 case Intrinsic::experimental_convergence_anchor:
6401 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6402 break;
6403 case Intrinsic::experimental_convergence_entry:
6404 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6405 break;
6406 case Intrinsic::experimental_convergence_loop: {
6407 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6408 auto *Token = Bundle->Inputs[0].get();
6409 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6410 getValue(Token)));
6411 break;
6412 }
6413 }
6414}
6415
6416void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6417 unsigned IntrinsicID) {
6418 // For now, we're only lowering an 'add' histogram.
6419 // We can add others later, e.g. saturating adds, min/max.
6420 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6421 "Tried to lower unsupported histogram type");
6422 SDLoc sdl = getCurSDLoc();
6423 Value *Ptr = I.getOperand(0);
6424 SDValue Inc = getValue(I.getOperand(1));
6425 SDValue Mask = getValue(I.getOperand(2));
6426
6427 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6428 DataLayout TargetDL = DAG.getDataLayout();
6429 EVT VT = Inc.getValueType();
6430 Align Alignment = DAG.getEVTAlign(VT);
6431
6432 const MDNode *Ranges = getRangeMetadata(I);
6433
6434 SDValue Root = DAG.getRoot();
6435 SDValue Base;
6436 SDValue Index;
6437 SDValue Scale;
6438 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6439 I.getParent(), VT.getScalarStoreSize());
6440
6441 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6442
6443 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6444 MachinePointerInfo(AS),
6446 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6447
6448 if (!UniformBase) {
6449 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6450 Index = getValue(Ptr);
6451 Scale =
6452 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6453 }
6454
6455 EVT IdxVT = Index.getValueType();
6456 EVT EltTy = IdxVT.getVectorElementType();
6457 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6458 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
6459 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6460 }
6461
6462 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6463
6464 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6465 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6466 Ops, MMO, ISD::SIGNED_SCALED);
6467
6468 setValue(&I, Histogram);
6469 DAG.setRoot(Histogram);
6470}
6471
6472void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6473 unsigned Intrinsic) {
6474 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6475 "Tried lowering invalid vector extract last");
6476 SDLoc sdl = getCurSDLoc();
6477 const DataLayout &Layout = DAG.getDataLayout();
6478 SDValue Data = getValue(I.getOperand(0));
6479 SDValue Mask = getValue(I.getOperand(1));
6480
6481 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6482 EVT ResVT = TLI.getValueType(Layout, I.getType());
6483
6484 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6485 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6486 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6487
6488 Value *Default = I.getOperand(2);
6490 SDValue PassThru = getValue(Default);
6491 EVT BoolVT = Mask.getValueType().getScalarType();
6492 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6493 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6494 }
6495
6496 setValue(&I, Result);
6497}
6498
6499/// Lower the call to the specified intrinsic function.
6500void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6501 unsigned Intrinsic) {
6502 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6503 SDLoc sdl = getCurSDLoc();
6504 DebugLoc dl = getCurDebugLoc();
6505 SDValue Res;
6506
6507 SDNodeFlags Flags;
6508 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6509 Flags.copyFMF(*FPOp);
6510
6511 switch (Intrinsic) {
6512 default:
6513 // By default, turn this into a target intrinsic node.
6514 visitTargetIntrinsic(I, Intrinsic);
6515 return;
6516 case Intrinsic::vscale: {
6517 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6518 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6519 return;
6520 }
6521 case Intrinsic::vastart: visitVAStart(I); return;
6522 case Intrinsic::vaend: visitVAEnd(I); return;
6523 case Intrinsic::vacopy: visitVACopy(I); return;
6524 case Intrinsic::returnaddress:
6525 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6526 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6527 getValue(I.getArgOperand(0))));
6528 return;
6529 case Intrinsic::addressofreturnaddress:
6530 setValue(&I,
6531 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6532 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6533 return;
6534 case Intrinsic::sponentry:
6535 setValue(&I,
6536 DAG.getNode(ISD::SPONENTRY, sdl,
6537 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6538 return;
6539 case Intrinsic::frameaddress:
6540 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6541 TLI.getFrameIndexTy(DAG.getDataLayout()),
6542 getValue(I.getArgOperand(0))));
6543 return;
6544 case Intrinsic::read_volatile_register:
6545 case Intrinsic::read_register: {
6546 Value *Reg = I.getArgOperand(0);
6547 SDValue Chain = getRoot();
6549 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6550 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6551 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6552 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6553 setValue(&I, Res);
6554 DAG.setRoot(Res.getValue(1));
6555 return;
6556 }
6557 case Intrinsic::write_register: {
6558 Value *Reg = I.getArgOperand(0);
6559 Value *RegValue = I.getArgOperand(1);
6560 SDValue Chain = getRoot();
6562 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6563 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6564 RegName, getValue(RegValue)));
6565 return;
6566 }
6567 case Intrinsic::memcpy:
6568 case Intrinsic::memcpy_inline: {
6569 const auto &MCI = cast<MemCpyInst>(I);
6570 SDValue Dst = getValue(I.getArgOperand(0));
6571 SDValue Src = getValue(I.getArgOperand(1));
6572 SDValue Size = getValue(I.getArgOperand(2));
6573 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6574 "memcpy_inline needs constant size");
6575 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6576 Align DstAlign = MCI.getDestAlign().valueOrOne();
6577 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6578 Align Alignment = std::min(DstAlign, SrcAlign);
6579 bool isVol = MCI.isVolatile();
6580 // FIXME: Support passing different dest/src alignments to the memcpy DAG
6581 // node.
6582 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6583 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol,
6584 MCI.isForceInlined(), &I, std::nullopt,
6585 MachinePointerInfo(I.getArgOperand(0)),
6586 MachinePointerInfo(I.getArgOperand(1)),
6587 I.getAAMetadata(), BatchAA);
6588 updateDAGForMaybeTailCall(MC);
6589 return;
6590 }
6591 case Intrinsic::memset:
6592 case Intrinsic::memset_inline: {
6593 const auto &MSII = cast<MemSetInst>(I);
6594 SDValue Dst = getValue(I.getArgOperand(0));
6595 SDValue Value = getValue(I.getArgOperand(1));
6596 SDValue Size = getValue(I.getArgOperand(2));
6597 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6598 "memset_inline needs constant size");
6599 // @llvm.memset defines 0 and 1 to both mean no alignment.
6600 Align DstAlign = MSII.getDestAlign().valueOrOne();
6601 bool isVol = MSII.isVolatile();
6602 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6603 SDValue MC = DAG.getMemset(
6604 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6605 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6606 updateDAGForMaybeTailCall(MC);
6607 return;
6608 }
6609 case Intrinsic::memmove: {
6610 const auto &MMI = cast<MemMoveInst>(I);
6611 SDValue Op1 = getValue(I.getArgOperand(0));
6612 SDValue Op2 = getValue(I.getArgOperand(1));
6613 SDValue Op3 = getValue(I.getArgOperand(2));
6614 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6615 Align DstAlign = MMI.getDestAlign().valueOrOne();
6616 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6617 Align Alignment = std::min(DstAlign, SrcAlign);
6618 bool isVol = MMI.isVolatile();
6619 // FIXME: Support passing different dest/src alignments to the memmove DAG
6620 // node.
6621 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6622 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, &I,
6623 /* OverrideTailCall */ std::nullopt,
6624 MachinePointerInfo(I.getArgOperand(0)),
6625 MachinePointerInfo(I.getArgOperand(1)),
6626 I.getAAMetadata(), BatchAA);
6627 updateDAGForMaybeTailCall(MM);
6628 return;
6629 }
6630 case Intrinsic::memcpy_element_unordered_atomic: {
6631 auto &MI = cast<AnyMemCpyInst>(I);
6632 SDValue Dst = getValue(MI.getRawDest());
6633 SDValue Src = getValue(MI.getRawSource());
6634 SDValue Length = getValue(MI.getLength());
6635
6636 Type *LengthTy = MI.getLength()->getType();
6637 unsigned ElemSz = MI.getElementSizeInBytes();
6638 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6639 SDValue MC =
6640 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6641 isTC, MachinePointerInfo(MI.getRawDest()),
6642 MachinePointerInfo(MI.getRawSource()));
6643 updateDAGForMaybeTailCall(MC);
6644 return;
6645 }
6646 case Intrinsic::memmove_element_unordered_atomic: {
6647 auto &MI = cast<AnyMemMoveInst>(I);
6648 SDValue Dst = getValue(MI.getRawDest());
6649 SDValue Src = getValue(MI.getRawSource());
6650 SDValue Length = getValue(MI.getLength());
6651
6652 Type *LengthTy = MI.getLength()->getType();
6653 unsigned ElemSz = MI.getElementSizeInBytes();
6654 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6655 SDValue MC =
6656 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6657 isTC, MachinePointerInfo(MI.getRawDest()),
6658 MachinePointerInfo(MI.getRawSource()));
6659 updateDAGForMaybeTailCall(MC);
6660 return;
6661 }
6662 case Intrinsic::memset_element_unordered_atomic: {
6663 auto &MI = cast<AnyMemSetInst>(I);
6664 SDValue Dst = getValue(MI.getRawDest());
6665 SDValue Val = getValue(MI.getValue());
6666 SDValue Length = getValue(MI.getLength());
6667
6668 Type *LengthTy = MI.getLength()->getType();
6669 unsigned ElemSz = MI.getElementSizeInBytes();
6670 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6671 SDValue MC =
6672 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6673 isTC, MachinePointerInfo(MI.getRawDest()));
6674 updateDAGForMaybeTailCall(MC);
6675 return;
6676 }
6677 case Intrinsic::call_preallocated_setup: {
6678 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6679 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6680 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6681 getRoot(), SrcValue);
6682 setValue(&I, Res);
6683 DAG.setRoot(Res);
6684 return;
6685 }
6686 case Intrinsic::call_preallocated_arg: {
6687 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6688 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6689 SDValue Ops[3];
6690 Ops[0] = getRoot();
6691 Ops[1] = SrcValue;
6692 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6693 MVT::i32); // arg index
6694 SDValue Res = DAG.getNode(
6695 ISD::PREALLOCATED_ARG, sdl,
6696 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6697 setValue(&I, Res);
6698 DAG.setRoot(Res.getValue(1));
6699 return;
6700 }
6701
6702 case Intrinsic::eh_typeid_for: {
6703 // Find the type id for the given typeinfo.
6704 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6705 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6706 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6707 setValue(&I, Res);
6708 return;
6709 }
6710
6711 case Intrinsic::eh_return_i32:
6712 case Intrinsic::eh_return_i64:
6713 DAG.getMachineFunction().setCallsEHReturn(true);
6714 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6715 MVT::Other,
6717 getValue(I.getArgOperand(0)),
6718 getValue(I.getArgOperand(1))));
6719 return;
6720 case Intrinsic::eh_unwind_init:
6721 DAG.getMachineFunction().setCallsUnwindInit(true);
6722 return;
6723 case Intrinsic::eh_dwarf_cfa:
6724 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6725 TLI.getPointerTy(DAG.getDataLayout()),
6726 getValue(I.getArgOperand(0))));
6727 return;
6728 case Intrinsic::eh_sjlj_callsite: {
6729 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6730 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6731
6732 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6733 return;
6734 }
6735 case Intrinsic::eh_sjlj_functioncontext: {
6736 // Get and store the index of the function context.
6737 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6738 AllocaInst *FnCtx =
6739 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6740 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6742 return;
6743 }
6744 case Intrinsic::eh_sjlj_setjmp: {
6745 SDValue Ops[2];
6746 Ops[0] = getRoot();
6747 Ops[1] = getValue(I.getArgOperand(0));
6748 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6749 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6750 setValue(&I, Op.getValue(0));
6751 DAG.setRoot(Op.getValue(1));
6752 return;
6753 }
6754 case Intrinsic::eh_sjlj_longjmp:
6755 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6756 getRoot(), getValue(I.getArgOperand(0))));
6757 return;
6758 case Intrinsic::eh_sjlj_setup_dispatch:
6759 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6760 getRoot()));
6761 return;
6762 case Intrinsic::masked_gather:
6763 visitMaskedGather(I);
6764 return;
6765 case Intrinsic::masked_load:
6766 visitMaskedLoad(I);
6767 return;
6768 case Intrinsic::masked_scatter:
6769 visitMaskedScatter(I);
6770 return;
6771 case Intrinsic::masked_store:
6772 visitMaskedStore(I);
6773 return;
6774 case Intrinsic::masked_expandload:
6775 visitMaskedLoad(I, true /* IsExpanding */);
6776 return;
6777 case Intrinsic::masked_compressstore:
6778 visitMaskedStore(I, true /* IsCompressing */);
6779 return;
6780 case Intrinsic::powi:
6781 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6782 getValue(I.getArgOperand(1)), DAG));
6783 return;
6784 case Intrinsic::log:
6785 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6786 return;
6787 case Intrinsic::log2:
6788 setValue(&I,
6789 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6790 return;
6791 case Intrinsic::log10:
6792 setValue(&I,
6793 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6794 return;
6795 case Intrinsic::exp:
6796 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6797 return;
6798 case Intrinsic::exp2:
6799 setValue(&I,
6800 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6801 return;
6802 case Intrinsic::pow:
6803 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6804 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6805 return;
6806 case Intrinsic::sqrt:
6807 case Intrinsic::fabs:
6808 case Intrinsic::sin:
6809 case Intrinsic::cos:
6810 case Intrinsic::tan:
6811 case Intrinsic::asin:
6812 case Intrinsic::acos:
6813 case Intrinsic::atan:
6814 case Intrinsic::sinh:
6815 case Intrinsic::cosh:
6816 case Intrinsic::tanh:
6817 case Intrinsic::exp10:
6818 case Intrinsic::floor:
6819 case Intrinsic::ceil:
6820 case Intrinsic::trunc:
6821 case Intrinsic::rint:
6822 case Intrinsic::nearbyint:
6823 case Intrinsic::round:
6824 case Intrinsic::roundeven:
6825 case Intrinsic::canonicalize: {
6826 unsigned Opcode;
6827 // clang-format off
6828 switch (Intrinsic) {
6829 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6830 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6831 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6832 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6833 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6834 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6835 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6836 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6837 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6838 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6839 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6840 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6841 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6842 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6843 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6844 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6845 case Intrinsic::rint: Opcode = ISD::FRINT; break;
6846 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6847 case Intrinsic::round: Opcode = ISD::FROUND; break;
6848 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6849 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6850 }
6851 // clang-format on
6852
6853 setValue(&I, DAG.getNode(Opcode, sdl,
6854 getValue(I.getArgOperand(0)).getValueType(),
6855 getValue(I.getArgOperand(0)), Flags));
6856 return;
6857 }
6858 case Intrinsic::atan2:
6859 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
6860 getValue(I.getArgOperand(0)).getValueType(),
6861 getValue(I.getArgOperand(0)),
6862 getValue(I.getArgOperand(1)), Flags));
6863 return;
6864 case Intrinsic::lround:
6865 case Intrinsic::llround:
6866 case Intrinsic::lrint:
6867 case Intrinsic::llrint: {
6868 unsigned Opcode;
6869 // clang-format off
6870 switch (Intrinsic) {
6871 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6872 case Intrinsic::lround: Opcode = ISD::LROUND; break;
6873 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6874 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
6875 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
6876 }
6877 // clang-format on
6878
6879 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6880 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6881 getValue(I.getArgOperand(0))));
6882 return;
6883 }
6884 case Intrinsic::minnum:
6885 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6886 getValue(I.getArgOperand(0)).getValueType(),
6887 getValue(I.getArgOperand(0)),
6888 getValue(I.getArgOperand(1)), Flags));
6889 return;
6890 case Intrinsic::maxnum:
6891 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6892 getValue(I.getArgOperand(0)).getValueType(),
6893 getValue(I.getArgOperand(0)),
6894 getValue(I.getArgOperand(1)), Flags));
6895 return;
6896 case Intrinsic::minimum:
6897 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
6898 getValue(I.getArgOperand(0)).getValueType(),
6899 getValue(I.getArgOperand(0)),
6900 getValue(I.getArgOperand(1)), Flags));
6901 return;
6902 case Intrinsic::maximum:
6903 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
6904 getValue(I.getArgOperand(0)).getValueType(),
6905 getValue(I.getArgOperand(0)),
6906 getValue(I.getArgOperand(1)), Flags));
6907 return;
6908 case Intrinsic::minimumnum:
6909 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
6910 getValue(I.getArgOperand(0)).getValueType(),
6911 getValue(I.getArgOperand(0)),
6912 getValue(I.getArgOperand(1)), Flags));
6913 return;
6914 case Intrinsic::maximumnum:
6915 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
6916 getValue(I.getArgOperand(0)).getValueType(),
6917 getValue(I.getArgOperand(0)),
6918 getValue(I.getArgOperand(1)), Flags));
6919 return;
6920 case Intrinsic::copysign:
6921 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
6922 getValue(I.getArgOperand(0)).getValueType(),
6923 getValue(I.getArgOperand(0)),
6924 getValue(I.getArgOperand(1)), Flags));
6925 return;
6926 case Intrinsic::ldexp:
6927 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
6928 getValue(I.getArgOperand(0)).getValueType(),
6929 getValue(I.getArgOperand(0)),
6930 getValue(I.getArgOperand(1)), Flags));
6931 return;
6932 case Intrinsic::modf:
6933 case Intrinsic::sincos:
6934 case Intrinsic::sincospi:
6935 case Intrinsic::frexp: {
6936 unsigned Opcode;
6937 switch (Intrinsic) {
6938 default:
6939 llvm_unreachable("unexpected intrinsic");
6940 case Intrinsic::sincos:
6941 Opcode = ISD::FSINCOS;
6942 break;
6943 case Intrinsic::sincospi:
6944 Opcode = ISD::FSINCOSPI;
6945 break;
6946 case Intrinsic::modf:
6947 Opcode = ISD::FMODF;
6948 break;
6949 case Intrinsic::frexp:
6950 Opcode = ISD::FFREXP;
6951 break;
6952 }
6953 SmallVector<EVT, 2> ValueVTs;
6954 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
6955 SDVTList VTs = DAG.getVTList(ValueVTs);
6956 setValue(
6957 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
6958 return;
6959 }
6960 case Intrinsic::arithmetic_fence: {
6961 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
6962 getValue(I.getArgOperand(0)).getValueType(),
6963 getValue(I.getArgOperand(0)), Flags));
6964 return;
6965 }
6966 case Intrinsic::fma:
6967 setValue(&I, DAG.getNode(
6968 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
6969 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
6970 getValue(I.getArgOperand(2)), Flags));
6971 return;
6972#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
6973 case Intrinsic::INTRINSIC:
6974#include "llvm/IR/ConstrainedOps.def"
6975 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
6976 return;
6977#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
6978#include "llvm/IR/VPIntrinsics.def"
6979 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
6980 return;
6981 case Intrinsic::fptrunc_round: {
6982 // Get the last argument, the metadata and convert it to an integer in the
6983 // call
6984 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
6985 std::optional<RoundingMode> RoundMode =
6986 convertStrToRoundingMode(cast<MDString>(MD)->getString());
6987
6988 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6989
6990 // Propagate fast-math-flags from IR to node(s).
6991 SDNodeFlags Flags;
6992 Flags.copyFMF(*cast<FPMathOperator>(&I));
6993 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
6994
6996 Result = DAG.getNode(
6997 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
6998 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
6999 setValue(&I, Result);
7000
7001 return;
7002 }
7003 case Intrinsic::fmuladd: {
7004 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7005 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
7006 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
7007 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7008 getValue(I.getArgOperand(0)).getValueType(),
7009 getValue(I.getArgOperand(0)),
7010 getValue(I.getArgOperand(1)),
7011 getValue(I.getArgOperand(2)), Flags));
7012 } else if (TLI.isOperationLegalOrCustom(ISD::FMULADD, VT)) {
7013 // TODO: Support splitting the vector.
7014 setValue(&I, DAG.getNode(ISD::FMULADD, sdl,
7015 getValue(I.getArgOperand(0)).getValueType(),
7016 getValue(I.getArgOperand(0)),
7017 getValue(I.getArgOperand(1)),
7018 getValue(I.getArgOperand(2)), Flags));
7019 } else {
7020 // TODO: Intrinsic calls should have fast-math-flags.
7021 SDValue Mul = DAG.getNode(
7022 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7023 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7024 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7025 getValue(I.getArgOperand(0)).getValueType(),
7026 Mul, getValue(I.getArgOperand(2)), Flags);
7027 setValue(&I, Add);
7028 }
7029 return;
7030 }
7031 case Intrinsic::convert_to_fp16:
7032 setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16,
7033 DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16,
7034 getValue(I.getArgOperand(0)),
7035 DAG.getTargetConstant(0, sdl,
7036 MVT::i32))));
7037 return;
7038 case Intrinsic::convert_from_fp16:
7039 setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl,
7040 TLI.getValueType(DAG.getDataLayout(), I.getType()),
7041 DAG.getNode(ISD::BITCAST, sdl, MVT::f16,
7042 getValue(I.getArgOperand(0)))));
7043 return;
7044 case Intrinsic::fptosi_sat: {
7045 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7046 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7047 getValue(I.getArgOperand(0)),
7048 DAG.getValueType(VT.getScalarType())));
7049 return;
7050 }
7051 case Intrinsic::fptoui_sat: {
7052 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7053 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7054 getValue(I.getArgOperand(0)),
7055 DAG.getValueType(VT.getScalarType())));
7056 return;
7057 }
7058 case Intrinsic::set_rounding:
7059 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7060 {getRoot(), getValue(I.getArgOperand(0))});
7061 setValue(&I, Res);
7062 DAG.setRoot(Res.getValue(0));
7063 return;
7064 case Intrinsic::is_fpclass: {
7065 const DataLayout DLayout = DAG.getDataLayout();
7066 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7067 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7068 FPClassTest Test = static_cast<FPClassTest>(
7069 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7070 MachineFunction &MF = DAG.getMachineFunction();
7071 const Function &F = MF.getFunction();
7072 SDValue Op = getValue(I.getArgOperand(0));
7073 SDNodeFlags Flags;
7074 Flags.setNoFPExcept(
7075 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7076 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7077 // expansion can use illegal types. Making expansion early allows
7078 // legalizing these types prior to selection.
7079 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7080 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7081 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7082 setValue(&I, Result);
7083 return;
7084 }
7085
7086 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7087 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7088 setValue(&I, V);
7089 return;
7090 }
7091 case Intrinsic::get_fpenv: {
7092 const DataLayout DLayout = DAG.getDataLayout();
7093 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7094 Align TempAlign = DAG.getEVTAlign(EnvVT);
7095 SDValue Chain = getRoot();
7096 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7097 // and temporary storage in stack.
7098 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7099 Res = DAG.getNode(
7100 ISD::GET_FPENV, sdl,
7101 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7102 MVT::Other),
7103 Chain);
7104 } else {
7105 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7106 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7107 auto MPI =
7108 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7109 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7111 TempAlign);
7112 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7113 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7114 }
7115 setValue(&I, Res);
7116 DAG.setRoot(Res.getValue(1));
7117 return;
7118 }
7119 case Intrinsic::set_fpenv: {
7120 const DataLayout DLayout = DAG.getDataLayout();
7121 SDValue Env = getValue(I.getArgOperand(0));
7122 EVT EnvVT = Env.getValueType();
7123 Align TempAlign = DAG.getEVTAlign(EnvVT);
7124 SDValue Chain = getRoot();
7125 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7126 // environment from memory.
7127 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7128 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7129 } else {
7130 // Allocate space in stack, copy environment bits into it and use this
7131 // memory in SET_FPENV_MEM.
7132 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7133 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7134 auto MPI =
7135 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7136 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7138 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7140 TempAlign);
7141 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7142 }
7143 DAG.setRoot(Chain);
7144 return;
7145 }
7146 case Intrinsic::reset_fpenv:
7147 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7148 return;
7149 case Intrinsic::get_fpmode:
7150 Res = DAG.getNode(
7151 ISD::GET_FPMODE, sdl,
7152 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7153 MVT::Other),
7154 DAG.getRoot());
7155 setValue(&I, Res);
7156 DAG.setRoot(Res.getValue(1));
7157 return;
7158 case Intrinsic::set_fpmode:
7159 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7160 getValue(I.getArgOperand(0)));
7161 DAG.setRoot(Res);
7162 return;
7163 case Intrinsic::reset_fpmode: {
7164 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7165 DAG.setRoot(Res);
7166 return;
7167 }
7168 case Intrinsic::pcmarker: {
7169 SDValue Tmp = getValue(I.getArgOperand(0));
7170 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7171 return;
7172 }
7173 case Intrinsic::readcyclecounter: {
7174 SDValue Op = getRoot();
7175 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7176 DAG.getVTList(MVT::i64, MVT::Other), Op);
7177 setValue(&I, Res);
7178 DAG.setRoot(Res.getValue(1));
7179 return;
7180 }
7181 case Intrinsic::readsteadycounter: {
7182 SDValue Op = getRoot();
7183 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7184 DAG.getVTList(MVT::i64, MVT::Other), Op);
7185 setValue(&I, Res);
7186 DAG.setRoot(Res.getValue(1));
7187 return;
7188 }
7189 case Intrinsic::bitreverse:
7190 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7191 getValue(I.getArgOperand(0)).getValueType(),
7192 getValue(I.getArgOperand(0))));
7193 return;
7194 case Intrinsic::bswap:
7195 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7196 getValue(I.getArgOperand(0)).getValueType(),
7197 getValue(I.getArgOperand(0))));
7198 return;
7199 case Intrinsic::cttz: {
7200 SDValue Arg = getValue(I.getArgOperand(0));
7201 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7202 EVT Ty = Arg.getValueType();
7203 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
7204 sdl, Ty, Arg));
7205 return;
7206 }
7207 case Intrinsic::ctlz: {
7208 SDValue Arg = getValue(I.getArgOperand(0));
7209 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7210 EVT Ty = Arg.getValueType();
7211 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
7212 sdl, Ty, Arg));
7213 return;
7214 }
7215 case Intrinsic::ctpop: {
7216 SDValue Arg = getValue(I.getArgOperand(0));
7217 EVT Ty = Arg.getValueType();
7218 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7219 return;
7220 }
7221 case Intrinsic::fshl:
7222 case Intrinsic::fshr: {
7223 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7224 SDValue X = getValue(I.getArgOperand(0));
7225 SDValue Y = getValue(I.getArgOperand(1));
7226 SDValue Z = getValue(I.getArgOperand(2));
7227 EVT VT = X.getValueType();
7228
7229 if (X == Y) {
7230 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7231 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7232 } else {
7233 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7234 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7235 }
7236 return;
7237 }
7238 case Intrinsic::sadd_sat: {
7239 SDValue Op1 = getValue(I.getArgOperand(0));
7240 SDValue Op2 = getValue(I.getArgOperand(1));
7241 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7242 return;
7243 }
7244 case Intrinsic::uadd_sat: {
7245 SDValue Op1 = getValue(I.getArgOperand(0));
7246 SDValue Op2 = getValue(I.getArgOperand(1));
7247 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7248 return;
7249 }
7250 case Intrinsic::ssub_sat: {
7251 SDValue Op1 = getValue(I.getArgOperand(0));
7252 SDValue Op2 = getValue(I.getArgOperand(1));
7253 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7254 return;
7255 }
7256 case Intrinsic::usub_sat: {
7257 SDValue Op1 = getValue(I.getArgOperand(0));
7258 SDValue Op2 = getValue(I.getArgOperand(1));
7259 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7260 return;
7261 }
7262 case Intrinsic::sshl_sat: {
7263 SDValue Op1 = getValue(I.getArgOperand(0));
7264 SDValue Op2 = getValue(I.getArgOperand(1));
7265 setValue(&I, DAG.getNode(ISD::SSHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7266 return;
7267 }
7268 case Intrinsic::ushl_sat: {
7269 SDValue Op1 = getValue(I.getArgOperand(0));
7270 SDValue Op2 = getValue(I.getArgOperand(1));
7271 setValue(&I, DAG.getNode(ISD::USHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7272 return;
7273 }
7274 case Intrinsic::smul_fix:
7275 case Intrinsic::umul_fix:
7276 case Intrinsic::smul_fix_sat:
7277 case Intrinsic::umul_fix_sat: {
7278 SDValue Op1 = getValue(I.getArgOperand(0));
7279 SDValue Op2 = getValue(I.getArgOperand(1));
7280 SDValue Op3 = getValue(I.getArgOperand(2));
7281 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7282 Op1.getValueType(), Op1, Op2, Op3));
7283 return;
7284 }
7285 case Intrinsic::sdiv_fix:
7286 case Intrinsic::udiv_fix:
7287 case Intrinsic::sdiv_fix_sat:
7288 case Intrinsic::udiv_fix_sat: {
7289 SDValue Op1 = getValue(I.getArgOperand(0));
7290 SDValue Op2 = getValue(I.getArgOperand(1));
7291 SDValue Op3 = getValue(I.getArgOperand(2));
7293 Op1, Op2, Op3, DAG, TLI));
7294 return;
7295 }
7296 case Intrinsic::smax: {
7297 SDValue Op1 = getValue(I.getArgOperand(0));
7298 SDValue Op2 = getValue(I.getArgOperand(1));
7299 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7300 return;
7301 }
7302 case Intrinsic::smin: {
7303 SDValue Op1 = getValue(I.getArgOperand(0));
7304 SDValue Op2 = getValue(I.getArgOperand(1));
7305 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7306 return;
7307 }
7308 case Intrinsic::umax: {
7309 SDValue Op1 = getValue(I.getArgOperand(0));
7310 SDValue Op2 = getValue(I.getArgOperand(1));
7311 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7312 return;
7313 }
7314 case Intrinsic::umin: {
7315 SDValue Op1 = getValue(I.getArgOperand(0));
7316 SDValue Op2 = getValue(I.getArgOperand(1));
7317 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7318 return;
7319 }
7320 case Intrinsic::abs: {
7321 // TODO: Preserve "int min is poison" arg in SDAG?
7322 SDValue Op1 = getValue(I.getArgOperand(0));
7323 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7324 return;
7325 }
7326 case Intrinsic::scmp: {
7327 SDValue Op1 = getValue(I.getArgOperand(0));
7328 SDValue Op2 = getValue(I.getArgOperand(1));
7329 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7330 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7331 break;
7332 }
7333 case Intrinsic::ucmp: {
7334 SDValue Op1 = getValue(I.getArgOperand(0));
7335 SDValue Op2 = getValue(I.getArgOperand(1));
7336 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7337 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7338 break;
7339 }
7340 case Intrinsic::stacksave: {
7341 SDValue Op = getRoot();
7342 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7343 Res = DAG.getNode(ISD::STACKSAVE, sdl, DAG.getVTList(VT, MVT::Other), Op);
7344 setValue(&I, Res);
7345 DAG.setRoot(Res.getValue(1));
7346 return;
7347 }
7348 case Intrinsic::stackrestore:
7349 Res = getValue(I.getArgOperand(0));
7350 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7351 return;
7352 case Intrinsic::get_dynamic_area_offset: {
7353 SDValue Op = getRoot();
7354 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7355 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7356 Op);
7357 DAG.setRoot(Op);
7358 setValue(&I, Res);
7359 return;
7360 }
7361 case Intrinsic::stackguard: {
7362 MachineFunction &MF = DAG.getMachineFunction();
7363 const Module &M = *MF.getFunction().getParent();
7364 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7365 SDValue Chain = getRoot();
7366 if (TLI.useLoadStackGuardNode(M)) {
7367 Res = getLoadStackGuard(DAG, sdl, Chain);
7368 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7369 } else {
7370 const Value *Global = TLI.getSDagStackGuard(M);
7371 if (!Global) {
7372 LLVMContext &Ctx = *DAG.getContext();
7373 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
7374 setValue(&I, DAG.getPOISON(PtrTy));
7375 return;
7376 }
7377
7378 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7379 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7380 MachinePointerInfo(Global, 0), Align,
7382 }
7383 if (TLI.useStackGuardXorFP())
7384 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7385 DAG.setRoot(Chain);
7386 setValue(&I, Res);
7387 return;
7388 }
7389 case Intrinsic::stackprotector: {
7390 // Emit code into the DAG to store the stack guard onto the stack.
7391 MachineFunction &MF = DAG.getMachineFunction();
7392 MachineFrameInfo &MFI = MF.getFrameInfo();
7393 const Module &M = *MF.getFunction().getParent();
7394 SDValue Src, Chain = getRoot();
7395
7396 if (TLI.useLoadStackGuardNode(M))
7397 Src = getLoadStackGuard(DAG, sdl, Chain);
7398 else
7399 Src = getValue(I.getArgOperand(0)); // The guard's value.
7400
7401 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7402
7403 int FI = FuncInfo.StaticAllocaMap[Slot];
7404 MFI.setStackProtectorIndex(FI);
7405 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7406
7407 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7408
7409 // Store the stack protector onto the stack.
7410 Res = DAG.getStore(
7411 Chain, sdl, Src, FIN,
7412 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7413 MaybeAlign(), MachineMemOperand::MOVolatile);
7414 setValue(&I, Res);
7415 DAG.setRoot(Res);
7416 return;
7417 }
7418 case Intrinsic::objectsize:
7419 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7420
7421 case Intrinsic::is_constant:
7422 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7423
7424 case Intrinsic::annotation:
7425 case Intrinsic::ptr_annotation:
7426 case Intrinsic::launder_invariant_group:
7427 case Intrinsic::strip_invariant_group:
7428 // Drop the intrinsic, but forward the value
7429 setValue(&I, getValue(I.getOperand(0)));
7430 return;
7431
7432 case Intrinsic::type_test:
7433 case Intrinsic::public_type_test:
7434 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7435 return;
7436
7437 case Intrinsic::assume:
7438 case Intrinsic::experimental_noalias_scope_decl:
7439 case Intrinsic::var_annotation:
7440 case Intrinsic::sideeffect:
7441 // Discard annotate attributes, noalias scope declarations, assumptions, and
7442 // artificial side-effects.
7443 return;
7444
7445 case Intrinsic::codeview_annotation: {
7446 // Emit a label associated with this metadata.
7447 MachineFunction &MF = DAG.getMachineFunction();
7448 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7449 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7450 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7451 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7452 DAG.setRoot(Res);
7453 return;
7454 }
7455
7456 case Intrinsic::init_trampoline: {
7457 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7458
7459 SDValue Ops[6];
7460 Ops[0] = getRoot();
7461 Ops[1] = getValue(I.getArgOperand(0));
7462 Ops[2] = getValue(I.getArgOperand(1));
7463 Ops[3] = getValue(I.getArgOperand(2));
7464 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7465 Ops[5] = DAG.getSrcValue(F);
7466
7467 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7468
7469 DAG.setRoot(Res);
7470 return;
7471 }
7472 case Intrinsic::adjust_trampoline:
7473 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7474 TLI.getPointerTy(DAG.getDataLayout()),
7475 getValue(I.getArgOperand(0))));
7476 return;
7477 case Intrinsic::gcroot: {
7478 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7479 "only valid in functions with gc specified, enforced by Verifier");
7480 assert(GFI && "implied by previous");
7481 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7482 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7483
7484 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7485 GFI->addStackRoot(FI->getIndex(), TypeMap);
7486 return;
7487 }
7488 case Intrinsic::gcread:
7489 case Intrinsic::gcwrite:
7490 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7491 case Intrinsic::get_rounding:
7492 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7493 setValue(&I, Res);
7494 DAG.setRoot(Res.getValue(1));
7495 return;
7496
7497 case Intrinsic::expect:
7498 case Intrinsic::expect_with_probability:
7499 // Just replace __builtin_expect(exp, c) and
7500 // __builtin_expect_with_probability(exp, c, p) with EXP.
7501 setValue(&I, getValue(I.getArgOperand(0)));
7502 return;
7503
7504 case Intrinsic::ubsantrap:
7505 case Intrinsic::debugtrap:
7506 case Intrinsic::trap: {
7507 StringRef TrapFuncName =
7508 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7509 if (TrapFuncName.empty()) {
7510 switch (Intrinsic) {
7511 case Intrinsic::trap:
7512 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7513 break;
7514 case Intrinsic::debugtrap:
7515 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7516 break;
7517 case Intrinsic::ubsantrap:
7518 DAG.setRoot(DAG.getNode(
7519 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7520 DAG.getTargetConstant(
7521 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7522 MVT::i32)));
7523 break;
7524 default: llvm_unreachable("unknown trap intrinsic");
7525 }
7526 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7527 I.hasFnAttr(Attribute::NoMerge));
7528 return;
7529 }
7531 if (Intrinsic == Intrinsic::ubsantrap) {
7532 Value *Arg = I.getArgOperand(0);
7533 Args.emplace_back(Arg, getValue(Arg));
7534 }
7535
7536 TargetLowering::CallLoweringInfo CLI(DAG);
7537 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7538 CallingConv::C, I.getType(),
7539 DAG.getExternalSymbol(TrapFuncName.data(),
7540 TLI.getPointerTy(DAG.getDataLayout())),
7541 std::move(Args));
7542 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7543 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7544 DAG.setRoot(Result.second);
7545 return;
7546 }
7547
7548 case Intrinsic::allow_runtime_check:
7549 case Intrinsic::allow_ubsan_check:
7550 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7551 return;
7552
7553 case Intrinsic::uadd_with_overflow:
7554 case Intrinsic::sadd_with_overflow:
7555 case Intrinsic::usub_with_overflow:
7556 case Intrinsic::ssub_with_overflow:
7557 case Intrinsic::umul_with_overflow:
7558 case Intrinsic::smul_with_overflow: {
7560 switch (Intrinsic) {
7561 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7562 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7563 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7564 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7565 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7566 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7567 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7568 }
7569 SDValue Op1 = getValue(I.getArgOperand(0));
7570 SDValue Op2 = getValue(I.getArgOperand(1));
7571
7572 EVT ResultVT = Op1.getValueType();
7573 EVT OverflowVT = MVT::i1;
7574 if (ResultVT.isVector())
7575 OverflowVT = EVT::getVectorVT(
7576 *Context, OverflowVT, ResultVT.getVectorElementCount());
7577
7578 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7579 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7580 return;
7581 }
7582 case Intrinsic::prefetch: {
7583 SDValue Ops[5];
7584 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7586 Ops[0] = DAG.getRoot();
7587 Ops[1] = getValue(I.getArgOperand(0));
7588 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7589 MVT::i32);
7590 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7591 MVT::i32);
7592 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7593 MVT::i32);
7594 SDValue Result = DAG.getMemIntrinsicNode(
7595 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7596 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7597 /* align */ std::nullopt, Flags);
7598
7599 // Chain the prefetch in parallel with any pending loads, to stay out of
7600 // the way of later optimizations.
7601 PendingLoads.push_back(Result);
7602 Result = getRoot();
7603 DAG.setRoot(Result);
7604 return;
7605 }
7606 case Intrinsic::lifetime_start:
7607 case Intrinsic::lifetime_end: {
7608 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7609 // Stack coloring is not enabled in O0, discard region information.
7610 if (TM.getOptLevel() == CodeGenOptLevel::None)
7611 return;
7612
7613 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7614 if (!LifetimeObject)
7615 return;
7616
7617 // First check that the Alloca is static, otherwise it won't have a
7618 // valid frame index.
7619 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7620 if (SI == FuncInfo.StaticAllocaMap.end())
7621 return;
7622
7623 const int FrameIndex = SI->second;
7624 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7625 DAG.setRoot(Res);
7626 return;
7627 }
7628 case Intrinsic::pseudoprobe: {
7629 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7630 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7631 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7632 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7633 DAG.setRoot(Res);
7634 return;
7635 }
7636 case Intrinsic::invariant_start:
7637 // Discard region information.
7638 setValue(&I,
7639 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7640 return;
7641 case Intrinsic::invariant_end:
7642 // Discard region information.
7643 return;
7644 case Intrinsic::clear_cache: {
7645 SDValue InputChain = DAG.getRoot();
7646 SDValue StartVal = getValue(I.getArgOperand(0));
7647 SDValue EndVal = getValue(I.getArgOperand(1));
7648 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7649 {InputChain, StartVal, EndVal});
7650 setValue(&I, Res);
7651 DAG.setRoot(Res);
7652 return;
7653 }
7654 case Intrinsic::donothing:
7655 case Intrinsic::seh_try_begin:
7656 case Intrinsic::seh_scope_begin:
7657 case Intrinsic::seh_try_end:
7658 case Intrinsic::seh_scope_end:
7659 // ignore
7660 return;
7661 case Intrinsic::experimental_stackmap:
7662 visitStackmap(I);
7663 return;
7664 case Intrinsic::experimental_patchpoint_void:
7665 case Intrinsic::experimental_patchpoint:
7666 visitPatchpoint(I);
7667 return;
7668 case Intrinsic::experimental_gc_statepoint:
7670 return;
7671 case Intrinsic::experimental_gc_result:
7672 visitGCResult(cast<GCResultInst>(I));
7673 return;
7674 case Intrinsic::experimental_gc_relocate:
7675 visitGCRelocate(cast<GCRelocateInst>(I));
7676 return;
7677 case Intrinsic::instrprof_cover:
7678 llvm_unreachable("instrprof failed to lower a cover");
7679 case Intrinsic::instrprof_increment:
7680 llvm_unreachable("instrprof failed to lower an increment");
7681 case Intrinsic::instrprof_timestamp:
7682 llvm_unreachable("instrprof failed to lower a timestamp");
7683 case Intrinsic::instrprof_value_profile:
7684 llvm_unreachable("instrprof failed to lower a value profiling call");
7685 case Intrinsic::instrprof_mcdc_parameters:
7686 llvm_unreachable("instrprof failed to lower mcdc parameters");
7687 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7688 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7689 case Intrinsic::localescape: {
7690 MachineFunction &MF = DAG.getMachineFunction();
7691 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7692
7693 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7694 // is the same on all targets.
7695 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7696 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7697 if (isa<ConstantPointerNull>(Arg))
7698 continue; // Skip null pointers. They represent a hole in index space.
7699 AllocaInst *Slot = cast<AllocaInst>(Arg);
7700 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7701 "can only escape static allocas");
7702 int FI = FuncInfo.StaticAllocaMap[Slot];
7703 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7705 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7706 TII->get(TargetOpcode::LOCAL_ESCAPE))
7707 .addSym(FrameAllocSym)
7708 .addFrameIndex(FI);
7709 }
7710
7711 return;
7712 }
7713
7714 case Intrinsic::localrecover: {
7715 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7716 MachineFunction &MF = DAG.getMachineFunction();
7717
7718 // Get the symbol that defines the frame offset.
7719 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7720 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7721 unsigned IdxVal =
7722 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7723 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7725
7726 Value *FP = I.getArgOperand(1);
7727 SDValue FPVal = getValue(FP);
7728 EVT PtrVT = FPVal.getValueType();
7729
7730 // Create a MCSymbol for the label to avoid any target lowering
7731 // that would make this PC relative.
7732 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7733 SDValue OffsetVal =
7734 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7735
7736 // Add the offset to the FP.
7737 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7738 setValue(&I, Add);
7739
7740 return;
7741 }
7742
7743 case Intrinsic::fake_use: {
7744 Value *V = I.getArgOperand(0);
7745 SDValue Ops[2];
7746 // For Values not declared or previously used in this basic block, the
7747 // NodeMap will not have an entry, and `getValue` will assert if V has no
7748 // valid register value.
7749 auto FakeUseValue = [&]() -> SDValue {
7750 SDValue &N = NodeMap[V];
7751 if (N.getNode())
7752 return N;
7753
7754 // If there's a virtual register allocated and initialized for this
7755 // value, use it.
7756 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7757 return copyFromReg;
7758 // FIXME: Do we want to preserve constants? It seems pointless.
7759 if (isa<Constant>(V))
7760 return getValue(V);
7761 return SDValue();
7762 }();
7763 if (!FakeUseValue || FakeUseValue.isUndef())
7764 return;
7765 Ops[0] = getRoot();
7766 Ops[1] = FakeUseValue;
7767 // Also, do not translate a fake use with an undef operand, or any other
7768 // empty SDValues.
7769 if (!Ops[1] || Ops[1].isUndef())
7770 return;
7771 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7772 return;
7773 }
7774
7775 case Intrinsic::eh_exceptionpointer:
7776 case Intrinsic::eh_exceptioncode: {
7777 // Get the exception pointer vreg, copy from it, and resize it to fit.
7778 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7779 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7780 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7781 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7782 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7783 if (Intrinsic == Intrinsic::eh_exceptioncode)
7784 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7785 setValue(&I, N);
7786 return;
7787 }
7788 case Intrinsic::xray_customevent: {
7789 // Here we want to make sure that the intrinsic behaves as if it has a
7790 // specific calling convention.
7791 const auto &Triple = DAG.getTarget().getTargetTriple();
7792 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7793 return;
7794
7796
7797 // We want to say that we always want the arguments in registers.
7798 SDValue LogEntryVal = getValue(I.getArgOperand(0));
7799 SDValue StrSizeVal = getValue(I.getArgOperand(1));
7800 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7801 SDValue Chain = getRoot();
7802 Ops.push_back(LogEntryVal);
7803 Ops.push_back(StrSizeVal);
7804 Ops.push_back(Chain);
7805
7806 // We need to enforce the calling convention for the callsite, so that
7807 // argument ordering is enforced correctly, and that register allocation can
7808 // see that some registers may be assumed clobbered and have to preserve
7809 // them across calls to the intrinsic.
7810 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7811 sdl, NodeTys, Ops);
7812 SDValue patchableNode = SDValue(MN, 0);
7813 DAG.setRoot(patchableNode);
7814 setValue(&I, patchableNode);
7815 return;
7816 }
7817 case Intrinsic::xray_typedevent: {
7818 // Here we want to make sure that the intrinsic behaves as if it has a
7819 // specific calling convention.
7820 const auto &Triple = DAG.getTarget().getTargetTriple();
7821 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7822 return;
7823
7825
7826 // We want to say that we always want the arguments in registers.
7827 // It's unclear to me how manipulating the selection DAG here forces callers
7828 // to provide arguments in registers instead of on the stack.
7829 SDValue LogTypeId = getValue(I.getArgOperand(0));
7830 SDValue LogEntryVal = getValue(I.getArgOperand(1));
7831 SDValue StrSizeVal = getValue(I.getArgOperand(2));
7832 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7833 SDValue Chain = getRoot();
7834 Ops.push_back(LogTypeId);
7835 Ops.push_back(LogEntryVal);
7836 Ops.push_back(StrSizeVal);
7837 Ops.push_back(Chain);
7838
7839 // We need to enforce the calling convention for the callsite, so that
7840 // argument ordering is enforced correctly, and that register allocation can
7841 // see that some registers may be assumed clobbered and have to preserve
7842 // them across calls to the intrinsic.
7843 MachineSDNode *MN = DAG.getMachineNode(
7844 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
7845 SDValue patchableNode = SDValue(MN, 0);
7846 DAG.setRoot(patchableNode);
7847 setValue(&I, patchableNode);
7848 return;
7849 }
7850 case Intrinsic::experimental_deoptimize:
7852 return;
7853 case Intrinsic::stepvector:
7854 visitStepVector(I);
7855 return;
7856 case Intrinsic::vector_reduce_fadd:
7857 case Intrinsic::vector_reduce_fmul:
7858 case Intrinsic::vector_reduce_add:
7859 case Intrinsic::vector_reduce_mul:
7860 case Intrinsic::vector_reduce_and:
7861 case Intrinsic::vector_reduce_or:
7862 case Intrinsic::vector_reduce_xor:
7863 case Intrinsic::vector_reduce_smax:
7864 case Intrinsic::vector_reduce_smin:
7865 case Intrinsic::vector_reduce_umax:
7866 case Intrinsic::vector_reduce_umin:
7867 case Intrinsic::vector_reduce_fmax:
7868 case Intrinsic::vector_reduce_fmin:
7869 case Intrinsic::vector_reduce_fmaximum:
7870 case Intrinsic::vector_reduce_fminimum:
7871 visitVectorReduce(I, Intrinsic);
7872 return;
7873
7874 case Intrinsic::icall_branch_funnel: {
7876 Ops.push_back(getValue(I.getArgOperand(0)));
7877
7878 int64_t Offset;
7880 I.getArgOperand(1), Offset, DAG.getDataLayout()));
7881 if (!Base)
7883 "llvm.icall.branch.funnel operand must be a GlobalValue");
7884 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
7885
7886 struct BranchFunnelTarget {
7887 int64_t Offset;
7889 };
7891
7892 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
7894 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
7895 if (ElemBase != Base)
7896 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
7897 "to the same GlobalValue");
7898
7899 SDValue Val = getValue(I.getArgOperand(Op + 1));
7900 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
7901 if (!GA)
7903 "llvm.icall.branch.funnel operand must be a GlobalValue");
7904 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
7905 GA->getGlobal(), sdl, Val.getValueType(),
7906 GA->getOffset())});
7907 }
7908 llvm::sort(Targets,
7909 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
7910 return T1.Offset < T2.Offset;
7911 });
7912
7913 for (auto &T : Targets) {
7914 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
7915 Ops.push_back(T.Target);
7916 }
7917
7918 Ops.push_back(DAG.getRoot()); // Chain
7919 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
7920 MVT::Other, Ops),
7921 0);
7922 DAG.setRoot(N);
7923 setValue(&I, N);
7924 HasTailCall = true;
7925 return;
7926 }
7927
7928 case Intrinsic::wasm_landingpad_index:
7929 // Information this intrinsic contained has been transferred to
7930 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
7931 // delete it now.
7932 return;
7933
7934 case Intrinsic::aarch64_settag:
7935 case Intrinsic::aarch64_settag_zero: {
7936 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
7937 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
7939 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
7940 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
7941 ZeroMemory);
7942 DAG.setRoot(Val);
7943 setValue(&I, Val);
7944 return;
7945 }
7946 case Intrinsic::amdgcn_cs_chain: {
7947 // At this point we don't care if it's amdgpu_cs_chain or
7948 // amdgpu_cs_chain_preserve.
7950
7951 Type *RetTy = I.getType();
7952 assert(RetTy->isVoidTy() && "Should not return");
7953
7954 SDValue Callee = getValue(I.getOperand(0));
7955
7956 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
7957 // We'll also tack the value of the EXEC mask at the end.
7959 Args.reserve(3);
7960
7961 for (unsigned Idx : {2, 3, 1}) {
7962 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
7963 I.getOperand(Idx)->getType());
7964 Arg.setAttributes(&I, Idx);
7965 Args.push_back(Arg);
7966 }
7967
7968 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
7969 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
7970 Args[2].IsInReg = true; // EXEC should be inreg
7971
7972 // Forward the flags and any additional arguments.
7973 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
7974 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
7975 I.getOperand(Idx)->getType());
7976 Arg.setAttributes(&I, Idx);
7977 Args.push_back(Arg);
7978 }
7979
7980 TargetLowering::CallLoweringInfo CLI(DAG);
7981 CLI.setDebugLoc(getCurSDLoc())
7982 .setChain(getRoot())
7983 .setCallee(CC, RetTy, Callee, std::move(Args))
7984 .setNoReturn(true)
7985 .setTailCall(true)
7986 .setConvergent(I.isConvergent());
7987 CLI.CB = &I;
7988 std::pair<SDValue, SDValue> Result =
7989 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
7990 (void)Result;
7991 assert(!Result.first.getNode() && !Result.second.getNode() &&
7992 "Should've lowered as tail call");
7993
7994 HasTailCall = true;
7995 return;
7996 }
7997 case Intrinsic::amdgcn_call_whole_wave: {
7999 bool isTailCall = I.isTailCall();
8000
8001 // The first argument is the callee. Skip it when assembling the call args.
8002 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
8003 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
8004 I.getArgOperand(Idx)->getType());
8005 Arg.setAttributes(&I, Idx);
8006
8007 // If we have an explicit sret argument that is an Instruction, (i.e., it
8008 // might point to function-local memory), we can't meaningfully tail-call.
8009 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
8010 isTailCall = false;
8011
8012 Args.push_back(Arg);
8013 }
8014
8015 SDValue ConvControlToken;
8016 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8017 auto *Token = Bundle->Inputs[0].get();
8018 ConvControlToken = getValue(Token);
8019 }
8020
8021 TargetLowering::CallLoweringInfo CLI(DAG);
8022 CLI.setDebugLoc(getCurSDLoc())
8023 .setChain(getRoot())
8024 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8025 getValue(I.getArgOperand(0)), std::move(Args))
8026 .setTailCall(isTailCall && canTailCall(I))
8027 .setIsPreallocated(
8028 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8029 .setConvergent(I.isConvergent())
8030 .setConvergenceControlToken(ConvControlToken);
8031 CLI.CB = &I;
8032
8033 std::pair<SDValue, SDValue> Result =
8034 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8035
8036 if (Result.first.getNode())
8037 setValue(&I, Result.first);
8038 return;
8039 }
8040 case Intrinsic::ptrmask: {
8041 SDValue Ptr = getValue(I.getOperand(0));
8042 SDValue Mask = getValue(I.getOperand(1));
8043
8044 // On arm64_32, pointers are 32 bits when stored in memory, but
8045 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8046 // match the index type, but the pointer is 64 bits, so the mask must be
8047 // zero-extended up to 64 bits to match the pointer.
8048 EVT PtrVT =
8049 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8050 EVT MemVT =
8051 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8052 assert(PtrVT == Ptr.getValueType());
8053 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8054 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8055 // 128-bit, so we have to pad the mask with ones for unused bits.
8056 auto HighOnes = DAG.getNode(
8057 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8058 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8059 PtrVT, sdl));
8060 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8061 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8062 } else if (Mask.getValueType() != PtrVT)
8063 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8064
8065 assert(Mask.getValueType() == PtrVT);
8066 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8067 return;
8068 }
8069 case Intrinsic::threadlocal_address: {
8070 setValue(&I, getValue(I.getOperand(0)));
8071 return;
8072 }
8073 case Intrinsic::get_active_lane_mask: {
8074 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8075 SDValue Index = getValue(I.getOperand(0));
8076 SDValue TripCount = getValue(I.getOperand(1));
8077 EVT ElementVT = Index.getValueType();
8078
8079 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8080 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8081 TripCount));
8082 return;
8083 }
8084
8085 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8086 CCVT.getVectorElementCount());
8087
8088 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8089 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8090 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8091 SDValue VectorInduction = DAG.getNode(
8092 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8093 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8094 VectorTripCount, ISD::CondCode::SETULT);
8095 setValue(&I, SetCC);
8096 return;
8097 }
8098 case Intrinsic::experimental_get_vector_length: {
8099 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8100 "Expected positive VF");
8101 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8102 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8103
8104 SDValue Count = getValue(I.getOperand(0));
8105 EVT CountVT = Count.getValueType();
8106
8107 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8108 visitTargetIntrinsic(I, Intrinsic);
8109 return;
8110 }
8111
8112 // Expand to a umin between the trip count and the maximum elements the type
8113 // can hold.
8114 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8115
8116 // Extend the trip count to at least the result VT.
8117 if (CountVT.bitsLT(VT)) {
8118 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8119 CountVT = VT;
8120 }
8121
8122 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8123 ElementCount::get(VF, IsScalable));
8124
8125 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8126 // Clip to the result type if needed.
8127 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8128
8129 setValue(&I, Trunc);
8130 return;
8131 }
8132 case Intrinsic::vector_partial_reduce_add: {
8133 SDValue Acc = getValue(I.getOperand(0));
8134 SDValue Input = getValue(I.getOperand(1));
8135 setValue(&I,
8136 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8137 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8138 return;
8139 }
8140 case Intrinsic::experimental_cttz_elts: {
8141 auto DL = getCurSDLoc();
8142 SDValue Op = getValue(I.getOperand(0));
8143 EVT OpVT = Op.getValueType();
8144
8145 if (!TLI.shouldExpandCttzElements(OpVT)) {
8146 visitTargetIntrinsic(I, Intrinsic);
8147 return;
8148 }
8149
8150 if (OpVT.getScalarType() != MVT::i1) {
8151 // Compare the input vector elements to zero & use to count trailing zeros
8152 SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8153 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8154 OpVT.getVectorElementCount());
8155 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8156 }
8157
8158 // If the zero-is-poison flag is set, we can assume the upper limit
8159 // of the result is VF-1.
8160 bool ZeroIsPoison =
8161 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8162 ConstantRange VScaleRange(1, true); // Dummy value.
8163 if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8164 VScaleRange = getVScaleRange(I.getCaller(), 64);
8165 unsigned EltWidth = TLI.getBitWidthForCttzElements(
8166 I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8167
8168 MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8169
8170 // Create the new vector type & get the vector length
8171 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8172 OpVT.getVectorElementCount());
8173
8174 SDValue VL =
8175 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8176
8177 SDValue StepVec = DAG.getStepVector(DL, NewVT);
8178 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8179 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8180 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8181 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8182 SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8183 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8184
8185 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8186 SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
8187
8188 setValue(&I, Ret);
8189 return;
8190 }
8191 case Intrinsic::vector_insert: {
8192 SDValue Vec = getValue(I.getOperand(0));
8193 SDValue SubVec = getValue(I.getOperand(1));
8194 SDValue Index = getValue(I.getOperand(2));
8195
8196 // The intrinsic's index type is i64, but the SDNode requires an index type
8197 // suitable for the target. Convert the index as required.
8198 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8199 if (Index.getValueType() != VectorIdxTy)
8200 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8201
8202 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8203 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8204 Index));
8205 return;
8206 }
8207 case Intrinsic::vector_extract: {
8208 SDValue Vec = getValue(I.getOperand(0));
8209 SDValue Index = getValue(I.getOperand(1));
8210 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8211
8212 // The intrinsic's index type is i64, but the SDNode requires an index type
8213 // suitable for the target. Convert the index as required.
8214 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8215 if (Index.getValueType() != VectorIdxTy)
8216 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8217
8218 setValue(&I,
8219 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8220 return;
8221 }
8222 case Intrinsic::experimental_vector_match: {
8223 SDValue Op1 = getValue(I.getOperand(0));
8224 SDValue Op2 = getValue(I.getOperand(1));
8225 SDValue Mask = getValue(I.getOperand(2));
8226 EVT Op1VT = Op1.getValueType();
8227 EVT Op2VT = Op2.getValueType();
8228 EVT ResVT = Mask.getValueType();
8229 unsigned SearchSize = Op2VT.getVectorNumElements();
8230
8231 // If the target has native support for this vector match operation, lower
8232 // the intrinsic untouched; otherwise, expand it below.
8233 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8234 visitTargetIntrinsic(I, Intrinsic);
8235 return;
8236 }
8237
8238 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8239
8240 for (unsigned i = 0; i < SearchSize; ++i) {
8241 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8242 Op2VT.getVectorElementType(), Op2,
8243 DAG.getVectorIdxConstant(i, sdl));
8244 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8245 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8246 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8247 }
8248
8249 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8250 return;
8251 }
8252 case Intrinsic::vector_reverse:
8253 visitVectorReverse(I);
8254 return;
8255 case Intrinsic::vector_splice:
8256 visitVectorSplice(I);
8257 return;
8258 case Intrinsic::callbr_landingpad:
8259 visitCallBrLandingPad(I);
8260 return;
8261 case Intrinsic::vector_interleave2:
8262 visitVectorInterleave(I, 2);
8263 return;
8264 case Intrinsic::vector_interleave3:
8265 visitVectorInterleave(I, 3);
8266 return;
8267 case Intrinsic::vector_interleave4:
8268 visitVectorInterleave(I, 4);
8269 return;
8270 case Intrinsic::vector_interleave5:
8271 visitVectorInterleave(I, 5);
8272 return;
8273 case Intrinsic::vector_interleave6:
8274 visitVectorInterleave(I, 6);
8275 return;
8276 case Intrinsic::vector_interleave7:
8277 visitVectorInterleave(I, 7);
8278 return;
8279 case Intrinsic::vector_interleave8:
8280 visitVectorInterleave(I, 8);
8281 return;
8282 case Intrinsic::vector_deinterleave2:
8283 visitVectorDeinterleave(I, 2);
8284 return;
8285 case Intrinsic::vector_deinterleave3:
8286 visitVectorDeinterleave(I, 3);
8287 return;
8288 case Intrinsic::vector_deinterleave4:
8289 visitVectorDeinterleave(I, 4);
8290 return;
8291 case Intrinsic::vector_deinterleave5:
8292 visitVectorDeinterleave(I, 5);
8293 return;
8294 case Intrinsic::vector_deinterleave6:
8295 visitVectorDeinterleave(I, 6);
8296 return;
8297 case Intrinsic::vector_deinterleave7:
8298 visitVectorDeinterleave(I, 7);
8299 return;
8300 case Intrinsic::vector_deinterleave8:
8301 visitVectorDeinterleave(I, 8);
8302 return;
8303 case Intrinsic::experimental_vector_compress:
8304 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8305 getValue(I.getArgOperand(0)).getValueType(),
8306 getValue(I.getArgOperand(0)),
8307 getValue(I.getArgOperand(1)),
8308 getValue(I.getArgOperand(2)), Flags));
8309 return;
8310 case Intrinsic::experimental_convergence_anchor:
8311 case Intrinsic::experimental_convergence_entry:
8312 case Intrinsic::experimental_convergence_loop:
8313 visitConvergenceControl(I, Intrinsic);
8314 return;
8315 case Intrinsic::experimental_vector_histogram_add: {
8316 visitVectorHistogram(I, Intrinsic);
8317 return;
8318 }
8319 case Intrinsic::experimental_vector_extract_last_active: {
8320 visitVectorExtractLastActive(I, Intrinsic);
8321 return;
8322 }
8323 case Intrinsic::loop_dependence_war_mask:
8324 setValue(&I,
8326 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8327 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8328 return;
8329 case Intrinsic::loop_dependence_raw_mask:
8330 setValue(&I,
8332 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8333 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8334 return;
8335 }
8336}
8337
8338void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
8340 assert(Result.getNode()->getNumValues() == 2);
8341 SDValue OutChain = Result.getValue(1);
8342 assert(OutChain.getValueType() == MVT::Other);
8343
8344 // Instead of updating the root immediately, push the produced chain to the
8345 // appropriate list, deferring the update until the root is requested. In this
8346 // case, the nodes from the lists are chained using TokenFactor, indicating
8347 // that the operations are independent.
8348 //
8349 // In particular, the root is updated before any call that might access the
8350 // floating-point environment, except for constrained intrinsics.
8351 switch (EB) {
8354 PendingConstrainedFP.push_back(OutChain);
8355 break;
8357 PendingConstrainedFPStrict.push_back(OutChain);
8358 break;
8359 }
8360}
8361
8362void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8363 const ConstrainedFPIntrinsic &FPI) {
8364 SDLoc sdl = getCurSDLoc();
8365
8366 // We do not need to serialize constrained FP intrinsics against
8367 // each other or against (nonvolatile) loads, so they can be
8368 // chained like loads.
8370 SDValue Chain = getFPOperationRoot(EB);
8372 Opers.push_back(Chain);
8373 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8374 Opers.push_back(getValue(FPI.getArgOperand(I)));
8375
8376 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8377 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8378 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8379
8380 SDNodeFlags Flags;
8382 Flags.setNoFPExcept(true);
8383
8384 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8385 Flags.copyFMF(*FPOp);
8386
8387 unsigned Opcode;
8388 switch (FPI.getIntrinsicID()) {
8389 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8390#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8391 case Intrinsic::INTRINSIC: \
8392 Opcode = ISD::STRICT_##DAGN; \
8393 break;
8394#include "llvm/IR/ConstrainedOps.def"
8395 case Intrinsic::experimental_constrained_fmuladd: {
8396 Opcode = ISD::STRICT_FMA;
8397 // Break fmuladd into fmul and fadd.
8398 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8399 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8400 Opers.pop_back();
8401 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8402 pushFPOpOutChain(Mul, EB);
8403 Opcode = ISD::STRICT_FADD;
8404 Opers.clear();
8405 Opers.push_back(Mul.getValue(1));
8406 Opers.push_back(Mul.getValue(0));
8407 Opers.push_back(getValue(FPI.getArgOperand(2)));
8408 }
8409 break;
8410 }
8411 }
8412
8413 // A few strict DAG nodes carry additional operands that are not
8414 // set up by the default code above.
8415 switch (Opcode) {
8416 default: break;
8418 Opers.push_back(
8419 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8420 break;
8421 case ISD::STRICT_FSETCC:
8422 case ISD::STRICT_FSETCCS: {
8424 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8425 if (TM.Options.NoNaNsFPMath)
8426 Condition = getFCmpCodeWithoutNaN(Condition);
8427 Opers.push_back(DAG.getCondCode(Condition));
8428 break;
8429 }
8430 }
8431
8432 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8433 pushFPOpOutChain(Result, EB);
8434
8435 SDValue FPResult = Result.getValue(0);
8436 setValue(&FPI, FPResult);
8437}
8438
8439static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8440 std::optional<unsigned> ResOPC;
8441 switch (VPIntrin.getIntrinsicID()) {
8442 case Intrinsic::vp_ctlz: {
8443 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8444 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8445 break;
8446 }
8447 case Intrinsic::vp_cttz: {
8448 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8449 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8450 break;
8451 }
8452 case Intrinsic::vp_cttz_elts: {
8453 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8454 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8455 break;
8456 }
8457#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8458 case Intrinsic::VPID: \
8459 ResOPC = ISD::VPSD; \
8460 break;
8461#include "llvm/IR/VPIntrinsics.def"
8462 }
8463
8464 if (!ResOPC)
8466 "Inconsistency: no SDNode available for this VPIntrinsic!");
8467
8468 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8469 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8470 if (VPIntrin.getFastMathFlags().allowReassoc())
8471 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8472 : ISD::VP_REDUCE_FMUL;
8473 }
8474
8475 return *ResOPC;
8476}
8477
8478void SelectionDAGBuilder::visitVPLoad(
8479 const VPIntrinsic &VPIntrin, EVT VT,
8480 const SmallVectorImpl<SDValue> &OpValues) {
8481 SDLoc DL = getCurSDLoc();
8482 Value *PtrOperand = VPIntrin.getArgOperand(0);
8483 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8484 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8485 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8486 SDValue LD;
8487 // Do not serialize variable-length loads of constant memory with
8488 // anything.
8489 if (!Alignment)
8490 Alignment = DAG.getEVTAlign(VT);
8491 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8492 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8493 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8494 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8495 MachineMemOperand::Flags MMOFlags =
8496 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8497 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8498 MachinePointerInfo(PtrOperand), MMOFlags,
8499 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8500 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8501 MMO, false /*IsExpanding */);
8502 if (AddToChain)
8503 PendingLoads.push_back(LD.getValue(1));
8504 setValue(&VPIntrin, LD);
8505}
8506
8507void SelectionDAGBuilder::visitVPLoadFF(
8508 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8509 const SmallVectorImpl<SDValue> &OpValues) {
8510 assert(OpValues.size() == 3 && "Unexpected number of operands");
8511 SDLoc DL = getCurSDLoc();
8512 Value *PtrOperand = VPIntrin.getArgOperand(0);
8513 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8514 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8515 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8516 SDValue LD;
8517 // Do not serialize variable-length loads of constant memory with
8518 // anything.
8519 if (!Alignment)
8520 Alignment = DAG.getEVTAlign(VT);
8521 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8522 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8523 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8524 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8525 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8526 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8527 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8528 MMO);
8529 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8530 if (AddToChain)
8531 PendingLoads.push_back(LD.getValue(2));
8532 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8533}
8534
8535void SelectionDAGBuilder::visitVPGather(
8536 const VPIntrinsic &VPIntrin, EVT VT,
8537 const SmallVectorImpl<SDValue> &OpValues) {
8538 SDLoc DL = getCurSDLoc();
8539 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8540 Value *PtrOperand = VPIntrin.getArgOperand(0);
8541 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8542 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8543 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8544 SDValue LD;
8545 if (!Alignment)
8546 Alignment = DAG.getEVTAlign(VT.getScalarType());
8547 unsigned AS =
8548 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8549 MachineMemOperand::Flags MMOFlags =
8550 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8551 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8552 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8553 *Alignment, AAInfo, Ranges);
8554 SDValue Base, Index, Scale;
8555 bool UniformBase =
8556 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8557 VT.getScalarStoreSize());
8558 if (!UniformBase) {
8559 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8560 Index = getValue(PtrOperand);
8561 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8562 }
8563 EVT IdxVT = Index.getValueType();
8564 EVT EltTy = IdxVT.getVectorElementType();
8565 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8566 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8567 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8568 }
8569 LD = DAG.getGatherVP(
8570 DAG.getVTList(VT, MVT::Other), VT, DL,
8571 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8573 PendingLoads.push_back(LD.getValue(1));
8574 setValue(&VPIntrin, LD);
8575}
8576
8577void SelectionDAGBuilder::visitVPStore(
8578 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8579 SDLoc DL = getCurSDLoc();
8580 Value *PtrOperand = VPIntrin.getArgOperand(1);
8581 EVT VT = OpValues[0].getValueType();
8582 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8583 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8584 SDValue ST;
8585 if (!Alignment)
8586 Alignment = DAG.getEVTAlign(VT);
8587 SDValue Ptr = OpValues[1];
8588 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8589 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8590 MachineMemOperand::Flags MMOFlags =
8591 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8592 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8593 MachinePointerInfo(PtrOperand), MMOFlags,
8594 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8595 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8596 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8597 /* IsTruncating */ false, /*IsCompressing*/ false);
8598 DAG.setRoot(ST);
8599 setValue(&VPIntrin, ST);
8600}
8601
8602void SelectionDAGBuilder::visitVPScatter(
8603 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8604 SDLoc DL = getCurSDLoc();
8605 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8606 Value *PtrOperand = VPIntrin.getArgOperand(1);
8607 EVT VT = OpValues[0].getValueType();
8608 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8609 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8610 SDValue ST;
8611 if (!Alignment)
8612 Alignment = DAG.getEVTAlign(VT.getScalarType());
8613 unsigned AS =
8614 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8615 MachineMemOperand::Flags MMOFlags =
8616 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8617 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8618 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8619 *Alignment, AAInfo);
8620 SDValue Base, Index, Scale;
8621 bool UniformBase =
8622 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8623 VT.getScalarStoreSize());
8624 if (!UniformBase) {
8625 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8626 Index = getValue(PtrOperand);
8627 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8628 }
8629 EVT IdxVT = Index.getValueType();
8630 EVT EltTy = IdxVT.getVectorElementType();
8631 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8632 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8633 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8634 }
8635 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8636 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8637 OpValues[2], OpValues[3]},
8638 MMO, ISD::SIGNED_SCALED);
8639 DAG.setRoot(ST);
8640 setValue(&VPIntrin, ST);
8641}
8642
8643void SelectionDAGBuilder::visitVPStridedLoad(
8644 const VPIntrinsic &VPIntrin, EVT VT,
8645 const SmallVectorImpl<SDValue> &OpValues) {
8646 SDLoc DL = getCurSDLoc();
8647 Value *PtrOperand = VPIntrin.getArgOperand(0);
8648 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8649 if (!Alignment)
8650 Alignment = DAG.getEVTAlign(VT.getScalarType());
8651 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8652 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8653 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8654 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8655 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8656 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8657 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8658 MachineMemOperand::Flags MMOFlags =
8659 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8660 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8661 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8662 *Alignment, AAInfo, Ranges);
8663
8664 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8665 OpValues[2], OpValues[3], MMO,
8666 false /*IsExpanding*/);
8667
8668 if (AddToChain)
8669 PendingLoads.push_back(LD.getValue(1));
8670 setValue(&VPIntrin, LD);
8671}
8672
8673void SelectionDAGBuilder::visitVPStridedStore(
8674 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8675 SDLoc DL = getCurSDLoc();
8676 Value *PtrOperand = VPIntrin.getArgOperand(1);
8677 EVT VT = OpValues[0].getValueType();
8678 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8679 if (!Alignment)
8680 Alignment = DAG.getEVTAlign(VT.getScalarType());
8681 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8682 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8683 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8684 MachineMemOperand::Flags MMOFlags =
8685 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8686 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8687 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8688 *Alignment, AAInfo);
8689
8690 SDValue ST = DAG.getStridedStoreVP(
8691 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8692 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8693 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8694 /*IsCompressing*/ false);
8695
8696 DAG.setRoot(ST);
8697 setValue(&VPIntrin, ST);
8698}
8699
8700void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8701 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8702 SDLoc DL = getCurSDLoc();
8703
8704 ISD::CondCode Condition;
8706 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8707 if (IsFP) {
8708 // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
8709 // flags, but calls that don't return floating-point types can't be
8710 // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
8711 Condition = getFCmpCondCode(CondCode);
8712 if (TM.Options.NoNaNsFPMath)
8713 Condition = getFCmpCodeWithoutNaN(Condition);
8714 } else {
8715 Condition = getICmpCondCode(CondCode);
8716 }
8717
8718 SDValue Op1 = getValue(VPIntrin.getOperand(0));
8719 SDValue Op2 = getValue(VPIntrin.getOperand(1));
8720 // #2 is the condition code
8721 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8722 SDValue EVL = getValue(VPIntrin.getOperand(4));
8723 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8724 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8725 "Unexpected target EVL type");
8726 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8727
8728 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8729 VPIntrin.getType());
8730 setValue(&VPIntrin,
8731 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8732}
8733
8734void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8735 const VPIntrinsic &VPIntrin) {
8736 SDLoc DL = getCurSDLoc();
8737 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8738
8739 auto IID = VPIntrin.getIntrinsicID();
8740
8741 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8742 return visitVPCmp(*CmpI);
8743
8744 SmallVector<EVT, 4> ValueVTs;
8745 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8746 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8747 SDVTList VTs = DAG.getVTList(ValueVTs);
8748
8749 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8750
8751 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8752 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8753 "Unexpected target EVL type");
8754
8755 // Request operands.
8756 SmallVector<SDValue, 7> OpValues;
8757 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8758 auto Op = getValue(VPIntrin.getArgOperand(I));
8759 if (I == EVLParamPos)
8760 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8761 OpValues.push_back(Op);
8762 }
8763
8764 switch (Opcode) {
8765 default: {
8766 SDNodeFlags SDFlags;
8767 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8768 SDFlags.copyFMF(*FPMO);
8769 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8770 setValue(&VPIntrin, Result);
8771 break;
8772 }
8773 case ISD::VP_LOAD:
8774 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8775 break;
8776 case ISD::VP_LOAD_FF:
8777 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8778 break;
8779 case ISD::VP_GATHER:
8780 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8781 break;
8782 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8783 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8784 break;
8785 case ISD::VP_STORE:
8786 visitVPStore(VPIntrin, OpValues);
8787 break;
8788 case ISD::VP_SCATTER:
8789 visitVPScatter(VPIntrin, OpValues);
8790 break;
8791 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8792 visitVPStridedStore(VPIntrin, OpValues);
8793 break;
8794 case ISD::VP_FMULADD: {
8795 assert(OpValues.size() == 5 && "Unexpected number of operands");
8796 SDNodeFlags SDFlags;
8797 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8798 SDFlags.copyFMF(*FPMO);
8799 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8800 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8801 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8802 } else {
8803 SDValue Mul = DAG.getNode(
8804 ISD::VP_FMUL, DL, VTs,
8805 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8806 SDValue Add =
8807 DAG.getNode(ISD::VP_FADD, DL, VTs,
8808 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8809 setValue(&VPIntrin, Add);
8810 }
8811 break;
8812 }
8813 case ISD::VP_IS_FPCLASS: {
8814 const DataLayout DLayout = DAG.getDataLayout();
8815 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8816 auto Constant = OpValues[1]->getAsZExtVal();
8817 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8818 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8819 {OpValues[0], Check, OpValues[2], OpValues[3]});
8820 setValue(&VPIntrin, V);
8821 return;
8822 }
8823 case ISD::VP_INTTOPTR: {
8824 SDValue N = OpValues[0];
8825 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8826 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8827 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8828 OpValues[2]);
8829 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8830 OpValues[2]);
8831 setValue(&VPIntrin, N);
8832 break;
8833 }
8834 case ISD::VP_PTRTOINT: {
8835 SDValue N = OpValues[0];
8836 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8837 VPIntrin.getType());
8838 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
8839 VPIntrin.getOperand(0)->getType());
8840 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8841 OpValues[2]);
8842 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8843 OpValues[2]);
8844 setValue(&VPIntrin, N);
8845 break;
8846 }
8847 case ISD::VP_ABS:
8848 case ISD::VP_CTLZ:
8849 case ISD::VP_CTLZ_ZERO_UNDEF:
8850 case ISD::VP_CTTZ:
8851 case ISD::VP_CTTZ_ZERO_UNDEF:
8852 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
8853 case ISD::VP_CTTZ_ELTS: {
8854 SDValue Result =
8855 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
8856 setValue(&VPIntrin, Result);
8857 break;
8858 }
8859 }
8860}
8861
8862SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
8863 const BasicBlock *EHPadBB,
8864 MCSymbol *&BeginLabel) {
8865 MachineFunction &MF = DAG.getMachineFunction();
8866
8867 // Insert a label before the invoke call to mark the try range. This can be
8868 // used to detect deletion of the invoke via the MachineModuleInfo.
8869 BeginLabel = MF.getContext().createTempSymbol();
8870
8871 // For SjLj, keep track of which landing pads go with which invokes
8872 // so as to maintain the ordering of pads in the LSDA.
8873 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
8874 if (CallSiteIndex) {
8875 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
8876 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
8877
8878 // Now that the call site is handled, stop tracking it.
8879 FuncInfo.setCurrentCallSite(0);
8880 }
8881
8882 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
8883}
8884
8885SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
8886 const BasicBlock *EHPadBB,
8887 MCSymbol *BeginLabel) {
8888 assert(BeginLabel && "BeginLabel should've been set");
8889
8890 MachineFunction &MF = DAG.getMachineFunction();
8891
8892 // Insert a label at the end of the invoke call to mark the try range. This
8893 // can be used to detect deletion of the invoke via the MachineModuleInfo.
8894 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
8895 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
8896
8897 // Inform MachineModuleInfo of range.
8898 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
8899 // There is a platform (e.g. wasm) that uses funclet style IR but does not
8900 // actually use outlined funclets and their LSDA info style.
8901 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
8902 assert(II && "II should've been set");
8903 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
8904 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
8905 } else if (!isScopedEHPersonality(Pers)) {
8906 assert(EHPadBB);
8907 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
8908 }
8909
8910 return Chain;
8911}
8912
8913std::pair<SDValue, SDValue>
8915 const BasicBlock *EHPadBB) {
8916 MCSymbol *BeginLabel = nullptr;
8917
8918 if (EHPadBB) {
8919 // Both PendingLoads and PendingExports must be flushed here;
8920 // this call might not return.
8921 (void)getRoot();
8922 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
8923 CLI.setChain(getRoot());
8924 }
8925
8926 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8927 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
8928
8929 assert((CLI.IsTailCall || Result.second.getNode()) &&
8930 "Non-null chain expected with non-tail call!");
8931 assert((Result.second.getNode() || !Result.first.getNode()) &&
8932 "Null value expected with tail call!");
8933
8934 if (!Result.second.getNode()) {
8935 // As a special case, a null chain means that a tail call has been emitted
8936 // and the DAG root is already updated.
8937 HasTailCall = true;
8938
8939 // Since there's no actual continuation from this block, nothing can be
8940 // relying on us setting vregs for them.
8941 PendingExports.clear();
8942 } else {
8943 DAG.setRoot(Result.second);
8944 }
8945
8946 if (EHPadBB) {
8947 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
8948 BeginLabel));
8949 Result.second = getRoot();
8950 }
8951
8952 return Result;
8953}
8954
8956 bool isMustTailCall = CB.isMustTailCall();
8957
8958 // Avoid emitting tail calls in functions with the disable-tail-calls
8959 // attribute.
8960 const Function *Caller = CB.getParent()->getParent();
8961 if (!isMustTailCall &&
8962 Caller->getFnAttribute("disable-tail-calls").getValueAsBool())
8963 return false;
8964
8965 // We can't tail call inside a function with a swifterror argument. Lowering
8966 // does not support this yet. It would have to move into the swifterror
8967 // register before the call.
8968 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
8969 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
8970 return false;
8971
8972 // Check if target-independent constraints permit a tail call here.
8973 // Target-dependent constraints are checked within TLI->LowerCallTo.
8974 return isInTailCallPosition(CB, DAG.getTarget());
8975}
8976
8978 bool isTailCall, bool isMustTailCall,
8979 const BasicBlock *EHPadBB,
8980 const TargetLowering::PtrAuthInfo *PAI) {
8981 auto &DL = DAG.getDataLayout();
8982 FunctionType *FTy = CB.getFunctionType();
8983 Type *RetTy = CB.getType();
8984
8986 Args.reserve(CB.arg_size());
8987
8988 const Value *SwiftErrorVal = nullptr;
8989 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8990
8991 if (isTailCall)
8992 isTailCall = canTailCall(CB);
8993
8994 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
8995 const Value *V = *I;
8996
8997 // Skip empty types
8998 if (V->getType()->isEmptyTy())
8999 continue;
9000
9001 SDValue ArgNode = getValue(V);
9002 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
9003 Entry.setAttributes(&CB, I - CB.arg_begin());
9004
9005 // Use swifterror virtual register as input to the call.
9006 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
9007 SwiftErrorVal = V;
9008 // We find the virtual register for the actual swifterror argument.
9009 // Instead of using the Value, we use the virtual register instead.
9010 Entry.Node =
9011 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
9012 EVT(TLI.getPointerTy(DL)));
9013 }
9014
9015 Args.push_back(Entry);
9016
9017 // If we have an explicit sret argument that is an Instruction, (i.e., it
9018 // might point to function-local memory), we can't meaningfully tail-call.
9019 if (Entry.IsSRet && isa<Instruction>(V))
9020 isTailCall = false;
9021 }
9022
9023 // If call site has a cfguardtarget operand bundle, create and add an
9024 // additional ArgListEntry.
9025 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9026 Value *V = Bundle->Inputs[0];
9028 Entry.IsCFGuardTarget = true;
9029 Args.push_back(Entry);
9030 }
9031
9032 // Disable tail calls if there is an swifterror argument. Targets have not
9033 // been updated to support tail calls.
9034 if (TLI.supportSwiftError() && SwiftErrorVal)
9035 isTailCall = false;
9036
9037 ConstantInt *CFIType = nullptr;
9038 if (CB.isIndirectCall()) {
9039 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9040 if (!TLI.supportKCFIBundles())
9042 "Target doesn't support calls with kcfi operand bundles.");
9043 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9044 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9045 }
9046 }
9047
9048 SDValue ConvControlToken;
9049 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9050 auto *Token = Bundle->Inputs[0].get();
9051 ConvControlToken = getValue(Token);
9052 }
9053
9056 .setChain(getRoot())
9057 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9058 .setTailCall(isTailCall)
9062 .setCFIType(CFIType)
9063 .setConvergenceControlToken(ConvControlToken);
9064
9065 // Set the pointer authentication info if we have it.
9066 if (PAI) {
9067 if (!TLI.supportPtrAuthBundles())
9069 "This target doesn't support calls with ptrauth operand bundles.");
9070 CLI.setPtrAuth(*PAI);
9071 }
9072
9073 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9074
9075 if (Result.first.getNode()) {
9076 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9077 setValue(&CB, Result.first);
9078 }
9079
9080 // The last element of CLI.InVals has the SDValue for swifterror return.
9081 // Here we copy it to a virtual register and update SwiftErrorMap for
9082 // book-keeping.
9083 if (SwiftErrorVal && TLI.supportSwiftError()) {
9084 // Get the last element of InVals.
9085 SDValue Src = CLI.InVals.back();
9086 Register VReg =
9087 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9088 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9089 DAG.setRoot(CopyNode);
9090 }
9091}
9092
9093static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9094 SelectionDAGBuilder &Builder) {
9095 // Check to see if this load can be trivially constant folded, e.g. if the
9096 // input is from a string literal.
9097 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9098 // Cast pointer to the type we really want to load.
9099 Type *LoadTy =
9100 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9101 if (LoadVT.isVector())
9102 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9103 if (const Constant *LoadCst =
9104 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9105 LoadTy, Builder.DAG.getDataLayout()))
9106 return Builder.getValue(LoadCst);
9107 }
9108
9109 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9110 // still constant memory, the input chain can be the entry node.
9111 SDValue Root;
9112 bool ConstantMemory = false;
9113
9114 // Do not serialize (non-volatile) loads of constant memory with anything.
9115 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9116 Root = Builder.DAG.getEntryNode();
9117 ConstantMemory = true;
9118 } else {
9119 // Do not serialize non-volatile loads against each other.
9120 Root = Builder.DAG.getRoot();
9121 }
9122
9123 SDValue Ptr = Builder.getValue(PtrVal);
9124 SDValue LoadVal =
9125 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9126 MachinePointerInfo(PtrVal), Align(1));
9127
9128 if (!ConstantMemory)
9129 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9130 return LoadVal;
9131}
9132
9133/// Record the value for an instruction that produces an integer result,
9134/// converting the type where necessary.
9135void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9136 SDValue Value,
9137 bool IsSigned) {
9138 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9139 I.getType(), true);
9140 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9141 setValue(&I, Value);
9142}
9143
9144/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9145/// true and lower it. Otherwise return false, and it will be lowered like a
9146/// normal call.
9147/// The caller already checked that \p I calls the appropriate LibFunc with a
9148/// correct prototype.
9149bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9150 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9151 const Value *Size = I.getArgOperand(2);
9152 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9153 if (CSize && CSize->getZExtValue() == 0) {
9154 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9155 I.getType(), true);
9156 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9157 return true;
9158 }
9159
9160 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9161 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9162 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9163 getValue(Size), &I);
9164 if (Res.first.getNode()) {
9165 processIntegerCallValue(I, Res.first, true);
9166 PendingLoads.push_back(Res.second);
9167 return true;
9168 }
9169
9170 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9171 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9172 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9173 return false;
9174
9175 // If the target has a fast compare for the given size, it will return a
9176 // preferred load type for that size. Require that the load VT is legal and
9177 // that the target supports unaligned loads of that type. Otherwise, return
9178 // INVALID.
9179 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9180 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9181 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9182 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9183 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9184 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9185 // TODO: Check alignment of src and dest ptrs.
9186 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9187 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9188 if (!TLI.isTypeLegal(LVT) ||
9189 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9190 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9192 }
9193
9194 return LVT;
9195 };
9196
9197 // This turns into unaligned loads. We only do this if the target natively
9198 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9199 // we'll only produce a small number of byte loads.
9200 MVT LoadVT;
9201 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9202 switch (NumBitsToCompare) {
9203 default:
9204 return false;
9205 case 16:
9206 LoadVT = MVT::i16;
9207 break;
9208 case 32:
9209 LoadVT = MVT::i32;
9210 break;
9211 case 64:
9212 case 128:
9213 case 256:
9214 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9215 break;
9216 }
9217
9218 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9219 return false;
9220
9221 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9222 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9223
9224 // Bitcast to a wide integer type if the loads are vectors.
9225 if (LoadVT.isVector()) {
9226 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9227 LoadL = DAG.getBitcast(CmpVT, LoadL);
9228 LoadR = DAG.getBitcast(CmpVT, LoadR);
9229 }
9230
9231 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9232 processIntegerCallValue(I, Cmp, false);
9233 return true;
9234}
9235
9236/// See if we can lower a memchr call into an optimized form. If so, return
9237/// true and lower it. Otherwise return false, and it will be lowered like a
9238/// normal call.
9239/// The caller already checked that \p I calls the appropriate LibFunc with a
9240/// correct prototype.
9241bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9242 const Value *Src = I.getArgOperand(0);
9243 const Value *Char = I.getArgOperand(1);
9244 const Value *Length = I.getArgOperand(2);
9245
9246 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9247 std::pair<SDValue, SDValue> Res =
9248 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9249 getValue(Src), getValue(Char), getValue(Length),
9250 MachinePointerInfo(Src));
9251 if (Res.first.getNode()) {
9252 setValue(&I, Res.first);
9253 PendingLoads.push_back(Res.second);
9254 return true;
9255 }
9256
9257 return false;
9258}
9259
9260/// See if we can lower a mempcpy call into an optimized form. If so, return
9261/// true and lower it. Otherwise return false, and it will be lowered like a
9262/// normal call.
9263/// The caller already checked that \p I calls the appropriate LibFunc with a
9264/// correct prototype.
9265bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9266 SDValue Dst = getValue(I.getArgOperand(0));
9267 SDValue Src = getValue(I.getArgOperand(1));
9268 SDValue Size = getValue(I.getArgOperand(2));
9269
9270 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9271 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9272 // DAG::getMemcpy needs Alignment to be defined.
9273 Align Alignment = std::min(DstAlign, SrcAlign);
9274
9275 SDLoc sdl = getCurSDLoc();
9276
9277 // In the mempcpy context we need to pass in a false value for isTailCall
9278 // because the return pointer needs to be adjusted by the size of
9279 // the copied memory.
9280 SDValue Root = getMemoryRoot();
9281 SDValue MC = DAG.getMemcpy(
9282 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9283 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9284 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9285 assert(MC.getNode() != nullptr &&
9286 "** memcpy should not be lowered as TailCall in mempcpy context **");
9287 DAG.setRoot(MC);
9288
9289 // Check if Size needs to be truncated or extended.
9290 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9291
9292 // Adjust return pointer to point just past the last dst byte.
9293 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9294 setValue(&I, DstPlusSize);
9295 return true;
9296}
9297
9298/// See if we can lower a strcpy call into an optimized form. If so, return
9299/// true and lower it, otherwise return false and it will be lowered like a
9300/// normal call.
9301/// The caller already checked that \p I calls the appropriate LibFunc with a
9302/// correct prototype.
9303bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9304 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9305
9306 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9307 std::pair<SDValue, SDValue> Res =
9309 getValue(Arg0), getValue(Arg1),
9310 MachinePointerInfo(Arg0),
9311 MachinePointerInfo(Arg1), isStpcpy);
9312 if (Res.first.getNode()) {
9313 setValue(&I, Res.first);
9314 DAG.setRoot(Res.second);
9315 return true;
9316 }
9317
9318 return false;
9319}
9320
9321/// See if we can lower a strcmp call into an optimized form. If so, return
9322/// true and lower it, otherwise return false and it will be lowered like a
9323/// normal call.
9324/// The caller already checked that \p I calls the appropriate LibFunc with a
9325/// correct prototype.
9326bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9327 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9328
9329 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9330 std::pair<SDValue, SDValue> Res =
9331 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
9332 getValue(Arg0), getValue(Arg1),
9333 MachinePointerInfo(Arg0),
9334 MachinePointerInfo(Arg1));
9335 if (Res.first.getNode()) {
9336 processIntegerCallValue(I, Res.first, true);
9337 PendingLoads.push_back(Res.second);
9338 return true;
9339 }
9340
9341 return false;
9342}
9343
9344/// See if we can lower a strlen call into an optimized form. If so, return
9345/// true and lower it, otherwise return false and it will be lowered like a
9346/// normal call.
9347/// The caller already checked that \p I calls the appropriate LibFunc with a
9348/// correct prototype.
9349bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9350 const Value *Arg0 = I.getArgOperand(0);
9351
9352 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9353 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrlen(
9354 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), &I);
9355 if (Res.first.getNode()) {
9356 processIntegerCallValue(I, Res.first, false);
9357 PendingLoads.push_back(Res.second);
9358 return true;
9359 }
9360
9361 return false;
9362}
9363
9364/// See if we can lower a strnlen call into an optimized form. If so, return
9365/// true and lower it, otherwise return false and it will be lowered like a
9366/// normal call.
9367/// The caller already checked that \p I calls the appropriate LibFunc with a
9368/// correct prototype.
9369bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9370 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9371
9372 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9373 std::pair<SDValue, SDValue> Res =
9374 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9375 getValue(Arg0), getValue(Arg1),
9376 MachinePointerInfo(Arg0));
9377 if (Res.first.getNode()) {
9378 processIntegerCallValue(I, Res.first, false);
9379 PendingLoads.push_back(Res.second);
9380 return true;
9381 }
9382
9383 return false;
9384}
9385
9386/// See if we can lower a unary floating-point operation into an SDNode with
9387/// the specified Opcode. If so, return true and lower it, otherwise return
9388/// false and it will be lowered like a normal call.
9389/// The caller already checked that \p I calls the appropriate LibFunc with a
9390/// correct prototype.
9391bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9392 unsigned Opcode) {
9393 // We already checked this call's prototype; verify it doesn't modify errno.
9394 if (!I.onlyReadsMemory())
9395 return false;
9396
9397 SDNodeFlags Flags;
9398 Flags.copyFMF(cast<FPMathOperator>(I));
9399
9400 SDValue Tmp = getValue(I.getArgOperand(0));
9401 setValue(&I,
9402 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9403 return true;
9404}
9405
9406/// See if we can lower a binary floating-point operation into an SDNode with
9407/// the specified Opcode. If so, return true and lower it. Otherwise return
9408/// false, and it will be lowered like a normal call.
9409/// The caller already checked that \p I calls the appropriate LibFunc with a
9410/// correct prototype.
9411bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9412 unsigned Opcode) {
9413 // We already checked this call's prototype; verify it doesn't modify errno.
9414 if (!I.onlyReadsMemory())
9415 return false;
9416
9417 SDNodeFlags Flags;
9418 Flags.copyFMF(cast<FPMathOperator>(I));
9419
9420 SDValue Tmp0 = getValue(I.getArgOperand(0));
9421 SDValue Tmp1 = getValue(I.getArgOperand(1));
9422 EVT VT = Tmp0.getValueType();
9423 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9424 return true;
9425}
9426
9427void SelectionDAGBuilder::visitCall(const CallInst &I) {
9428 // Handle inline assembly differently.
9429 if (I.isInlineAsm()) {
9430 visitInlineAsm(I);
9431 return;
9432 }
9433
9435
9436 if (Function *F = I.getCalledFunction()) {
9437 if (F->isDeclaration()) {
9438 // Is this an LLVM intrinsic?
9439 if (unsigned IID = F->getIntrinsicID()) {
9440 visitIntrinsicCall(I, IID);
9441 return;
9442 }
9443 }
9444
9445 // Check for well-known libc/libm calls. If the function is internal, it
9446 // can't be a library call. Don't do the check if marked as nobuiltin for
9447 // some reason or the call site requires strict floating point semantics.
9448 LibFunc Func;
9449 if (!I.isNoBuiltin() && !I.isStrictFP() && !F->hasLocalLinkage() &&
9450 F->hasName() && LibInfo->getLibFunc(*F, Func) &&
9451 LibInfo->hasOptimizedCodeGen(Func)) {
9452 switch (Func) {
9453 default: break;
9454 case LibFunc_bcmp:
9455 if (visitMemCmpBCmpCall(I))
9456 return;
9457 break;
9458 case LibFunc_copysign:
9459 case LibFunc_copysignf:
9460 case LibFunc_copysignl:
9461 // We already checked this call's prototype; verify it doesn't modify
9462 // errno.
9463 if (I.onlyReadsMemory()) {
9464 SDValue LHS = getValue(I.getArgOperand(0));
9465 SDValue RHS = getValue(I.getArgOperand(1));
9467 LHS.getValueType(), LHS, RHS));
9468 return;
9469 }
9470 break;
9471 case LibFunc_fabs:
9472 case LibFunc_fabsf:
9473 case LibFunc_fabsl:
9474 if (visitUnaryFloatCall(I, ISD::FABS))
9475 return;
9476 break;
9477 case LibFunc_fmin:
9478 case LibFunc_fminf:
9479 case LibFunc_fminl:
9480 if (visitBinaryFloatCall(I, ISD::FMINNUM))
9481 return;
9482 break;
9483 case LibFunc_fmax:
9484 case LibFunc_fmaxf:
9485 case LibFunc_fmaxl:
9486 if (visitBinaryFloatCall(I, ISD::FMAXNUM))
9487 return;
9488 break;
9489 case LibFunc_fminimum_num:
9490 case LibFunc_fminimum_numf:
9491 case LibFunc_fminimum_numl:
9492 if (visitBinaryFloatCall(I, ISD::FMINIMUMNUM))
9493 return;
9494 break;
9495 case LibFunc_fmaximum_num:
9496 case LibFunc_fmaximum_numf:
9497 case LibFunc_fmaximum_numl:
9498 if (visitBinaryFloatCall(I, ISD::FMAXIMUMNUM))
9499 return;
9500 break;
9501 case LibFunc_sin:
9502 case LibFunc_sinf:
9503 case LibFunc_sinl:
9504 if (visitUnaryFloatCall(I, ISD::FSIN))
9505 return;
9506 break;
9507 case LibFunc_cos:
9508 case LibFunc_cosf:
9509 case LibFunc_cosl:
9510 if (visitUnaryFloatCall(I, ISD::FCOS))
9511 return;
9512 break;
9513 case LibFunc_tan:
9514 case LibFunc_tanf:
9515 case LibFunc_tanl:
9516 if (visitUnaryFloatCall(I, ISD::FTAN))
9517 return;
9518 break;
9519 case LibFunc_asin:
9520 case LibFunc_asinf:
9521 case LibFunc_asinl:
9522 if (visitUnaryFloatCall(I, ISD::FASIN))
9523 return;
9524 break;
9525 case LibFunc_acos:
9526 case LibFunc_acosf:
9527 case LibFunc_acosl:
9528 if (visitUnaryFloatCall(I, ISD::FACOS))
9529 return;
9530 break;
9531 case LibFunc_atan:
9532 case LibFunc_atanf:
9533 case LibFunc_atanl:
9534 if (visitUnaryFloatCall(I, ISD::FATAN))
9535 return;
9536 break;
9537 case LibFunc_atan2:
9538 case LibFunc_atan2f:
9539 case LibFunc_atan2l:
9540 if (visitBinaryFloatCall(I, ISD::FATAN2))
9541 return;
9542 break;
9543 case LibFunc_sinh:
9544 case LibFunc_sinhf:
9545 case LibFunc_sinhl:
9546 if (visitUnaryFloatCall(I, ISD::FSINH))
9547 return;
9548 break;
9549 case LibFunc_cosh:
9550 case LibFunc_coshf:
9551 case LibFunc_coshl:
9552 if (visitUnaryFloatCall(I, ISD::FCOSH))
9553 return;
9554 break;
9555 case LibFunc_tanh:
9556 case LibFunc_tanhf:
9557 case LibFunc_tanhl:
9558 if (visitUnaryFloatCall(I, ISD::FTANH))
9559 return;
9560 break;
9561 case LibFunc_sqrt:
9562 case LibFunc_sqrtf:
9563 case LibFunc_sqrtl:
9564 case LibFunc_sqrt_finite:
9565 case LibFunc_sqrtf_finite:
9566 case LibFunc_sqrtl_finite:
9567 if (visitUnaryFloatCall(I, ISD::FSQRT))
9568 return;
9569 break;
9570 case LibFunc_floor:
9571 case LibFunc_floorf:
9572 case LibFunc_floorl:
9573 if (visitUnaryFloatCall(I, ISD::FFLOOR))
9574 return;
9575 break;
9576 case LibFunc_nearbyint:
9577 case LibFunc_nearbyintf:
9578 case LibFunc_nearbyintl:
9579 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
9580 return;
9581 break;
9582 case LibFunc_ceil:
9583 case LibFunc_ceilf:
9584 case LibFunc_ceill:
9585 if (visitUnaryFloatCall(I, ISD::FCEIL))
9586 return;
9587 break;
9588 case LibFunc_rint:
9589 case LibFunc_rintf:
9590 case LibFunc_rintl:
9591 if (visitUnaryFloatCall(I, ISD::FRINT))
9592 return;
9593 break;
9594 case LibFunc_round:
9595 case LibFunc_roundf:
9596 case LibFunc_roundl:
9597 if (visitUnaryFloatCall(I, ISD::FROUND))
9598 return;
9599 break;
9600 case LibFunc_trunc:
9601 case LibFunc_truncf:
9602 case LibFunc_truncl:
9603 if (visitUnaryFloatCall(I, ISD::FTRUNC))
9604 return;
9605 break;
9606 case LibFunc_log2:
9607 case LibFunc_log2f:
9608 case LibFunc_log2l:
9609 if (visitUnaryFloatCall(I, ISD::FLOG2))
9610 return;
9611 break;
9612 case LibFunc_exp2:
9613 case LibFunc_exp2f:
9614 case LibFunc_exp2l:
9615 if (visitUnaryFloatCall(I, ISD::FEXP2))
9616 return;
9617 break;
9618 case LibFunc_exp10:
9619 case LibFunc_exp10f:
9620 case LibFunc_exp10l:
9621 if (visitUnaryFloatCall(I, ISD::FEXP10))
9622 return;
9623 break;
9624 case LibFunc_ldexp:
9625 case LibFunc_ldexpf:
9626 case LibFunc_ldexpl:
9627 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9628 return;
9629 break;
9630 case LibFunc_memcmp:
9631 if (visitMemCmpBCmpCall(I))
9632 return;
9633 break;
9634 case LibFunc_mempcpy:
9635 if (visitMemPCpyCall(I))
9636 return;
9637 break;
9638 case LibFunc_memchr:
9639 if (visitMemChrCall(I))
9640 return;
9641 break;
9642 case LibFunc_strcpy:
9643 if (visitStrCpyCall(I, false))
9644 return;
9645 break;
9646 case LibFunc_stpcpy:
9647 if (visitStrCpyCall(I, true))
9648 return;
9649 break;
9650 case LibFunc_strcmp:
9651 if (visitStrCmpCall(I))
9652 return;
9653 break;
9654 case LibFunc_strlen:
9655 if (visitStrLenCall(I))
9656 return;
9657 break;
9658 case LibFunc_strnlen:
9659 if (visitStrNLenCall(I))
9660 return;
9661 break;
9662 }
9663 }
9664 }
9665
9666 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9667 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9668 return;
9669 }
9670
9671 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9672 // have to do anything here to lower funclet bundles.
9673 // CFGuardTarget bundles are lowered in LowerCallTo.
9675 I, "calls",
9680
9681 SDValue Callee = getValue(I.getCalledOperand());
9682
9683 if (I.hasDeoptState())
9684 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9685 else
9686 // Check if we can potentially perform a tail call. More detailed checking
9687 // is be done within LowerCallTo, after more information about the call is
9688 // known.
9689 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9690}
9691
9693 const CallBase &CB, const BasicBlock *EHPadBB) {
9694 auto PAB = CB.getOperandBundle("ptrauth");
9695 const Value *CalleeV = CB.getCalledOperand();
9696
9697 // Gather the call ptrauth data from the operand bundle:
9698 // [ i32 <key>, i64 <discriminator> ]
9699 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9700 const Value *Discriminator = PAB->Inputs[1];
9701
9702 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9703 assert(Discriminator->getType()->isIntegerTy(64) &&
9704 "Invalid ptrauth discriminator");
9705
9706 // Look through ptrauth constants to find the raw callee.
9707 // Do a direct unauthenticated call if we found it and everything matches.
9708 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9709 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9710 DAG.getDataLayout()))
9711 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9712 CB.isMustTailCall(), EHPadBB);
9713
9714 // Functions should never be ptrauth-called directly.
9715 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9716
9717 // Otherwise, do an authenticated indirect call.
9718 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9719 getValue(Discriminator)};
9720
9721 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9722 EHPadBB, &PAI);
9723}
9724
9725namespace {
9726
9727/// AsmOperandInfo - This contains information for each constraint that we are
9728/// lowering.
9729class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9730public:
9731 /// CallOperand - If this is the result output operand or a clobber
9732 /// this is null, otherwise it is the incoming operand to the CallInst.
9733 /// This gets modified as the asm is processed.
9734 SDValue CallOperand;
9735
9736 /// AssignedRegs - If this is a register or register class operand, this
9737 /// contains the set of register corresponding to the operand.
9738 RegsForValue AssignedRegs;
9739
9740 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9741 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9742 }
9743
9744 /// Whether or not this operand accesses memory
9745 bool hasMemory(const TargetLowering &TLI) const {
9746 // Indirect operand accesses access memory.
9747 if (isIndirect)
9748 return true;
9749
9750 for (const auto &Code : Codes)
9752 return true;
9753
9754 return false;
9755 }
9756};
9757
9758
9759} // end anonymous namespace
9760
9761/// Make sure that the output operand \p OpInfo and its corresponding input
9762/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9763/// out).
9764static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9765 SDISelAsmOperandInfo &MatchingOpInfo,
9766 SelectionDAG &DAG) {
9767 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9768 return;
9769
9771 const auto &TLI = DAG.getTargetLoweringInfo();
9772
9773 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9774 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9775 OpInfo.ConstraintVT);
9776 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9777 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9778 MatchingOpInfo.ConstraintVT);
9779 const bool OutOpIsIntOrFP =
9780 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9781 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9782 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9783 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9784 // FIXME: error out in a more elegant fashion
9785 report_fatal_error("Unsupported asm: input constraint"
9786 " with a matching output constraint of"
9787 " incompatible type!");
9788 }
9789 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9790}
9791
9792/// Get a direct memory input to behave well as an indirect operand.
9793/// This may introduce stores, hence the need for a \p Chain.
9794/// \return The (possibly updated) chain.
9795static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9796 SDISelAsmOperandInfo &OpInfo,
9797 SelectionDAG &DAG) {
9798 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9799
9800 // If we don't have an indirect input, put it in the constpool if we can,
9801 // otherwise spill it to a stack slot.
9802 // TODO: This isn't quite right. We need to handle these according to
9803 // the addressing mode that the constraint wants. Also, this may take
9804 // an additional register for the computation and we don't want that
9805 // either.
9806
9807 // If the operand is a float, integer, or vector constant, spill to a
9808 // constant pool entry to get its address.
9809 const Value *OpVal = OpInfo.CallOperandVal;
9810 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9812 OpInfo.CallOperand = DAG.getConstantPool(
9813 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9814 return Chain;
9815 }
9816
9817 // Otherwise, create a stack slot and emit a store to it before the asm.
9818 Type *Ty = OpVal->getType();
9819 auto &DL = DAG.getDataLayout();
9820 TypeSize TySize = DL.getTypeAllocSize(Ty);
9823 int StackID = 0;
9824 if (TySize.isScalable())
9825 StackID = TFI->getStackIDForScalableVectors();
9826 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9827 DL.getPrefTypeAlign(Ty), false,
9828 nullptr, StackID);
9829 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9830 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9832 TLI.getMemValueType(DL, Ty));
9833 OpInfo.CallOperand = StackSlot;
9834
9835 return Chain;
9836}
9837
9838/// GetRegistersForValue - Assign registers (virtual or physical) for the
9839/// specified operand. We prefer to assign virtual registers, to allow the
9840/// register allocator to handle the assignment process. However, if the asm
9841/// uses features that we can't model on machineinstrs, we have SDISel do the
9842/// allocation. This produces generally horrible, but correct, code.
9843///
9844/// OpInfo describes the operand
9845/// RefOpInfo describes the matching operand if any, the operand otherwise
9846static std::optional<unsigned>
9848 SDISelAsmOperandInfo &OpInfo,
9849 SDISelAsmOperandInfo &RefOpInfo) {
9850 LLVMContext &Context = *DAG.getContext();
9851 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9852
9856
9857 // No work to do for memory/address operands.
9858 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9859 OpInfo.ConstraintType == TargetLowering::C_Address)
9860 return std::nullopt;
9861
9862 // If this is a constraint for a single physreg, or a constraint for a
9863 // register class, find it.
9864 unsigned AssignedReg;
9865 const TargetRegisterClass *RC;
9866 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
9867 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
9868 // RC is unset only on failure. Return immediately.
9869 if (!RC)
9870 return std::nullopt;
9871
9872 // Get the actual register value type. This is important, because the user
9873 // may have asked for (e.g.) the AX register in i32 type. We need to
9874 // remember that AX is actually i16 to get the right extension.
9875 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
9876
9877 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
9878 // If this is an FP operand in an integer register (or visa versa), or more
9879 // generally if the operand value disagrees with the register class we plan
9880 // to stick it in, fix the operand type.
9881 //
9882 // If this is an input value, the bitcast to the new type is done now.
9883 // Bitcast for output value is done at the end of visitInlineAsm().
9884 if ((OpInfo.Type == InlineAsm::isOutput ||
9885 OpInfo.Type == InlineAsm::isInput) &&
9886 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
9887 // Try to convert to the first EVT that the reg class contains. If the
9888 // types are identical size, use a bitcast to convert (e.g. two differing
9889 // vector types). Note: output bitcast is done at the end of
9890 // visitInlineAsm().
9891 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
9892 // Exclude indirect inputs while they are unsupported because the code
9893 // to perform the load is missing and thus OpInfo.CallOperand still
9894 // refers to the input address rather than the pointed-to value.
9895 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
9896 OpInfo.CallOperand =
9897 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
9898 OpInfo.ConstraintVT = RegVT;
9899 // If the operand is an FP value and we want it in integer registers,
9900 // use the corresponding integer type. This turns an f64 value into
9901 // i64, which can be passed with two i32 values on a 32-bit machine.
9902 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
9903 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
9904 if (OpInfo.Type == InlineAsm::isInput)
9905 OpInfo.CallOperand =
9906 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
9907 OpInfo.ConstraintVT = VT;
9908 }
9909 }
9910 }
9911
9912 // No need to allocate a matching input constraint since the constraint it's
9913 // matching to has already been allocated.
9914 if (OpInfo.isMatchingInputConstraint())
9915 return std::nullopt;
9916
9917 EVT ValueVT = OpInfo.ConstraintVT;
9918 if (OpInfo.ConstraintVT == MVT::Other)
9919 ValueVT = RegVT;
9920
9921 // Initialize NumRegs.
9922 unsigned NumRegs = 1;
9923 if (OpInfo.ConstraintVT != MVT::Other)
9924 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
9925
9926 // If this is a constraint for a specific physical register, like {r17},
9927 // assign it now.
9928
9929 // If this associated to a specific register, initialize iterator to correct
9930 // place. If virtual, make sure we have enough registers
9931
9932 // Initialize iterator if necessary
9935
9936 // Do not check for single registers.
9937 if (AssignedReg) {
9938 I = std::find(I, RC->end(), AssignedReg);
9939 if (I == RC->end()) {
9940 // RC does not contain the selected register, which indicates a
9941 // mismatch between the register and the required type/bitwidth.
9942 return {AssignedReg};
9943 }
9944 }
9945
9946 for (; NumRegs; --NumRegs, ++I) {
9947 assert(I != RC->end() && "Ran out of registers to allocate!");
9948 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
9949 Regs.push_back(R);
9950 }
9951
9952 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
9953 return std::nullopt;
9954}
9955
9956static unsigned
9958 const std::vector<SDValue> &AsmNodeOperands) {
9959 // Scan until we find the definition we already emitted of this operand.
9960 unsigned CurOp = InlineAsm::Op_FirstOperand;
9961 for (; OperandNo; --OperandNo) {
9962 // Advance to the next operand.
9963 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
9964 const InlineAsm::Flag F(OpFlag);
9965 assert(
9966 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
9967 "Skipped past definitions?");
9968 CurOp += F.getNumOperandRegisters() + 1;
9969 }
9970 return CurOp;
9971}
9972
9973namespace {
9974
9975class ExtraFlags {
9976 unsigned Flags = 0;
9977
9978public:
9979 explicit ExtraFlags(const CallBase &Call) {
9980 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
9981 if (IA->hasSideEffects())
9983 if (IA->isAlignStack())
9985 if (Call.isConvergent())
9987 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
9988 }
9989
9990 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
9991 // Ideally, we would only check against memory constraints. However, the
9992 // meaning of an Other constraint can be target-specific and we can't easily
9993 // reason about it. Therefore, be conservative and set MayLoad/MayStore
9994 // for Other constraints as well.
9997 if (OpInfo.Type == InlineAsm::isInput)
9999 else if (OpInfo.Type == InlineAsm::isOutput)
10001 else if (OpInfo.Type == InlineAsm::isClobber)
10003 }
10004 }
10005
10006 unsigned get() const { return Flags; }
10007};
10008
10009} // end anonymous namespace
10010
10011static bool isFunction(SDValue Op) {
10012 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
10013 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10014 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10015
10016 // In normal "call dllimport func" instruction (non-inlineasm) it force
10017 // indirect access by specifing call opcode. And usually specially print
10018 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10019 // not do in this way now. (In fact, this is similar with "Data Access"
10020 // action). So here we ignore dllimport function.
10021 if (Fn && !Fn->hasDLLImportStorageClass())
10022 return true;
10023 }
10024 }
10025 return false;
10026}
10027
10028/// visitInlineAsm - Handle a call to an InlineAsm object.
10029void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10030 const BasicBlock *EHPadBB) {
10031 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10032
10033 /// ConstraintOperands - Information about all of the constraints.
10034 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10035
10036 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10038 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10039
10040 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
10041 // AsmDialect, MayLoad, MayStore).
10042 bool HasSideEffect = IA->hasSideEffects();
10043 ExtraFlags ExtraInfo(Call);
10044
10045 for (auto &T : TargetConstraints) {
10046 ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10047 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
10048
10049 if (OpInfo.CallOperandVal)
10050 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
10051
10052 if (!HasSideEffect)
10053 HasSideEffect = OpInfo.hasMemory(TLI);
10054
10055 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10056 // FIXME: Could we compute this on OpInfo rather than T?
10057
10058 // Compute the constraint code and ConstraintType to use.
10060
10061 if (T.ConstraintType == TargetLowering::C_Immediate &&
10062 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
10063 // We've delayed emitting a diagnostic like the "n" constraint because
10064 // inlining could cause an integer showing up.
10065 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
10066 "' expects an integer constant "
10067 "expression");
10068
10069 ExtraInfo.update(T);
10070 }
10071
10072 // We won't need to flush pending loads if this asm doesn't touch
10073 // memory and is nonvolatile.
10074 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
10075
10076 bool EmitEHLabels = isa<InvokeInst>(Call);
10077 if (EmitEHLabels) {
10078 assert(EHPadBB && "InvokeInst must have an EHPadBB");
10079 }
10080 bool IsCallBr = isa<CallBrInst>(Call);
10081
10082 if (IsCallBr || EmitEHLabels) {
10083 // If this is a callbr or invoke we need to flush pending exports since
10084 // inlineasm_br and invoke are terminators.
10085 // We need to do this before nodes are glued to the inlineasm_br node.
10086 Chain = getControlRoot();
10087 }
10088
10089 MCSymbol *BeginLabel = nullptr;
10090 if (EmitEHLabels) {
10091 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
10092 }
10093
10094 int OpNo = -1;
10095 SmallVector<StringRef> AsmStrs;
10096 IA->collectAsmStrs(AsmStrs);
10097
10098 // Second pass over the constraints: compute which constraint option to use.
10099 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10100 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10101 OpNo++;
10102
10103 // If this is an output operand with a matching input operand, look up the
10104 // matching input. If their types mismatch, e.g. one is an integer, the
10105 // other is floating point, or their sizes are different, flag it as an
10106 // error.
10107 if (OpInfo.hasMatchingInput()) {
10108 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
10109 patchMatchingInput(OpInfo, Input, DAG);
10110 }
10111
10112 // Compute the constraint code and ConstraintType to use.
10113 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10114
10115 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10116 OpInfo.Type == InlineAsm::isClobber) ||
10117 OpInfo.ConstraintType == TargetLowering::C_Address)
10118 continue;
10119
10120 // In Linux PIC model, there are 4 cases about value/label addressing:
10121 //
10122 // 1: Function call or Label jmp inside the module.
10123 // 2: Data access (such as global variable, static variable) inside module.
10124 // 3: Function call or Label jmp outside the module.
10125 // 4: Data access (such as global variable) outside the module.
10126 //
10127 // Due to current llvm inline asm architecture designed to not "recognize"
10128 // the asm code, there are quite troubles for us to treat mem addressing
10129 // differently for same value/adress used in different instuctions.
10130 // For example, in pic model, call a func may in plt way or direclty
10131 // pc-related, but lea/mov a function adress may use got.
10132 //
10133 // Here we try to "recognize" function call for the case 1 and case 3 in
10134 // inline asm. And try to adjust the constraint for them.
10135 //
10136 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10137 // label, so here we don't handle jmp function label now, but we need to
10138 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10139 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10140 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10141 TM.getCodeModel() != CodeModel::Large) {
10142 OpInfo.isIndirect = false;
10143 OpInfo.ConstraintType = TargetLowering::C_Address;
10144 }
10145
10146 // If this is a memory input, and if the operand is not indirect, do what we
10147 // need to provide an address for the memory input.
10148 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10149 !OpInfo.isIndirect) {
10150 assert((OpInfo.isMultipleAlternative ||
10151 (OpInfo.Type == InlineAsm::isInput)) &&
10152 "Can only indirectify direct input operands!");
10153
10154 // Memory operands really want the address of the value.
10155 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
10156
10157 // There is no longer a Value* corresponding to this operand.
10158 OpInfo.CallOperandVal = nullptr;
10159
10160 // It is now an indirect operand.
10161 OpInfo.isIndirect = true;
10162 }
10163
10164 }
10165
10166 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10167 std::vector<SDValue> AsmNodeOperands;
10168 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10169 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10170 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10171
10172 // If we have a !srcloc metadata node associated with it, we want to attach
10173 // this to the ultimately generated inline asm machineinstr. To do this, we
10174 // pass in the third operand as this (potentially null) inline asm MDNode.
10175 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10176 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10177
10178 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10179 // bits as operand 3.
10180 AsmNodeOperands.push_back(DAG.getTargetConstant(
10181 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10182
10183 // Third pass: Loop over operands to prepare DAG-level operands.. As part of
10184 // this, assign virtual and physical registers for inputs and otput.
10185 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10186 // Assign Registers.
10187 SDISelAsmOperandInfo &RefOpInfo =
10188 OpInfo.isMatchingInputConstraint()
10189 ? ConstraintOperands[OpInfo.getMatchedOperand()]
10190 : OpInfo;
10191 const auto RegError =
10192 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
10193 if (RegError) {
10194 const MachineFunction &MF = DAG.getMachineFunction();
10195 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10196 const char *RegName = TRI.getName(*RegError);
10197 emitInlineAsmError(Call, "register '" + Twine(RegName) +
10198 "' allocated for constraint '" +
10199 Twine(OpInfo.ConstraintCode) +
10200 "' does not match required type");
10201 return;
10202 }
10203
10204 auto DetectWriteToReservedRegister = [&]() {
10205 const MachineFunction &MF = DAG.getMachineFunction();
10206 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10207 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10208 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10209 const char *RegName = TRI.getName(Reg);
10210 emitInlineAsmError(Call, "write to reserved register '" +
10211 Twine(RegName) + "'");
10212 return true;
10213 }
10214 }
10215 return false;
10216 };
10217 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10218 (OpInfo.Type == InlineAsm::isInput &&
10219 !OpInfo.isMatchingInputConstraint())) &&
10220 "Only address as input operand is allowed.");
10221
10222 switch (OpInfo.Type) {
10224 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10225 const InlineAsm::ConstraintCode ConstraintID =
10226 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10228 "Failed to convert memory constraint code to constraint id.");
10229
10230 // Add information to the INLINEASM node to know about this output.
10231 InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1);
10232 OpFlags.setMemConstraint(ConstraintID);
10233 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
10234 MVT::i32));
10235 AsmNodeOperands.push_back(OpInfo.CallOperand);
10236 } else {
10237 // Otherwise, this outputs to a register (directly for C_Register /
10238 // C_RegisterClass, and a target-defined fashion for
10239 // C_Immediate/C_Other). Find a register that we can use.
10240 if (OpInfo.AssignedRegs.Regs.empty()) {
10241 emitInlineAsmError(
10242 Call, "couldn't allocate output register for constraint '" +
10243 Twine(OpInfo.ConstraintCode) + "'");
10244 return;
10245 }
10246
10247 if (DetectWriteToReservedRegister())
10248 return;
10249
10250 // Add information to the INLINEASM node to know that this register is
10251 // set.
10252 OpInfo.AssignedRegs.AddInlineAsmOperands(
10253 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10255 false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
10256 }
10257 break;
10258
10259 case InlineAsm::isInput:
10260 case InlineAsm::isLabel: {
10261 SDValue InOperandVal = OpInfo.CallOperand;
10262
10263 if (OpInfo.isMatchingInputConstraint()) {
10264 // If this is required to match an output register we have already set,
10265 // just use its register.
10266 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10267 AsmNodeOperands);
10268 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
10269 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10270 if (OpInfo.isIndirect) {
10271 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10272 emitInlineAsmError(Call, "inline asm not supported yet: "
10273 "don't know how to handle tied "
10274 "indirect register inputs");
10275 return;
10276 }
10277
10279 MachineFunction &MF = DAG.getMachineFunction();
10280 MachineRegisterInfo &MRI = MF.getRegInfo();
10281 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10282 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
10283 Register TiedReg = R->getReg();
10284 MVT RegVT = R->getSimpleValueType(0);
10285 const TargetRegisterClass *RC =
10286 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10287 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10288 : TRI.getMinimalPhysRegClass(TiedReg);
10289 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
10290 Regs.push_back(MRI.createVirtualRegister(RC));
10291
10292 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10293
10294 SDLoc dl = getCurSDLoc();
10295 // Use the produced MatchedRegs object to
10296 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
10297 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
10298 OpInfo.getMatchedOperand(), dl, DAG,
10299 AsmNodeOperands);
10300 break;
10301 }
10302
10303 assert(Flag.isMemKind() && "Unknown matching constraint!");
10304 assert(Flag.getNumOperandRegisters() == 1 &&
10305 "Unexpected number of operands");
10306 // Add information to the INLINEASM node to know about this input.
10307 // See InlineAsm.h isUseOperandTiedToDef.
10308 Flag.clearMemConstraint();
10309 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10310 AsmNodeOperands.push_back(DAG.getTargetConstant(
10311 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10312 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
10313 break;
10314 }
10315
10316 // Treat indirect 'X' constraint as memory.
10317 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10318 OpInfo.isIndirect)
10319 OpInfo.ConstraintType = TargetLowering::C_Memory;
10320
10321 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10322 OpInfo.ConstraintType == TargetLowering::C_Other) {
10323 std::vector<SDValue> Ops;
10324 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10325 Ops, DAG);
10326 if (Ops.empty()) {
10327 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10328 if (isa<ConstantSDNode>(InOperandVal)) {
10329 emitInlineAsmError(Call, "value out of range for constraint '" +
10330 Twine(OpInfo.ConstraintCode) + "'");
10331 return;
10332 }
10333
10334 emitInlineAsmError(Call,
10335 "invalid operand for inline asm constraint '" +
10336 Twine(OpInfo.ConstraintCode) + "'");
10337 return;
10338 }
10339
10340 // Add information to the INLINEASM node to know about this input.
10341 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10342 AsmNodeOperands.push_back(DAG.getTargetConstant(
10343 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10344 llvm::append_range(AsmNodeOperands, Ops);
10345 break;
10346 }
10347
10348 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10349 assert((OpInfo.isIndirect ||
10350 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10351 "Operand must be indirect to be a mem!");
10352 assert(InOperandVal.getValueType() ==
10353 TLI.getPointerTy(DAG.getDataLayout()) &&
10354 "Memory operands expect pointer values");
10355
10356 const InlineAsm::ConstraintCode ConstraintID =
10357 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10359 "Failed to convert memory constraint code to constraint id.");
10360
10361 // Add information to the INLINEASM node to know about this input.
10362 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10363 ResOpType.setMemConstraint(ConstraintID);
10364 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10365 getCurSDLoc(),
10366 MVT::i32));
10367 AsmNodeOperands.push_back(InOperandVal);
10368 break;
10369 }
10370
10371 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10372 const InlineAsm::ConstraintCode ConstraintID =
10373 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10375 "Failed to convert memory constraint code to constraint id.");
10376
10377 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10378
10379 SDValue AsmOp = InOperandVal;
10380 if (isFunction(InOperandVal)) {
10381 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10382 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10383 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10384 InOperandVal.getValueType(),
10385 GA->getOffset());
10386 }
10387
10388 // Add information to the INLINEASM node to know about this input.
10389 ResOpType.setMemConstraint(ConstraintID);
10390
10391 AsmNodeOperands.push_back(
10392 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10393
10394 AsmNodeOperands.push_back(AsmOp);
10395 break;
10396 }
10397
10398 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10399 OpInfo.ConstraintType != TargetLowering::C_Register) {
10400 emitInlineAsmError(Call, "unknown asm constraint '" +
10401 Twine(OpInfo.ConstraintCode) + "'");
10402 return;
10403 }
10404
10405 // TODO: Support this.
10406 if (OpInfo.isIndirect) {
10407 emitInlineAsmError(
10408 Call, "Don't know how to handle indirect register inputs yet "
10409 "for constraint '" +
10410 Twine(OpInfo.ConstraintCode) + "'");
10411 return;
10412 }
10413
10414 // Copy the input into the appropriate registers.
10415 if (OpInfo.AssignedRegs.Regs.empty()) {
10416 emitInlineAsmError(Call,
10417 "couldn't allocate input reg for constraint '" +
10418 Twine(OpInfo.ConstraintCode) + "'");
10419 return;
10420 }
10421
10422 if (DetectWriteToReservedRegister())
10423 return;
10424
10425 SDLoc dl = getCurSDLoc();
10426
10427 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10428 &Call);
10429
10430 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10431 0, dl, DAG, AsmNodeOperands);
10432 break;
10433 }
10435 // Add the clobbered value to the operand list, so that the register
10436 // allocator is aware that the physreg got clobbered.
10437 if (!OpInfo.AssignedRegs.Regs.empty())
10439 false, 0, getCurSDLoc(), DAG,
10440 AsmNodeOperands);
10441 break;
10442 }
10443 }
10444
10445 // Finish up input operands. Set the input chain and add the flag last.
10446 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10447 if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10448
10449 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10450 Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10451 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10452 Glue = Chain.getValue(1);
10453
10454 // Do additional work to generate outputs.
10455
10456 SmallVector<EVT, 1> ResultVTs;
10457 SmallVector<SDValue, 1> ResultValues;
10458 SmallVector<SDValue, 8> OutChains;
10459
10460 llvm::Type *CallResultType = Call.getType();
10461 ArrayRef<Type *> ResultTypes;
10462 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10463 ResultTypes = StructResult->elements();
10464 else if (!CallResultType->isVoidTy())
10465 ResultTypes = ArrayRef(CallResultType);
10466
10467 auto CurResultType = ResultTypes.begin();
10468 auto handleRegAssign = [&](SDValue V) {
10469 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10470 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10471 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10472 ++CurResultType;
10473 // If the type of the inline asm call site return value is different but has
10474 // same size as the type of the asm output bitcast it. One example of this
10475 // is for vectors with different width / number of elements. This can
10476 // happen for register classes that can contain multiple different value
10477 // types. The preg or vreg allocated may not have the same VT as was
10478 // expected.
10479 //
10480 // This can also happen for a return value that disagrees with the register
10481 // class it is put in, eg. a double in a general-purpose register on a
10482 // 32-bit machine.
10483 if (ResultVT != V.getValueType() &&
10484 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10485 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10486 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10487 V.getValueType().isInteger()) {
10488 // If a result value was tied to an input value, the computed result
10489 // may have a wider width than the expected result. Extract the
10490 // relevant portion.
10491 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10492 }
10493 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10494 ResultVTs.push_back(ResultVT);
10495 ResultValues.push_back(V);
10496 };
10497
10498 // Deal with output operands.
10499 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10500 if (OpInfo.Type == InlineAsm::isOutput) {
10501 SDValue Val;
10502 // Skip trivial output operands.
10503 if (OpInfo.AssignedRegs.Regs.empty())
10504 continue;
10505
10506 switch (OpInfo.ConstraintType) {
10509 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10510 Chain, &Glue, &Call);
10511 break;
10514 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10515 OpInfo, DAG);
10516 break;
10518 break; // Already handled.
10520 break; // Silence warning.
10522 assert(false && "Unexpected unknown constraint");
10523 }
10524
10525 // Indirect output manifest as stores. Record output chains.
10526 if (OpInfo.isIndirect) {
10527 const Value *Ptr = OpInfo.CallOperandVal;
10528 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10529 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10530 MachinePointerInfo(Ptr));
10531 OutChains.push_back(Store);
10532 } else {
10533 // generate CopyFromRegs to associated registers.
10534 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10535 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10536 for (const SDValue &V : Val->op_values())
10537 handleRegAssign(V);
10538 } else
10539 handleRegAssign(Val);
10540 }
10541 }
10542 }
10543
10544 // Set results.
10545 if (!ResultValues.empty()) {
10546 assert(CurResultType == ResultTypes.end() &&
10547 "Mismatch in number of ResultTypes");
10548 assert(ResultValues.size() == ResultTypes.size() &&
10549 "Mismatch in number of output operands in asm result");
10550
10552 DAG.getVTList(ResultVTs), ResultValues);
10553 setValue(&Call, V);
10554 }
10555
10556 // Collect store chains.
10557 if (!OutChains.empty())
10558 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10559
10560 if (EmitEHLabels) {
10561 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10562 }
10563
10564 // Only Update Root if inline assembly has a memory effect.
10565 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10566 EmitEHLabels)
10567 DAG.setRoot(Chain);
10568}
10569
10570void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10571 const Twine &Message) {
10572 LLVMContext &Ctx = *DAG.getContext();
10573 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10574
10575 // Make sure we leave the DAG in a valid state
10576 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10577 SmallVector<EVT, 1> ValueVTs;
10578 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10579
10580 if (ValueVTs.empty())
10581 return;
10582
10584 for (const EVT &VT : ValueVTs)
10585 Ops.push_back(DAG.getUNDEF(VT));
10586
10587 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10588}
10589
10590void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10591 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10592 MVT::Other, getRoot(),
10593 getValue(I.getArgOperand(0)),
10594 DAG.getSrcValue(I.getArgOperand(0))));
10595}
10596
10597void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10598 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10599 const DataLayout &DL = DAG.getDataLayout();
10600 SDValue V = DAG.getVAArg(
10601 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10602 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10603 DL.getABITypeAlign(I.getType()).value());
10604 DAG.setRoot(V.getValue(1));
10605
10606 if (I.getType()->isPointerTy())
10607 V = DAG.getPtrExtOrTrunc(
10608 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10609 setValue(&I, V);
10610}
10611
10612void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10613 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10614 MVT::Other, getRoot(),
10615 getValue(I.getArgOperand(0)),
10616 DAG.getSrcValue(I.getArgOperand(0))));
10617}
10618
10619void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10620 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10621 MVT::Other, getRoot(),
10622 getValue(I.getArgOperand(0)),
10623 getValue(I.getArgOperand(1)),
10624 DAG.getSrcValue(I.getArgOperand(0)),
10625 DAG.getSrcValue(I.getArgOperand(1))));
10626}
10627
10629 const Instruction &I,
10630 SDValue Op) {
10631 std::optional<ConstantRange> CR = getRange(I);
10632
10633 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10634 return Op;
10635
10636 APInt Lo = CR->getUnsignedMin();
10637 if (!Lo.isMinValue())
10638 return Op;
10639
10640 APInt Hi = CR->getUnsignedMax();
10641 unsigned Bits = std::max(Hi.getActiveBits(),
10642 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10643
10644 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10645
10646 SDLoc SL = getCurSDLoc();
10647
10648 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10649 DAG.getValueType(SmallVT));
10650 unsigned NumVals = Op.getNode()->getNumValues();
10651 if (NumVals == 1)
10652 return ZExt;
10653
10655
10656 Ops.push_back(ZExt);
10657 for (unsigned I = 1; I != NumVals; ++I)
10658 Ops.push_back(Op.getValue(I));
10659
10660 return DAG.getMergeValues(Ops, SL);
10661}
10662
10663/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10664/// the call being lowered.
10665///
10666/// This is a helper for lowering intrinsics that follow a target calling
10667/// convention or require stack pointer adjustment. Only a subset of the
10668/// intrinsic's operands need to participate in the calling convention.
10671 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10672 AttributeSet RetAttrs, bool IsPatchPoint) {
10674 Args.reserve(NumArgs);
10675
10676 // Populate the argument list.
10677 // Attributes for args start at offset 1, after the return attribute.
10678 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10679 ArgI != ArgE; ++ArgI) {
10680 const Value *V = Call->getOperand(ArgI);
10681
10682 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10683
10684 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10685 Entry.setAttributes(Call, ArgI);
10686 Args.push_back(Entry);
10687 }
10688
10690 .setChain(getRoot())
10691 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10692 RetAttrs)
10693 .setDiscardResult(Call->use_empty())
10694 .setIsPatchPoint(IsPatchPoint)
10696 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10697}
10698
10699/// Add a stack map intrinsic call's live variable operands to a stackmap
10700/// or patchpoint target node's operand list.
10701///
10702/// Constants are converted to TargetConstants purely as an optimization to
10703/// avoid constant materialization and register allocation.
10704///
10705/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10706/// generate addess computation nodes, and so FinalizeISel can convert the
10707/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10708/// address materialization and register allocation, but may also be required
10709/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10710/// alloca in the entry block, then the runtime may assume that the alloca's
10711/// StackMap location can be read immediately after compilation and that the
10712/// location is valid at any point during execution (this is similar to the
10713/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10714/// only available in a register, then the runtime would need to trap when
10715/// execution reaches the StackMap in order to read the alloca's location.
10716static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10718 SelectionDAGBuilder &Builder) {
10719 SelectionDAG &DAG = Builder.DAG;
10720 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10721 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10722
10723 // Things on the stack are pointer-typed, meaning that they are already
10724 // legal and can be emitted directly to target nodes.
10726 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10727 } else {
10728 // Otherwise emit a target independent node to be legalised.
10729 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10730 }
10731 }
10732}
10733
10734/// Lower llvm.experimental.stackmap.
10735void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10736 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10737 // [live variables...])
10738
10739 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10740
10741 SDValue Chain, InGlue, Callee;
10743
10744 SDLoc DL = getCurSDLoc();
10746
10747 // The stackmap intrinsic only records the live variables (the arguments
10748 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10749 // intrinsic, this won't be lowered to a function call. This means we don't
10750 // have to worry about calling conventions and target specific lowering code.
10751 // Instead we perform the call lowering right here.
10752 //
10753 // chain, flag = CALLSEQ_START(chain, 0, 0)
10754 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10755 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10756 //
10757 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10758 InGlue = Chain.getValue(1);
10759
10760 // Add the STACKMAP operands, starting with DAG house-keeping.
10761 Ops.push_back(Chain);
10762 Ops.push_back(InGlue);
10763
10764 // Add the <id>, <numShadowBytes> operands.
10765 //
10766 // These do not require legalisation, and can be emitted directly to target
10767 // constant nodes.
10769 assert(ID.getValueType() == MVT::i64);
10770 SDValue IDConst =
10771 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10772 Ops.push_back(IDConst);
10773
10774 SDValue Shad = getValue(CI.getArgOperand(1));
10775 assert(Shad.getValueType() == MVT::i32);
10776 SDValue ShadConst =
10777 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
10778 Ops.push_back(ShadConst);
10779
10780 // Add the live variables.
10781 addStackMapLiveVars(CI, 2, DL, Ops, *this);
10782
10783 // Create the STACKMAP node.
10784 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10785 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10786 InGlue = Chain.getValue(1);
10787
10788 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10789
10790 // Stackmaps don't generate values, so nothing goes into the NodeMap.
10791
10792 // Set the root to the target-lowered call chain.
10793 DAG.setRoot(Chain);
10794
10795 // Inform the Frame Information that we have a stackmap in this function.
10796 FuncInfo.MF->getFrameInfo().setHasStackMap();
10797}
10798
10799/// Lower llvm.experimental.patchpoint directly to its target opcode.
10800void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10801 const BasicBlock *EHPadBB) {
10802 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10803 // i32 <numBytes>,
10804 // i8* <target>,
10805 // i32 <numArgs>,
10806 // [Args...],
10807 // [live variables...])
10808
10810 bool IsAnyRegCC = CC == CallingConv::AnyReg;
10811 bool HasDef = !CB.getType()->isVoidTy();
10812 SDLoc dl = getCurSDLoc();
10814
10815 // Handle immediate and symbolic callees.
10816 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10817 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10818 /*isTarget=*/true);
10819 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
10820 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
10821 SDLoc(SymbolicCallee),
10822 SymbolicCallee->getValueType(0));
10823
10824 // Get the real number of arguments participating in the call <numArgs>
10826 unsigned NumArgs = NArgVal->getAsZExtVal();
10827
10828 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
10829 // Intrinsics include all meta-operands up to but not including CC.
10830 unsigned NumMetaOpers = PatchPointOpers::CCPos;
10831 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
10832 "Not enough arguments provided to the patchpoint intrinsic");
10833
10834 // For AnyRegCC the arguments are lowered later on manually.
10835 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
10836 Type *ReturnTy =
10837 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
10838
10839 TargetLowering::CallLoweringInfo CLI(DAG);
10840 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
10841 ReturnTy, CB.getAttributes().getRetAttrs(), true);
10842 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
10843
10844 SDNode *CallEnd = Result.second.getNode();
10845 if (CallEnd->getOpcode() == ISD::EH_LABEL)
10846 CallEnd = CallEnd->getOperand(0).getNode();
10847 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
10848 CallEnd = CallEnd->getOperand(0).getNode();
10849
10850 /// Get a call instruction from the call sequence chain.
10851 /// Tail calls are not allowed.
10852 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
10853 "Expected a callseq node.");
10854 SDNode *Call = CallEnd->getOperand(0).getNode();
10855 bool HasGlue = Call->getGluedNode();
10856
10857 // Replace the target specific call node with the patchable intrinsic.
10859
10860 // Push the chain.
10861 Ops.push_back(*(Call->op_begin()));
10862
10863 // Optionally, push the glue (if any).
10864 if (HasGlue)
10865 Ops.push_back(*(Call->op_end() - 1));
10866
10867 // Push the register mask info.
10868 if (HasGlue)
10869 Ops.push_back(*(Call->op_end() - 2));
10870 else
10871 Ops.push_back(*(Call->op_end() - 1));
10872
10873 // Add the <id> and <numBytes> constants.
10875 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
10877 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
10878
10879 // Add the callee.
10880 Ops.push_back(Callee);
10881
10882 // Adjust <numArgs> to account for any arguments that have been passed on the
10883 // stack instead.
10884 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
10885 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
10886 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
10887 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
10888
10889 // Add the calling convention
10890 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
10891
10892 // Add the arguments we omitted previously. The register allocator should
10893 // place these in any free register.
10894 if (IsAnyRegCC)
10895 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
10896 Ops.push_back(getValue(CB.getArgOperand(i)));
10897
10898 // Push the arguments from the call instruction.
10899 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
10900 Ops.append(Call->op_begin() + 2, e);
10901
10902 // Push live variables for the stack map.
10903 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
10904
10905 SDVTList NodeTys;
10906 if (IsAnyRegCC && HasDef) {
10907 // Create the return types based on the intrinsic definition
10908 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10909 SmallVector<EVT, 3> ValueVTs;
10910 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
10911 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
10912
10913 // There is always a chain and a glue type at the end
10914 ValueVTs.push_back(MVT::Other);
10915 ValueVTs.push_back(MVT::Glue);
10916 NodeTys = DAG.getVTList(ValueVTs);
10917 } else
10918 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10919
10920 // Replace the target specific call node with a PATCHPOINT node.
10921 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
10922
10923 // Update the NodeMap.
10924 if (HasDef) {
10925 if (IsAnyRegCC)
10926 setValue(&CB, SDValue(PPV.getNode(), 0));
10927 else
10928 setValue(&CB, Result.first);
10929 }
10930
10931 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
10932 // call sequence. Furthermore the location of the chain and glue can change
10933 // when the AnyReg calling convention is used and the intrinsic returns a
10934 // value.
10935 if (IsAnyRegCC && HasDef) {
10936 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
10937 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
10938 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
10939 } else
10940 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
10941 DAG.DeleteNode(Call);
10942
10943 // Inform the Frame Information that we have a patchpoint in this function.
10944 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
10945}
10946
10947void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
10948 unsigned Intrinsic) {
10949 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10950 SDValue Op1 = getValue(I.getArgOperand(0));
10951 SDValue Op2;
10952 if (I.arg_size() > 1)
10953 Op2 = getValue(I.getArgOperand(1));
10954 SDLoc dl = getCurSDLoc();
10955 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
10956 SDValue Res;
10957 SDNodeFlags SDFlags;
10958 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
10959 SDFlags.copyFMF(*FPMO);
10960
10961 switch (Intrinsic) {
10962 case Intrinsic::vector_reduce_fadd:
10963 if (SDFlags.hasAllowReassociation())
10964 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
10965 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
10966 SDFlags);
10967 else
10968 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
10969 break;
10970 case Intrinsic::vector_reduce_fmul:
10971 if (SDFlags.hasAllowReassociation())
10972 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
10973 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
10974 SDFlags);
10975 else
10976 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
10977 break;
10978 case Intrinsic::vector_reduce_add:
10979 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
10980 break;
10981 case Intrinsic::vector_reduce_mul:
10982 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
10983 break;
10984 case Intrinsic::vector_reduce_and:
10985 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
10986 break;
10987 case Intrinsic::vector_reduce_or:
10988 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
10989 break;
10990 case Intrinsic::vector_reduce_xor:
10991 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
10992 break;
10993 case Intrinsic::vector_reduce_smax:
10994 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
10995 break;
10996 case Intrinsic::vector_reduce_smin:
10997 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
10998 break;
10999 case Intrinsic::vector_reduce_umax:
11000 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
11001 break;
11002 case Intrinsic::vector_reduce_umin:
11003 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
11004 break;
11005 case Intrinsic::vector_reduce_fmax:
11006 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
11007 break;
11008 case Intrinsic::vector_reduce_fmin:
11009 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
11010 break;
11011 case Intrinsic::vector_reduce_fmaximum:
11012 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
11013 break;
11014 case Intrinsic::vector_reduce_fminimum:
11015 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11016 break;
11017 default:
11018 llvm_unreachable("Unhandled vector reduce intrinsic");
11019 }
11020 setValue(&I, Res);
11021}
11022
11023/// Returns an AttributeList representing the attributes applied to the return
11024/// value of the given call.
11027 if (CLI.RetSExt)
11028 Attrs.push_back(Attribute::SExt);
11029 if (CLI.RetZExt)
11030 Attrs.push_back(Attribute::ZExt);
11031 if (CLI.IsInReg)
11032 Attrs.push_back(Attribute::InReg);
11033
11034 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11035 Attrs);
11036}
11037
11038/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11039/// implementation, which just calls LowerCall.
11040/// FIXME: When all targets are
11041/// migrated to using LowerCall, this hook should be integrated into SDISel.
11042std::pair<SDValue, SDValue>
11044 LLVMContext &Context = CLI.RetTy->getContext();
11045
11046 // Handle the incoming return values from the call.
11047 CLI.Ins.clear();
11048 SmallVector<Type *, 4> RetOrigTys;
11050 auto &DL = CLI.DAG.getDataLayout();
11051 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11052
11053 SmallVector<EVT, 4> RetVTs;
11054 if (CLI.RetTy != CLI.OrigRetTy) {
11055 assert(RetOrigTys.size() == 1 &&
11056 "Only supported for non-aggregate returns");
11057 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11058 } else {
11059 for (Type *Ty : RetOrigTys)
11060 RetVTs.push_back(getValueType(DL, Ty));
11061 }
11062
11063 if (CLI.IsPostTypeLegalization) {
11064 // If we are lowering a libcall after legalization, split the return type.
11065 SmallVector<Type *, 4> OldRetOrigTys;
11066 SmallVector<EVT, 4> OldRetVTs;
11067 SmallVector<TypeSize, 4> OldOffsets;
11068 RetOrigTys.swap(OldRetOrigTys);
11069 RetVTs.swap(OldRetVTs);
11070 Offsets.swap(OldOffsets);
11071
11072 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11073 EVT RetVT = OldRetVTs[i];
11074 uint64_t Offset = OldOffsets[i];
11075 MVT RegisterVT = getRegisterType(Context, RetVT);
11076 unsigned NumRegs = getNumRegisters(Context, RetVT);
11077 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11078 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11079 RetVTs.append(NumRegs, RegisterVT);
11080 for (unsigned j = 0; j != NumRegs; ++j)
11081 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11082 }
11083 }
11084
11086 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11087
11088 bool CanLowerReturn =
11090 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11091
11092 SDValue DemoteStackSlot;
11093 int DemoteStackIdx = -100;
11094 if (!CanLowerReturn) {
11095 // FIXME: equivalent assert?
11096 // assert(!CS.hasInAllocaArgument() &&
11097 // "sret demotion is incompatible with inalloca");
11098 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11099 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11101 DemoteStackIdx =
11102 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11103 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11104
11105 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11106 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11107 Entry.IsSRet = true;
11108 Entry.Alignment = Alignment;
11109 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11110 CLI.NumFixedArgs += 1;
11111 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11112 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11113
11114 // sret demotion isn't compatible with tail-calls, since the sret argument
11115 // points into the callers stack frame.
11116 CLI.IsTailCall = false;
11117 } else {
11118 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11119 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11120 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11121 ISD::ArgFlagsTy Flags;
11122 if (NeedsRegBlock) {
11123 Flags.setInConsecutiveRegs();
11124 if (I == RetVTs.size() - 1)
11125 Flags.setInConsecutiveRegsLast();
11126 }
11127 EVT VT = RetVTs[I];
11128 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11129 unsigned NumRegs =
11130 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11131 for (unsigned i = 0; i != NumRegs; ++i) {
11132 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11134 if (CLI.RetTy->isPointerTy()) {
11135 Ret.Flags.setPointer();
11136 Ret.Flags.setPointerAddrSpace(
11137 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11138 }
11139 if (CLI.RetSExt)
11140 Ret.Flags.setSExt();
11141 if (CLI.RetZExt)
11142 Ret.Flags.setZExt();
11143 if (CLI.IsInReg)
11144 Ret.Flags.setInReg();
11145 CLI.Ins.push_back(Ret);
11146 }
11147 }
11148 }
11149
11150 // We push in swifterror return as the last element of CLI.Ins.
11151 ArgListTy &Args = CLI.getArgs();
11152 if (supportSwiftError()) {
11153 for (const ArgListEntry &Arg : Args) {
11154 if (Arg.IsSwiftError) {
11155 ISD::ArgFlagsTy Flags;
11156 Flags.setSwiftError();
11158 PointerType::getUnqual(Context),
11159 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11160 CLI.Ins.push_back(Ret);
11161 }
11162 }
11163 }
11164
11165 // Handle all of the outgoing arguments.
11166 CLI.Outs.clear();
11167 CLI.OutVals.clear();
11168 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11169 SmallVector<Type *, 4> OrigArgTys;
11170 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11171 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11172 Type *FinalType = Args[i].Ty;
11173 if (Args[i].IsByVal)
11174 FinalType = Args[i].IndirectType;
11175 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11176 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11177 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11178 ++Value) {
11179 Type *OrigArgTy = OrigArgTys[Value];
11180 Type *ArgTy = OrigArgTy;
11181 if (Args[i].Ty != Args[i].OrigTy) {
11182 assert(Value == 0 && "Only supported for non-aggregate arguments");
11183 ArgTy = Args[i].Ty;
11184 }
11185
11186 EVT VT = getValueType(DL, ArgTy);
11187 SDValue Op = SDValue(Args[i].Node.getNode(),
11188 Args[i].Node.getResNo() + Value);
11189 ISD::ArgFlagsTy Flags;
11190
11191 // Certain targets (such as MIPS), may have a different ABI alignment
11192 // for a type depending on the context. Give the target a chance to
11193 // specify the alignment it wants.
11194 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11195 Flags.setOrigAlign(OriginalAlignment);
11196
11197 if (i >= CLI.NumFixedArgs)
11198 Flags.setVarArg();
11199 if (ArgTy->isPointerTy()) {
11200 Flags.setPointer();
11201 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11202 }
11203 if (Args[i].IsZExt)
11204 Flags.setZExt();
11205 if (Args[i].IsSExt)
11206 Flags.setSExt();
11207 if (Args[i].IsNoExt)
11208 Flags.setNoExt();
11209 if (Args[i].IsInReg) {
11210 // If we are using vectorcall calling convention, a structure that is
11211 // passed InReg - is surely an HVA
11213 isa<StructType>(FinalType)) {
11214 // The first value of a structure is marked
11215 if (0 == Value)
11216 Flags.setHvaStart();
11217 Flags.setHva();
11218 }
11219 // Set InReg Flag
11220 Flags.setInReg();
11221 }
11222 if (Args[i].IsSRet)
11223 Flags.setSRet();
11224 if (Args[i].IsSwiftSelf)
11225 Flags.setSwiftSelf();
11226 if (Args[i].IsSwiftAsync)
11227 Flags.setSwiftAsync();
11228 if (Args[i].IsSwiftError)
11229 Flags.setSwiftError();
11230 if (Args[i].IsCFGuardTarget)
11231 Flags.setCFGuardTarget();
11232 if (Args[i].IsByVal)
11233 Flags.setByVal();
11234 if (Args[i].IsByRef)
11235 Flags.setByRef();
11236 if (Args[i].IsPreallocated) {
11237 Flags.setPreallocated();
11238 // Set the byval flag for CCAssignFn callbacks that don't know about
11239 // preallocated. This way we can know how many bytes we should've
11240 // allocated and how many bytes a callee cleanup function will pop. If
11241 // we port preallocated to more targets, we'll have to add custom
11242 // preallocated handling in the various CC lowering callbacks.
11243 Flags.setByVal();
11244 }
11245 if (Args[i].IsInAlloca) {
11246 Flags.setInAlloca();
11247 // Set the byval flag for CCAssignFn callbacks that don't know about
11248 // inalloca. This way we can know how many bytes we should've allocated
11249 // and how many bytes a callee cleanup function will pop. If we port
11250 // inalloca to more targets, we'll have to add custom inalloca handling
11251 // in the various CC lowering callbacks.
11252 Flags.setByVal();
11253 }
11254 Align MemAlign;
11255 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11256 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11257 Flags.setByValSize(FrameSize);
11258
11259 // info is not there but there are cases it cannot get right.
11260 if (auto MA = Args[i].Alignment)
11261 MemAlign = *MA;
11262 else
11263 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11264 } else if (auto MA = Args[i].Alignment) {
11265 MemAlign = *MA;
11266 } else {
11267 MemAlign = OriginalAlignment;
11268 }
11269 Flags.setMemAlign(MemAlign);
11270 if (Args[i].IsNest)
11271 Flags.setNest();
11272 if (NeedsRegBlock)
11273 Flags.setInConsecutiveRegs();
11274
11275 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11276 unsigned NumParts =
11277 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11278 SmallVector<SDValue, 4> Parts(NumParts);
11279 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11280
11281 if (Args[i].IsSExt)
11282 ExtendKind = ISD::SIGN_EXTEND;
11283 else if (Args[i].IsZExt)
11284 ExtendKind = ISD::ZERO_EXTEND;
11285
11286 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11287 // for now.
11288 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11290 assert((CLI.RetTy == Args[i].Ty ||
11291 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11293 Args[i].Ty->getPointerAddressSpace())) &&
11294 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11295 // Before passing 'returned' to the target lowering code, ensure that
11296 // either the register MVT and the actual EVT are the same size or that
11297 // the return value and argument are extended in the same way; in these
11298 // cases it's safe to pass the argument register value unchanged as the
11299 // return register value (although it's at the target's option whether
11300 // to do so)
11301 // TODO: allow code generation to take advantage of partially preserved
11302 // registers rather than clobbering the entire register when the
11303 // parameter extension method is not compatible with the return
11304 // extension method
11305 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11306 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11307 CLI.RetZExt == Args[i].IsZExt))
11308 Flags.setReturned();
11309 }
11310
11311 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11312 CLI.CallConv, ExtendKind);
11313
11314 for (unsigned j = 0; j != NumParts; ++j) {
11315 // if it isn't first piece, alignment must be 1
11316 // For scalable vectors the scalable part is currently handled
11317 // by individual targets, so we just use the known minimum size here.
11318 ISD::OutputArg MyFlags(
11319 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11320 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11321 if (NumParts > 1 && j == 0)
11322 MyFlags.Flags.setSplit();
11323 else if (j != 0) {
11324 MyFlags.Flags.setOrigAlign(Align(1));
11325 if (j == NumParts - 1)
11326 MyFlags.Flags.setSplitEnd();
11327 }
11328
11329 CLI.Outs.push_back(MyFlags);
11330 CLI.OutVals.push_back(Parts[j]);
11331 }
11332
11333 if (NeedsRegBlock && Value == NumValues - 1)
11334 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11335 }
11336 }
11337
11339 CLI.Chain = LowerCall(CLI, InVals);
11340
11341 // Update CLI.InVals to use outside of this function.
11342 CLI.InVals = InVals;
11343
11344 // Verify that the target's LowerCall behaved as expected.
11345 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11346 "LowerCall didn't return a valid chain!");
11347 assert((!CLI.IsTailCall || InVals.empty()) &&
11348 "LowerCall emitted a return value for a tail call!");
11349 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11350 "LowerCall didn't emit the correct number of values!");
11351
11352 // For a tail call, the return value is merely live-out and there aren't
11353 // any nodes in the DAG representing it. Return a special value to
11354 // indicate that a tail call has been emitted and no more Instructions
11355 // should be processed in the current block.
11356 if (CLI.IsTailCall) {
11357 CLI.DAG.setRoot(CLI.Chain);
11358 return std::make_pair(SDValue(), SDValue());
11359 }
11360
11361#ifndef NDEBUG
11362 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11363 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11364 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11365 "LowerCall emitted a value with the wrong type!");
11366 }
11367#endif
11368
11369 SmallVector<SDValue, 4> ReturnValues;
11370 if (!CanLowerReturn) {
11371 // The instruction result is the result of loading from the
11372 // hidden sret parameter.
11373 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11374
11375 unsigned NumValues = RetVTs.size();
11376 ReturnValues.resize(NumValues);
11377 SmallVector<SDValue, 4> Chains(NumValues);
11378
11379 // An aggregate return value cannot wrap around the address space, so
11380 // offsets to its parts don't wrap either.
11382 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11383 for (unsigned i = 0; i < NumValues; ++i) {
11385 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11387 SDValue L = CLI.DAG.getLoad(
11388 RetVTs[i], CLI.DL, CLI.Chain, Add,
11390 DemoteStackIdx, Offsets[i]),
11391 HiddenSRetAlign);
11392 ReturnValues[i] = L;
11393 Chains[i] = L.getValue(1);
11394 }
11395
11396 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11397 } else {
11398 // Collect the legal value parts into potentially illegal values
11399 // that correspond to the original function's return values.
11400 std::optional<ISD::NodeType> AssertOp;
11401 if (CLI.RetSExt)
11402 AssertOp = ISD::AssertSext;
11403 else if (CLI.RetZExt)
11404 AssertOp = ISD::AssertZext;
11405 unsigned CurReg = 0;
11406 for (EVT VT : RetVTs) {
11407 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11408 unsigned NumRegs =
11409 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11410
11411 ReturnValues.push_back(getCopyFromParts(
11412 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11413 CLI.Chain, CLI.CallConv, AssertOp));
11414 CurReg += NumRegs;
11415 }
11416
11417 // For a function returning void, there is no return value. We can't create
11418 // such a node, so we just return a null return value in that case. In
11419 // that case, nothing will actually look at the value.
11420 if (ReturnValues.empty())
11421 return std::make_pair(SDValue(), CLI.Chain);
11422 }
11423
11424 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11425 CLI.DAG.getVTList(RetVTs), ReturnValues);
11426 return std::make_pair(Res, CLI.Chain);
11427}
11428
11429/// Places new result values for the node in Results (their number
11430/// and types must exactly match those of the original return values of
11431/// the node), or leaves Results empty, which indicates that the node is not
11432/// to be custom lowered after all.
11435 SelectionDAG &DAG) const {
11436 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11437
11438 if (!Res.getNode())
11439 return;
11440
11441 // If the original node has one result, take the return value from
11442 // LowerOperation as is. It might not be result number 0.
11443 if (N->getNumValues() == 1) {
11444 Results.push_back(Res);
11445 return;
11446 }
11447
11448 // If the original node has multiple results, then the return node should
11449 // have the same number of results.
11450 assert((N->getNumValues() == Res->getNumValues()) &&
11451 "Lowering returned the wrong number of results!");
11452
11453 // Places new result values base on N result number.
11454 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11455 Results.push_back(Res.getValue(I));
11456}
11457
11459 llvm_unreachable("LowerOperation not implemented for this target!");
11460}
11461
11463 Register Reg,
11464 ISD::NodeType ExtendType) {
11466 assert((Op.getOpcode() != ISD::CopyFromReg ||
11467 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11468 "Copy from a reg to the same reg!");
11469 assert(!Reg.isPhysical() && "Is a physreg");
11470
11471 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11472 // If this is an InlineAsm we have to match the registers required, not the
11473 // notional registers required by the type.
11474
11475 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11476 std::nullopt); // This is not an ABI copy.
11477 SDValue Chain = DAG.getEntryNode();
11478
11479 if (ExtendType == ISD::ANY_EXTEND) {
11480 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11481 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11482 ExtendType = PreferredExtendIt->second;
11483 }
11484 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11485 PendingExports.push_back(Chain);
11486}
11487
11489
11490/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11491/// entry block, return true. This includes arguments used by switches, since
11492/// the switch may expand into multiple basic blocks.
11493static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11494 // With FastISel active, we may be splitting blocks, so force creation
11495 // of virtual registers for all non-dead arguments.
11496 if (FastISel)
11497 return A->use_empty();
11498
11499 const BasicBlock &Entry = A->getParent()->front();
11500 for (const User *U : A->users())
11501 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11502 return false; // Use not in entry block.
11503
11504 return true;
11505}
11506
11508 DenseMap<const Argument *,
11509 std::pair<const AllocaInst *, const StoreInst *>>;
11510
11511/// Scan the entry block of the function in FuncInfo for arguments that look
11512/// like copies into a local alloca. Record any copied arguments in
11513/// ArgCopyElisionCandidates.
11514static void
11516 FunctionLoweringInfo *FuncInfo,
11517 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11518 // Record the state of every static alloca used in the entry block. Argument
11519 // allocas are all used in the entry block, so we need approximately as many
11520 // entries as we have arguments.
11521 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11523 unsigned NumArgs = FuncInfo->Fn->arg_size();
11524 StaticAllocas.reserve(NumArgs * 2);
11525
11526 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11527 if (!V)
11528 return nullptr;
11529 V = V->stripPointerCasts();
11530 const auto *AI = dyn_cast<AllocaInst>(V);
11531 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11532 return nullptr;
11533 auto Iter = StaticAllocas.insert({AI, Unknown});
11534 return &Iter.first->second;
11535 };
11536
11537 // Look for stores of arguments to static allocas. Look through bitcasts and
11538 // GEPs to handle type coercions, as long as the alloca is fully initialized
11539 // by the store. Any non-store use of an alloca escapes it and any subsequent
11540 // unanalyzed store might write it.
11541 // FIXME: Handle structs initialized with multiple stores.
11542 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11543 // Look for stores, and handle non-store uses conservatively.
11544 const auto *SI = dyn_cast<StoreInst>(&I);
11545 if (!SI) {
11546 // We will look through cast uses, so ignore them completely.
11547 if (I.isCast())
11548 continue;
11549 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11550 // to allocas.
11551 if (I.isDebugOrPseudoInst())
11552 continue;
11553 // This is an unknown instruction. Assume it escapes or writes to all
11554 // static alloca operands.
11555 for (const Use &U : I.operands()) {
11556 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11557 *Info = StaticAllocaInfo::Clobbered;
11558 }
11559 continue;
11560 }
11561
11562 // If the stored value is a static alloca, mark it as escaped.
11563 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11564 *Info = StaticAllocaInfo::Clobbered;
11565
11566 // Check if the destination is a static alloca.
11567 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11568 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11569 if (!Info)
11570 continue;
11571 const AllocaInst *AI = cast<AllocaInst>(Dst);
11572
11573 // Skip allocas that have been initialized or clobbered.
11574 if (*Info != StaticAllocaInfo::Unknown)
11575 continue;
11576
11577 // Check if the stored value is an argument, and that this store fully
11578 // initializes the alloca.
11579 // If the argument type has padding bits we can't directly forward a pointer
11580 // as the upper bits may contain garbage.
11581 // Don't elide copies from the same argument twice.
11582 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11583 const auto *Arg = dyn_cast<Argument>(Val);
11584 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11585 Arg->getType()->isEmptyTy() ||
11586 DL.getTypeStoreSize(Arg->getType()) !=
11587 DL.getTypeAllocSize(AI->getAllocatedType()) ||
11588 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11589 ArgCopyElisionCandidates.count(Arg)) {
11590 *Info = StaticAllocaInfo::Clobbered;
11591 continue;
11592 }
11593
11594 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11595 << '\n');
11596
11597 // Mark this alloca and store for argument copy elision.
11598 *Info = StaticAllocaInfo::Elidable;
11599 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11600
11601 // Stop scanning if we've seen all arguments. This will happen early in -O0
11602 // builds, which is useful, because -O0 builds have large entry blocks and
11603 // many allocas.
11604 if (ArgCopyElisionCandidates.size() == NumArgs)
11605 break;
11606 }
11607}
11608
11609/// Try to elide argument copies from memory into a local alloca. Succeeds if
11610/// ArgVal is a load from a suitable fixed stack object.
11613 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11614 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11615 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11616 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11617 // Check if this is a load from a fixed stack object.
11618 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11619 if (!LNode)
11620 return;
11621 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11622 if (!FINode)
11623 return;
11624
11625 // Check that the fixed stack object is the right size and alignment.
11626 // Look at the alignment that the user wrote on the alloca instead of looking
11627 // at the stack object.
11628 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11629 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11630 const AllocaInst *AI = ArgCopyIter->second.first;
11631 int FixedIndex = FINode->getIndex();
11632 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11633 int OldIndex = AllocaIndex;
11634 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11635 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11636 LLVM_DEBUG(
11637 dbgs() << " argument copy elision failed due to bad fixed stack "
11638 "object size\n");
11639 return;
11640 }
11641 Align RequiredAlignment = AI->getAlign();
11642 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11643 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11644 "greater than stack argument alignment ("
11645 << DebugStr(RequiredAlignment) << " vs "
11646 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11647 return;
11648 }
11649
11650 // Perform the elision. Delete the old stack object and replace its only use
11651 // in the variable info map. Mark the stack object as mutable and aliased.
11652 LLVM_DEBUG({
11653 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11654 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11655 << '\n';
11656 });
11657 MFI.RemoveStackObject(OldIndex);
11658 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11659 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11660 AllocaIndex = FixedIndex;
11661 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11662 for (SDValue ArgVal : ArgVals)
11663 Chains.push_back(ArgVal.getValue(1));
11664
11665 // Avoid emitting code for the store implementing the copy.
11666 const StoreInst *SI = ArgCopyIter->second.second;
11667 ElidedArgCopyInstrs.insert(SI);
11668
11669 // Check for uses of the argument again so that we can avoid exporting ArgVal
11670 // if it is't used by anything other than the store.
11671 for (const Value *U : Arg.users()) {
11672 if (U != SI) {
11673 ArgHasUses = true;
11674 break;
11675 }
11676 }
11677}
11678
11679void SelectionDAGISel::LowerArguments(const Function &F) {
11680 SelectionDAG &DAG = SDB->DAG;
11681 SDLoc dl = SDB->getCurSDLoc();
11682 const DataLayout &DL = DAG.getDataLayout();
11684
11685 // In Naked functions we aren't going to save any registers.
11686 if (F.hasFnAttribute(Attribute::Naked))
11687 return;
11688
11689 if (!FuncInfo->CanLowerReturn) {
11690 // Put in an sret pointer parameter before all the other parameters.
11691 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11692
11693 ISD::ArgFlagsTy Flags;
11694 Flags.setSRet();
11695 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11696 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11698 Ins.push_back(RetArg);
11699 }
11700
11701 // Look for stores of arguments to static allocas. Mark such arguments with a
11702 // flag to ask the target to give us the memory location of that argument if
11703 // available.
11704 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11706 ArgCopyElisionCandidates);
11707
11708 // Set up the incoming argument description vector.
11709 for (const Argument &Arg : F.args()) {
11710 unsigned ArgNo = Arg.getArgNo();
11712 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11713 bool isArgValueUsed = !Arg.use_empty();
11714 unsigned PartBase = 0;
11715 Type *FinalType = Arg.getType();
11716 if (Arg.hasAttribute(Attribute::ByVal))
11717 FinalType = Arg.getParamByValType();
11718 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11719 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11720 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11721 ++Value) {
11722 Type *ArgTy = Types[Value];
11723 EVT VT = TLI->getValueType(DL, ArgTy);
11724 ISD::ArgFlagsTy Flags;
11725
11726 if (ArgTy->isPointerTy()) {
11727 Flags.setPointer();
11728 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11729 }
11730 if (Arg.hasAttribute(Attribute::ZExt))
11731 Flags.setZExt();
11732 if (Arg.hasAttribute(Attribute::SExt))
11733 Flags.setSExt();
11734 if (Arg.hasAttribute(Attribute::InReg)) {
11735 // If we are using vectorcall calling convention, a structure that is
11736 // passed InReg - is surely an HVA
11737 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11738 isa<StructType>(Arg.getType())) {
11739 // The first value of a structure is marked
11740 if (0 == Value)
11741 Flags.setHvaStart();
11742 Flags.setHva();
11743 }
11744 // Set InReg Flag
11745 Flags.setInReg();
11746 }
11747 if (Arg.hasAttribute(Attribute::StructRet))
11748 Flags.setSRet();
11749 if (Arg.hasAttribute(Attribute::SwiftSelf))
11750 Flags.setSwiftSelf();
11751 if (Arg.hasAttribute(Attribute::SwiftAsync))
11752 Flags.setSwiftAsync();
11753 if (Arg.hasAttribute(Attribute::SwiftError))
11754 Flags.setSwiftError();
11755 if (Arg.hasAttribute(Attribute::ByVal))
11756 Flags.setByVal();
11757 if (Arg.hasAttribute(Attribute::ByRef))
11758 Flags.setByRef();
11759 if (Arg.hasAttribute(Attribute::InAlloca)) {
11760 Flags.setInAlloca();
11761 // Set the byval flag for CCAssignFn callbacks that don't know about
11762 // inalloca. This way we can know how many bytes we should've allocated
11763 // and how many bytes a callee cleanup function will pop. If we port
11764 // inalloca to more targets, we'll have to add custom inalloca handling
11765 // in the various CC lowering callbacks.
11766 Flags.setByVal();
11767 }
11768 if (Arg.hasAttribute(Attribute::Preallocated)) {
11769 Flags.setPreallocated();
11770 // Set the byval flag for CCAssignFn callbacks that don't know about
11771 // preallocated. This way we can know how many bytes we should've
11772 // allocated and how many bytes a callee cleanup function will pop. If
11773 // we port preallocated to more targets, we'll have to add custom
11774 // preallocated handling in the various CC lowering callbacks.
11775 Flags.setByVal();
11776 }
11777
11778 // Certain targets (such as MIPS), may have a different ABI alignment
11779 // for a type depending on the context. Give the target a chance to
11780 // specify the alignment it wants.
11781 const Align OriginalAlignment(
11782 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
11783 Flags.setOrigAlign(OriginalAlignment);
11784
11785 Align MemAlign;
11786 Type *ArgMemTy = nullptr;
11787 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11788 Flags.isByRef()) {
11789 if (!ArgMemTy)
11790 ArgMemTy = Arg.getPointeeInMemoryValueType();
11791
11792 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11793
11794 // For in-memory arguments, size and alignment should be passed from FE.
11795 // BE will guess if this info is not there but there are cases it cannot
11796 // get right.
11797 if (auto ParamAlign = Arg.getParamStackAlign())
11798 MemAlign = *ParamAlign;
11799 else if ((ParamAlign = Arg.getParamAlign()))
11800 MemAlign = *ParamAlign;
11801 else
11802 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
11803 if (Flags.isByRef())
11804 Flags.setByRefSize(MemSize);
11805 else
11806 Flags.setByValSize(MemSize);
11807 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11808 MemAlign = *ParamAlign;
11809 } else {
11810 MemAlign = OriginalAlignment;
11811 }
11812 Flags.setMemAlign(MemAlign);
11813
11814 if (Arg.hasAttribute(Attribute::Nest))
11815 Flags.setNest();
11816 if (NeedsRegBlock)
11817 Flags.setInConsecutiveRegs();
11818 if (ArgCopyElisionCandidates.count(&Arg))
11819 Flags.setCopyElisionCandidate();
11820 if (Arg.hasAttribute(Attribute::Returned))
11821 Flags.setReturned();
11822
11823 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
11824 *CurDAG->getContext(), F.getCallingConv(), VT);
11825 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
11826 *CurDAG->getContext(), F.getCallingConv(), VT);
11827 for (unsigned i = 0; i != NumRegs; ++i) {
11828 // For scalable vectors, use the minimum size; individual targets
11829 // are responsible for handling scalable vector arguments and
11830 // return values.
11831 ISD::InputArg MyFlags(
11832 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
11833 PartBase + i * RegisterVT.getStoreSize().getKnownMinValue());
11834 if (NumRegs > 1 && i == 0)
11835 MyFlags.Flags.setSplit();
11836 // if it isn't first piece, alignment must be 1
11837 else if (i > 0) {
11838 MyFlags.Flags.setOrigAlign(Align(1));
11839 if (i == NumRegs - 1)
11840 MyFlags.Flags.setSplitEnd();
11841 }
11842 Ins.push_back(MyFlags);
11843 }
11844 if (NeedsRegBlock && Value == NumValues - 1)
11845 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
11846 PartBase += VT.getStoreSize().getKnownMinValue();
11847 }
11848 }
11849
11850 // Call the target to set up the argument values.
11852 SDValue NewRoot = TLI->LowerFormalArguments(
11853 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
11854
11855 // Verify that the target's LowerFormalArguments behaved as expected.
11856 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
11857 "LowerFormalArguments didn't return a valid chain!");
11858 assert(InVals.size() == Ins.size() &&
11859 "LowerFormalArguments didn't emit the correct number of values!");
11860 LLVM_DEBUG({
11861 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
11862 assert(InVals[i].getNode() &&
11863 "LowerFormalArguments emitted a null value!");
11864 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
11865 "LowerFormalArguments emitted a value with the wrong type!");
11866 }
11867 });
11868
11869 // Update the DAG with the new chain value resulting from argument lowering.
11870 DAG.setRoot(NewRoot);
11871
11872 // Set up the argument values.
11873 unsigned i = 0;
11874 if (!FuncInfo->CanLowerReturn) {
11875 // Create a virtual register for the sret pointer, and put in a copy
11876 // from the sret argument into it.
11877 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11878 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
11879 std::optional<ISD::NodeType> AssertOp;
11880 SDValue ArgValue =
11881 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
11882 F.getCallingConv(), AssertOp);
11883
11884 MachineFunction& MF = SDB->DAG.getMachineFunction();
11885 MachineRegisterInfo& RegInfo = MF.getRegInfo();
11886 Register SRetReg =
11887 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
11888 FuncInfo->DemoteRegister = SRetReg;
11889 NewRoot =
11890 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
11891 DAG.setRoot(NewRoot);
11892
11893 // i indexes lowered arguments. Bump it past the hidden sret argument.
11894 ++i;
11895 }
11896
11898 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
11899 for (const Argument &Arg : F.args()) {
11900 SmallVector<SDValue, 4> ArgValues;
11901 SmallVector<EVT, 4> ValueVTs;
11902 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
11903 unsigned NumValues = ValueVTs.size();
11904 if (NumValues == 0)
11905 continue;
11906
11907 bool ArgHasUses = !Arg.use_empty();
11908
11909 // Elide the copying store if the target loaded this argument from a
11910 // suitable fixed stack object.
11911 if (Ins[i].Flags.isCopyElisionCandidate()) {
11912 unsigned NumParts = 0;
11913 for (EVT VT : ValueVTs)
11914 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
11915 F.getCallingConv(), VT);
11916
11917 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
11918 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
11919 ArrayRef(&InVals[i], NumParts), ArgHasUses);
11920 }
11921
11922 // If this argument is unused then remember its value. It is used to generate
11923 // debugging information.
11924 bool isSwiftErrorArg =
11925 TLI->supportSwiftError() &&
11926 Arg.hasAttribute(Attribute::SwiftError);
11927 if (!ArgHasUses && !isSwiftErrorArg) {
11928 SDB->setUnusedArgValue(&Arg, InVals[i]);
11929
11930 // Also remember any frame index for use in FastISel.
11931 if (FrameIndexSDNode *FI =
11933 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11934 }
11935
11936 for (unsigned Val = 0; Val != NumValues; ++Val) {
11937 EVT VT = ValueVTs[Val];
11938 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
11939 F.getCallingConv(), VT);
11940 unsigned NumParts = TLI->getNumRegistersForCallingConv(
11941 *CurDAG->getContext(), F.getCallingConv(), VT);
11942
11943 // Even an apparent 'unused' swifterror argument needs to be returned. So
11944 // we do generate a copy for it that can be used on return from the
11945 // function.
11946 if (ArgHasUses || isSwiftErrorArg) {
11947 std::optional<ISD::NodeType> AssertOp;
11948 if (Arg.hasAttribute(Attribute::SExt))
11949 AssertOp = ISD::AssertSext;
11950 else if (Arg.hasAttribute(Attribute::ZExt))
11951 AssertOp = ISD::AssertZext;
11952
11953 SDValue OutVal =
11954 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
11955 NewRoot, F.getCallingConv(), AssertOp);
11956
11957 FPClassTest NoFPClass = Arg.getNoFPClass();
11958 if (NoFPClass != fcNone) {
11959 SDValue SDNoFPClass = DAG.getTargetConstant(
11960 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
11961 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
11962 OutVal, SDNoFPClass);
11963 }
11964 ArgValues.push_back(OutVal);
11965 }
11966
11967 i += NumParts;
11968 }
11969
11970 // We don't need to do anything else for unused arguments.
11971 if (ArgValues.empty())
11972 continue;
11973
11974 // Note down frame index.
11975 if (FrameIndexSDNode *FI =
11976 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
11977 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11978
11979 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
11980 SDB->getCurSDLoc());
11981
11982 SDB->setValue(&Arg, Res);
11983 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
11984 // We want to associate the argument with the frame index, among
11985 // involved operands, that correspond to the lowest address. The
11986 // getCopyFromParts function, called earlier, is swapping the order of
11987 // the operands to BUILD_PAIR depending on endianness. The result of
11988 // that swapping is that the least significant bits of the argument will
11989 // be in the first operand of the BUILD_PAIR node, and the most
11990 // significant bits will be in the second operand.
11991 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
11992 if (LoadSDNode *LNode =
11993 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
11994 if (FrameIndexSDNode *FI =
11995 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
11996 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11997 }
11998
11999 // Analyses past this point are naive and don't expect an assertion.
12000 if (Res.getOpcode() == ISD::AssertZext)
12001 Res = Res.getOperand(0);
12002
12003 // Update the SwiftErrorVRegDefMap.
12004 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
12005 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12006 if (Reg.isVirtual())
12007 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
12008 Reg);
12009 }
12010
12011 // If this argument is live outside of the entry block, insert a copy from
12012 // wherever we got it to the vreg that other BB's will reference it as.
12013 if (Res.getOpcode() == ISD::CopyFromReg) {
12014 // If we can, though, try to skip creating an unnecessary vreg.
12015 // FIXME: This isn't very clean... it would be nice to make this more
12016 // general.
12017 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12018 if (Reg.isVirtual()) {
12019 FuncInfo->ValueMap[&Arg] = Reg;
12020 continue;
12021 }
12022 }
12023 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12024 FuncInfo->InitializeRegForValue(&Arg);
12025 SDB->CopyToExportRegsIfNeeded(&Arg);
12026 }
12027 }
12028
12029 if (!Chains.empty()) {
12030 Chains.push_back(NewRoot);
12031 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12032 }
12033
12034 DAG.setRoot(NewRoot);
12035
12036 assert(i == InVals.size() && "Argument register count mismatch!");
12037
12038 // If any argument copy elisions occurred and we have debug info, update the
12039 // stale frame indices used in the dbg.declare variable info table.
12040 if (!ArgCopyElisionFrameIndexMap.empty()) {
12041 for (MachineFunction::VariableDbgInfo &VI :
12042 MF->getInStackSlotVariableDbgInfo()) {
12043 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12044 if (I != ArgCopyElisionFrameIndexMap.end())
12045 VI.updateStackSlot(I->second);
12046 }
12047 }
12048
12049 // Finally, if the target has anything special to do, allow it to do so.
12051}
12052
12053/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12054/// ensure constants are generated when needed. Remember the virtual registers
12055/// that need to be added to the Machine PHI nodes as input. We cannot just
12056/// directly add them, because expansion might result in multiple MBB's for one
12057/// BB. As such, the start of the BB might correspond to a different MBB than
12058/// the end.
12059void
12060SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12061 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12062
12063 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12064
12065 // Check PHI nodes in successors that expect a value to be available from this
12066 // block.
12067 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12068 if (!isa<PHINode>(SuccBB->begin())) continue;
12069 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12070
12071 // If this terminator has multiple identical successors (common for
12072 // switches), only handle each succ once.
12073 if (!SuccsHandled.insert(SuccMBB).second)
12074 continue;
12075
12077
12078 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12079 // nodes and Machine PHI nodes, but the incoming operands have not been
12080 // emitted yet.
12081 for (const PHINode &PN : SuccBB->phis()) {
12082 // Ignore dead phi's.
12083 if (PN.use_empty())
12084 continue;
12085
12086 // Skip empty types
12087 if (PN.getType()->isEmptyTy())
12088 continue;
12089
12090 Register Reg;
12091 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12092
12093 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12094 Register &RegOut = ConstantsOut[C];
12095 if (!RegOut) {
12096 RegOut = FuncInfo.CreateRegs(&PN);
12097 // We need to zero/sign extend ConstantInt phi operands to match
12098 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12099 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12100 if (auto *CI = dyn_cast<ConstantInt>(C))
12101 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12103 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12104 }
12105 Reg = RegOut;
12106 } else {
12108 FuncInfo.ValueMap.find(PHIOp);
12109 if (I != FuncInfo.ValueMap.end())
12110 Reg = I->second;
12111 else {
12112 assert(isa<AllocaInst>(PHIOp) &&
12113 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12114 "Didn't codegen value into a register!??");
12115 Reg = FuncInfo.CreateRegs(&PN);
12117 }
12118 }
12119
12120 // Remember that this register needs to added to the machine PHI node as
12121 // the input for this MBB.
12122 SmallVector<EVT, 4> ValueVTs;
12123 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12124 for (EVT VT : ValueVTs) {
12125 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12126 for (unsigned i = 0; i != NumRegisters; ++i)
12127 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12128 Reg += NumRegisters;
12129 }
12130 }
12131 }
12132
12133 ConstantsOut.clear();
12134}
12135
12136MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12138 if (++I == FuncInfo.MF->end())
12139 return nullptr;
12140 return &*I;
12141}
12142
12143/// During lowering new call nodes can be created (such as memset, etc.).
12144/// Those will become new roots of the current DAG, but complications arise
12145/// when they are tail calls. In such cases, the call lowering will update
12146/// the root, but the builder still needs to know that a tail call has been
12147/// lowered in order to avoid generating an additional return.
12148void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12149 // If the node is null, we do have a tail call.
12150 if (MaybeTC.getNode() != nullptr)
12151 DAG.setRoot(MaybeTC);
12152 else
12153 HasTailCall = true;
12154}
12155
12156void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12157 MachineBasicBlock *SwitchMBB,
12158 MachineBasicBlock *DefaultMBB) {
12159 MachineFunction *CurMF = FuncInfo.MF;
12160 MachineBasicBlock *NextMBB = nullptr;
12162 if (++BBI != FuncInfo.MF->end())
12163 NextMBB = &*BBI;
12164
12165 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12166
12167 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12168
12169 if (Size == 2 && W.MBB == SwitchMBB) {
12170 // If any two of the cases has the same destination, and if one value
12171 // is the same as the other, but has one bit unset that the other has set,
12172 // use bit manipulation to do two compares at once. For example:
12173 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12174 // TODO: This could be extended to merge any 2 cases in switches with 3
12175 // cases.
12176 // TODO: Handle cases where W.CaseBB != SwitchBB.
12177 CaseCluster &Small = *W.FirstCluster;
12178 CaseCluster &Big = *W.LastCluster;
12179
12180 if (Small.Low == Small.High && Big.Low == Big.High &&
12181 Small.MBB == Big.MBB) {
12182 const APInt &SmallValue = Small.Low->getValue();
12183 const APInt &BigValue = Big.Low->getValue();
12184
12185 // Check that there is only one bit different.
12186 APInt CommonBit = BigValue ^ SmallValue;
12187 if (CommonBit.isPowerOf2()) {
12188 SDValue CondLHS = getValue(Cond);
12189 EVT VT = CondLHS.getValueType();
12190 SDLoc DL = getCurSDLoc();
12191
12192 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12193 DAG.getConstant(CommonBit, DL, VT));
12194 SDValue Cond = DAG.getSetCC(
12195 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12196 ISD::SETEQ);
12197
12198 // Update successor info.
12199 // Both Small and Big will jump to Small.BB, so we sum up the
12200 // probabilities.
12201 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12202 if (BPI)
12203 addSuccessorWithProb(
12204 SwitchMBB, DefaultMBB,
12205 // The default destination is the first successor in IR.
12206 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12207 else
12208 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12209
12210 // Insert the true branch.
12211 SDValue BrCond =
12212 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12213 DAG.getBasicBlock(Small.MBB));
12214 // Insert the false branch.
12215 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12216 DAG.getBasicBlock(DefaultMBB));
12217
12218 DAG.setRoot(BrCond);
12219 return;
12220 }
12221 }
12222 }
12223
12224 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12225 // Here, we order cases by probability so the most likely case will be
12226 // checked first. However, two clusters can have the same probability in
12227 // which case their relative ordering is non-deterministic. So we use Low
12228 // as a tie-breaker as clusters are guaranteed to never overlap.
12229 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12230 [](const CaseCluster &a, const CaseCluster &b) {
12231 return a.Prob != b.Prob ?
12232 a.Prob > b.Prob :
12233 a.Low->getValue().slt(b.Low->getValue());
12234 });
12235
12236 // Rearrange the case blocks so that the last one falls through if possible
12237 // without changing the order of probabilities.
12238 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12239 --I;
12240 if (I->Prob > W.LastCluster->Prob)
12241 break;
12242 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12243 std::swap(*I, *W.LastCluster);
12244 break;
12245 }
12246 }
12247 }
12248
12249 // Compute total probability.
12250 BranchProbability DefaultProb = W.DefaultProb;
12251 BranchProbability UnhandledProbs = DefaultProb;
12252 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12253 UnhandledProbs += I->Prob;
12254
12255 MachineBasicBlock *CurMBB = W.MBB;
12256 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12257 bool FallthroughUnreachable = false;
12258 MachineBasicBlock *Fallthrough;
12259 if (I == W.LastCluster) {
12260 // For the last cluster, fall through to the default destination.
12261 Fallthrough = DefaultMBB;
12262 FallthroughUnreachable = isa<UnreachableInst>(
12263 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12264 } else {
12265 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12266 CurMF->insert(BBI, Fallthrough);
12267 // Put Cond in a virtual register to make it available from the new blocks.
12269 }
12270 UnhandledProbs -= I->Prob;
12271
12272 switch (I->Kind) {
12273 case CC_JumpTable: {
12274 // FIXME: Optimize away range check based on pivot comparisons.
12275 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12276 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12277
12278 // The jump block hasn't been inserted yet; insert it here.
12279 MachineBasicBlock *JumpMBB = JT->MBB;
12280 CurMF->insert(BBI, JumpMBB);
12281
12282 auto JumpProb = I->Prob;
12283 auto FallthroughProb = UnhandledProbs;
12284
12285 // If the default statement is a target of the jump table, we evenly
12286 // distribute the default probability to successors of CurMBB. Also
12287 // update the probability on the edge from JumpMBB to Fallthrough.
12288 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12289 SE = JumpMBB->succ_end();
12290 SI != SE; ++SI) {
12291 if (*SI == DefaultMBB) {
12292 JumpProb += DefaultProb / 2;
12293 FallthroughProb -= DefaultProb / 2;
12294 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12295 JumpMBB->normalizeSuccProbs();
12296 break;
12297 }
12298 }
12299
12300 // If the default clause is unreachable, propagate that knowledge into
12301 // JTH->FallthroughUnreachable which will use it to suppress the range
12302 // check.
12303 //
12304 // However, don't do this if we're doing branch target enforcement,
12305 // because a table branch _without_ a range check can be a tempting JOP
12306 // gadget - out-of-bounds inputs that are impossible in correct
12307 // execution become possible again if an attacker can influence the
12308 // control flow. So if an attacker doesn't already have a BTI bypass
12309 // available, we don't want them to be able to get one out of this
12310 // table branch.
12311 if (FallthroughUnreachable) {
12312 Function &CurFunc = CurMF->getFunction();
12313 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12314 JTH->FallthroughUnreachable = true;
12315 }
12316
12317 if (!JTH->FallthroughUnreachable)
12318 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12319 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12320 CurMBB->normalizeSuccProbs();
12321
12322 // The jump table header will be inserted in our current block, do the
12323 // range check, and fall through to our fallthrough block.
12324 JTH->HeaderBB = CurMBB;
12325 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12326
12327 // If we're in the right place, emit the jump table header right now.
12328 if (CurMBB == SwitchMBB) {
12329 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12330 JTH->Emitted = true;
12331 }
12332 break;
12333 }
12334 case CC_BitTests: {
12335 // FIXME: Optimize away range check based on pivot comparisons.
12336 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12337
12338 // The bit test blocks haven't been inserted yet; insert them here.
12339 for (BitTestCase &BTC : BTB->Cases)
12340 CurMF->insert(BBI, BTC.ThisBB);
12341
12342 // Fill in fields of the BitTestBlock.
12343 BTB->Parent = CurMBB;
12344 BTB->Default = Fallthrough;
12345
12346 BTB->DefaultProb = UnhandledProbs;
12347 // If the cases in bit test don't form a contiguous range, we evenly
12348 // distribute the probability on the edge to Fallthrough to two
12349 // successors of CurMBB.
12350 if (!BTB->ContiguousRange) {
12351 BTB->Prob += DefaultProb / 2;
12352 BTB->DefaultProb -= DefaultProb / 2;
12353 }
12354
12355 if (FallthroughUnreachable)
12356 BTB->FallthroughUnreachable = true;
12357
12358 // If we're in the right place, emit the bit test header right now.
12359 if (CurMBB == SwitchMBB) {
12360 visitBitTestHeader(*BTB, SwitchMBB);
12361 BTB->Emitted = true;
12362 }
12363 break;
12364 }
12365 case CC_Range: {
12366 const Value *RHS, *LHS, *MHS;
12367 ISD::CondCode CC;
12368 if (I->Low == I->High) {
12369 // Check Cond == I->Low.
12370 CC = ISD::SETEQ;
12371 LHS = Cond;
12372 RHS=I->Low;
12373 MHS = nullptr;
12374 } else {
12375 // Check I->Low <= Cond <= I->High.
12376 CC = ISD::SETLE;
12377 LHS = I->Low;
12378 MHS = Cond;
12379 RHS = I->High;
12380 }
12381
12382 // If Fallthrough is unreachable, fold away the comparison.
12383 if (FallthroughUnreachable)
12384 CC = ISD::SETTRUE;
12385
12386 // The false probability is the sum of all unhandled cases.
12387 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12388 getCurSDLoc(), I->Prob, UnhandledProbs);
12389
12390 if (CurMBB == SwitchMBB)
12391 visitSwitchCase(CB, SwitchMBB);
12392 else
12393 SL->SwitchCases.push_back(CB);
12394
12395 break;
12396 }
12397 }
12398 CurMBB = Fallthrough;
12399 }
12400}
12401
12402void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12403 const SwitchWorkListItem &W,
12404 Value *Cond,
12405 MachineBasicBlock *SwitchMBB) {
12406 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12407 "Clusters not sorted?");
12408 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12409
12410 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12411 SL->computeSplitWorkItemInfo(W);
12412
12413 // Use the first element on the right as pivot since we will make less-than
12414 // comparisons against it.
12415 CaseClusterIt PivotCluster = FirstRight;
12416 assert(PivotCluster > W.FirstCluster);
12417 assert(PivotCluster <= W.LastCluster);
12418
12419 CaseClusterIt FirstLeft = W.FirstCluster;
12420 CaseClusterIt LastRight = W.LastCluster;
12421
12422 const ConstantInt *Pivot = PivotCluster->Low;
12423
12424 // New blocks will be inserted immediately after the current one.
12426 ++BBI;
12427
12428 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12429 // we can branch to its destination directly if it's squeezed exactly in
12430 // between the known lower bound and Pivot - 1.
12431 MachineBasicBlock *LeftMBB;
12432 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12433 FirstLeft->Low == W.GE &&
12434 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12435 LeftMBB = FirstLeft->MBB;
12436 } else {
12437 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12438 FuncInfo.MF->insert(BBI, LeftMBB);
12439 WorkList.push_back(
12440 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12441 // Put Cond in a virtual register to make it available from the new blocks.
12443 }
12444
12445 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12446 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12447 // directly if RHS.High equals the current upper bound.
12448 MachineBasicBlock *RightMBB;
12449 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12450 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12451 RightMBB = FirstRight->MBB;
12452 } else {
12453 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12454 FuncInfo.MF->insert(BBI, RightMBB);
12455 WorkList.push_back(
12456 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12457 // Put Cond in a virtual register to make it available from the new blocks.
12459 }
12460
12461 // Create the CaseBlock record that will be used to lower the branch.
12462 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12463 getCurSDLoc(), LeftProb, RightProb);
12464
12465 if (W.MBB == SwitchMBB)
12466 visitSwitchCase(CB, SwitchMBB);
12467 else
12468 SL->SwitchCases.push_back(CB);
12469}
12470
12471// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12472// from the swith statement.
12474 BranchProbability PeeledCaseProb) {
12475 if (PeeledCaseProb == BranchProbability::getOne())
12477 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12478
12479 uint32_t Numerator = CaseProb.getNumerator();
12480 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12481 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12482}
12483
12484// Try to peel the top probability case if it exceeds the threshold.
12485// Return current MachineBasicBlock for the switch statement if the peeling
12486// does not occur.
12487// If the peeling is performed, return the newly created MachineBasicBlock
12488// for the peeled switch statement. Also update Clusters to remove the peeled
12489// case. PeeledCaseProb is the BranchProbability for the peeled case.
12490MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12491 const SwitchInst &SI, CaseClusterVector &Clusters,
12492 BranchProbability &PeeledCaseProb) {
12493 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12494 // Don't perform if there is only one cluster or optimizing for size.
12495 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12496 TM.getOptLevel() == CodeGenOptLevel::None ||
12497 SwitchMBB->getParent()->getFunction().hasMinSize())
12498 return SwitchMBB;
12499
12500 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12501 unsigned PeeledCaseIndex = 0;
12502 bool SwitchPeeled = false;
12503 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12504 CaseCluster &CC = Clusters[Index];
12505 if (CC.Prob < TopCaseProb)
12506 continue;
12507 TopCaseProb = CC.Prob;
12508 PeeledCaseIndex = Index;
12509 SwitchPeeled = true;
12510 }
12511 if (!SwitchPeeled)
12512 return SwitchMBB;
12513
12514 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12515 << TopCaseProb << "\n");
12516
12517 // Record the MBB for the peeled switch statement.
12518 MachineFunction::iterator BBI(SwitchMBB);
12519 ++BBI;
12520 MachineBasicBlock *PeeledSwitchMBB =
12521 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12522 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12523
12524 ExportFromCurrentBlock(SI.getCondition());
12525 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12526 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12527 nullptr, nullptr, TopCaseProb.getCompl()};
12528 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12529
12530 Clusters.erase(PeeledCaseIt);
12531 for (CaseCluster &CC : Clusters) {
12532 LLVM_DEBUG(
12533 dbgs() << "Scale the probablity for one cluster, before scaling: "
12534 << CC.Prob << "\n");
12535 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12536 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12537 }
12538 PeeledCaseProb = TopCaseProb;
12539 return PeeledSwitchMBB;
12540}
12541
12542void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12543 // Extract cases from the switch.
12544 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12545 CaseClusterVector Clusters;
12546 Clusters.reserve(SI.getNumCases());
12547 for (auto I : SI.cases()) {
12548 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12549 const ConstantInt *CaseVal = I.getCaseValue();
12550 BranchProbability Prob =
12551 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12552 : BranchProbability(1, SI.getNumCases() + 1);
12553 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12554 }
12555
12556 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12557
12558 // Cluster adjacent cases with the same destination. We do this at all
12559 // optimization levels because it's cheap to do and will make codegen faster
12560 // if there are many clusters.
12561 sortAndRangeify(Clusters);
12562
12563 // The branch probablity of the peeled case.
12564 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12565 MachineBasicBlock *PeeledSwitchMBB =
12566 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12567
12568 // If there is only the default destination, jump there directly.
12569 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12570 if (Clusters.empty()) {
12571 assert(PeeledSwitchMBB == SwitchMBB);
12572 SwitchMBB->addSuccessor(DefaultMBB);
12573 if (DefaultMBB != NextBlock(SwitchMBB)) {
12574 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12575 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12576 }
12577 return;
12578 }
12579
12580 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12581 DAG.getBFI());
12582 SL->findBitTestClusters(Clusters, &SI);
12583
12584 LLVM_DEBUG({
12585 dbgs() << "Case clusters: ";
12586 for (const CaseCluster &C : Clusters) {
12587 if (C.Kind == CC_JumpTable)
12588 dbgs() << "JT:";
12589 if (C.Kind == CC_BitTests)
12590 dbgs() << "BT:";
12591
12592 C.Low->getValue().print(dbgs(), true);
12593 if (C.Low != C.High) {
12594 dbgs() << '-';
12595 C.High->getValue().print(dbgs(), true);
12596 }
12597 dbgs() << ' ';
12598 }
12599 dbgs() << '\n';
12600 });
12601
12602 assert(!Clusters.empty());
12603 SwitchWorkList WorkList;
12604 CaseClusterIt First = Clusters.begin();
12605 CaseClusterIt Last = Clusters.end() - 1;
12606 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12607 // Scale the branchprobability for DefaultMBB if the peel occurs and
12608 // DefaultMBB is not replaced.
12609 if (PeeledCaseProb != BranchProbability::getZero() &&
12610 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12611 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12612 WorkList.push_back(
12613 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12614
12615 while (!WorkList.empty()) {
12616 SwitchWorkListItem W = WorkList.pop_back_val();
12617 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12618
12619 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12620 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12621 // For optimized builds, lower large range as a balanced binary tree.
12622 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12623 continue;
12624 }
12625
12626 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12627 }
12628}
12629
12630void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12631 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12632 auto DL = getCurSDLoc();
12633 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12634 setValue(&I, DAG.getStepVector(DL, ResultVT));
12635}
12636
12637void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12638 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12639 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12640
12641 SDLoc DL = getCurSDLoc();
12642 SDValue V = getValue(I.getOperand(0));
12643 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12644
12645 if (VT.isScalableVector()) {
12646 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12647 return;
12648 }
12649
12650 // Use VECTOR_SHUFFLE for the fixed-length vector
12651 // to maintain existing behavior.
12652 SmallVector<int, 8> Mask;
12653 unsigned NumElts = VT.getVectorMinNumElements();
12654 for (unsigned i = 0; i != NumElts; ++i)
12655 Mask.push_back(NumElts - 1 - i);
12656
12657 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12658}
12659
12660void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12661 unsigned Factor) {
12662 auto DL = getCurSDLoc();
12663 SDValue InVec = getValue(I.getOperand(0));
12664
12665 SmallVector<EVT, 4> ValueVTs;
12666 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12667 ValueVTs);
12668
12669 EVT OutVT = ValueVTs[0];
12670 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12671
12672 SmallVector<SDValue, 4> SubVecs(Factor);
12673 for (unsigned i = 0; i != Factor; ++i) {
12674 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12675 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12676 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12677 }
12678
12679 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12680 // from existing legalisation and combines.
12681 if (OutVT.isFixedLengthVector() && Factor == 2) {
12682 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12683 createStrideMask(0, 2, OutNumElts));
12684 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12685 createStrideMask(1, 2, OutNumElts));
12686 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12687 setValue(&I, Res);
12688 return;
12689 }
12690
12691 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12692 DAG.getVTList(ValueVTs), SubVecs);
12693 setValue(&I, Res);
12694}
12695
12696void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12697 unsigned Factor) {
12698 auto DL = getCurSDLoc();
12699 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12700 EVT InVT = getValue(I.getOperand(0)).getValueType();
12701 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12702
12703 SmallVector<SDValue, 8> InVecs(Factor);
12704 for (unsigned i = 0; i < Factor; ++i) {
12705 InVecs[i] = getValue(I.getOperand(i));
12706 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12707 "Expected VTs to be the same");
12708 }
12709
12710 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12711 // from existing legalisation and combines.
12712 if (OutVT.isFixedLengthVector() && Factor == 2) {
12713 unsigned NumElts = InVT.getVectorMinNumElements();
12714 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12715 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12716 createInterleaveMask(NumElts, 2)));
12717 return;
12718 }
12719
12720 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12721 SDValue Res =
12722 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12723
12725 for (unsigned i = 0; i < Factor; ++i)
12726 Results[i] = Res.getValue(i);
12727
12728 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12729 setValue(&I, Res);
12730}
12731
12732void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12733 SmallVector<EVT, 4> ValueVTs;
12734 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12735 ValueVTs);
12736 unsigned NumValues = ValueVTs.size();
12737 if (NumValues == 0) return;
12738
12739 SmallVector<SDValue, 4> Values(NumValues);
12740 SDValue Op = getValue(I.getOperand(0));
12741
12742 for (unsigned i = 0; i != NumValues; ++i)
12743 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12744 SDValue(Op.getNode(), Op.getResNo() + i));
12745
12747 DAG.getVTList(ValueVTs), Values));
12748}
12749
12750void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12751 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12752 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12753
12754 SDLoc DL = getCurSDLoc();
12755 SDValue V1 = getValue(I.getOperand(0));
12756 SDValue V2 = getValue(I.getOperand(1));
12757 int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue();
12758
12759 // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
12760 if (VT.isScalableVector()) {
12761 setValue(
12762 &I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2,
12763 DAG.getSignedConstant(
12764 Imm, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
12765 return;
12766 }
12767
12768 unsigned NumElts = VT.getVectorNumElements();
12769
12770 uint64_t Idx = (NumElts + Imm) % NumElts;
12771
12772 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12773 SmallVector<int, 8> Mask;
12774 for (unsigned i = 0; i < NumElts; ++i)
12775 Mask.push_back(Idx + i);
12776 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12777}
12778
12779// Consider the following MIR after SelectionDAG, which produces output in
12780// phyregs in the first case or virtregs in the second case.
12781//
12782// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12783// %5:gr32 = COPY $ebx
12784// %6:gr32 = COPY $edx
12785// %1:gr32 = COPY %6:gr32
12786// %0:gr32 = COPY %5:gr32
12787//
12788// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12789// %1:gr32 = COPY %6:gr32
12790// %0:gr32 = COPY %5:gr32
12791//
12792// Given %0, we'd like to return $ebx in the first case and %5 in the second.
12793// Given %1, we'd like to return $edx in the first case and %6 in the second.
12794//
12795// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12796// to a single virtreg (such as %0). The remaining outputs monotonically
12797// increase in virtreg number from there. If a callbr has no outputs, then it
12798// should not have a corresponding callbr landingpad; in fact, the callbr
12799// landingpad would not even be able to refer to such a callbr.
12801 MachineInstr *MI = MRI.def_begin(Reg)->getParent();
12802 // There is definitely at least one copy.
12803 assert(MI->getOpcode() == TargetOpcode::COPY &&
12804 "start of copy chain MUST be COPY");
12805 Reg = MI->getOperand(1).getReg();
12806
12807 // If the copied register in the first copy must be virtual.
12808 assert(Reg.isVirtual() && "expected COPY of virtual register");
12809 MI = MRI.def_begin(Reg)->getParent();
12810
12811 // There may be an optional second copy.
12812 if (MI->getOpcode() == TargetOpcode::COPY) {
12813 assert(Reg.isVirtual() && "expected COPY of virtual register");
12814 Reg = MI->getOperand(1).getReg();
12815 assert(Reg.isPhysical() && "expected COPY of physical register");
12816 } else {
12817 // The start of the chain must be an INLINEASM_BR.
12818 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12819 "end of copy chain MUST be INLINEASM_BR");
12820 }
12821
12822 return Reg;
12823}
12824
12825// We must do this walk rather than the simpler
12826// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
12827// otherwise we will end up with copies of virtregs only valid along direct
12828// edges.
12829void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
12830 SmallVector<EVT, 8> ResultVTs;
12831 SmallVector<SDValue, 8> ResultValues;
12832 const auto *CBR =
12833 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
12834
12835 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12836 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
12837 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
12838
12839 Register InitialDef = FuncInfo.ValueMap[CBR];
12840 SDValue Chain = DAG.getRoot();
12841
12842 // Re-parse the asm constraints string.
12843 TargetLowering::AsmOperandInfoVector TargetConstraints =
12844 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
12845 for (auto &T : TargetConstraints) {
12846 SDISelAsmOperandInfo OpInfo(T);
12847 if (OpInfo.Type != InlineAsm::isOutput)
12848 continue;
12849
12850 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
12851 // individual constraint.
12852 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
12853
12854 switch (OpInfo.ConstraintType) {
12857 // Fill in OpInfo.AssignedRegs.Regs.
12858 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
12859
12860 // getRegistersForValue may produce 1 to many registers based on whether
12861 // the OpInfo.ConstraintVT is legal on the target or not.
12862 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
12863 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
12864 if (OriginalDef.isPhysical())
12865 FuncInfo.MBB->addLiveIn(OriginalDef);
12866 // Update the assigned registers to use the original defs.
12867 Reg = OriginalDef;
12868 }
12869
12870 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
12871 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
12872 ResultValues.push_back(V);
12873 ResultVTs.push_back(OpInfo.ConstraintVT);
12874 break;
12875 }
12877 SDValue Flag;
12878 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
12879 OpInfo, DAG);
12880 ++InitialDef;
12881 ResultValues.push_back(V);
12882 ResultVTs.push_back(OpInfo.ConstraintVT);
12883 break;
12884 }
12885 default:
12886 break;
12887 }
12888 }
12890 DAG.getVTList(ResultVTs), ResultValues);
12891 setValue(&I, V);
12892}
unsigned const MachineRegisterInfo * MRI
return SDValue()
static unsigned getIntrinsicID(const SDNode *N)
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Function Alias Analysis Results
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI)
Returns an AttributeList representing the attributes applied to the return value of the given call.
Definition FastISel.cpp:942
#define Check(C,...)
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:55
#define I(x, y, z)
Definition MD5.cpp:58
Machine Check Debug Module
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static const Function * getCalledFunction(const Value *V)
This file provides utility analysis objects describing memory locations.
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
#define T
#define T1
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static unsigned getAddressSpace(const Value *V, unsigned MaxLookup)
MachineInstr unsigned OpIdx
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t High
uint64_t IntrinsicInst * II
OptimizedStructLayoutField Field
#define P(N)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static bool hasOnlySelectUsers(const Value *Cond)
static SDValue getLoadStackGuard(SelectionDAG &DAG, const SDLoc &DL, SDValue &Chain)
Create a LOAD_STACK_GUARD node, and let it carry the target specific global variable if there exists ...
static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index, SDValue &Scale, SelectionDAGBuilder *SDB, const BasicBlock *CurBB, uint64_t ElemSize)
static void failForInvalidBundles(const CallBase &I, StringRef Name, ArrayRef< uint32_t > AllowedBundles)
static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx, const SDLoc &DL, SmallVectorImpl< SDValue > &Ops, SelectionDAGBuilder &Builder)
Add a stack map intrinsic call's live variable operands to a stackmap or patchpoint target node's ope...
static const unsigned MaxParallelChains
static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
visitPow - Lower a pow intrinsic.
static const CallBase * FindPreallocatedCall(const Value *PreallocatedSetup)
Given a @llvm.call.preallocated.setup, return the corresponding preallocated call.
static cl::opt< unsigned > SwitchPeelThreshold("switch-peel-threshold", cl::Hidden, cl::init(66), cl::desc("Set the case probability threshold for peeling the case from a " "switch statement. A value greater than 100 will void this " "optimization"))
static cl::opt< bool > InsertAssertAlign("insert-assert-align", cl::init(true), cl::desc("Insert the experimental `assertalign` node."), cl::ReallyHidden)
static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin)
static bool handleDanglingVariadicDebugInfo(SelectionDAG &DAG, DILocalVariable *Variable, DebugLoc DL, unsigned Order, SmallVectorImpl< Value * > &Values, DIExpression *Expression)
static unsigned findMatchingInlineAsmOperand(unsigned OperandNo, const std::vector< SDValue > &AsmNodeOperands)
static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo, SDISelAsmOperandInfo &MatchingOpInfo, SelectionDAG &DAG)
Make sure that the output operand OpInfo and its corresponding input operand MatchingOpInfo have comp...
static void findUnwindDestinations(FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB, BranchProbability Prob, SmallVectorImpl< std::pair< MachineBasicBlock *, BranchProbability > > &UnwindDests)
When an invoke or a cleanupret unwinds to the next EH pad, there are many places it could ultimately ...
static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic)
static BranchProbability scaleCaseProbality(BranchProbability CaseProb, BranchProbability PeeledCaseProb)
static SDValue expandExp2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp2 - Lower an exp2 intrinsic.
static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue Scale, SelectionDAG &DAG, const TargetLowering &TLI)
static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt, const SDLoc &dl)
getF32Constant - Get 32-bit floating point constant.
static SDValue widenVectorToPartType(SelectionDAG &DAG, SDValue Val, const SDLoc &DL, EVT PartVT)
static SDValue expandLog10(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog10 - Lower a log10 intrinsic.
DenseMap< const Argument *, std::pair< const AllocaInst *, const StoreInst * > > ArgCopyElisionMapTy
static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv)
getCopyToPartsVector - Create a series of nodes that contain the specified value split into legal par...
static void getUnderlyingArgRegs(SmallVectorImpl< std::pair< Register, TypeSize > > &Regs, const SDValue &N)
static void getCopyToParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv=std::nullopt, ISD::NodeType ExtendKind=ISD::ANY_EXTEND)
getCopyToParts - Create a series of nodes that contain the specified value split into legal parts.
static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, SelectionDAGBuilder &Builder)
static SDValue expandLog2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog2 - Lower a log2 intrinsic.
static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location, SDISelAsmOperandInfo &OpInfo, SelectionDAG &DAG)
Get a direct memory input to behave well as an indirect operand.
static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel)
isOnlyUsedInEntryBlock - If the specified argument is only used in the entry block,...
static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V, const Twine &ErrMsg)
static bool collectInstructionDeps(SmallMapVector< const Instruction *, bool, 8 > *Deps, const Value *V, SmallMapVector< const Instruction *, bool, 8 > *Necessary=nullptr, unsigned Depth=0)
static void findArgumentCopyElisionCandidates(const DataLayout &DL, FunctionLoweringInfo *FuncInfo, ArgCopyElisionMapTy &ArgCopyElisionCandidates)
Scan the entry block of the function in FuncInfo for arguments that look like copies into a local all...
static bool isFunction(SDValue Op)
static SDValue GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, const SDLoc &dl)
GetExponent - Get the exponent:
static Register FollowCopyChain(MachineRegisterInfo &MRI, Register Reg)
static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS, SelectionDAG &DAG)
ExpandPowI - Expand a llvm.powi intrinsic.
static SDValue expandLog(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog - Lower a log intrinsic.
static SDValue getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC=std::nullopt, std::optional< ISD::NodeType > AssertOp=std::nullopt)
getCopyFromParts - Create a value that contains the specified legal parts combined into the value the...
static SDValue getLimitedPrecisionExp2(SDValue t0, const SDLoc &dl, SelectionDAG &DAG)
static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl)
GetSignificand - Get the significand and build it into a floating-point number with exponent of 1:
static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp - Lower an exp intrinsic.
static const MDNode * getRangeMetadata(const Instruction &I)
static cl::opt< unsigned, true > LimitFPPrecision("limit-float-precision", cl::desc("Generate low-precision inline sequences " "for some float libcalls"), cl::location(LimitFloatPrecision), cl::Hidden, cl::init(0))
static void tryToElideArgumentCopy(FunctionLoweringInfo &FuncInfo, SmallVectorImpl< SDValue > &Chains, DenseMap< int, int > &ArgCopyElisionFrameIndexMap, SmallPtrSetImpl< const Instruction * > &ElidedArgCopyInstrs, ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg, ArrayRef< SDValue > ArgVals, bool &ArgHasUses)
Try to elide argument copies from memory into a local alloca.
static unsigned LimitFloatPrecision
LimitFloatPrecision - Generate low-precision inline sequences for some float libcalls (6,...
static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC)
getCopyFromPartsVector - Create a value that contains the specified legal parts combined into the val...
static bool InBlock(const Value *V, const BasicBlock *BB)
static LLVM_ATTRIBUTE_ALWAYS_INLINE MVT::SimpleValueType getSimpleVT(const unsigned char *MatcherTable, unsigned &MatcherIndex)
getSimpleVT - Decode a value in MatcherTable, if it's a VBR encoded value, use GetVBR to decode it.
This file defines the SmallPtrSet class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h: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.
Value * RHS
Value * LHS
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
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.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Check if an argument has a given attribute.
Definition Function.cpp:339
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
iterator end() const
Definition ArrayRef.h:132
size_t size() const
size - Get the array size.
Definition ArrayRef.h:143
iterator begin() const
Definition ArrayRef.h:131
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:138
A cache of @llvm.assume calls within a function.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:361
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This class represents a no-op cast from one type to another.
The address of a basic block.
Definition Constants.h:899
Conditional or Unconditional Branch instruction.
Analysis providing branch probability information.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static uint32_t getDenominator()
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
BranchProbability getCompl() const
static BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
CallingConv::ID getCallingConv() const
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI bool isTailCall() const
Tests if this call site is marked as a tail call.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition InstrTypes.h: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:593
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1120
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:214
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
A signed pointer, in the ptrauth sense.
Definition Constants.h:1032
uint64_t getZExtValue() const
Constant Vector Declarations.
Definition Constants.h:517
This is an important base class in LLVM.
Definition Constant.h:43
This is the common base class for constrained floating point intrinsics.
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI unsigned getNonMetadataArgCount() const
DWARF expression.
LLVM_ABI bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B)
Check if fragments overlap between a pair of FragmentInfos.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI std::optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
LLVM_ABI uint64_t getNumLocationOperands() const
Return the number of unique location operands referred to (via DW_OP_LLVM_arg) in this expression; th...
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
static LLVM_ABI const DIExpression * convertToUndefExpression(const DIExpression *Expr)
Removes all elements from Expr that do not apply to an undef debug value, which includes every operat...
static LLVM_ABI DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
static LLVM_ABI DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
Base class for variables.
LLVM_ABI std::optional< uint64_t > getSizeInBits() const
Determines the size of the variable's type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isBigEndian() const
Definition DataLayout.h:208
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
DIExpression * getExpression() const
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) const
DILocalVariable * getVariable() const
LLVM_ABI iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
A debug info location.
Definition DebugLoc.h:124
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:69
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h: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:233
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:310
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:316
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:321
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:64
An instruction for ordering other memory operations.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:803
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
BranchProbabilityInfo * BPI
MachineBasicBlock * getMBB(const BasicBlock *BB) const
DenseMap< const AllocaInst *, int > StaticAllocaMap
StaticAllocaMap - Keep track of frame indices for fixed sized allocas in the entry block.
const LiveOutInfo * GetLiveOutRegInfo(Register Reg)
GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the register is a PHI destinat...
MachineBasicBlock * MBB
MBB - The current block.
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Type * getParamType(unsigned i) const
Parameter type accessors.
Type * getReturnType() const
Data structure describing the variable locations in a function.
const BasicBlock & getEntryBlock() const
Definition Function.h:807
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:244
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition Function.cpp:739
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:270
Constant * getPersonalityFn() const
Get the personality function associated with this function.
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:249
size_t arg_size() const
Definition Function.h:899
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
Garbage collection metadata for a single function.
Definition GCMetadata.h:80
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
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:1078
Machine Value Type.
@ INVALID_SIMPLE_VALUE_TYPE
uint64_t getScalarSizeInBits() const
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
ElementCount getVectorElementCount() const
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
bool bitsGE(MVT VT) const
Return true if this has no less bits than VT.
bool isScalarInteger() const
Return true if this is an integer, not including vectors.
static MVT getVectorVT(MVT VT, unsigned NumElements)
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
static MVT getIntegerVT(unsigned BitWidth)
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void setIsEHContTarget(bool V=true)
Indicates if this is a target of Windows EH Continuation Guard.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
MachineInstrBundleIterator< MachineInstr > iterator
void setIsEHScopeEntry(bool V=true)
Indicates if this is the entry block of an EH scope, i.e., the block that that used to have a catchpa...
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
int getStackProtectorIndex() const
Return the index for the stack protector object.
void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased)
Set "maybe pointed to by an LLVM IR value" for an object.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
void setFunctionContextIndex(int I)
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site)
Map the begin label for a call site.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD)
Record annotations associated with a particular label.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
void setHasEHContTarget(bool V)
void addInvoke(MachineBasicBlock *LandingPad, MCSymbol *BeginLabel, MCSymbol *EndLabel)
Provide the begin and end labels of an invoke style call and associate it with a try landing pad bloc...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
Representation of each machine instruction.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI MCRegister getLiveInPhysReg(Register VReg) const
getLiveInPhysReg - If VReg is a live-in virtual register, return the corresponding live-in physical r...
An SDNode that represents everything that will be needed to construct a MachineInstr.
bool contains(const KeyT &Key) const
Definition MapVector.h:141
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:111
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:183
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:78
Resume the propagation of an exception.
Return a value (possibly void), from a function.
Holds the information from a dbg_label node through SDISel.
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getOpcode() const
SelectionDAGBuilder - This is the common target-independent lowering implementation that is parameter...
SDValue getValue(const Value *V)
getValue - Return an SDValue for the given Value.
DenseMap< const Constant *, Register > ConstantsOut
void addDanglingDebugInfo(SmallVectorImpl< Value * > &Values, DILocalVariable *Var, DIExpression *Expr, bool IsVariadic, DebugLoc DL, unsigned Order)
Register a dbg_value which relies on a Value which we have not yet seen.
void visitDbgInfo(const Instruction &I)
void clearDanglingDebugInfo()
Clear the dangling debug information map.
void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall, bool IsMustTailCall, const BasicBlock *EHPadBB=nullptr, const TargetLowering::PtrAuthInfo *PAI=nullptr)
void clear()
Clear out the current SelectionDAG and the associated state and prepare this SelectionDAGBuilder obje...
void visitBitTestHeader(SwitchCG::BitTestBlock &B, MachineBasicBlock *SwitchBB)
visitBitTestHeader - This function emits necessary code to produce value suitable for "bit tests"
void LowerStatepoint(const GCStatepointInst &I, const BasicBlock *EHPadBB=nullptr)
std::unique_ptr< SDAGSwitchLowering > SL
SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, SDValue Op)
bool HasTailCall
This is set to true if a call in the current block has been translated as a tail call.
bool ShouldEmitAsBranches(const std::vector< SwitchCG::CaseBlock > &Cases)
If the set of cases should be emitted as a series of branches, return true.
void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
EmitBranchForMergedCondition - Helper method for FindMergedConditions.
void LowerDeoptimizeCall(const CallInst *CI)
void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB)
SwiftErrorValueTracking & SwiftError
Information about the swifterror values used throughout the function.
SDValue getNonRegisterValue(const Value *V)
getNonRegisterValue - Return an SDValue for the given Value, but don't look in FuncInfo....
DenseMap< MachineBasicBlock *, SmallVector< unsigned, 4 > > LPadToCallSiteMap
Map a landing pad to the call site indexes.
void handleDebugDeclare(Value *Address, DILocalVariable *Variable, DIExpression *Expression, DebugLoc DL)
bool shouldKeepJumpConditionsTogether(const FunctionLoweringInfo &FuncInfo, const BranchInst &I, Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs, TargetLoweringBase::CondMergingParams Params) const
StatepointLoweringState StatepointLowering
State used while lowering a statepoint sequence (gc_statepoint, gc_relocate, and gc_result).
void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB, BranchProbability BranchProbToNext, Register Reg, SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB)
visitBitTestCase - this function produces one "bit test"
bool canTailCall(const CallBase &CB) const
void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, AttributeSet RetAttrs, bool IsPatchPoint)
Populate a CallLowerinInfo (into CLI) based on the properties of the call being lowered.
void CopyValueToVirtualRegister(const Value *V, Register Reg, ISD::NodeType ExtendType=ISD::ANY_EXTEND)
void salvageUnresolvedDbgValue(const Value *V, DanglingDebugInfo &DDI)
For the given dangling debuginfo record, perform last-ditch efforts to resolve the debuginfo to somet...
SmallVector< SDValue, 8 > PendingLoads
Loads are not emitted to the program immediately.
GCFunctionInfo * GFI
Garbage collection metadata for the function.
SDValue getRoot()
Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) items.
void ExportFromCurrentBlock(const Value *V)
ExportFromCurrentBlock - If this condition isn't known to be exported from the current basic block,...
void init(GCFunctionInfo *gfi, BatchAAResults *BatchAA, AssumptionCache *AC, const TargetLibraryInfo *li)
void resolveOrClearDbgInfo()
Evict any dangling debug information, attempting to salvage it first.
std::pair< SDValue, SDValue > lowerInvokable(TargetLowering::CallLoweringInfo &CLI, const BasicBlock *EHPadBB=nullptr)
SDValue getMemoryRoot()
Return the current virtual root of the Selection DAG, flushing any PendingLoad items.
void resolveDanglingDebugInfo(const Value *V, SDValue Val)
If we saw an earlier dbg_value referring to V, generate the debug data structures now that we've seen...
void visit(const Instruction &I)
void dropDanglingDebugInfo(const DILocalVariable *Variable, const DIExpression *Expr)
If we have dangling debug info that describes Variable, or an overlapping part of variable considerin...
SDValue getCopyFromRegs(const Value *V, Type *Ty)
If there was virtual register allocated for the value V emit CopyFromReg of the specified type Ty.
void CopyToExportRegsIfNeeded(const Value *V)
CopyToExportRegsIfNeeded - If the given value has virtual registers created for it,...
void handleKillDebugValue(DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order)
Create a record for a kill location debug intrinsic.
void visitJumpTable(SwitchCG::JumpTable &JT)
visitJumpTable - Emit JumpTable node in the current MBB
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 > 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 > EmitTargetCodeForStrcpy(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dest, SDValue Src, MachinePointerInfo DestPtrInfo, MachinePointerInfo SrcPtrInfo, bool isStpcpy) const
Emit target-specific code that performs a strcpy or stpcpy, in cases where that is faster than a libc...
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemchr(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Src, SDValue Char, SDValue Length, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memchr, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, const CallInst *CI) const
Emit target-specific code that performs a memcmp/bcmp, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, MachinePointerInfo Op1PtrInfo, MachinePointerInfo Op2PtrInfo) const
Emit target-specific code that performs a strcmp, in cases where that is faster than a libcall.
virtual SDValue EmitTargetCodeForSetTag(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Addr, SDValue Size, MachinePointerInfo DstPtrInfo, bool ZeroData) const
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SDValue getExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT, unsigned Opcode)
Convert Op, which must be of integer type, to the integer type VT, by either any/sign/zero-extending ...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
const TargetSubtargetInfo & getSubtarget() const
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
const TargetLowering & getTargetLoweringInfo() const
static constexpr unsigned MaxRecursionDepth
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
const DataLayout & getDataLayout() const
SDValue getTargetFrameIndex(int FI, EVT VT)
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
MachineFunction & getMachineFunction() const
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVMContext * getContext() const
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void swap(SmallVectorImpl &RHS)
void resize(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Encapsulates all of the information needed to generate a stack protector check, and signals to isel w...
MachineBasicBlock * getSuccessMBB()
MachineBasicBlock * getFailureMBB()
MachineBasicBlock * getParentMBB()
bool shouldEmitFunctionBasedCheckStackProtector() const
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Multiway switch.
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Provides information about what library functions are available for the current target.
virtual Align getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Returns the desired alignment for ByVal or InAlloca aggregate function arguments in the caller parame...
virtual bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, EVT) const
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
EVT getMemValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
virtual bool useStackGuardXorFP() const
If this function returns true, stack protection checks should XOR the frame pointer (or whichever poi...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
virtual bool isLegalScaleForGatherScatter(uint64_t Scale, uint64_t ElemSize) const
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
const TargetMachine & getTargetMachine() const
virtual unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain targets require unusual breakdowns of certain types.
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
virtual Value * getSDagStackGuard(const Module &M) const
Return the variable that's previously inserted by insertSSPDeclarations, if any, otherwise return nul...
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const
Return the number of registers that this ValueType will eventually require.
unsigned getBitWidthForCttzElements(Type *RetTy, ElementCount EC, bool ZeroIsPoison, const ConstantRange *VScaleRange) const
Return the minimum number of bits required to hold the maximum possible number of trailing zero vecto...
virtual bool shouldExtendGSIndex(EVT VT, EVT &EltTy) const
Returns true if the index type for a masked gather/scatter requires extending.
virtual unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
Function * getSSPStackGuardCheck(const Module &M) const
If the target has a standard stack protection check function that performs validation and error handl...
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
LegalizeAction getFixedPointOperationAction(unsigned Op, EVT VT, unsigned Scale) const
Some fixed point operations may be natively supported by the target but only for specific scales.
MachineMemOperand::Flags getAtomicMemOperandFlags(const Instruction &AI, const DataLayout &DL) const
virtual bool getTgtMemIntrinsic(IntrinsicInfo &, const CallInst &, MachineFunction &, unsigned) const
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
virtual bool allowsMisalignedMemoryAccesses(EVT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *=nullptr) const
Determine if the target supports unaligned memory accesses.
bool isOperationCustom(unsigned Op, EVT VT) const
Return true if the operation uses custom lowering, regardless of whether the type is legal or not.
bool hasBigEndianPartOrdering(EVT VT, const DataLayout &DL) const
When splitting a value of the specified type into parts, does the Lo or Hi part come first?
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
virtual Align getABIAlignmentForCallingConv(Type *ArgTy, const DataLayout &DL) const
Certain targets have context sensitive alignment requirements, where one type has the alignment requi...
MachineMemOperand::Flags getVPIntrinsicMemOperandFlags(const VPIntrinsic &VPIntrin) const
virtual bool shouldExpandGetActiveLaneMask(EVT VT, EVT OpVT) const
Return true if the @llvm.get.active.lane.mask intrinsic should be expanded using generic code in Sele...
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
MachineMemOperand::Flags getLoadMemOperandFlags(const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC=nullptr, const TargetLibraryInfo *LibInfo=nullptr) const
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
MVT getProgramPointerTy(const DataLayout &DL) const
Return the type for code pointers, which is determined by the program address space specified through...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
virtual bool shouldExpandVectorMatch(EVT VT, unsigned SearchSize) const
Return true if the @llvm.experimental.vector.match intrinsic should be expanded for vector type ‘VT’ ...
virtual MVT getFenceOperandTy(const DataLayout &DL) const
Return the type for operands of fence.
virtual bool shouldExpandGetVectorLength(EVT CountVT, unsigned VF, bool IsScalable) const
bool isOperationLegalOrCustom(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
virtual MVT hasFastEqualityCompare(unsigned NumBits) const
Return the preferred operand type if the target has a quick way to compare integer values of the give...
MachineMemOperand::Flags getStoreMemOperandFlags(const StoreInst &SI, const DataLayout &DL) const
virtual bool shouldExpandCttzElements(EVT VT) const
Return true if the @llvm.experimental.cttz.elts intrinsic should be expanded using generic code in Se...
virtual bool signExtendConstant(const ConstantInt *C) const
Return true if this constant should be sign extended when promoting to a larger type.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
virtual Register getExceptionPointerRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception address on entry to an ...
bool supportsUnalignedAtomics() const
Whether the target supports unaligned atomic operations.
std::vector< ArgListEntry > ArgListTy
bool isBeneficialToExpandPowI(int64_t Exponent, bool OptForSize) const
Return true if it is beneficial to expand an @llvm.powi.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual Register getExceptionSelectorRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception typeid on entry to a la...
virtual MVT getPointerMemTy(const DataLayout &DL, uint32_t AS=0) const
Return the in-memory pointer type for the given address space, defaults to the pointer type from the ...
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
virtual MVT getVPExplicitVectorLengthTy() const
Returns the type to be used for the EVL/AVL operand of VP nodes: ISD::VP_ADD, ISD::VP_SUB,...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool supportKCFIBundles() const
Return true if the target supports kcfi operand bundles.
virtual bool supportPtrAuthBundles() const
Return true if the target supports ptrauth operand bundles.
virtual bool supportSwiftError() const
Return true if the target supports swifterror attribute.
virtual SDValue visitMaskedLoad(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue &NewLoad, SDValue Ptr, SDValue PassThru, SDValue Mask) const
virtual SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, const SDLoc &DL) const
virtual EVT getTypeForExtReturn(LLVMContext &Context, EVT VT, ISD::NodeType) const
Return the type that should be used to zero or sign extend a zeroext/signext integer return value.
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, ArrayRef< SDValue > Ops, MakeLibCallOptions CallOptions, const SDLoc &dl, SDValue Chain=SDValue()) const
Returns a pair of (return value, chain).
virtual InlineAsm::ConstraintCode getInlineAsmMemConstraint(StringRef ConstraintCode) const
std::vector< AsmOperandInfo > AsmOperandInfoVector
SDValue expandIS_FPCLASS(EVT ResultVT, SDValue Op, FPClassTest Test, SDNodeFlags Flags, const SDLoc &DL, SelectionDAG &DAG) const
Expand check for floating point class.
virtual SDValue prepareVolatileOrAtomicLoad(SDValue Chain, const SDLoc &DL, SelectionDAG &DAG) const
This callback is used to prepare for a volatile or atomic load.
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
virtual bool splitValueIntoRegisterParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, std::optional< CallingConv::ID > CC) const
Target-specific splitting of values into parts that fit a register storing a legal type.
virtual SDValue joinRegisterPartsIntoValue(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, std::optional< CallingConv::ID > CC) const
Target-specific combining of register parts into its original value.
virtual SDValue LowerCall(CallLoweringInfo &, SmallVectorImpl< SDValue > &) const
This hook must be implemented to lower calls into the specified DAG.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual SDValue LowerAsmOutputForConstraint(SDValue &Chain, SDValue &Glue, const SDLoc &DL, const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, const CallBase &Call) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const
This callback is invoked for operations that are unsupported by the target, which are registered to u...
virtual bool functionArgumentNeedsConsecutiveRegisters(Type *Ty, CallingConv::ID CallConv, bool isVarArg, const DataLayout &DL) const
For some targets, an LLVM struct type must be broken down into multiple simple types,...
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo,...
virtual void CollectTargetIntrinsicOperands(const CallInst &I, SmallVectorImpl< SDValue > &Ops, SelectionDAG &DAG) const
virtual SDValue visitMaskedStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue Ptr, SDValue Val, SDValue Mask) const
virtual bool useLoadStackGuardNode(const Module &M) const
If this function returns true, SelectionDAGBuilder emits a LOAD_STACK_GUARD node when it is lowering ...
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
virtual void LowerOperationWrapper(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const
This callback is invoked by the type legalizer to legalize nodes with an illegal operand type but leg...
virtual bool isInlineAsmTargetBranch(const SmallVectorImpl< StringRef > &AsmStrs, unsigned OpNo) const
On x86, return true if the operand with index OpNo is a CALL or JUMP instruction, which can use eithe...
virtual MVT getJumpTableRegTy(const DataLayout &DL) const
virtual bool CanLowerReturn(CallingConv::ID, MachineFunction &, bool, const SmallVectorImpl< ISD::OutputArg > &, LLVMContext &, const Type *RetTy) const
This hook should be implemented to check whether the return values described by the Outs array can fi...
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
virtual TargetTransformInfo getTargetTransformInfo(const Function &F) const
Return a TargetTransformInfo for a given function.
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
unsigned getID() const
Return the register class ID number.
iterator begin() const
begin/end - Return all of the registers in this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
@ TCK_Latency
The latency of instruction.
LLVM_ABI bool hasConditionalLoadStoreForType(Type *Ty, bool IsStore) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:344
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
Definition Type.cpp:181
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:294
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:234
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:301
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_iterator op_begin()
Definition User.h:284
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
op_iterator op_end()
Definition User.h:286
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
LLVM_ABI CmpInst::Predicate getPredicate() const
This is the common base class for vector predication intrinsics.
static LLVM_ABI std::optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
LLVM_ABI MaybeAlign getPointerAlignment() const
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:693
bool use_empty() const
Definition Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1091
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:231
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
const ParentTy * getParent() const
Definition ilist_node.h:34
A raw_ostream that writes to an std::string.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:780
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:504
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ LOOP_DEPENDENCE_RAW_MASK
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition ISDOpcodes.h:163
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:593
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:771
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:387
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:393
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:841
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ EH_SJLJ_SETUP_DISPATCH
OUTCHAIN = EH_SJLJ_SETUP_DISPATCH(INCHAIN) The target initializes the dispatch table here.
Definition ISDOpcodes.h:167
@ GlobalAddress
Definition ISDOpcodes.h:88
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:868
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:577
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:744
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:521
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition ISDOpcodes.h:508
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:400
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:151
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:832
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition ISDOpcodes.h:117
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:779
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:628
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:534
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:541
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:369
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:784
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:242
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:669
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:225
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:958
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:762
@ 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:607
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:134
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:569
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:838
@ LOCAL_RECOVER
LOCAL_RECOVER - Represents the llvm.localrecover intrinsic.
Definition ISDOpcodes.h:130
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:724
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:633
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:406
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:793
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:145
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:493
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:914
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:736
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:200
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:732
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition ISDOpcodes.h:420
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:236
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:558
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition ISDOpcodes.h:654
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:947
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:696
@ SPONENTRY
SPONENTRY - Represents the llvm.sponentry intrinsic.
Definition ISDOpcodes.h:122
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:933
@ EH_SJLJ_SETJMP
RESULT, OUTCHAIN = EH_SJLJ_SETJMP(INCHAIN, buffer) This corresponds to the eh.sjlj....
Definition ISDOpcodes.h:157
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:844
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:527
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:617
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:208
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:549
@ LOOP_DEPENDENCE_WAR_MASK
Set rounding mode.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Offsets
Offsets in bytes from the start of the input buffer.
std::pair< JumpTableHeader, JumpTable > JumpTableBlock
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::vector< CaseCluster > CaseClusterVector
@ CC_Range
A cluster of adjacent case labels with the same destination, or just one case.
@ CC_JumpTable
A cluster of cases suitable for jump table lowering.
@ CC_BitTests
A cluster of cases suitable for bit test lowering.
SmallVector< SwitchWorkListItem, 4 > SwitchWorkList
CaseClusterVector::iterator CaseClusterIt
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebStrict
This corresponds to "fpexcept.strict".
Definition FPEnv.h:42
@ ebMayTrap
This corresponds to "fpexcept.maytrap".
Definition FPEnv.h:41
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
constexpr float log2ef
Definition MathExtras.h:51
constexpr double e
constexpr float ln2f
Definition MathExtras.h:49
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:477
@ Length
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
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:1655
LLVM_ABI void GetReturnInfo(CallingConv::ID CC, Type *ReturnType, AttributeList attr, SmallVectorImpl< ISD::OutputArg > &Outs, const TargetLowering &TLI, const DataLayout &DL)
Given an LLVM IR type and return type attributes, compute the return value EVTs and flags,...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h: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:2136
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 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:1150
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:1732
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:852
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
generic_gep_type_iterator<> gep_type_iterator
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
auto succ_size(const MachineBasicBlock *BB)
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition STLExtras.h: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:207
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h: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:2274
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Global
Append to llvm.global_dtors.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
TargetTransformInfo TTI
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
LLVM_ABI llvm::SmallVector< int, 16 > createInterleaveMask(unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ And
Bitwise or logical AND of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h: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:543
DWARFExpression::Operation Op
ISD::CondCode getFCmpCodeWithoutNaN(ISD::CondCode CC)
getFCmpCodeWithoutNaN - Given an ISD condition code comparing floats, return the equivalent code if w...
Definition Analysis.cpp:229
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition Analysis.cpp:119
ArrayRef(const T &OneElt) -> ArrayRef< T >
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition FPEnv.cpp:24
gep_type_iterator gep_type_begin(const User *GEP)
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2120
GlobalValue * ExtractTypeInfo(Value *V)
ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
Definition Analysis.cpp:185
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1897
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:2108
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:180
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
#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:395
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
uint64_t getScalarStoreSize() const
Definition ValueTypes.h:402
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isRISCVVectorTuple() const
Return true if this is a vector value type.
Definition ValueTypes.h:179
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:381
bool isFixedLengthVector() const
Definition ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:157
EVT changeVectorElementType(EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:102
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
InputArg - This struct carries flags and type information about a single incoming (formal) argument o...
static const unsigned NoArgIndex
Sentinel value for implicit machine-level input arguments.
OutputArg - This struct carries flags and a value for a single outgoing (actual) argument or outgoing...
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber/label.
Definition InlineAsm.h:128
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:130
A lightweight accessor for an operand bundle meant to be passed around by value.
This struct represents the registers (physical or virtual) that a particular set of values is assigne...
SmallVector< std::pair< Register, TypeSize >, 4 > getRegsAndSizes() const
Return a list of registers and their sizes.
RegsForValue()=default
SmallVector< unsigned, 4 > RegCount
This list holds the number of registers for each value.
SmallVector< EVT, 4 > ValueVTs
The value types of the values, which may not be legal, and may need be promoted or synthesized from o...
SmallVector< Register, 4 > Regs
This list holds the registers assigned to the values.
void AddInlineAsmOperands(InlineAsm::Kind Code, bool HasMatching, unsigned MatchingIdx, const SDLoc &dl, SelectionDAG &DAG, std::vector< SDValue > &Ops) const
Add this value to the specified inlineasm node operand list.
SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr) const
Emit a series of CopyFromReg nodes that copies from this value and returns the result as a ValueVTs v...
SmallVector< MVT, 4 > RegVTs
The value types of the registers.
void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr, ISD::NodeType PreferredExtendType=ISD::ANY_EXTEND) const
Emit a series of CopyToReg nodes that copies the specified value into the registers specified by this...
std::optional< CallingConv::ID > CallConv
Records if this value needs to be treated in an ABI dependant manner, different to normal type legali...
bool occupiesMultipleRegs() const
Check if the total RegCount is greater than one.
These are IR-level optimization flags that may be propagated to SDNodes.
void copyFMF(const FPMathOperator &FPMO)
Propagate the fast-math-flags from an IR FPMathOperator.
void setUnpredictable(bool b)
bool hasAllowReassociation() const
void setNoUnsignedWrap(bool b)
void setNoSignedWrap(bool b)
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:257
This structure is used to communicate between SelectionDAGBuilder and SDISel for the code generation ...
SDLoc DL
The debug location of the instruction this CaseBlock was produced from.
static CaseCluster range(const ConstantInt *Low, const ConstantInt *High, MachineBasicBlock *MBB, BranchProbability Prob)
This contains information for each constraint that we are lowering.
TargetLowering::ConstraintType ConstraintType
Information about the constraint code, e.g.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setConvergent(bool Value=true)
CallLoweringInfo & setCFIType(const ConstantInt *Type)
SmallVector< ISD::InputArg, 32 > Ins
Type * OrigRetTy
Original unlegalized return type.
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setIsPatchPoint(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setIsPreallocated(bool Value=true)
CallLoweringInfo & setConvergenceControlToken(SDValue Token)
SmallVector< ISD::OutputArg, 32 > Outs
Type * RetTy
Same as OrigRetTy, or partially legalized for soft float libcalls.
CallLoweringInfo & setChain(SDValue InChain)
CallLoweringInfo & setPtrAuth(PtrAuthInfo Value)
CallLoweringInfo & setCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList, AttributeSet ResultAttrs={})
This structure is used to pass arguments to makeLibCall function.
MakeLibCallOptions & setDiscardResult(bool Value=true)
This structure contains the information necessary for lowering pointer-authenticating indirect calls.
void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)