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 // Chain up all pending constrained intrinsics together with all
1167 // pending loads, by simply appending them to PendingLoads and
1168 // then calling getMemoryRoot().
1169 PendingLoads.reserve(PendingLoads.size() +
1170 PendingConstrainedFP.size() +
1171 PendingConstrainedFPStrict.size());
1172 PendingLoads.append(PendingConstrainedFP.begin(),
1173 PendingConstrainedFP.end());
1174 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1175 PendingConstrainedFPStrict.end());
1176 PendingConstrainedFP.clear();
1177 PendingConstrainedFPStrict.clear();
1178 return getMemoryRoot();
1179}
1180
1182 // We need to emit pending fpexcept.strict constrained intrinsics,
1183 // so append them to the PendingExports list.
1184 PendingExports.append(PendingConstrainedFPStrict.begin(),
1185 PendingConstrainedFPStrict.end());
1186 PendingConstrainedFPStrict.clear();
1187 return updateRoot(PendingExports);
1188}
1189
1191 DILocalVariable *Variable,
1193 DebugLoc DL) {
1194 assert(Variable && "Missing variable");
1195
1196 // Check if address has undef value.
1197 if (!Address || isa<UndefValue>(Address) ||
1198 (Address->use_empty() && !isa<Argument>(Address))) {
1199 LLVM_DEBUG(
1200 dbgs()
1201 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1202 return;
1203 }
1204
1205 bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1206
1207 SDValue &N = NodeMap[Address];
1208 if (!N.getNode() && isa<Argument>(Address))
1209 // Check unused arguments map.
1210 N = UnusedArgNodeMap[Address];
1211 SDDbgValue *SDV;
1212 if (N.getNode()) {
1213 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1214 Address = BCI->getOperand(0);
1215 // Parameters are handled specially.
1216 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1217 if (IsParameter && FINode) {
1218 // Byval parameter. We have a frame index at this point.
1219 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1220 /*IsIndirect*/ true, DL, SDNodeOrder);
1221 } else if (isa<Argument>(Address)) {
1222 // Address is an argument, so try to emit its dbg value using
1223 // virtual register info from the FuncInfo.ValueMap.
1224 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1225 FuncArgumentDbgValueKind::Declare, N);
1226 return;
1227 } else {
1228 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1229 true, DL, SDNodeOrder);
1230 }
1231 DAG.AddDbgValue(SDV, IsParameter);
1232 } else {
1233 // If Address is an argument then try to emit its dbg value using
1234 // virtual register info from the FuncInfo.ValueMap.
1235 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1236 FuncArgumentDbgValueKind::Declare, N)) {
1237 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1238 << " (could not emit func-arg dbg_value)\n");
1239 }
1240 }
1241}
1242
1244 // Add SDDbgValue nodes for any var locs here. Do so before updating
1245 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1246 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1247 // Add SDDbgValue nodes for any var locs here. Do so before updating
1248 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1249 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1250 It != End; ++It) {
1251 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1252 dropDanglingDebugInfo(Var, It->Expr);
1253 if (It->Values.isKillLocation(It->Expr)) {
1254 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1255 continue;
1256 }
1257 SmallVector<Value *> Values(It->Values.location_ops());
1258 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1259 It->Values.hasArgList())) {
1260 SmallVector<Value *, 4> Vals(It->Values.location_ops());
1262 FnVarLocs->getDILocalVariable(It->VariableID),
1263 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1264 }
1265 }
1266 }
1267
1268 // We must skip DbgVariableRecords if they've already been processed above as
1269 // we have just emitted the debug values resulting from assignment tracking
1270 // analysis, making any existing DbgVariableRecords redundant (and probably
1271 // less correct). We still need to process DbgLabelRecords. This does sink
1272 // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1273 // be important as it does so deterministcally and ordering between
1274 // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1275 // printing).
1276 bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1277 // Is there is any debug-info attached to this instruction, in the form of
1278 // DbgRecord non-instruction debug-info records.
1279 for (DbgRecord &DR : I.getDbgRecordRange()) {
1280 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1281 assert(DLR->getLabel() && "Missing label");
1282 SDDbgLabel *SDV =
1283 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1284 DAG.AddDbgLabel(SDV);
1285 continue;
1286 }
1287
1288 if (SkipDbgVariableRecords)
1289 continue;
1291 DILocalVariable *Variable = DVR.getVariable();
1294
1296 if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1297 continue;
1298 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1299 << "\n");
1301 DVR.getDebugLoc());
1302 continue;
1303 }
1304
1305 // A DbgVariableRecord with no locations is a kill location.
1307 if (Values.empty()) {
1309 SDNodeOrder);
1310 continue;
1311 }
1312
1313 // A DbgVariableRecord with an undef or absent location is also a kill
1314 // location.
1315 if (llvm::any_of(Values,
1316 [](Value *V) { return !V || isa<UndefValue>(V); })) {
1318 SDNodeOrder);
1319 continue;
1320 }
1321
1322 bool IsVariadic = DVR.hasArgList();
1323 if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1324 SDNodeOrder, IsVariadic)) {
1325 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1326 DVR.getDebugLoc(), SDNodeOrder);
1327 }
1328 }
1329}
1330
1332 visitDbgInfo(I);
1333
1334 // Set up outgoing PHI node register values before emitting the terminator.
1335 if (I.isTerminator()) {
1336 HandlePHINodesInSuccessorBlocks(I.getParent());
1337 }
1338
1339 ++SDNodeOrder;
1340 CurInst = &I;
1341
1342 // Set inserted listener only if required.
1343 bool NodeInserted = false;
1344 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1345 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1346 MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1347 if (PCSectionsMD || MMRA) {
1348 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1349 DAG, [&](SDNode *) { NodeInserted = true; });
1350 }
1351
1352 visit(I.getOpcode(), I);
1353
1354 if (!I.isTerminator() && !HasTailCall &&
1355 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1357
1358 // Handle metadata.
1359 if (PCSectionsMD || MMRA) {
1360 auto It = NodeMap.find(&I);
1361 if (It != NodeMap.end()) {
1362 if (PCSectionsMD)
1363 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1364 if (MMRA)
1365 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1366 } else if (NodeInserted) {
1367 // This should not happen; if it does, don't let it go unnoticed so we can
1368 // fix it. Relevant visit*() function is probably missing a setValue().
1369 errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1370 << I.getModule()->getName() << "]\n";
1371 LLVM_DEBUG(I.dump());
1372 assert(false);
1373 }
1374 }
1375
1376 CurInst = nullptr;
1377}
1378
1379void SelectionDAGBuilder::visitPHI(const PHINode &) {
1380 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1381}
1382
1383void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1384 // Note: this doesn't use InstVisitor, because it has to work with
1385 // ConstantExpr's in addition to instructions.
1386 switch (Opcode) {
1387 default: llvm_unreachable("Unknown instruction type encountered!");
1388 // Build the switch statement using the Instruction.def file.
1389#define HANDLE_INST(NUM, OPCODE, CLASS) \
1390 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1391#include "llvm/IR/Instruction.def"
1392 }
1393}
1394
1396 DILocalVariable *Variable,
1397 DebugLoc DL, unsigned Order,
1400 // For variadic dbg_values we will now insert poison.
1401 // FIXME: We can potentially recover these!
1403 for (const Value *V : Values) {
1404 auto *Poison = PoisonValue::get(V->getType());
1406 }
1407 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1408 /*IsIndirect=*/false, DL, Order,
1409 /*IsVariadic=*/true);
1410 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1411 return true;
1412}
1413
1415 DILocalVariable *Var,
1416 DIExpression *Expr,
1417 bool IsVariadic, DebugLoc DL,
1418 unsigned Order) {
1419 if (IsVariadic) {
1420 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1421 return;
1422 }
1423 // TODO: Dangling debug info will eventually either be resolved or produce
1424 // a poison DBG_VALUE. However in the resolution case, a gap may appear
1425 // between the original dbg.value location and its resolved DBG_VALUE,
1426 // which we should ideally fill with an extra poison DBG_VALUE.
1427 assert(Values.size() == 1);
1428 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1429}
1430
1432 const DIExpression *Expr) {
1433 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1434 DIVariable *DanglingVariable = DDI.getVariable();
1435 DIExpression *DanglingExpr = DDI.getExpression();
1436 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1437 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1438 << printDDI(nullptr, DDI) << "\n");
1439 return true;
1440 }
1441 return false;
1442 };
1443
1444 for (auto &DDIMI : DanglingDebugInfoMap) {
1445 DanglingDebugInfoVector &DDIV = DDIMI.second;
1446
1447 // If debug info is to be dropped, run it through final checks to see
1448 // whether it can be salvaged.
1449 for (auto &DDI : DDIV)
1450 if (isMatchingDbgValue(DDI))
1451 salvageUnresolvedDbgValue(DDIMI.first, DDI);
1452
1453 erase_if(DDIV, isMatchingDbgValue);
1454 }
1455}
1456
1457// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1458// generate the debug data structures now that we've seen its definition.
1460 SDValue Val) {
1461 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1462 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1463 return;
1464
1465 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1466 for (auto &DDI : DDIV) {
1467 DebugLoc DL = DDI.getDebugLoc();
1468 unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1469 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1470 DILocalVariable *Variable = DDI.getVariable();
1471 DIExpression *Expr = DDI.getExpression();
1472 assert(Variable->isValidLocationForIntrinsic(DL) &&
1473 "Expected inlined-at fields to agree");
1474 SDDbgValue *SDV;
1475 if (Val.getNode()) {
1476 // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1477 // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1478 // we couldn't resolve it directly when examining the DbgValue intrinsic
1479 // in the first place we should not be more successful here). Unless we
1480 // have some test case that prove this to be correct we should avoid
1481 // calling EmitFuncArgumentDbgValue here.
1482 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1483 FuncArgumentDbgValueKind::Value, Val)) {
1484 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1485 << printDDI(V, DDI) << "\n");
1486 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump());
1487 // Increase the SDNodeOrder for the DbgValue here to make sure it is
1488 // inserted after the definition of Val when emitting the instructions
1489 // after ISel. An alternative could be to teach
1490 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1491 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1492 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1493 << ValSDNodeOrder << "\n");
1494 SDV = getDbgValue(Val, Variable, Expr, DL,
1495 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1496 DAG.AddDbgValue(SDV, false);
1497 } else
1498 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1499 << printDDI(V, DDI)
1500 << " in EmitFuncArgumentDbgValue\n");
1501 } else {
1502 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1503 << "\n");
1504 auto Poison = PoisonValue::get(V->getType());
1505 auto SDV =
1506 DAG.getConstantDbgValue(Variable, Expr, Poison, DL, DbgSDNodeOrder);
1507 DAG.AddDbgValue(SDV, false);
1508 }
1509 }
1510 DDIV.clear();
1511}
1512
1514 DanglingDebugInfo &DDI) {
1515 // TODO: For the variadic implementation, instead of only checking the fail
1516 // state of `handleDebugValue`, we need know specifically which values were
1517 // invalid, so that we attempt to salvage only those values when processing
1518 // a DIArgList.
1519 const Value *OrigV = V;
1520 DILocalVariable *Var = DDI.getVariable();
1521 DIExpression *Expr = DDI.getExpression();
1522 DebugLoc DL = DDI.getDebugLoc();
1523 unsigned SDOrder = DDI.getSDNodeOrder();
1524
1525 // Currently we consider only dbg.value intrinsics -- we tell the salvager
1526 // that DW_OP_stack_value is desired.
1527 bool StackValue = true;
1528
1529 // Can this Value can be encoded without any further work?
1530 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1531 return;
1532
1533 // Attempt to salvage back through as many instructions as possible. Bail if
1534 // a non-instruction is seen, such as a constant expression or global
1535 // variable. FIXME: Further work could recover those too.
1536 while (isa<Instruction>(V)) {
1537 const Instruction &VAsInst = *cast<const Instruction>(V);
1538 // Temporary "0", awaiting real implementation.
1540 SmallVector<Value *, 4> AdditionalValues;
1541 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1542 Expr->getNumLocationOperands(), Ops,
1543 AdditionalValues);
1544 // If we cannot salvage any further, and haven't yet found a suitable debug
1545 // expression, bail out.
1546 if (!V)
1547 break;
1548
1549 // TODO: If AdditionalValues isn't empty, then the salvage can only be
1550 // represented with a DBG_VALUE_LIST, so we give up. When we have support
1551 // here for variadic dbg_values, remove that condition.
1552 if (!AdditionalValues.empty())
1553 break;
1554
1555 // New value and expr now represent this debuginfo.
1556 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1557
1558 // Some kind of simplification occurred: check whether the operand of the
1559 // salvaged debug expression can be encoded in this DAG.
1560 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1561 LLVM_DEBUG(
1562 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n"
1563 << *OrigV << "\nBy stripping back to:\n " << *V << "\n");
1564 return;
1565 }
1566 }
1567
1568 // This was the final opportunity to salvage this debug information, and it
1569 // couldn't be done. Place a poison DBG_VALUE at this location to terminate
1570 // any earlier variable location.
1571 assert(OrigV && "V shouldn't be null");
1572 auto *Poison = PoisonValue::get(OrigV->getType());
1573 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Poison, DL, SDNodeOrder);
1574 DAG.AddDbgValue(SDV, false);
1575 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n "
1576 << printDDI(OrigV, DDI) << "\n");
1577}
1578
1580 DIExpression *Expr,
1581 DebugLoc DbgLoc,
1582 unsigned Order) {
1586 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1587 /*IsVariadic*/ false);
1588}
1589
1591 DILocalVariable *Var,
1592 DIExpression *Expr, DebugLoc DbgLoc,
1593 unsigned Order, bool IsVariadic) {
1594 if (Values.empty())
1595 return true;
1596
1597 // Filter EntryValue locations out early.
1598 if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1599 return true;
1600
1601 SmallVector<SDDbgOperand> LocationOps;
1602 SmallVector<SDNode *> Dependencies;
1603 for (const Value *V : Values) {
1604 // Constant value.
1607 LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1608 continue;
1609 }
1610
1611 // Look through IntToPtr constants.
1612 if (auto *CE = dyn_cast<ConstantExpr>(V))
1613 if (CE->getOpcode() == Instruction::IntToPtr) {
1614 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1615 continue;
1616 }
1617
1618 // If the Value is a frame index, we can create a FrameIndex debug value
1619 // without relying on the DAG at all.
1620 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1621 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1622 if (SI != FuncInfo.StaticAllocaMap.end()) {
1623 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1624 continue;
1625 }
1626 }
1627
1628 // Do not use getValue() in here; we don't want to generate code at
1629 // this point if it hasn't been done yet.
1630 SDValue N = NodeMap[V];
1631 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1632 N = UnusedArgNodeMap[V];
1633
1634 if (N.getNode()) {
1635 // Only emit func arg dbg value for non-variadic dbg.values for now.
1636 if (!IsVariadic &&
1637 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1638 FuncArgumentDbgValueKind::Value, N))
1639 return true;
1640 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1641 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1642 // describe stack slot locations.
1643 //
1644 // Consider "int x = 0; int *px = &x;". There are two kinds of
1645 // interesting debug values here after optimization:
1646 //
1647 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
1648 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1649 //
1650 // Both describe the direct values of their associated variables.
1651 Dependencies.push_back(N.getNode());
1652 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1653 continue;
1654 }
1655 LocationOps.emplace_back(
1656 SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1657 continue;
1658 }
1659
1660 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1661 // Special rules apply for the first dbg.values of parameter variables in a
1662 // function. Identify them by the fact they reference Argument Values, that
1663 // they're parameters, and they are parameters of the current function. We
1664 // need to let them dangle until they get an SDNode.
1665 bool IsParamOfFunc =
1666 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1667 if (IsParamOfFunc)
1668 return false;
1669
1670 // The value is not used in this block yet (or it would have an SDNode).
1671 // We still want the value to appear for the user if possible -- if it has
1672 // an associated VReg, we can refer to that instead.
1673 auto VMI = FuncInfo.ValueMap.find(V);
1674 if (VMI != FuncInfo.ValueMap.end()) {
1675 Register Reg = VMI->second;
1676 // If this is a PHI node, it may be split up into several MI PHI nodes
1677 // (in FunctionLoweringInfo::set).
1678 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1679 V->getType(), std::nullopt);
1680 if (RFV.occupiesMultipleRegs()) {
1681 // FIXME: We could potentially support variadic dbg_values here.
1682 if (IsVariadic)
1683 return false;
1684 unsigned Offset = 0;
1685 unsigned BitsToDescribe = 0;
1686 if (auto VarSize = Var->getSizeInBits())
1687 BitsToDescribe = *VarSize;
1688 if (auto Fragment = Expr->getFragmentInfo())
1689 BitsToDescribe = Fragment->SizeInBits;
1690 for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1691 // Bail out if all bits are described already.
1692 if (Offset >= BitsToDescribe)
1693 break;
1694 // TODO: handle scalable vectors.
1695 unsigned RegisterSize = RegAndSize.second;
1696 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1697 ? BitsToDescribe - Offset
1698 : RegisterSize;
1699 auto FragmentExpr = DIExpression::createFragmentExpression(
1700 Expr, Offset, FragmentSize);
1701 if (!FragmentExpr)
1702 continue;
1703 SDDbgValue *SDV = DAG.getVRegDbgValue(
1704 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1705 DAG.AddDbgValue(SDV, false);
1706 Offset += RegisterSize;
1707 }
1708 return true;
1709 }
1710 // We can use simple vreg locations for variadic dbg_values as well.
1711 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1712 continue;
1713 }
1714 // We failed to create a SDDbgOperand for V.
1715 return false;
1716 }
1717
1718 // We have created a SDDbgOperand for each Value in Values.
1719 assert(!LocationOps.empty());
1720 SDDbgValue *SDV =
1721 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1722 /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1723 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1724 return true;
1725}
1726
1728 // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1729 for (auto &Pair : DanglingDebugInfoMap)
1730 for (auto &DDI : Pair.second)
1731 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1733}
1734
1735/// getCopyFromRegs - If there was virtual register allocated for the value V
1736/// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1739 SDValue Result;
1740
1741 if (It != FuncInfo.ValueMap.end()) {
1742 Register InReg = It->second;
1743
1744 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1745 DAG.getDataLayout(), InReg, Ty,
1746 std::nullopt); // This is not an ABI copy.
1747 SDValue Chain = DAG.getEntryNode();
1748 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1749 V);
1750 resolveDanglingDebugInfo(V, Result);
1751 }
1752
1753 return Result;
1754}
1755
1756/// getValue - Return an SDValue for the given Value.
1758 // If we already have an SDValue for this value, use it. It's important
1759 // to do this first, so that we don't create a CopyFromReg if we already
1760 // have a regular SDValue.
1761 SDValue &N = NodeMap[V];
1762 if (N.getNode()) return N;
1763
1764 // If there's a virtual register allocated and initialized for this
1765 // value, use it.
1766 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1767 return copyFromReg;
1768
1769 // Otherwise create a new SDValue and remember it.
1770 SDValue Val = getValueImpl(V);
1771 NodeMap[V] = Val;
1773 return Val;
1774}
1775
1776/// getNonRegisterValue - Return an SDValue for the given Value, but
1777/// don't look in FuncInfo.ValueMap for a virtual register.
1779 // If we already have an SDValue for this value, use it.
1780 SDValue &N = NodeMap[V];
1781 if (N.getNode()) {
1782 if (isIntOrFPConstant(N)) {
1783 // Remove the debug location from the node as the node is about to be used
1784 // in a location which may differ from the original debug location. This
1785 // is relevant to Constant and ConstantFP nodes because they can appear
1786 // as constant expressions inside PHI nodes.
1787 N->setDebugLoc(DebugLoc());
1788 }
1789 return N;
1790 }
1791
1792 // Otherwise create a new SDValue and remember it.
1793 SDValue Val = getValueImpl(V);
1794 NodeMap[V] = Val;
1796 return Val;
1797}
1798
1799/// getValueImpl - Helper function for getValue and getNonRegisterValue.
1800/// Create an SDValue for the given value.
1802 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1803
1804 if (const Constant *C = dyn_cast<Constant>(V)) {
1805 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1806
1807 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1808 SDLoc DL = getCurSDLoc();
1809
1810 // DAG.getConstant() may attempt to legalise the vector constant which can
1811 // significantly change the combines applied to the DAG. To reduce the
1812 // divergence when enabling ConstantInt based vectors we try to construct
1813 // the DAG in the same way as shufflevector based splats. TODO: The
1814 // divergence sometimes leads to better optimisations. Ideally we should
1815 // prevent DAG.getConstant() from legalising too early but there are some
1816 // degradations preventing this.
1817 if (VT.isScalableVector())
1818 return DAG.getNode(
1819 ISD::SPLAT_VECTOR, DL, VT,
1820 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1821 if (VT.isFixedLengthVector())
1822 return DAG.getSplatBuildVector(
1823 VT, DL,
1824 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1825 return DAG.getConstant(*CI, DL, VT);
1826 }
1827
1828 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1829 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1830
1831 if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1832 return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1833 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1834 getValue(CPA->getAddrDiscriminator()),
1835 getValue(CPA->getDiscriminator()));
1836 }
1837
1839 return DAG.getConstant(0, getCurSDLoc(), VT);
1840
1841 if (match(C, m_VScale()))
1842 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1843
1844 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1845 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1846
1847 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1848 return isa<PoisonValue>(C) ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
1849
1850 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1851 visit(CE->getOpcode(), *CE);
1852 SDValue N1 = NodeMap[V];
1853 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1854 return N1;
1855 }
1856
1858 SmallVector<SDValue, 4> Constants;
1859 for (const Use &U : C->operands()) {
1860 SDNode *Val = getValue(U).getNode();
1861 // If the operand is an empty aggregate, there are no values.
1862 if (!Val) continue;
1863 // Add each leaf value from the operand to the Constants list
1864 // to form a flattened list of all the values.
1865 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1866 Constants.push_back(SDValue(Val, i));
1867 }
1868
1869 return DAG.getMergeValues(Constants, getCurSDLoc());
1870 }
1871
1872 if (const ConstantDataSequential *CDS =
1875 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1876 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1877 // Add each leaf value from the operand to the Constants list
1878 // to form a flattened list of all the values.
1879 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1880 Ops.push_back(SDValue(Val, i));
1881 }
1882
1883 if (isa<ArrayType>(CDS->getType()))
1884 return DAG.getMergeValues(Ops, getCurSDLoc());
1885 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1886 }
1887
1888 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1890 "Unknown struct or array constant!");
1891
1892 SmallVector<EVT, 4> ValueVTs;
1893 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1894 unsigned NumElts = ValueVTs.size();
1895 if (NumElts == 0)
1896 return SDValue(); // empty struct
1897 SmallVector<SDValue, 4> Constants(NumElts);
1898 for (unsigned i = 0; i != NumElts; ++i) {
1899 EVT EltVT = ValueVTs[i];
1900 if (isa<UndefValue>(C))
1901 Constants[i] = DAG.getUNDEF(EltVT);
1902 else if (EltVT.isFloatingPoint())
1903 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1904 else
1905 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1906 }
1907
1908 return DAG.getMergeValues(Constants, getCurSDLoc());
1909 }
1910
1911 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1912 return DAG.getBlockAddress(BA, VT);
1913
1914 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1915 return getValue(Equiv->getGlobalValue());
1916
1917 if (const auto *NC = dyn_cast<NoCFIValue>(C))
1918 return getValue(NC->getGlobalValue());
1919
1920 if (VT == MVT::aarch64svcount) {
1921 assert(C->isNullValue() && "Can only zero this target type!");
1922 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1923 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1924 }
1925
1926 if (VT.isRISCVVectorTuple()) {
1927 assert(C->isNullValue() && "Can only zero this target type!");
1928 return DAG.getNode(
1929 ISD::BITCAST, getCurSDLoc(), VT,
1930 DAG.getNode(
1932 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1933 VT.getSizeInBits().getKnownMinValue() / 8, true),
1934 DAG.getConstant(0, getCurSDLoc(), MVT::getIntegerVT(8))));
1935 }
1936
1937 VectorType *VecTy = cast<VectorType>(V->getType());
1938
1939 // Now that we know the number and type of the elements, get that number of
1940 // elements into the Ops array based on what kind of constant it is.
1941 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1943 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1944 for (unsigned i = 0; i != NumElements; ++i)
1945 Ops.push_back(getValue(CV->getOperand(i)));
1946
1947 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1948 }
1949
1951 EVT EltVT =
1952 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1953
1954 SDValue Op;
1955 if (EltVT.isFloatingPoint())
1956 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1957 else
1958 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1959
1960 return DAG.getSplat(VT, getCurSDLoc(), Op);
1961 }
1962
1963 llvm_unreachable("Unknown vector constant");
1964 }
1965
1966 // If this is a static alloca, generate it as the frameindex instead of
1967 // computation.
1968 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1970 FuncInfo.StaticAllocaMap.find(AI);
1971 if (SI != FuncInfo.StaticAllocaMap.end())
1972 return DAG.getFrameIndex(
1973 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
1974 }
1975
1976 // If this is an instruction which fast-isel has deferred, select it now.
1977 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
1978 Register InReg = FuncInfo.InitializeRegForValue(Inst);
1979
1980 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
1981 Inst->getType(), std::nullopt);
1982 SDValue Chain = DAG.getEntryNode();
1983 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
1984 }
1985
1986 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
1987 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
1988
1989 if (const auto *BB = dyn_cast<BasicBlock>(V))
1990 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
1991
1992 llvm_unreachable("Can't get register for value!");
1993}
1994
1995void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
1997 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
1998 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
1999 bool IsSEH = isAsynchronousEHPersonality(Pers);
2000 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2001 if (IsSEH) {
2002 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2003 CatchPadMBB->setIsEHContTarget(true);
2005 } else
2006 CatchPadMBB->setIsEHScopeEntry();
2007 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2008 if (IsMSVCCXX || IsCoreCLR)
2009 CatchPadMBB->setIsEHFuncletEntry();
2010}
2011
2012void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2013 // Update machine-CFG edge.
2014 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2015 FuncInfo.MBB->addSuccessor(TargetMBB);
2016
2017 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2018 bool IsSEH = isAsynchronousEHPersonality(Pers);
2019 if (IsSEH) {
2020 // If this is not a fall-through branch or optimizations are switched off,
2021 // emit the branch.
2022 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2023 TM.getOptLevel() == CodeGenOptLevel::None)
2024 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2025 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2026 return;
2027 }
2028
2029 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2030 TargetMBB->setIsEHContTarget(true);
2031 DAG.getMachineFunction().setHasEHContTarget(true);
2032
2033 // Figure out the funclet membership for the catchret's successor.
2034 // This will be used by the FuncletLayout pass to determine how to order the
2035 // BB's.
2036 // A 'catchret' returns to the outer scope's color.
2037 Value *ParentPad = I.getCatchSwitchParentPad();
2038 const BasicBlock *SuccessorColor;
2039 if (isa<ConstantTokenNone>(ParentPad))
2040 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2041 else
2042 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2043 assert(SuccessorColor && "No parent funclet for catchret!");
2044 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2045 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2046
2047 // Create the terminator node.
2048 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2049 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2050 DAG.getBasicBlock(SuccessorColorMBB));
2051 DAG.setRoot(Ret);
2052}
2053
2054void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2055 // Don't emit any special code for the cleanuppad instruction. It just marks
2056 // the start of an EH scope/funclet.
2057 FuncInfo.MBB->setIsEHScopeEntry();
2058 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2059 if (Pers != EHPersonality::Wasm_CXX) {
2060 FuncInfo.MBB->setIsEHFuncletEntry();
2061 FuncInfo.MBB->setIsCleanupFuncletEntry();
2062 }
2063}
2064
2065/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2066/// many places it could ultimately go. In the IR, we have a single unwind
2067/// destination, but in the machine CFG, we enumerate all the possible blocks.
2068/// This function skips over imaginary basic blocks that hold catchswitch
2069/// instructions, and finds all the "real" machine
2070/// basic block destinations. As those destinations may not be successors of
2071/// EHPadBB, here we also calculate the edge probability to those destinations.
2072/// The passed-in Prob is the edge probability to EHPadBB.
2074 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2075 BranchProbability Prob,
2076 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2077 &UnwindDests) {
2078 EHPersonality Personality =
2080 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2081 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2082 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2083 bool IsSEH = isAsynchronousEHPersonality(Personality);
2084
2085 while (EHPadBB) {
2087 BasicBlock *NewEHPadBB = nullptr;
2088 if (isa<LandingPadInst>(Pad)) {
2089 // Stop on landingpads. They are not funclets.
2090 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2091 break;
2092 } else if (isa<CleanupPadInst>(Pad)) {
2093 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2094 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2095 // which always catches an exception.
2096 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2097 UnwindDests.back().first->setIsEHScopeEntry();
2098 // In Wasm, EH scopes are not funclets
2099 if (!IsWasmCXX)
2100 UnwindDests.back().first->setIsEHFuncletEntry();
2101 break;
2102 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2103 // Add the catchpad handlers to the possible destinations.
2104 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2105 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2106 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2107 if (IsMSVCCXX || IsCoreCLR)
2108 UnwindDests.back().first->setIsEHFuncletEntry();
2109 if (!IsSEH)
2110 UnwindDests.back().first->setIsEHScopeEntry();
2111 }
2112 NewEHPadBB = CatchSwitch->getUnwindDest();
2113 } else {
2114 continue;
2115 }
2116
2117 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2118 if (BPI && NewEHPadBB)
2119 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2120 EHPadBB = NewEHPadBB;
2121 }
2122}
2123
2124void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2125 // Update successor info.
2127 auto UnwindDest = I.getUnwindDest();
2128 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2129 BranchProbability UnwindDestProb =
2130 (BPI && UnwindDest)
2131 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2133 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2134 for (auto &UnwindDest : UnwindDests) {
2135 UnwindDest.first->setIsEHPad();
2136 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2137 }
2138 FuncInfo.MBB->normalizeSuccProbs();
2139
2140 // Create the terminator node.
2141 MachineBasicBlock *CleanupPadMBB =
2142 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2143 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
2144 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2145 DAG.setRoot(Ret);
2146}
2147
2148void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2149 report_fatal_error("visitCatchSwitch not yet implemented!");
2150}
2151
2152void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2153 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2154 auto &DL = DAG.getDataLayout();
2155 SDValue Chain = getControlRoot();
2158
2159 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2160 // lower
2161 //
2162 // %val = call <ty> @llvm.experimental.deoptimize()
2163 // ret <ty> %val
2164 //
2165 // differently.
2166 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2168 return;
2169 }
2170
2171 if (!FuncInfo.CanLowerReturn) {
2172 Register DemoteReg = FuncInfo.DemoteRegister;
2173
2174 // Emit a store of the return value through the virtual register.
2175 // Leave Outs empty so that LowerReturn won't try to load return
2176 // registers the usual way.
2177 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2178 SDValue RetPtr =
2179 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2180 SDValue RetOp = getValue(I.getOperand(0));
2181
2182 SmallVector<EVT, 4> ValueVTs, MemVTs;
2183 SmallVector<uint64_t, 4> Offsets;
2184 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2185 &Offsets, 0);
2186 unsigned NumValues = ValueVTs.size();
2187
2188 SmallVector<SDValue, 4> Chains(NumValues);
2189 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2190 for (unsigned i = 0; i != NumValues; ++i) {
2191 // An aggregate return value cannot wrap around the address space, so
2192 // offsets to its parts don't wrap either.
2193 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2194 TypeSize::getFixed(Offsets[i]));
2195
2196 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2197 if (MemVTs[i] != ValueVTs[i])
2198 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2199 Chains[i] = DAG.getStore(
2200 Chain, getCurSDLoc(), Val,
2201 // FIXME: better loc info would be nice.
2202 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2203 commonAlignment(BaseAlign, Offsets[i]));
2204 }
2205
2206 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2207 MVT::Other, Chains);
2208 } else if (I.getNumOperands() != 0) {
2210 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2211 unsigned NumValues = Types.size();
2212 if (NumValues) {
2213 SDValue RetOp = getValue(I.getOperand(0));
2214
2215 const Function *F = I.getParent()->getParent();
2216
2217 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2218 I.getOperand(0)->getType(), F->getCallingConv(),
2219 /*IsVarArg*/ false, DL);
2220
2221 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2222 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2223 ExtendKind = ISD::SIGN_EXTEND;
2224 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2225 ExtendKind = ISD::ZERO_EXTEND;
2226
2227 LLVMContext &Context = F->getContext();
2228 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2229
2230 for (unsigned j = 0; j != NumValues; ++j) {
2231 EVT VT = TLI.getValueType(DL, Types[j]);
2232
2233 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2234 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2235
2236 CallingConv::ID CC = F->getCallingConv();
2237
2238 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2239 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2240 SmallVector<SDValue, 4> Parts(NumParts);
2242 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2243 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2244
2245 // 'inreg' on function refers to return value
2246 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2247 if (RetInReg)
2248 Flags.setInReg();
2249
2250 if (I.getOperand(0)->getType()->isPointerTy()) {
2251 Flags.setPointer();
2252 Flags.setPointerAddrSpace(
2253 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2254 }
2255
2256 if (NeedsRegBlock) {
2257 Flags.setInConsecutiveRegs();
2258 if (j == NumValues - 1)
2259 Flags.setInConsecutiveRegsLast();
2260 }
2261
2262 // Propagate extension type if any
2263 if (ExtendKind == ISD::SIGN_EXTEND)
2264 Flags.setSExt();
2265 else if (ExtendKind == ISD::ZERO_EXTEND)
2266 Flags.setZExt();
2267 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2268 Flags.setNoExt();
2269
2270 for (unsigned i = 0; i < NumParts; ++i) {
2271 Outs.push_back(ISD::OutputArg(Flags,
2272 Parts[i].getValueType().getSimpleVT(),
2273 VT, Types[j], 0, 0));
2274 OutVals.push_back(Parts[i]);
2275 }
2276 }
2277 }
2278 }
2279
2280 // Push in swifterror virtual register as the last element of Outs. This makes
2281 // sure swifterror virtual register will be returned in the swifterror
2282 // physical register.
2283 const Function *F = I.getParent()->getParent();
2284 if (TLI.supportSwiftError() &&
2285 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2286 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2287 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2288 Flags.setSwiftError();
2289 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2290 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2291 PointerType::getUnqual(*DAG.getContext()),
2292 /*origidx=*/1, /*partOffs=*/0));
2293 // Create SDNode for the swifterror virtual register.
2294 OutVals.push_back(
2295 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2296 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2297 EVT(TLI.getPointerTy(DL))));
2298 }
2299
2300 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2301 CallingConv::ID CallConv =
2302 DAG.getMachineFunction().getFunction().getCallingConv();
2303 Chain = DAG.getTargetLoweringInfo().LowerReturn(
2304 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2305
2306 // Verify that the target's LowerReturn behaved as expected.
2307 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2308 "LowerReturn didn't return a valid chain!");
2309
2310 // Update the DAG with the new chain value resulting from return lowering.
2311 DAG.setRoot(Chain);
2312}
2313
2314/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2315/// created for it, emit nodes to copy the value into the virtual
2316/// registers.
2318 // Skip empty types
2319 if (V->getType()->isEmptyTy())
2320 return;
2321
2323 if (VMI != FuncInfo.ValueMap.end()) {
2324 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2325 "Unused value assigned virtual registers!");
2326 CopyValueToVirtualRegister(V, VMI->second);
2327 }
2328}
2329
2330/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2331/// the current basic block, add it to ValueMap now so that we'll get a
2332/// CopyTo/FromReg.
2334 // No need to export constants.
2335 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2336
2337 // Already exported?
2338 if (FuncInfo.isExportedInst(V)) return;
2339
2340 Register Reg = FuncInfo.InitializeRegForValue(V);
2342}
2343
2345 const BasicBlock *FromBB) {
2346 // The operands of the setcc have to be in this block. We don't know
2347 // how to export them from some other block.
2348 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2349 // Can export from current BB.
2350 if (VI->getParent() == FromBB)
2351 return true;
2352
2353 // Is already exported, noop.
2354 return FuncInfo.isExportedInst(V);
2355 }
2356
2357 // If this is an argument, we can export it if the BB is the entry block or
2358 // if it is already exported.
2359 if (isa<Argument>(V)) {
2360 if (FromBB->isEntryBlock())
2361 return true;
2362
2363 // Otherwise, can only export this if it is already exported.
2364 return FuncInfo.isExportedInst(V);
2365 }
2366
2367 // Otherwise, constants can always be exported.
2368 return true;
2369}
2370
2371/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2373SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2374 const MachineBasicBlock *Dst) const {
2376 const BasicBlock *SrcBB = Src->getBasicBlock();
2377 const BasicBlock *DstBB = Dst->getBasicBlock();
2378 if (!BPI) {
2379 // If BPI is not available, set the default probability as 1 / N, where N is
2380 // the number of successors.
2381 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2382 return BranchProbability(1, SuccSize);
2383 }
2384 return BPI->getEdgeProbability(SrcBB, DstBB);
2385}
2386
2387void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2388 MachineBasicBlock *Dst,
2389 BranchProbability Prob) {
2390 if (!FuncInfo.BPI)
2391 Src->addSuccessorWithoutProb(Dst);
2392 else {
2393 if (Prob.isUnknown())
2394 Prob = getEdgeProbability(Src, Dst);
2395 Src->addSuccessor(Dst, Prob);
2396 }
2397}
2398
2399static bool InBlock(const Value *V, const BasicBlock *BB) {
2400 if (const Instruction *I = dyn_cast<Instruction>(V))
2401 return I->getParent() == BB;
2402 return true;
2403}
2404
2405/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2406/// This function emits a branch and is used at the leaves of an OR or an
2407/// AND operator tree.
2408void
2411 MachineBasicBlock *FBB,
2412 MachineBasicBlock *CurBB,
2413 MachineBasicBlock *SwitchBB,
2414 BranchProbability TProb,
2415 BranchProbability FProb,
2416 bool InvertCond) {
2417 const BasicBlock *BB = CurBB->getBasicBlock();
2418
2419 // If the leaf of the tree is a comparison, merge the condition into
2420 // the caseblock.
2421 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2422 // The operands of the cmp have to be in this block. We don't know
2423 // how to export them from some other block. If this is the first block
2424 // of the sequence, no exporting is needed.
2425 if (CurBB == SwitchBB ||
2426 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2427 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2428 ISD::CondCode Condition;
2429 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2430 ICmpInst::Predicate Pred =
2431 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2432 Condition = getICmpCondCode(Pred);
2433 } else {
2434 const FCmpInst *FC = cast<FCmpInst>(Cond);
2435 FCmpInst::Predicate Pred =
2436 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2437 Condition = getFCmpCondCode(Pred);
2438 if (TM.Options.NoNaNsFPMath)
2439 Condition = getFCmpCodeWithoutNaN(Condition);
2440 }
2441
2442 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2443 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2444 SL->SwitchCases.push_back(CB);
2445 return;
2446 }
2447 }
2448
2449 // Create a CaseBlock record representing this branch.
2450 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2451 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2452 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2453 SL->SwitchCases.push_back(CB);
2454}
2455
2456// Collect dependencies on V recursively. This is used for the cost analysis in
2457// `shouldKeepJumpConditionsTogether`.
2461 unsigned Depth = 0) {
2462 // Return false if we have an incomplete count.
2464 return false;
2465
2466 auto *I = dyn_cast<Instruction>(V);
2467 if (I == nullptr)
2468 return true;
2469
2470 if (Necessary != nullptr) {
2471 // This instruction is necessary for the other side of the condition so
2472 // don't count it.
2473 if (Necessary->contains(I))
2474 return true;
2475 }
2476
2477 // Already added this dep.
2478 if (!Deps->try_emplace(I, false).second)
2479 return true;
2480
2481 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2482 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2483 Depth + 1))
2484 return false;
2485 return true;
2486}
2487
2490 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2492 if (I.getNumSuccessors() != 2)
2493 return false;
2494
2495 if (!I.isConditional())
2496 return false;
2497
2498 if (Params.BaseCost < 0)
2499 return false;
2500
2501 // Baseline cost.
2502 InstructionCost CostThresh = Params.BaseCost;
2503
2504 BranchProbabilityInfo *BPI = nullptr;
2505 if (Params.LikelyBias || Params.UnlikelyBias)
2506 BPI = FuncInfo.BPI;
2507 if (BPI != nullptr) {
2508 // See if we are either likely to get an early out or compute both lhs/rhs
2509 // of the condition.
2510 BasicBlock *IfFalse = I.getSuccessor(0);
2511 BasicBlock *IfTrue = I.getSuccessor(1);
2512
2513 std::optional<bool> Likely;
2514 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2515 Likely = true;
2516 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2517 Likely = false;
2518
2519 if (Likely) {
2520 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2521 // Its likely we will have to compute both lhs and rhs of condition
2522 CostThresh += Params.LikelyBias;
2523 else {
2524 if (Params.UnlikelyBias < 0)
2525 return false;
2526 // Its likely we will get an early out.
2527 CostThresh -= Params.UnlikelyBias;
2528 }
2529 }
2530 }
2531
2532 if (CostThresh <= 0)
2533 return false;
2534
2535 // Collect "all" instructions that lhs condition is dependent on.
2536 // Use map for stable iteration (to avoid non-determanism of iteration of
2537 // SmallPtrSet). The `bool` value is just a dummy.
2539 collectInstructionDeps(&LhsDeps, Lhs);
2540 // Collect "all" instructions that rhs condition is dependent on AND are
2541 // dependencies of lhs. This gives us an estimate on which instructions we
2542 // stand to save by splitting the condition.
2543 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2544 return false;
2545 // Add the compare instruction itself unless its a dependency on the LHS.
2546 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2547 if (!LhsDeps.contains(RhsI))
2548 RhsDeps.try_emplace(RhsI, false);
2549
2550 const auto &TLI = DAG.getTargetLoweringInfo();
2551 const auto &TTI =
2552 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
2553
2554 InstructionCost CostOfIncluding = 0;
2555 // See if this instruction will need to computed independently of whether RHS
2556 // is.
2557 Value *BrCond = I.getCondition();
2558 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2559 for (const auto *U : Ins->users()) {
2560 // If user is independent of RHS calculation we don't need to count it.
2561 if (auto *UIns = dyn_cast<Instruction>(U))
2562 if (UIns != BrCond && !RhsDeps.contains(UIns))
2563 return false;
2564 }
2565 return true;
2566 };
2567
2568 // Prune instructions from RHS Deps that are dependencies of unrelated
2569 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2570 // arbitrary and just meant to cap the how much time we spend in the pruning
2571 // loop. Its highly unlikely to come into affect.
2572 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2573 // Stop after a certain point. No incorrectness from including too many
2574 // instructions.
2575 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2576 const Instruction *ToDrop = nullptr;
2577 for (const auto &InsPair : RhsDeps) {
2578 if (!ShouldCountInsn(InsPair.first)) {
2579 ToDrop = InsPair.first;
2580 break;
2581 }
2582 }
2583 if (ToDrop == nullptr)
2584 break;
2585 RhsDeps.erase(ToDrop);
2586 }
2587
2588 for (const auto &InsPair : RhsDeps) {
2589 // Finally accumulate latency that we can only attribute to computing the
2590 // RHS condition. Use latency because we are essentially trying to calculate
2591 // the cost of the dependency chain.
2592 // Possible TODO: We could try to estimate ILP and make this more precise.
2593 CostOfIncluding +=
2594 TTI.getInstructionCost(InsPair.first, TargetTransformInfo::TCK_Latency);
2595
2596 if (CostOfIncluding > CostThresh)
2597 return false;
2598 }
2599 return true;
2600}
2601
2604 MachineBasicBlock *FBB,
2605 MachineBasicBlock *CurBB,
2606 MachineBasicBlock *SwitchBB,
2608 BranchProbability TProb,
2609 BranchProbability FProb,
2610 bool InvertCond) {
2611 // Skip over not part of the tree and remember to invert op and operands at
2612 // next level.
2613 Value *NotCond;
2614 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2615 InBlock(NotCond, CurBB->getBasicBlock())) {
2616 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2617 !InvertCond);
2618 return;
2619 }
2620
2622 const Value *BOpOp0, *BOpOp1;
2623 // Compute the effective opcode for Cond, taking into account whether it needs
2624 // to be inverted, e.g.
2625 // and (not (or A, B)), C
2626 // gets lowered as
2627 // and (and (not A, not B), C)
2629 if (BOp) {
2630 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2631 ? Instruction::And
2632 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2633 ? Instruction::Or
2635 if (InvertCond) {
2636 if (BOpc == Instruction::And)
2637 BOpc = Instruction::Or;
2638 else if (BOpc == Instruction::Or)
2639 BOpc = Instruction::And;
2640 }
2641 }
2642
2643 // If this node is not part of the or/and tree, emit it as a branch.
2644 // Note that all nodes in the tree should have same opcode.
2645 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2646 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2647 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2648 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2649 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2650 TProb, FProb, InvertCond);
2651 return;
2652 }
2653
2654 // Create TmpBB after CurBB.
2655 MachineFunction::iterator BBI(CurBB);
2656 MachineFunction &MF = DAG.getMachineFunction();
2658 CurBB->getParent()->insert(++BBI, TmpBB);
2659
2660 if (Opc == Instruction::Or) {
2661 // Codegen X | Y as:
2662 // BB1:
2663 // jmp_if_X TBB
2664 // jmp TmpBB
2665 // TmpBB:
2666 // jmp_if_Y TBB
2667 // jmp FBB
2668 //
2669
2670 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2671 // The requirement is that
2672 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2673 // = TrueProb for original BB.
2674 // Assuming the original probabilities are A and B, one choice is to set
2675 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2676 // A/(1+B) and 2B/(1+B). This choice assumes that
2677 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2678 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2679 // TmpBB, but the math is more complicated.
2680
2681 auto NewTrueProb = TProb / 2;
2682 auto NewFalseProb = TProb / 2 + FProb;
2683 // Emit the LHS condition.
2684 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2685 NewFalseProb, InvertCond);
2686
2687 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2688 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2690 // Emit the RHS condition into TmpBB.
2691 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2692 Probs[1], InvertCond);
2693 } else {
2694 assert(Opc == Instruction::And && "Unknown merge op!");
2695 // Codegen X & Y as:
2696 // BB1:
2697 // jmp_if_X TmpBB
2698 // jmp FBB
2699 // TmpBB:
2700 // jmp_if_Y TBB
2701 // jmp FBB
2702 //
2703 // This requires creation of TmpBB after CurBB.
2704
2705 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2706 // The requirement is that
2707 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2708 // = FalseProb for original BB.
2709 // Assuming the original probabilities are A and B, one choice is to set
2710 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2711 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2712 // TrueProb for BB1 * FalseProb for TmpBB.
2713
2714 auto NewTrueProb = TProb + FProb / 2;
2715 auto NewFalseProb = FProb / 2;
2716 // Emit the LHS condition.
2717 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2718 NewFalseProb, InvertCond);
2719
2720 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2721 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2723 // Emit the RHS condition into TmpBB.
2724 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2725 Probs[1], InvertCond);
2726 }
2727}
2728
2729/// If the set of cases should be emitted as a series of branches, return true.
2730/// If we should emit this as a bunch of and/or'd together conditions, return
2731/// false.
2732bool
2733SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2734 if (Cases.size() != 2) return true;
2735
2736 // If this is two comparisons of the same values or'd or and'd together, they
2737 // will get folded into a single comparison, so don't emit two blocks.
2738 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2739 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2740 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2741 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2742 return false;
2743 }
2744
2745 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2746 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2747 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2748 Cases[0].CC == Cases[1].CC &&
2749 isa<Constant>(Cases[0].CmpRHS) &&
2750 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2751 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2752 return false;
2753 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2754 return false;
2755 }
2756
2757 return true;
2758}
2759
2760void SelectionDAGBuilder::visitBr(const BranchInst &I) {
2762
2763 // Update machine-CFG edges.
2764 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2765
2766 if (I.isUnconditional()) {
2767 // Update machine-CFG edges.
2768 BrMBB->addSuccessor(Succ0MBB);
2769
2770 // If this is not a fall-through branch or optimizations are switched off,
2771 // emit the branch.
2772 if (Succ0MBB != NextBlock(BrMBB) ||
2774 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2775 getControlRoot(), DAG.getBasicBlock(Succ0MBB));
2776 setValue(&I, Br);
2777 DAG.setRoot(Br);
2778 }
2779
2780 return;
2781 }
2782
2783 // If this condition is one of the special cases we handle, do special stuff
2784 // now.
2785 const Value *CondVal = I.getCondition();
2786 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2787
2788 // If this is a series of conditions that are or'd or and'd together, emit
2789 // this as a sequence of branches instead of setcc's with and/or operations.
2790 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2791 // unpredictable branches, and vector extracts because those jumps are likely
2792 // expensive for any target), this should improve performance.
2793 // For example, instead of something like:
2794 // cmp A, B
2795 // C = seteq
2796 // cmp D, E
2797 // F = setle
2798 // or C, F
2799 // jnz foo
2800 // Emit:
2801 // cmp A, B
2802 // je foo
2803 // cmp D, E
2804 // jle foo
2805 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2806 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2807 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2808 BOp->hasOneUse() && !IsUnpredictable) {
2809 Value *Vec;
2810 const Value *BOp0, *BOp1;
2812 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2813 Opcode = Instruction::And;
2814 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2815 Opcode = Instruction::Or;
2816
2817 if (Opcode &&
2818 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2819 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2821 FuncInfo, I, Opcode, BOp0, BOp1,
2822 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2823 Opcode, BOp0, BOp1))) {
2824 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2825 getEdgeProbability(BrMBB, Succ0MBB),
2826 getEdgeProbability(BrMBB, Succ1MBB),
2827 /*InvertCond=*/false);
2828 // If the compares in later blocks need to use values not currently
2829 // exported from this block, export them now. This block should always
2830 // be the first entry.
2831 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2832
2833 // Allow some cases to be rejected.
2834 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2835 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2836 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2837 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2838 }
2839
2840 // Emit the branch for this block.
2841 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2842 SL->SwitchCases.erase(SL->SwitchCases.begin());
2843 return;
2844 }
2845
2846 // Okay, we decided not to do this, remove any inserted MBB's and clear
2847 // SwitchCases.
2848 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2849 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2850
2851 SL->SwitchCases.clear();
2852 }
2853 }
2854
2855 // Create a CaseBlock record representing this branch.
2856 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2857 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2859 IsUnpredictable);
2860
2861 // Use visitSwitchCase to actually insert the fast branch sequence for this
2862 // cond branch.
2863 visitSwitchCase(CB, BrMBB);
2864}
2865
2866/// visitSwitchCase - Emits the necessary code to represent a single node in
2867/// the binary search tree resulting from lowering a switch instruction.
2869 MachineBasicBlock *SwitchBB) {
2870 SDValue Cond;
2871 SDValue CondLHS = getValue(CB.CmpLHS);
2872 SDLoc dl = CB.DL;
2873
2874 if (CB.CC == ISD::SETTRUE) {
2875 // Branch or fall through to TrueBB.
2876 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2877 SwitchBB->normalizeSuccProbs();
2878 if (CB.TrueBB != NextBlock(SwitchBB)) {
2879 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2880 DAG.getBasicBlock(CB.TrueBB)));
2881 }
2882 return;
2883 }
2884
2885 auto &TLI = DAG.getTargetLoweringInfo();
2886 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2887
2888 // Build the setcc now.
2889 if (!CB.CmpMHS) {
2890 // Fold "(X == true)" to X and "(X == false)" to !X to
2891 // handle common cases produced by branch lowering.
2892 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2893 CB.CC == ISD::SETEQ)
2894 Cond = CondLHS;
2895 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2896 CB.CC == ISD::SETEQ) {
2897 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2898 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2899 } else {
2900 SDValue CondRHS = getValue(CB.CmpRHS);
2901
2902 // If a pointer's DAG type is larger than its memory type then the DAG
2903 // values are zero-extended. This breaks signed comparisons so truncate
2904 // back to the underlying type before doing the compare.
2905 if (CondLHS.getValueType() != MemVT) {
2906 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2907 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2908 }
2909 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2910 }
2911 } else {
2912 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2913
2914 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2915 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2916
2917 SDValue CmpOp = getValue(CB.CmpMHS);
2918 EVT VT = CmpOp.getValueType();
2919
2920 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2921 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2922 ISD::SETLE);
2923 } else {
2924 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2925 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2926 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2927 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2928 }
2929 }
2930
2931 // Update successor info
2932 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2933 // TrueBB and FalseBB are always different unless the incoming IR is
2934 // degenerate. This only happens when running llc on weird IR.
2935 if (CB.TrueBB != CB.FalseBB)
2936 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2937 SwitchBB->normalizeSuccProbs();
2938
2939 // If the lhs block is the next block, invert the condition so that we can
2940 // fall through to the lhs instead of the rhs block.
2941 if (CB.TrueBB == NextBlock(SwitchBB)) {
2942 std::swap(CB.TrueBB, CB.FalseBB);
2943 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2944 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2945 }
2946
2947 SDNodeFlags Flags;
2949 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2950 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2951
2952 setValue(CurInst, BrCond);
2953
2954 // Insert the false branch. Do this even if it's a fall through branch,
2955 // this makes it easier to do DAG optimizations which require inverting
2956 // the branch condition.
2957 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2958 DAG.getBasicBlock(CB.FalseBB));
2959
2960 DAG.setRoot(BrCond);
2961}
2962
2963/// visitJumpTable - Emit JumpTable node in the current MBB
2965 // Emit the code for the jump table
2966 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
2967 assert(JT.Reg && "Should lower JT Header first!");
2968 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
2969 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
2970 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
2971 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
2972 Index.getValue(1), Table, Index);
2973 DAG.setRoot(BrJumpTable);
2974}
2975
2976/// visitJumpTableHeader - This function emits necessary code to produce index
2977/// in the JumpTable from switch case.
2979 JumpTableHeader &JTH,
2980 MachineBasicBlock *SwitchBB) {
2981 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
2982 const SDLoc &dl = *JT.SL;
2983
2984 // Subtract the lowest switch case value from the value being switched on.
2985 SDValue SwitchOp = getValue(JTH.SValue);
2986 EVT VT = SwitchOp.getValueType();
2987 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
2988 DAG.getConstant(JTH.First, dl, VT));
2989
2990 // The SDNode we just created, which holds the value being switched on minus
2991 // the smallest case value, needs to be copied to a virtual register so it
2992 // can be used as an index into the jump table in a subsequent basic block.
2993 // This value may be smaller or larger than the target's pointer type, and
2994 // therefore require extension or truncating.
2995 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2996 SwitchOp =
2997 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
2998
2999 Register JumpTableReg =
3000 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
3001 SDValue CopyTo =
3002 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3003 JT.Reg = JumpTableReg;
3004
3005 if (!JTH.FallthroughUnreachable) {
3006 // Emit the range check for the jump table, and branch to the default block
3007 // for the switch statement if the value being switched on exceeds the
3008 // largest case in the switch.
3009 SDValue CMP = DAG.getSetCC(
3010 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3011 Sub.getValueType()),
3012 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3013
3014 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3015 MVT::Other, CopyTo, CMP,
3016 DAG.getBasicBlock(JT.Default));
3017
3018 // Avoid emitting unnecessary branches to the next block.
3019 if (JT.MBB != NextBlock(SwitchBB))
3020 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3021 DAG.getBasicBlock(JT.MBB));
3022
3023 DAG.setRoot(BrCond);
3024 } else {
3025 // Avoid emitting unnecessary branches to the next block.
3026 if (JT.MBB != NextBlock(SwitchBB))
3027 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3028 DAG.getBasicBlock(JT.MBB)));
3029 else
3030 DAG.setRoot(CopyTo);
3031 }
3032}
3033
3034/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3035/// variable if there exists one.
3037 SDValue &Chain) {
3038 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3039 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3040 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3044 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3045 if (Global) {
3046 MachinePointerInfo MPInfo(Global);
3050 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3051 DAG.setNodeMemRefs(Node, {MemRef});
3052 }
3053 if (PtrTy != PtrMemTy)
3054 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3055 return SDValue(Node, 0);
3056}
3057
3058/// Codegen a new tail for a stack protector check ParentMBB which has had its
3059/// tail spliced into a stack protector check success bb.
3060///
3061/// For a high level explanation of how this fits into the stack protector
3062/// generation see the comment on the declaration of class
3063/// StackProtectorDescriptor.
3065 MachineBasicBlock *ParentBB) {
3066
3067 // First create the loads to the guard/stack slot for the comparison.
3068 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3069 auto &DL = DAG.getDataLayout();
3070 EVT PtrTy = TLI.getFrameIndexTy(DL);
3071 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3072
3073 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3074 int FI = MFI.getStackProtectorIndex();
3075
3076 SDValue Guard;
3077 SDLoc dl = getCurSDLoc();
3078 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3079 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3080 Align Align = DL.getPrefTypeAlign(
3081 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3082
3083 // Generate code to load the content of the guard slot.
3084 SDValue GuardVal = DAG.getLoad(
3085 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3086 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3088
3089 if (TLI.useStackGuardXorFP())
3090 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3091
3092 // If we're using function-based instrumentation, call the guard check
3093 // function
3095 // Get the guard check function from the target and verify it exists since
3096 // we're using function-based instrumentation
3097 const Function *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3098 assert(GuardCheckFn && "Guard check function is null");
3099
3100 // The target provides a guard check function to validate the guard value.
3101 // Generate a call to that function with the content of the guard slot as
3102 // argument.
3103 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3104 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3105
3107 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3108 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3109 Entry.IsInReg = true;
3110 Args.push_back(Entry);
3111
3114 .setChain(DAG.getEntryNode())
3115 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3116 getValue(GuardCheckFn), std::move(Args));
3117
3118 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3119 DAG.setRoot(Result.second);
3120 return;
3121 }
3122
3123 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3124 // Otherwise, emit a volatile load to retrieve the stack guard value.
3125 SDValue Chain = DAG.getEntryNode();
3126 if (TLI.useLoadStackGuardNode(M)) {
3127 Guard = getLoadStackGuard(DAG, dl, Chain);
3128 } else {
3129 const Value *IRGuard = TLI.getSDagStackGuard(M);
3130 SDValue GuardPtr = getValue(IRGuard);
3131
3132 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3133 MachinePointerInfo(IRGuard, 0), Align,
3135 }
3136
3137 // Perform the comparison via a getsetcc.
3138 SDValue Cmp = DAG.getSetCC(
3139 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3140 Guard, GuardVal, ISD::SETNE);
3141
3142 // If the guard/stackslot do not equal, branch to failure MBB.
3143 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3144 MVT::Other, GuardVal.getOperand(0),
3145 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3146 // Otherwise branch to success MBB.
3147 SDValue Br = DAG.getNode(ISD::BR, dl,
3148 MVT::Other, BrCond,
3149 DAG.getBasicBlock(SPD.getSuccessMBB()));
3150
3151 DAG.setRoot(Br);
3152}
3153
3154/// Codegen the failure basic block for a stack protector check.
3155///
3156/// A failure stack protector machine basic block consists simply of a call to
3157/// __stack_chk_fail().
3158///
3159/// For a high level explanation of how this fits into the stack protector
3160/// generation see the comment on the declaration of class
3161/// StackProtectorDescriptor.
3164
3165 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3166 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3167 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3168 SDValue Chain;
3169
3170 // For -Oz builds with a guard check function, we use function-based
3171 // instrumentation. Otherwise, if we have a guard check function, we call it
3172 // in the failure block.
3173 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3174 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3175 // First create the loads to the guard/stack slot for the comparison.
3176 auto &DL = DAG.getDataLayout();
3177 EVT PtrTy = TLI.getFrameIndexTy(DL);
3178 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3179
3180 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3181 int FI = MFI.getStackProtectorIndex();
3182
3183 SDLoc dl = getCurSDLoc();
3184 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3185 Align Align = DL.getPrefTypeAlign(
3186 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3187
3188 // Generate code to load the content of the guard slot.
3189 SDValue GuardVal = DAG.getLoad(
3190 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3191 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3193
3194 if (TLI.useStackGuardXorFP())
3195 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3196
3197 // The target provides a guard check function to validate the guard value.
3198 // Generate a call to that function with the content of the guard slot as
3199 // argument.
3200 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3201 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3202
3204 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3205 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3206 Entry.IsInReg = true;
3207 Args.push_back(Entry);
3208
3211 .setChain(DAG.getEntryNode())
3212 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3213 getValue(GuardCheckFn), std::move(Args));
3214
3215 Chain = TLI.LowerCallTo(CLI).second;
3216 } else {
3218 CallOptions.setDiscardResult(true);
3219 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3220 {}, CallOptions, getCurSDLoc())
3221 .second;
3222 }
3223
3224 // Emit a trap instruction if we are required to do so.
3225 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3226 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3227 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3228
3229 DAG.setRoot(Chain);
3230}
3231
3232/// visitBitTestHeader - This function emits necessary code to produce value
3233/// suitable for "bit tests"
3235 MachineBasicBlock *SwitchBB) {
3236 SDLoc dl = getCurSDLoc();
3237
3238 // Subtract the minimum value.
3239 SDValue SwitchOp = getValue(B.SValue);
3240 EVT VT = SwitchOp.getValueType();
3241 SDValue RangeSub =
3242 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3243
3244 // Determine the type of the test operands.
3245 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3246 bool UsePtrType = false;
3247 if (!TLI.isTypeLegal(VT)) {
3248 UsePtrType = true;
3249 } else {
3250 for (const BitTestCase &Case : B.Cases)
3251 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3252 // Switch table case range are encoded into series of masks.
3253 // Just use pointer type, it's guaranteed to fit.
3254 UsePtrType = true;
3255 break;
3256 }
3257 }
3258 SDValue Sub = RangeSub;
3259 if (UsePtrType) {
3260 VT = TLI.getPointerTy(DAG.getDataLayout());
3261 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3262 }
3263
3264 B.RegVT = VT.getSimpleVT();
3265 B.Reg = FuncInfo.CreateReg(B.RegVT);
3266 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3267
3268 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3269
3270 if (!B.FallthroughUnreachable)
3271 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3272 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3273 SwitchBB->normalizeSuccProbs();
3274
3275 SDValue Root = CopyTo;
3276 if (!B.FallthroughUnreachable) {
3277 // Conditional branch to the default block.
3278 SDValue RangeCmp = DAG.getSetCC(dl,
3279 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3280 RangeSub.getValueType()),
3281 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3282 ISD::SETUGT);
3283
3284 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3285 DAG.getBasicBlock(B.Default));
3286 }
3287
3288 // Avoid emitting unnecessary branches to the next block.
3289 if (MBB != NextBlock(SwitchBB))
3290 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3291
3292 DAG.setRoot(Root);
3293}
3294
3295/// visitBitTestCase - this function produces one "bit test"
3297 MachineBasicBlock *NextMBB,
3298 BranchProbability BranchProbToNext,
3299 Register Reg, BitTestCase &B,
3300 MachineBasicBlock *SwitchBB) {
3301 SDLoc dl = getCurSDLoc();
3302 MVT VT = BB.RegVT;
3303 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3304 SDValue Cmp;
3305 unsigned PopCount = llvm::popcount(B.Mask);
3306 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3307 if (PopCount == 1) {
3308 // Testing for a single bit; just compare the shift count with what it
3309 // would need to be to shift a 1 bit in that position.
3310 Cmp = DAG.getSetCC(
3311 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3312 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3313 ISD::SETEQ);
3314 } else if (PopCount == BB.Range) {
3315 // There is only one zero bit in the range, test for it directly.
3316 Cmp = DAG.getSetCC(
3317 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3318 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3319 } else {
3320 // Make desired shift
3321 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3322 DAG.getConstant(1, dl, VT), ShiftOp);
3323
3324 // Emit bit tests and jumps
3325 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3326 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3327 Cmp = DAG.getSetCC(
3328 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3329 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3330 }
3331
3332 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3333 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3334 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3335 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3336 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3337 // one as they are relative probabilities (and thus work more like weights),
3338 // and hence we need to normalize them to let the sum of them become one.
3339 SwitchBB->normalizeSuccProbs();
3340
3341 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3342 MVT::Other, getControlRoot(),
3343 Cmp, DAG.getBasicBlock(B.TargetBB));
3344
3345 // Avoid emitting unnecessary branches to the next block.
3346 if (NextMBB != NextBlock(SwitchBB))
3347 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3348 DAG.getBasicBlock(NextMBB));
3349
3350 DAG.setRoot(BrAnd);
3351}
3352
3353void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3354 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3355
3356 // Retrieve successors. Look through artificial IR level blocks like
3357 // catchswitch for successors.
3358 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3359 const BasicBlock *EHPadBB = I.getSuccessor(1);
3360 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3361
3362 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3363 // have to do anything here to lower funclet bundles.
3364 failForInvalidBundles(I, "invokes",
3370
3371 const Value *Callee(I.getCalledOperand());
3372 const Function *Fn = dyn_cast<Function>(Callee);
3373 if (isa<InlineAsm>(Callee))
3374 visitInlineAsm(I, EHPadBB);
3375 else if (Fn && Fn->isIntrinsic()) {
3376 switch (Fn->getIntrinsicID()) {
3377 default:
3378 llvm_unreachable("Cannot invoke this intrinsic");
3379 case Intrinsic::donothing:
3380 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3381 case Intrinsic::seh_try_begin:
3382 case Intrinsic::seh_scope_begin:
3383 case Intrinsic::seh_try_end:
3384 case Intrinsic::seh_scope_end:
3385 if (EHPadMBB)
3386 // a block referenced by EH table
3387 // so dtor-funclet not removed by opts
3388 EHPadMBB->setMachineBlockAddressTaken();
3389 break;
3390 case Intrinsic::experimental_patchpoint_void:
3391 case Intrinsic::experimental_patchpoint:
3392 visitPatchpoint(I, EHPadBB);
3393 break;
3394 case Intrinsic::experimental_gc_statepoint:
3396 break;
3397 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3398 // but these intrinsics are special because they can be invoked, so we
3399 // manually lower it to a DAG node here.
3400 case Intrinsic::wasm_throw: {
3402 std::array<SDValue, 4> Ops = {
3403 getControlRoot(), // inchain for the terminator node
3404 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3406 getValue(I.getArgOperand(0)), // tag
3407 getValue(I.getArgOperand(1)) // thrown value
3408 };
3409 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3410 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3411 break;
3412 }
3413 case Intrinsic::wasm_rethrow: {
3414 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3415 std::array<SDValue, 2> Ops = {
3416 getControlRoot(), // inchain for the terminator node
3417 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3418 TLI.getPointerTy(DAG.getDataLayout()))};
3419 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3420 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3421 break;
3422 }
3423 }
3424 } else if (I.hasDeoptState()) {
3425 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3426 // Eventually we will support lowering the @llvm.experimental.deoptimize
3427 // intrinsic, and right now there are no plans to support other intrinsics
3428 // with deopt state.
3429 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3430 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3432 } else {
3433 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3434 }
3435
3436 // If the value of the invoke is used outside of its defining block, make it
3437 // available as a virtual register.
3438 // We already took care of the exported value for the statepoint instruction
3439 // during call to the LowerStatepoint.
3440 if (!isa<GCStatepointInst>(I)) {
3442 }
3443
3445 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3446 BranchProbability EHPadBBProb =
3447 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3449 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3450
3451 // Update successor info.
3452 addSuccessorWithProb(InvokeMBB, Return);
3453 for (auto &UnwindDest : UnwindDests) {
3454 UnwindDest.first->setIsEHPad();
3455 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3456 }
3457 InvokeMBB->normalizeSuccProbs();
3458
3459 // Drop into normal successor.
3460 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3461 DAG.getBasicBlock(Return)));
3462}
3463
3464void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3465 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3466
3467 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3468 // have to do anything here to lower funclet bundles.
3469 failForInvalidBundles(I, "callbrs",
3471
3472 assert(I.isInlineAsm() && "Only know how to handle inlineasm callbr");
3473 visitInlineAsm(I);
3475
3476 // Retrieve successors.
3477 SmallPtrSet<BasicBlock *, 8> Dests;
3478 Dests.insert(I.getDefaultDest());
3479 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3480
3481 // Update successor info.
3482 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3483 for (unsigned i = 0, e = I.getNumIndirectDests(); i < e; ++i) {
3484 BasicBlock *Dest = I.getIndirectDest(i);
3485 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
3486 Target->setIsInlineAsmBrIndirectTarget();
3487 // If we introduce a type of asm goto statement that is permitted to use an
3488 // indirect call instruction to jump to its labels, then we should add a
3489 // call to Target->setMachineBlockAddressTaken() here, to mark the target
3490 // block as requiring a BTI.
3491
3492 Target->setLabelMustBeEmitted();
3493 // Don't add duplicate machine successors.
3494 if (Dests.insert(Dest).second)
3495 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3496 }
3497 CallBrMBB->normalizeSuccProbs();
3498
3499 // Drop into default successor.
3500 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3501 MVT::Other, getControlRoot(),
3502 DAG.getBasicBlock(Return)));
3503}
3504
3505void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3506 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3507}
3508
3509void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3510 assert(FuncInfo.MBB->isEHPad() &&
3511 "Call to landingpad not in landing pad!");
3512
3513 // If there aren't registers to copy the values into (e.g., during SjLj
3514 // exceptions), then don't bother to create these DAG nodes.
3515 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3516 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3517 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3518 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3519 return;
3520
3521 // If landingpad's return type is token type, we don't create DAG nodes
3522 // for its exception pointer and selector value. The extraction of exception
3523 // pointer or selector value from token type landingpads is not currently
3524 // supported.
3525 if (LP.getType()->isTokenTy())
3526 return;
3527
3528 SmallVector<EVT, 2> ValueVTs;
3529 SDLoc dl = getCurSDLoc();
3530 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3531 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3532
3533 // Get the two live-in registers as SDValues. The physregs have already been
3534 // copied into virtual registers.
3535 SDValue Ops[2];
3536 if (FuncInfo.ExceptionPointerVirtReg) {
3537 Ops[0] = DAG.getZExtOrTrunc(
3538 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3539 FuncInfo.ExceptionPointerVirtReg,
3540 TLI.getPointerTy(DAG.getDataLayout())),
3541 dl, ValueVTs[0]);
3542 } else {
3543 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3544 }
3545 Ops[1] = DAG.getZExtOrTrunc(
3546 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3547 FuncInfo.ExceptionSelectorVirtReg,
3548 TLI.getPointerTy(DAG.getDataLayout())),
3549 dl, ValueVTs[1]);
3550
3551 // Merge into one.
3552 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3553 DAG.getVTList(ValueVTs), Ops);
3554 setValue(&LP, Res);
3555}
3556
3559 // Update JTCases.
3560 for (JumpTableBlock &JTB : SL->JTCases)
3561 if (JTB.first.HeaderBB == First)
3562 JTB.first.HeaderBB = Last;
3563
3564 // Update BitTestCases.
3565 for (BitTestBlock &BTB : SL->BitTestCases)
3566 if (BTB.Parent == First)
3567 BTB.Parent = Last;
3568}
3569
3570void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3571 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3572
3573 // Update machine-CFG edges with unique successors.
3575 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3576 BasicBlock *BB = I.getSuccessor(i);
3577 bool Inserted = Done.insert(BB).second;
3578 if (!Inserted)
3579 continue;
3580
3581 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3582 addSuccessorWithProb(IndirectBrMBB, Succ);
3583 }
3584 IndirectBrMBB->normalizeSuccProbs();
3585
3586 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
3587 MVT::Other, getControlRoot(),
3588 getValue(I.getAddress())));
3589}
3590
3591void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3592 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3593 DAG.getTarget().Options.NoTrapAfterNoreturn))
3594 return;
3595
3596 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3597}
3598
3599void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3600 SDNodeFlags Flags;
3601 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3602 Flags.copyFMF(*FPOp);
3603
3604 SDValue Op = getValue(I.getOperand(0));
3605 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3606 Op, Flags);
3607 setValue(&I, UnNodeValue);
3608}
3609
3610void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3611 SDNodeFlags Flags;
3612 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3613 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3614 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3615 }
3616 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3617 Flags.setExact(ExactOp->isExact());
3618 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3619 Flags.setDisjoint(DisjointOp->isDisjoint());
3620 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3621 Flags.copyFMF(*FPOp);
3622
3623 SDValue Op1 = getValue(I.getOperand(0));
3624 SDValue Op2 = getValue(I.getOperand(1));
3625 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3626 Op1, Op2, Flags);
3627 setValue(&I, BinNodeValue);
3628}
3629
3630void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3631 SDValue Op1 = getValue(I.getOperand(0));
3632 SDValue Op2 = getValue(I.getOperand(1));
3633
3634 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3635 Op1.getValueType(), DAG.getDataLayout());
3636
3637 // Coerce the shift amount to the right type if we can. This exposes the
3638 // truncate or zext to optimization early.
3639 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3641 "Unexpected shift type");
3642 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3643 }
3644
3645 bool nuw = false;
3646 bool nsw = false;
3647 bool exact = false;
3648
3649 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3650
3651 if (const OverflowingBinaryOperator *OFBinOp =
3653 nuw = OFBinOp->hasNoUnsignedWrap();
3654 nsw = OFBinOp->hasNoSignedWrap();
3655 }
3656 if (const PossiblyExactOperator *ExactOp =
3658 exact = ExactOp->isExact();
3659 }
3660 SDNodeFlags Flags;
3661 Flags.setExact(exact);
3662 Flags.setNoSignedWrap(nsw);
3663 Flags.setNoUnsignedWrap(nuw);
3664 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3665 Flags);
3666 setValue(&I, Res);
3667}
3668
3669void SelectionDAGBuilder::visitSDiv(const User &I) {
3670 SDValue Op1 = getValue(I.getOperand(0));
3671 SDValue Op2 = getValue(I.getOperand(1));
3672
3673 SDNodeFlags Flags;
3674 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3675 cast<PossiblyExactOperator>(&I)->isExact());
3676 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3677 Op2, Flags));
3678}
3679
3680void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3681 ICmpInst::Predicate predicate = I.getPredicate();
3682 SDValue Op1 = getValue(I.getOperand(0));
3683 SDValue Op2 = getValue(I.getOperand(1));
3684 ISD::CondCode Opcode = getICmpCondCode(predicate);
3685
3686 auto &TLI = DAG.getTargetLoweringInfo();
3687 EVT MemVT =
3688 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3689
3690 // If a pointer's DAG type is larger than its memory type then the DAG values
3691 // are zero-extended. This breaks signed comparisons so truncate back to the
3692 // underlying type before doing the compare.
3693 if (Op1.getValueType() != MemVT) {
3694 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3695 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3696 }
3697
3698 SDNodeFlags Flags;
3699 Flags.setSameSign(I.hasSameSign());
3700 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3701
3702 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3703 I.getType());
3704 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
3705}
3706
3707void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3708 FCmpInst::Predicate predicate = I.getPredicate();
3709 SDValue Op1 = getValue(I.getOperand(0));
3710 SDValue Op2 = getValue(I.getOperand(1));
3711
3712 ISD::CondCode Condition = getFCmpCondCode(predicate);
3713 auto *FPMO = cast<FPMathOperator>(&I);
3714 if (FPMO->hasNoNaNs() || TM.Options.NoNaNsFPMath)
3715 Condition = getFCmpCodeWithoutNaN(Condition);
3716
3717 SDNodeFlags Flags;
3718 Flags.copyFMF(*FPMO);
3719 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3720
3721 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3722 I.getType());
3723 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
3724}
3725
3726// Check if the condition of the select has one use or two users that are both
3727// selects with the same condition.
3728static bool hasOnlySelectUsers(const Value *Cond) {
3729 return llvm::all_of(Cond->users(), [](const Value *V) {
3730 return isa<SelectInst>(V);
3731 });
3732}
3733
3734void SelectionDAGBuilder::visitSelect(const User &I) {
3735 SmallVector<EVT, 4> ValueVTs;
3736 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3737 ValueVTs);
3738 unsigned NumValues = ValueVTs.size();
3739 if (NumValues == 0) return;
3740
3741 SmallVector<SDValue, 4> Values(NumValues);
3742 SDValue Cond = getValue(I.getOperand(0));
3743 SDValue LHSVal = getValue(I.getOperand(1));
3744 SDValue RHSVal = getValue(I.getOperand(2));
3745 SmallVector<SDValue, 1> BaseOps(1, Cond);
3747 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3748
3749 bool IsUnaryAbs = false;
3750 bool Negate = false;
3751
3752 SDNodeFlags Flags;
3753 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3754 Flags.copyFMF(*FPOp);
3755
3756 Flags.setUnpredictable(
3757 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3758
3759 // Min/max matching is only viable if all output VTs are the same.
3760 if (all_equal(ValueVTs)) {
3761 EVT VT = ValueVTs[0];
3762 LLVMContext &Ctx = *DAG.getContext();
3763 auto &TLI = DAG.getTargetLoweringInfo();
3764
3765 // We care about the legality of the operation after it has been type
3766 // legalized.
3767 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3768 VT = TLI.getTypeToTransformTo(Ctx, VT);
3769
3770 // If the vselect is legal, assume we want to leave this as a vector setcc +
3771 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3772 // min/max is legal on the scalar type.
3773 bool UseScalarMinMax = VT.isVector() &&
3775
3776 // ValueTracking's select pattern matching does not account for -0.0,
3777 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3778 // -0.0 is less than +0.0.
3779 const Value *LHS, *RHS;
3780 auto SPR = matchSelectPattern(&I, LHS, RHS);
3782 switch (SPR.Flavor) {
3783 case SPF_UMAX: Opc = ISD::UMAX; break;
3784 case SPF_UMIN: Opc = ISD::UMIN; break;
3785 case SPF_SMAX: Opc = ISD::SMAX; break;
3786 case SPF_SMIN: Opc = ISD::SMIN; break;
3787 case SPF_FMINNUM:
3788 switch (SPR.NaNBehavior) {
3789 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3790 case SPNB_RETURNS_NAN: break;
3791 case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break;
3792 case SPNB_RETURNS_ANY:
3793 if (TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT) ||
3794 (UseScalarMinMax &&
3795 TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT.getScalarType())))
3796 Opc = ISD::FMINNUM;
3797 break;
3798 }
3799 break;
3800 case SPF_FMAXNUM:
3801 switch (SPR.NaNBehavior) {
3802 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3803 case SPNB_RETURNS_NAN: break;
3804 case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break;
3805 case SPNB_RETURNS_ANY:
3806 if (TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT) ||
3807 (UseScalarMinMax &&
3808 TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT.getScalarType())))
3809 Opc = ISD::FMAXNUM;
3810 break;
3811 }
3812 break;
3813 case SPF_NABS:
3814 Negate = true;
3815 [[fallthrough]];
3816 case SPF_ABS:
3817 IsUnaryAbs = true;
3818 Opc = ISD::ABS;
3819 break;
3820 default: break;
3821 }
3822
3823 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3824 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3825 (UseScalarMinMax &&
3827 // If the underlying comparison instruction is used by any other
3828 // instruction, the consumed instructions won't be destroyed, so it is
3829 // not profitable to convert to a min/max.
3831 OpCode = Opc;
3832 LHSVal = getValue(LHS);
3833 RHSVal = getValue(RHS);
3834 BaseOps.clear();
3835 }
3836
3837 if (IsUnaryAbs) {
3838 OpCode = Opc;
3839 LHSVal = getValue(LHS);
3840 BaseOps.clear();
3841 }
3842 }
3843
3844 if (IsUnaryAbs) {
3845 for (unsigned i = 0; i != NumValues; ++i) {
3846 SDLoc dl = getCurSDLoc();
3847 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3848 Values[i] =
3849 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3850 if (Negate)
3851 Values[i] = DAG.getNegative(Values[i], dl, VT);
3852 }
3853 } else {
3854 for (unsigned i = 0; i != NumValues; ++i) {
3855 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3856 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3857 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3858 Values[i] = DAG.getNode(
3859 OpCode, getCurSDLoc(),
3860 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3861 }
3862 }
3863
3865 DAG.getVTList(ValueVTs), Values));
3866}
3867
3868void SelectionDAGBuilder::visitTrunc(const User &I) {
3869 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3870 SDValue N = getValue(I.getOperand(0));
3871 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3872 I.getType());
3873 SDNodeFlags Flags;
3874 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3875 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
3876 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
3877 }
3878
3879 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
3880}
3881
3882void SelectionDAGBuilder::visitZExt(const User &I) {
3883 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3884 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3885 SDValue N = getValue(I.getOperand(0));
3886 auto &TLI = DAG.getTargetLoweringInfo();
3887 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3888
3889 SDNodeFlags Flags;
3890 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3891 Flags.setNonNeg(PNI->hasNonNeg());
3892
3893 // Eagerly use nonneg information to canonicalize towards sign_extend if
3894 // that is the target's preference.
3895 // TODO: Let the target do this later.
3896 if (Flags.hasNonNeg() &&
3897 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3898 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3899 return;
3900 }
3901
3902 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3903}
3904
3905void SelectionDAGBuilder::visitSExt(const User &I) {
3906 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3907 // SExt also can't be a cast to bool for same reason. So, nothing much to do
3908 SDValue N = getValue(I.getOperand(0));
3909 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3910 I.getType());
3911 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3912}
3913
3914void SelectionDAGBuilder::visitFPTrunc(const User &I) {
3915 // FPTrunc is never a no-op cast, no need to check
3916 SDValue N = getValue(I.getOperand(0));
3917 SDLoc dl = getCurSDLoc();
3918 SDNodeFlags Flags;
3919 if (auto *TruncInst = dyn_cast<FPMathOperator>(&I))
3920 Flags.copyFMF(*TruncInst);
3921 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3922 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3923 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
3924 DAG.getTargetConstant(
3925 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
3926 Flags));
3927}
3928
3929void SelectionDAGBuilder::visitFPExt(const User &I) {
3930 // FPExt is never a no-op cast, no need to check
3931 SDValue N = getValue(I.getOperand(0));
3932 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3933 I.getType());
3934 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
3935}
3936
3937void SelectionDAGBuilder::visitFPToUI(const User &I) {
3938 // FPToUI is never a no-op cast, no need to check
3939 SDValue N = getValue(I.getOperand(0));
3940 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3941 I.getType());
3942 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
3943}
3944
3945void SelectionDAGBuilder::visitFPToSI(const User &I) {
3946 // FPToSI is never a no-op cast, no need to check
3947 SDValue N = getValue(I.getOperand(0));
3948 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3949 I.getType());
3950 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
3951}
3952
3953void SelectionDAGBuilder::visitUIToFP(const User &I) {
3954 // UIToFP is never a no-op cast, no need to check
3955 SDValue N = getValue(I.getOperand(0));
3956 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3957 I.getType());
3958 SDNodeFlags Flags;
3959 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3960 Flags.setNonNeg(PNI->hasNonNeg());
3961
3962 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
3963}
3964
3965void SelectionDAGBuilder::visitSIToFP(const User &I) {
3966 // SIToFP is never a no-op cast, no need to check
3967 SDValue N = getValue(I.getOperand(0));
3968 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3969 I.getType());
3970 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
3971}
3972
3973void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
3974 SDValue N = getValue(I.getOperand(0));
3975 // By definition the type of the ptrtoaddr must be equal to the address type.
3976 const auto &TLI = DAG.getTargetLoweringInfo();
3977 EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3978 // The address width must be smaller or equal to the pointer representation
3979 // width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
3980 N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
3981 setValue(&I, N);
3982}
3983
3984void SelectionDAGBuilder::visitPtrToInt(const User &I) {
3985 // What to do depends on the size of the integer and the size of the pointer.
3986 // We can either truncate, zero extend, or no-op, accordingly.
3987 SDValue N = getValue(I.getOperand(0));
3988 auto &TLI = DAG.getTargetLoweringInfo();
3989 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3990 I.getType());
3991 EVT PtrMemVT =
3992 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3993 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
3994 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
3995 setValue(&I, N);
3996}
3997
3998void SelectionDAGBuilder::visitIntToPtr(const User &I) {
3999 // What to do depends on the size of the integer and the size of the pointer.
4000 // We can either truncate, zero extend, or no-op, accordingly.
4001 SDValue N = getValue(I.getOperand(0));
4002 auto &TLI = DAG.getTargetLoweringInfo();
4003 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4004 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4005 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4006 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4007 setValue(&I, N);
4008}
4009
4010void SelectionDAGBuilder::visitBitCast(const User &I) {
4011 SDValue N = getValue(I.getOperand(0));
4012 SDLoc dl = getCurSDLoc();
4013 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4014 I.getType());
4015
4016 // BitCast assures us that source and destination are the same size so this is
4017 // either a BITCAST or a no-op.
4018 if (DestVT != N.getValueType())
4019 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
4020 DestVT, N)); // convert types.
4021 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4022 // might fold any kind of constant expression to an integer constant and that
4023 // is not what we are looking for. Only recognize a bitcast of a genuine
4024 // constant integer as an opaque constant.
4025 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4026 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4027 /*isOpaque*/true));
4028 else
4029 setValue(&I, N); // noop cast.
4030}
4031
4032void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4033 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4034 const Value *SV = I.getOperand(0);
4035 SDValue N = getValue(SV);
4036 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4037
4038 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4039 unsigned DestAS = I.getType()->getPointerAddressSpace();
4040
4041 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4042 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4043
4044 setValue(&I, N);
4045}
4046
4047void SelectionDAGBuilder::visitInsertElement(const User &I) {
4048 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4049 SDValue InVec = getValue(I.getOperand(0));
4050 SDValue InVal = getValue(I.getOperand(1));
4051 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4052 TLI.getVectorIdxTy(DAG.getDataLayout()));
4054 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4055 InVec, InVal, InIdx));
4056}
4057
4058void SelectionDAGBuilder::visitExtractElement(const User &I) {
4059 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4060 SDValue InVec = getValue(I.getOperand(0));
4061 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4062 TLI.getVectorIdxTy(DAG.getDataLayout()));
4064 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4065 InVec, InIdx));
4066}
4067
4068void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4069 SDValue Src1 = getValue(I.getOperand(0));
4070 SDValue Src2 = getValue(I.getOperand(1));
4071 ArrayRef<int> Mask;
4072 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4073 Mask = SVI->getShuffleMask();
4074 else
4075 Mask = cast<ConstantExpr>(I).getShuffleMask();
4076 SDLoc DL = getCurSDLoc();
4077 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4078 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4079 EVT SrcVT = Src1.getValueType();
4080
4081 if (all_of(Mask, [](int Elem) { return Elem == 0; }) &&
4082 VT.isScalableVector()) {
4083 // Canonical splat form of first element of first input vector.
4084 SDValue FirstElt =
4085 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4086 DAG.getVectorIdxConstant(0, DL));
4087 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4088 return;
4089 }
4090
4091 // For now, we only handle splats for scalable vectors.
4092 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4093 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4094 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4095
4096 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4097 unsigned MaskNumElts = Mask.size();
4098
4099 if (SrcNumElts == MaskNumElts) {
4100 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4101 return;
4102 }
4103
4104 // Normalize the shuffle vector since mask and vector length don't match.
4105 if (SrcNumElts < MaskNumElts) {
4106 // Mask is longer than the source vectors. We can use concatenate vector to
4107 // make the mask and vectors lengths match.
4108
4109 if (MaskNumElts % SrcNumElts == 0) {
4110 // Mask length is a multiple of the source vector length.
4111 // Check if the shuffle is some kind of concatenation of the input
4112 // vectors.
4113 unsigned NumConcat = MaskNumElts / SrcNumElts;
4114 bool IsConcat = true;
4115 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4116 for (unsigned i = 0; i != MaskNumElts; ++i) {
4117 int Idx = Mask[i];
4118 if (Idx < 0)
4119 continue;
4120 // Ensure the indices in each SrcVT sized piece are sequential and that
4121 // the same source is used for the whole piece.
4122 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4123 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4124 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4125 IsConcat = false;
4126 break;
4127 }
4128 // Remember which source this index came from.
4129 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4130 }
4131
4132 // The shuffle is concatenating multiple vectors together. Just emit
4133 // a CONCAT_VECTORS operation.
4134 if (IsConcat) {
4135 SmallVector<SDValue, 8> ConcatOps;
4136 for (auto Src : ConcatSrcs) {
4137 if (Src < 0)
4138 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4139 else if (Src == 0)
4140 ConcatOps.push_back(Src1);
4141 else
4142 ConcatOps.push_back(Src2);
4143 }
4144 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4145 return;
4146 }
4147 }
4148
4149 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4150 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4151 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4152 PaddedMaskNumElts);
4153
4154 // Pad both vectors with undefs to make them the same length as the mask.
4155 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4156
4157 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4158 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4159 MOps1[0] = Src1;
4160 MOps2[0] = Src2;
4161
4162 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4163 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4164
4165 // Readjust mask for new input vector length.
4166 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4167 for (unsigned i = 0; i != MaskNumElts; ++i) {
4168 int Idx = Mask[i];
4169 if (Idx >= (int)SrcNumElts)
4170 Idx -= SrcNumElts - PaddedMaskNumElts;
4171 MappedOps[i] = Idx;
4172 }
4173
4174 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4175
4176 // If the concatenated vector was padded, extract a subvector with the
4177 // correct number of elements.
4178 if (MaskNumElts != PaddedMaskNumElts)
4179 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4180 DAG.getVectorIdxConstant(0, DL));
4181
4182 setValue(&I, Result);
4183 return;
4184 }
4185
4186 assert(SrcNumElts > MaskNumElts);
4187
4188 // Analyze the access pattern of the vector to see if we can extract
4189 // two subvectors and do the shuffle.
4190 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4191 bool CanExtract = true;
4192 for (int Idx : Mask) {
4193 unsigned Input = 0;
4194 if (Idx < 0)
4195 continue;
4196
4197 if (Idx >= (int)SrcNumElts) {
4198 Input = 1;
4199 Idx -= SrcNumElts;
4200 }
4201
4202 // If all the indices come from the same MaskNumElts sized portion of
4203 // the sources we can use extract. Also make sure the extract wouldn't
4204 // extract past the end of the source.
4205 int NewStartIdx = alignDown(Idx, MaskNumElts);
4206 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4207 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4208 CanExtract = false;
4209 // Make sure we always update StartIdx as we use it to track if all
4210 // elements are undef.
4211 StartIdx[Input] = NewStartIdx;
4212 }
4213
4214 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4215 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4216 return;
4217 }
4218 if (CanExtract) {
4219 // Extract appropriate subvector and generate a vector shuffle
4220 for (unsigned Input = 0; Input < 2; ++Input) {
4221 SDValue &Src = Input == 0 ? Src1 : Src2;
4222 if (StartIdx[Input] < 0)
4223 Src = DAG.getUNDEF(VT);
4224 else {
4225 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4226 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4227 }
4228 }
4229
4230 // Calculate new mask.
4231 SmallVector<int, 8> MappedOps(Mask);
4232 for (int &Idx : MappedOps) {
4233 if (Idx >= (int)SrcNumElts)
4234 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4235 else if (Idx >= 0)
4236 Idx -= StartIdx[0];
4237 }
4238
4239 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4240 return;
4241 }
4242
4243 // We can't use either concat vectors or extract subvectors so fall back to
4244 // replacing the shuffle with extract and build vector.
4245 // to insert and build vector.
4246 EVT EltVT = VT.getVectorElementType();
4248 for (int Idx : Mask) {
4249 SDValue Res;
4250
4251 if (Idx < 0) {
4252 Res = DAG.getUNDEF(EltVT);
4253 } else {
4254 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4255 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4256
4257 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4258 DAG.getVectorIdxConstant(Idx, DL));
4259 }
4260
4261 Ops.push_back(Res);
4262 }
4263
4264 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4265}
4266
4267void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4268 ArrayRef<unsigned> Indices = I.getIndices();
4269 const Value *Op0 = I.getOperand(0);
4270 const Value *Op1 = I.getOperand(1);
4271 Type *AggTy = I.getType();
4272 Type *ValTy = Op1->getType();
4273 bool IntoUndef = isa<UndefValue>(Op0);
4274 bool FromUndef = isa<UndefValue>(Op1);
4275
4276 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4277
4278 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4279 SmallVector<EVT, 4> AggValueVTs;
4280 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4281 SmallVector<EVT, 4> ValValueVTs;
4282 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4283
4284 unsigned NumAggValues = AggValueVTs.size();
4285 unsigned NumValValues = ValValueVTs.size();
4286 SmallVector<SDValue, 4> Values(NumAggValues);
4287
4288 // Ignore an insertvalue that produces an empty object
4289 if (!NumAggValues) {
4290 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4291 return;
4292 }
4293
4294 SDValue Agg = getValue(Op0);
4295 unsigned i = 0;
4296 // Copy the beginning value(s) from the original aggregate.
4297 for (; i != LinearIndex; ++i)
4298 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4299 SDValue(Agg.getNode(), Agg.getResNo() + i);
4300 // Copy values from the inserted value(s).
4301 if (NumValValues) {
4302 SDValue Val = getValue(Op1);
4303 for (; i != LinearIndex + NumValValues; ++i)
4304 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4305 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4306 }
4307 // Copy remaining value(s) from the original aggregate.
4308 for (; i != NumAggValues; ++i)
4309 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4310 SDValue(Agg.getNode(), Agg.getResNo() + i);
4311
4313 DAG.getVTList(AggValueVTs), Values));
4314}
4315
4316void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4317 ArrayRef<unsigned> Indices = I.getIndices();
4318 const Value *Op0 = I.getOperand(0);
4319 Type *AggTy = Op0->getType();
4320 Type *ValTy = I.getType();
4321 bool OutOfUndef = isa<UndefValue>(Op0);
4322
4323 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4324
4325 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4326 SmallVector<EVT, 4> ValValueVTs;
4327 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4328
4329 unsigned NumValValues = ValValueVTs.size();
4330
4331 // Ignore a extractvalue that produces an empty object
4332 if (!NumValValues) {
4333 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4334 return;
4335 }
4336
4337 SmallVector<SDValue, 4> Values(NumValValues);
4338
4339 SDValue Agg = getValue(Op0);
4340 // Copy out the selected value(s).
4341 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4342 Values[i - LinearIndex] =
4343 OutOfUndef ?
4344 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4345 SDValue(Agg.getNode(), Agg.getResNo() + i);
4346
4348 DAG.getVTList(ValValueVTs), Values));
4349}
4350
4351void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4352 Value *Op0 = I.getOperand(0);
4353 // Note that the pointer operand may be a vector of pointers. Take the scalar
4354 // element which holds a pointer.
4355 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4356 SDValue N = getValue(Op0);
4357 SDLoc dl = getCurSDLoc();
4358 auto &TLI = DAG.getTargetLoweringInfo();
4359 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4360
4361 // For a vector GEP, keep the prefix scalar as long as possible, then
4362 // convert any scalars encountered after the first vector operand to vectors.
4363 bool IsVectorGEP = I.getType()->isVectorTy();
4364 ElementCount VectorElementCount =
4365 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4367
4369 GTI != E; ++GTI) {
4370 const Value *Idx = GTI.getOperand();
4371 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4372 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4373 if (Field) {
4374 // N = N + Offset
4375 uint64_t Offset =
4376 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4377
4378 // In an inbounds GEP with an offset that is nonnegative even when
4379 // interpreted as signed, assume there is no unsigned overflow.
4380 SDNodeFlags Flags;
4381 if (NW.hasNoUnsignedWrap() ||
4382 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4384
4385 N = DAG.getMemBasePlusOffset(
4386 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4387 }
4388 } else {
4389 // IdxSize is the width of the arithmetic according to IR semantics.
4390 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4391 // (and fix up the result later).
4392 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4393 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4394 TypeSize ElementSize =
4395 GTI.getSequentialElementStride(DAG.getDataLayout());
4396 // We intentionally mask away the high bits here; ElementSize may not
4397 // fit in IdxTy.
4398 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4399 /*isSigned=*/false, /*implicitTrunc=*/true);
4400 bool ElementScalable = ElementSize.isScalable();
4401
4402 // If this is a scalar constant or a splat vector of constants,
4403 // handle it quickly.
4404 const auto *C = dyn_cast<Constant>(Idx);
4405 if (C && isa<VectorType>(C->getType()))
4406 C = C->getSplatValue();
4407
4408 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4409 if (CI && CI->isZero())
4410 continue;
4411 if (CI && !ElementScalable) {
4412 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4413 LLVMContext &Context = *DAG.getContext();
4414 SDValue OffsVal;
4415 if (N.getValueType().isVector())
4416 OffsVal = DAG.getConstant(
4417 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4418 else
4419 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4420
4421 // In an inbounds GEP with an offset that is nonnegative even when
4422 // interpreted as signed, assume there is no unsigned overflow.
4423 SDNodeFlags Flags;
4424 if (NW.hasNoUnsignedWrap() ||
4425 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4426 Flags.setNoUnsignedWrap(true);
4427
4428 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4429
4430 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4431 continue;
4432 }
4433
4434 // N = N + Idx * ElementMul;
4435 SDValue IdxN = getValue(Idx);
4436
4437 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4438 if (N.getValueType().isVector()) {
4439 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4440 VectorElementCount);
4441 IdxN = DAG.getSplat(VT, dl, IdxN);
4442 } else {
4443 EVT VT =
4444 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4445 N = DAG.getSplat(VT, dl, N);
4446 }
4447 }
4448
4449 // If the index is smaller or larger than intptr_t, truncate or extend
4450 // it.
4451 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4452
4453 SDNodeFlags ScaleFlags;
4454 // The multiplication of an index by the type size does not wrap the
4455 // pointer index type in a signed sense (mul nsw).
4457
4458 // The multiplication of an index by the type size does not wrap the
4459 // pointer index type in an unsigned sense (mul nuw).
4460 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4461
4462 if (ElementScalable) {
4463 EVT VScaleTy = N.getValueType().getScalarType();
4464 SDValue VScale = DAG.getNode(
4465 ISD::VSCALE, dl, VScaleTy,
4466 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4467 if (N.getValueType().isVector())
4468 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4469 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4470 ScaleFlags);
4471 } else {
4472 // If this is a multiply by a power of two, turn it into a shl
4473 // immediately. This is a very common case.
4474 if (ElementMul != 1) {
4475 if (ElementMul.isPowerOf2()) {
4476 unsigned Amt = ElementMul.logBase2();
4477 IdxN = DAG.getNode(
4478 ISD::SHL, dl, N.getValueType(), IdxN,
4479 DAG.getShiftAmountConstant(Amt, N.getValueType(), dl),
4480 ScaleFlags);
4481 } else {
4482 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4483 IdxN.getValueType());
4484 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4485 ScaleFlags);
4486 }
4487 }
4488 }
4489
4490 // The successive addition of the current address, truncated to the
4491 // pointer index type and interpreted as an unsigned number, and each
4492 // offset, also interpreted as an unsigned number, does not wrap the
4493 // pointer index type (add nuw).
4494 SDNodeFlags AddFlags;
4495 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4496
4497 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4498 }
4499 }
4500
4501 if (IsVectorGEP && !N.getValueType().isVector()) {
4502 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4503 N = DAG.getSplat(VT, dl, N);
4504 }
4505
4506 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4507 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4508 if (IsVectorGEP) {
4509 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4510 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4511 }
4512
4513 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4514 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4515
4516 setValue(&I, N);
4517}
4518
4519void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4520 // If this is a fixed sized alloca in the entry block of the function,
4521 // allocate it statically on the stack.
4522 if (FuncInfo.StaticAllocaMap.count(&I))
4523 return; // getValue will auto-populate this.
4524
4525 SDLoc dl = getCurSDLoc();
4526 Type *Ty = I.getAllocatedType();
4527 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4528 auto &DL = DAG.getDataLayout();
4529 TypeSize TySize = DL.getTypeAllocSize(Ty);
4530 MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign());
4531
4532 SDValue AllocSize = getValue(I.getArraySize());
4533
4534 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4535 if (AllocSize.getValueType() != IntPtr)
4536 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4537
4538 if (TySize.isScalable())
4539 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4540 DAG.getVScale(dl, IntPtr,
4541 APInt(IntPtr.getScalarSizeInBits(),
4542 TySize.getKnownMinValue())));
4543 else {
4544 SDValue TySizeValue =
4545 DAG.getConstant(TySize.getFixedValue(), dl, MVT::getIntegerVT(64));
4546 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4547 DAG.getZExtOrTrunc(TySizeValue, dl, IntPtr));
4548 }
4549
4550 // Handle alignment. If the requested alignment is less than or equal to
4551 // the stack alignment, ignore it. If the size is greater than or equal to
4552 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4553 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4554 if (*Alignment <= StackAlign)
4555 Alignment = std::nullopt;
4556
4557 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4558 // Round the size of the allocation up to the stack alignment size
4559 // by add SA-1 to the size. This doesn't overflow because we're computing
4560 // an address inside an alloca.
4561 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4562 DAG.getConstant(StackAlignMask, dl, IntPtr),
4564
4565 // Mask out the low bits for alignment purposes.
4566 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4567 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4568
4569 SDValue Ops[] = {
4570 getRoot(), AllocSize,
4571 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4572 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4573 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4574 setValue(&I, DSA);
4575 DAG.setRoot(DSA.getValue(1));
4576
4577 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4578}
4579
4580static const MDNode *getRangeMetadata(const Instruction &I) {
4581 return I.getMetadata(LLVMContext::MD_range);
4582}
4583
4584static std::optional<ConstantRange> getRange(const Instruction &I) {
4585 if (const auto *CB = dyn_cast<CallBase>(&I))
4586 if (std::optional<ConstantRange> CR = CB->getRange())
4587 return CR;
4588 if (const MDNode *Range = getRangeMetadata(I))
4590 return std::nullopt;
4591}
4592
4593void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4594 if (I.isAtomic())
4595 return visitAtomicLoad(I);
4596
4597 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4598 const Value *SV = I.getOperand(0);
4599 if (TLI.supportSwiftError()) {
4600 // Swifterror values can come from either a function parameter with
4601 // swifterror attribute or an alloca with swifterror attribute.
4602 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4603 if (Arg->hasSwiftErrorAttr())
4604 return visitLoadFromSwiftError(I);
4605 }
4606
4607 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4608 if (Alloca->isSwiftError())
4609 return visitLoadFromSwiftError(I);
4610 }
4611 }
4612
4613 SDValue Ptr = getValue(SV);
4614
4615 Type *Ty = I.getType();
4616 SmallVector<EVT, 4> ValueVTs, MemVTs;
4618 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4619 unsigned NumValues = ValueVTs.size();
4620 if (NumValues == 0)
4621 return;
4622
4623 Align Alignment = I.getAlign();
4624 AAMDNodes AAInfo = I.getAAMetadata();
4625 const MDNode *Ranges = getRangeMetadata(I);
4626 bool isVolatile = I.isVolatile();
4627 MachineMemOperand::Flags MMOFlags =
4628 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4629
4630 SDValue Root;
4631 bool ConstantMemory = false;
4632 if (isVolatile)
4633 // Serialize volatile loads with other side effects.
4634 Root = getRoot();
4635 else if (NumValues > MaxParallelChains)
4636 Root = getMemoryRoot();
4637 else if (BatchAA &&
4638 BatchAA->pointsToConstantMemory(MemoryLocation(
4639 SV,
4640 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4641 AAInfo))) {
4642 // Do not serialize (non-volatile) loads of constant memory with anything.
4643 Root = DAG.getEntryNode();
4644 ConstantMemory = true;
4646 } else {
4647 // Do not serialize non-volatile loads against each other.
4648 Root = DAG.getRoot();
4649 }
4650
4651 SDLoc dl = getCurSDLoc();
4652
4653 if (isVolatile)
4654 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4655
4656 SmallVector<SDValue, 4> Values(NumValues);
4657 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4658
4659 unsigned ChainI = 0;
4660 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4661 // Serializing loads here may result in excessive register pressure, and
4662 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4663 // could recover a bit by hoisting nodes upward in the chain by recognizing
4664 // they are side-effect free or do not alias. The optimizer should really
4665 // avoid this case by converting large object/array copies to llvm.memcpy
4666 // (MaxParallelChains should always remain as failsafe).
4667 if (ChainI == MaxParallelChains) {
4668 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4669 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4670 ArrayRef(Chains.data(), ChainI));
4671 Root = Chain;
4672 ChainI = 0;
4673 }
4674
4675 // TODO: MachinePointerInfo only supports a fixed length offset.
4676 MachinePointerInfo PtrInfo =
4677 !Offsets[i].isScalable() || Offsets[i].isZero()
4678 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4679 : MachinePointerInfo();
4680
4681 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4682 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4683 MMOFlags, AAInfo, Ranges);
4684 Chains[ChainI] = L.getValue(1);
4685
4686 if (MemVTs[i] != ValueVTs[i])
4687 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4688
4689 Values[i] = L;
4690 }
4691
4692 if (!ConstantMemory) {
4693 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4694 ArrayRef(Chains.data(), ChainI));
4695 if (isVolatile)
4696 DAG.setRoot(Chain);
4697 else
4698 PendingLoads.push_back(Chain);
4699 }
4700
4701 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4702 DAG.getVTList(ValueVTs), Values));
4703}
4704
4705void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4706 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4707 "call visitStoreToSwiftError when backend supports swifterror");
4708
4709 SmallVector<EVT, 4> ValueVTs;
4710 SmallVector<uint64_t, 4> Offsets;
4711 const Value *SrcV = I.getOperand(0);
4712 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4713 SrcV->getType(), ValueVTs, &Offsets, 0);
4714 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4715 "expect a single EVT for swifterror");
4716
4717 SDValue Src = getValue(SrcV);
4718 // Create a virtual register, then update the virtual register.
4719 Register VReg =
4720 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4721 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4722 // Chain can be getRoot or getControlRoot.
4723 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4724 SDValue(Src.getNode(), Src.getResNo()));
4725 DAG.setRoot(CopyNode);
4726}
4727
4728void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4729 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4730 "call visitLoadFromSwiftError when backend supports swifterror");
4731
4732 assert(!I.isVolatile() &&
4733 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4734 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4735 "Support volatile, non temporal, invariant for load_from_swift_error");
4736
4737 const Value *SV = I.getOperand(0);
4738 Type *Ty = I.getType();
4739 assert(
4740 (!BatchAA ||
4741 !BatchAA->pointsToConstantMemory(MemoryLocation(
4742 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4743 I.getAAMetadata()))) &&
4744 "load_from_swift_error should not be constant memory");
4745
4746 SmallVector<EVT, 4> ValueVTs;
4747 SmallVector<uint64_t, 4> Offsets;
4748 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4749 ValueVTs, &Offsets, 0);
4750 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4751 "expect a single EVT for swifterror");
4752
4753 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4754 SDValue L = DAG.getCopyFromReg(
4755 getRoot(), getCurSDLoc(),
4756 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4757
4758 setValue(&I, L);
4759}
4760
4761void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4762 if (I.isAtomic())
4763 return visitAtomicStore(I);
4764
4765 const Value *SrcV = I.getOperand(0);
4766 const Value *PtrV = I.getOperand(1);
4767
4768 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4769 if (TLI.supportSwiftError()) {
4770 // Swifterror values can come from either a function parameter with
4771 // swifterror attribute or an alloca with swifterror attribute.
4772 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4773 if (Arg->hasSwiftErrorAttr())
4774 return visitStoreToSwiftError(I);
4775 }
4776
4777 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4778 if (Alloca->isSwiftError())
4779 return visitStoreToSwiftError(I);
4780 }
4781 }
4782
4783 SmallVector<EVT, 4> ValueVTs, MemVTs;
4785 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4786 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4787 unsigned NumValues = ValueVTs.size();
4788 if (NumValues == 0)
4789 return;
4790
4791 // Get the lowered operands. Note that we do this after
4792 // checking if NumResults is zero, because with zero results
4793 // the operands won't have values in the map.
4794 SDValue Src = getValue(SrcV);
4795 SDValue Ptr = getValue(PtrV);
4796
4797 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4798 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4799 SDLoc dl = getCurSDLoc();
4800 Align Alignment = I.getAlign();
4801 AAMDNodes AAInfo = I.getAAMetadata();
4802
4803 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4804
4805 unsigned ChainI = 0;
4806 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4807 // See visitLoad comments.
4808 if (ChainI == MaxParallelChains) {
4809 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4810 ArrayRef(Chains.data(), ChainI));
4811 Root = Chain;
4812 ChainI = 0;
4813 }
4814
4815 // TODO: MachinePointerInfo only supports a fixed length offset.
4816 MachinePointerInfo PtrInfo =
4817 !Offsets[i].isScalable() || Offsets[i].isZero()
4818 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4819 : MachinePointerInfo();
4820
4821 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4822 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4823 if (MemVTs[i] != ValueVTs[i])
4824 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4825 SDValue St =
4826 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4827 Chains[ChainI] = St;
4828 }
4829
4830 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4831 ArrayRef(Chains.data(), ChainI));
4832 setValue(&I, StoreNode);
4833 DAG.setRoot(StoreNode);
4834}
4835
4836void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4837 bool IsCompressing) {
4838 SDLoc sdl = getCurSDLoc();
4839
4840 auto getMaskedStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4841 Align &Alignment) {
4842 // llvm.masked.store.*(Src0, Ptr, alignment, Mask)
4843 Src0 = I.getArgOperand(0);
4844 Ptr = I.getArgOperand(1);
4845 Alignment = cast<ConstantInt>(I.getArgOperand(2))->getAlignValue();
4846 Mask = I.getArgOperand(3);
4847 };
4848 auto getCompressingStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4849 Align &Alignment) {
4850 // llvm.masked.compressstore.*(Src0, Ptr, Mask)
4851 Src0 = I.getArgOperand(0);
4852 Ptr = I.getArgOperand(1);
4853 Mask = I.getArgOperand(2);
4854 Alignment = I.getParamAlign(1).valueOrOne();
4855 };
4856
4857 Value *PtrOperand, *MaskOperand, *Src0Operand;
4858 Align Alignment;
4859 if (IsCompressing)
4860 getCompressingStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4861 else
4862 getMaskedStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4863
4864 SDValue Ptr = getValue(PtrOperand);
4865 SDValue Src0 = getValue(Src0Operand);
4866 SDValue Mask = getValue(MaskOperand);
4867 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4868
4869 EVT VT = Src0.getValueType();
4870
4871 auto MMOFlags = MachineMemOperand::MOStore;
4872 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4874
4875 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4876 MachinePointerInfo(PtrOperand), MMOFlags,
4877 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4878
4879 const auto &TLI = DAG.getTargetLoweringInfo();
4880 const auto &TTI =
4881 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
4882 SDValue StoreNode =
4883 !IsCompressing && TTI.hasConditionalLoadStoreForType(
4884 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4885 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4886 Mask)
4887 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4888 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4889 IsCompressing);
4890 DAG.setRoot(StoreNode);
4891 setValue(&I, StoreNode);
4892}
4893
4894// Get a uniform base for the Gather/Scatter intrinsic.
4895// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4896// We try to represent it as a base pointer + vector of indices.
4897// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4898// The first operand of the GEP may be a single pointer or a vector of pointers
4899// Example:
4900// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4901// or
4902// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4903// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4904//
4905// When the first GEP operand is a single pointer - it is the uniform base we
4906// are looking for. If first operand of the GEP is a splat vector - we
4907// extract the splat value and use it as a uniform base.
4908// In all other cases the function returns 'false'.
4909static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4910 SDValue &Scale, SelectionDAGBuilder *SDB,
4911 const BasicBlock *CurBB, uint64_t ElemSize) {
4912 SelectionDAG& DAG = SDB->DAG;
4913 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4914 const DataLayout &DL = DAG.getDataLayout();
4915
4916 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4917
4918 // Handle splat constant pointer.
4919 if (auto *C = dyn_cast<Constant>(Ptr)) {
4920 C = C->getSplatValue();
4921 if (!C)
4922 return false;
4923
4924 Base = SDB->getValue(C);
4925
4926 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
4927 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
4928 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
4929 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4930 return true;
4931 }
4932
4934 if (!GEP || GEP->getParent() != CurBB)
4935 return false;
4936
4937 if (GEP->getNumOperands() != 2)
4938 return false;
4939
4940 const Value *BasePtr = GEP->getPointerOperand();
4941 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
4942
4943 // Make sure the base is scalar and the index is a vector.
4944 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
4945 return false;
4946
4947 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
4948 if (ScaleVal.isScalable())
4949 return false;
4950
4951 // Target may not support the required addressing mode.
4952 if (ScaleVal != 1 &&
4953 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
4954 return false;
4955
4956 Base = SDB->getValue(BasePtr);
4957 Index = SDB->getValue(IndexVal);
4958
4959 Scale =
4960 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4961 return true;
4962}
4963
4964void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
4965 SDLoc sdl = getCurSDLoc();
4966
4967 // llvm.masked.scatter.*(Src0, Ptrs, alignment, Mask)
4968 const Value *Ptr = I.getArgOperand(1);
4969 SDValue Src0 = getValue(I.getArgOperand(0));
4970 SDValue Mask = getValue(I.getArgOperand(3));
4971 EVT VT = Src0.getValueType();
4972 Align Alignment = cast<ConstantInt>(I.getArgOperand(2))
4973 ->getMaybeAlignValue()
4974 .value_or(DAG.getEVTAlign(VT.getScalarType()));
4975 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4976
4977 SDValue Base;
4978 SDValue Index;
4979 SDValue Scale;
4980 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
4981 I.getParent(), VT.getScalarStoreSize());
4982
4983 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
4984 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4985 MachinePointerInfo(AS), MachineMemOperand::MOStore,
4986 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4987 if (!UniformBase) {
4988 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4989 Index = getValue(Ptr);
4990 Scale =
4991 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4992 }
4993
4994 EVT IdxVT = Index.getValueType();
4995 EVT EltTy = IdxVT.getVectorElementType();
4996 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
4997 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
4998 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
4999 }
5000
5001 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
5002 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
5003 Ops, MMO, ISD::SIGNED_SCALED, false);
5004 DAG.setRoot(Scatter);
5005 setValue(&I, Scatter);
5006}
5007
5008void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5009 SDLoc sdl = getCurSDLoc();
5010
5011 auto getMaskedLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
5012 Align &Alignment) {
5013 // @llvm.masked.load.*(Ptr, alignment, Mask, Src0)
5014 Ptr = I.getArgOperand(0);
5015 Alignment = cast<ConstantInt>(I.getArgOperand(1))->getAlignValue();
5016 Mask = I.getArgOperand(2);
5017 Src0 = I.getArgOperand(3);
5018 };
5019 auto getExpandingLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
5020 Align &Alignment) {
5021 // @llvm.masked.expandload.*(Ptr, Mask, Src0)
5022 Ptr = I.getArgOperand(0);
5023 Alignment = I.getParamAlign(0).valueOrOne();
5024 Mask = I.getArgOperand(1);
5025 Src0 = I.getArgOperand(2);
5026 };
5027
5028 Value *PtrOperand, *MaskOperand, *Src0Operand;
5029 Align Alignment;
5030 if (IsExpanding)
5031 getExpandingLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
5032 else
5033 getMaskedLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
5034
5035 SDValue Ptr = getValue(PtrOperand);
5036 SDValue Src0 = getValue(Src0Operand);
5037 SDValue Mask = getValue(MaskOperand);
5038 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5039
5040 EVT VT = Src0.getValueType();
5041 AAMDNodes AAInfo = I.getAAMetadata();
5042 const MDNode *Ranges = getRangeMetadata(I);
5043
5044 // Do not serialize masked loads of constant memory with anything.
5045 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5046 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5047
5048 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5049
5050 auto MMOFlags = MachineMemOperand::MOLoad;
5051 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5053
5054 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5055 MachinePointerInfo(PtrOperand), MMOFlags,
5056 LocationSize::beforeOrAfterPointer(), Alignment, AAInfo, Ranges);
5057
5058 const auto &TLI = DAG.getTargetLoweringInfo();
5059 const auto &TTI =
5060 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
5061 // The Load/Res may point to different values and both of them are output
5062 // variables.
5063 SDValue Load;
5064 SDValue Res;
5065 if (!IsExpanding && TTI.hasConditionalLoadStoreForType(Src0Operand->getType(),
5066 /*IsStore=*/false))
5067 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5068 else
5069 Res = Load =
5070 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5071 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5072 if (AddToChain)
5073 PendingLoads.push_back(Load.getValue(1));
5074 setValue(&I, Res);
5075}
5076
5077void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5078 SDLoc sdl = getCurSDLoc();
5079
5080 // @llvm.masked.gather.*(Ptrs, alignment, Mask, Src0)
5081 const Value *Ptr = I.getArgOperand(0);
5082 SDValue Src0 = getValue(I.getArgOperand(3));
5083 SDValue Mask = getValue(I.getArgOperand(2));
5084
5085 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5086 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5087 Align Alignment = cast<ConstantInt>(I.getArgOperand(1))
5088 ->getMaybeAlignValue()
5089 .value_or(DAG.getEVTAlign(VT.getScalarType()));
5090
5091 const MDNode *Ranges = getRangeMetadata(I);
5092
5093 SDValue Root = DAG.getRoot();
5094 SDValue Base;
5095 SDValue Index;
5096 SDValue Scale;
5097 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5098 I.getParent(), VT.getScalarStoreSize());
5099 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5100 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5101 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5102 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5103 Ranges);
5104
5105 if (!UniformBase) {
5106 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5107 Index = getValue(Ptr);
5108 Scale =
5109 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5110 }
5111
5112 EVT IdxVT = Index.getValueType();
5113 EVT EltTy = IdxVT.getVectorElementType();
5114 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5115 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5116 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5117 }
5118
5119 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5120 SDValue Gather =
5121 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5123
5124 PendingLoads.push_back(Gather.getValue(1));
5125 setValue(&I, Gather);
5126}
5127
5128void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5129 SDLoc dl = getCurSDLoc();
5130 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5131 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5132 SyncScope::ID SSID = I.getSyncScopeID();
5133
5134 SDValue InChain = getRoot();
5135
5136 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5137 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5138
5139 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5140 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5141
5142 MachineFunction &MF = DAG.getMachineFunction();
5143 MachineMemOperand *MMO = MF.getMachineMemOperand(
5144 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5145 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5146 FailureOrdering);
5147
5148 SDValue L = DAG.getAtomicCmpSwap(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS,
5149 dl, MemVT, VTs, InChain,
5150 getValue(I.getPointerOperand()),
5151 getValue(I.getCompareOperand()),
5152 getValue(I.getNewValOperand()), MMO);
5153
5154 SDValue OutChain = L.getValue(2);
5155
5156 setValue(&I, L);
5157 DAG.setRoot(OutChain);
5158}
5159
5160void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5161 SDLoc dl = getCurSDLoc();
5163 switch (I.getOperation()) {
5164 default: llvm_unreachable("Unknown atomicrmw operation");
5165 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
5166 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
5167 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
5168 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
5169 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
5170 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
5171 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
5172 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
5173 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
5174 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
5175 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
5176 case AtomicRMWInst::FAdd: NT = ISD::ATOMIC_LOAD_FADD; break;
5177 case AtomicRMWInst::FSub: NT = ISD::ATOMIC_LOAD_FSUB; break;
5178 case AtomicRMWInst::FMax: NT = ISD::ATOMIC_LOAD_FMAX; break;
5179 case AtomicRMWInst::FMin: NT = ISD::ATOMIC_LOAD_FMIN; break;
5181 NT = ISD::ATOMIC_LOAD_FMAXIMUM;
5182 break;
5184 NT = ISD::ATOMIC_LOAD_FMINIMUM;
5185 break;
5187 NT = ISD::ATOMIC_LOAD_UINC_WRAP;
5188 break;
5190 NT = ISD::ATOMIC_LOAD_UDEC_WRAP;
5191 break;
5193 NT = ISD::ATOMIC_LOAD_USUB_COND;
5194 break;
5196 NT = ISD::ATOMIC_LOAD_USUB_SAT;
5197 break;
5198 }
5199 AtomicOrdering Ordering = I.getOrdering();
5200 SyncScope::ID SSID = I.getSyncScopeID();
5201
5202 SDValue InChain = getRoot();
5203
5204 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5205 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5206 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5207
5208 MachineFunction &MF = DAG.getMachineFunction();
5209 MachineMemOperand *MMO = MF.getMachineMemOperand(
5210 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5211 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
5212
5213 SDValue L =
5214 DAG.getAtomic(NT, dl, MemVT, InChain,
5215 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5216 MMO);
5217
5218 SDValue OutChain = L.getValue(1);
5219
5220 setValue(&I, L);
5221 DAG.setRoot(OutChain);
5222}
5223
5224void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5225 SDLoc dl = getCurSDLoc();
5226 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5227 SDValue Ops[3];
5228 Ops[0] = getRoot();
5229 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5230 TLI.getFenceOperandTy(DAG.getDataLayout()));
5231 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5232 TLI.getFenceOperandTy(DAG.getDataLayout()));
5233 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5234 setValue(&I, N);
5235 DAG.setRoot(N);
5236}
5237
5238void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5239 SDLoc dl = getCurSDLoc();
5240 AtomicOrdering Order = I.getOrdering();
5241 SyncScope::ID SSID = I.getSyncScopeID();
5242
5243 SDValue InChain = getRoot();
5244
5245 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5246 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5247 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5248
5249 if (!TLI.supportsUnalignedAtomics() &&
5250 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5251 report_fatal_error("Cannot generate unaligned atomic load");
5252
5253 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5254
5255 const MDNode *Ranges = getRangeMetadata(I);
5256 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5257 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5258 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5259
5260 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5261
5262 SDValue Ptr = getValue(I.getPointerOperand());
5263 SDValue L =
5264 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5265
5266 SDValue OutChain = L.getValue(1);
5267 if (MemVT != VT)
5268 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5269
5270 setValue(&I, L);
5271 DAG.setRoot(OutChain);
5272}
5273
5274void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5275 SDLoc dl = getCurSDLoc();
5276
5277 AtomicOrdering Ordering = I.getOrdering();
5278 SyncScope::ID SSID = I.getSyncScopeID();
5279
5280 SDValue InChain = getRoot();
5281
5282 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5283 EVT MemVT =
5284 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5285
5286 if (!TLI.supportsUnalignedAtomics() &&
5287 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5288 report_fatal_error("Cannot generate unaligned atomic store");
5289
5290 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5291
5292 MachineFunction &MF = DAG.getMachineFunction();
5293 MachineMemOperand *MMO = MF.getMachineMemOperand(
5294 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5295 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5296
5297 SDValue Val = getValue(I.getValueOperand());
5298 if (Val.getValueType() != MemVT)
5299 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5300 SDValue Ptr = getValue(I.getPointerOperand());
5301
5302 SDValue OutChain =
5303 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5304
5305 setValue(&I, OutChain);
5306 DAG.setRoot(OutChain);
5307}
5308
5309/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5310/// node.
5311void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5312 unsigned Intrinsic) {
5313 // Ignore the callsite's attributes. A specific call site may be marked with
5314 // readnone, but the lowering code will expect the chain based on the
5315 // definition.
5316 const Function *F = I.getCalledFunction();
5317 bool HasChain = !F->doesNotAccessMemory();
5318 bool OnlyLoad =
5319 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5320
5321 // Build the operand list.
5323 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5324 if (OnlyLoad) {
5325 // We don't need to serialize loads against other loads.
5326 Ops.push_back(DAG.getRoot());
5327 } else {
5328 Ops.push_back(getRoot());
5329 }
5330 }
5331
5332 // Info is set by getTgtMemIntrinsic
5333 TargetLowering::IntrinsicInfo Info;
5334 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5335 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I,
5336 DAG.getMachineFunction(),
5337 Intrinsic);
5338
5339 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5340 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
5342 Ops.push_back(DAG.getTargetConstant(Intrinsic, getCurSDLoc(),
5343 TLI.getPointerTy(DAG.getDataLayout())));
5344
5345 // Add all operands of the call to the operand list.
5346 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5347 const Value *Arg = I.getArgOperand(i);
5348 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5349 Ops.push_back(getValue(Arg));
5350 continue;
5351 }
5352
5353 // Use TargetConstant instead of a regular constant for immarg.
5354 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5355 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5356 assert(CI->getBitWidth() <= 64 &&
5357 "large intrinsic immediates not handled");
5358 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5359 } else {
5360 Ops.push_back(
5361 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5362 }
5363 }
5364
5365 SmallVector<EVT, 4> ValueVTs;
5366 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5367
5368 if (HasChain)
5369 ValueVTs.push_back(MVT::Other);
5370
5371 SDVTList VTs = DAG.getVTList(ValueVTs);
5372
5373 // Propagate fast-math-flags from IR to node(s).
5374 SDNodeFlags Flags;
5375 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5376 Flags.copyFMF(*FPMO);
5377 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5378
5379 // Create the node.
5381
5382 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5383 auto *Token = Bundle->Inputs[0].get();
5384 SDValue ConvControlToken = getValue(Token);
5385 assert(Ops.back().getValueType() != MVT::Glue &&
5386 "Did not expected another glue node here.");
5387 ConvControlToken =
5388 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5389 Ops.push_back(ConvControlToken);
5390 }
5391
5392 // In some cases, custom collection of operands from CallInst I may be needed.
5394 if (IsTgtIntrinsic) {
5395 // This is target intrinsic that touches memory
5396 //
5397 // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic
5398 // didn't yield anything useful.
5399 MachinePointerInfo MPI;
5400 if (Info.ptrVal)
5401 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5402 else if (Info.fallbackAddressSpace)
5403 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5404 EVT MemVT = Info.memVT;
5405 LocationSize Size = LocationSize::precise(Info.size);
5406 if (Size.hasValue() && !Size.getValue())
5408 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5409 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5410 MPI, Info.flags, Size, Alignment, I.getAAMetadata(), /*Ranges=*/nullptr,
5411 Info.ssid, Info.order, Info.failureOrder);
5412 Result =
5413 DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops, MemVT, MMO);
5414 } else if (!HasChain) {
5415 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5416 } else if (!I.getType()->isVoidTy()) {
5417 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5418 } else {
5419 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5420 }
5421
5422 if (HasChain) {
5423 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
5424 if (OnlyLoad)
5425 PendingLoads.push_back(Chain);
5426 else
5427 DAG.setRoot(Chain);
5428 }
5429
5430 if (!I.getType()->isVoidTy()) {
5431 if (!isa<VectorType>(I.getType()))
5432 Result = lowerRangeToAssertZExt(DAG, I, Result);
5433
5434 MaybeAlign Alignment = I.getRetAlign();
5435
5436 // Insert `assertalign` node if there's an alignment.
5437 if (InsertAssertAlign && Alignment) {
5438 Result =
5439 DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5440 }
5441 }
5442
5443 setValue(&I, Result);
5444}
5445
5446/// GetSignificand - Get the significand and build it into a floating-point
5447/// number with exponent of 1:
5448///
5449/// Op = (Op & 0x007fffff) | 0x3f800000;
5450///
5451/// where Op is the hexadecimal representation of floating point value.
5453 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5454 DAG.getConstant(0x007fffff, dl, MVT::i32));
5455 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5456 DAG.getConstant(0x3f800000, dl, MVT::i32));
5457 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5458}
5459
5460/// GetExponent - Get the exponent:
5461///
5462/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5463///
5464/// where Op is the hexadecimal representation of floating point value.
5466 const TargetLowering &TLI, const SDLoc &dl) {
5467 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5468 DAG.getConstant(0x7f800000, dl, MVT::i32));
5469 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5470 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5471 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5472 DAG.getConstant(127, dl, MVT::i32));
5473 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5474}
5475
5476/// getF32Constant - Get 32-bit floating point constant.
5478 const SDLoc &dl) {
5479 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5480 MVT::f32);
5481}
5482
5484 SelectionDAG &DAG) {
5485 // TODO: What fast-math-flags should be set on the floating-point nodes?
5486
5487 // IntegerPartOfX = ((int32_t)(t0);
5488 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5489
5490 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5491 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5492 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5493
5494 // IntegerPartOfX <<= 23;
5495 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5496 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5497
5498 SDValue TwoToFractionalPartOfX;
5499 if (LimitFloatPrecision <= 6) {
5500 // For floating-point precision of 6:
5501 //
5502 // TwoToFractionalPartOfX =
5503 // 0.997535578f +
5504 // (0.735607626f + 0.252464424f * x) * x;
5505 //
5506 // error 0.0144103317, which is 6 bits
5507 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5508 getF32Constant(DAG, 0x3e814304, dl));
5509 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5510 getF32Constant(DAG, 0x3f3c50c8, dl));
5511 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5512 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5513 getF32Constant(DAG, 0x3f7f5e7e, dl));
5514 } else if (LimitFloatPrecision <= 12) {
5515 // For floating-point precision of 12:
5516 //
5517 // TwoToFractionalPartOfX =
5518 // 0.999892986f +
5519 // (0.696457318f +
5520 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5521 //
5522 // error 0.000107046256, which is 13 to 14 bits
5523 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5524 getF32Constant(DAG, 0x3da235e3, dl));
5525 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5526 getF32Constant(DAG, 0x3e65b8f3, dl));
5527 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5528 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5529 getF32Constant(DAG, 0x3f324b07, dl));
5530 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5531 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5532 getF32Constant(DAG, 0x3f7ff8fd, dl));
5533 } else { // LimitFloatPrecision <= 18
5534 // For floating-point precision of 18:
5535 //
5536 // TwoToFractionalPartOfX =
5537 // 0.999999982f +
5538 // (0.693148872f +
5539 // (0.240227044f +
5540 // (0.554906021e-1f +
5541 // (0.961591928e-2f +
5542 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5543 // error 2.47208000*10^(-7), which is better than 18 bits
5544 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5545 getF32Constant(DAG, 0x3924b03e, dl));
5546 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5547 getF32Constant(DAG, 0x3ab24b87, dl));
5548 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5549 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5550 getF32Constant(DAG, 0x3c1d8c17, dl));
5551 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5552 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5553 getF32Constant(DAG, 0x3d634a1d, dl));
5554 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5555 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5556 getF32Constant(DAG, 0x3e75fe14, dl));
5557 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5558 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5559 getF32Constant(DAG, 0x3f317234, dl));
5560 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5561 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5562 getF32Constant(DAG, 0x3f800000, dl));
5563 }
5564
5565 // Add the exponent into the result in integer domain.
5566 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5567 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5568 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5569}
5570
5571/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5572/// limited-precision mode.
5574 const TargetLowering &TLI, SDNodeFlags Flags) {
5575 if (Op.getValueType() == MVT::f32 &&
5577
5578 // Put the exponent in the right bit position for later addition to the
5579 // final result:
5580 //
5581 // t0 = Op * log2(e)
5582
5583 // TODO: What fast-math-flags should be set here?
5584 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5585 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5586 return getLimitedPrecisionExp2(t0, dl, DAG);
5587 }
5588
5589 // No special expansion.
5590 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5591}
5592
5593/// expandLog - Lower a log intrinsic. Handles the special sequences for
5594/// limited-precision mode.
5596 const TargetLowering &TLI, SDNodeFlags Flags) {
5597 // TODO: What fast-math-flags should be set on the floating-point nodes?
5598
5599 if (Op.getValueType() == MVT::f32 &&
5601 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5602
5603 // Scale the exponent by log(2).
5604 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5605 SDValue LogOfExponent =
5606 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5607 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5608
5609 // Get the significand and build it into a floating-point number with
5610 // exponent of 1.
5611 SDValue X = GetSignificand(DAG, Op1, dl);
5612
5613 SDValue LogOfMantissa;
5614 if (LimitFloatPrecision <= 6) {
5615 // For floating-point precision of 6:
5616 //
5617 // LogofMantissa =
5618 // -1.1609546f +
5619 // (1.4034025f - 0.23903021f * x) * x;
5620 //
5621 // error 0.0034276066, which is better than 8 bits
5622 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5623 getF32Constant(DAG, 0xbe74c456, dl));
5624 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5625 getF32Constant(DAG, 0x3fb3a2b1, dl));
5626 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5627 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5628 getF32Constant(DAG, 0x3f949a29, dl));
5629 } else if (LimitFloatPrecision <= 12) {
5630 // For floating-point precision of 12:
5631 //
5632 // LogOfMantissa =
5633 // -1.7417939f +
5634 // (2.8212026f +
5635 // (-1.4699568f +
5636 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5637 //
5638 // error 0.000061011436, which is 14 bits
5639 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5640 getF32Constant(DAG, 0xbd67b6d6, dl));
5641 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5642 getF32Constant(DAG, 0x3ee4f4b8, dl));
5643 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5644 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5645 getF32Constant(DAG, 0x3fbc278b, dl));
5646 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5647 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5648 getF32Constant(DAG, 0x40348e95, dl));
5649 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5650 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5651 getF32Constant(DAG, 0x3fdef31a, dl));
5652 } else { // LimitFloatPrecision <= 18
5653 // For floating-point precision of 18:
5654 //
5655 // LogOfMantissa =
5656 // -2.1072184f +
5657 // (4.2372794f +
5658 // (-3.7029485f +
5659 // (2.2781945f +
5660 // (-0.87823314f +
5661 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5662 //
5663 // error 0.0000023660568, which is better than 18 bits
5664 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5665 getF32Constant(DAG, 0xbc91e5ac, dl));
5666 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5667 getF32Constant(DAG, 0x3e4350aa, dl));
5668 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5669 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5670 getF32Constant(DAG, 0x3f60d3e3, dl));
5671 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5672 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5673 getF32Constant(DAG, 0x4011cdf0, dl));
5674 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5675 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5676 getF32Constant(DAG, 0x406cfd1c, dl));
5677 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5678 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5679 getF32Constant(DAG, 0x408797cb, dl));
5680 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5681 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5682 getF32Constant(DAG, 0x4006dcab, dl));
5683 }
5684
5685 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5686 }
5687
5688 // No special expansion.
5689 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5690}
5691
5692/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5693/// limited-precision mode.
5695 const TargetLowering &TLI, SDNodeFlags Flags) {
5696 // TODO: What fast-math-flags should be set on the floating-point nodes?
5697
5698 if (Op.getValueType() == MVT::f32 &&
5700 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5701
5702 // Get the exponent.
5703 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5704
5705 // Get the significand and build it into a floating-point number with
5706 // exponent of 1.
5707 SDValue X = GetSignificand(DAG, Op1, dl);
5708
5709 // Different possible minimax approximations of significand in
5710 // floating-point for various degrees of accuracy over [1,2].
5711 SDValue Log2ofMantissa;
5712 if (LimitFloatPrecision <= 6) {
5713 // For floating-point precision of 6:
5714 //
5715 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5716 //
5717 // error 0.0049451742, which is more than 7 bits
5718 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5719 getF32Constant(DAG, 0xbeb08fe0, dl));
5720 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5721 getF32Constant(DAG, 0x40019463, dl));
5722 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5723 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5724 getF32Constant(DAG, 0x3fd6633d, dl));
5725 } else if (LimitFloatPrecision <= 12) {
5726 // For floating-point precision of 12:
5727 //
5728 // Log2ofMantissa =
5729 // -2.51285454f +
5730 // (4.07009056f +
5731 // (-2.12067489f +
5732 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5733 //
5734 // error 0.0000876136000, which is better than 13 bits
5735 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5736 getF32Constant(DAG, 0xbda7262e, dl));
5737 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5738 getF32Constant(DAG, 0x3f25280b, dl));
5739 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5740 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5741 getF32Constant(DAG, 0x4007b923, dl));
5742 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5743 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5744 getF32Constant(DAG, 0x40823e2f, dl));
5745 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5746 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5747 getF32Constant(DAG, 0x4020d29c, dl));
5748 } else { // LimitFloatPrecision <= 18
5749 // For floating-point precision of 18:
5750 //
5751 // Log2ofMantissa =
5752 // -3.0400495f +
5753 // (6.1129976f +
5754 // (-5.3420409f +
5755 // (3.2865683f +
5756 // (-1.2669343f +
5757 // (0.27515199f -
5758 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5759 //
5760 // error 0.0000018516, which is better than 18 bits
5761 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5762 getF32Constant(DAG, 0xbcd2769e, dl));
5763 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5764 getF32Constant(DAG, 0x3e8ce0b9, dl));
5765 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5766 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5767 getF32Constant(DAG, 0x3fa22ae7, dl));
5768 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5769 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5770 getF32Constant(DAG, 0x40525723, dl));
5771 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5772 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5773 getF32Constant(DAG, 0x40aaf200, dl));
5774 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5775 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5776 getF32Constant(DAG, 0x40c39dad, dl));
5777 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5778 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5779 getF32Constant(DAG, 0x4042902c, dl));
5780 }
5781
5782 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5783 }
5784
5785 // No special expansion.
5786 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5787}
5788
5789/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5790/// limited-precision mode.
5792 const TargetLowering &TLI, SDNodeFlags Flags) {
5793 // TODO: What fast-math-flags should be set on the floating-point nodes?
5794
5795 if (Op.getValueType() == MVT::f32 &&
5797 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5798
5799 // Scale the exponent by log10(2) [0.30102999f].
5800 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5801 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5802 getF32Constant(DAG, 0x3e9a209a, dl));
5803
5804 // Get the significand and build it into a floating-point number with
5805 // exponent of 1.
5806 SDValue X = GetSignificand(DAG, Op1, dl);
5807
5808 SDValue Log10ofMantissa;
5809 if (LimitFloatPrecision <= 6) {
5810 // For floating-point precision of 6:
5811 //
5812 // Log10ofMantissa =
5813 // -0.50419619f +
5814 // (0.60948995f - 0.10380950f * x) * x;
5815 //
5816 // error 0.0014886165, which is 6 bits
5817 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5818 getF32Constant(DAG, 0xbdd49a13, dl));
5819 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5820 getF32Constant(DAG, 0x3f1c0789, dl));
5821 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5822 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5823 getF32Constant(DAG, 0x3f011300, dl));
5824 } else if (LimitFloatPrecision <= 12) {
5825 // For floating-point precision of 12:
5826 //
5827 // Log10ofMantissa =
5828 // -0.64831180f +
5829 // (0.91751397f +
5830 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5831 //
5832 // error 0.00019228036, which is better than 12 bits
5833 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5834 getF32Constant(DAG, 0x3d431f31, dl));
5835 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5836 getF32Constant(DAG, 0x3ea21fb2, dl));
5837 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5838 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5839 getF32Constant(DAG, 0x3f6ae232, dl));
5840 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5841 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5842 getF32Constant(DAG, 0x3f25f7c3, dl));
5843 } else { // LimitFloatPrecision <= 18
5844 // For floating-point precision of 18:
5845 //
5846 // Log10ofMantissa =
5847 // -0.84299375f +
5848 // (1.5327582f +
5849 // (-1.0688956f +
5850 // (0.49102474f +
5851 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5852 //
5853 // error 0.0000037995730, which is better than 18 bits
5854 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5855 getF32Constant(DAG, 0x3c5d51ce, dl));
5856 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5857 getF32Constant(DAG, 0x3e00685a, dl));
5858 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5859 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5860 getF32Constant(DAG, 0x3efb6798, dl));
5861 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5862 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5863 getF32Constant(DAG, 0x3f88d192, dl));
5864 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5865 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5866 getF32Constant(DAG, 0x3fc4316c, dl));
5867 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5868 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5869 getF32Constant(DAG, 0x3f57ce70, dl));
5870 }
5871
5872 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5873 }
5874
5875 // No special expansion.
5876 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5877}
5878
5879/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5880/// limited-precision mode.
5882 const TargetLowering &TLI, SDNodeFlags Flags) {
5883 if (Op.getValueType() == MVT::f32 &&
5885 return getLimitedPrecisionExp2(Op, dl, DAG);
5886
5887 // No special expansion.
5888 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
5889}
5890
5891/// visitPow - Lower a pow intrinsic. Handles the special sequences for
5892/// limited-precision mode with x == 10.0f.
5894 SelectionDAG &DAG, const TargetLowering &TLI,
5895 SDNodeFlags Flags) {
5896 bool IsExp10 = false;
5897 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
5900 APFloat Ten(10.0f);
5901 IsExp10 = LHSC->isExactlyValue(Ten);
5902 }
5903 }
5904
5905 // TODO: What fast-math-flags should be set on the FMUL node?
5906 if (IsExp10) {
5907 // Put the exponent in the right bit position for later addition to the
5908 // final result:
5909 //
5910 // #define LOG2OF10 3.3219281f
5911 // t0 = Op * LOG2OF10;
5912 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
5913 getF32Constant(DAG, 0x40549a78, dl));
5914 return getLimitedPrecisionExp2(t0, dl, DAG);
5915 }
5916
5917 // No special expansion.
5918 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
5919}
5920
5921/// ExpandPowI - Expand a llvm.powi intrinsic.
5923 SelectionDAG &DAG) {
5924 // If RHS is a constant, we can expand this out to a multiplication tree if
5925 // it's beneficial on the target, otherwise we end up lowering to a call to
5926 // __powidf2 (for example).
5928 unsigned Val = RHSC->getSExtValue();
5929
5930 // powi(x, 0) -> 1.0
5931 if (Val == 0)
5932 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
5933
5935 Val, DAG.shouldOptForSize())) {
5936 // Get the exponent as a positive value.
5937 if ((int)Val < 0)
5938 Val = -Val;
5939 // We use the simple binary decomposition method to generate the multiply
5940 // sequence. There are more optimal ways to do this (for example,
5941 // powi(x,15) generates one more multiply than it should), but this has
5942 // the benefit of being both really simple and much better than a libcall.
5943 SDValue Res; // Logically starts equal to 1.0
5944 SDValue CurSquare = LHS;
5945 // TODO: Intrinsics should have fast-math-flags that propagate to these
5946 // nodes.
5947 while (Val) {
5948 if (Val & 1) {
5949 if (Res.getNode())
5950 Res =
5951 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
5952 else
5953 Res = CurSquare; // 1.0*CurSquare.
5954 }
5955
5956 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
5957 CurSquare, CurSquare);
5958 Val >>= 1;
5959 }
5960
5961 // If the original was negative, invert the result, producing 1/(x*x*x).
5962 if (RHSC->getSExtValue() < 0)
5963 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
5964 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
5965 return Res;
5966 }
5967 }
5968
5969 // Otherwise, expand to a libcall.
5970 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
5971}
5972
5973static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
5974 SDValue LHS, SDValue RHS, SDValue Scale,
5975 SelectionDAG &DAG, const TargetLowering &TLI) {
5976 EVT VT = LHS.getValueType();
5977 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
5978 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
5979 LLVMContext &Ctx = *DAG.getContext();
5980
5981 // If the type is legal but the operation isn't, this node might survive all
5982 // the way to operation legalization. If we end up there and we do not have
5983 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
5984 // node.
5985
5986 // Coax the legalizer into expanding the node during type legalization instead
5987 // by bumping the size by one bit. This will force it to Promote, enabling the
5988 // early expansion and avoiding the need to expand later.
5989
5990 // We don't have to do this if Scale is 0; that can always be expanded, unless
5991 // it's a saturating signed operation. Those can experience true integer
5992 // division overflow, a case which we must avoid.
5993
5994 // FIXME: We wouldn't have to do this (or any of the early
5995 // expansion/promotion) if it was possible to expand a libcall of an
5996 // illegal type during operation legalization. But it's not, so things
5997 // get a bit hacky.
5998 unsigned ScaleInt = Scale->getAsZExtVal();
5999 if ((ScaleInt > 0 || (Saturating && Signed)) &&
6000 (TLI.isTypeLegal(VT) ||
6001 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6003 Opcode, VT, ScaleInt);
6004 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6005 EVT PromVT;
6006 if (VT.isScalarInteger())
6007 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6008 else if (VT.isVector()) {
6009 PromVT = VT.getVectorElementType();
6010 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6011 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6012 } else
6013 llvm_unreachable("Wrong VT for DIVFIX?");
6014 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6015 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6016 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6017 // For saturating operations, we need to shift up the LHS to get the
6018 // proper saturation width, and then shift down again afterwards.
6019 if (Saturating)
6020 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6021 DAG.getConstant(1, DL, ShiftTy));
6022 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6023 if (Saturating)
6024 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6025 DAG.getConstant(1, DL, ShiftTy));
6026 return DAG.getZExtOrTrunc(Res, DL, VT);
6027 }
6028 }
6029
6030 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6031}
6032
6033// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6034// bitcasted, or split argument. Returns a list of <Register, size in bits>
6035static void
6036getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6037 const SDValue &N) {
6038 switch (N.getOpcode()) {
6039 case ISD::CopyFromReg: {
6040 SDValue Op = N.getOperand(1);
6041 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6042 Op.getValueType().getSizeInBits());
6043 return;
6044 }
6045 case ISD::BITCAST:
6046 case ISD::AssertZext:
6047 case ISD::AssertSext:
6048 case ISD::TRUNCATE:
6049 getUnderlyingArgRegs(Regs, N.getOperand(0));
6050 return;
6051 case ISD::BUILD_PAIR:
6052 case ISD::BUILD_VECTOR:
6054 for (SDValue Op : N->op_values())
6055 getUnderlyingArgRegs(Regs, Op);
6056 return;
6057 default:
6058 return;
6059 }
6060}
6061
6062/// If the DbgValueInst is a dbg_value of a function argument, create the
6063/// corresponding DBG_VALUE machine instruction for it now. At the end of
6064/// instruction selection, they will be inserted to the entry BB.
6065/// We don't currently support this for variadic dbg_values, as they shouldn't
6066/// appear for function arguments or in the prologue.
6067bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6068 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6069 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6070 const Argument *Arg = dyn_cast<Argument>(V);
6071 if (!Arg)
6072 return false;
6073
6074 MachineFunction &MF = DAG.getMachineFunction();
6075 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6076
6077 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6078 // we've been asked to pursue.
6079 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6080 bool Indirect) {
6081 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6082 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6083 // pointing at the VReg, which will be patched up later.
6084 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6086 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6087 /* isKill */ false, /* isDead */ false,
6088 /* isUndef */ false, /* isEarlyClobber */ false,
6089 /* SubReg */ 0, /* isDebug */ true)});
6090
6091 auto *NewDIExpr = FragExpr;
6092 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6093 // the DIExpression.
6094 if (Indirect)
6095 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6097 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6098 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6099 } else {
6100 // Create a completely standard DBG_VALUE.
6101 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6102 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6103 }
6104 };
6105
6106 if (Kind == FuncArgumentDbgValueKind::Value) {
6107 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6108 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6109 // the entry block.
6110 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6111 if (!IsInEntryBlock)
6112 return false;
6113
6114 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6115 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6116 // variable that also is a param.
6117 //
6118 // Although, if we are at the top of the entry block already, we can still
6119 // emit using ArgDbgValue. This might catch some situations when the
6120 // dbg.value refers to an argument that isn't used in the entry block, so
6121 // any CopyToReg node would be optimized out and the only way to express
6122 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6123 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6124 // we should only emit as ArgDbgValue if the Variable is an argument to the
6125 // current function, and the dbg.value intrinsic is found in the entry
6126 // block.
6127 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6128 !DL->getInlinedAt();
6129 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6130 if (!IsInPrologue && !VariableIsFunctionInputArg)
6131 return false;
6132
6133 // Here we assume that a function argument on IR level only can be used to
6134 // describe one input parameter on source level. If we for example have
6135 // source code like this
6136 //
6137 // struct A { long x, y; };
6138 // void foo(struct A a, long b) {
6139 // ...
6140 // b = a.x;
6141 // ...
6142 // }
6143 //
6144 // and IR like this
6145 //
6146 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6147 // entry:
6148 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6149 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6150 // call void @llvm.dbg.value(metadata i32 %b, "b",
6151 // ...
6152 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6153 // ...
6154 //
6155 // then the last dbg.value is describing a parameter "b" using a value that
6156 // is an argument. But since we already has used %a1 to describe a parameter
6157 // we should not handle that last dbg.value here (that would result in an
6158 // incorrect hoisting of the DBG_VALUE to the function entry).
6159 // Notice that we allow one dbg.value per IR level argument, to accommodate
6160 // for the situation with fragments above.
6161 // If there is no node for the value being handled, we return true to skip
6162 // the normal generation of debug info, as it would kill existing debug
6163 // info for the parameter in case of duplicates.
6164 if (VariableIsFunctionInputArg) {
6165 unsigned ArgNo = Arg->getArgNo();
6166 if (ArgNo >= FuncInfo.DescribedArgs.size())
6167 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6168 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6169 return !NodeMap[V].getNode();
6170 FuncInfo.DescribedArgs.set(ArgNo);
6171 }
6172 }
6173
6174 bool IsIndirect = false;
6175 std::optional<MachineOperand> Op;
6176 // Some arguments' frame index is recorded during argument lowering.
6177 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6178 if (FI != std::numeric_limits<int>::max())
6180
6182 if (!Op && N.getNode()) {
6183 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6184 Register Reg;
6185 if (ArgRegsAndSizes.size() == 1)
6186 Reg = ArgRegsAndSizes.front().first;
6187
6188 if (Reg && Reg.isVirtual()) {
6189 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6190 Register PR = RegInfo.getLiveInPhysReg(Reg);
6191 if (PR)
6192 Reg = PR;
6193 }
6194 if (Reg) {
6196 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6197 }
6198 }
6199
6200 if (!Op && N.getNode()) {
6201 // Check if frame index is available.
6202 SDValue LCandidate = peekThroughBitcasts(N);
6203 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6204 if (FrameIndexSDNode *FINode =
6205 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6206 Op = MachineOperand::CreateFI(FINode->getIndex());
6207 }
6208
6209 if (!Op) {
6210 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6211 auto splitMultiRegDbgValue = [&](ArrayRef<std::pair<Register, TypeSize>>
6212 SplitRegs) {
6213 unsigned Offset = 0;
6214 for (const auto &RegAndSize : SplitRegs) {
6215 // If the expression is already a fragment, the current register
6216 // offset+size might extend beyond the fragment. In this case, only
6217 // the register bits that are inside the fragment are relevant.
6218 int RegFragmentSizeInBits = RegAndSize.second;
6219 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6220 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6221 // The register is entirely outside the expression fragment,
6222 // so is irrelevant for debug info.
6223 if (Offset >= ExprFragmentSizeInBits)
6224 break;
6225 // The register is partially outside the expression fragment, only
6226 // the low bits within the fragment are relevant for debug info.
6227 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6228 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6229 }
6230 }
6231
6232 auto FragmentExpr = DIExpression::createFragmentExpression(
6233 Expr, Offset, RegFragmentSizeInBits);
6234 Offset += RegAndSize.second;
6235 // If a valid fragment expression cannot be created, the variable's
6236 // correct value cannot be determined and so it is set as poison.
6237 if (!FragmentExpr) {
6238 SDDbgValue *SDV = DAG.getConstantDbgValue(
6239 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6240 DAG.AddDbgValue(SDV, false);
6241 continue;
6242 }
6243 MachineInstr *NewMI =
6244 MakeVRegDbgValue(RegAndSize.first, *FragmentExpr,
6245 Kind != FuncArgumentDbgValueKind::Value);
6246 FuncInfo.ArgDbgValues.push_back(NewMI);
6247 }
6248 };
6249
6250 // Check if ValueMap has reg number.
6252 VMI = FuncInfo.ValueMap.find(V);
6253 if (VMI != FuncInfo.ValueMap.end()) {
6254 const auto &TLI = DAG.getTargetLoweringInfo();
6255 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6256 V->getType(), std::nullopt);
6257 if (RFV.occupiesMultipleRegs()) {
6258 splitMultiRegDbgValue(RFV.getRegsAndSizes());
6259 return true;
6260 }
6261
6262 Op = MachineOperand::CreateReg(VMI->second, false);
6263 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6264 } else if (ArgRegsAndSizes.size() > 1) {
6265 // This was split due to the calling convention, and no virtual register
6266 // mapping exists for the value.
6267 splitMultiRegDbgValue(ArgRegsAndSizes);
6268 return true;
6269 }
6270 }
6271
6272 if (!Op)
6273 return false;
6274
6275 assert(Variable->isValidLocationForIntrinsic(DL) &&
6276 "Expected inlined-at fields to agree");
6277 MachineInstr *NewMI = nullptr;
6278
6279 if (Op->isReg())
6280 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6281 else
6282 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6283 Variable, Expr);
6284
6285 // Otherwise, use ArgDbgValues.
6286 FuncInfo.ArgDbgValues.push_back(NewMI);
6287 return true;
6288}
6289
6290/// Return the appropriate SDDbgValue based on N.
6291SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6292 DILocalVariable *Variable,
6293 DIExpression *Expr,
6294 const DebugLoc &dl,
6295 unsigned DbgSDNodeOrder) {
6296 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6297 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6298 // stack slot locations.
6299 //
6300 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6301 // debug values here after optimization:
6302 //
6303 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6304 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6305 //
6306 // Both describe the direct values of their associated variables.
6307 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6308 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6309 }
6310 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6311 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6312}
6313
6314static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6315 switch (Intrinsic) {
6316 case Intrinsic::smul_fix:
6317 return ISD::SMULFIX;
6318 case Intrinsic::umul_fix:
6319 return ISD::UMULFIX;
6320 case Intrinsic::smul_fix_sat:
6321 return ISD::SMULFIXSAT;
6322 case Intrinsic::umul_fix_sat:
6323 return ISD::UMULFIXSAT;
6324 case Intrinsic::sdiv_fix:
6325 return ISD::SDIVFIX;
6326 case Intrinsic::udiv_fix:
6327 return ISD::UDIVFIX;
6328 case Intrinsic::sdiv_fix_sat:
6329 return ISD::SDIVFIXSAT;
6330 case Intrinsic::udiv_fix_sat:
6331 return ISD::UDIVFIXSAT;
6332 default:
6333 llvm_unreachable("Unhandled fixed point intrinsic");
6334 }
6335}
6336
6337/// Given a @llvm.call.preallocated.setup, return the corresponding
6338/// preallocated call.
6339static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6340 assert(cast<CallBase>(PreallocatedSetup)
6342 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6343 "expected call_preallocated_setup Value");
6344 for (const auto *U : PreallocatedSetup->users()) {
6345 auto *UseCall = cast<CallBase>(U);
6346 const Function *Fn = UseCall->getCalledFunction();
6347 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6348 return UseCall;
6349 }
6350 }
6351 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6352}
6353
6354/// If DI is a debug value with an EntryValue expression, lower it using the
6355/// corresponding physical register of the associated Argument value
6356/// (guaranteed to exist by the verifier).
6357bool SelectionDAGBuilder::visitEntryValueDbgValue(
6358 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6359 DIExpression *Expr, DebugLoc DbgLoc) {
6360 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6361 return false;
6362
6363 // These properties are guaranteed by the verifier.
6364 const Argument *Arg = cast<Argument>(Values[0]);
6365 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6366
6367 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6368 if (ArgIt == FuncInfo.ValueMap.end()) {
6369 LLVM_DEBUG(
6370 dbgs() << "Dropping dbg.value: expression is entry_value but "
6371 "couldn't find an associated register for the Argument\n");
6372 return true;
6373 }
6374 Register ArgVReg = ArgIt->getSecond();
6375
6376 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6377 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6378 SDDbgValue *SDV = DAG.getVRegDbgValue(
6379 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6380 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6381 return true;
6382 }
6383 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6384 "couldn't find a physical register\n");
6385 return true;
6386}
6387
6388/// Lower the call to the specified intrinsic function.
6389void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6390 unsigned Intrinsic) {
6391 SDLoc sdl = getCurSDLoc();
6392 switch (Intrinsic) {
6393 case Intrinsic::experimental_convergence_anchor:
6394 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6395 break;
6396 case Intrinsic::experimental_convergence_entry:
6397 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6398 break;
6399 case Intrinsic::experimental_convergence_loop: {
6400 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6401 auto *Token = Bundle->Inputs[0].get();
6402 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6403 getValue(Token)));
6404 break;
6405 }
6406 }
6407}
6408
6409void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6410 unsigned IntrinsicID) {
6411 // For now, we're only lowering an 'add' histogram.
6412 // We can add others later, e.g. saturating adds, min/max.
6413 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6414 "Tried to lower unsupported histogram type");
6415 SDLoc sdl = getCurSDLoc();
6416 Value *Ptr = I.getOperand(0);
6417 SDValue Inc = getValue(I.getOperand(1));
6418 SDValue Mask = getValue(I.getOperand(2));
6419
6420 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6421 DataLayout TargetDL = DAG.getDataLayout();
6422 EVT VT = Inc.getValueType();
6423 Align Alignment = DAG.getEVTAlign(VT);
6424
6425 const MDNode *Ranges = getRangeMetadata(I);
6426
6427 SDValue Root = DAG.getRoot();
6428 SDValue Base;
6429 SDValue Index;
6430 SDValue Scale;
6431 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6432 I.getParent(), VT.getScalarStoreSize());
6433
6434 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6435
6436 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6437 MachinePointerInfo(AS),
6439 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6440
6441 if (!UniformBase) {
6442 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6443 Index = getValue(Ptr);
6444 Scale =
6445 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6446 }
6447
6448 EVT IdxVT = Index.getValueType();
6449 EVT EltTy = IdxVT.getVectorElementType();
6450 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6451 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
6452 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6453 }
6454
6455 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6456
6457 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6458 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6459 Ops, MMO, ISD::SIGNED_SCALED);
6460
6461 setValue(&I, Histogram);
6462 DAG.setRoot(Histogram);
6463}
6464
6465void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6466 unsigned Intrinsic) {
6467 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6468 "Tried lowering invalid vector extract last");
6469 SDLoc sdl = getCurSDLoc();
6470 const DataLayout &Layout = DAG.getDataLayout();
6471 SDValue Data = getValue(I.getOperand(0));
6472 SDValue Mask = getValue(I.getOperand(1));
6473
6474 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6475 EVT ResVT = TLI.getValueType(Layout, I.getType());
6476
6477 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6478 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6479 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6480
6481 Value *Default = I.getOperand(2);
6483 SDValue PassThru = getValue(Default);
6484 EVT BoolVT = Mask.getValueType().getScalarType();
6485 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6486 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6487 }
6488
6489 setValue(&I, Result);
6490}
6491
6492/// Lower the call to the specified intrinsic function.
6493void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6494 unsigned Intrinsic) {
6495 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6496 SDLoc sdl = getCurSDLoc();
6497 DebugLoc dl = getCurDebugLoc();
6498 SDValue Res;
6499
6500 SDNodeFlags Flags;
6501 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6502 Flags.copyFMF(*FPOp);
6503
6504 switch (Intrinsic) {
6505 default:
6506 // By default, turn this into a target intrinsic node.
6507 visitTargetIntrinsic(I, Intrinsic);
6508 return;
6509 case Intrinsic::vscale: {
6510 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6511 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6512 return;
6513 }
6514 case Intrinsic::vastart: visitVAStart(I); return;
6515 case Intrinsic::vaend: visitVAEnd(I); return;
6516 case Intrinsic::vacopy: visitVACopy(I); return;
6517 case Intrinsic::returnaddress:
6518 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6519 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6520 getValue(I.getArgOperand(0))));
6521 return;
6522 case Intrinsic::addressofreturnaddress:
6523 setValue(&I,
6524 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6525 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6526 return;
6527 case Intrinsic::sponentry:
6528 setValue(&I,
6529 DAG.getNode(ISD::SPONENTRY, sdl,
6530 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6531 return;
6532 case Intrinsic::frameaddress:
6533 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6534 TLI.getFrameIndexTy(DAG.getDataLayout()),
6535 getValue(I.getArgOperand(0))));
6536 return;
6537 case Intrinsic::read_volatile_register:
6538 case Intrinsic::read_register: {
6539 Value *Reg = I.getArgOperand(0);
6540 SDValue Chain = getRoot();
6542 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6543 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6544 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6545 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6546 setValue(&I, Res);
6547 DAG.setRoot(Res.getValue(1));
6548 return;
6549 }
6550 case Intrinsic::write_register: {
6551 Value *Reg = I.getArgOperand(0);
6552 Value *RegValue = I.getArgOperand(1);
6553 SDValue Chain = getRoot();
6555 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6556 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6557 RegName, getValue(RegValue)));
6558 return;
6559 }
6560 case Intrinsic::memcpy:
6561 case Intrinsic::memcpy_inline: {
6562 const auto &MCI = cast<MemCpyInst>(I);
6563 SDValue Dst = getValue(I.getArgOperand(0));
6564 SDValue Src = getValue(I.getArgOperand(1));
6565 SDValue Size = getValue(I.getArgOperand(2));
6566 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6567 "memcpy_inline needs constant size");
6568 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6569 Align DstAlign = MCI.getDestAlign().valueOrOne();
6570 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6571 Align Alignment = std::min(DstAlign, SrcAlign);
6572 bool isVol = MCI.isVolatile();
6573 // FIXME: Support passing different dest/src alignments to the memcpy DAG
6574 // node.
6575 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6576 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol,
6577 MCI.isForceInlined(), &I, std::nullopt,
6578 MachinePointerInfo(I.getArgOperand(0)),
6579 MachinePointerInfo(I.getArgOperand(1)),
6580 I.getAAMetadata(), BatchAA);
6581 updateDAGForMaybeTailCall(MC);
6582 return;
6583 }
6584 case Intrinsic::memset:
6585 case Intrinsic::memset_inline: {
6586 const auto &MSII = cast<MemSetInst>(I);
6587 SDValue Dst = getValue(I.getArgOperand(0));
6588 SDValue Value = getValue(I.getArgOperand(1));
6589 SDValue Size = getValue(I.getArgOperand(2));
6590 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6591 "memset_inline needs constant size");
6592 // @llvm.memset defines 0 and 1 to both mean no alignment.
6593 Align DstAlign = MSII.getDestAlign().valueOrOne();
6594 bool isVol = MSII.isVolatile();
6595 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6596 SDValue MC = DAG.getMemset(
6597 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6598 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6599 updateDAGForMaybeTailCall(MC);
6600 return;
6601 }
6602 case Intrinsic::memmove: {
6603 const auto &MMI = cast<MemMoveInst>(I);
6604 SDValue Op1 = getValue(I.getArgOperand(0));
6605 SDValue Op2 = getValue(I.getArgOperand(1));
6606 SDValue Op3 = getValue(I.getArgOperand(2));
6607 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6608 Align DstAlign = MMI.getDestAlign().valueOrOne();
6609 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6610 Align Alignment = std::min(DstAlign, SrcAlign);
6611 bool isVol = MMI.isVolatile();
6612 // FIXME: Support passing different dest/src alignments to the memmove DAG
6613 // node.
6614 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6615 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, &I,
6616 /* OverrideTailCall */ std::nullopt,
6617 MachinePointerInfo(I.getArgOperand(0)),
6618 MachinePointerInfo(I.getArgOperand(1)),
6619 I.getAAMetadata(), BatchAA);
6620 updateDAGForMaybeTailCall(MM);
6621 return;
6622 }
6623 case Intrinsic::memcpy_element_unordered_atomic: {
6624 auto &MI = cast<AnyMemCpyInst>(I);
6625 SDValue Dst = getValue(MI.getRawDest());
6626 SDValue Src = getValue(MI.getRawSource());
6627 SDValue Length = getValue(MI.getLength());
6628
6629 Type *LengthTy = MI.getLength()->getType();
6630 unsigned ElemSz = MI.getElementSizeInBytes();
6631 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6632 SDValue MC =
6633 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6634 isTC, MachinePointerInfo(MI.getRawDest()),
6635 MachinePointerInfo(MI.getRawSource()));
6636 updateDAGForMaybeTailCall(MC);
6637 return;
6638 }
6639 case Intrinsic::memmove_element_unordered_atomic: {
6640 auto &MI = cast<AnyMemMoveInst>(I);
6641 SDValue Dst = getValue(MI.getRawDest());
6642 SDValue Src = getValue(MI.getRawSource());
6643 SDValue Length = getValue(MI.getLength());
6644
6645 Type *LengthTy = MI.getLength()->getType();
6646 unsigned ElemSz = MI.getElementSizeInBytes();
6647 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6648 SDValue MC =
6649 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6650 isTC, MachinePointerInfo(MI.getRawDest()),
6651 MachinePointerInfo(MI.getRawSource()));
6652 updateDAGForMaybeTailCall(MC);
6653 return;
6654 }
6655 case Intrinsic::memset_element_unordered_atomic: {
6656 auto &MI = cast<AnyMemSetInst>(I);
6657 SDValue Dst = getValue(MI.getRawDest());
6658 SDValue Val = getValue(MI.getValue());
6659 SDValue Length = getValue(MI.getLength());
6660
6661 Type *LengthTy = MI.getLength()->getType();
6662 unsigned ElemSz = MI.getElementSizeInBytes();
6663 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6664 SDValue MC =
6665 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6666 isTC, MachinePointerInfo(MI.getRawDest()));
6667 updateDAGForMaybeTailCall(MC);
6668 return;
6669 }
6670 case Intrinsic::call_preallocated_setup: {
6671 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6672 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6673 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6674 getRoot(), SrcValue);
6675 setValue(&I, Res);
6676 DAG.setRoot(Res);
6677 return;
6678 }
6679 case Intrinsic::call_preallocated_arg: {
6680 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6681 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6682 SDValue Ops[3];
6683 Ops[0] = getRoot();
6684 Ops[1] = SrcValue;
6685 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6686 MVT::i32); // arg index
6687 SDValue Res = DAG.getNode(
6688 ISD::PREALLOCATED_ARG, sdl,
6689 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6690 setValue(&I, Res);
6691 DAG.setRoot(Res.getValue(1));
6692 return;
6693 }
6694
6695 case Intrinsic::eh_typeid_for: {
6696 // Find the type id for the given typeinfo.
6697 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6698 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6699 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6700 setValue(&I, Res);
6701 return;
6702 }
6703
6704 case Intrinsic::eh_return_i32:
6705 case Intrinsic::eh_return_i64:
6706 DAG.getMachineFunction().setCallsEHReturn(true);
6707 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6708 MVT::Other,
6710 getValue(I.getArgOperand(0)),
6711 getValue(I.getArgOperand(1))));
6712 return;
6713 case Intrinsic::eh_unwind_init:
6714 DAG.getMachineFunction().setCallsUnwindInit(true);
6715 return;
6716 case Intrinsic::eh_dwarf_cfa:
6717 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6718 TLI.getPointerTy(DAG.getDataLayout()),
6719 getValue(I.getArgOperand(0))));
6720 return;
6721 case Intrinsic::eh_sjlj_callsite: {
6722 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6723 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6724
6725 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6726 return;
6727 }
6728 case Intrinsic::eh_sjlj_functioncontext: {
6729 // Get and store the index of the function context.
6730 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6731 AllocaInst *FnCtx =
6732 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6733 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6735 return;
6736 }
6737 case Intrinsic::eh_sjlj_setjmp: {
6738 SDValue Ops[2];
6739 Ops[0] = getRoot();
6740 Ops[1] = getValue(I.getArgOperand(0));
6741 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6742 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6743 setValue(&I, Op.getValue(0));
6744 DAG.setRoot(Op.getValue(1));
6745 return;
6746 }
6747 case Intrinsic::eh_sjlj_longjmp:
6748 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6749 getRoot(), getValue(I.getArgOperand(0))));
6750 return;
6751 case Intrinsic::eh_sjlj_setup_dispatch:
6752 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6753 getRoot()));
6754 return;
6755 case Intrinsic::masked_gather:
6756 visitMaskedGather(I);
6757 return;
6758 case Intrinsic::masked_load:
6759 visitMaskedLoad(I);
6760 return;
6761 case Intrinsic::masked_scatter:
6762 visitMaskedScatter(I);
6763 return;
6764 case Intrinsic::masked_store:
6765 visitMaskedStore(I);
6766 return;
6767 case Intrinsic::masked_expandload:
6768 visitMaskedLoad(I, true /* IsExpanding */);
6769 return;
6770 case Intrinsic::masked_compressstore:
6771 visitMaskedStore(I, true /* IsCompressing */);
6772 return;
6773 case Intrinsic::powi:
6774 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6775 getValue(I.getArgOperand(1)), DAG));
6776 return;
6777 case Intrinsic::log:
6778 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6779 return;
6780 case Intrinsic::log2:
6781 setValue(&I,
6782 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6783 return;
6784 case Intrinsic::log10:
6785 setValue(&I,
6786 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6787 return;
6788 case Intrinsic::exp:
6789 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6790 return;
6791 case Intrinsic::exp2:
6792 setValue(&I,
6793 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6794 return;
6795 case Intrinsic::pow:
6796 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6797 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6798 return;
6799 case Intrinsic::sqrt:
6800 case Intrinsic::fabs:
6801 case Intrinsic::sin:
6802 case Intrinsic::cos:
6803 case Intrinsic::tan:
6804 case Intrinsic::asin:
6805 case Intrinsic::acos:
6806 case Intrinsic::atan:
6807 case Intrinsic::sinh:
6808 case Intrinsic::cosh:
6809 case Intrinsic::tanh:
6810 case Intrinsic::exp10:
6811 case Intrinsic::floor:
6812 case Intrinsic::ceil:
6813 case Intrinsic::trunc:
6814 case Intrinsic::rint:
6815 case Intrinsic::nearbyint:
6816 case Intrinsic::round:
6817 case Intrinsic::roundeven:
6818 case Intrinsic::canonicalize: {
6819 unsigned Opcode;
6820 // clang-format off
6821 switch (Intrinsic) {
6822 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6823 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6824 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6825 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6826 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6827 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6828 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6829 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6830 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6831 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6832 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6833 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6834 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6835 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6836 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6837 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6838 case Intrinsic::rint: Opcode = ISD::FRINT; break;
6839 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6840 case Intrinsic::round: Opcode = ISD::FROUND; break;
6841 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6842 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6843 }
6844 // clang-format on
6845
6846 setValue(&I, DAG.getNode(Opcode, sdl,
6847 getValue(I.getArgOperand(0)).getValueType(),
6848 getValue(I.getArgOperand(0)), Flags));
6849 return;
6850 }
6851 case Intrinsic::atan2:
6852 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
6853 getValue(I.getArgOperand(0)).getValueType(),
6854 getValue(I.getArgOperand(0)),
6855 getValue(I.getArgOperand(1)), Flags));
6856 return;
6857 case Intrinsic::lround:
6858 case Intrinsic::llround:
6859 case Intrinsic::lrint:
6860 case Intrinsic::llrint: {
6861 unsigned Opcode;
6862 // clang-format off
6863 switch (Intrinsic) {
6864 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6865 case Intrinsic::lround: Opcode = ISD::LROUND; break;
6866 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6867 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
6868 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
6869 }
6870 // clang-format on
6871
6872 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6873 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6874 getValue(I.getArgOperand(0))));
6875 return;
6876 }
6877 case Intrinsic::minnum:
6878 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6879 getValue(I.getArgOperand(0)).getValueType(),
6880 getValue(I.getArgOperand(0)),
6881 getValue(I.getArgOperand(1)), Flags));
6882 return;
6883 case Intrinsic::maxnum:
6884 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6885 getValue(I.getArgOperand(0)).getValueType(),
6886 getValue(I.getArgOperand(0)),
6887 getValue(I.getArgOperand(1)), Flags));
6888 return;
6889 case Intrinsic::minimum:
6890 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
6891 getValue(I.getArgOperand(0)).getValueType(),
6892 getValue(I.getArgOperand(0)),
6893 getValue(I.getArgOperand(1)), Flags));
6894 return;
6895 case Intrinsic::maximum:
6896 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
6897 getValue(I.getArgOperand(0)).getValueType(),
6898 getValue(I.getArgOperand(0)),
6899 getValue(I.getArgOperand(1)), Flags));
6900 return;
6901 case Intrinsic::minimumnum:
6902 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
6903 getValue(I.getArgOperand(0)).getValueType(),
6904 getValue(I.getArgOperand(0)),
6905 getValue(I.getArgOperand(1)), Flags));
6906 return;
6907 case Intrinsic::maximumnum:
6908 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
6909 getValue(I.getArgOperand(0)).getValueType(),
6910 getValue(I.getArgOperand(0)),
6911 getValue(I.getArgOperand(1)), Flags));
6912 return;
6913 case Intrinsic::copysign:
6914 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
6915 getValue(I.getArgOperand(0)).getValueType(),
6916 getValue(I.getArgOperand(0)),
6917 getValue(I.getArgOperand(1)), Flags));
6918 return;
6919 case Intrinsic::ldexp:
6920 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
6921 getValue(I.getArgOperand(0)).getValueType(),
6922 getValue(I.getArgOperand(0)),
6923 getValue(I.getArgOperand(1)), Flags));
6924 return;
6925 case Intrinsic::modf:
6926 case Intrinsic::sincos:
6927 case Intrinsic::sincospi:
6928 case Intrinsic::frexp: {
6929 unsigned Opcode;
6930 switch (Intrinsic) {
6931 default:
6932 llvm_unreachable("unexpected intrinsic");
6933 case Intrinsic::sincos:
6934 Opcode = ISD::FSINCOS;
6935 break;
6936 case Intrinsic::sincospi:
6937 Opcode = ISD::FSINCOSPI;
6938 break;
6939 case Intrinsic::modf:
6940 Opcode = ISD::FMODF;
6941 break;
6942 case Intrinsic::frexp:
6943 Opcode = ISD::FFREXP;
6944 break;
6945 }
6946 SmallVector<EVT, 2> ValueVTs;
6947 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
6948 SDVTList VTs = DAG.getVTList(ValueVTs);
6949 setValue(
6950 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
6951 return;
6952 }
6953 case Intrinsic::arithmetic_fence: {
6954 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
6955 getValue(I.getArgOperand(0)).getValueType(),
6956 getValue(I.getArgOperand(0)), Flags));
6957 return;
6958 }
6959 case Intrinsic::fma:
6960 setValue(&I, DAG.getNode(
6961 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
6962 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
6963 getValue(I.getArgOperand(2)), Flags));
6964 return;
6965#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
6966 case Intrinsic::INTRINSIC:
6967#include "llvm/IR/ConstrainedOps.def"
6968 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
6969 return;
6970#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
6971#include "llvm/IR/VPIntrinsics.def"
6972 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
6973 return;
6974 case Intrinsic::fptrunc_round: {
6975 // Get the last argument, the metadata and convert it to an integer in the
6976 // call
6977 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
6978 std::optional<RoundingMode> RoundMode =
6979 convertStrToRoundingMode(cast<MDString>(MD)->getString());
6980
6981 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6982
6983 // Propagate fast-math-flags from IR to node(s).
6984 SDNodeFlags Flags;
6985 Flags.copyFMF(*cast<FPMathOperator>(&I));
6986 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
6987
6989 Result = DAG.getNode(
6990 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
6991 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
6992 setValue(&I, Result);
6993
6994 return;
6995 }
6996 case Intrinsic::fmuladd: {
6997 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6998 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
6999 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
7000 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7001 getValue(I.getArgOperand(0)).getValueType(),
7002 getValue(I.getArgOperand(0)),
7003 getValue(I.getArgOperand(1)),
7004 getValue(I.getArgOperand(2)), Flags));
7005 } else if (TLI.isOperationLegalOrCustom(ISD::FMULADD, VT)) {
7006 // TODO: Support splitting the vector.
7007 setValue(&I, DAG.getNode(ISD::FMULADD, 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 {
7013 // TODO: Intrinsic calls should have fast-math-flags.
7014 SDValue Mul = DAG.getNode(
7015 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7016 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7017 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7018 getValue(I.getArgOperand(0)).getValueType(),
7019 Mul, getValue(I.getArgOperand(2)), Flags);
7020 setValue(&I, Add);
7021 }
7022 return;
7023 }
7024 case Intrinsic::convert_to_fp16:
7025 setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16,
7026 DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16,
7027 getValue(I.getArgOperand(0)),
7028 DAG.getTargetConstant(0, sdl,
7029 MVT::i32))));
7030 return;
7031 case Intrinsic::convert_from_fp16:
7032 setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl,
7033 TLI.getValueType(DAG.getDataLayout(), I.getType()),
7034 DAG.getNode(ISD::BITCAST, sdl, MVT::f16,
7035 getValue(I.getArgOperand(0)))));
7036 return;
7037 case Intrinsic::fptosi_sat: {
7038 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7039 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7040 getValue(I.getArgOperand(0)),
7041 DAG.getValueType(VT.getScalarType())));
7042 return;
7043 }
7044 case Intrinsic::fptoui_sat: {
7045 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7046 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7047 getValue(I.getArgOperand(0)),
7048 DAG.getValueType(VT.getScalarType())));
7049 return;
7050 }
7051 case Intrinsic::set_rounding:
7052 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7053 {getRoot(), getValue(I.getArgOperand(0))});
7054 setValue(&I, Res);
7055 DAG.setRoot(Res.getValue(0));
7056 return;
7057 case Intrinsic::is_fpclass: {
7058 const DataLayout DLayout = DAG.getDataLayout();
7059 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7060 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7061 FPClassTest Test = static_cast<FPClassTest>(
7062 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7063 MachineFunction &MF = DAG.getMachineFunction();
7064 const Function &F = MF.getFunction();
7065 SDValue Op = getValue(I.getArgOperand(0));
7066 SDNodeFlags Flags;
7067 Flags.setNoFPExcept(
7068 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7069 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7070 // expansion can use illegal types. Making expansion early allows
7071 // legalizing these types prior to selection.
7072 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7073 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7074 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7075 setValue(&I, Result);
7076 return;
7077 }
7078
7079 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7080 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7081 setValue(&I, V);
7082 return;
7083 }
7084 case Intrinsic::get_fpenv: {
7085 const DataLayout DLayout = DAG.getDataLayout();
7086 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7087 Align TempAlign = DAG.getEVTAlign(EnvVT);
7088 SDValue Chain = getRoot();
7089 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7090 // and temporary storage in stack.
7091 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7092 Res = DAG.getNode(
7093 ISD::GET_FPENV, sdl,
7094 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7095 MVT::Other),
7096 Chain);
7097 } else {
7098 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7099 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7100 auto MPI =
7101 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7102 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7104 TempAlign);
7105 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7106 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7107 }
7108 setValue(&I, Res);
7109 DAG.setRoot(Res.getValue(1));
7110 return;
7111 }
7112 case Intrinsic::set_fpenv: {
7113 const DataLayout DLayout = DAG.getDataLayout();
7114 SDValue Env = getValue(I.getArgOperand(0));
7115 EVT EnvVT = Env.getValueType();
7116 Align TempAlign = DAG.getEVTAlign(EnvVT);
7117 SDValue Chain = getRoot();
7118 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7119 // environment from memory.
7120 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7121 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7122 } else {
7123 // Allocate space in stack, copy environment bits into it and use this
7124 // memory in SET_FPENV_MEM.
7125 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7126 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7127 auto MPI =
7128 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7129 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7131 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7133 TempAlign);
7134 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7135 }
7136 DAG.setRoot(Chain);
7137 return;
7138 }
7139 case Intrinsic::reset_fpenv:
7140 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7141 return;
7142 case Intrinsic::get_fpmode:
7143 Res = DAG.getNode(
7144 ISD::GET_FPMODE, sdl,
7145 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7146 MVT::Other),
7147 DAG.getRoot());
7148 setValue(&I, Res);
7149 DAG.setRoot(Res.getValue(1));
7150 return;
7151 case Intrinsic::set_fpmode:
7152 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7153 getValue(I.getArgOperand(0)));
7154 DAG.setRoot(Res);
7155 return;
7156 case Intrinsic::reset_fpmode: {
7157 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7158 DAG.setRoot(Res);
7159 return;
7160 }
7161 case Intrinsic::pcmarker: {
7162 SDValue Tmp = getValue(I.getArgOperand(0));
7163 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7164 return;
7165 }
7166 case Intrinsic::readcyclecounter: {
7167 SDValue Op = getRoot();
7168 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7169 DAG.getVTList(MVT::i64, MVT::Other), Op);
7170 setValue(&I, Res);
7171 DAG.setRoot(Res.getValue(1));
7172 return;
7173 }
7174 case Intrinsic::readsteadycounter: {
7175 SDValue Op = getRoot();
7176 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7177 DAG.getVTList(MVT::i64, MVT::Other), Op);
7178 setValue(&I, Res);
7179 DAG.setRoot(Res.getValue(1));
7180 return;
7181 }
7182 case Intrinsic::bitreverse:
7183 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7184 getValue(I.getArgOperand(0)).getValueType(),
7185 getValue(I.getArgOperand(0))));
7186 return;
7187 case Intrinsic::bswap:
7188 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7189 getValue(I.getArgOperand(0)).getValueType(),
7190 getValue(I.getArgOperand(0))));
7191 return;
7192 case Intrinsic::cttz: {
7193 SDValue Arg = getValue(I.getArgOperand(0));
7194 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7195 EVT Ty = Arg.getValueType();
7196 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
7197 sdl, Ty, Arg));
7198 return;
7199 }
7200 case Intrinsic::ctlz: {
7201 SDValue Arg = getValue(I.getArgOperand(0));
7202 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7203 EVT Ty = Arg.getValueType();
7204 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
7205 sdl, Ty, Arg));
7206 return;
7207 }
7208 case Intrinsic::ctpop: {
7209 SDValue Arg = getValue(I.getArgOperand(0));
7210 EVT Ty = Arg.getValueType();
7211 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7212 return;
7213 }
7214 case Intrinsic::fshl:
7215 case Intrinsic::fshr: {
7216 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7217 SDValue X = getValue(I.getArgOperand(0));
7218 SDValue Y = getValue(I.getArgOperand(1));
7219 SDValue Z = getValue(I.getArgOperand(2));
7220 EVT VT = X.getValueType();
7221
7222 if (X == Y) {
7223 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7224 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7225 } else {
7226 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7227 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7228 }
7229 return;
7230 }
7231 case Intrinsic::sadd_sat: {
7232 SDValue Op1 = getValue(I.getArgOperand(0));
7233 SDValue Op2 = getValue(I.getArgOperand(1));
7234 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7235 return;
7236 }
7237 case Intrinsic::uadd_sat: {
7238 SDValue Op1 = getValue(I.getArgOperand(0));
7239 SDValue Op2 = getValue(I.getArgOperand(1));
7240 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7241 return;
7242 }
7243 case Intrinsic::ssub_sat: {
7244 SDValue Op1 = getValue(I.getArgOperand(0));
7245 SDValue Op2 = getValue(I.getArgOperand(1));
7246 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7247 return;
7248 }
7249 case Intrinsic::usub_sat: {
7250 SDValue Op1 = getValue(I.getArgOperand(0));
7251 SDValue Op2 = getValue(I.getArgOperand(1));
7252 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7253 return;
7254 }
7255 case Intrinsic::sshl_sat: {
7256 SDValue Op1 = getValue(I.getArgOperand(0));
7257 SDValue Op2 = getValue(I.getArgOperand(1));
7258 setValue(&I, DAG.getNode(ISD::SSHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7259 return;
7260 }
7261 case Intrinsic::ushl_sat: {
7262 SDValue Op1 = getValue(I.getArgOperand(0));
7263 SDValue Op2 = getValue(I.getArgOperand(1));
7264 setValue(&I, DAG.getNode(ISD::USHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7265 return;
7266 }
7267 case Intrinsic::smul_fix:
7268 case Intrinsic::umul_fix:
7269 case Intrinsic::smul_fix_sat:
7270 case Intrinsic::umul_fix_sat: {
7271 SDValue Op1 = getValue(I.getArgOperand(0));
7272 SDValue Op2 = getValue(I.getArgOperand(1));
7273 SDValue Op3 = getValue(I.getArgOperand(2));
7274 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7275 Op1.getValueType(), Op1, Op2, Op3));
7276 return;
7277 }
7278 case Intrinsic::sdiv_fix:
7279 case Intrinsic::udiv_fix:
7280 case Intrinsic::sdiv_fix_sat:
7281 case Intrinsic::udiv_fix_sat: {
7282 SDValue Op1 = getValue(I.getArgOperand(0));
7283 SDValue Op2 = getValue(I.getArgOperand(1));
7284 SDValue Op3 = getValue(I.getArgOperand(2));
7286 Op1, Op2, Op3, DAG, TLI));
7287 return;
7288 }
7289 case Intrinsic::smax: {
7290 SDValue Op1 = getValue(I.getArgOperand(0));
7291 SDValue Op2 = getValue(I.getArgOperand(1));
7292 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7293 return;
7294 }
7295 case Intrinsic::smin: {
7296 SDValue Op1 = getValue(I.getArgOperand(0));
7297 SDValue Op2 = getValue(I.getArgOperand(1));
7298 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7299 return;
7300 }
7301 case Intrinsic::umax: {
7302 SDValue Op1 = getValue(I.getArgOperand(0));
7303 SDValue Op2 = getValue(I.getArgOperand(1));
7304 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7305 return;
7306 }
7307 case Intrinsic::umin: {
7308 SDValue Op1 = getValue(I.getArgOperand(0));
7309 SDValue Op2 = getValue(I.getArgOperand(1));
7310 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7311 return;
7312 }
7313 case Intrinsic::abs: {
7314 // TODO: Preserve "int min is poison" arg in SDAG?
7315 SDValue Op1 = getValue(I.getArgOperand(0));
7316 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7317 return;
7318 }
7319 case Intrinsic::scmp: {
7320 SDValue Op1 = getValue(I.getArgOperand(0));
7321 SDValue Op2 = getValue(I.getArgOperand(1));
7322 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7323 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7324 break;
7325 }
7326 case Intrinsic::ucmp: {
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::UCMP, sdl, DestVT, Op1, Op2));
7331 break;
7332 }
7333 case Intrinsic::stacksave: {
7334 SDValue Op = getRoot();
7335 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7336 Res = DAG.getNode(ISD::STACKSAVE, sdl, DAG.getVTList(VT, MVT::Other), Op);
7337 setValue(&I, Res);
7338 DAG.setRoot(Res.getValue(1));
7339 return;
7340 }
7341 case Intrinsic::stackrestore:
7342 Res = getValue(I.getArgOperand(0));
7343 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7344 return;
7345 case Intrinsic::get_dynamic_area_offset: {
7346 SDValue Op = getRoot();
7347 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7348 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7349 Op);
7350 DAG.setRoot(Op);
7351 setValue(&I, Res);
7352 return;
7353 }
7354 case Intrinsic::stackguard: {
7355 MachineFunction &MF = DAG.getMachineFunction();
7356 const Module &M = *MF.getFunction().getParent();
7357 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7358 SDValue Chain = getRoot();
7359 if (TLI.useLoadStackGuardNode(M)) {
7360 Res = getLoadStackGuard(DAG, sdl, Chain);
7361 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7362 } else {
7363 const Value *Global = TLI.getSDagStackGuard(M);
7364 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7365 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7366 MachinePointerInfo(Global, 0), Align,
7368 }
7369 if (TLI.useStackGuardXorFP())
7370 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7371 DAG.setRoot(Chain);
7372 setValue(&I, Res);
7373 return;
7374 }
7375 case Intrinsic::stackprotector: {
7376 // Emit code into the DAG to store the stack guard onto the stack.
7377 MachineFunction &MF = DAG.getMachineFunction();
7378 MachineFrameInfo &MFI = MF.getFrameInfo();
7379 const Module &M = *MF.getFunction().getParent();
7380 SDValue Src, Chain = getRoot();
7381
7382 if (TLI.useLoadStackGuardNode(M))
7383 Src = getLoadStackGuard(DAG, sdl, Chain);
7384 else
7385 Src = getValue(I.getArgOperand(0)); // The guard's value.
7386
7387 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7388
7389 int FI = FuncInfo.StaticAllocaMap[Slot];
7390 MFI.setStackProtectorIndex(FI);
7391 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7392
7393 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7394
7395 // Store the stack protector onto the stack.
7396 Res = DAG.getStore(
7397 Chain, sdl, Src, FIN,
7398 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7399 MaybeAlign(), MachineMemOperand::MOVolatile);
7400 setValue(&I, Res);
7401 DAG.setRoot(Res);
7402 return;
7403 }
7404 case Intrinsic::objectsize:
7405 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7406
7407 case Intrinsic::is_constant:
7408 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7409
7410 case Intrinsic::annotation:
7411 case Intrinsic::ptr_annotation:
7412 case Intrinsic::launder_invariant_group:
7413 case Intrinsic::strip_invariant_group:
7414 // Drop the intrinsic, but forward the value
7415 setValue(&I, getValue(I.getOperand(0)));
7416 return;
7417
7418 case Intrinsic::type_test:
7419 case Intrinsic::public_type_test:
7420 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7421 return;
7422
7423 case Intrinsic::assume:
7424 case Intrinsic::experimental_noalias_scope_decl:
7425 case Intrinsic::var_annotation:
7426 case Intrinsic::sideeffect:
7427 // Discard annotate attributes, noalias scope declarations, assumptions, and
7428 // artificial side-effects.
7429 return;
7430
7431 case Intrinsic::codeview_annotation: {
7432 // Emit a label associated with this metadata.
7433 MachineFunction &MF = DAG.getMachineFunction();
7434 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7435 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7436 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7437 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7438 DAG.setRoot(Res);
7439 return;
7440 }
7441
7442 case Intrinsic::init_trampoline: {
7443 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7444
7445 SDValue Ops[6];
7446 Ops[0] = getRoot();
7447 Ops[1] = getValue(I.getArgOperand(0));
7448 Ops[2] = getValue(I.getArgOperand(1));
7449 Ops[3] = getValue(I.getArgOperand(2));
7450 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7451 Ops[5] = DAG.getSrcValue(F);
7452
7453 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7454
7455 DAG.setRoot(Res);
7456 return;
7457 }
7458 case Intrinsic::adjust_trampoline:
7459 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7460 TLI.getPointerTy(DAG.getDataLayout()),
7461 getValue(I.getArgOperand(0))));
7462 return;
7463 case Intrinsic::gcroot: {
7464 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7465 "only valid in functions with gc specified, enforced by Verifier");
7466 assert(GFI && "implied by previous");
7467 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7468 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7469
7470 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7471 GFI->addStackRoot(FI->getIndex(), TypeMap);
7472 return;
7473 }
7474 case Intrinsic::gcread:
7475 case Intrinsic::gcwrite:
7476 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7477 case Intrinsic::get_rounding:
7478 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7479 setValue(&I, Res);
7480 DAG.setRoot(Res.getValue(1));
7481 return;
7482
7483 case Intrinsic::expect:
7484 case Intrinsic::expect_with_probability:
7485 // Just replace __builtin_expect(exp, c) and
7486 // __builtin_expect_with_probability(exp, c, p) with EXP.
7487 setValue(&I, getValue(I.getArgOperand(0)));
7488 return;
7489
7490 case Intrinsic::ubsantrap:
7491 case Intrinsic::debugtrap:
7492 case Intrinsic::trap: {
7493 StringRef TrapFuncName =
7494 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7495 if (TrapFuncName.empty()) {
7496 switch (Intrinsic) {
7497 case Intrinsic::trap:
7498 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7499 break;
7500 case Intrinsic::debugtrap:
7501 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7502 break;
7503 case Intrinsic::ubsantrap:
7504 DAG.setRoot(DAG.getNode(
7505 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7506 DAG.getTargetConstant(
7507 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7508 MVT::i32)));
7509 break;
7510 default: llvm_unreachable("unknown trap intrinsic");
7511 }
7512 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7513 I.hasFnAttr(Attribute::NoMerge));
7514 return;
7515 }
7517 if (Intrinsic == Intrinsic::ubsantrap) {
7518 Value *Arg = I.getArgOperand(0);
7519 Args.emplace_back(Arg, getValue(Arg));
7520 }
7521
7522 TargetLowering::CallLoweringInfo CLI(DAG);
7523 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7524 CallingConv::C, I.getType(),
7525 DAG.getExternalSymbol(TrapFuncName.data(),
7526 TLI.getPointerTy(DAG.getDataLayout())),
7527 std::move(Args));
7528 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7529 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7530 DAG.setRoot(Result.second);
7531 return;
7532 }
7533
7534 case Intrinsic::allow_runtime_check:
7535 case Intrinsic::allow_ubsan_check:
7536 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7537 return;
7538
7539 case Intrinsic::uadd_with_overflow:
7540 case Intrinsic::sadd_with_overflow:
7541 case Intrinsic::usub_with_overflow:
7542 case Intrinsic::ssub_with_overflow:
7543 case Intrinsic::umul_with_overflow:
7544 case Intrinsic::smul_with_overflow: {
7546 switch (Intrinsic) {
7547 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7548 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7549 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7550 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7551 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7552 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7553 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7554 }
7555 SDValue Op1 = getValue(I.getArgOperand(0));
7556 SDValue Op2 = getValue(I.getArgOperand(1));
7557
7558 EVT ResultVT = Op1.getValueType();
7559 EVT OverflowVT = MVT::i1;
7560 if (ResultVT.isVector())
7561 OverflowVT = EVT::getVectorVT(
7562 *Context, OverflowVT, ResultVT.getVectorElementCount());
7563
7564 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7565 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7566 return;
7567 }
7568 case Intrinsic::prefetch: {
7569 SDValue Ops[5];
7570 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7572 Ops[0] = DAG.getRoot();
7573 Ops[1] = getValue(I.getArgOperand(0));
7574 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7575 MVT::i32);
7576 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7577 MVT::i32);
7578 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7579 MVT::i32);
7580 SDValue Result = DAG.getMemIntrinsicNode(
7581 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7582 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7583 /* align */ std::nullopt, Flags);
7584
7585 // Chain the prefetch in parallel with any pending loads, to stay out of
7586 // the way of later optimizations.
7587 PendingLoads.push_back(Result);
7588 Result = getRoot();
7589 DAG.setRoot(Result);
7590 return;
7591 }
7592 case Intrinsic::lifetime_start:
7593 case Intrinsic::lifetime_end: {
7594 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7595 // Stack coloring is not enabled in O0, discard region information.
7596 if (TM.getOptLevel() == CodeGenOptLevel::None)
7597 return;
7598
7599 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7600 if (!LifetimeObject)
7601 return;
7602
7603 // First check that the Alloca is static, otherwise it won't have a
7604 // valid frame index.
7605 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7606 if (SI == FuncInfo.StaticAllocaMap.end())
7607 return;
7608
7609 const int FrameIndex = SI->second;
7610 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7611 DAG.setRoot(Res);
7612 return;
7613 }
7614 case Intrinsic::pseudoprobe: {
7615 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7616 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7617 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7618 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7619 DAG.setRoot(Res);
7620 return;
7621 }
7622 case Intrinsic::invariant_start:
7623 // Discard region information.
7624 setValue(&I,
7625 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7626 return;
7627 case Intrinsic::invariant_end:
7628 // Discard region information.
7629 return;
7630 case Intrinsic::clear_cache: {
7631 SDValue InputChain = DAG.getRoot();
7632 SDValue StartVal = getValue(I.getArgOperand(0));
7633 SDValue EndVal = getValue(I.getArgOperand(1));
7634 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7635 {InputChain, StartVal, EndVal});
7636 setValue(&I, Res);
7637 DAG.setRoot(Res);
7638 return;
7639 }
7640 case Intrinsic::donothing:
7641 case Intrinsic::seh_try_begin:
7642 case Intrinsic::seh_scope_begin:
7643 case Intrinsic::seh_try_end:
7644 case Intrinsic::seh_scope_end:
7645 // ignore
7646 return;
7647 case Intrinsic::experimental_stackmap:
7648 visitStackmap(I);
7649 return;
7650 case Intrinsic::experimental_patchpoint_void:
7651 case Intrinsic::experimental_patchpoint:
7652 visitPatchpoint(I);
7653 return;
7654 case Intrinsic::experimental_gc_statepoint:
7656 return;
7657 case Intrinsic::experimental_gc_result:
7658 visitGCResult(cast<GCResultInst>(I));
7659 return;
7660 case Intrinsic::experimental_gc_relocate:
7661 visitGCRelocate(cast<GCRelocateInst>(I));
7662 return;
7663 case Intrinsic::instrprof_cover:
7664 llvm_unreachable("instrprof failed to lower a cover");
7665 case Intrinsic::instrprof_increment:
7666 llvm_unreachable("instrprof failed to lower an increment");
7667 case Intrinsic::instrprof_timestamp:
7668 llvm_unreachable("instrprof failed to lower a timestamp");
7669 case Intrinsic::instrprof_value_profile:
7670 llvm_unreachable("instrprof failed to lower a value profiling call");
7671 case Intrinsic::instrprof_mcdc_parameters:
7672 llvm_unreachable("instrprof failed to lower mcdc parameters");
7673 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7674 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7675 case Intrinsic::localescape: {
7676 MachineFunction &MF = DAG.getMachineFunction();
7677 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7678
7679 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7680 // is the same on all targets.
7681 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7682 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7683 if (isa<ConstantPointerNull>(Arg))
7684 continue; // Skip null pointers. They represent a hole in index space.
7685 AllocaInst *Slot = cast<AllocaInst>(Arg);
7686 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7687 "can only escape static allocas");
7688 int FI = FuncInfo.StaticAllocaMap[Slot];
7689 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7691 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7692 TII->get(TargetOpcode::LOCAL_ESCAPE))
7693 .addSym(FrameAllocSym)
7694 .addFrameIndex(FI);
7695 }
7696
7697 return;
7698 }
7699
7700 case Intrinsic::localrecover: {
7701 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7702 MachineFunction &MF = DAG.getMachineFunction();
7703
7704 // Get the symbol that defines the frame offset.
7705 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7706 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7707 unsigned IdxVal =
7708 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7709 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7711
7712 Value *FP = I.getArgOperand(1);
7713 SDValue FPVal = getValue(FP);
7714 EVT PtrVT = FPVal.getValueType();
7715
7716 // Create a MCSymbol for the label to avoid any target lowering
7717 // that would make this PC relative.
7718 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7719 SDValue OffsetVal =
7720 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7721
7722 // Add the offset to the FP.
7723 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7724 setValue(&I, Add);
7725
7726 return;
7727 }
7728
7729 case Intrinsic::fake_use: {
7730 Value *V = I.getArgOperand(0);
7731 SDValue Ops[2];
7732 // For Values not declared or previously used in this basic block, the
7733 // NodeMap will not have an entry, and `getValue` will assert if V has no
7734 // valid register value.
7735 auto FakeUseValue = [&]() -> SDValue {
7736 SDValue &N = NodeMap[V];
7737 if (N.getNode())
7738 return N;
7739
7740 // If there's a virtual register allocated and initialized for this
7741 // value, use it.
7742 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7743 return copyFromReg;
7744 // FIXME: Do we want to preserve constants? It seems pointless.
7745 if (isa<Constant>(V))
7746 return getValue(V);
7747 return SDValue();
7748 }();
7749 if (!FakeUseValue || FakeUseValue.isUndef())
7750 return;
7751 Ops[0] = getRoot();
7752 Ops[1] = FakeUseValue;
7753 // Also, do not translate a fake use with an undef operand, or any other
7754 // empty SDValues.
7755 if (!Ops[1] || Ops[1].isUndef())
7756 return;
7757 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7758 return;
7759 }
7760
7761 case Intrinsic::eh_exceptionpointer:
7762 case Intrinsic::eh_exceptioncode: {
7763 // Get the exception pointer vreg, copy from it, and resize it to fit.
7764 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7765 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7766 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7767 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7768 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7769 if (Intrinsic == Intrinsic::eh_exceptioncode)
7770 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7771 setValue(&I, N);
7772 return;
7773 }
7774 case Intrinsic::xray_customevent: {
7775 // Here we want to make sure that the intrinsic behaves as if it has a
7776 // specific calling convention.
7777 const auto &Triple = DAG.getTarget().getTargetTriple();
7778 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7779 return;
7780
7782
7783 // We want to say that we always want the arguments in registers.
7784 SDValue LogEntryVal = getValue(I.getArgOperand(0));
7785 SDValue StrSizeVal = getValue(I.getArgOperand(1));
7786 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7787 SDValue Chain = getRoot();
7788 Ops.push_back(LogEntryVal);
7789 Ops.push_back(StrSizeVal);
7790 Ops.push_back(Chain);
7791
7792 // We need to enforce the calling convention for the callsite, so that
7793 // argument ordering is enforced correctly, and that register allocation can
7794 // see that some registers may be assumed clobbered and have to preserve
7795 // them across calls to the intrinsic.
7796 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7797 sdl, NodeTys, Ops);
7798 SDValue patchableNode = SDValue(MN, 0);
7799 DAG.setRoot(patchableNode);
7800 setValue(&I, patchableNode);
7801 return;
7802 }
7803 case Intrinsic::xray_typedevent: {
7804 // Here we want to make sure that the intrinsic behaves as if it has a
7805 // specific calling convention.
7806 const auto &Triple = DAG.getTarget().getTargetTriple();
7807 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7808 return;
7809
7811
7812 // We want to say that we always want the arguments in registers.
7813 // It's unclear to me how manipulating the selection DAG here forces callers
7814 // to provide arguments in registers instead of on the stack.
7815 SDValue LogTypeId = getValue(I.getArgOperand(0));
7816 SDValue LogEntryVal = getValue(I.getArgOperand(1));
7817 SDValue StrSizeVal = getValue(I.getArgOperand(2));
7818 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7819 SDValue Chain = getRoot();
7820 Ops.push_back(LogTypeId);
7821 Ops.push_back(LogEntryVal);
7822 Ops.push_back(StrSizeVal);
7823 Ops.push_back(Chain);
7824
7825 // We need to enforce the calling convention for the callsite, so that
7826 // argument ordering is enforced correctly, and that register allocation can
7827 // see that some registers may be assumed clobbered and have to preserve
7828 // them across calls to the intrinsic.
7829 MachineSDNode *MN = DAG.getMachineNode(
7830 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
7831 SDValue patchableNode = SDValue(MN, 0);
7832 DAG.setRoot(patchableNode);
7833 setValue(&I, patchableNode);
7834 return;
7835 }
7836 case Intrinsic::experimental_deoptimize:
7838 return;
7839 case Intrinsic::stepvector:
7840 visitStepVector(I);
7841 return;
7842 case Intrinsic::vector_reduce_fadd:
7843 case Intrinsic::vector_reduce_fmul:
7844 case Intrinsic::vector_reduce_add:
7845 case Intrinsic::vector_reduce_mul:
7846 case Intrinsic::vector_reduce_and:
7847 case Intrinsic::vector_reduce_or:
7848 case Intrinsic::vector_reduce_xor:
7849 case Intrinsic::vector_reduce_smax:
7850 case Intrinsic::vector_reduce_smin:
7851 case Intrinsic::vector_reduce_umax:
7852 case Intrinsic::vector_reduce_umin:
7853 case Intrinsic::vector_reduce_fmax:
7854 case Intrinsic::vector_reduce_fmin:
7855 case Intrinsic::vector_reduce_fmaximum:
7856 case Intrinsic::vector_reduce_fminimum:
7857 visitVectorReduce(I, Intrinsic);
7858 return;
7859
7860 case Intrinsic::icall_branch_funnel: {
7862 Ops.push_back(getValue(I.getArgOperand(0)));
7863
7864 int64_t Offset;
7866 I.getArgOperand(1), Offset, DAG.getDataLayout()));
7867 if (!Base)
7869 "llvm.icall.branch.funnel operand must be a GlobalValue");
7870 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
7871
7872 struct BranchFunnelTarget {
7873 int64_t Offset;
7875 };
7877
7878 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
7880 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
7881 if (ElemBase != Base)
7882 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
7883 "to the same GlobalValue");
7884
7885 SDValue Val = getValue(I.getArgOperand(Op + 1));
7886 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
7887 if (!GA)
7889 "llvm.icall.branch.funnel operand must be a GlobalValue");
7890 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
7891 GA->getGlobal(), sdl, Val.getValueType(),
7892 GA->getOffset())});
7893 }
7894 llvm::sort(Targets,
7895 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
7896 return T1.Offset < T2.Offset;
7897 });
7898
7899 for (auto &T : Targets) {
7900 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
7901 Ops.push_back(T.Target);
7902 }
7903
7904 Ops.push_back(DAG.getRoot()); // Chain
7905 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
7906 MVT::Other, Ops),
7907 0);
7908 DAG.setRoot(N);
7909 setValue(&I, N);
7910 HasTailCall = true;
7911 return;
7912 }
7913
7914 case Intrinsic::wasm_landingpad_index:
7915 // Information this intrinsic contained has been transferred to
7916 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
7917 // delete it now.
7918 return;
7919
7920 case Intrinsic::aarch64_settag:
7921 case Intrinsic::aarch64_settag_zero: {
7922 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
7923 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
7925 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
7926 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
7927 ZeroMemory);
7928 DAG.setRoot(Val);
7929 setValue(&I, Val);
7930 return;
7931 }
7932 case Intrinsic::amdgcn_cs_chain: {
7933 // At this point we don't care if it's amdgpu_cs_chain or
7934 // amdgpu_cs_chain_preserve.
7936
7937 Type *RetTy = I.getType();
7938 assert(RetTy->isVoidTy() && "Should not return");
7939
7940 SDValue Callee = getValue(I.getOperand(0));
7941
7942 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
7943 // We'll also tack the value of the EXEC mask at the end.
7945 Args.reserve(3);
7946
7947 for (unsigned Idx : {2, 3, 1}) {
7948 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
7949 I.getOperand(Idx)->getType());
7950 Arg.setAttributes(&I, Idx);
7951 Args.push_back(Arg);
7952 }
7953
7954 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
7955 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
7956 Args[2].IsInReg = true; // EXEC should be inreg
7957
7958 // Forward the flags and any additional arguments.
7959 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
7960 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
7961 I.getOperand(Idx)->getType());
7962 Arg.setAttributes(&I, Idx);
7963 Args.push_back(Arg);
7964 }
7965
7966 TargetLowering::CallLoweringInfo CLI(DAG);
7967 CLI.setDebugLoc(getCurSDLoc())
7968 .setChain(getRoot())
7969 .setCallee(CC, RetTy, Callee, std::move(Args))
7970 .setNoReturn(true)
7971 .setTailCall(true)
7972 .setConvergent(I.isConvergent());
7973 CLI.CB = &I;
7974 std::pair<SDValue, SDValue> Result =
7975 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
7976 (void)Result;
7977 assert(!Result.first.getNode() && !Result.second.getNode() &&
7978 "Should've lowered as tail call");
7979
7980 HasTailCall = true;
7981 return;
7982 }
7983 case Intrinsic::amdgcn_call_whole_wave: {
7985 bool isTailCall = I.isTailCall();
7986
7987 // The first argument is the callee. Skip it when assembling the call args.
7988 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
7989 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
7990 I.getArgOperand(Idx)->getType());
7991 Arg.setAttributes(&I, Idx);
7992
7993 // If we have an explicit sret argument that is an Instruction, (i.e., it
7994 // might point to function-local memory), we can't meaningfully tail-call.
7995 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
7996 isTailCall = false;
7997
7998 Args.push_back(Arg);
7999 }
8000
8001 SDValue ConvControlToken;
8002 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8003 auto *Token = Bundle->Inputs[0].get();
8004 ConvControlToken = getValue(Token);
8005 }
8006
8007 TargetLowering::CallLoweringInfo CLI(DAG);
8008 CLI.setDebugLoc(getCurSDLoc())
8009 .setChain(getRoot())
8010 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8011 getValue(I.getArgOperand(0)), std::move(Args))
8012 .setTailCall(isTailCall && canTailCall(I))
8013 .setIsPreallocated(
8014 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8015 .setConvergent(I.isConvergent())
8016 .setConvergenceControlToken(ConvControlToken);
8017 CLI.CB = &I;
8018
8019 std::pair<SDValue, SDValue> Result =
8020 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8021
8022 if (Result.first.getNode())
8023 setValue(&I, Result.first);
8024 return;
8025 }
8026 case Intrinsic::ptrmask: {
8027 SDValue Ptr = getValue(I.getOperand(0));
8028 SDValue Mask = getValue(I.getOperand(1));
8029
8030 // On arm64_32, pointers are 32 bits when stored in memory, but
8031 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8032 // match the index type, but the pointer is 64 bits, so the mask must be
8033 // zero-extended up to 64 bits to match the pointer.
8034 EVT PtrVT =
8035 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8036 EVT MemVT =
8037 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8038 assert(PtrVT == Ptr.getValueType());
8039 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8040 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8041 // 128-bit, so we have to pad the mask with ones for unused bits.
8042 auto HighOnes = DAG.getNode(
8043 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8044 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8045 PtrVT, sdl));
8046 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8047 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8048 } else if (Mask.getValueType() != PtrVT)
8049 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8050
8051 assert(Mask.getValueType() == PtrVT);
8052 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8053 return;
8054 }
8055 case Intrinsic::threadlocal_address: {
8056 setValue(&I, getValue(I.getOperand(0)));
8057 return;
8058 }
8059 case Intrinsic::get_active_lane_mask: {
8060 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8061 SDValue Index = getValue(I.getOperand(0));
8062 SDValue TripCount = getValue(I.getOperand(1));
8063 EVT ElementVT = Index.getValueType();
8064
8065 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8066 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8067 TripCount));
8068 return;
8069 }
8070
8071 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8072 CCVT.getVectorElementCount());
8073
8074 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8075 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8076 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8077 SDValue VectorInduction = DAG.getNode(
8078 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8079 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8080 VectorTripCount, ISD::CondCode::SETULT);
8081 setValue(&I, SetCC);
8082 return;
8083 }
8084 case Intrinsic::experimental_get_vector_length: {
8085 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8086 "Expected positive VF");
8087 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8088 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8089
8090 SDValue Count = getValue(I.getOperand(0));
8091 EVT CountVT = Count.getValueType();
8092
8093 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8094 visitTargetIntrinsic(I, Intrinsic);
8095 return;
8096 }
8097
8098 // Expand to a umin between the trip count and the maximum elements the type
8099 // can hold.
8100 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8101
8102 // Extend the trip count to at least the result VT.
8103 if (CountVT.bitsLT(VT)) {
8104 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8105 CountVT = VT;
8106 }
8107
8108 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8109 ElementCount::get(VF, IsScalable));
8110
8111 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8112 // Clip to the result type if needed.
8113 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8114
8115 setValue(&I, Trunc);
8116 return;
8117 }
8118 case Intrinsic::vector_partial_reduce_add: {
8119 SDValue Acc = getValue(I.getOperand(0));
8120 SDValue Input = getValue(I.getOperand(1));
8121 setValue(&I,
8122 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8123 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8124 return;
8125 }
8126 case Intrinsic::experimental_cttz_elts: {
8127 auto DL = getCurSDLoc();
8128 SDValue Op = getValue(I.getOperand(0));
8129 EVT OpVT = Op.getValueType();
8130
8131 if (!TLI.shouldExpandCttzElements(OpVT)) {
8132 visitTargetIntrinsic(I, Intrinsic);
8133 return;
8134 }
8135
8136 if (OpVT.getScalarType() != MVT::i1) {
8137 // Compare the input vector elements to zero & use to count trailing zeros
8138 SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8139 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8140 OpVT.getVectorElementCount());
8141 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8142 }
8143
8144 // If the zero-is-poison flag is set, we can assume the upper limit
8145 // of the result is VF-1.
8146 bool ZeroIsPoison =
8147 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8148 ConstantRange VScaleRange(1, true); // Dummy value.
8149 if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8150 VScaleRange = getVScaleRange(I.getCaller(), 64);
8151 unsigned EltWidth = TLI.getBitWidthForCttzElements(
8152 I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8153
8154 MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8155
8156 // Create the new vector type & get the vector length
8157 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8158 OpVT.getVectorElementCount());
8159
8160 SDValue VL =
8161 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8162
8163 SDValue StepVec = DAG.getStepVector(DL, NewVT);
8164 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8165 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8166 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8167 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8168 SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8169 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8170
8171 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8172 SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
8173
8174 setValue(&I, Ret);
8175 return;
8176 }
8177 case Intrinsic::vector_insert: {
8178 SDValue Vec = getValue(I.getOperand(0));
8179 SDValue SubVec = getValue(I.getOperand(1));
8180 SDValue Index = getValue(I.getOperand(2));
8181
8182 // The intrinsic's index type is i64, but the SDNode requires an index type
8183 // suitable for the target. Convert the index as required.
8184 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8185 if (Index.getValueType() != VectorIdxTy)
8186 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8187
8188 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8189 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8190 Index));
8191 return;
8192 }
8193 case Intrinsic::vector_extract: {
8194 SDValue Vec = getValue(I.getOperand(0));
8195 SDValue Index = getValue(I.getOperand(1));
8196 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8197
8198 // The intrinsic's index type is i64, but the SDNode requires an index type
8199 // suitable for the target. Convert the index as required.
8200 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8201 if (Index.getValueType() != VectorIdxTy)
8202 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8203
8204 setValue(&I,
8205 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8206 return;
8207 }
8208 case Intrinsic::experimental_vector_match: {
8209 SDValue Op1 = getValue(I.getOperand(0));
8210 SDValue Op2 = getValue(I.getOperand(1));
8211 SDValue Mask = getValue(I.getOperand(2));
8212 EVT Op1VT = Op1.getValueType();
8213 EVT Op2VT = Op2.getValueType();
8214 EVT ResVT = Mask.getValueType();
8215 unsigned SearchSize = Op2VT.getVectorNumElements();
8216
8217 // If the target has native support for this vector match operation, lower
8218 // the intrinsic untouched; otherwise, expand it below.
8219 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8220 visitTargetIntrinsic(I, Intrinsic);
8221 return;
8222 }
8223
8224 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8225
8226 for (unsigned i = 0; i < SearchSize; ++i) {
8227 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8228 Op2VT.getVectorElementType(), Op2,
8229 DAG.getVectorIdxConstant(i, sdl));
8230 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8231 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8232 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8233 }
8234
8235 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8236 return;
8237 }
8238 case Intrinsic::vector_reverse:
8239 visitVectorReverse(I);
8240 return;
8241 case Intrinsic::vector_splice:
8242 visitVectorSplice(I);
8243 return;
8244 case Intrinsic::callbr_landingpad:
8245 visitCallBrLandingPad(I);
8246 return;
8247 case Intrinsic::vector_interleave2:
8248 visitVectorInterleave(I, 2);
8249 return;
8250 case Intrinsic::vector_interleave3:
8251 visitVectorInterleave(I, 3);
8252 return;
8253 case Intrinsic::vector_interleave4:
8254 visitVectorInterleave(I, 4);
8255 return;
8256 case Intrinsic::vector_interleave5:
8257 visitVectorInterleave(I, 5);
8258 return;
8259 case Intrinsic::vector_interleave6:
8260 visitVectorInterleave(I, 6);
8261 return;
8262 case Intrinsic::vector_interleave7:
8263 visitVectorInterleave(I, 7);
8264 return;
8265 case Intrinsic::vector_interleave8:
8266 visitVectorInterleave(I, 8);
8267 return;
8268 case Intrinsic::vector_deinterleave2:
8269 visitVectorDeinterleave(I, 2);
8270 return;
8271 case Intrinsic::vector_deinterleave3:
8272 visitVectorDeinterleave(I, 3);
8273 return;
8274 case Intrinsic::vector_deinterleave4:
8275 visitVectorDeinterleave(I, 4);
8276 return;
8277 case Intrinsic::vector_deinterleave5:
8278 visitVectorDeinterleave(I, 5);
8279 return;
8280 case Intrinsic::vector_deinterleave6:
8281 visitVectorDeinterleave(I, 6);
8282 return;
8283 case Intrinsic::vector_deinterleave7:
8284 visitVectorDeinterleave(I, 7);
8285 return;
8286 case Intrinsic::vector_deinterleave8:
8287 visitVectorDeinterleave(I, 8);
8288 return;
8289 case Intrinsic::experimental_vector_compress:
8290 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8291 getValue(I.getArgOperand(0)).getValueType(),
8292 getValue(I.getArgOperand(0)),
8293 getValue(I.getArgOperand(1)),
8294 getValue(I.getArgOperand(2)), Flags));
8295 return;
8296 case Intrinsic::experimental_convergence_anchor:
8297 case Intrinsic::experimental_convergence_entry:
8298 case Intrinsic::experimental_convergence_loop:
8299 visitConvergenceControl(I, Intrinsic);
8300 return;
8301 case Intrinsic::experimental_vector_histogram_add: {
8302 visitVectorHistogram(I, Intrinsic);
8303 return;
8304 }
8305 case Intrinsic::experimental_vector_extract_last_active: {
8306 visitVectorExtractLastActive(I, Intrinsic);
8307 return;
8308 }
8309 case Intrinsic::loop_dependence_war_mask:
8310 setValue(&I,
8312 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8313 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8314 return;
8315 case Intrinsic::loop_dependence_raw_mask:
8316 setValue(&I,
8318 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8319 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8320 return;
8321 }
8322}
8323
8324void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8325 const ConstrainedFPIntrinsic &FPI) {
8326 SDLoc sdl = getCurSDLoc();
8327
8328 // We do not need to serialize constrained FP intrinsics against
8329 // each other or against (nonvolatile) loads, so they can be
8330 // chained like loads.
8331 SDValue Chain = DAG.getRoot();
8333 Opers.push_back(Chain);
8334 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8335 Opers.push_back(getValue(FPI.getArgOperand(I)));
8336
8337 auto pushOutChain = [this](SDValue Result, fp::ExceptionBehavior EB) {
8338 assert(Result.getNode()->getNumValues() == 2);
8339
8340 // Push node to the appropriate list so that future instructions can be
8341 // chained up correctly.
8342 SDValue OutChain = Result.getValue(1);
8343 switch (EB) {
8345 // The only reason why ebIgnore nodes still need to be chained is that
8346 // they might depend on the current rounding mode, and therefore must
8347 // not be moved across instruction that may change that mode.
8348 [[fallthrough]];
8350 // These must not be moved across calls or instructions that may change
8351 // floating-point exception masks.
8352 PendingConstrainedFP.push_back(OutChain);
8353 break;
8355 // These must not be moved across calls or instructions that may change
8356 // floating-point exception masks or read floating-point exception flags.
8357 // In addition, they cannot be optimized out even if unused.
8358 PendingConstrainedFPStrict.push_back(OutChain);
8359 break;
8360 }
8361 };
8362
8363 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8364 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8365 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8367
8368 SDNodeFlags Flags;
8370 Flags.setNoFPExcept(true);
8371
8372 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8373 Flags.copyFMF(*FPOp);
8374
8375 unsigned Opcode;
8376 switch (FPI.getIntrinsicID()) {
8377 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8378#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8379 case Intrinsic::INTRINSIC: \
8380 Opcode = ISD::STRICT_##DAGN; \
8381 break;
8382#include "llvm/IR/ConstrainedOps.def"
8383 case Intrinsic::experimental_constrained_fmuladd: {
8384 Opcode = ISD::STRICT_FMA;
8385 // Break fmuladd into fmul and fadd.
8386 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8387 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8388 Opers.pop_back();
8389 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8390 pushOutChain(Mul, EB);
8391 Opcode = ISD::STRICT_FADD;
8392 Opers.clear();
8393 Opers.push_back(Mul.getValue(1));
8394 Opers.push_back(Mul.getValue(0));
8395 Opers.push_back(getValue(FPI.getArgOperand(2)));
8396 }
8397 break;
8398 }
8399 }
8400
8401 // A few strict DAG nodes carry additional operands that are not
8402 // set up by the default code above.
8403 switch (Opcode) {
8404 default: break;
8406 Opers.push_back(
8407 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8408 break;
8409 case ISD::STRICT_FSETCC:
8410 case ISD::STRICT_FSETCCS: {
8412 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8413 if (TM.Options.NoNaNsFPMath)
8414 Condition = getFCmpCodeWithoutNaN(Condition);
8415 Opers.push_back(DAG.getCondCode(Condition));
8416 break;
8417 }
8418 }
8419
8420 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8421 pushOutChain(Result, EB);
8422
8423 SDValue FPResult = Result.getValue(0);
8424 setValue(&FPI, FPResult);
8425}
8426
8427static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8428 std::optional<unsigned> ResOPC;
8429 switch (VPIntrin.getIntrinsicID()) {
8430 case Intrinsic::vp_ctlz: {
8431 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8432 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8433 break;
8434 }
8435 case Intrinsic::vp_cttz: {
8436 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8437 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8438 break;
8439 }
8440 case Intrinsic::vp_cttz_elts: {
8441 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8442 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8443 break;
8444 }
8445#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8446 case Intrinsic::VPID: \
8447 ResOPC = ISD::VPSD; \
8448 break;
8449#include "llvm/IR/VPIntrinsics.def"
8450 }
8451
8452 if (!ResOPC)
8454 "Inconsistency: no SDNode available for this VPIntrinsic!");
8455
8456 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8457 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8458 if (VPIntrin.getFastMathFlags().allowReassoc())
8459 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8460 : ISD::VP_REDUCE_FMUL;
8461 }
8462
8463 return *ResOPC;
8464}
8465
8466void SelectionDAGBuilder::visitVPLoad(
8467 const VPIntrinsic &VPIntrin, EVT VT,
8468 const SmallVectorImpl<SDValue> &OpValues) {
8469 SDLoc DL = getCurSDLoc();
8470 Value *PtrOperand = VPIntrin.getArgOperand(0);
8471 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8472 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8473 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8474 SDValue LD;
8475 // Do not serialize variable-length loads of constant memory with
8476 // anything.
8477 if (!Alignment)
8478 Alignment = DAG.getEVTAlign(VT);
8479 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8480 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8481 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8482 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8483 MachineMemOperand::Flags MMOFlags =
8484 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8485 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8486 MachinePointerInfo(PtrOperand), MMOFlags,
8487 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8488 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8489 MMO, false /*IsExpanding */);
8490 if (AddToChain)
8491 PendingLoads.push_back(LD.getValue(1));
8492 setValue(&VPIntrin, LD);
8493}
8494
8495void SelectionDAGBuilder::visitVPLoadFF(
8496 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8497 const SmallVectorImpl<SDValue> &OpValues) {
8498 assert(OpValues.size() == 3 && "Unexpected number of operands");
8499 SDLoc DL = getCurSDLoc();
8500 Value *PtrOperand = VPIntrin.getArgOperand(0);
8501 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8502 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8503 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8504 SDValue LD;
8505 // Do not serialize variable-length loads of constant memory with
8506 // anything.
8507 if (!Alignment)
8508 Alignment = DAG.getEVTAlign(VT);
8509 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8510 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8511 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8512 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8513 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8514 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8515 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8516 MMO);
8517 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8518 if (AddToChain)
8519 PendingLoads.push_back(LD.getValue(2));
8520 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8521}
8522
8523void SelectionDAGBuilder::visitVPGather(
8524 const VPIntrinsic &VPIntrin, EVT VT,
8525 const SmallVectorImpl<SDValue> &OpValues) {
8526 SDLoc DL = getCurSDLoc();
8527 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8528 Value *PtrOperand = VPIntrin.getArgOperand(0);
8529 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8530 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8531 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8532 SDValue LD;
8533 if (!Alignment)
8534 Alignment = DAG.getEVTAlign(VT.getScalarType());
8535 unsigned AS =
8536 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8537 MachineMemOperand::Flags MMOFlags =
8538 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8539 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8540 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8541 *Alignment, AAInfo, Ranges);
8542 SDValue Base, Index, Scale;
8543 bool UniformBase =
8544 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8545 VT.getScalarStoreSize());
8546 if (!UniformBase) {
8547 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8548 Index = getValue(PtrOperand);
8549 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8550 }
8551 EVT IdxVT = Index.getValueType();
8552 EVT EltTy = IdxVT.getVectorElementType();
8553 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8554 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8555 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8556 }
8557 LD = DAG.getGatherVP(
8558 DAG.getVTList(VT, MVT::Other), VT, DL,
8559 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8561 PendingLoads.push_back(LD.getValue(1));
8562 setValue(&VPIntrin, LD);
8563}
8564
8565void SelectionDAGBuilder::visitVPStore(
8566 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8567 SDLoc DL = getCurSDLoc();
8568 Value *PtrOperand = VPIntrin.getArgOperand(1);
8569 EVT VT = OpValues[0].getValueType();
8570 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8571 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8572 SDValue ST;
8573 if (!Alignment)
8574 Alignment = DAG.getEVTAlign(VT);
8575 SDValue Ptr = OpValues[1];
8576 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8577 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8578 MachineMemOperand::Flags MMOFlags =
8579 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8580 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8581 MachinePointerInfo(PtrOperand), MMOFlags,
8582 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8583 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8584 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8585 /* IsTruncating */ false, /*IsCompressing*/ false);
8586 DAG.setRoot(ST);
8587 setValue(&VPIntrin, ST);
8588}
8589
8590void SelectionDAGBuilder::visitVPScatter(
8591 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8592 SDLoc DL = getCurSDLoc();
8593 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8594 Value *PtrOperand = VPIntrin.getArgOperand(1);
8595 EVT VT = OpValues[0].getValueType();
8596 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8597 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8598 SDValue ST;
8599 if (!Alignment)
8600 Alignment = DAG.getEVTAlign(VT.getScalarType());
8601 unsigned AS =
8602 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8603 MachineMemOperand::Flags MMOFlags =
8604 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8605 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8606 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8607 *Alignment, AAInfo);
8608 SDValue Base, Index, Scale;
8609 bool UniformBase =
8610 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8611 VT.getScalarStoreSize());
8612 if (!UniformBase) {
8613 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8614 Index = getValue(PtrOperand);
8615 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8616 }
8617 EVT IdxVT = Index.getValueType();
8618 EVT EltTy = IdxVT.getVectorElementType();
8619 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8620 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8621 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8622 }
8623 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8624 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8625 OpValues[2], OpValues[3]},
8626 MMO, ISD::SIGNED_SCALED);
8627 DAG.setRoot(ST);
8628 setValue(&VPIntrin, ST);
8629}
8630
8631void SelectionDAGBuilder::visitVPStridedLoad(
8632 const VPIntrinsic &VPIntrin, EVT VT,
8633 const SmallVectorImpl<SDValue> &OpValues) {
8634 SDLoc DL = getCurSDLoc();
8635 Value *PtrOperand = VPIntrin.getArgOperand(0);
8636 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8637 if (!Alignment)
8638 Alignment = DAG.getEVTAlign(VT.getScalarType());
8639 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8640 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8641 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8642 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8643 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8644 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8645 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8646 MachineMemOperand::Flags MMOFlags =
8647 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8648 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8649 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8650 *Alignment, AAInfo, Ranges);
8651
8652 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8653 OpValues[2], OpValues[3], MMO,
8654 false /*IsExpanding*/);
8655
8656 if (AddToChain)
8657 PendingLoads.push_back(LD.getValue(1));
8658 setValue(&VPIntrin, LD);
8659}
8660
8661void SelectionDAGBuilder::visitVPStridedStore(
8662 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8663 SDLoc DL = getCurSDLoc();
8664 Value *PtrOperand = VPIntrin.getArgOperand(1);
8665 EVT VT = OpValues[0].getValueType();
8666 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8667 if (!Alignment)
8668 Alignment = DAG.getEVTAlign(VT.getScalarType());
8669 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8670 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8671 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8672 MachineMemOperand::Flags MMOFlags =
8673 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8674 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8675 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8676 *Alignment, AAInfo);
8677
8678 SDValue ST = DAG.getStridedStoreVP(
8679 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8680 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8681 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8682 /*IsCompressing*/ false);
8683
8684 DAG.setRoot(ST);
8685 setValue(&VPIntrin, ST);
8686}
8687
8688void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8689 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8690 SDLoc DL = getCurSDLoc();
8691
8692 ISD::CondCode Condition;
8694 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8695 if (IsFP) {
8696 // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
8697 // flags, but calls that don't return floating-point types can't be
8698 // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
8699 Condition = getFCmpCondCode(CondCode);
8700 if (TM.Options.NoNaNsFPMath)
8701 Condition = getFCmpCodeWithoutNaN(Condition);
8702 } else {
8703 Condition = getICmpCondCode(CondCode);
8704 }
8705
8706 SDValue Op1 = getValue(VPIntrin.getOperand(0));
8707 SDValue Op2 = getValue(VPIntrin.getOperand(1));
8708 // #2 is the condition code
8709 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8710 SDValue EVL = getValue(VPIntrin.getOperand(4));
8711 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8712 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8713 "Unexpected target EVL type");
8714 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8715
8716 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8717 VPIntrin.getType());
8718 setValue(&VPIntrin,
8719 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8720}
8721
8722void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8723 const VPIntrinsic &VPIntrin) {
8724 SDLoc DL = getCurSDLoc();
8725 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8726
8727 auto IID = VPIntrin.getIntrinsicID();
8728
8729 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8730 return visitVPCmp(*CmpI);
8731
8732 SmallVector<EVT, 4> ValueVTs;
8733 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8734 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8735 SDVTList VTs = DAG.getVTList(ValueVTs);
8736
8737 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8738
8739 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8740 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8741 "Unexpected target EVL type");
8742
8743 // Request operands.
8744 SmallVector<SDValue, 7> OpValues;
8745 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8746 auto Op = getValue(VPIntrin.getArgOperand(I));
8747 if (I == EVLParamPos)
8748 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8749 OpValues.push_back(Op);
8750 }
8751
8752 switch (Opcode) {
8753 default: {
8754 SDNodeFlags SDFlags;
8755 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8756 SDFlags.copyFMF(*FPMO);
8757 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8758 setValue(&VPIntrin, Result);
8759 break;
8760 }
8761 case ISD::VP_LOAD:
8762 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8763 break;
8764 case ISD::VP_LOAD_FF:
8765 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8766 break;
8767 case ISD::VP_GATHER:
8768 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8769 break;
8770 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8771 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8772 break;
8773 case ISD::VP_STORE:
8774 visitVPStore(VPIntrin, OpValues);
8775 break;
8776 case ISD::VP_SCATTER:
8777 visitVPScatter(VPIntrin, OpValues);
8778 break;
8779 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8780 visitVPStridedStore(VPIntrin, OpValues);
8781 break;
8782 case ISD::VP_FMULADD: {
8783 assert(OpValues.size() == 5 && "Unexpected number of operands");
8784 SDNodeFlags SDFlags;
8785 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8786 SDFlags.copyFMF(*FPMO);
8787 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8788 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8789 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8790 } else {
8791 SDValue Mul = DAG.getNode(
8792 ISD::VP_FMUL, DL, VTs,
8793 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8794 SDValue Add =
8795 DAG.getNode(ISD::VP_FADD, DL, VTs,
8796 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8797 setValue(&VPIntrin, Add);
8798 }
8799 break;
8800 }
8801 case ISD::VP_IS_FPCLASS: {
8802 const DataLayout DLayout = DAG.getDataLayout();
8803 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8804 auto Constant = OpValues[1]->getAsZExtVal();
8805 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8806 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8807 {OpValues[0], Check, OpValues[2], OpValues[3]});
8808 setValue(&VPIntrin, V);
8809 return;
8810 }
8811 case ISD::VP_INTTOPTR: {
8812 SDValue N = OpValues[0];
8813 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8814 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8815 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8816 OpValues[2]);
8817 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8818 OpValues[2]);
8819 setValue(&VPIntrin, N);
8820 break;
8821 }
8822 case ISD::VP_PTRTOINT: {
8823 SDValue N = OpValues[0];
8824 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8825 VPIntrin.getType());
8826 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
8827 VPIntrin.getOperand(0)->getType());
8828 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8829 OpValues[2]);
8830 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8831 OpValues[2]);
8832 setValue(&VPIntrin, N);
8833 break;
8834 }
8835 case ISD::VP_ABS:
8836 case ISD::VP_CTLZ:
8837 case ISD::VP_CTLZ_ZERO_UNDEF:
8838 case ISD::VP_CTTZ:
8839 case ISD::VP_CTTZ_ZERO_UNDEF:
8840 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
8841 case ISD::VP_CTTZ_ELTS: {
8842 SDValue Result =
8843 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
8844 setValue(&VPIntrin, Result);
8845 break;
8846 }
8847 }
8848}
8849
8850SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
8851 const BasicBlock *EHPadBB,
8852 MCSymbol *&BeginLabel) {
8853 MachineFunction &MF = DAG.getMachineFunction();
8854
8855 // Insert a label before the invoke call to mark the try range. This can be
8856 // used to detect deletion of the invoke via the MachineModuleInfo.
8857 BeginLabel = MF.getContext().createTempSymbol();
8858
8859 // For SjLj, keep track of which landing pads go with which invokes
8860 // so as to maintain the ordering of pads in the LSDA.
8861 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
8862 if (CallSiteIndex) {
8863 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
8864 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
8865
8866 // Now that the call site is handled, stop tracking it.
8867 FuncInfo.setCurrentCallSite(0);
8868 }
8869
8870 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
8871}
8872
8873SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
8874 const BasicBlock *EHPadBB,
8875 MCSymbol *BeginLabel) {
8876 assert(BeginLabel && "BeginLabel should've been set");
8877
8878 MachineFunction &MF = DAG.getMachineFunction();
8879
8880 // Insert a label at the end of the invoke call to mark the try range. This
8881 // can be used to detect deletion of the invoke via the MachineModuleInfo.
8882 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
8883 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
8884
8885 // Inform MachineModuleInfo of range.
8886 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
8887 // There is a platform (e.g. wasm) that uses funclet style IR but does not
8888 // actually use outlined funclets and their LSDA info style.
8889 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
8890 assert(II && "II should've been set");
8891 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
8892 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
8893 } else if (!isScopedEHPersonality(Pers)) {
8894 assert(EHPadBB);
8895 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
8896 }
8897
8898 return Chain;
8899}
8900
8901std::pair<SDValue, SDValue>
8903 const BasicBlock *EHPadBB) {
8904 MCSymbol *BeginLabel = nullptr;
8905
8906 if (EHPadBB) {
8907 // Both PendingLoads and PendingExports must be flushed here;
8908 // this call might not return.
8909 (void)getRoot();
8910 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
8911 CLI.setChain(getRoot());
8912 }
8913
8914 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8915 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
8916
8917 assert((CLI.IsTailCall || Result.second.getNode()) &&
8918 "Non-null chain expected with non-tail call!");
8919 assert((Result.second.getNode() || !Result.first.getNode()) &&
8920 "Null value expected with tail call!");
8921
8922 if (!Result.second.getNode()) {
8923 // As a special case, a null chain means that a tail call has been emitted
8924 // and the DAG root is already updated.
8925 HasTailCall = true;
8926
8927 // Since there's no actual continuation from this block, nothing can be
8928 // relying on us setting vregs for them.
8929 PendingExports.clear();
8930 } else {
8931 DAG.setRoot(Result.second);
8932 }
8933
8934 if (EHPadBB) {
8935 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
8936 BeginLabel));
8937 Result.second = getRoot();
8938 }
8939
8940 return Result;
8941}
8942
8944 bool isMustTailCall = CB.isMustTailCall();
8945
8946 // Avoid emitting tail calls in functions with the disable-tail-calls
8947 // attribute.
8948 const Function *Caller = CB.getParent()->getParent();
8949 if (Caller->getFnAttribute("disable-tail-calls").getValueAsString() ==
8950 "true" &&
8951 !isMustTailCall)
8952 return false;
8953
8954 // We can't tail call inside a function with a swifterror argument. Lowering
8955 // does not support this yet. It would have to move into the swifterror
8956 // register before the call.
8957 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
8958 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
8959 return false;
8960
8961 // Check if target-independent constraints permit a tail call here.
8962 // Target-dependent constraints are checked within TLI->LowerCallTo.
8963 return isInTailCallPosition(CB, DAG.getTarget());
8964}
8965
8967 bool isTailCall, bool isMustTailCall,
8968 const BasicBlock *EHPadBB,
8969 const TargetLowering::PtrAuthInfo *PAI) {
8970 auto &DL = DAG.getDataLayout();
8971 FunctionType *FTy = CB.getFunctionType();
8972 Type *RetTy = CB.getType();
8973
8975 Args.reserve(CB.arg_size());
8976
8977 const Value *SwiftErrorVal = nullptr;
8978 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8979
8980 if (isTailCall)
8981 isTailCall = canTailCall(CB);
8982
8983 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
8984 const Value *V = *I;
8985
8986 // Skip empty types
8987 if (V->getType()->isEmptyTy())
8988 continue;
8989
8990 SDValue ArgNode = getValue(V);
8991 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
8992 Entry.setAttributes(&CB, I - CB.arg_begin());
8993
8994 // Use swifterror virtual register as input to the call.
8995 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
8996 SwiftErrorVal = V;
8997 // We find the virtual register for the actual swifterror argument.
8998 // Instead of using the Value, we use the virtual register instead.
8999 Entry.Node =
9000 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
9001 EVT(TLI.getPointerTy(DL)));
9002 }
9003
9004 Args.push_back(Entry);
9005
9006 // If we have an explicit sret argument that is an Instruction, (i.e., it
9007 // might point to function-local memory), we can't meaningfully tail-call.
9008 if (Entry.IsSRet && isa<Instruction>(V))
9009 isTailCall = false;
9010 }
9011
9012 // If call site has a cfguardtarget operand bundle, create and add an
9013 // additional ArgListEntry.
9014 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9015 Value *V = Bundle->Inputs[0];
9017 Entry.IsCFGuardTarget = true;
9018 Args.push_back(Entry);
9019 }
9020
9021 // Disable tail calls if there is an swifterror argument. Targets have not
9022 // been updated to support tail calls.
9023 if (TLI.supportSwiftError() && SwiftErrorVal)
9024 isTailCall = false;
9025
9026 ConstantInt *CFIType = nullptr;
9027 if (CB.isIndirectCall()) {
9028 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9029 if (!TLI.supportKCFIBundles())
9031 "Target doesn't support calls with kcfi operand bundles.");
9032 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9033 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9034 }
9035 }
9036
9037 SDValue ConvControlToken;
9038 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9039 auto *Token = Bundle->Inputs[0].get();
9040 ConvControlToken = getValue(Token);
9041 }
9042
9045 .setChain(getRoot())
9046 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9047 .setTailCall(isTailCall)
9051 .setCFIType(CFIType)
9052 .setConvergenceControlToken(ConvControlToken);
9053
9054 // Set the pointer authentication info if we have it.
9055 if (PAI) {
9056 if (!TLI.supportPtrAuthBundles())
9058 "This target doesn't support calls with ptrauth operand bundles.");
9059 CLI.setPtrAuth(*PAI);
9060 }
9061
9062 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9063
9064 if (Result.first.getNode()) {
9065 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9066 setValue(&CB, Result.first);
9067 }
9068
9069 // The last element of CLI.InVals has the SDValue for swifterror return.
9070 // Here we copy it to a virtual register and update SwiftErrorMap for
9071 // book-keeping.
9072 if (SwiftErrorVal && TLI.supportSwiftError()) {
9073 // Get the last element of InVals.
9074 SDValue Src = CLI.InVals.back();
9075 Register VReg =
9076 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9077 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9078 DAG.setRoot(CopyNode);
9079 }
9080}
9081
9082static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9083 SelectionDAGBuilder &Builder) {
9084 // Check to see if this load can be trivially constant folded, e.g. if the
9085 // input is from a string literal.
9086 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9087 // Cast pointer to the type we really want to load.
9088 Type *LoadTy =
9089 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9090 if (LoadVT.isVector())
9091 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9092 if (const Constant *LoadCst =
9093 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9094 LoadTy, Builder.DAG.getDataLayout()))
9095 return Builder.getValue(LoadCst);
9096 }
9097
9098 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9099 // still constant memory, the input chain can be the entry node.
9100 SDValue Root;
9101 bool ConstantMemory = false;
9102
9103 // Do not serialize (non-volatile) loads of constant memory with anything.
9104 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9105 Root = Builder.DAG.getEntryNode();
9106 ConstantMemory = true;
9107 } else {
9108 // Do not serialize non-volatile loads against each other.
9109 Root = Builder.DAG.getRoot();
9110 }
9111
9112 SDValue Ptr = Builder.getValue(PtrVal);
9113 SDValue LoadVal =
9114 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9115 MachinePointerInfo(PtrVal), Align(1));
9116
9117 if (!ConstantMemory)
9118 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9119 return LoadVal;
9120}
9121
9122/// Record the value for an instruction that produces an integer result,
9123/// converting the type where necessary.
9124void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9125 SDValue Value,
9126 bool IsSigned) {
9127 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9128 I.getType(), true);
9129 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9130 setValue(&I, Value);
9131}
9132
9133/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9134/// true and lower it. Otherwise return false, and it will be lowered like a
9135/// normal call.
9136/// The caller already checked that \p I calls the appropriate LibFunc with a
9137/// correct prototype.
9138bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9139 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9140 const Value *Size = I.getArgOperand(2);
9141 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9142 if (CSize && CSize->getZExtValue() == 0) {
9143 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9144 I.getType(), true);
9145 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9146 return true;
9147 }
9148
9149 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9150 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9151 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9152 getValue(Size), &I);
9153 if (Res.first.getNode()) {
9154 processIntegerCallValue(I, Res.first, true);
9155 PendingLoads.push_back(Res.second);
9156 return true;
9157 }
9158
9159 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9160 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9161 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9162 return false;
9163
9164 // If the target has a fast compare for the given size, it will return a
9165 // preferred load type for that size. Require that the load VT is legal and
9166 // that the target supports unaligned loads of that type. Otherwise, return
9167 // INVALID.
9168 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9169 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9170 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9171 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9172 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9173 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9174 // TODO: Check alignment of src and dest ptrs.
9175 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9176 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9177 if (!TLI.isTypeLegal(LVT) ||
9178 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9179 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9181 }
9182
9183 return LVT;
9184 };
9185
9186 // This turns into unaligned loads. We only do this if the target natively
9187 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9188 // we'll only produce a small number of byte loads.
9189 MVT LoadVT;
9190 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9191 switch (NumBitsToCompare) {
9192 default:
9193 return false;
9194 case 16:
9195 LoadVT = MVT::i16;
9196 break;
9197 case 32:
9198 LoadVT = MVT::i32;
9199 break;
9200 case 64:
9201 case 128:
9202 case 256:
9203 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9204 break;
9205 }
9206
9207 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9208 return false;
9209
9210 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9211 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9212
9213 // Bitcast to a wide integer type if the loads are vectors.
9214 if (LoadVT.isVector()) {
9215 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9216 LoadL = DAG.getBitcast(CmpVT, LoadL);
9217 LoadR = DAG.getBitcast(CmpVT, LoadR);
9218 }
9219
9220 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9221 processIntegerCallValue(I, Cmp, false);
9222 return true;
9223}
9224
9225/// See if we can lower a memchr call into an optimized form. If so, return
9226/// true and lower it. Otherwise return false, and it will be lowered like a
9227/// normal call.
9228/// The caller already checked that \p I calls the appropriate LibFunc with a
9229/// correct prototype.
9230bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9231 const Value *Src = I.getArgOperand(0);
9232 const Value *Char = I.getArgOperand(1);
9233 const Value *Length = I.getArgOperand(2);
9234
9235 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9236 std::pair<SDValue, SDValue> Res =
9237 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9238 getValue(Src), getValue(Char), getValue(Length),
9239 MachinePointerInfo(Src));
9240 if (Res.first.getNode()) {
9241 setValue(&I, Res.first);
9242 PendingLoads.push_back(Res.second);
9243 return true;
9244 }
9245
9246 return false;
9247}
9248
9249/// See if we can lower a mempcpy call into an optimized form. If so, return
9250/// true and lower it. Otherwise return false, and it will be lowered like a
9251/// normal call.
9252/// The caller already checked that \p I calls the appropriate LibFunc with a
9253/// correct prototype.
9254bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9255 SDValue Dst = getValue(I.getArgOperand(0));
9256 SDValue Src = getValue(I.getArgOperand(1));
9257 SDValue Size = getValue(I.getArgOperand(2));
9258
9259 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9260 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9261 // DAG::getMemcpy needs Alignment to be defined.
9262 Align Alignment = std::min(DstAlign, SrcAlign);
9263
9264 SDLoc sdl = getCurSDLoc();
9265
9266 // In the mempcpy context we need to pass in a false value for isTailCall
9267 // because the return pointer needs to be adjusted by the size of
9268 // the copied memory.
9269 SDValue Root = getMemoryRoot();
9270 SDValue MC = DAG.getMemcpy(
9271 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9272 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9273 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9274 assert(MC.getNode() != nullptr &&
9275 "** memcpy should not be lowered as TailCall in mempcpy context **");
9276 DAG.setRoot(MC);
9277
9278 // Check if Size needs to be truncated or extended.
9279 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9280
9281 // Adjust return pointer to point just past the last dst byte.
9282 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9283 setValue(&I, DstPlusSize);
9284 return true;
9285}
9286
9287/// See if we can lower a strcpy call into an optimized form. If so, return
9288/// true and lower it, otherwise return false and it will be lowered like a
9289/// normal call.
9290/// The caller already checked that \p I calls the appropriate LibFunc with a
9291/// correct prototype.
9292bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9293 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9294
9295 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9296 std::pair<SDValue, SDValue> Res =
9298 getValue(Arg0), getValue(Arg1),
9299 MachinePointerInfo(Arg0),
9300 MachinePointerInfo(Arg1), isStpcpy);
9301 if (Res.first.getNode()) {
9302 setValue(&I, Res.first);
9303 DAG.setRoot(Res.second);
9304 return true;
9305 }
9306
9307 return false;
9308}
9309
9310/// See if we can lower a strcmp call into an optimized form. If so, return
9311/// true and lower it, otherwise return false and it will be lowered like a
9312/// normal call.
9313/// The caller already checked that \p I calls the appropriate LibFunc with a
9314/// correct prototype.
9315bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9316 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9317
9318 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9319 std::pair<SDValue, SDValue> Res =
9320 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
9321 getValue(Arg0), getValue(Arg1),
9322 MachinePointerInfo(Arg0),
9323 MachinePointerInfo(Arg1));
9324 if (Res.first.getNode()) {
9325 processIntegerCallValue(I, Res.first, true);
9326 PendingLoads.push_back(Res.second);
9327 return true;
9328 }
9329
9330 return false;
9331}
9332
9333/// See if we can lower a strlen call into an optimized form. If so, return
9334/// true and lower it, otherwise return false and it will be lowered like a
9335/// normal call.
9336/// The caller already checked that \p I calls the appropriate LibFunc with a
9337/// correct prototype.
9338bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9339 const Value *Arg0 = I.getArgOperand(0);
9340
9341 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9342 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrlen(
9343 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), &I);
9344 if (Res.first.getNode()) {
9345 processIntegerCallValue(I, Res.first, false);
9346 PendingLoads.push_back(Res.second);
9347 return true;
9348 }
9349
9350 return false;
9351}
9352
9353/// See if we can lower a strnlen call into an optimized form. If so, return
9354/// true and lower it, otherwise return false and it will be lowered like a
9355/// normal call.
9356/// The caller already checked that \p I calls the appropriate LibFunc with a
9357/// correct prototype.
9358bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9359 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9360
9361 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9362 std::pair<SDValue, SDValue> Res =
9363 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9364 getValue(Arg0), getValue(Arg1),
9365 MachinePointerInfo(Arg0));
9366 if (Res.first.getNode()) {
9367 processIntegerCallValue(I, Res.first, false);
9368 PendingLoads.push_back(Res.second);
9369 return true;
9370 }
9371
9372 return false;
9373}
9374
9375/// See if we can lower a unary floating-point operation into an SDNode with
9376/// the specified Opcode. If so, return true and lower it, otherwise return
9377/// false and it will be lowered like a normal call.
9378/// The caller already checked that \p I calls the appropriate LibFunc with a
9379/// correct prototype.
9380bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9381 unsigned Opcode) {
9382 // We already checked this call's prototype; verify it doesn't modify errno.
9383 if (!I.onlyReadsMemory())
9384 return false;
9385
9386 SDNodeFlags Flags;
9387 Flags.copyFMF(cast<FPMathOperator>(I));
9388
9389 SDValue Tmp = getValue(I.getArgOperand(0));
9390 setValue(&I,
9391 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9392 return true;
9393}
9394
9395/// See if we can lower a binary floating-point operation into an SDNode with
9396/// the specified Opcode. If so, return true and lower it. Otherwise return
9397/// false, and it will be lowered like a normal call.
9398/// The caller already checked that \p I calls the appropriate LibFunc with a
9399/// correct prototype.
9400bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9401 unsigned Opcode) {
9402 // We already checked this call's prototype; verify it doesn't modify errno.
9403 if (!I.onlyReadsMemory())
9404 return false;
9405
9406 SDNodeFlags Flags;
9407 Flags.copyFMF(cast<FPMathOperator>(I));
9408
9409 SDValue Tmp0 = getValue(I.getArgOperand(0));
9410 SDValue Tmp1 = getValue(I.getArgOperand(1));
9411 EVT VT = Tmp0.getValueType();
9412 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9413 return true;
9414}
9415
9416void SelectionDAGBuilder::visitCall(const CallInst &I) {
9417 // Handle inline assembly differently.
9418 if (I.isInlineAsm()) {
9419 visitInlineAsm(I);
9420 return;
9421 }
9422
9424
9425 if (Function *F = I.getCalledFunction()) {
9426 if (F->isDeclaration()) {
9427 // Is this an LLVM intrinsic?
9428 if (unsigned IID = F->getIntrinsicID()) {
9429 visitIntrinsicCall(I, IID);
9430 return;
9431 }
9432 }
9433
9434 // Check for well-known libc/libm calls. If the function is internal, it
9435 // can't be a library call. Don't do the check if marked as nobuiltin for
9436 // some reason or the call site requires strict floating point semantics.
9437 LibFunc Func;
9438 if (!I.isNoBuiltin() && !I.isStrictFP() && !F->hasLocalLinkage() &&
9439 F->hasName() && LibInfo->getLibFunc(*F, Func) &&
9440 LibInfo->hasOptimizedCodeGen(Func)) {
9441 switch (Func) {
9442 default: break;
9443 case LibFunc_bcmp:
9444 if (visitMemCmpBCmpCall(I))
9445 return;
9446 break;
9447 case LibFunc_copysign:
9448 case LibFunc_copysignf:
9449 case LibFunc_copysignl:
9450 // We already checked this call's prototype; verify it doesn't modify
9451 // errno.
9452 if (I.onlyReadsMemory()) {
9453 SDValue LHS = getValue(I.getArgOperand(0));
9454 SDValue RHS = getValue(I.getArgOperand(1));
9456 LHS.getValueType(), LHS, RHS));
9457 return;
9458 }
9459 break;
9460 case LibFunc_fabs:
9461 case LibFunc_fabsf:
9462 case LibFunc_fabsl:
9463 if (visitUnaryFloatCall(I, ISD::FABS))
9464 return;
9465 break;
9466 case LibFunc_fmin:
9467 case LibFunc_fminf:
9468 case LibFunc_fminl:
9469 if (visitBinaryFloatCall(I, ISD::FMINNUM))
9470 return;
9471 break;
9472 case LibFunc_fmax:
9473 case LibFunc_fmaxf:
9474 case LibFunc_fmaxl:
9475 if (visitBinaryFloatCall(I, ISD::FMAXNUM))
9476 return;
9477 break;
9478 case LibFunc_fminimum_num:
9479 case LibFunc_fminimum_numf:
9480 case LibFunc_fminimum_numl:
9481 if (visitBinaryFloatCall(I, ISD::FMINIMUMNUM))
9482 return;
9483 break;
9484 case LibFunc_fmaximum_num:
9485 case LibFunc_fmaximum_numf:
9486 case LibFunc_fmaximum_numl:
9487 if (visitBinaryFloatCall(I, ISD::FMAXIMUMNUM))
9488 return;
9489 break;
9490 case LibFunc_sin:
9491 case LibFunc_sinf:
9492 case LibFunc_sinl:
9493 if (visitUnaryFloatCall(I, ISD::FSIN))
9494 return;
9495 break;
9496 case LibFunc_cos:
9497 case LibFunc_cosf:
9498 case LibFunc_cosl:
9499 if (visitUnaryFloatCall(I, ISD::FCOS))
9500 return;
9501 break;
9502 case LibFunc_tan:
9503 case LibFunc_tanf:
9504 case LibFunc_tanl:
9505 if (visitUnaryFloatCall(I, ISD::FTAN))
9506 return;
9507 break;
9508 case LibFunc_asin:
9509 case LibFunc_asinf:
9510 case LibFunc_asinl:
9511 if (visitUnaryFloatCall(I, ISD::FASIN))
9512 return;
9513 break;
9514 case LibFunc_acos:
9515 case LibFunc_acosf:
9516 case LibFunc_acosl:
9517 if (visitUnaryFloatCall(I, ISD::FACOS))
9518 return;
9519 break;
9520 case LibFunc_atan:
9521 case LibFunc_atanf:
9522 case LibFunc_atanl:
9523 if (visitUnaryFloatCall(I, ISD::FATAN))
9524 return;
9525 break;
9526 case LibFunc_atan2:
9527 case LibFunc_atan2f:
9528 case LibFunc_atan2l:
9529 if (visitBinaryFloatCall(I, ISD::FATAN2))
9530 return;
9531 break;
9532 case LibFunc_sinh:
9533 case LibFunc_sinhf:
9534 case LibFunc_sinhl:
9535 if (visitUnaryFloatCall(I, ISD::FSINH))
9536 return;
9537 break;
9538 case LibFunc_cosh:
9539 case LibFunc_coshf:
9540 case LibFunc_coshl:
9541 if (visitUnaryFloatCall(I, ISD::FCOSH))
9542 return;
9543 break;
9544 case LibFunc_tanh:
9545 case LibFunc_tanhf:
9546 case LibFunc_tanhl:
9547 if (visitUnaryFloatCall(I, ISD::FTANH))
9548 return;
9549 break;
9550 case LibFunc_sqrt:
9551 case LibFunc_sqrtf:
9552 case LibFunc_sqrtl:
9553 case LibFunc_sqrt_finite:
9554 case LibFunc_sqrtf_finite:
9555 case LibFunc_sqrtl_finite:
9556 if (visitUnaryFloatCall(I, ISD::FSQRT))
9557 return;
9558 break;
9559 case LibFunc_floor:
9560 case LibFunc_floorf:
9561 case LibFunc_floorl:
9562 if (visitUnaryFloatCall(I, ISD::FFLOOR))
9563 return;
9564 break;
9565 case LibFunc_nearbyint:
9566 case LibFunc_nearbyintf:
9567 case LibFunc_nearbyintl:
9568 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
9569 return;
9570 break;
9571 case LibFunc_ceil:
9572 case LibFunc_ceilf:
9573 case LibFunc_ceill:
9574 if (visitUnaryFloatCall(I, ISD::FCEIL))
9575 return;
9576 break;
9577 case LibFunc_rint:
9578 case LibFunc_rintf:
9579 case LibFunc_rintl:
9580 if (visitUnaryFloatCall(I, ISD::FRINT))
9581 return;
9582 break;
9583 case LibFunc_round:
9584 case LibFunc_roundf:
9585 case LibFunc_roundl:
9586 if (visitUnaryFloatCall(I, ISD::FROUND))
9587 return;
9588 break;
9589 case LibFunc_trunc:
9590 case LibFunc_truncf:
9591 case LibFunc_truncl:
9592 if (visitUnaryFloatCall(I, ISD::FTRUNC))
9593 return;
9594 break;
9595 case LibFunc_log2:
9596 case LibFunc_log2f:
9597 case LibFunc_log2l:
9598 if (visitUnaryFloatCall(I, ISD::FLOG2))
9599 return;
9600 break;
9601 case LibFunc_exp2:
9602 case LibFunc_exp2f:
9603 case LibFunc_exp2l:
9604 if (visitUnaryFloatCall(I, ISD::FEXP2))
9605 return;
9606 break;
9607 case LibFunc_exp10:
9608 case LibFunc_exp10f:
9609 case LibFunc_exp10l:
9610 if (visitUnaryFloatCall(I, ISD::FEXP10))
9611 return;
9612 break;
9613 case LibFunc_ldexp:
9614 case LibFunc_ldexpf:
9615 case LibFunc_ldexpl:
9616 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9617 return;
9618 break;
9619 case LibFunc_memcmp:
9620 if (visitMemCmpBCmpCall(I))
9621 return;
9622 break;
9623 case LibFunc_mempcpy:
9624 if (visitMemPCpyCall(I))
9625 return;
9626 break;
9627 case LibFunc_memchr:
9628 if (visitMemChrCall(I))
9629 return;
9630 break;
9631 case LibFunc_strcpy:
9632 if (visitStrCpyCall(I, false))
9633 return;
9634 break;
9635 case LibFunc_stpcpy:
9636 if (visitStrCpyCall(I, true))
9637 return;
9638 break;
9639 case LibFunc_strcmp:
9640 if (visitStrCmpCall(I))
9641 return;
9642 break;
9643 case LibFunc_strlen:
9644 if (visitStrLenCall(I))
9645 return;
9646 break;
9647 case LibFunc_strnlen:
9648 if (visitStrNLenCall(I))
9649 return;
9650 break;
9651 }
9652 }
9653 }
9654
9655 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9656 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9657 return;
9658 }
9659
9660 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9661 // have to do anything here to lower funclet bundles.
9662 // CFGuardTarget bundles are lowered in LowerCallTo.
9664 I, "calls",
9669
9670 SDValue Callee = getValue(I.getCalledOperand());
9671
9672 if (I.hasDeoptState())
9673 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9674 else
9675 // Check if we can potentially perform a tail call. More detailed checking
9676 // is be done within LowerCallTo, after more information about the call is
9677 // known.
9678 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9679}
9680
9682 const CallBase &CB, const BasicBlock *EHPadBB) {
9683 auto PAB = CB.getOperandBundle("ptrauth");
9684 const Value *CalleeV = CB.getCalledOperand();
9685
9686 // Gather the call ptrauth data from the operand bundle:
9687 // [ i32 <key>, i64 <discriminator> ]
9688 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9689 const Value *Discriminator = PAB->Inputs[1];
9690
9691 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9692 assert(Discriminator->getType()->isIntegerTy(64) &&
9693 "Invalid ptrauth discriminator");
9694
9695 // Look through ptrauth constants to find the raw callee.
9696 // Do a direct unauthenticated call if we found it and everything matches.
9697 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9698 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9699 DAG.getDataLayout()))
9700 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9701 CB.isMustTailCall(), EHPadBB);
9702
9703 // Functions should never be ptrauth-called directly.
9704 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9705
9706 // Otherwise, do an authenticated indirect call.
9707 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9708 getValue(Discriminator)};
9709
9710 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9711 EHPadBB, &PAI);
9712}
9713
9714namespace {
9715
9716/// AsmOperandInfo - This contains information for each constraint that we are
9717/// lowering.
9718class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9719public:
9720 /// CallOperand - If this is the result output operand or a clobber
9721 /// this is null, otherwise it is the incoming operand to the CallInst.
9722 /// This gets modified as the asm is processed.
9723 SDValue CallOperand;
9724
9725 /// AssignedRegs - If this is a register or register class operand, this
9726 /// contains the set of register corresponding to the operand.
9727 RegsForValue AssignedRegs;
9728
9729 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9730 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9731 }
9732
9733 /// Whether or not this operand accesses memory
9734 bool hasMemory(const TargetLowering &TLI) const {
9735 // Indirect operand accesses access memory.
9736 if (isIndirect)
9737 return true;
9738
9739 for (const auto &Code : Codes)
9741 return true;
9742
9743 return false;
9744 }
9745};
9746
9747
9748} // end anonymous namespace
9749
9750/// Make sure that the output operand \p OpInfo and its corresponding input
9751/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9752/// out).
9753static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9754 SDISelAsmOperandInfo &MatchingOpInfo,
9755 SelectionDAG &DAG) {
9756 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9757 return;
9758
9760 const auto &TLI = DAG.getTargetLoweringInfo();
9761
9762 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9763 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9764 OpInfo.ConstraintVT);
9765 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9766 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9767 MatchingOpInfo.ConstraintVT);
9768 const bool OutOpIsIntOrFP =
9769 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9770 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9771 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9772 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9773 // FIXME: error out in a more elegant fashion
9774 report_fatal_error("Unsupported asm: input constraint"
9775 " with a matching output constraint of"
9776 " incompatible type!");
9777 }
9778 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9779}
9780
9781/// Get a direct memory input to behave well as an indirect operand.
9782/// This may introduce stores, hence the need for a \p Chain.
9783/// \return The (possibly updated) chain.
9784static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9785 SDISelAsmOperandInfo &OpInfo,
9786 SelectionDAG &DAG) {
9787 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9788
9789 // If we don't have an indirect input, put it in the constpool if we can,
9790 // otherwise spill it to a stack slot.
9791 // TODO: This isn't quite right. We need to handle these according to
9792 // the addressing mode that the constraint wants. Also, this may take
9793 // an additional register for the computation and we don't want that
9794 // either.
9795
9796 // If the operand is a float, integer, or vector constant, spill to a
9797 // constant pool entry to get its address.
9798 const Value *OpVal = OpInfo.CallOperandVal;
9799 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9801 OpInfo.CallOperand = DAG.getConstantPool(
9802 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9803 return Chain;
9804 }
9805
9806 // Otherwise, create a stack slot and emit a store to it before the asm.
9807 Type *Ty = OpVal->getType();
9808 auto &DL = DAG.getDataLayout();
9809 TypeSize TySize = DL.getTypeAllocSize(Ty);
9812 int StackID = 0;
9813 if (TySize.isScalable())
9814 StackID = TFI->getStackIDForScalableVectors();
9815 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9816 DL.getPrefTypeAlign(Ty), false,
9817 nullptr, StackID);
9818 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9819 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9821 TLI.getMemValueType(DL, Ty));
9822 OpInfo.CallOperand = StackSlot;
9823
9824 return Chain;
9825}
9826
9827/// GetRegistersForValue - Assign registers (virtual or physical) for the
9828/// specified operand. We prefer to assign virtual registers, to allow the
9829/// register allocator to handle the assignment process. However, if the asm
9830/// uses features that we can't model on machineinstrs, we have SDISel do the
9831/// allocation. This produces generally horrible, but correct, code.
9832///
9833/// OpInfo describes the operand
9834/// RefOpInfo describes the matching operand if any, the operand otherwise
9835static std::optional<unsigned>
9837 SDISelAsmOperandInfo &OpInfo,
9838 SDISelAsmOperandInfo &RefOpInfo) {
9839 LLVMContext &Context = *DAG.getContext();
9840 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9841
9845
9846 // No work to do for memory/address operands.
9847 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9848 OpInfo.ConstraintType == TargetLowering::C_Address)
9849 return std::nullopt;
9850
9851 // If this is a constraint for a single physreg, or a constraint for a
9852 // register class, find it.
9853 unsigned AssignedReg;
9854 const TargetRegisterClass *RC;
9855 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
9856 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
9857 // RC is unset only on failure. Return immediately.
9858 if (!RC)
9859 return std::nullopt;
9860
9861 // Get the actual register value type. This is important, because the user
9862 // may have asked for (e.g.) the AX register in i32 type. We need to
9863 // remember that AX is actually i16 to get the right extension.
9864 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
9865
9866 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
9867 // If this is an FP operand in an integer register (or visa versa), or more
9868 // generally if the operand value disagrees with the register class we plan
9869 // to stick it in, fix the operand type.
9870 //
9871 // If this is an input value, the bitcast to the new type is done now.
9872 // Bitcast for output value is done at the end of visitInlineAsm().
9873 if ((OpInfo.Type == InlineAsm::isOutput ||
9874 OpInfo.Type == InlineAsm::isInput) &&
9875 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
9876 // Try to convert to the first EVT that the reg class contains. If the
9877 // types are identical size, use a bitcast to convert (e.g. two differing
9878 // vector types). Note: output bitcast is done at the end of
9879 // visitInlineAsm().
9880 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
9881 // Exclude indirect inputs while they are unsupported because the code
9882 // to perform the load is missing and thus OpInfo.CallOperand still
9883 // refers to the input address rather than the pointed-to value.
9884 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
9885 OpInfo.CallOperand =
9886 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
9887 OpInfo.ConstraintVT = RegVT;
9888 // If the operand is an FP value and we want it in integer registers,
9889 // use the corresponding integer type. This turns an f64 value into
9890 // i64, which can be passed with two i32 values on a 32-bit machine.
9891 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
9892 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
9893 if (OpInfo.Type == InlineAsm::isInput)
9894 OpInfo.CallOperand =
9895 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
9896 OpInfo.ConstraintVT = VT;
9897 }
9898 }
9899 }
9900
9901 // No need to allocate a matching input constraint since the constraint it's
9902 // matching to has already been allocated.
9903 if (OpInfo.isMatchingInputConstraint())
9904 return std::nullopt;
9905
9906 EVT ValueVT = OpInfo.ConstraintVT;
9907 if (OpInfo.ConstraintVT == MVT::Other)
9908 ValueVT = RegVT;
9909
9910 // Initialize NumRegs.
9911 unsigned NumRegs = 1;
9912 if (OpInfo.ConstraintVT != MVT::Other)
9913 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
9914
9915 // If this is a constraint for a specific physical register, like {r17},
9916 // assign it now.
9917
9918 // If this associated to a specific register, initialize iterator to correct
9919 // place. If virtual, make sure we have enough registers
9920
9921 // Initialize iterator if necessary
9924
9925 // Do not check for single registers.
9926 if (AssignedReg) {
9927 I = std::find(I, RC->end(), AssignedReg);
9928 if (I == RC->end()) {
9929 // RC does not contain the selected register, which indicates a
9930 // mismatch between the register and the required type/bitwidth.
9931 return {AssignedReg};
9932 }
9933 }
9934
9935 for (; NumRegs; --NumRegs, ++I) {
9936 assert(I != RC->end() && "Ran out of registers to allocate!");
9937 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
9938 Regs.push_back(R);
9939 }
9940
9941 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
9942 return std::nullopt;
9943}
9944
9945static unsigned
9947 const std::vector<SDValue> &AsmNodeOperands) {
9948 // Scan until we find the definition we already emitted of this operand.
9949 unsigned CurOp = InlineAsm::Op_FirstOperand;
9950 for (; OperandNo; --OperandNo) {
9951 // Advance to the next operand.
9952 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
9953 const InlineAsm::Flag F(OpFlag);
9954 assert(
9955 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
9956 "Skipped past definitions?");
9957 CurOp += F.getNumOperandRegisters() + 1;
9958 }
9959 return CurOp;
9960}
9961
9962namespace {
9963
9964class ExtraFlags {
9965 unsigned Flags = 0;
9966
9967public:
9968 explicit ExtraFlags(const CallBase &Call) {
9969 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
9970 if (IA->hasSideEffects())
9972 if (IA->isAlignStack())
9974 if (Call.isConvergent())
9976 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
9977 }
9978
9979 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
9980 // Ideally, we would only check against memory constraints. However, the
9981 // meaning of an Other constraint can be target-specific and we can't easily
9982 // reason about it. Therefore, be conservative and set MayLoad/MayStore
9983 // for Other constraints as well.
9986 if (OpInfo.Type == InlineAsm::isInput)
9988 else if (OpInfo.Type == InlineAsm::isOutput)
9990 else if (OpInfo.Type == InlineAsm::isClobber)
9992 }
9993 }
9994
9995 unsigned get() const { return Flags; }
9996};
9997
9998} // end anonymous namespace
9999
10000static bool isFunction(SDValue Op) {
10001 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
10002 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10003 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10004
10005 // In normal "call dllimport func" instruction (non-inlineasm) it force
10006 // indirect access by specifing call opcode. And usually specially print
10007 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10008 // not do in this way now. (In fact, this is similar with "Data Access"
10009 // action). So here we ignore dllimport function.
10010 if (Fn && !Fn->hasDLLImportStorageClass())
10011 return true;
10012 }
10013 }
10014 return false;
10015}
10016
10017/// visitInlineAsm - Handle a call to an InlineAsm object.
10018void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10019 const BasicBlock *EHPadBB) {
10020 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10021
10022 /// ConstraintOperands - Information about all of the constraints.
10023 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10024
10025 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10027 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10028
10029 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
10030 // AsmDialect, MayLoad, MayStore).
10031 bool HasSideEffect = IA->hasSideEffects();
10032 ExtraFlags ExtraInfo(Call);
10033
10034 for (auto &T : TargetConstraints) {
10035 ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10036 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
10037
10038 if (OpInfo.CallOperandVal)
10039 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
10040
10041 if (!HasSideEffect)
10042 HasSideEffect = OpInfo.hasMemory(TLI);
10043
10044 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10045 // FIXME: Could we compute this on OpInfo rather than T?
10046
10047 // Compute the constraint code and ConstraintType to use.
10049
10050 if (T.ConstraintType == TargetLowering::C_Immediate &&
10051 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
10052 // We've delayed emitting a diagnostic like the "n" constraint because
10053 // inlining could cause an integer showing up.
10054 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
10055 "' expects an integer constant "
10056 "expression");
10057
10058 ExtraInfo.update(T);
10059 }
10060
10061 // We won't need to flush pending loads if this asm doesn't touch
10062 // memory and is nonvolatile.
10063 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
10064
10065 bool EmitEHLabels = isa<InvokeInst>(Call);
10066 if (EmitEHLabels) {
10067 assert(EHPadBB && "InvokeInst must have an EHPadBB");
10068 }
10069 bool IsCallBr = isa<CallBrInst>(Call);
10070
10071 if (IsCallBr || EmitEHLabels) {
10072 // If this is a callbr or invoke we need to flush pending exports since
10073 // inlineasm_br and invoke are terminators.
10074 // We need to do this before nodes are glued to the inlineasm_br node.
10075 Chain = getControlRoot();
10076 }
10077
10078 MCSymbol *BeginLabel = nullptr;
10079 if (EmitEHLabels) {
10080 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
10081 }
10082
10083 int OpNo = -1;
10084 SmallVector<StringRef> AsmStrs;
10085 IA->collectAsmStrs(AsmStrs);
10086
10087 // Second pass over the constraints: compute which constraint option to use.
10088 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10089 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10090 OpNo++;
10091
10092 // If this is an output operand with a matching input operand, look up the
10093 // matching input. If their types mismatch, e.g. one is an integer, the
10094 // other is floating point, or their sizes are different, flag it as an
10095 // error.
10096 if (OpInfo.hasMatchingInput()) {
10097 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
10098 patchMatchingInput(OpInfo, Input, DAG);
10099 }
10100
10101 // Compute the constraint code and ConstraintType to use.
10102 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10103
10104 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10105 OpInfo.Type == InlineAsm::isClobber) ||
10106 OpInfo.ConstraintType == TargetLowering::C_Address)
10107 continue;
10108
10109 // In Linux PIC model, there are 4 cases about value/label addressing:
10110 //
10111 // 1: Function call or Label jmp inside the module.
10112 // 2: Data access (such as global variable, static variable) inside module.
10113 // 3: Function call or Label jmp outside the module.
10114 // 4: Data access (such as global variable) outside the module.
10115 //
10116 // Due to current llvm inline asm architecture designed to not "recognize"
10117 // the asm code, there are quite troubles for us to treat mem addressing
10118 // differently for same value/adress used in different instuctions.
10119 // For example, in pic model, call a func may in plt way or direclty
10120 // pc-related, but lea/mov a function adress may use got.
10121 //
10122 // Here we try to "recognize" function call for the case 1 and case 3 in
10123 // inline asm. And try to adjust the constraint for them.
10124 //
10125 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10126 // label, so here we don't handle jmp function label now, but we need to
10127 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10128 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10129 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10130 TM.getCodeModel() != CodeModel::Large) {
10131 OpInfo.isIndirect = false;
10132 OpInfo.ConstraintType = TargetLowering::C_Address;
10133 }
10134
10135 // If this is a memory input, and if the operand is not indirect, do what we
10136 // need to provide an address for the memory input.
10137 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10138 !OpInfo.isIndirect) {
10139 assert((OpInfo.isMultipleAlternative ||
10140 (OpInfo.Type == InlineAsm::isInput)) &&
10141 "Can only indirectify direct input operands!");
10142
10143 // Memory operands really want the address of the value.
10144 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
10145
10146 // There is no longer a Value* corresponding to this operand.
10147 OpInfo.CallOperandVal = nullptr;
10148
10149 // It is now an indirect operand.
10150 OpInfo.isIndirect = true;
10151 }
10152
10153 }
10154
10155 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10156 std::vector<SDValue> AsmNodeOperands;
10157 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10158 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10159 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10160
10161 // If we have a !srcloc metadata node associated with it, we want to attach
10162 // this to the ultimately generated inline asm machineinstr. To do this, we
10163 // pass in the third operand as this (potentially null) inline asm MDNode.
10164 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10165 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10166
10167 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10168 // bits as operand 3.
10169 AsmNodeOperands.push_back(DAG.getTargetConstant(
10170 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10171
10172 // Third pass: Loop over operands to prepare DAG-level operands.. As part of
10173 // this, assign virtual and physical registers for inputs and otput.
10174 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10175 // Assign Registers.
10176 SDISelAsmOperandInfo &RefOpInfo =
10177 OpInfo.isMatchingInputConstraint()
10178 ? ConstraintOperands[OpInfo.getMatchedOperand()]
10179 : OpInfo;
10180 const auto RegError =
10181 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
10182 if (RegError) {
10183 const MachineFunction &MF = DAG.getMachineFunction();
10184 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10185 const char *RegName = TRI.getName(*RegError);
10186 emitInlineAsmError(Call, "register '" + Twine(RegName) +
10187 "' allocated for constraint '" +
10188 Twine(OpInfo.ConstraintCode) +
10189 "' does not match required type");
10190 return;
10191 }
10192
10193 auto DetectWriteToReservedRegister = [&]() {
10194 const MachineFunction &MF = DAG.getMachineFunction();
10195 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10196 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10197 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10198 const char *RegName = TRI.getName(Reg);
10199 emitInlineAsmError(Call, "write to reserved register '" +
10200 Twine(RegName) + "'");
10201 return true;
10202 }
10203 }
10204 return false;
10205 };
10206 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10207 (OpInfo.Type == InlineAsm::isInput &&
10208 !OpInfo.isMatchingInputConstraint())) &&
10209 "Only address as input operand is allowed.");
10210
10211 switch (OpInfo.Type) {
10213 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10214 const InlineAsm::ConstraintCode ConstraintID =
10215 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10217 "Failed to convert memory constraint code to constraint id.");
10218
10219 // Add information to the INLINEASM node to know about this output.
10220 InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1);
10221 OpFlags.setMemConstraint(ConstraintID);
10222 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
10223 MVT::i32));
10224 AsmNodeOperands.push_back(OpInfo.CallOperand);
10225 } else {
10226 // Otherwise, this outputs to a register (directly for C_Register /
10227 // C_RegisterClass, and a target-defined fashion for
10228 // C_Immediate/C_Other). Find a register that we can use.
10229 if (OpInfo.AssignedRegs.Regs.empty()) {
10230 emitInlineAsmError(
10231 Call, "couldn't allocate output register for constraint '" +
10232 Twine(OpInfo.ConstraintCode) + "'");
10233 return;
10234 }
10235
10236 if (DetectWriteToReservedRegister())
10237 return;
10238
10239 // Add information to the INLINEASM node to know that this register is
10240 // set.
10241 OpInfo.AssignedRegs.AddInlineAsmOperands(
10242 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10244 false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
10245 }
10246 break;
10247
10248 case InlineAsm::isInput:
10249 case InlineAsm::isLabel: {
10250 SDValue InOperandVal = OpInfo.CallOperand;
10251
10252 if (OpInfo.isMatchingInputConstraint()) {
10253 // If this is required to match an output register we have already set,
10254 // just use its register.
10255 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10256 AsmNodeOperands);
10257 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
10258 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10259 if (OpInfo.isIndirect) {
10260 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10261 emitInlineAsmError(Call, "inline asm not supported yet: "
10262 "don't know how to handle tied "
10263 "indirect register inputs");
10264 return;
10265 }
10266
10268 MachineFunction &MF = DAG.getMachineFunction();
10269 MachineRegisterInfo &MRI = MF.getRegInfo();
10270 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10271 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
10272 Register TiedReg = R->getReg();
10273 MVT RegVT = R->getSimpleValueType(0);
10274 const TargetRegisterClass *RC =
10275 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10276 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10277 : TRI.getMinimalPhysRegClass(TiedReg);
10278 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
10279 Regs.push_back(MRI.createVirtualRegister(RC));
10280
10281 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10282
10283 SDLoc dl = getCurSDLoc();
10284 // Use the produced MatchedRegs object to
10285 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
10286 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
10287 OpInfo.getMatchedOperand(), dl, DAG,
10288 AsmNodeOperands);
10289 break;
10290 }
10291
10292 assert(Flag.isMemKind() && "Unknown matching constraint!");
10293 assert(Flag.getNumOperandRegisters() == 1 &&
10294 "Unexpected number of operands");
10295 // Add information to the INLINEASM node to know about this input.
10296 // See InlineAsm.h isUseOperandTiedToDef.
10297 Flag.clearMemConstraint();
10298 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10299 AsmNodeOperands.push_back(DAG.getTargetConstant(
10300 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10301 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
10302 break;
10303 }
10304
10305 // Treat indirect 'X' constraint as memory.
10306 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10307 OpInfo.isIndirect)
10308 OpInfo.ConstraintType = TargetLowering::C_Memory;
10309
10310 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10311 OpInfo.ConstraintType == TargetLowering::C_Other) {
10312 std::vector<SDValue> Ops;
10313 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10314 Ops, DAG);
10315 if (Ops.empty()) {
10316 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10317 if (isa<ConstantSDNode>(InOperandVal)) {
10318 emitInlineAsmError(Call, "value out of range for constraint '" +
10319 Twine(OpInfo.ConstraintCode) + "'");
10320 return;
10321 }
10322
10323 emitInlineAsmError(Call,
10324 "invalid operand for inline asm constraint '" +
10325 Twine(OpInfo.ConstraintCode) + "'");
10326 return;
10327 }
10328
10329 // Add information to the INLINEASM node to know about this input.
10330 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10331 AsmNodeOperands.push_back(DAG.getTargetConstant(
10332 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10333 llvm::append_range(AsmNodeOperands, Ops);
10334 break;
10335 }
10336
10337 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10338 assert((OpInfo.isIndirect ||
10339 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10340 "Operand must be indirect to be a mem!");
10341 assert(InOperandVal.getValueType() ==
10342 TLI.getPointerTy(DAG.getDataLayout()) &&
10343 "Memory operands expect pointer values");
10344
10345 const InlineAsm::ConstraintCode ConstraintID =
10346 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10348 "Failed to convert memory constraint code to constraint id.");
10349
10350 // Add information to the INLINEASM node to know about this input.
10351 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10352 ResOpType.setMemConstraint(ConstraintID);
10353 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10354 getCurSDLoc(),
10355 MVT::i32));
10356 AsmNodeOperands.push_back(InOperandVal);
10357 break;
10358 }
10359
10360 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10361 const InlineAsm::ConstraintCode ConstraintID =
10362 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10364 "Failed to convert memory constraint code to constraint id.");
10365
10366 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10367
10368 SDValue AsmOp = InOperandVal;
10369 if (isFunction(InOperandVal)) {
10370 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10371 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10372 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10373 InOperandVal.getValueType(),
10374 GA->getOffset());
10375 }
10376
10377 // Add information to the INLINEASM node to know about this input.
10378 ResOpType.setMemConstraint(ConstraintID);
10379
10380 AsmNodeOperands.push_back(
10381 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10382
10383 AsmNodeOperands.push_back(AsmOp);
10384 break;
10385 }
10386
10387 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10388 OpInfo.ConstraintType != TargetLowering::C_Register) {
10389 emitInlineAsmError(Call, "unknown asm constraint '" +
10390 Twine(OpInfo.ConstraintCode) + "'");
10391 return;
10392 }
10393
10394 // TODO: Support this.
10395 if (OpInfo.isIndirect) {
10396 emitInlineAsmError(
10397 Call, "Don't know how to handle indirect register inputs yet "
10398 "for constraint '" +
10399 Twine(OpInfo.ConstraintCode) + "'");
10400 return;
10401 }
10402
10403 // Copy the input into the appropriate registers.
10404 if (OpInfo.AssignedRegs.Regs.empty()) {
10405 emitInlineAsmError(Call,
10406 "couldn't allocate input reg for constraint '" +
10407 Twine(OpInfo.ConstraintCode) + "'");
10408 return;
10409 }
10410
10411 if (DetectWriteToReservedRegister())
10412 return;
10413
10414 SDLoc dl = getCurSDLoc();
10415
10416 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10417 &Call);
10418
10419 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10420 0, dl, DAG, AsmNodeOperands);
10421 break;
10422 }
10424 // Add the clobbered value to the operand list, so that the register
10425 // allocator is aware that the physreg got clobbered.
10426 if (!OpInfo.AssignedRegs.Regs.empty())
10428 false, 0, getCurSDLoc(), DAG,
10429 AsmNodeOperands);
10430 break;
10431 }
10432 }
10433
10434 // Finish up input operands. Set the input chain and add the flag last.
10435 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10436 if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10437
10438 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10439 Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10440 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10441 Glue = Chain.getValue(1);
10442
10443 // Do additional work to generate outputs.
10444
10445 SmallVector<EVT, 1> ResultVTs;
10446 SmallVector<SDValue, 1> ResultValues;
10447 SmallVector<SDValue, 8> OutChains;
10448
10449 llvm::Type *CallResultType = Call.getType();
10450 ArrayRef<Type *> ResultTypes;
10451 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10452 ResultTypes = StructResult->elements();
10453 else if (!CallResultType->isVoidTy())
10454 ResultTypes = ArrayRef(CallResultType);
10455
10456 auto CurResultType = ResultTypes.begin();
10457 auto handleRegAssign = [&](SDValue V) {
10458 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10459 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10460 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10461 ++CurResultType;
10462 // If the type of the inline asm call site return value is different but has
10463 // same size as the type of the asm output bitcast it. One example of this
10464 // is for vectors with different width / number of elements. This can
10465 // happen for register classes that can contain multiple different value
10466 // types. The preg or vreg allocated may not have the same VT as was
10467 // expected.
10468 //
10469 // This can also happen for a return value that disagrees with the register
10470 // class it is put in, eg. a double in a general-purpose register on a
10471 // 32-bit machine.
10472 if (ResultVT != V.getValueType() &&
10473 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10474 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10475 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10476 V.getValueType().isInteger()) {
10477 // If a result value was tied to an input value, the computed result
10478 // may have a wider width than the expected result. Extract the
10479 // relevant portion.
10480 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10481 }
10482 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10483 ResultVTs.push_back(ResultVT);
10484 ResultValues.push_back(V);
10485 };
10486
10487 // Deal with output operands.
10488 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10489 if (OpInfo.Type == InlineAsm::isOutput) {
10490 SDValue Val;
10491 // Skip trivial output operands.
10492 if (OpInfo.AssignedRegs.Regs.empty())
10493 continue;
10494
10495 switch (OpInfo.ConstraintType) {
10498 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10499 Chain, &Glue, &Call);
10500 break;
10503 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10504 OpInfo, DAG);
10505 break;
10507 break; // Already handled.
10509 break; // Silence warning.
10511 assert(false && "Unexpected unknown constraint");
10512 }
10513
10514 // Indirect output manifest as stores. Record output chains.
10515 if (OpInfo.isIndirect) {
10516 const Value *Ptr = OpInfo.CallOperandVal;
10517 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10518 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10519 MachinePointerInfo(Ptr));
10520 OutChains.push_back(Store);
10521 } else {
10522 // generate CopyFromRegs to associated registers.
10523 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10524 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10525 for (const SDValue &V : Val->op_values())
10526 handleRegAssign(V);
10527 } else
10528 handleRegAssign(Val);
10529 }
10530 }
10531 }
10532
10533 // Set results.
10534 if (!ResultValues.empty()) {
10535 assert(CurResultType == ResultTypes.end() &&
10536 "Mismatch in number of ResultTypes");
10537 assert(ResultValues.size() == ResultTypes.size() &&
10538 "Mismatch in number of output operands in asm result");
10539
10541 DAG.getVTList(ResultVTs), ResultValues);
10542 setValue(&Call, V);
10543 }
10544
10545 // Collect store chains.
10546 if (!OutChains.empty())
10547 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10548
10549 if (EmitEHLabels) {
10550 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10551 }
10552
10553 // Only Update Root if inline assembly has a memory effect.
10554 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10555 EmitEHLabels)
10556 DAG.setRoot(Chain);
10557}
10558
10559void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10560 const Twine &Message) {
10561 LLVMContext &Ctx = *DAG.getContext();
10562 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10563
10564 // Make sure we leave the DAG in a valid state
10565 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10566 SmallVector<EVT, 1> ValueVTs;
10567 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10568
10569 if (ValueVTs.empty())
10570 return;
10571
10573 for (const EVT &VT : ValueVTs)
10574 Ops.push_back(DAG.getUNDEF(VT));
10575
10576 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10577}
10578
10579void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10580 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10581 MVT::Other, getRoot(),
10582 getValue(I.getArgOperand(0)),
10583 DAG.getSrcValue(I.getArgOperand(0))));
10584}
10585
10586void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10587 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10588 const DataLayout &DL = DAG.getDataLayout();
10589 SDValue V = DAG.getVAArg(
10590 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10591 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10592 DL.getABITypeAlign(I.getType()).value());
10593 DAG.setRoot(V.getValue(1));
10594
10595 if (I.getType()->isPointerTy())
10596 V = DAG.getPtrExtOrTrunc(
10597 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10598 setValue(&I, V);
10599}
10600
10601void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10602 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10603 MVT::Other, getRoot(),
10604 getValue(I.getArgOperand(0)),
10605 DAG.getSrcValue(I.getArgOperand(0))));
10606}
10607
10608void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10609 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10610 MVT::Other, getRoot(),
10611 getValue(I.getArgOperand(0)),
10612 getValue(I.getArgOperand(1)),
10613 DAG.getSrcValue(I.getArgOperand(0)),
10614 DAG.getSrcValue(I.getArgOperand(1))));
10615}
10616
10618 const Instruction &I,
10619 SDValue Op) {
10620 std::optional<ConstantRange> CR = getRange(I);
10621
10622 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10623 return Op;
10624
10625 APInt Lo = CR->getUnsignedMin();
10626 if (!Lo.isMinValue())
10627 return Op;
10628
10629 APInt Hi = CR->getUnsignedMax();
10630 unsigned Bits = std::max(Hi.getActiveBits(),
10631 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10632
10633 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10634
10635 SDLoc SL = getCurSDLoc();
10636
10637 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10638 DAG.getValueType(SmallVT));
10639 unsigned NumVals = Op.getNode()->getNumValues();
10640 if (NumVals == 1)
10641 return ZExt;
10642
10644
10645 Ops.push_back(ZExt);
10646 for (unsigned I = 1; I != NumVals; ++I)
10647 Ops.push_back(Op.getValue(I));
10648
10649 return DAG.getMergeValues(Ops, SL);
10650}
10651
10652/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10653/// the call being lowered.
10654///
10655/// This is a helper for lowering intrinsics that follow a target calling
10656/// convention or require stack pointer adjustment. Only a subset of the
10657/// intrinsic's operands need to participate in the calling convention.
10660 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10661 AttributeSet RetAttrs, bool IsPatchPoint) {
10663 Args.reserve(NumArgs);
10664
10665 // Populate the argument list.
10666 // Attributes for args start at offset 1, after the return attribute.
10667 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10668 ArgI != ArgE; ++ArgI) {
10669 const Value *V = Call->getOperand(ArgI);
10670
10671 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10672
10673 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10674 Entry.setAttributes(Call, ArgI);
10675 Args.push_back(Entry);
10676 }
10677
10679 .setChain(getRoot())
10680 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10681 RetAttrs)
10682 .setDiscardResult(Call->use_empty())
10683 .setIsPatchPoint(IsPatchPoint)
10685 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10686}
10687
10688/// Add a stack map intrinsic call's live variable operands to a stackmap
10689/// or patchpoint target node's operand list.
10690///
10691/// Constants are converted to TargetConstants purely as an optimization to
10692/// avoid constant materialization and register allocation.
10693///
10694/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10695/// generate addess computation nodes, and so FinalizeISel can convert the
10696/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10697/// address materialization and register allocation, but may also be required
10698/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10699/// alloca in the entry block, then the runtime may assume that the alloca's
10700/// StackMap location can be read immediately after compilation and that the
10701/// location is valid at any point during execution (this is similar to the
10702/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10703/// only available in a register, then the runtime would need to trap when
10704/// execution reaches the StackMap in order to read the alloca's location.
10705static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10707 SelectionDAGBuilder &Builder) {
10708 SelectionDAG &DAG = Builder.DAG;
10709 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10710 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10711
10712 // Things on the stack are pointer-typed, meaning that they are already
10713 // legal and can be emitted directly to target nodes.
10715 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10716 } else {
10717 // Otherwise emit a target independent node to be legalised.
10718 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10719 }
10720 }
10721}
10722
10723/// Lower llvm.experimental.stackmap.
10724void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10725 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10726 // [live variables...])
10727
10728 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10729
10730 SDValue Chain, InGlue, Callee;
10732
10733 SDLoc DL = getCurSDLoc();
10735
10736 // The stackmap intrinsic only records the live variables (the arguments
10737 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10738 // intrinsic, this won't be lowered to a function call. This means we don't
10739 // have to worry about calling conventions and target specific lowering code.
10740 // Instead we perform the call lowering right here.
10741 //
10742 // chain, flag = CALLSEQ_START(chain, 0, 0)
10743 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10744 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10745 //
10746 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10747 InGlue = Chain.getValue(1);
10748
10749 // Add the STACKMAP operands, starting with DAG house-keeping.
10750 Ops.push_back(Chain);
10751 Ops.push_back(InGlue);
10752
10753 // Add the <id>, <numShadowBytes> operands.
10754 //
10755 // These do not require legalisation, and can be emitted directly to target
10756 // constant nodes.
10758 assert(ID.getValueType() == MVT::i64);
10759 SDValue IDConst =
10760 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10761 Ops.push_back(IDConst);
10762
10763 SDValue Shad = getValue(CI.getArgOperand(1));
10764 assert(Shad.getValueType() == MVT::i32);
10765 SDValue ShadConst =
10766 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
10767 Ops.push_back(ShadConst);
10768
10769 // Add the live variables.
10770 addStackMapLiveVars(CI, 2, DL, Ops, *this);
10771
10772 // Create the STACKMAP node.
10773 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10774 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10775 InGlue = Chain.getValue(1);
10776
10777 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10778
10779 // Stackmaps don't generate values, so nothing goes into the NodeMap.
10780
10781 // Set the root to the target-lowered call chain.
10782 DAG.setRoot(Chain);
10783
10784 // Inform the Frame Information that we have a stackmap in this function.
10785 FuncInfo.MF->getFrameInfo().setHasStackMap();
10786}
10787
10788/// Lower llvm.experimental.patchpoint directly to its target opcode.
10789void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10790 const BasicBlock *EHPadBB) {
10791 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10792 // i32 <numBytes>,
10793 // i8* <target>,
10794 // i32 <numArgs>,
10795 // [Args...],
10796 // [live variables...])
10797
10799 bool IsAnyRegCC = CC == CallingConv::AnyReg;
10800 bool HasDef = !CB.getType()->isVoidTy();
10801 SDLoc dl = getCurSDLoc();
10803
10804 // Handle immediate and symbolic callees.
10805 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10806 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10807 /*isTarget=*/true);
10808 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
10809 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
10810 SDLoc(SymbolicCallee),
10811 SymbolicCallee->getValueType(0));
10812
10813 // Get the real number of arguments participating in the call <numArgs>
10815 unsigned NumArgs = NArgVal->getAsZExtVal();
10816
10817 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
10818 // Intrinsics include all meta-operands up to but not including CC.
10819 unsigned NumMetaOpers = PatchPointOpers::CCPos;
10820 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
10821 "Not enough arguments provided to the patchpoint intrinsic");
10822
10823 // For AnyRegCC the arguments are lowered later on manually.
10824 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
10825 Type *ReturnTy =
10826 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
10827
10828 TargetLowering::CallLoweringInfo CLI(DAG);
10829 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
10830 ReturnTy, CB.getAttributes().getRetAttrs(), true);
10831 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
10832
10833 SDNode *CallEnd = Result.second.getNode();
10834 if (CallEnd->getOpcode() == ISD::EH_LABEL)
10835 CallEnd = CallEnd->getOperand(0).getNode();
10836 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
10837 CallEnd = CallEnd->getOperand(0).getNode();
10838
10839 /// Get a call instruction from the call sequence chain.
10840 /// Tail calls are not allowed.
10841 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
10842 "Expected a callseq node.");
10843 SDNode *Call = CallEnd->getOperand(0).getNode();
10844 bool HasGlue = Call->getGluedNode();
10845
10846 // Replace the target specific call node with the patchable intrinsic.
10848
10849 // Push the chain.
10850 Ops.push_back(*(Call->op_begin()));
10851
10852 // Optionally, push the glue (if any).
10853 if (HasGlue)
10854 Ops.push_back(*(Call->op_end() - 1));
10855
10856 // Push the register mask info.
10857 if (HasGlue)
10858 Ops.push_back(*(Call->op_end() - 2));
10859 else
10860 Ops.push_back(*(Call->op_end() - 1));
10861
10862 // Add the <id> and <numBytes> constants.
10864 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
10866 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
10867
10868 // Add the callee.
10869 Ops.push_back(Callee);
10870
10871 // Adjust <numArgs> to account for any arguments that have been passed on the
10872 // stack instead.
10873 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
10874 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
10875 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
10876 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
10877
10878 // Add the calling convention
10879 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
10880
10881 // Add the arguments we omitted previously. The register allocator should
10882 // place these in any free register.
10883 if (IsAnyRegCC)
10884 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
10885 Ops.push_back(getValue(CB.getArgOperand(i)));
10886
10887 // Push the arguments from the call instruction.
10888 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
10889 Ops.append(Call->op_begin() + 2, e);
10890
10891 // Push live variables for the stack map.
10892 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
10893
10894 SDVTList NodeTys;
10895 if (IsAnyRegCC && HasDef) {
10896 // Create the return types based on the intrinsic definition
10897 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10898 SmallVector<EVT, 3> ValueVTs;
10899 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
10900 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
10901
10902 // There is always a chain and a glue type at the end
10903 ValueVTs.push_back(MVT::Other);
10904 ValueVTs.push_back(MVT::Glue);
10905 NodeTys = DAG.getVTList(ValueVTs);
10906 } else
10907 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10908
10909 // Replace the target specific call node with a PATCHPOINT node.
10910 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
10911
10912 // Update the NodeMap.
10913 if (HasDef) {
10914 if (IsAnyRegCC)
10915 setValue(&CB, SDValue(PPV.getNode(), 0));
10916 else
10917 setValue(&CB, Result.first);
10918 }
10919
10920 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
10921 // call sequence. Furthermore the location of the chain and glue can change
10922 // when the AnyReg calling convention is used and the intrinsic returns a
10923 // value.
10924 if (IsAnyRegCC && HasDef) {
10925 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
10926 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
10927 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
10928 } else
10929 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
10930 DAG.DeleteNode(Call);
10931
10932 // Inform the Frame Information that we have a patchpoint in this function.
10933 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
10934}
10935
10936void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
10937 unsigned Intrinsic) {
10938 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10939 SDValue Op1 = getValue(I.getArgOperand(0));
10940 SDValue Op2;
10941 if (I.arg_size() > 1)
10942 Op2 = getValue(I.getArgOperand(1));
10943 SDLoc dl = getCurSDLoc();
10944 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
10945 SDValue Res;
10946 SDNodeFlags SDFlags;
10947 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
10948 SDFlags.copyFMF(*FPMO);
10949
10950 switch (Intrinsic) {
10951 case Intrinsic::vector_reduce_fadd:
10952 if (SDFlags.hasAllowReassociation())
10953 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
10954 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
10955 SDFlags);
10956 else
10957 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
10958 break;
10959 case Intrinsic::vector_reduce_fmul:
10960 if (SDFlags.hasAllowReassociation())
10961 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
10962 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
10963 SDFlags);
10964 else
10965 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
10966 break;
10967 case Intrinsic::vector_reduce_add:
10968 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
10969 break;
10970 case Intrinsic::vector_reduce_mul:
10971 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
10972 break;
10973 case Intrinsic::vector_reduce_and:
10974 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
10975 break;
10976 case Intrinsic::vector_reduce_or:
10977 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
10978 break;
10979 case Intrinsic::vector_reduce_xor:
10980 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
10981 break;
10982 case Intrinsic::vector_reduce_smax:
10983 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
10984 break;
10985 case Intrinsic::vector_reduce_smin:
10986 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
10987 break;
10988 case Intrinsic::vector_reduce_umax:
10989 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
10990 break;
10991 case Intrinsic::vector_reduce_umin:
10992 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
10993 break;
10994 case Intrinsic::vector_reduce_fmax:
10995 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
10996 break;
10997 case Intrinsic::vector_reduce_fmin:
10998 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
10999 break;
11000 case Intrinsic::vector_reduce_fmaximum:
11001 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
11002 break;
11003 case Intrinsic::vector_reduce_fminimum:
11004 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11005 break;
11006 default:
11007 llvm_unreachable("Unhandled vector reduce intrinsic");
11008 }
11009 setValue(&I, Res);
11010}
11011
11012/// Returns an AttributeList representing the attributes applied to the return
11013/// value of the given call.
11016 if (CLI.RetSExt)
11017 Attrs.push_back(Attribute::SExt);
11018 if (CLI.RetZExt)
11019 Attrs.push_back(Attribute::ZExt);
11020 if (CLI.IsInReg)
11021 Attrs.push_back(Attribute::InReg);
11022
11023 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11024 Attrs);
11025}
11026
11027/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11028/// implementation, which just calls LowerCall.
11029/// FIXME: When all targets are
11030/// migrated to using LowerCall, this hook should be integrated into SDISel.
11031std::pair<SDValue, SDValue>
11033 LLVMContext &Context = CLI.RetTy->getContext();
11034
11035 // Handle the incoming return values from the call.
11036 CLI.Ins.clear();
11037 SmallVector<Type *, 4> RetOrigTys;
11039 auto &DL = CLI.DAG.getDataLayout();
11040 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11041
11042 SmallVector<EVT, 4> RetVTs;
11043 if (CLI.RetTy != CLI.OrigRetTy) {
11044 assert(RetOrigTys.size() == 1 &&
11045 "Only supported for non-aggregate returns");
11046 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11047 } else {
11048 for (Type *Ty : RetOrigTys)
11049 RetVTs.push_back(getValueType(DL, Ty));
11050 }
11051
11052 if (CLI.IsPostTypeLegalization) {
11053 // If we are lowering a libcall after legalization, split the return type.
11054 SmallVector<Type *, 4> OldRetOrigTys;
11055 SmallVector<EVT, 4> OldRetVTs;
11056 SmallVector<TypeSize, 4> OldOffsets;
11057 RetOrigTys.swap(OldRetOrigTys);
11058 RetVTs.swap(OldRetVTs);
11059 Offsets.swap(OldOffsets);
11060
11061 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11062 EVT RetVT = OldRetVTs[i];
11063 uint64_t Offset = OldOffsets[i];
11064 MVT RegisterVT = getRegisterType(Context, RetVT);
11065 unsigned NumRegs = getNumRegisters(Context, RetVT);
11066 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11067 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11068 RetVTs.append(NumRegs, RegisterVT);
11069 for (unsigned j = 0; j != NumRegs; ++j)
11070 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11071 }
11072 }
11073
11075 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11076
11077 bool CanLowerReturn =
11079 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11080
11081 SDValue DemoteStackSlot;
11082 int DemoteStackIdx = -100;
11083 if (!CanLowerReturn) {
11084 // FIXME: equivalent assert?
11085 // assert(!CS.hasInAllocaArgument() &&
11086 // "sret demotion is incompatible with inalloca");
11087 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11088 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11090 DemoteStackIdx =
11091 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11092 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11093
11094 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11095 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11096 Entry.IsSRet = true;
11097 Entry.Alignment = Alignment;
11098 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11099 CLI.NumFixedArgs += 1;
11100 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11101 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11102
11103 // sret demotion isn't compatible with tail-calls, since the sret argument
11104 // points into the callers stack frame.
11105 CLI.IsTailCall = false;
11106 } else {
11107 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11108 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11109 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11110 ISD::ArgFlagsTy Flags;
11111 if (NeedsRegBlock) {
11112 Flags.setInConsecutiveRegs();
11113 if (I == RetVTs.size() - 1)
11114 Flags.setInConsecutiveRegsLast();
11115 }
11116 EVT VT = RetVTs[I];
11117 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11118 unsigned NumRegs =
11119 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11120 for (unsigned i = 0; i != NumRegs; ++i) {
11121 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11123 if (CLI.RetTy->isPointerTy()) {
11124 Ret.Flags.setPointer();
11125 Ret.Flags.setPointerAddrSpace(
11126 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11127 }
11128 if (CLI.RetSExt)
11129 Ret.Flags.setSExt();
11130 if (CLI.RetZExt)
11131 Ret.Flags.setZExt();
11132 if (CLI.IsInReg)
11133 Ret.Flags.setInReg();
11134 CLI.Ins.push_back(Ret);
11135 }
11136 }
11137 }
11138
11139 // We push in swifterror return as the last element of CLI.Ins.
11140 ArgListTy &Args = CLI.getArgs();
11141 if (supportSwiftError()) {
11142 for (const ArgListEntry &Arg : Args) {
11143 if (Arg.IsSwiftError) {
11144 ISD::ArgFlagsTy Flags;
11145 Flags.setSwiftError();
11147 PointerType::getUnqual(Context),
11148 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11149 CLI.Ins.push_back(Ret);
11150 }
11151 }
11152 }
11153
11154 // Handle all of the outgoing arguments.
11155 CLI.Outs.clear();
11156 CLI.OutVals.clear();
11157 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11158 SmallVector<Type *, 4> OrigArgTys;
11159 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11160 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11161 Type *FinalType = Args[i].Ty;
11162 if (Args[i].IsByVal)
11163 FinalType = Args[i].IndirectType;
11164 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11165 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11166 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11167 ++Value) {
11168 Type *OrigArgTy = OrigArgTys[Value];
11169 Type *ArgTy = OrigArgTy;
11170 if (Args[i].Ty != Args[i].OrigTy) {
11171 assert(Value == 0 && "Only supported for non-aggregate arguments");
11172 ArgTy = Args[i].Ty;
11173 }
11174
11175 EVT VT = getValueType(DL, ArgTy);
11176 SDValue Op = SDValue(Args[i].Node.getNode(),
11177 Args[i].Node.getResNo() + Value);
11178 ISD::ArgFlagsTy Flags;
11179
11180 // Certain targets (such as MIPS), may have a different ABI alignment
11181 // for a type depending on the context. Give the target a chance to
11182 // specify the alignment it wants.
11183 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11184 Flags.setOrigAlign(OriginalAlignment);
11185
11186 if (i >= CLI.NumFixedArgs)
11187 Flags.setVarArg();
11188 if (ArgTy->isPointerTy()) {
11189 Flags.setPointer();
11190 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11191 }
11192 if (Args[i].IsZExt)
11193 Flags.setZExt();
11194 if (Args[i].IsSExt)
11195 Flags.setSExt();
11196 if (Args[i].IsNoExt)
11197 Flags.setNoExt();
11198 if (Args[i].IsInReg) {
11199 // If we are using vectorcall calling convention, a structure that is
11200 // passed InReg - is surely an HVA
11202 isa<StructType>(FinalType)) {
11203 // The first value of a structure is marked
11204 if (0 == Value)
11205 Flags.setHvaStart();
11206 Flags.setHva();
11207 }
11208 // Set InReg Flag
11209 Flags.setInReg();
11210 }
11211 if (Args[i].IsSRet)
11212 Flags.setSRet();
11213 if (Args[i].IsSwiftSelf)
11214 Flags.setSwiftSelf();
11215 if (Args[i].IsSwiftAsync)
11216 Flags.setSwiftAsync();
11217 if (Args[i].IsSwiftError)
11218 Flags.setSwiftError();
11219 if (Args[i].IsCFGuardTarget)
11220 Flags.setCFGuardTarget();
11221 if (Args[i].IsByVal)
11222 Flags.setByVal();
11223 if (Args[i].IsByRef)
11224 Flags.setByRef();
11225 if (Args[i].IsPreallocated) {
11226 Flags.setPreallocated();
11227 // Set the byval flag for CCAssignFn callbacks that don't know about
11228 // preallocated. This way we can know how many bytes we should've
11229 // allocated and how many bytes a callee cleanup function will pop. If
11230 // we port preallocated to more targets, we'll have to add custom
11231 // preallocated handling in the various CC lowering callbacks.
11232 Flags.setByVal();
11233 }
11234 if (Args[i].IsInAlloca) {
11235 Flags.setInAlloca();
11236 // Set the byval flag for CCAssignFn callbacks that don't know about
11237 // inalloca. This way we can know how many bytes we should've allocated
11238 // and how many bytes a callee cleanup function will pop. If we port
11239 // inalloca to more targets, we'll have to add custom inalloca handling
11240 // in the various CC lowering callbacks.
11241 Flags.setByVal();
11242 }
11243 Align MemAlign;
11244 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11245 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11246 Flags.setByValSize(FrameSize);
11247
11248 // info is not there but there are cases it cannot get right.
11249 if (auto MA = Args[i].Alignment)
11250 MemAlign = *MA;
11251 else
11252 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11253 } else if (auto MA = Args[i].Alignment) {
11254 MemAlign = *MA;
11255 } else {
11256 MemAlign = OriginalAlignment;
11257 }
11258 Flags.setMemAlign(MemAlign);
11259 if (Args[i].IsNest)
11260 Flags.setNest();
11261 if (NeedsRegBlock)
11262 Flags.setInConsecutiveRegs();
11263
11264 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11265 unsigned NumParts =
11266 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11267 SmallVector<SDValue, 4> Parts(NumParts);
11268 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11269
11270 if (Args[i].IsSExt)
11271 ExtendKind = ISD::SIGN_EXTEND;
11272 else if (Args[i].IsZExt)
11273 ExtendKind = ISD::ZERO_EXTEND;
11274
11275 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11276 // for now.
11277 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11279 assert((CLI.RetTy == Args[i].Ty ||
11280 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11282 Args[i].Ty->getPointerAddressSpace())) &&
11283 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11284 // Before passing 'returned' to the target lowering code, ensure that
11285 // either the register MVT and the actual EVT are the same size or that
11286 // the return value and argument are extended in the same way; in these
11287 // cases it's safe to pass the argument register value unchanged as the
11288 // return register value (although it's at the target's option whether
11289 // to do so)
11290 // TODO: allow code generation to take advantage of partially preserved
11291 // registers rather than clobbering the entire register when the
11292 // parameter extension method is not compatible with the return
11293 // extension method
11294 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11295 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11296 CLI.RetZExt == Args[i].IsZExt))
11297 Flags.setReturned();
11298 }
11299
11300 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11301 CLI.CallConv, ExtendKind);
11302
11303 for (unsigned j = 0; j != NumParts; ++j) {
11304 // if it isn't first piece, alignment must be 1
11305 // For scalable vectors the scalable part is currently handled
11306 // by individual targets, so we just use the known minimum size here.
11307 ISD::OutputArg MyFlags(
11308 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11309 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11310 if (NumParts > 1 && j == 0)
11311 MyFlags.Flags.setSplit();
11312 else if (j != 0) {
11313 MyFlags.Flags.setOrigAlign(Align(1));
11314 if (j == NumParts - 1)
11315 MyFlags.Flags.setSplitEnd();
11316 }
11317
11318 CLI.Outs.push_back(MyFlags);
11319 CLI.OutVals.push_back(Parts[j]);
11320 }
11321
11322 if (NeedsRegBlock && Value == NumValues - 1)
11323 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11324 }
11325 }
11326
11328 CLI.Chain = LowerCall(CLI, InVals);
11329
11330 // Update CLI.InVals to use outside of this function.
11331 CLI.InVals = InVals;
11332
11333 // Verify that the target's LowerCall behaved as expected.
11334 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11335 "LowerCall didn't return a valid chain!");
11336 assert((!CLI.IsTailCall || InVals.empty()) &&
11337 "LowerCall emitted a return value for a tail call!");
11338 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11339 "LowerCall didn't emit the correct number of values!");
11340
11341 // For a tail call, the return value is merely live-out and there aren't
11342 // any nodes in the DAG representing it. Return a special value to
11343 // indicate that a tail call has been emitted and no more Instructions
11344 // should be processed in the current block.
11345 if (CLI.IsTailCall) {
11346 CLI.DAG.setRoot(CLI.Chain);
11347 return std::make_pair(SDValue(), SDValue());
11348 }
11349
11350#ifndef NDEBUG
11351 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11352 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11353 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11354 "LowerCall emitted a value with the wrong type!");
11355 }
11356#endif
11357
11358 SmallVector<SDValue, 4> ReturnValues;
11359 if (!CanLowerReturn) {
11360 // The instruction result is the result of loading from the
11361 // hidden sret parameter.
11362 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11363
11364 unsigned NumValues = RetVTs.size();
11365 ReturnValues.resize(NumValues);
11366 SmallVector<SDValue, 4> Chains(NumValues);
11367
11368 // An aggregate return value cannot wrap around the address space, so
11369 // offsets to its parts don't wrap either.
11371 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11372 for (unsigned i = 0; i < NumValues; ++i) {
11374 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11376 SDValue L = CLI.DAG.getLoad(
11377 RetVTs[i], CLI.DL, CLI.Chain, Add,
11379 DemoteStackIdx, Offsets[i]),
11380 HiddenSRetAlign);
11381 ReturnValues[i] = L;
11382 Chains[i] = L.getValue(1);
11383 }
11384
11385 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11386 } else {
11387 // Collect the legal value parts into potentially illegal values
11388 // that correspond to the original function's return values.
11389 std::optional<ISD::NodeType> AssertOp;
11390 if (CLI.RetSExt)
11391 AssertOp = ISD::AssertSext;
11392 else if (CLI.RetZExt)
11393 AssertOp = ISD::AssertZext;
11394 unsigned CurReg = 0;
11395 for (EVT VT : RetVTs) {
11396 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11397 unsigned NumRegs =
11398 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11399
11400 ReturnValues.push_back(getCopyFromParts(
11401 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11402 CLI.Chain, CLI.CallConv, AssertOp));
11403 CurReg += NumRegs;
11404 }
11405
11406 // For a function returning void, there is no return value. We can't create
11407 // such a node, so we just return a null return value in that case. In
11408 // that case, nothing will actually look at the value.
11409 if (ReturnValues.empty())
11410 return std::make_pair(SDValue(), CLI.Chain);
11411 }
11412
11413 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11414 CLI.DAG.getVTList(RetVTs), ReturnValues);
11415 return std::make_pair(Res, CLI.Chain);
11416}
11417
11418/// Places new result values for the node in Results (their number
11419/// and types must exactly match those of the original return values of
11420/// the node), or leaves Results empty, which indicates that the node is not
11421/// to be custom lowered after all.
11424 SelectionDAG &DAG) const {
11425 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11426
11427 if (!Res.getNode())
11428 return;
11429
11430 // If the original node has one result, take the return value from
11431 // LowerOperation as is. It might not be result number 0.
11432 if (N->getNumValues() == 1) {
11433 Results.push_back(Res);
11434 return;
11435 }
11436
11437 // If the original node has multiple results, then the return node should
11438 // have the same number of results.
11439 assert((N->getNumValues() == Res->getNumValues()) &&
11440 "Lowering returned the wrong number of results!");
11441
11442 // Places new result values base on N result number.
11443 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11444 Results.push_back(Res.getValue(I));
11445}
11446
11448 llvm_unreachable("LowerOperation not implemented for this target!");
11449}
11450
11452 Register Reg,
11453 ISD::NodeType ExtendType) {
11455 assert((Op.getOpcode() != ISD::CopyFromReg ||
11456 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11457 "Copy from a reg to the same reg!");
11458 assert(!Reg.isPhysical() && "Is a physreg");
11459
11460 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11461 // If this is an InlineAsm we have to match the registers required, not the
11462 // notional registers required by the type.
11463
11464 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11465 std::nullopt); // This is not an ABI copy.
11466 SDValue Chain = DAG.getEntryNode();
11467
11468 if (ExtendType == ISD::ANY_EXTEND) {
11469 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11470 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11471 ExtendType = PreferredExtendIt->second;
11472 }
11473 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11474 PendingExports.push_back(Chain);
11475}
11476
11478
11479/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11480/// entry block, return true. This includes arguments used by switches, since
11481/// the switch may expand into multiple basic blocks.
11482static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11483 // With FastISel active, we may be splitting blocks, so force creation
11484 // of virtual registers for all non-dead arguments.
11485 if (FastISel)
11486 return A->use_empty();
11487
11488 const BasicBlock &Entry = A->getParent()->front();
11489 for (const User *U : A->users())
11490 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11491 return false; // Use not in entry block.
11492
11493 return true;
11494}
11495
11497 DenseMap<const Argument *,
11498 std::pair<const AllocaInst *, const StoreInst *>>;
11499
11500/// Scan the entry block of the function in FuncInfo for arguments that look
11501/// like copies into a local alloca. Record any copied arguments in
11502/// ArgCopyElisionCandidates.
11503static void
11505 FunctionLoweringInfo *FuncInfo,
11506 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11507 // Record the state of every static alloca used in the entry block. Argument
11508 // allocas are all used in the entry block, so we need approximately as many
11509 // entries as we have arguments.
11510 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11512 unsigned NumArgs = FuncInfo->Fn->arg_size();
11513 StaticAllocas.reserve(NumArgs * 2);
11514
11515 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11516 if (!V)
11517 return nullptr;
11518 V = V->stripPointerCasts();
11519 const auto *AI = dyn_cast<AllocaInst>(V);
11520 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11521 return nullptr;
11522 auto Iter = StaticAllocas.insert({AI, Unknown});
11523 return &Iter.first->second;
11524 };
11525
11526 // Look for stores of arguments to static allocas. Look through bitcasts and
11527 // GEPs to handle type coercions, as long as the alloca is fully initialized
11528 // by the store. Any non-store use of an alloca escapes it and any subsequent
11529 // unanalyzed store might write it.
11530 // FIXME: Handle structs initialized with multiple stores.
11531 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11532 // Look for stores, and handle non-store uses conservatively.
11533 const auto *SI = dyn_cast<StoreInst>(&I);
11534 if (!SI) {
11535 // We will look through cast uses, so ignore them completely.
11536 if (I.isCast())
11537 continue;
11538 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11539 // to allocas.
11540 if (I.isDebugOrPseudoInst())
11541 continue;
11542 // This is an unknown instruction. Assume it escapes or writes to all
11543 // static alloca operands.
11544 for (const Use &U : I.operands()) {
11545 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11546 *Info = StaticAllocaInfo::Clobbered;
11547 }
11548 continue;
11549 }
11550
11551 // If the stored value is a static alloca, mark it as escaped.
11552 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11553 *Info = StaticAllocaInfo::Clobbered;
11554
11555 // Check if the destination is a static alloca.
11556 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11557 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11558 if (!Info)
11559 continue;
11560 const AllocaInst *AI = cast<AllocaInst>(Dst);
11561
11562 // Skip allocas that have been initialized or clobbered.
11563 if (*Info != StaticAllocaInfo::Unknown)
11564 continue;
11565
11566 // Check if the stored value is an argument, and that this store fully
11567 // initializes the alloca.
11568 // If the argument type has padding bits we can't directly forward a pointer
11569 // as the upper bits may contain garbage.
11570 // Don't elide copies from the same argument twice.
11571 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11572 const auto *Arg = dyn_cast<Argument>(Val);
11573 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11574 Arg->getType()->isEmptyTy() ||
11575 DL.getTypeStoreSize(Arg->getType()) !=
11576 DL.getTypeAllocSize(AI->getAllocatedType()) ||
11577 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11578 ArgCopyElisionCandidates.count(Arg)) {
11579 *Info = StaticAllocaInfo::Clobbered;
11580 continue;
11581 }
11582
11583 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11584 << '\n');
11585
11586 // Mark this alloca and store for argument copy elision.
11587 *Info = StaticAllocaInfo::Elidable;
11588 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11589
11590 // Stop scanning if we've seen all arguments. This will happen early in -O0
11591 // builds, which is useful, because -O0 builds have large entry blocks and
11592 // many allocas.
11593 if (ArgCopyElisionCandidates.size() == NumArgs)
11594 break;
11595 }
11596}
11597
11598/// Try to elide argument copies from memory into a local alloca. Succeeds if
11599/// ArgVal is a load from a suitable fixed stack object.
11602 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11603 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11604 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11605 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11606 // Check if this is a load from a fixed stack object.
11607 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11608 if (!LNode)
11609 return;
11610 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11611 if (!FINode)
11612 return;
11613
11614 // Check that the fixed stack object is the right size and alignment.
11615 // Look at the alignment that the user wrote on the alloca instead of looking
11616 // at the stack object.
11617 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11618 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11619 const AllocaInst *AI = ArgCopyIter->second.first;
11620 int FixedIndex = FINode->getIndex();
11621 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11622 int OldIndex = AllocaIndex;
11623 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11624 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11625 LLVM_DEBUG(
11626 dbgs() << " argument copy elision failed due to bad fixed stack "
11627 "object size\n");
11628 return;
11629 }
11630 Align RequiredAlignment = AI->getAlign();
11631 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11632 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11633 "greater than stack argument alignment ("
11634 << DebugStr(RequiredAlignment) << " vs "
11635 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11636 return;
11637 }
11638
11639 // Perform the elision. Delete the old stack object and replace its only use
11640 // in the variable info map. Mark the stack object as mutable and aliased.
11641 LLVM_DEBUG({
11642 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11643 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11644 << '\n';
11645 });
11646 MFI.RemoveStackObject(OldIndex);
11647 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11648 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11649 AllocaIndex = FixedIndex;
11650 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11651 for (SDValue ArgVal : ArgVals)
11652 Chains.push_back(ArgVal.getValue(1));
11653
11654 // Avoid emitting code for the store implementing the copy.
11655 const StoreInst *SI = ArgCopyIter->second.second;
11656 ElidedArgCopyInstrs.insert(SI);
11657
11658 // Check for uses of the argument again so that we can avoid exporting ArgVal
11659 // if it is't used by anything other than the store.
11660 for (const Value *U : Arg.users()) {
11661 if (U != SI) {
11662 ArgHasUses = true;
11663 break;
11664 }
11665 }
11666}
11667
11668void SelectionDAGISel::LowerArguments(const Function &F) {
11669 SelectionDAG &DAG = SDB->DAG;
11670 SDLoc dl = SDB->getCurSDLoc();
11671 const DataLayout &DL = DAG.getDataLayout();
11673
11674 // In Naked functions we aren't going to save any registers.
11675 if (F.hasFnAttribute(Attribute::Naked))
11676 return;
11677
11678 if (!FuncInfo->CanLowerReturn) {
11679 // Put in an sret pointer parameter before all the other parameters.
11680 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11681
11682 ISD::ArgFlagsTy Flags;
11683 Flags.setSRet();
11684 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11685 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11687 Ins.push_back(RetArg);
11688 }
11689
11690 // Look for stores of arguments to static allocas. Mark such arguments with a
11691 // flag to ask the target to give us the memory location of that argument if
11692 // available.
11693 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11695 ArgCopyElisionCandidates);
11696
11697 // Set up the incoming argument description vector.
11698 for (const Argument &Arg : F.args()) {
11699 unsigned ArgNo = Arg.getArgNo();
11701 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11702 bool isArgValueUsed = !Arg.use_empty();
11703 unsigned PartBase = 0;
11704 Type *FinalType = Arg.getType();
11705 if (Arg.hasAttribute(Attribute::ByVal))
11706 FinalType = Arg.getParamByValType();
11707 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11708 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11709 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11710 ++Value) {
11711 Type *ArgTy = Types[Value];
11712 EVT VT = TLI->getValueType(DL, ArgTy);
11713 ISD::ArgFlagsTy Flags;
11714
11715 if (ArgTy->isPointerTy()) {
11716 Flags.setPointer();
11717 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11718 }
11719 if (Arg.hasAttribute(Attribute::ZExt))
11720 Flags.setZExt();
11721 if (Arg.hasAttribute(Attribute::SExt))
11722 Flags.setSExt();
11723 if (Arg.hasAttribute(Attribute::InReg)) {
11724 // If we are using vectorcall calling convention, a structure that is
11725 // passed InReg - is surely an HVA
11726 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11727 isa<StructType>(Arg.getType())) {
11728 // The first value of a structure is marked
11729 if (0 == Value)
11730 Flags.setHvaStart();
11731 Flags.setHva();
11732 }
11733 // Set InReg Flag
11734 Flags.setInReg();
11735 }
11736 if (Arg.hasAttribute(Attribute::StructRet))
11737 Flags.setSRet();
11738 if (Arg.hasAttribute(Attribute::SwiftSelf))
11739 Flags.setSwiftSelf();
11740 if (Arg.hasAttribute(Attribute::SwiftAsync))
11741 Flags.setSwiftAsync();
11742 if (Arg.hasAttribute(Attribute::SwiftError))
11743 Flags.setSwiftError();
11744 if (Arg.hasAttribute(Attribute::ByVal))
11745 Flags.setByVal();
11746 if (Arg.hasAttribute(Attribute::ByRef))
11747 Flags.setByRef();
11748 if (Arg.hasAttribute(Attribute::InAlloca)) {
11749 Flags.setInAlloca();
11750 // Set the byval flag for CCAssignFn callbacks that don't know about
11751 // inalloca. This way we can know how many bytes we should've allocated
11752 // and how many bytes a callee cleanup function will pop. If we port
11753 // inalloca to more targets, we'll have to add custom inalloca handling
11754 // in the various CC lowering callbacks.
11755 Flags.setByVal();
11756 }
11757 if (Arg.hasAttribute(Attribute::Preallocated)) {
11758 Flags.setPreallocated();
11759 // Set the byval flag for CCAssignFn callbacks that don't know about
11760 // preallocated. This way we can know how many bytes we should've
11761 // allocated and how many bytes a callee cleanup function will pop. If
11762 // we port preallocated to more targets, we'll have to add custom
11763 // preallocated handling in the various CC lowering callbacks.
11764 Flags.setByVal();
11765 }
11766
11767 // Certain targets (such as MIPS), may have a different ABI alignment
11768 // for a type depending on the context. Give the target a chance to
11769 // specify the alignment it wants.
11770 const Align OriginalAlignment(
11771 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
11772 Flags.setOrigAlign(OriginalAlignment);
11773
11774 Align MemAlign;
11775 Type *ArgMemTy = nullptr;
11776 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11777 Flags.isByRef()) {
11778 if (!ArgMemTy)
11779 ArgMemTy = Arg.getPointeeInMemoryValueType();
11780
11781 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11782
11783 // For in-memory arguments, size and alignment should be passed from FE.
11784 // BE will guess if this info is not there but there are cases it cannot
11785 // get right.
11786 if (auto ParamAlign = Arg.getParamStackAlign())
11787 MemAlign = *ParamAlign;
11788 else if ((ParamAlign = Arg.getParamAlign()))
11789 MemAlign = *ParamAlign;
11790 else
11791 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
11792 if (Flags.isByRef())
11793 Flags.setByRefSize(MemSize);
11794 else
11795 Flags.setByValSize(MemSize);
11796 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11797 MemAlign = *ParamAlign;
11798 } else {
11799 MemAlign = OriginalAlignment;
11800 }
11801 Flags.setMemAlign(MemAlign);
11802
11803 if (Arg.hasAttribute(Attribute::Nest))
11804 Flags.setNest();
11805 if (NeedsRegBlock)
11806 Flags.setInConsecutiveRegs();
11807 if (ArgCopyElisionCandidates.count(&Arg))
11808 Flags.setCopyElisionCandidate();
11809 if (Arg.hasAttribute(Attribute::Returned))
11810 Flags.setReturned();
11811
11812 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
11813 *CurDAG->getContext(), F.getCallingConv(), VT);
11814 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
11815 *CurDAG->getContext(), F.getCallingConv(), VT);
11816 for (unsigned i = 0; i != NumRegs; ++i) {
11817 // For scalable vectors, use the minimum size; individual targets
11818 // are responsible for handling scalable vector arguments and
11819 // return values.
11820 ISD::InputArg MyFlags(
11821 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
11822 PartBase + i * RegisterVT.getStoreSize().getKnownMinValue());
11823 if (NumRegs > 1 && i == 0)
11824 MyFlags.Flags.setSplit();
11825 // if it isn't first piece, alignment must be 1
11826 else if (i > 0) {
11827 MyFlags.Flags.setOrigAlign(Align(1));
11828 if (i == NumRegs - 1)
11829 MyFlags.Flags.setSplitEnd();
11830 }
11831 Ins.push_back(MyFlags);
11832 }
11833 if (NeedsRegBlock && Value == NumValues - 1)
11834 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
11835 PartBase += VT.getStoreSize().getKnownMinValue();
11836 }
11837 }
11838
11839 // Call the target to set up the argument values.
11841 SDValue NewRoot = TLI->LowerFormalArguments(
11842 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
11843
11844 // Verify that the target's LowerFormalArguments behaved as expected.
11845 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
11846 "LowerFormalArguments didn't return a valid chain!");
11847 assert(InVals.size() == Ins.size() &&
11848 "LowerFormalArguments didn't emit the correct number of values!");
11849 LLVM_DEBUG({
11850 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
11851 assert(InVals[i].getNode() &&
11852 "LowerFormalArguments emitted a null value!");
11853 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
11854 "LowerFormalArguments emitted a value with the wrong type!");
11855 }
11856 });
11857
11858 // Update the DAG with the new chain value resulting from argument lowering.
11859 DAG.setRoot(NewRoot);
11860
11861 // Set up the argument values.
11862 unsigned i = 0;
11863 if (!FuncInfo->CanLowerReturn) {
11864 // Create a virtual register for the sret pointer, and put in a copy
11865 // from the sret argument into it.
11866 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11867 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
11868 std::optional<ISD::NodeType> AssertOp;
11869 SDValue ArgValue =
11870 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
11871 F.getCallingConv(), AssertOp);
11872
11873 MachineFunction& MF = SDB->DAG.getMachineFunction();
11874 MachineRegisterInfo& RegInfo = MF.getRegInfo();
11875 Register SRetReg =
11876 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
11877 FuncInfo->DemoteRegister = SRetReg;
11878 NewRoot =
11879 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
11880 DAG.setRoot(NewRoot);
11881
11882 // i indexes lowered arguments. Bump it past the hidden sret argument.
11883 ++i;
11884 }
11885
11887 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
11888 for (const Argument &Arg : F.args()) {
11889 SmallVector<SDValue, 4> ArgValues;
11890 SmallVector<EVT, 4> ValueVTs;
11891 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
11892 unsigned NumValues = ValueVTs.size();
11893 if (NumValues == 0)
11894 continue;
11895
11896 bool ArgHasUses = !Arg.use_empty();
11897
11898 // Elide the copying store if the target loaded this argument from a
11899 // suitable fixed stack object.
11900 if (Ins[i].Flags.isCopyElisionCandidate()) {
11901 unsigned NumParts = 0;
11902 for (EVT VT : ValueVTs)
11903 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
11904 F.getCallingConv(), VT);
11905
11906 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
11907 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
11908 ArrayRef(&InVals[i], NumParts), ArgHasUses);
11909 }
11910
11911 // If this argument is unused then remember its value. It is used to generate
11912 // debugging information.
11913 bool isSwiftErrorArg =
11914 TLI->supportSwiftError() &&
11915 Arg.hasAttribute(Attribute::SwiftError);
11916 if (!ArgHasUses && !isSwiftErrorArg) {
11917 SDB->setUnusedArgValue(&Arg, InVals[i]);
11918
11919 // Also remember any frame index for use in FastISel.
11920 if (FrameIndexSDNode *FI =
11922 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11923 }
11924
11925 for (unsigned Val = 0; Val != NumValues; ++Val) {
11926 EVT VT = ValueVTs[Val];
11927 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
11928 F.getCallingConv(), VT);
11929 unsigned NumParts = TLI->getNumRegistersForCallingConv(
11930 *CurDAG->getContext(), F.getCallingConv(), VT);
11931
11932 // Even an apparent 'unused' swifterror argument needs to be returned. So
11933 // we do generate a copy for it that can be used on return from the
11934 // function.
11935 if (ArgHasUses || isSwiftErrorArg) {
11936 std::optional<ISD::NodeType> AssertOp;
11937 if (Arg.hasAttribute(Attribute::SExt))
11938 AssertOp = ISD::AssertSext;
11939 else if (Arg.hasAttribute(Attribute::ZExt))
11940 AssertOp = ISD::AssertZext;
11941
11942 SDValue OutVal =
11943 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
11944 NewRoot, F.getCallingConv(), AssertOp);
11945
11946 FPClassTest NoFPClass = Arg.getNoFPClass();
11947 if (NoFPClass != fcNone) {
11948 SDValue SDNoFPClass = DAG.getTargetConstant(
11949 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
11950 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
11951 OutVal, SDNoFPClass);
11952 }
11953 ArgValues.push_back(OutVal);
11954 }
11955
11956 i += NumParts;
11957 }
11958
11959 // We don't need to do anything else for unused arguments.
11960 if (ArgValues.empty())
11961 continue;
11962
11963 // Note down frame index.
11964 if (FrameIndexSDNode *FI =
11965 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
11966 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11967
11968 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
11969 SDB->getCurSDLoc());
11970
11971 SDB->setValue(&Arg, Res);
11972 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
11973 // We want to associate the argument with the frame index, among
11974 // involved operands, that correspond to the lowest address. The
11975 // getCopyFromParts function, called earlier, is swapping the order of
11976 // the operands to BUILD_PAIR depending on endianness. The result of
11977 // that swapping is that the least significant bits of the argument will
11978 // be in the first operand of the BUILD_PAIR node, and the most
11979 // significant bits will be in the second operand.
11980 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
11981 if (LoadSDNode *LNode =
11982 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
11983 if (FrameIndexSDNode *FI =
11984 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
11985 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11986 }
11987
11988 // Analyses past this point are naive and don't expect an assertion.
11989 if (Res.getOpcode() == ISD::AssertZext)
11990 Res = Res.getOperand(0);
11991
11992 // Update the SwiftErrorVRegDefMap.
11993 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
11994 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
11995 if (Reg.isVirtual())
11996 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
11997 Reg);
11998 }
11999
12000 // If this argument is live outside of the entry block, insert a copy from
12001 // wherever we got it to the vreg that other BB's will reference it as.
12002 if (Res.getOpcode() == ISD::CopyFromReg) {
12003 // If we can, though, try to skip creating an unnecessary vreg.
12004 // FIXME: This isn't very clean... it would be nice to make this more
12005 // general.
12006 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12007 if (Reg.isVirtual()) {
12008 FuncInfo->ValueMap[&Arg] = Reg;
12009 continue;
12010 }
12011 }
12012 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12013 FuncInfo->InitializeRegForValue(&Arg);
12014 SDB->CopyToExportRegsIfNeeded(&Arg);
12015 }
12016 }
12017
12018 if (!Chains.empty()) {
12019 Chains.push_back(NewRoot);
12020 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12021 }
12022
12023 DAG.setRoot(NewRoot);
12024
12025 assert(i == InVals.size() && "Argument register count mismatch!");
12026
12027 // If any argument copy elisions occurred and we have debug info, update the
12028 // stale frame indices used in the dbg.declare variable info table.
12029 if (!ArgCopyElisionFrameIndexMap.empty()) {
12030 for (MachineFunction::VariableDbgInfo &VI :
12031 MF->getInStackSlotVariableDbgInfo()) {
12032 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12033 if (I != ArgCopyElisionFrameIndexMap.end())
12034 VI.updateStackSlot(I->second);
12035 }
12036 }
12037
12038 // Finally, if the target has anything special to do, allow it to do so.
12040}
12041
12042/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12043/// ensure constants are generated when needed. Remember the virtual registers
12044/// that need to be added to the Machine PHI nodes as input. We cannot just
12045/// directly add them, because expansion might result in multiple MBB's for one
12046/// BB. As such, the start of the BB might correspond to a different MBB than
12047/// the end.
12048void
12049SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12050 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12051
12052 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12053
12054 // Check PHI nodes in successors that expect a value to be available from this
12055 // block.
12056 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12057 if (!isa<PHINode>(SuccBB->begin())) continue;
12058 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12059
12060 // If this terminator has multiple identical successors (common for
12061 // switches), only handle each succ once.
12062 if (!SuccsHandled.insert(SuccMBB).second)
12063 continue;
12064
12066
12067 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12068 // nodes and Machine PHI nodes, but the incoming operands have not been
12069 // emitted yet.
12070 for (const PHINode &PN : SuccBB->phis()) {
12071 // Ignore dead phi's.
12072 if (PN.use_empty())
12073 continue;
12074
12075 // Skip empty types
12076 if (PN.getType()->isEmptyTy())
12077 continue;
12078
12079 Register Reg;
12080 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12081
12082 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12083 Register &RegOut = ConstantsOut[C];
12084 if (!RegOut) {
12085 RegOut = FuncInfo.CreateRegs(&PN);
12086 // We need to zero/sign extend ConstantInt phi operands to match
12087 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12088 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12089 if (auto *CI = dyn_cast<ConstantInt>(C))
12090 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12092 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12093 }
12094 Reg = RegOut;
12095 } else {
12097 FuncInfo.ValueMap.find(PHIOp);
12098 if (I != FuncInfo.ValueMap.end())
12099 Reg = I->second;
12100 else {
12101 assert(isa<AllocaInst>(PHIOp) &&
12102 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12103 "Didn't codegen value into a register!??");
12104 Reg = FuncInfo.CreateRegs(&PN);
12106 }
12107 }
12108
12109 // Remember that this register needs to added to the machine PHI node as
12110 // the input for this MBB.
12111 SmallVector<EVT, 4> ValueVTs;
12112 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12113 for (EVT VT : ValueVTs) {
12114 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12115 for (unsigned i = 0; i != NumRegisters; ++i)
12116 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12117 Reg += NumRegisters;
12118 }
12119 }
12120 }
12121
12122 ConstantsOut.clear();
12123}
12124
12125MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12127 if (++I == FuncInfo.MF->end())
12128 return nullptr;
12129 return &*I;
12130}
12131
12132/// During lowering new call nodes can be created (such as memset, etc.).
12133/// Those will become new roots of the current DAG, but complications arise
12134/// when they are tail calls. In such cases, the call lowering will update
12135/// the root, but the builder still needs to know that a tail call has been
12136/// lowered in order to avoid generating an additional return.
12137void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12138 // If the node is null, we do have a tail call.
12139 if (MaybeTC.getNode() != nullptr)
12140 DAG.setRoot(MaybeTC);
12141 else
12142 HasTailCall = true;
12143}
12144
12145void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12146 MachineBasicBlock *SwitchMBB,
12147 MachineBasicBlock *DefaultMBB) {
12148 MachineFunction *CurMF = FuncInfo.MF;
12149 MachineBasicBlock *NextMBB = nullptr;
12151 if (++BBI != FuncInfo.MF->end())
12152 NextMBB = &*BBI;
12153
12154 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12155
12156 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12157
12158 if (Size == 2 && W.MBB == SwitchMBB) {
12159 // If any two of the cases has the same destination, and if one value
12160 // is the same as the other, but has one bit unset that the other has set,
12161 // use bit manipulation to do two compares at once. For example:
12162 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12163 // TODO: This could be extended to merge any 2 cases in switches with 3
12164 // cases.
12165 // TODO: Handle cases where W.CaseBB != SwitchBB.
12166 CaseCluster &Small = *W.FirstCluster;
12167 CaseCluster &Big = *W.LastCluster;
12168
12169 if (Small.Low == Small.High && Big.Low == Big.High &&
12170 Small.MBB == Big.MBB) {
12171 const APInt &SmallValue = Small.Low->getValue();
12172 const APInt &BigValue = Big.Low->getValue();
12173
12174 // Check that there is only one bit different.
12175 APInt CommonBit = BigValue ^ SmallValue;
12176 if (CommonBit.isPowerOf2()) {
12177 SDValue CondLHS = getValue(Cond);
12178 EVT VT = CondLHS.getValueType();
12179 SDLoc DL = getCurSDLoc();
12180
12181 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12182 DAG.getConstant(CommonBit, DL, VT));
12183 SDValue Cond = DAG.getSetCC(
12184 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12185 ISD::SETEQ);
12186
12187 // Update successor info.
12188 // Both Small and Big will jump to Small.BB, so we sum up the
12189 // probabilities.
12190 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12191 if (BPI)
12192 addSuccessorWithProb(
12193 SwitchMBB, DefaultMBB,
12194 // The default destination is the first successor in IR.
12195 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12196 else
12197 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12198
12199 // Insert the true branch.
12200 SDValue BrCond =
12201 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12202 DAG.getBasicBlock(Small.MBB));
12203 // Insert the false branch.
12204 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12205 DAG.getBasicBlock(DefaultMBB));
12206
12207 DAG.setRoot(BrCond);
12208 return;
12209 }
12210 }
12211 }
12212
12213 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12214 // Here, we order cases by probability so the most likely case will be
12215 // checked first. However, two clusters can have the same probability in
12216 // which case their relative ordering is non-deterministic. So we use Low
12217 // as a tie-breaker as clusters are guaranteed to never overlap.
12218 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12219 [](const CaseCluster &a, const CaseCluster &b) {
12220 return a.Prob != b.Prob ?
12221 a.Prob > b.Prob :
12222 a.Low->getValue().slt(b.Low->getValue());
12223 });
12224
12225 // Rearrange the case blocks so that the last one falls through if possible
12226 // without changing the order of probabilities.
12227 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12228 --I;
12229 if (I->Prob > W.LastCluster->Prob)
12230 break;
12231 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12232 std::swap(*I, *W.LastCluster);
12233 break;
12234 }
12235 }
12236 }
12237
12238 // Compute total probability.
12239 BranchProbability DefaultProb = W.DefaultProb;
12240 BranchProbability UnhandledProbs = DefaultProb;
12241 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12242 UnhandledProbs += I->Prob;
12243
12244 MachineBasicBlock *CurMBB = W.MBB;
12245 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12246 bool FallthroughUnreachable = false;
12247 MachineBasicBlock *Fallthrough;
12248 if (I == W.LastCluster) {
12249 // For the last cluster, fall through to the default destination.
12250 Fallthrough = DefaultMBB;
12251 FallthroughUnreachable = isa<UnreachableInst>(
12252 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12253 } else {
12254 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12255 CurMF->insert(BBI, Fallthrough);
12256 // Put Cond in a virtual register to make it available from the new blocks.
12258 }
12259 UnhandledProbs -= I->Prob;
12260
12261 switch (I->Kind) {
12262 case CC_JumpTable: {
12263 // FIXME: Optimize away range check based on pivot comparisons.
12264 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12265 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12266
12267 // The jump block hasn't been inserted yet; insert it here.
12268 MachineBasicBlock *JumpMBB = JT->MBB;
12269 CurMF->insert(BBI, JumpMBB);
12270
12271 auto JumpProb = I->Prob;
12272 auto FallthroughProb = UnhandledProbs;
12273
12274 // If the default statement is a target of the jump table, we evenly
12275 // distribute the default probability to successors of CurMBB. Also
12276 // update the probability on the edge from JumpMBB to Fallthrough.
12277 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12278 SE = JumpMBB->succ_end();
12279 SI != SE; ++SI) {
12280 if (*SI == DefaultMBB) {
12281 JumpProb += DefaultProb / 2;
12282 FallthroughProb -= DefaultProb / 2;
12283 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12284 JumpMBB->normalizeSuccProbs();
12285 break;
12286 }
12287 }
12288
12289 // If the default clause is unreachable, propagate that knowledge into
12290 // JTH->FallthroughUnreachable which will use it to suppress the range
12291 // check.
12292 //
12293 // However, don't do this if we're doing branch target enforcement,
12294 // because a table branch _without_ a range check can be a tempting JOP
12295 // gadget - out-of-bounds inputs that are impossible in correct
12296 // execution become possible again if an attacker can influence the
12297 // control flow. So if an attacker doesn't already have a BTI bypass
12298 // available, we don't want them to be able to get one out of this
12299 // table branch.
12300 if (FallthroughUnreachable) {
12301 Function &CurFunc = CurMF->getFunction();
12302 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12303 JTH->FallthroughUnreachable = true;
12304 }
12305
12306 if (!JTH->FallthroughUnreachable)
12307 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12308 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12309 CurMBB->normalizeSuccProbs();
12310
12311 // The jump table header will be inserted in our current block, do the
12312 // range check, and fall through to our fallthrough block.
12313 JTH->HeaderBB = CurMBB;
12314 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12315
12316 // If we're in the right place, emit the jump table header right now.
12317 if (CurMBB == SwitchMBB) {
12318 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12319 JTH->Emitted = true;
12320 }
12321 break;
12322 }
12323 case CC_BitTests: {
12324 // FIXME: Optimize away range check based on pivot comparisons.
12325 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12326
12327 // The bit test blocks haven't been inserted yet; insert them here.
12328 for (BitTestCase &BTC : BTB->Cases)
12329 CurMF->insert(BBI, BTC.ThisBB);
12330
12331 // Fill in fields of the BitTestBlock.
12332 BTB->Parent = CurMBB;
12333 BTB->Default = Fallthrough;
12334
12335 BTB->DefaultProb = UnhandledProbs;
12336 // If the cases in bit test don't form a contiguous range, we evenly
12337 // distribute the probability on the edge to Fallthrough to two
12338 // successors of CurMBB.
12339 if (!BTB->ContiguousRange) {
12340 BTB->Prob += DefaultProb / 2;
12341 BTB->DefaultProb -= DefaultProb / 2;
12342 }
12343
12344 if (FallthroughUnreachable)
12345 BTB->FallthroughUnreachable = true;
12346
12347 // If we're in the right place, emit the bit test header right now.
12348 if (CurMBB == SwitchMBB) {
12349 visitBitTestHeader(*BTB, SwitchMBB);
12350 BTB->Emitted = true;
12351 }
12352 break;
12353 }
12354 case CC_Range: {
12355 const Value *RHS, *LHS, *MHS;
12356 ISD::CondCode CC;
12357 if (I->Low == I->High) {
12358 // Check Cond == I->Low.
12359 CC = ISD::SETEQ;
12360 LHS = Cond;
12361 RHS=I->Low;
12362 MHS = nullptr;
12363 } else {
12364 // Check I->Low <= Cond <= I->High.
12365 CC = ISD::SETLE;
12366 LHS = I->Low;
12367 MHS = Cond;
12368 RHS = I->High;
12369 }
12370
12371 // If Fallthrough is unreachable, fold away the comparison.
12372 if (FallthroughUnreachable)
12373 CC = ISD::SETTRUE;
12374
12375 // The false probability is the sum of all unhandled cases.
12376 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12377 getCurSDLoc(), I->Prob, UnhandledProbs);
12378
12379 if (CurMBB == SwitchMBB)
12380 visitSwitchCase(CB, SwitchMBB);
12381 else
12382 SL->SwitchCases.push_back(CB);
12383
12384 break;
12385 }
12386 }
12387 CurMBB = Fallthrough;
12388 }
12389}
12390
12391void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12392 const SwitchWorkListItem &W,
12393 Value *Cond,
12394 MachineBasicBlock *SwitchMBB) {
12395 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12396 "Clusters not sorted?");
12397 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12398
12399 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12400 SL->computeSplitWorkItemInfo(W);
12401
12402 // Use the first element on the right as pivot since we will make less-than
12403 // comparisons against it.
12404 CaseClusterIt PivotCluster = FirstRight;
12405 assert(PivotCluster > W.FirstCluster);
12406 assert(PivotCluster <= W.LastCluster);
12407
12408 CaseClusterIt FirstLeft = W.FirstCluster;
12409 CaseClusterIt LastRight = W.LastCluster;
12410
12411 const ConstantInt *Pivot = PivotCluster->Low;
12412
12413 // New blocks will be inserted immediately after the current one.
12415 ++BBI;
12416
12417 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12418 // we can branch to its destination directly if it's squeezed exactly in
12419 // between the known lower bound and Pivot - 1.
12420 MachineBasicBlock *LeftMBB;
12421 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12422 FirstLeft->Low == W.GE &&
12423 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12424 LeftMBB = FirstLeft->MBB;
12425 } else {
12426 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12427 FuncInfo.MF->insert(BBI, LeftMBB);
12428 WorkList.push_back(
12429 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12430 // Put Cond in a virtual register to make it available from the new blocks.
12432 }
12433
12434 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12435 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12436 // directly if RHS.High equals the current upper bound.
12437 MachineBasicBlock *RightMBB;
12438 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12439 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12440 RightMBB = FirstRight->MBB;
12441 } else {
12442 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12443 FuncInfo.MF->insert(BBI, RightMBB);
12444 WorkList.push_back(
12445 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12446 // Put Cond in a virtual register to make it available from the new blocks.
12448 }
12449
12450 // Create the CaseBlock record that will be used to lower the branch.
12451 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12452 getCurSDLoc(), LeftProb, RightProb);
12453
12454 if (W.MBB == SwitchMBB)
12455 visitSwitchCase(CB, SwitchMBB);
12456 else
12457 SL->SwitchCases.push_back(CB);
12458}
12459
12460// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12461// from the swith statement.
12463 BranchProbability PeeledCaseProb) {
12464 if (PeeledCaseProb == BranchProbability::getOne())
12466 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12467
12468 uint32_t Numerator = CaseProb.getNumerator();
12469 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12470 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12471}
12472
12473// Try to peel the top probability case if it exceeds the threshold.
12474// Return current MachineBasicBlock for the switch statement if the peeling
12475// does not occur.
12476// If the peeling is performed, return the newly created MachineBasicBlock
12477// for the peeled switch statement. Also update Clusters to remove the peeled
12478// case. PeeledCaseProb is the BranchProbability for the peeled case.
12479MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12480 const SwitchInst &SI, CaseClusterVector &Clusters,
12481 BranchProbability &PeeledCaseProb) {
12482 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12483 // Don't perform if there is only one cluster or optimizing for size.
12484 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12485 TM.getOptLevel() == CodeGenOptLevel::None ||
12486 SwitchMBB->getParent()->getFunction().hasMinSize())
12487 return SwitchMBB;
12488
12489 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12490 unsigned PeeledCaseIndex = 0;
12491 bool SwitchPeeled = false;
12492 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12493 CaseCluster &CC = Clusters[Index];
12494 if (CC.Prob < TopCaseProb)
12495 continue;
12496 TopCaseProb = CC.Prob;
12497 PeeledCaseIndex = Index;
12498 SwitchPeeled = true;
12499 }
12500 if (!SwitchPeeled)
12501 return SwitchMBB;
12502
12503 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12504 << TopCaseProb << "\n");
12505
12506 // Record the MBB for the peeled switch statement.
12507 MachineFunction::iterator BBI(SwitchMBB);
12508 ++BBI;
12509 MachineBasicBlock *PeeledSwitchMBB =
12510 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12511 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12512
12513 ExportFromCurrentBlock(SI.getCondition());
12514 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12515 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12516 nullptr, nullptr, TopCaseProb.getCompl()};
12517 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12518
12519 Clusters.erase(PeeledCaseIt);
12520 for (CaseCluster &CC : Clusters) {
12521 LLVM_DEBUG(
12522 dbgs() << "Scale the probablity for one cluster, before scaling: "
12523 << CC.Prob << "\n");
12524 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12525 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12526 }
12527 PeeledCaseProb = TopCaseProb;
12528 return PeeledSwitchMBB;
12529}
12530
12531void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12532 // Extract cases from the switch.
12533 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12534 CaseClusterVector Clusters;
12535 Clusters.reserve(SI.getNumCases());
12536 for (auto I : SI.cases()) {
12537 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12538 const ConstantInt *CaseVal = I.getCaseValue();
12539 BranchProbability Prob =
12540 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12541 : BranchProbability(1, SI.getNumCases() + 1);
12542 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12543 }
12544
12545 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12546
12547 // Cluster adjacent cases with the same destination. We do this at all
12548 // optimization levels because it's cheap to do and will make codegen faster
12549 // if there are many clusters.
12550 sortAndRangeify(Clusters);
12551
12552 // The branch probablity of the peeled case.
12553 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12554 MachineBasicBlock *PeeledSwitchMBB =
12555 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12556
12557 // If there is only the default destination, jump there directly.
12558 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12559 if (Clusters.empty()) {
12560 assert(PeeledSwitchMBB == SwitchMBB);
12561 SwitchMBB->addSuccessor(DefaultMBB);
12562 if (DefaultMBB != NextBlock(SwitchMBB)) {
12563 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12564 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12565 }
12566 return;
12567 }
12568
12569 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12570 DAG.getBFI());
12571 SL->findBitTestClusters(Clusters, &SI);
12572
12573 LLVM_DEBUG({
12574 dbgs() << "Case clusters: ";
12575 for (const CaseCluster &C : Clusters) {
12576 if (C.Kind == CC_JumpTable)
12577 dbgs() << "JT:";
12578 if (C.Kind == CC_BitTests)
12579 dbgs() << "BT:";
12580
12581 C.Low->getValue().print(dbgs(), true);
12582 if (C.Low != C.High) {
12583 dbgs() << '-';
12584 C.High->getValue().print(dbgs(), true);
12585 }
12586 dbgs() << ' ';
12587 }
12588 dbgs() << '\n';
12589 });
12590
12591 assert(!Clusters.empty());
12592 SwitchWorkList WorkList;
12593 CaseClusterIt First = Clusters.begin();
12594 CaseClusterIt Last = Clusters.end() - 1;
12595 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12596 // Scale the branchprobability for DefaultMBB if the peel occurs and
12597 // DefaultMBB is not replaced.
12598 if (PeeledCaseProb != BranchProbability::getZero() &&
12599 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12600 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12601 WorkList.push_back(
12602 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12603
12604 while (!WorkList.empty()) {
12605 SwitchWorkListItem W = WorkList.pop_back_val();
12606 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12607
12608 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12609 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12610 // For optimized builds, lower large range as a balanced binary tree.
12611 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12612 continue;
12613 }
12614
12615 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12616 }
12617}
12618
12619void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12620 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12621 auto DL = getCurSDLoc();
12622 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12623 setValue(&I, DAG.getStepVector(DL, ResultVT));
12624}
12625
12626void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12627 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12628 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12629
12630 SDLoc DL = getCurSDLoc();
12631 SDValue V = getValue(I.getOperand(0));
12632 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12633
12634 if (VT.isScalableVector()) {
12635 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12636 return;
12637 }
12638
12639 // Use VECTOR_SHUFFLE for the fixed-length vector
12640 // to maintain existing behavior.
12641 SmallVector<int, 8> Mask;
12642 unsigned NumElts = VT.getVectorMinNumElements();
12643 for (unsigned i = 0; i != NumElts; ++i)
12644 Mask.push_back(NumElts - 1 - i);
12645
12646 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12647}
12648
12649void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12650 unsigned Factor) {
12651 auto DL = getCurSDLoc();
12652 SDValue InVec = getValue(I.getOperand(0));
12653
12654 SmallVector<EVT, 4> ValueVTs;
12655 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12656 ValueVTs);
12657
12658 EVT OutVT = ValueVTs[0];
12659 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12660
12661 SmallVector<SDValue, 4> SubVecs(Factor);
12662 for (unsigned i = 0; i != Factor; ++i) {
12663 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12664 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12665 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12666 }
12667
12668 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12669 // from existing legalisation and combines.
12670 if (OutVT.isFixedLengthVector() && Factor == 2) {
12671 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12672 createStrideMask(0, 2, OutNumElts));
12673 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12674 createStrideMask(1, 2, OutNumElts));
12675 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12676 setValue(&I, Res);
12677 return;
12678 }
12679
12680 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12681 DAG.getVTList(ValueVTs), SubVecs);
12682 setValue(&I, Res);
12683}
12684
12685void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12686 unsigned Factor) {
12687 auto DL = getCurSDLoc();
12688 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12689 EVT InVT = getValue(I.getOperand(0)).getValueType();
12690 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12691
12692 SmallVector<SDValue, 8> InVecs(Factor);
12693 for (unsigned i = 0; i < Factor; ++i) {
12694 InVecs[i] = getValue(I.getOperand(i));
12695 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12696 "Expected VTs to be the same");
12697 }
12698
12699 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12700 // from existing legalisation and combines.
12701 if (OutVT.isFixedLengthVector() && Factor == 2) {
12702 unsigned NumElts = InVT.getVectorMinNumElements();
12703 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12704 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12705 createInterleaveMask(NumElts, 2)));
12706 return;
12707 }
12708
12709 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12710 SDValue Res =
12711 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12712
12714 for (unsigned i = 0; i < Factor; ++i)
12715 Results[i] = Res.getValue(i);
12716
12717 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12718 setValue(&I, Res);
12719}
12720
12721void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12722 SmallVector<EVT, 4> ValueVTs;
12723 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12724 ValueVTs);
12725 unsigned NumValues = ValueVTs.size();
12726 if (NumValues == 0) return;
12727
12728 SmallVector<SDValue, 4> Values(NumValues);
12729 SDValue Op = getValue(I.getOperand(0));
12730
12731 for (unsigned i = 0; i != NumValues; ++i)
12732 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12733 SDValue(Op.getNode(), Op.getResNo() + i));
12734
12736 DAG.getVTList(ValueVTs), Values));
12737}
12738
12739void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12740 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12741 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12742
12743 SDLoc DL = getCurSDLoc();
12744 SDValue V1 = getValue(I.getOperand(0));
12745 SDValue V2 = getValue(I.getOperand(1));
12746 int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue();
12747
12748 // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
12749 if (VT.isScalableVector()) {
12750 setValue(
12751 &I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2,
12752 DAG.getSignedConstant(
12753 Imm, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
12754 return;
12755 }
12756
12757 unsigned NumElts = VT.getVectorNumElements();
12758
12759 uint64_t Idx = (NumElts + Imm) % NumElts;
12760
12761 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12762 SmallVector<int, 8> Mask;
12763 for (unsigned i = 0; i < NumElts; ++i)
12764 Mask.push_back(Idx + i);
12765 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12766}
12767
12768// Consider the following MIR after SelectionDAG, which produces output in
12769// phyregs in the first case or virtregs in the second case.
12770//
12771// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12772// %5:gr32 = COPY $ebx
12773// %6:gr32 = COPY $edx
12774// %1:gr32 = COPY %6:gr32
12775// %0:gr32 = COPY %5:gr32
12776//
12777// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12778// %1:gr32 = COPY %6:gr32
12779// %0:gr32 = COPY %5:gr32
12780//
12781// Given %0, we'd like to return $ebx in the first case and %5 in the second.
12782// Given %1, we'd like to return $edx in the first case and %6 in the second.
12783//
12784// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12785// to a single virtreg (such as %0). The remaining outputs monotonically
12786// increase in virtreg number from there. If a callbr has no outputs, then it
12787// should not have a corresponding callbr landingpad; in fact, the callbr
12788// landingpad would not even be able to refer to such a callbr.
12790 MachineInstr *MI = MRI.def_begin(Reg)->getParent();
12791 // There is definitely at least one copy.
12792 assert(MI->getOpcode() == TargetOpcode::COPY &&
12793 "start of copy chain MUST be COPY");
12794 Reg = MI->getOperand(1).getReg();
12795
12796 // If the copied register in the first copy must be virtual.
12797 assert(Reg.isVirtual() && "expected COPY of virtual register");
12798 MI = MRI.def_begin(Reg)->getParent();
12799
12800 // There may be an optional second copy.
12801 if (MI->getOpcode() == TargetOpcode::COPY) {
12802 assert(Reg.isVirtual() && "expected COPY of virtual register");
12803 Reg = MI->getOperand(1).getReg();
12804 assert(Reg.isPhysical() && "expected COPY of physical register");
12805 } else {
12806 // The start of the chain must be an INLINEASM_BR.
12807 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12808 "end of copy chain MUST be INLINEASM_BR");
12809 }
12810
12811 return Reg;
12812}
12813
12814// We must do this walk rather than the simpler
12815// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
12816// otherwise we will end up with copies of virtregs only valid along direct
12817// edges.
12818void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
12819 SmallVector<EVT, 8> ResultVTs;
12820 SmallVector<SDValue, 8> ResultValues;
12821 const auto *CBR =
12822 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
12823
12824 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12825 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
12826 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
12827
12828 Register InitialDef = FuncInfo.ValueMap[CBR];
12829 SDValue Chain = DAG.getRoot();
12830
12831 // Re-parse the asm constraints string.
12832 TargetLowering::AsmOperandInfoVector TargetConstraints =
12833 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
12834 for (auto &T : TargetConstraints) {
12835 SDISelAsmOperandInfo OpInfo(T);
12836 if (OpInfo.Type != InlineAsm::isOutput)
12837 continue;
12838
12839 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
12840 // individual constraint.
12841 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
12842
12843 switch (OpInfo.ConstraintType) {
12846 // Fill in OpInfo.AssignedRegs.Regs.
12847 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
12848
12849 // getRegistersForValue may produce 1 to many registers based on whether
12850 // the OpInfo.ConstraintVT is legal on the target or not.
12851 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
12852 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
12853 if (OriginalDef.isPhysical())
12854 FuncInfo.MBB->addLiveIn(OriginalDef);
12855 // Update the assigned registers to use the original defs.
12856 Reg = OriginalDef;
12857 }
12858
12859 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
12860 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
12861 ResultValues.push_back(V);
12862 ResultVTs.push_back(OpInfo.ConstraintVT);
12863 break;
12864 }
12866 SDValue Flag;
12867 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
12868 OpInfo, DAG);
12869 ++InitialDef;
12870 ResultValues.push_back(V);
12871 ResultVTs.push_back(OpInfo.ConstraintVT);
12872 break;
12873 }
12874 default:
12875 break;
12876 }
12877 }
12879 DAG.getVTList(ResultVTs), ResultValues);
12880 setValue(&I, V);
12881}
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:334
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:440
an instruction to allocate memory on the stack
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Check if an argument has a given attribute.
Definition Function.cpp:339
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
iterator end() const
Definition ArrayRef.h:136
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
iterator begin() const
Definition ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
A cache of @llvm.assume calls within a function.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:361
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This class represents a no-op cast from one type to another.
The address of a basic block.
Definition Constants.h:899
Conditional or Unconditional Branch instruction.
Analysis providing branch probability information.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static uint32_t getDenominator()
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
BranchProbability getCompl() const
static BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
CallingConv::ID getCallingConv() const
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI bool isTailCall() const
Tests if this call site is marked as a tail call.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition InstrTypes.h: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:167
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:222
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
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
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...
virtual Function * getSSPStackGuardCheck(const Module &M) const
If the target has a standard stack protection check function that performs validation and error handl...
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
LegalizeAction getFixedPointOperationAction(unsigned Op, EVT VT, unsigned Scale) const
Some fixed point operations may be natively supported by the target but only for specific scales.
MachineMemOperand::Flags getAtomicMemOperandFlags(const Instruction &AI, const DataLayout &DL) const
virtual bool getTgtMemIntrinsic(IntrinsicInfo &, const CallInst &, MachineFunction &, unsigned) const
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
virtual bool allowsMisalignedMemoryAccesses(EVT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *=nullptr) const
Determine if the target supports unaligned memory accesses.
bool isOperationCustom(unsigned Op, EVT VT) const
Return true if the operation uses custom lowering, regardless of whether the type is legal or not.
bool hasBigEndianPartOrdering(EVT VT, const DataLayout &DL) const
When splitting a value of the specified type into parts, does the Lo or Hi part come first?
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
virtual Align getABIAlignmentForCallingConv(Type *ArgTy, const DataLayout &DL) const
Certain targets have context sensitive alignment requirements, where one type has the alignment requi...
MachineMemOperand::Flags getVPIntrinsicMemOperandFlags(const VPIntrinsic &VPIntrin) const
virtual bool shouldExpandGetActiveLaneMask(EVT VT, EVT OpVT) const
Return true if the @llvm.get.active.lane.mask intrinsic should be expanded using generic code in Sele...
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
MachineMemOperand::Flags getLoadMemOperandFlags(const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC=nullptr, const TargetLibraryInfo *LibInfo=nullptr) const
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
MVT getProgramPointerTy(const DataLayout &DL) const
Return the type for code pointers, which is determined by the program address space specified through...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
virtual bool shouldExpandVectorMatch(EVT VT, unsigned SearchSize) const
Return true if the @llvm.experimental.vector.match intrinsic should be expanded for vector type ‘VT’ ...
virtual MVT getFenceOperandTy(const DataLayout &DL) const
Return the type for operands of fence.
virtual bool shouldExpandGetVectorLength(EVT CountVT, unsigned VF, bool IsScalable) const
bool isOperationLegalOrCustom(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
virtual MVT hasFastEqualityCompare(unsigned NumBits) const
Return the preferred operand type if the target has a quick way to compare integer values of the give...
MachineMemOperand::Flags getStoreMemOperandFlags(const StoreInst &SI, const DataLayout &DL) const
virtual bool 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:701
bool use_empty() const
Definition Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h: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:346
@ 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:279
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:548
gep_type_iterator gep_type_end(const User *GEP)
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
Definition STLExtras.h: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:186
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:316
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20
int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
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)