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.
3830 hasOnlySelectUsers(cast<SelectInst>(I).getCondition())) {
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 // FIXME: this is not correct for pointers with addr width != pointer width
3975 visitPtrToInt(I);
3976}
3977
3978void SelectionDAGBuilder::visitPtrToInt(const User &I) {
3979 // What to do depends on the size of the integer and the size of the pointer.
3980 // We can either truncate, zero extend, or no-op, accordingly.
3981 SDValue N = getValue(I.getOperand(0));
3982 auto &TLI = DAG.getTargetLoweringInfo();
3983 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3984 I.getType());
3985 EVT PtrMemVT =
3986 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3987 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
3988 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
3989 setValue(&I, N);
3990}
3991
3992void SelectionDAGBuilder::visitIntToPtr(const User &I) {
3993 // What to do depends on the size of the integer and the size of the pointer.
3994 // We can either truncate, zero extend, or no-op, accordingly.
3995 SDValue N = getValue(I.getOperand(0));
3996 auto &TLI = DAG.getTargetLoweringInfo();
3997 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3998 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
3999 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4000 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4001 setValue(&I, N);
4002}
4003
4004void SelectionDAGBuilder::visitBitCast(const User &I) {
4005 SDValue N = getValue(I.getOperand(0));
4006 SDLoc dl = getCurSDLoc();
4007 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4008 I.getType());
4009
4010 // BitCast assures us that source and destination are the same size so this is
4011 // either a BITCAST or a no-op.
4012 if (DestVT != N.getValueType())
4013 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
4014 DestVT, N)); // convert types.
4015 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4016 // might fold any kind of constant expression to an integer constant and that
4017 // is not what we are looking for. Only recognize a bitcast of a genuine
4018 // constant integer as an opaque constant.
4019 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4020 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4021 /*isOpaque*/true));
4022 else
4023 setValue(&I, N); // noop cast.
4024}
4025
4026void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4027 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4028 const Value *SV = I.getOperand(0);
4029 SDValue N = getValue(SV);
4030 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4031
4032 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4033 unsigned DestAS = I.getType()->getPointerAddressSpace();
4034
4035 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4036 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4037
4038 setValue(&I, N);
4039}
4040
4041void SelectionDAGBuilder::visitInsertElement(const User &I) {
4042 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4043 SDValue InVec = getValue(I.getOperand(0));
4044 SDValue InVal = getValue(I.getOperand(1));
4045 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4046 TLI.getVectorIdxTy(DAG.getDataLayout()));
4048 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4049 InVec, InVal, InIdx));
4050}
4051
4052void SelectionDAGBuilder::visitExtractElement(const User &I) {
4053 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4054 SDValue InVec = getValue(I.getOperand(0));
4055 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4056 TLI.getVectorIdxTy(DAG.getDataLayout()));
4058 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4059 InVec, InIdx));
4060}
4061
4062void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4063 SDValue Src1 = getValue(I.getOperand(0));
4064 SDValue Src2 = getValue(I.getOperand(1));
4065 ArrayRef<int> Mask;
4066 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4067 Mask = SVI->getShuffleMask();
4068 else
4069 Mask = cast<ConstantExpr>(I).getShuffleMask();
4070 SDLoc DL = getCurSDLoc();
4071 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4072 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4073 EVT SrcVT = Src1.getValueType();
4074
4075 if (all_of(Mask, [](int Elem) { return Elem == 0; }) &&
4076 VT.isScalableVector()) {
4077 // Canonical splat form of first element of first input vector.
4078 SDValue FirstElt =
4079 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4080 DAG.getVectorIdxConstant(0, DL));
4081 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4082 return;
4083 }
4084
4085 // For now, we only handle splats for scalable vectors.
4086 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4087 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4088 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4089
4090 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4091 unsigned MaskNumElts = Mask.size();
4092
4093 if (SrcNumElts == MaskNumElts) {
4094 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4095 return;
4096 }
4097
4098 // Normalize the shuffle vector since mask and vector length don't match.
4099 if (SrcNumElts < MaskNumElts) {
4100 // Mask is longer than the source vectors. We can use concatenate vector to
4101 // make the mask and vectors lengths match.
4102
4103 if (MaskNumElts % SrcNumElts == 0) {
4104 // Mask length is a multiple of the source vector length.
4105 // Check if the shuffle is some kind of concatenation of the input
4106 // vectors.
4107 unsigned NumConcat = MaskNumElts / SrcNumElts;
4108 bool IsConcat = true;
4109 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4110 for (unsigned i = 0; i != MaskNumElts; ++i) {
4111 int Idx = Mask[i];
4112 if (Idx < 0)
4113 continue;
4114 // Ensure the indices in each SrcVT sized piece are sequential and that
4115 // the same source is used for the whole piece.
4116 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4117 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4118 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4119 IsConcat = false;
4120 break;
4121 }
4122 // Remember which source this index came from.
4123 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4124 }
4125
4126 // The shuffle is concatenating multiple vectors together. Just emit
4127 // a CONCAT_VECTORS operation.
4128 if (IsConcat) {
4129 SmallVector<SDValue, 8> ConcatOps;
4130 for (auto Src : ConcatSrcs) {
4131 if (Src < 0)
4132 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4133 else if (Src == 0)
4134 ConcatOps.push_back(Src1);
4135 else
4136 ConcatOps.push_back(Src2);
4137 }
4138 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4139 return;
4140 }
4141 }
4142
4143 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4144 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4145 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4146 PaddedMaskNumElts);
4147
4148 // Pad both vectors with undefs to make them the same length as the mask.
4149 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4150
4151 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4152 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4153 MOps1[0] = Src1;
4154 MOps2[0] = Src2;
4155
4156 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4157 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4158
4159 // Readjust mask for new input vector length.
4160 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4161 for (unsigned i = 0; i != MaskNumElts; ++i) {
4162 int Idx = Mask[i];
4163 if (Idx >= (int)SrcNumElts)
4164 Idx -= SrcNumElts - PaddedMaskNumElts;
4165 MappedOps[i] = Idx;
4166 }
4167
4168 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4169
4170 // If the concatenated vector was padded, extract a subvector with the
4171 // correct number of elements.
4172 if (MaskNumElts != PaddedMaskNumElts)
4173 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4174 DAG.getVectorIdxConstant(0, DL));
4175
4176 setValue(&I, Result);
4177 return;
4178 }
4179
4180 assert(SrcNumElts > MaskNumElts);
4181
4182 // Analyze the access pattern of the vector to see if we can extract
4183 // two subvectors and do the shuffle.
4184 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4185 bool CanExtract = true;
4186 for (int Idx : Mask) {
4187 unsigned Input = 0;
4188 if (Idx < 0)
4189 continue;
4190
4191 if (Idx >= (int)SrcNumElts) {
4192 Input = 1;
4193 Idx -= SrcNumElts;
4194 }
4195
4196 // If all the indices come from the same MaskNumElts sized portion of
4197 // the sources we can use extract. Also make sure the extract wouldn't
4198 // extract past the end of the source.
4199 int NewStartIdx = alignDown(Idx, MaskNumElts);
4200 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4201 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4202 CanExtract = false;
4203 // Make sure we always update StartIdx as we use it to track if all
4204 // elements are undef.
4205 StartIdx[Input] = NewStartIdx;
4206 }
4207
4208 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4209 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4210 return;
4211 }
4212 if (CanExtract) {
4213 // Extract appropriate subvector and generate a vector shuffle
4214 for (unsigned Input = 0; Input < 2; ++Input) {
4215 SDValue &Src = Input == 0 ? Src1 : Src2;
4216 if (StartIdx[Input] < 0)
4217 Src = DAG.getUNDEF(VT);
4218 else {
4219 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4220 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4221 }
4222 }
4223
4224 // Calculate new mask.
4225 SmallVector<int, 8> MappedOps(Mask);
4226 for (int &Idx : MappedOps) {
4227 if (Idx >= (int)SrcNumElts)
4228 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4229 else if (Idx >= 0)
4230 Idx -= StartIdx[0];
4231 }
4232
4233 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4234 return;
4235 }
4236
4237 // We can't use either concat vectors or extract subvectors so fall back to
4238 // replacing the shuffle with extract and build vector.
4239 // to insert and build vector.
4240 EVT EltVT = VT.getVectorElementType();
4242 for (int Idx : Mask) {
4243 SDValue Res;
4244
4245 if (Idx < 0) {
4246 Res = DAG.getUNDEF(EltVT);
4247 } else {
4248 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4249 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4250
4251 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4252 DAG.getVectorIdxConstant(Idx, DL));
4253 }
4254
4255 Ops.push_back(Res);
4256 }
4257
4258 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4259}
4260
4261void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4262 ArrayRef<unsigned> Indices = I.getIndices();
4263 const Value *Op0 = I.getOperand(0);
4264 const Value *Op1 = I.getOperand(1);
4265 Type *AggTy = I.getType();
4266 Type *ValTy = Op1->getType();
4267 bool IntoUndef = isa<UndefValue>(Op0);
4268 bool FromUndef = isa<UndefValue>(Op1);
4269
4270 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4271
4272 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4273 SmallVector<EVT, 4> AggValueVTs;
4274 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4275 SmallVector<EVT, 4> ValValueVTs;
4276 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4277
4278 unsigned NumAggValues = AggValueVTs.size();
4279 unsigned NumValValues = ValValueVTs.size();
4280 SmallVector<SDValue, 4> Values(NumAggValues);
4281
4282 // Ignore an insertvalue that produces an empty object
4283 if (!NumAggValues) {
4284 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4285 return;
4286 }
4287
4288 SDValue Agg = getValue(Op0);
4289 unsigned i = 0;
4290 // Copy the beginning value(s) from the original aggregate.
4291 for (; i != LinearIndex; ++i)
4292 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4293 SDValue(Agg.getNode(), Agg.getResNo() + i);
4294 // Copy values from the inserted value(s).
4295 if (NumValValues) {
4296 SDValue Val = getValue(Op1);
4297 for (; i != LinearIndex + NumValValues; ++i)
4298 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4299 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4300 }
4301 // Copy remaining value(s) from the original aggregate.
4302 for (; i != NumAggValues; ++i)
4303 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4304 SDValue(Agg.getNode(), Agg.getResNo() + i);
4305
4307 DAG.getVTList(AggValueVTs), Values));
4308}
4309
4310void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4311 ArrayRef<unsigned> Indices = I.getIndices();
4312 const Value *Op0 = I.getOperand(0);
4313 Type *AggTy = Op0->getType();
4314 Type *ValTy = I.getType();
4315 bool OutOfUndef = isa<UndefValue>(Op0);
4316
4317 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4318
4319 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4320 SmallVector<EVT, 4> ValValueVTs;
4321 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4322
4323 unsigned NumValValues = ValValueVTs.size();
4324
4325 // Ignore a extractvalue that produces an empty object
4326 if (!NumValValues) {
4327 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4328 return;
4329 }
4330
4331 SmallVector<SDValue, 4> Values(NumValValues);
4332
4333 SDValue Agg = getValue(Op0);
4334 // Copy out the selected value(s).
4335 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4336 Values[i - LinearIndex] =
4337 OutOfUndef ?
4338 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4339 SDValue(Agg.getNode(), Agg.getResNo() + i);
4340
4342 DAG.getVTList(ValValueVTs), Values));
4343}
4344
4345void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4346 Value *Op0 = I.getOperand(0);
4347 // Note that the pointer operand may be a vector of pointers. Take the scalar
4348 // element which holds a pointer.
4349 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4350 SDValue N = getValue(Op0);
4351 SDLoc dl = getCurSDLoc();
4352 auto &TLI = DAG.getTargetLoweringInfo();
4353 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4354
4355 // For a vector GEP, keep the prefix scalar as long as possible, then
4356 // convert any scalars encountered after the first vector operand to vectors.
4357 bool IsVectorGEP = I.getType()->isVectorTy();
4358 ElementCount VectorElementCount =
4359 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4361
4363 GTI != E; ++GTI) {
4364 const Value *Idx = GTI.getOperand();
4365 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4366 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4367 if (Field) {
4368 // N = N + Offset
4369 uint64_t Offset =
4370 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4371
4372 // In an inbounds GEP with an offset that is nonnegative even when
4373 // interpreted as signed, assume there is no unsigned overflow.
4374 SDNodeFlags Flags;
4375 if (NW.hasNoUnsignedWrap() ||
4376 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4378
4379 N = DAG.getMemBasePlusOffset(
4380 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4381 }
4382 } else {
4383 // IdxSize is the width of the arithmetic according to IR semantics.
4384 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4385 // (and fix up the result later).
4386 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4387 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4388 TypeSize ElementSize =
4389 GTI.getSequentialElementStride(DAG.getDataLayout());
4390 // We intentionally mask away the high bits here; ElementSize may not
4391 // fit in IdxTy.
4392 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4393 /*isSigned=*/false, /*implicitTrunc=*/true);
4394 bool ElementScalable = ElementSize.isScalable();
4395
4396 // If this is a scalar constant or a splat vector of constants,
4397 // handle it quickly.
4398 const auto *C = dyn_cast<Constant>(Idx);
4399 if (C && isa<VectorType>(C->getType()))
4400 C = C->getSplatValue();
4401
4402 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4403 if (CI && CI->isZero())
4404 continue;
4405 if (CI && !ElementScalable) {
4406 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4407 LLVMContext &Context = *DAG.getContext();
4408 SDValue OffsVal;
4409 if (N.getValueType().isVector())
4410 OffsVal = DAG.getConstant(
4411 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4412 else
4413 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4414
4415 // In an inbounds GEP with an offset that is nonnegative even when
4416 // interpreted as signed, assume there is no unsigned overflow.
4417 SDNodeFlags Flags;
4418 if (NW.hasNoUnsignedWrap() ||
4419 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4420 Flags.setNoUnsignedWrap(true);
4421
4422 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4423
4424 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4425 continue;
4426 }
4427
4428 // N = N + Idx * ElementMul;
4429 SDValue IdxN = getValue(Idx);
4430
4431 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4432 if (N.getValueType().isVector()) {
4433 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4434 VectorElementCount);
4435 IdxN = DAG.getSplat(VT, dl, IdxN);
4436 } else {
4437 EVT VT =
4438 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4439 N = DAG.getSplat(VT, dl, N);
4440 }
4441 }
4442
4443 // If the index is smaller or larger than intptr_t, truncate or extend
4444 // it.
4445 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4446
4447 SDNodeFlags ScaleFlags;
4448 // The multiplication of an index by the type size does not wrap the
4449 // pointer index type in a signed sense (mul nsw).
4451
4452 // The multiplication of an index by the type size does not wrap the
4453 // pointer index type in an unsigned sense (mul nuw).
4454 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4455
4456 if (ElementScalable) {
4457 EVT VScaleTy = N.getValueType().getScalarType();
4458 SDValue VScale = DAG.getNode(
4459 ISD::VSCALE, dl, VScaleTy,
4460 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4461 if (N.getValueType().isVector())
4462 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4463 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4464 ScaleFlags);
4465 } else {
4466 // If this is a multiply by a power of two, turn it into a shl
4467 // immediately. This is a very common case.
4468 if (ElementMul != 1) {
4469 if (ElementMul.isPowerOf2()) {
4470 unsigned Amt = ElementMul.logBase2();
4471 IdxN = DAG.getNode(
4472 ISD::SHL, dl, N.getValueType(), IdxN,
4473 DAG.getShiftAmountConstant(Amt, N.getValueType(), dl),
4474 ScaleFlags);
4475 } else {
4476 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4477 IdxN.getValueType());
4478 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4479 ScaleFlags);
4480 }
4481 }
4482 }
4483
4484 // The successive addition of the current address, truncated to the
4485 // pointer index type and interpreted as an unsigned number, and each
4486 // offset, also interpreted as an unsigned number, does not wrap the
4487 // pointer index type (add nuw).
4488 SDNodeFlags AddFlags;
4489 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4490
4491 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4492 }
4493 }
4494
4495 if (IsVectorGEP && !N.getValueType().isVector()) {
4496 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4497 N = DAG.getSplat(VT, dl, N);
4498 }
4499
4500 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4501 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4502 if (IsVectorGEP) {
4503 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4504 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4505 }
4506
4507 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4508 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4509
4510 setValue(&I, N);
4511}
4512
4513void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4514 // If this is a fixed sized alloca in the entry block of the function,
4515 // allocate it statically on the stack.
4516 if (FuncInfo.StaticAllocaMap.count(&I))
4517 return; // getValue will auto-populate this.
4518
4519 SDLoc dl = getCurSDLoc();
4520 Type *Ty = I.getAllocatedType();
4521 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4522 auto &DL = DAG.getDataLayout();
4523 TypeSize TySize = DL.getTypeAllocSize(Ty);
4524 MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign());
4525
4526 SDValue AllocSize = getValue(I.getArraySize());
4527
4528 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4529 if (AllocSize.getValueType() != IntPtr)
4530 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4531
4532 if (TySize.isScalable())
4533 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4534 DAG.getVScale(dl, IntPtr,
4535 APInt(IntPtr.getScalarSizeInBits(),
4536 TySize.getKnownMinValue())));
4537 else {
4538 SDValue TySizeValue =
4539 DAG.getConstant(TySize.getFixedValue(), dl, MVT::getIntegerVT(64));
4540 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4541 DAG.getZExtOrTrunc(TySizeValue, dl, IntPtr));
4542 }
4543
4544 // Handle alignment. If the requested alignment is less than or equal to
4545 // the stack alignment, ignore it. If the size is greater than or equal to
4546 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4547 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4548 if (*Alignment <= StackAlign)
4549 Alignment = std::nullopt;
4550
4551 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4552 // Round the size of the allocation up to the stack alignment size
4553 // by add SA-1 to the size. This doesn't overflow because we're computing
4554 // an address inside an alloca.
4555 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4556 DAG.getConstant(StackAlignMask, dl, IntPtr),
4558
4559 // Mask out the low bits for alignment purposes.
4560 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4561 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4562
4563 SDValue Ops[] = {
4564 getRoot(), AllocSize,
4565 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4566 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4567 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4568 setValue(&I, DSA);
4569 DAG.setRoot(DSA.getValue(1));
4570
4571 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4572}
4573
4574static const MDNode *getRangeMetadata(const Instruction &I) {
4575 return I.getMetadata(LLVMContext::MD_range);
4576}
4577
4578static std::optional<ConstantRange> getRange(const Instruction &I) {
4579 if (const auto *CB = dyn_cast<CallBase>(&I))
4580 if (std::optional<ConstantRange> CR = CB->getRange())
4581 return CR;
4582 if (const MDNode *Range = getRangeMetadata(I))
4584 return std::nullopt;
4585}
4586
4587void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4588 if (I.isAtomic())
4589 return visitAtomicLoad(I);
4590
4591 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4592 const Value *SV = I.getOperand(0);
4593 if (TLI.supportSwiftError()) {
4594 // Swifterror values can come from either a function parameter with
4595 // swifterror attribute or an alloca with swifterror attribute.
4596 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4597 if (Arg->hasSwiftErrorAttr())
4598 return visitLoadFromSwiftError(I);
4599 }
4600
4601 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4602 if (Alloca->isSwiftError())
4603 return visitLoadFromSwiftError(I);
4604 }
4605 }
4606
4607 SDValue Ptr = getValue(SV);
4608
4609 Type *Ty = I.getType();
4610 SmallVector<EVT, 4> ValueVTs, MemVTs;
4612 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4613 unsigned NumValues = ValueVTs.size();
4614 if (NumValues == 0)
4615 return;
4616
4617 Align Alignment = I.getAlign();
4618 AAMDNodes AAInfo = I.getAAMetadata();
4619 const MDNode *Ranges = getRangeMetadata(I);
4620 bool isVolatile = I.isVolatile();
4621 MachineMemOperand::Flags MMOFlags =
4622 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4623
4624 SDValue Root;
4625 bool ConstantMemory = false;
4626 if (isVolatile)
4627 // Serialize volatile loads with other side effects.
4628 Root = getRoot();
4629 else if (NumValues > MaxParallelChains)
4630 Root = getMemoryRoot();
4631 else if (BatchAA &&
4632 BatchAA->pointsToConstantMemory(MemoryLocation(
4633 SV,
4634 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4635 AAInfo))) {
4636 // Do not serialize (non-volatile) loads of constant memory with anything.
4637 Root = DAG.getEntryNode();
4638 ConstantMemory = true;
4640 } else {
4641 // Do not serialize non-volatile loads against each other.
4642 Root = DAG.getRoot();
4643 }
4644
4645 SDLoc dl = getCurSDLoc();
4646
4647 if (isVolatile)
4648 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4649
4650 SmallVector<SDValue, 4> Values(NumValues);
4651 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4652
4653 unsigned ChainI = 0;
4654 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4655 // Serializing loads here may result in excessive register pressure, and
4656 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4657 // could recover a bit by hoisting nodes upward in the chain by recognizing
4658 // they are side-effect free or do not alias. The optimizer should really
4659 // avoid this case by converting large object/array copies to llvm.memcpy
4660 // (MaxParallelChains should always remain as failsafe).
4661 if (ChainI == MaxParallelChains) {
4662 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4663 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4664 ArrayRef(Chains.data(), ChainI));
4665 Root = Chain;
4666 ChainI = 0;
4667 }
4668
4669 // TODO: MachinePointerInfo only supports a fixed length offset.
4670 MachinePointerInfo PtrInfo =
4671 !Offsets[i].isScalable() || Offsets[i].isZero()
4672 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4673 : MachinePointerInfo();
4674
4675 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4676 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4677 MMOFlags, AAInfo, Ranges);
4678 Chains[ChainI] = L.getValue(1);
4679
4680 if (MemVTs[i] != ValueVTs[i])
4681 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4682
4683 Values[i] = L;
4684 }
4685
4686 if (!ConstantMemory) {
4687 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4688 ArrayRef(Chains.data(), ChainI));
4689 if (isVolatile)
4690 DAG.setRoot(Chain);
4691 else
4692 PendingLoads.push_back(Chain);
4693 }
4694
4695 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4696 DAG.getVTList(ValueVTs), Values));
4697}
4698
4699void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4700 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4701 "call visitStoreToSwiftError when backend supports swifterror");
4702
4703 SmallVector<EVT, 4> ValueVTs;
4704 SmallVector<uint64_t, 4> Offsets;
4705 const Value *SrcV = I.getOperand(0);
4706 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4707 SrcV->getType(), ValueVTs, &Offsets, 0);
4708 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4709 "expect a single EVT for swifterror");
4710
4711 SDValue Src = getValue(SrcV);
4712 // Create a virtual register, then update the virtual register.
4713 Register VReg =
4714 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4715 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4716 // Chain can be getRoot or getControlRoot.
4717 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4718 SDValue(Src.getNode(), Src.getResNo()));
4719 DAG.setRoot(CopyNode);
4720}
4721
4722void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4723 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4724 "call visitLoadFromSwiftError when backend supports swifterror");
4725
4726 assert(!I.isVolatile() &&
4727 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4728 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4729 "Support volatile, non temporal, invariant for load_from_swift_error");
4730
4731 const Value *SV = I.getOperand(0);
4732 Type *Ty = I.getType();
4733 assert(
4734 (!BatchAA ||
4735 !BatchAA->pointsToConstantMemory(MemoryLocation(
4736 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4737 I.getAAMetadata()))) &&
4738 "load_from_swift_error should not be constant memory");
4739
4740 SmallVector<EVT, 4> ValueVTs;
4741 SmallVector<uint64_t, 4> Offsets;
4742 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4743 ValueVTs, &Offsets, 0);
4744 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4745 "expect a single EVT for swifterror");
4746
4747 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4748 SDValue L = DAG.getCopyFromReg(
4749 getRoot(), getCurSDLoc(),
4750 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4751
4752 setValue(&I, L);
4753}
4754
4755void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4756 if (I.isAtomic())
4757 return visitAtomicStore(I);
4758
4759 const Value *SrcV = I.getOperand(0);
4760 const Value *PtrV = I.getOperand(1);
4761
4762 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4763 if (TLI.supportSwiftError()) {
4764 // Swifterror values can come from either a function parameter with
4765 // swifterror attribute or an alloca with swifterror attribute.
4766 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4767 if (Arg->hasSwiftErrorAttr())
4768 return visitStoreToSwiftError(I);
4769 }
4770
4771 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4772 if (Alloca->isSwiftError())
4773 return visitStoreToSwiftError(I);
4774 }
4775 }
4776
4777 SmallVector<EVT, 4> ValueVTs, MemVTs;
4779 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4780 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4781 unsigned NumValues = ValueVTs.size();
4782 if (NumValues == 0)
4783 return;
4784
4785 // Get the lowered operands. Note that we do this after
4786 // checking if NumResults is zero, because with zero results
4787 // the operands won't have values in the map.
4788 SDValue Src = getValue(SrcV);
4789 SDValue Ptr = getValue(PtrV);
4790
4791 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4792 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4793 SDLoc dl = getCurSDLoc();
4794 Align Alignment = I.getAlign();
4795 AAMDNodes AAInfo = I.getAAMetadata();
4796
4797 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4798
4799 unsigned ChainI = 0;
4800 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4801 // See visitLoad comments.
4802 if (ChainI == MaxParallelChains) {
4803 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4804 ArrayRef(Chains.data(), ChainI));
4805 Root = Chain;
4806 ChainI = 0;
4807 }
4808
4809 // TODO: MachinePointerInfo only supports a fixed length offset.
4810 MachinePointerInfo PtrInfo =
4811 !Offsets[i].isScalable() || Offsets[i].isZero()
4812 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4813 : MachinePointerInfo();
4814
4815 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4816 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4817 if (MemVTs[i] != ValueVTs[i])
4818 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4819 SDValue St =
4820 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4821 Chains[ChainI] = St;
4822 }
4823
4824 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4825 ArrayRef(Chains.data(), ChainI));
4826 setValue(&I, StoreNode);
4827 DAG.setRoot(StoreNode);
4828}
4829
4830void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4831 bool IsCompressing) {
4832 SDLoc sdl = getCurSDLoc();
4833
4834 auto getMaskedStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4835 Align &Alignment) {
4836 // llvm.masked.store.*(Src0, Ptr, alignment, Mask)
4837 Src0 = I.getArgOperand(0);
4838 Ptr = I.getArgOperand(1);
4839 Alignment = cast<ConstantInt>(I.getArgOperand(2))->getAlignValue();
4840 Mask = I.getArgOperand(3);
4841 };
4842 auto getCompressingStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4843 Align &Alignment) {
4844 // llvm.masked.compressstore.*(Src0, Ptr, Mask)
4845 Src0 = I.getArgOperand(0);
4846 Ptr = I.getArgOperand(1);
4847 Mask = I.getArgOperand(2);
4848 Alignment = I.getParamAlign(1).valueOrOne();
4849 };
4850
4851 Value *PtrOperand, *MaskOperand, *Src0Operand;
4852 Align Alignment;
4853 if (IsCompressing)
4854 getCompressingStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4855 else
4856 getMaskedStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4857
4858 SDValue Ptr = getValue(PtrOperand);
4859 SDValue Src0 = getValue(Src0Operand);
4860 SDValue Mask = getValue(MaskOperand);
4861 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4862
4863 EVT VT = Src0.getValueType();
4864
4865 auto MMOFlags = MachineMemOperand::MOStore;
4866 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4868
4869 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4870 MachinePointerInfo(PtrOperand), MMOFlags,
4871 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4872
4873 const auto &TLI = DAG.getTargetLoweringInfo();
4874 const auto &TTI =
4875 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
4876 SDValue StoreNode =
4877 !IsCompressing && TTI.hasConditionalLoadStoreForType(
4878 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4879 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4880 Mask)
4881 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4882 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4883 IsCompressing);
4884 DAG.setRoot(StoreNode);
4885 setValue(&I, StoreNode);
4886}
4887
4888// Get a uniform base for the Gather/Scatter intrinsic.
4889// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4890// We try to represent it as a base pointer + vector of indices.
4891// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4892// The first operand of the GEP may be a single pointer or a vector of pointers
4893// Example:
4894// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4895// or
4896// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4897// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4898//
4899// When the first GEP operand is a single pointer - it is the uniform base we
4900// are looking for. If first operand of the GEP is a splat vector - we
4901// extract the splat value and use it as a uniform base.
4902// In all other cases the function returns 'false'.
4903static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4904 SDValue &Scale, SelectionDAGBuilder *SDB,
4905 const BasicBlock *CurBB, uint64_t ElemSize) {
4906 SelectionDAG& DAG = SDB->DAG;
4907 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4908 const DataLayout &DL = DAG.getDataLayout();
4909
4910 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4911
4912 // Handle splat constant pointer.
4913 if (auto *C = dyn_cast<Constant>(Ptr)) {
4914 C = C->getSplatValue();
4915 if (!C)
4916 return false;
4917
4918 Base = SDB->getValue(C);
4919
4920 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
4921 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
4922 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
4923 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4924 return true;
4925 }
4926
4928 if (!GEP || GEP->getParent() != CurBB)
4929 return false;
4930
4931 if (GEP->getNumOperands() != 2)
4932 return false;
4933
4934 const Value *BasePtr = GEP->getPointerOperand();
4935 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
4936
4937 // Make sure the base is scalar and the index is a vector.
4938 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
4939 return false;
4940
4941 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
4942 if (ScaleVal.isScalable())
4943 return false;
4944
4945 // Target may not support the required addressing mode.
4946 if (ScaleVal != 1 &&
4947 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
4948 return false;
4949
4950 Base = SDB->getValue(BasePtr);
4951 Index = SDB->getValue(IndexVal);
4952
4953 Scale =
4954 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4955 return true;
4956}
4957
4958void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
4959 SDLoc sdl = getCurSDLoc();
4960
4961 // llvm.masked.scatter.*(Src0, Ptrs, alignment, Mask)
4962 const Value *Ptr = I.getArgOperand(1);
4963 SDValue Src0 = getValue(I.getArgOperand(0));
4964 SDValue Mask = getValue(I.getArgOperand(3));
4965 EVT VT = Src0.getValueType();
4966 Align Alignment = cast<ConstantInt>(I.getArgOperand(2))
4967 ->getMaybeAlignValue()
4968 .value_or(DAG.getEVTAlign(VT.getScalarType()));
4969 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4970
4971 SDValue Base;
4972 SDValue Index;
4973 SDValue Scale;
4974 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
4975 I.getParent(), VT.getScalarStoreSize());
4976
4977 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
4978 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4979 MachinePointerInfo(AS), MachineMemOperand::MOStore,
4980 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4981 if (!UniformBase) {
4982 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4983 Index = getValue(Ptr);
4984 Scale =
4985 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4986 }
4987
4988 EVT IdxVT = Index.getValueType();
4989 EVT EltTy = IdxVT.getVectorElementType();
4990 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
4991 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
4992 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
4993 }
4994
4995 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
4996 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
4997 Ops, MMO, ISD::SIGNED_SCALED, false);
4998 DAG.setRoot(Scatter);
4999 setValue(&I, Scatter);
5000}
5001
5002void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5003 SDLoc sdl = getCurSDLoc();
5004
5005 auto getMaskedLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
5006 Align &Alignment) {
5007 // @llvm.masked.load.*(Ptr, alignment, Mask, Src0)
5008 Ptr = I.getArgOperand(0);
5009 Alignment = cast<ConstantInt>(I.getArgOperand(1))->getAlignValue();
5010 Mask = I.getArgOperand(2);
5011 Src0 = I.getArgOperand(3);
5012 };
5013 auto getExpandingLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
5014 Align &Alignment) {
5015 // @llvm.masked.expandload.*(Ptr, Mask, Src0)
5016 Ptr = I.getArgOperand(0);
5017 Alignment = I.getParamAlign(0).valueOrOne();
5018 Mask = I.getArgOperand(1);
5019 Src0 = I.getArgOperand(2);
5020 };
5021
5022 Value *PtrOperand, *MaskOperand, *Src0Operand;
5023 Align Alignment;
5024 if (IsExpanding)
5025 getExpandingLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
5026 else
5027 getMaskedLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
5028
5029 SDValue Ptr = getValue(PtrOperand);
5030 SDValue Src0 = getValue(Src0Operand);
5031 SDValue Mask = getValue(MaskOperand);
5032 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5033
5034 EVT VT = Src0.getValueType();
5035 AAMDNodes AAInfo = I.getAAMetadata();
5036 const MDNode *Ranges = getRangeMetadata(I);
5037
5038 // Do not serialize masked loads of constant memory with anything.
5039 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5040 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5041
5042 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5043
5044 auto MMOFlags = MachineMemOperand::MOLoad;
5045 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5047
5048 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5049 MachinePointerInfo(PtrOperand), MMOFlags,
5050 LocationSize::beforeOrAfterPointer(), Alignment, AAInfo, Ranges);
5051
5052 const auto &TLI = DAG.getTargetLoweringInfo();
5053 const auto &TTI =
5054 TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
5055 // The Load/Res may point to different values and both of them are output
5056 // variables.
5057 SDValue Load;
5058 SDValue Res;
5059 if (!IsExpanding && TTI.hasConditionalLoadStoreForType(Src0Operand->getType(),
5060 /*IsStore=*/false))
5061 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5062 else
5063 Res = Load =
5064 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5065 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5066 if (AddToChain)
5067 PendingLoads.push_back(Load.getValue(1));
5068 setValue(&I, Res);
5069}
5070
5071void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5072 SDLoc sdl = getCurSDLoc();
5073
5074 // @llvm.masked.gather.*(Ptrs, alignment, Mask, Src0)
5075 const Value *Ptr = I.getArgOperand(0);
5076 SDValue Src0 = getValue(I.getArgOperand(3));
5077 SDValue Mask = getValue(I.getArgOperand(2));
5078
5079 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5080 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5081 Align Alignment = cast<ConstantInt>(I.getArgOperand(1))
5082 ->getMaybeAlignValue()
5083 .value_or(DAG.getEVTAlign(VT.getScalarType()));
5084
5085 const MDNode *Ranges = getRangeMetadata(I);
5086
5087 SDValue Root = DAG.getRoot();
5088 SDValue Base;
5089 SDValue Index;
5090 SDValue Scale;
5091 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5092 I.getParent(), VT.getScalarStoreSize());
5093 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5094 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5095 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5096 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5097 Ranges);
5098
5099 if (!UniformBase) {
5100 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5101 Index = getValue(Ptr);
5102 Scale =
5103 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5104 }
5105
5106 EVT IdxVT = Index.getValueType();
5107 EVT EltTy = IdxVT.getVectorElementType();
5108 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5109 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5110 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5111 }
5112
5113 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5114 SDValue Gather =
5115 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5117
5118 PendingLoads.push_back(Gather.getValue(1));
5119 setValue(&I, Gather);
5120}
5121
5122void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5123 SDLoc dl = getCurSDLoc();
5124 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5125 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5126 SyncScope::ID SSID = I.getSyncScopeID();
5127
5128 SDValue InChain = getRoot();
5129
5130 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5131 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5132
5133 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5134 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5135
5136 MachineFunction &MF = DAG.getMachineFunction();
5137 MachineMemOperand *MMO = MF.getMachineMemOperand(
5138 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5139 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5140 FailureOrdering);
5141
5142 SDValue L = DAG.getAtomicCmpSwap(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS,
5143 dl, MemVT, VTs, InChain,
5144 getValue(I.getPointerOperand()),
5145 getValue(I.getCompareOperand()),
5146 getValue(I.getNewValOperand()), MMO);
5147
5148 SDValue OutChain = L.getValue(2);
5149
5150 setValue(&I, L);
5151 DAG.setRoot(OutChain);
5152}
5153
5154void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5155 SDLoc dl = getCurSDLoc();
5157 switch (I.getOperation()) {
5158 default: llvm_unreachable("Unknown atomicrmw operation");
5159 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
5160 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
5161 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
5162 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
5163 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
5164 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
5165 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
5166 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
5167 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
5168 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
5169 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
5170 case AtomicRMWInst::FAdd: NT = ISD::ATOMIC_LOAD_FADD; break;
5171 case AtomicRMWInst::FSub: NT = ISD::ATOMIC_LOAD_FSUB; break;
5172 case AtomicRMWInst::FMax: NT = ISD::ATOMIC_LOAD_FMAX; break;
5173 case AtomicRMWInst::FMin: NT = ISD::ATOMIC_LOAD_FMIN; break;
5175 NT = ISD::ATOMIC_LOAD_FMAXIMUM;
5176 break;
5178 NT = ISD::ATOMIC_LOAD_FMINIMUM;
5179 break;
5181 NT = ISD::ATOMIC_LOAD_UINC_WRAP;
5182 break;
5184 NT = ISD::ATOMIC_LOAD_UDEC_WRAP;
5185 break;
5187 NT = ISD::ATOMIC_LOAD_USUB_COND;
5188 break;
5190 NT = ISD::ATOMIC_LOAD_USUB_SAT;
5191 break;
5192 }
5193 AtomicOrdering Ordering = I.getOrdering();
5194 SyncScope::ID SSID = I.getSyncScopeID();
5195
5196 SDValue InChain = getRoot();
5197
5198 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5199 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5200 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5201
5202 MachineFunction &MF = DAG.getMachineFunction();
5203 MachineMemOperand *MMO = MF.getMachineMemOperand(
5204 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5205 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
5206
5207 SDValue L =
5208 DAG.getAtomic(NT, dl, MemVT, InChain,
5209 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5210 MMO);
5211
5212 SDValue OutChain = L.getValue(1);
5213
5214 setValue(&I, L);
5215 DAG.setRoot(OutChain);
5216}
5217
5218void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5219 SDLoc dl = getCurSDLoc();
5220 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5221 SDValue Ops[3];
5222 Ops[0] = getRoot();
5223 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5224 TLI.getFenceOperandTy(DAG.getDataLayout()));
5225 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5226 TLI.getFenceOperandTy(DAG.getDataLayout()));
5227 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5228 setValue(&I, N);
5229 DAG.setRoot(N);
5230}
5231
5232void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5233 SDLoc dl = getCurSDLoc();
5234 AtomicOrdering Order = I.getOrdering();
5235 SyncScope::ID SSID = I.getSyncScopeID();
5236
5237 SDValue InChain = getRoot();
5238
5239 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5240 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5241 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5242
5243 if (!TLI.supportsUnalignedAtomics() &&
5244 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5245 report_fatal_error("Cannot generate unaligned atomic load");
5246
5247 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5248
5249 const MDNode *Ranges = getRangeMetadata(I);
5250 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5251 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5252 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5253
5254 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5255
5256 SDValue Ptr = getValue(I.getPointerOperand());
5257 SDValue L =
5258 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5259
5260 SDValue OutChain = L.getValue(1);
5261 if (MemVT != VT)
5262 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5263
5264 setValue(&I, L);
5265 DAG.setRoot(OutChain);
5266}
5267
5268void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5269 SDLoc dl = getCurSDLoc();
5270
5271 AtomicOrdering Ordering = I.getOrdering();
5272 SyncScope::ID SSID = I.getSyncScopeID();
5273
5274 SDValue InChain = getRoot();
5275
5276 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5277 EVT MemVT =
5278 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5279
5280 if (!TLI.supportsUnalignedAtomics() &&
5281 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5282 report_fatal_error("Cannot generate unaligned atomic store");
5283
5284 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5285
5286 MachineFunction &MF = DAG.getMachineFunction();
5287 MachineMemOperand *MMO = MF.getMachineMemOperand(
5288 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5289 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5290
5291 SDValue Val = getValue(I.getValueOperand());
5292 if (Val.getValueType() != MemVT)
5293 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5294 SDValue Ptr = getValue(I.getPointerOperand());
5295
5296 SDValue OutChain =
5297 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5298
5299 setValue(&I, OutChain);
5300 DAG.setRoot(OutChain);
5301}
5302
5303/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5304/// node.
5305void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5306 unsigned Intrinsic) {
5307 // Ignore the callsite's attributes. A specific call site may be marked with
5308 // readnone, but the lowering code will expect the chain based on the
5309 // definition.
5310 const Function *F = I.getCalledFunction();
5311 bool HasChain = !F->doesNotAccessMemory();
5312 bool OnlyLoad =
5313 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5314
5315 // Build the operand list.
5317 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5318 if (OnlyLoad) {
5319 // We don't need to serialize loads against other loads.
5320 Ops.push_back(DAG.getRoot());
5321 } else {
5322 Ops.push_back(getRoot());
5323 }
5324 }
5325
5326 // Info is set by getTgtMemIntrinsic
5327 TargetLowering::IntrinsicInfo Info;
5328 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5329 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I,
5330 DAG.getMachineFunction(),
5331 Intrinsic);
5332
5333 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5334 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
5336 Ops.push_back(DAG.getTargetConstant(Intrinsic, getCurSDLoc(),
5337 TLI.getPointerTy(DAG.getDataLayout())));
5338
5339 // Add all operands of the call to the operand list.
5340 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5341 const Value *Arg = I.getArgOperand(i);
5342 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5343 Ops.push_back(getValue(Arg));
5344 continue;
5345 }
5346
5347 // Use TargetConstant instead of a regular constant for immarg.
5348 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5349 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5350 assert(CI->getBitWidth() <= 64 &&
5351 "large intrinsic immediates not handled");
5352 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5353 } else {
5354 Ops.push_back(
5355 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5356 }
5357 }
5358
5359 SmallVector<EVT, 4> ValueVTs;
5360 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5361
5362 if (HasChain)
5363 ValueVTs.push_back(MVT::Other);
5364
5365 SDVTList VTs = DAG.getVTList(ValueVTs);
5366
5367 // Propagate fast-math-flags from IR to node(s).
5368 SDNodeFlags Flags;
5369 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5370 Flags.copyFMF(*FPMO);
5371 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5372
5373 // Create the node.
5375
5376 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5377 auto *Token = Bundle->Inputs[0].get();
5378 SDValue ConvControlToken = getValue(Token);
5379 assert(Ops.back().getValueType() != MVT::Glue &&
5380 "Did not expected another glue node here.");
5381 ConvControlToken =
5382 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5383 Ops.push_back(ConvControlToken);
5384 }
5385
5386 // In some cases, custom collection of operands from CallInst I may be needed.
5388 if (IsTgtIntrinsic) {
5389 // This is target intrinsic that touches memory
5390 //
5391 // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic
5392 // didn't yield anything useful.
5393 MachinePointerInfo MPI;
5394 if (Info.ptrVal)
5395 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5396 else if (Info.fallbackAddressSpace)
5397 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5398 EVT MemVT = Info.memVT;
5399 LocationSize Size = LocationSize::precise(Info.size);
5400 if (Size.hasValue() && !Size.getValue())
5402 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5403 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5404 MPI, Info.flags, Size, Alignment, I.getAAMetadata(), /*Ranges=*/nullptr,
5405 Info.ssid, Info.order, Info.failureOrder);
5406 Result =
5407 DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops, MemVT, MMO);
5408 } else if (!HasChain) {
5409 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5410 } else if (!I.getType()->isVoidTy()) {
5411 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5412 } else {
5413 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5414 }
5415
5416 if (HasChain) {
5417 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
5418 if (OnlyLoad)
5419 PendingLoads.push_back(Chain);
5420 else
5421 DAG.setRoot(Chain);
5422 }
5423
5424 if (!I.getType()->isVoidTy()) {
5425 if (!isa<VectorType>(I.getType()))
5426 Result = lowerRangeToAssertZExt(DAG, I, Result);
5427
5428 MaybeAlign Alignment = I.getRetAlign();
5429
5430 // Insert `assertalign` node if there's an alignment.
5431 if (InsertAssertAlign && Alignment) {
5432 Result =
5433 DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5434 }
5435 }
5436
5437 setValue(&I, Result);
5438}
5439
5440/// GetSignificand - Get the significand and build it into a floating-point
5441/// number with exponent of 1:
5442///
5443/// Op = (Op & 0x007fffff) | 0x3f800000;
5444///
5445/// where Op is the hexadecimal representation of floating point value.
5447 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5448 DAG.getConstant(0x007fffff, dl, MVT::i32));
5449 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5450 DAG.getConstant(0x3f800000, dl, MVT::i32));
5451 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5452}
5453
5454/// GetExponent - Get the exponent:
5455///
5456/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5457///
5458/// where Op is the hexadecimal representation of floating point value.
5460 const TargetLowering &TLI, const SDLoc &dl) {
5461 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5462 DAG.getConstant(0x7f800000, dl, MVT::i32));
5463 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5464 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5465 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5466 DAG.getConstant(127, dl, MVT::i32));
5467 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5468}
5469
5470/// getF32Constant - Get 32-bit floating point constant.
5472 const SDLoc &dl) {
5473 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5474 MVT::f32);
5475}
5476
5478 SelectionDAG &DAG) {
5479 // TODO: What fast-math-flags should be set on the floating-point nodes?
5480
5481 // IntegerPartOfX = ((int32_t)(t0);
5482 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5483
5484 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5485 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5486 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5487
5488 // IntegerPartOfX <<= 23;
5489 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5490 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5491
5492 SDValue TwoToFractionalPartOfX;
5493 if (LimitFloatPrecision <= 6) {
5494 // For floating-point precision of 6:
5495 //
5496 // TwoToFractionalPartOfX =
5497 // 0.997535578f +
5498 // (0.735607626f + 0.252464424f * x) * x;
5499 //
5500 // error 0.0144103317, which is 6 bits
5501 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5502 getF32Constant(DAG, 0x3e814304, dl));
5503 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5504 getF32Constant(DAG, 0x3f3c50c8, dl));
5505 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5506 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5507 getF32Constant(DAG, 0x3f7f5e7e, dl));
5508 } else if (LimitFloatPrecision <= 12) {
5509 // For floating-point precision of 12:
5510 //
5511 // TwoToFractionalPartOfX =
5512 // 0.999892986f +
5513 // (0.696457318f +
5514 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5515 //
5516 // error 0.000107046256, which is 13 to 14 bits
5517 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5518 getF32Constant(DAG, 0x3da235e3, dl));
5519 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5520 getF32Constant(DAG, 0x3e65b8f3, dl));
5521 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5522 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5523 getF32Constant(DAG, 0x3f324b07, dl));
5524 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5525 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5526 getF32Constant(DAG, 0x3f7ff8fd, dl));
5527 } else { // LimitFloatPrecision <= 18
5528 // For floating-point precision of 18:
5529 //
5530 // TwoToFractionalPartOfX =
5531 // 0.999999982f +
5532 // (0.693148872f +
5533 // (0.240227044f +
5534 // (0.554906021e-1f +
5535 // (0.961591928e-2f +
5536 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5537 // error 2.47208000*10^(-7), which is better than 18 bits
5538 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5539 getF32Constant(DAG, 0x3924b03e, dl));
5540 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5541 getF32Constant(DAG, 0x3ab24b87, dl));
5542 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5543 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5544 getF32Constant(DAG, 0x3c1d8c17, dl));
5545 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5546 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5547 getF32Constant(DAG, 0x3d634a1d, dl));
5548 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5549 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5550 getF32Constant(DAG, 0x3e75fe14, dl));
5551 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5552 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5553 getF32Constant(DAG, 0x3f317234, dl));
5554 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5555 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5556 getF32Constant(DAG, 0x3f800000, dl));
5557 }
5558
5559 // Add the exponent into the result in integer domain.
5560 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5561 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5562 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5563}
5564
5565/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5566/// limited-precision mode.
5568 const TargetLowering &TLI, SDNodeFlags Flags) {
5569 if (Op.getValueType() == MVT::f32 &&
5571
5572 // Put the exponent in the right bit position for later addition to the
5573 // final result:
5574 //
5575 // t0 = Op * log2(e)
5576
5577 // TODO: What fast-math-flags should be set here?
5578 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5579 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5580 return getLimitedPrecisionExp2(t0, dl, DAG);
5581 }
5582
5583 // No special expansion.
5584 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5585}
5586
5587/// expandLog - Lower a log intrinsic. Handles the special sequences for
5588/// limited-precision mode.
5590 const TargetLowering &TLI, SDNodeFlags Flags) {
5591 // TODO: What fast-math-flags should be set on the floating-point nodes?
5592
5593 if (Op.getValueType() == MVT::f32 &&
5595 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5596
5597 // Scale the exponent by log(2).
5598 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5599 SDValue LogOfExponent =
5600 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5601 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5602
5603 // Get the significand and build it into a floating-point number with
5604 // exponent of 1.
5605 SDValue X = GetSignificand(DAG, Op1, dl);
5606
5607 SDValue LogOfMantissa;
5608 if (LimitFloatPrecision <= 6) {
5609 // For floating-point precision of 6:
5610 //
5611 // LogofMantissa =
5612 // -1.1609546f +
5613 // (1.4034025f - 0.23903021f * x) * x;
5614 //
5615 // error 0.0034276066, which is better than 8 bits
5616 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5617 getF32Constant(DAG, 0xbe74c456, dl));
5618 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5619 getF32Constant(DAG, 0x3fb3a2b1, dl));
5620 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5621 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5622 getF32Constant(DAG, 0x3f949a29, dl));
5623 } else if (LimitFloatPrecision <= 12) {
5624 // For floating-point precision of 12:
5625 //
5626 // LogOfMantissa =
5627 // -1.7417939f +
5628 // (2.8212026f +
5629 // (-1.4699568f +
5630 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5631 //
5632 // error 0.000061011436, which is 14 bits
5633 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5634 getF32Constant(DAG, 0xbd67b6d6, dl));
5635 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5636 getF32Constant(DAG, 0x3ee4f4b8, dl));
5637 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5638 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5639 getF32Constant(DAG, 0x3fbc278b, dl));
5640 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5641 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5642 getF32Constant(DAG, 0x40348e95, dl));
5643 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5644 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5645 getF32Constant(DAG, 0x3fdef31a, dl));
5646 } else { // LimitFloatPrecision <= 18
5647 // For floating-point precision of 18:
5648 //
5649 // LogOfMantissa =
5650 // -2.1072184f +
5651 // (4.2372794f +
5652 // (-3.7029485f +
5653 // (2.2781945f +
5654 // (-0.87823314f +
5655 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5656 //
5657 // error 0.0000023660568, which is better than 18 bits
5658 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5659 getF32Constant(DAG, 0xbc91e5ac, dl));
5660 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5661 getF32Constant(DAG, 0x3e4350aa, dl));
5662 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5663 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5664 getF32Constant(DAG, 0x3f60d3e3, dl));
5665 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5666 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5667 getF32Constant(DAG, 0x4011cdf0, dl));
5668 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5669 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5670 getF32Constant(DAG, 0x406cfd1c, dl));
5671 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5672 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5673 getF32Constant(DAG, 0x408797cb, dl));
5674 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5675 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5676 getF32Constant(DAG, 0x4006dcab, dl));
5677 }
5678
5679 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5680 }
5681
5682 // No special expansion.
5683 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5684}
5685
5686/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5687/// limited-precision mode.
5689 const TargetLowering &TLI, SDNodeFlags Flags) {
5690 // TODO: What fast-math-flags should be set on the floating-point nodes?
5691
5692 if (Op.getValueType() == MVT::f32 &&
5694 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5695
5696 // Get the exponent.
5697 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5698
5699 // Get the significand and build it into a floating-point number with
5700 // exponent of 1.
5701 SDValue X = GetSignificand(DAG, Op1, dl);
5702
5703 // Different possible minimax approximations of significand in
5704 // floating-point for various degrees of accuracy over [1,2].
5705 SDValue Log2ofMantissa;
5706 if (LimitFloatPrecision <= 6) {
5707 // For floating-point precision of 6:
5708 //
5709 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5710 //
5711 // error 0.0049451742, which is more than 7 bits
5712 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5713 getF32Constant(DAG, 0xbeb08fe0, dl));
5714 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5715 getF32Constant(DAG, 0x40019463, dl));
5716 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5717 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5718 getF32Constant(DAG, 0x3fd6633d, dl));
5719 } else if (LimitFloatPrecision <= 12) {
5720 // For floating-point precision of 12:
5721 //
5722 // Log2ofMantissa =
5723 // -2.51285454f +
5724 // (4.07009056f +
5725 // (-2.12067489f +
5726 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5727 //
5728 // error 0.0000876136000, which is better than 13 bits
5729 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5730 getF32Constant(DAG, 0xbda7262e, dl));
5731 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5732 getF32Constant(DAG, 0x3f25280b, dl));
5733 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5734 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5735 getF32Constant(DAG, 0x4007b923, dl));
5736 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5737 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5738 getF32Constant(DAG, 0x40823e2f, dl));
5739 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5740 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5741 getF32Constant(DAG, 0x4020d29c, dl));
5742 } else { // LimitFloatPrecision <= 18
5743 // For floating-point precision of 18:
5744 //
5745 // Log2ofMantissa =
5746 // -3.0400495f +
5747 // (6.1129976f +
5748 // (-5.3420409f +
5749 // (3.2865683f +
5750 // (-1.2669343f +
5751 // (0.27515199f -
5752 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5753 //
5754 // error 0.0000018516, which is better than 18 bits
5755 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5756 getF32Constant(DAG, 0xbcd2769e, dl));
5757 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5758 getF32Constant(DAG, 0x3e8ce0b9, dl));
5759 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5760 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5761 getF32Constant(DAG, 0x3fa22ae7, dl));
5762 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5763 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5764 getF32Constant(DAG, 0x40525723, dl));
5765 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5766 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5767 getF32Constant(DAG, 0x40aaf200, dl));
5768 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5769 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5770 getF32Constant(DAG, 0x40c39dad, dl));
5771 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5772 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5773 getF32Constant(DAG, 0x4042902c, dl));
5774 }
5775
5776 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5777 }
5778
5779 // No special expansion.
5780 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5781}
5782
5783/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5784/// limited-precision mode.
5786 const TargetLowering &TLI, SDNodeFlags Flags) {
5787 // TODO: What fast-math-flags should be set on the floating-point nodes?
5788
5789 if (Op.getValueType() == MVT::f32 &&
5791 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5792
5793 // Scale the exponent by log10(2) [0.30102999f].
5794 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5795 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5796 getF32Constant(DAG, 0x3e9a209a, dl));
5797
5798 // Get the significand and build it into a floating-point number with
5799 // exponent of 1.
5800 SDValue X = GetSignificand(DAG, Op1, dl);
5801
5802 SDValue Log10ofMantissa;
5803 if (LimitFloatPrecision <= 6) {
5804 // For floating-point precision of 6:
5805 //
5806 // Log10ofMantissa =
5807 // -0.50419619f +
5808 // (0.60948995f - 0.10380950f * x) * x;
5809 //
5810 // error 0.0014886165, which is 6 bits
5811 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5812 getF32Constant(DAG, 0xbdd49a13, dl));
5813 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5814 getF32Constant(DAG, 0x3f1c0789, dl));
5815 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5816 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5817 getF32Constant(DAG, 0x3f011300, dl));
5818 } else if (LimitFloatPrecision <= 12) {
5819 // For floating-point precision of 12:
5820 //
5821 // Log10ofMantissa =
5822 // -0.64831180f +
5823 // (0.91751397f +
5824 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5825 //
5826 // error 0.00019228036, which is better than 12 bits
5827 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5828 getF32Constant(DAG, 0x3d431f31, dl));
5829 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5830 getF32Constant(DAG, 0x3ea21fb2, dl));
5831 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5832 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5833 getF32Constant(DAG, 0x3f6ae232, dl));
5834 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5835 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5836 getF32Constant(DAG, 0x3f25f7c3, dl));
5837 } else { // LimitFloatPrecision <= 18
5838 // For floating-point precision of 18:
5839 //
5840 // Log10ofMantissa =
5841 // -0.84299375f +
5842 // (1.5327582f +
5843 // (-1.0688956f +
5844 // (0.49102474f +
5845 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5846 //
5847 // error 0.0000037995730, which is better than 18 bits
5848 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5849 getF32Constant(DAG, 0x3c5d51ce, dl));
5850 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5851 getF32Constant(DAG, 0x3e00685a, dl));
5852 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5853 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5854 getF32Constant(DAG, 0x3efb6798, dl));
5855 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5856 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5857 getF32Constant(DAG, 0x3f88d192, dl));
5858 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5859 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5860 getF32Constant(DAG, 0x3fc4316c, dl));
5861 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5862 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5863 getF32Constant(DAG, 0x3f57ce70, dl));
5864 }
5865
5866 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5867 }
5868
5869 // No special expansion.
5870 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5871}
5872
5873/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5874/// limited-precision mode.
5876 const TargetLowering &TLI, SDNodeFlags Flags) {
5877 if (Op.getValueType() == MVT::f32 &&
5879 return getLimitedPrecisionExp2(Op, dl, DAG);
5880
5881 // No special expansion.
5882 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
5883}
5884
5885/// visitPow - Lower a pow intrinsic. Handles the special sequences for
5886/// limited-precision mode with x == 10.0f.
5888 SelectionDAG &DAG, const TargetLowering &TLI,
5889 SDNodeFlags Flags) {
5890 bool IsExp10 = false;
5891 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
5894 APFloat Ten(10.0f);
5895 IsExp10 = LHSC->isExactlyValue(Ten);
5896 }
5897 }
5898
5899 // TODO: What fast-math-flags should be set on the FMUL node?
5900 if (IsExp10) {
5901 // Put the exponent in the right bit position for later addition to the
5902 // final result:
5903 //
5904 // #define LOG2OF10 3.3219281f
5905 // t0 = Op * LOG2OF10;
5906 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
5907 getF32Constant(DAG, 0x40549a78, dl));
5908 return getLimitedPrecisionExp2(t0, dl, DAG);
5909 }
5910
5911 // No special expansion.
5912 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
5913}
5914
5915/// ExpandPowI - Expand a llvm.powi intrinsic.
5917 SelectionDAG &DAG) {
5918 // If RHS is a constant, we can expand this out to a multiplication tree if
5919 // it's beneficial on the target, otherwise we end up lowering to a call to
5920 // __powidf2 (for example).
5922 unsigned Val = RHSC->getSExtValue();
5923
5924 // powi(x, 0) -> 1.0
5925 if (Val == 0)
5926 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
5927
5929 Val, DAG.shouldOptForSize())) {
5930 // Get the exponent as a positive value.
5931 if ((int)Val < 0)
5932 Val = -Val;
5933 // We use the simple binary decomposition method to generate the multiply
5934 // sequence. There are more optimal ways to do this (for example,
5935 // powi(x,15) generates one more multiply than it should), but this has
5936 // the benefit of being both really simple and much better than a libcall.
5937 SDValue Res; // Logically starts equal to 1.0
5938 SDValue CurSquare = LHS;
5939 // TODO: Intrinsics should have fast-math-flags that propagate to these
5940 // nodes.
5941 while (Val) {
5942 if (Val & 1) {
5943 if (Res.getNode())
5944 Res =
5945 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
5946 else
5947 Res = CurSquare; // 1.0*CurSquare.
5948 }
5949
5950 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
5951 CurSquare, CurSquare);
5952 Val >>= 1;
5953 }
5954
5955 // If the original was negative, invert the result, producing 1/(x*x*x).
5956 if (RHSC->getSExtValue() < 0)
5957 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
5958 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
5959 return Res;
5960 }
5961 }
5962
5963 // Otherwise, expand to a libcall.
5964 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
5965}
5966
5967static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
5968 SDValue LHS, SDValue RHS, SDValue Scale,
5969 SelectionDAG &DAG, const TargetLowering &TLI) {
5970 EVT VT = LHS.getValueType();
5971 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
5972 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
5973 LLVMContext &Ctx = *DAG.getContext();
5974
5975 // If the type is legal but the operation isn't, this node might survive all
5976 // the way to operation legalization. If we end up there and we do not have
5977 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
5978 // node.
5979
5980 // Coax the legalizer into expanding the node during type legalization instead
5981 // by bumping the size by one bit. This will force it to Promote, enabling the
5982 // early expansion and avoiding the need to expand later.
5983
5984 // We don't have to do this if Scale is 0; that can always be expanded, unless
5985 // it's a saturating signed operation. Those can experience true integer
5986 // division overflow, a case which we must avoid.
5987
5988 // FIXME: We wouldn't have to do this (or any of the early
5989 // expansion/promotion) if it was possible to expand a libcall of an
5990 // illegal type during operation legalization. But it's not, so things
5991 // get a bit hacky.
5992 unsigned ScaleInt = Scale->getAsZExtVal();
5993 if ((ScaleInt > 0 || (Saturating && Signed)) &&
5994 (TLI.isTypeLegal(VT) ||
5995 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
5997 Opcode, VT, ScaleInt);
5998 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
5999 EVT PromVT;
6000 if (VT.isScalarInteger())
6001 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6002 else if (VT.isVector()) {
6003 PromVT = VT.getVectorElementType();
6004 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6005 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6006 } else
6007 llvm_unreachable("Wrong VT for DIVFIX?");
6008 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6009 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6010 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6011 // For saturating operations, we need to shift up the LHS to get the
6012 // proper saturation width, and then shift down again afterwards.
6013 if (Saturating)
6014 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6015 DAG.getConstant(1, DL, ShiftTy));
6016 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6017 if (Saturating)
6018 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6019 DAG.getConstant(1, DL, ShiftTy));
6020 return DAG.getZExtOrTrunc(Res, DL, VT);
6021 }
6022 }
6023
6024 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6025}
6026
6027// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6028// bitcasted, or split argument. Returns a list of <Register, size in bits>
6029static void
6030getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6031 const SDValue &N) {
6032 switch (N.getOpcode()) {
6033 case ISD::CopyFromReg: {
6034 SDValue Op = N.getOperand(1);
6035 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6036 Op.getValueType().getSizeInBits());
6037 return;
6038 }
6039 case ISD::BITCAST:
6040 case ISD::AssertZext:
6041 case ISD::AssertSext:
6042 case ISD::TRUNCATE:
6043 getUnderlyingArgRegs(Regs, N.getOperand(0));
6044 return;
6045 case ISD::BUILD_PAIR:
6046 case ISD::BUILD_VECTOR:
6048 for (SDValue Op : N->op_values())
6049 getUnderlyingArgRegs(Regs, Op);
6050 return;
6051 default:
6052 return;
6053 }
6054}
6055
6056/// If the DbgValueInst is a dbg_value of a function argument, create the
6057/// corresponding DBG_VALUE machine instruction for it now. At the end of
6058/// instruction selection, they will be inserted to the entry BB.
6059/// We don't currently support this for variadic dbg_values, as they shouldn't
6060/// appear for function arguments or in the prologue.
6061bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6062 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6063 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6064 const Argument *Arg = dyn_cast<Argument>(V);
6065 if (!Arg)
6066 return false;
6067
6068 MachineFunction &MF = DAG.getMachineFunction();
6069 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6070
6071 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6072 // we've been asked to pursue.
6073 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6074 bool Indirect) {
6075 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6076 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6077 // pointing at the VReg, which will be patched up later.
6078 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6080 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6081 /* isKill */ false, /* isDead */ false,
6082 /* isUndef */ false, /* isEarlyClobber */ false,
6083 /* SubReg */ 0, /* isDebug */ true)});
6084
6085 auto *NewDIExpr = FragExpr;
6086 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6087 // the DIExpression.
6088 if (Indirect)
6089 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6091 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6092 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6093 } else {
6094 // Create a completely standard DBG_VALUE.
6095 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6096 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6097 }
6098 };
6099
6100 if (Kind == FuncArgumentDbgValueKind::Value) {
6101 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6102 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6103 // the entry block.
6104 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6105 if (!IsInEntryBlock)
6106 return false;
6107
6108 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6109 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6110 // variable that also is a param.
6111 //
6112 // Although, if we are at the top of the entry block already, we can still
6113 // emit using ArgDbgValue. This might catch some situations when the
6114 // dbg.value refers to an argument that isn't used in the entry block, so
6115 // any CopyToReg node would be optimized out and the only way to express
6116 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6117 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6118 // we should only emit as ArgDbgValue if the Variable is an argument to the
6119 // current function, and the dbg.value intrinsic is found in the entry
6120 // block.
6121 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6122 !DL->getInlinedAt();
6123 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6124 if (!IsInPrologue && !VariableIsFunctionInputArg)
6125 return false;
6126
6127 // Here we assume that a function argument on IR level only can be used to
6128 // describe one input parameter on source level. If we for example have
6129 // source code like this
6130 //
6131 // struct A { long x, y; };
6132 // void foo(struct A a, long b) {
6133 // ...
6134 // b = a.x;
6135 // ...
6136 // }
6137 //
6138 // and IR like this
6139 //
6140 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6141 // entry:
6142 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6143 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6144 // call void @llvm.dbg.value(metadata i32 %b, "b",
6145 // ...
6146 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6147 // ...
6148 //
6149 // then the last dbg.value is describing a parameter "b" using a value that
6150 // is an argument. But since we already has used %a1 to describe a parameter
6151 // we should not handle that last dbg.value here (that would result in an
6152 // incorrect hoisting of the DBG_VALUE to the function entry).
6153 // Notice that we allow one dbg.value per IR level argument, to accommodate
6154 // for the situation with fragments above.
6155 // If there is no node for the value being handled, we return true to skip
6156 // the normal generation of debug info, as it would kill existing debug
6157 // info for the parameter in case of duplicates.
6158 if (VariableIsFunctionInputArg) {
6159 unsigned ArgNo = Arg->getArgNo();
6160 if (ArgNo >= FuncInfo.DescribedArgs.size())
6161 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6162 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6163 return !NodeMap[V].getNode();
6164 FuncInfo.DescribedArgs.set(ArgNo);
6165 }
6166 }
6167
6168 bool IsIndirect = false;
6169 std::optional<MachineOperand> Op;
6170 // Some arguments' frame index is recorded during argument lowering.
6171 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6172 if (FI != std::numeric_limits<int>::max())
6174
6176 if (!Op && N.getNode()) {
6177 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6178 Register Reg;
6179 if (ArgRegsAndSizes.size() == 1)
6180 Reg = ArgRegsAndSizes.front().first;
6181
6182 if (Reg && Reg.isVirtual()) {
6183 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6184 Register PR = RegInfo.getLiveInPhysReg(Reg);
6185 if (PR)
6186 Reg = PR;
6187 }
6188 if (Reg) {
6190 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6191 }
6192 }
6193
6194 if (!Op && N.getNode()) {
6195 // Check if frame index is available.
6196 SDValue LCandidate = peekThroughBitcasts(N);
6197 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6198 if (FrameIndexSDNode *FINode =
6199 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6200 Op = MachineOperand::CreateFI(FINode->getIndex());
6201 }
6202
6203 if (!Op) {
6204 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6205 auto splitMultiRegDbgValue = [&](ArrayRef<std::pair<Register, TypeSize>>
6206 SplitRegs) {
6207 unsigned Offset = 0;
6208 for (const auto &RegAndSize : SplitRegs) {
6209 // If the expression is already a fragment, the current register
6210 // offset+size might extend beyond the fragment. In this case, only
6211 // the register bits that are inside the fragment are relevant.
6212 int RegFragmentSizeInBits = RegAndSize.second;
6213 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6214 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6215 // The register is entirely outside the expression fragment,
6216 // so is irrelevant for debug info.
6217 if (Offset >= ExprFragmentSizeInBits)
6218 break;
6219 // The register is partially outside the expression fragment, only
6220 // the low bits within the fragment are relevant for debug info.
6221 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6222 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6223 }
6224 }
6225
6226 auto FragmentExpr = DIExpression::createFragmentExpression(
6227 Expr, Offset, RegFragmentSizeInBits);
6228 Offset += RegAndSize.second;
6229 // If a valid fragment expression cannot be created, the variable's
6230 // correct value cannot be determined and so it is set as poison.
6231 if (!FragmentExpr) {
6232 SDDbgValue *SDV = DAG.getConstantDbgValue(
6233 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6234 DAG.AddDbgValue(SDV, false);
6235 continue;
6236 }
6237 MachineInstr *NewMI =
6238 MakeVRegDbgValue(RegAndSize.first, *FragmentExpr,
6239 Kind != FuncArgumentDbgValueKind::Value);
6240 FuncInfo.ArgDbgValues.push_back(NewMI);
6241 }
6242 };
6243
6244 // Check if ValueMap has reg number.
6246 VMI = FuncInfo.ValueMap.find(V);
6247 if (VMI != FuncInfo.ValueMap.end()) {
6248 const auto &TLI = DAG.getTargetLoweringInfo();
6249 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6250 V->getType(), std::nullopt);
6251 if (RFV.occupiesMultipleRegs()) {
6252 splitMultiRegDbgValue(RFV.getRegsAndSizes());
6253 return true;
6254 }
6255
6256 Op = MachineOperand::CreateReg(VMI->second, false);
6257 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6258 } else if (ArgRegsAndSizes.size() > 1) {
6259 // This was split due to the calling convention, and no virtual register
6260 // mapping exists for the value.
6261 splitMultiRegDbgValue(ArgRegsAndSizes);
6262 return true;
6263 }
6264 }
6265
6266 if (!Op)
6267 return false;
6268
6269 assert(Variable->isValidLocationForIntrinsic(DL) &&
6270 "Expected inlined-at fields to agree");
6271 MachineInstr *NewMI = nullptr;
6272
6273 if (Op->isReg())
6274 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6275 else
6276 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6277 Variable, Expr);
6278
6279 // Otherwise, use ArgDbgValues.
6280 FuncInfo.ArgDbgValues.push_back(NewMI);
6281 return true;
6282}
6283
6284/// Return the appropriate SDDbgValue based on N.
6285SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6286 DILocalVariable *Variable,
6287 DIExpression *Expr,
6288 const DebugLoc &dl,
6289 unsigned DbgSDNodeOrder) {
6290 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6291 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6292 // stack slot locations.
6293 //
6294 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6295 // debug values here after optimization:
6296 //
6297 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6298 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6299 //
6300 // Both describe the direct values of their associated variables.
6301 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6302 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6303 }
6304 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6305 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6306}
6307
6308static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6309 switch (Intrinsic) {
6310 case Intrinsic::smul_fix:
6311 return ISD::SMULFIX;
6312 case Intrinsic::umul_fix:
6313 return ISD::UMULFIX;
6314 case Intrinsic::smul_fix_sat:
6315 return ISD::SMULFIXSAT;
6316 case Intrinsic::umul_fix_sat:
6317 return ISD::UMULFIXSAT;
6318 case Intrinsic::sdiv_fix:
6319 return ISD::SDIVFIX;
6320 case Intrinsic::udiv_fix:
6321 return ISD::UDIVFIX;
6322 case Intrinsic::sdiv_fix_sat:
6323 return ISD::SDIVFIXSAT;
6324 case Intrinsic::udiv_fix_sat:
6325 return ISD::UDIVFIXSAT;
6326 default:
6327 llvm_unreachable("Unhandled fixed point intrinsic");
6328 }
6329}
6330
6331/// Given a @llvm.call.preallocated.setup, return the corresponding
6332/// preallocated call.
6333static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6334 assert(cast<CallBase>(PreallocatedSetup)
6336 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6337 "expected call_preallocated_setup Value");
6338 for (const auto *U : PreallocatedSetup->users()) {
6339 auto *UseCall = cast<CallBase>(U);
6340 const Function *Fn = UseCall->getCalledFunction();
6341 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6342 return UseCall;
6343 }
6344 }
6345 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6346}
6347
6348/// If DI is a debug value with an EntryValue expression, lower it using the
6349/// corresponding physical register of the associated Argument value
6350/// (guaranteed to exist by the verifier).
6351bool SelectionDAGBuilder::visitEntryValueDbgValue(
6352 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6353 DIExpression *Expr, DebugLoc DbgLoc) {
6354 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6355 return false;
6356
6357 // These properties are guaranteed by the verifier.
6358 const Argument *Arg = cast<Argument>(Values[0]);
6359 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6360
6361 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6362 if (ArgIt == FuncInfo.ValueMap.end()) {
6363 LLVM_DEBUG(
6364 dbgs() << "Dropping dbg.value: expression is entry_value but "
6365 "couldn't find an associated register for the Argument\n");
6366 return true;
6367 }
6368 Register ArgVReg = ArgIt->getSecond();
6369
6370 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6371 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6372 SDDbgValue *SDV = DAG.getVRegDbgValue(
6373 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6374 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6375 return true;
6376 }
6377 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6378 "couldn't find a physical register\n");
6379 return true;
6380}
6381
6382/// Lower the call to the specified intrinsic function.
6383void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6384 unsigned Intrinsic) {
6385 SDLoc sdl = getCurSDLoc();
6386 switch (Intrinsic) {
6387 case Intrinsic::experimental_convergence_anchor:
6388 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6389 break;
6390 case Intrinsic::experimental_convergence_entry:
6391 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6392 break;
6393 case Intrinsic::experimental_convergence_loop: {
6394 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6395 auto *Token = Bundle->Inputs[0].get();
6396 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6397 getValue(Token)));
6398 break;
6399 }
6400 }
6401}
6402
6403void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6404 unsigned IntrinsicID) {
6405 // For now, we're only lowering an 'add' histogram.
6406 // We can add others later, e.g. saturating adds, min/max.
6407 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6408 "Tried to lower unsupported histogram type");
6409 SDLoc sdl = getCurSDLoc();
6410 Value *Ptr = I.getOperand(0);
6411 SDValue Inc = getValue(I.getOperand(1));
6412 SDValue Mask = getValue(I.getOperand(2));
6413
6414 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6415 DataLayout TargetDL = DAG.getDataLayout();
6416 EVT VT = Inc.getValueType();
6417 Align Alignment = DAG.getEVTAlign(VT);
6418
6419 const MDNode *Ranges = getRangeMetadata(I);
6420
6421 SDValue Root = DAG.getRoot();
6422 SDValue Base;
6423 SDValue Index;
6424 SDValue Scale;
6425 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6426 I.getParent(), VT.getScalarStoreSize());
6427
6428 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6429
6430 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6431 MachinePointerInfo(AS),
6433 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6434
6435 if (!UniformBase) {
6436 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6437 Index = getValue(Ptr);
6438 Scale =
6439 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6440 }
6441
6442 EVT IdxVT = Index.getValueType();
6443 EVT EltTy = IdxVT.getVectorElementType();
6444 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6445 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
6446 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6447 }
6448
6449 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6450
6451 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6452 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6453 Ops, MMO, ISD::SIGNED_SCALED);
6454
6455 setValue(&I, Histogram);
6456 DAG.setRoot(Histogram);
6457}
6458
6459void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6460 unsigned Intrinsic) {
6461 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6462 "Tried lowering invalid vector extract last");
6463 SDLoc sdl = getCurSDLoc();
6464 const DataLayout &Layout = DAG.getDataLayout();
6465 SDValue Data = getValue(I.getOperand(0));
6466 SDValue Mask = getValue(I.getOperand(1));
6467
6468 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6469 EVT ResVT = TLI.getValueType(Layout, I.getType());
6470
6471 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6472 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6473 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6474
6475 Value *Default = I.getOperand(2);
6477 SDValue PassThru = getValue(Default);
6478 EVT BoolVT = Mask.getValueType().getScalarType();
6479 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6480 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6481 }
6482
6483 setValue(&I, Result);
6484}
6485
6486/// Lower the call to the specified intrinsic function.
6487void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6488 unsigned Intrinsic) {
6489 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6490 SDLoc sdl = getCurSDLoc();
6491 DebugLoc dl = getCurDebugLoc();
6492 SDValue Res;
6493
6494 SDNodeFlags Flags;
6495 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6496 Flags.copyFMF(*FPOp);
6497
6498 switch (Intrinsic) {
6499 default:
6500 // By default, turn this into a target intrinsic node.
6501 visitTargetIntrinsic(I, Intrinsic);
6502 return;
6503 case Intrinsic::vscale: {
6504 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6505 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6506 return;
6507 }
6508 case Intrinsic::vastart: visitVAStart(I); return;
6509 case Intrinsic::vaend: visitVAEnd(I); return;
6510 case Intrinsic::vacopy: visitVACopy(I); return;
6511 case Intrinsic::returnaddress:
6512 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6513 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6514 getValue(I.getArgOperand(0))));
6515 return;
6516 case Intrinsic::addressofreturnaddress:
6517 setValue(&I,
6518 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6519 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6520 return;
6521 case Intrinsic::sponentry:
6522 setValue(&I,
6523 DAG.getNode(ISD::SPONENTRY, sdl,
6524 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6525 return;
6526 case Intrinsic::frameaddress:
6527 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6528 TLI.getFrameIndexTy(DAG.getDataLayout()),
6529 getValue(I.getArgOperand(0))));
6530 return;
6531 case Intrinsic::read_volatile_register:
6532 case Intrinsic::read_register: {
6533 Value *Reg = I.getArgOperand(0);
6534 SDValue Chain = getRoot();
6536 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6537 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6538 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6539 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6540 setValue(&I, Res);
6541 DAG.setRoot(Res.getValue(1));
6542 return;
6543 }
6544 case Intrinsic::write_register: {
6545 Value *Reg = I.getArgOperand(0);
6546 Value *RegValue = I.getArgOperand(1);
6547 SDValue Chain = getRoot();
6549 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6550 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6551 RegName, getValue(RegValue)));
6552 return;
6553 }
6554 case Intrinsic::memcpy:
6555 case Intrinsic::memcpy_inline: {
6556 const auto &MCI = cast<MemCpyInst>(I);
6557 SDValue Dst = getValue(I.getArgOperand(0));
6558 SDValue Src = getValue(I.getArgOperand(1));
6559 SDValue Size = getValue(I.getArgOperand(2));
6560 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6561 "memcpy_inline needs constant size");
6562 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6563 Align DstAlign = MCI.getDestAlign().valueOrOne();
6564 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6565 Align Alignment = std::min(DstAlign, SrcAlign);
6566 bool isVol = MCI.isVolatile();
6567 // FIXME: Support passing different dest/src alignments to the memcpy DAG
6568 // node.
6569 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6570 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol,
6571 MCI.isForceInlined(), &I, std::nullopt,
6572 MachinePointerInfo(I.getArgOperand(0)),
6573 MachinePointerInfo(I.getArgOperand(1)),
6574 I.getAAMetadata(), BatchAA);
6575 updateDAGForMaybeTailCall(MC);
6576 return;
6577 }
6578 case Intrinsic::memset:
6579 case Intrinsic::memset_inline: {
6580 const auto &MSII = cast<MemSetInst>(I);
6581 SDValue Dst = getValue(I.getArgOperand(0));
6582 SDValue Value = getValue(I.getArgOperand(1));
6583 SDValue Size = getValue(I.getArgOperand(2));
6584 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6585 "memset_inline needs constant size");
6586 // @llvm.memset defines 0 and 1 to both mean no alignment.
6587 Align DstAlign = MSII.getDestAlign().valueOrOne();
6588 bool isVol = MSII.isVolatile();
6589 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6590 SDValue MC = DAG.getMemset(
6591 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6592 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6593 updateDAGForMaybeTailCall(MC);
6594 return;
6595 }
6596 case Intrinsic::memmove: {
6597 const auto &MMI = cast<MemMoveInst>(I);
6598 SDValue Op1 = getValue(I.getArgOperand(0));
6599 SDValue Op2 = getValue(I.getArgOperand(1));
6600 SDValue Op3 = getValue(I.getArgOperand(2));
6601 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6602 Align DstAlign = MMI.getDestAlign().valueOrOne();
6603 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6604 Align Alignment = std::min(DstAlign, SrcAlign);
6605 bool isVol = MMI.isVolatile();
6606 // FIXME: Support passing different dest/src alignments to the memmove DAG
6607 // node.
6608 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6609 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, &I,
6610 /* OverrideTailCall */ std::nullopt,
6611 MachinePointerInfo(I.getArgOperand(0)),
6612 MachinePointerInfo(I.getArgOperand(1)),
6613 I.getAAMetadata(), BatchAA);
6614 updateDAGForMaybeTailCall(MM);
6615 return;
6616 }
6617 case Intrinsic::memcpy_element_unordered_atomic: {
6618 auto &MI = cast<AnyMemCpyInst>(I);
6619 SDValue Dst = getValue(MI.getRawDest());
6620 SDValue Src = getValue(MI.getRawSource());
6621 SDValue Length = getValue(MI.getLength());
6622
6623 Type *LengthTy = MI.getLength()->getType();
6624 unsigned ElemSz = MI.getElementSizeInBytes();
6625 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6626 SDValue MC =
6627 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6628 isTC, MachinePointerInfo(MI.getRawDest()),
6629 MachinePointerInfo(MI.getRawSource()));
6630 updateDAGForMaybeTailCall(MC);
6631 return;
6632 }
6633 case Intrinsic::memmove_element_unordered_atomic: {
6634 auto &MI = cast<AnyMemMoveInst>(I);
6635 SDValue Dst = getValue(MI.getRawDest());
6636 SDValue Src = getValue(MI.getRawSource());
6637 SDValue Length = getValue(MI.getLength());
6638
6639 Type *LengthTy = MI.getLength()->getType();
6640 unsigned ElemSz = MI.getElementSizeInBytes();
6641 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6642 SDValue MC =
6643 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6644 isTC, MachinePointerInfo(MI.getRawDest()),
6645 MachinePointerInfo(MI.getRawSource()));
6646 updateDAGForMaybeTailCall(MC);
6647 return;
6648 }
6649 case Intrinsic::memset_element_unordered_atomic: {
6650 auto &MI = cast<AnyMemSetInst>(I);
6651 SDValue Dst = getValue(MI.getRawDest());
6652 SDValue Val = getValue(MI.getValue());
6653 SDValue Length = getValue(MI.getLength());
6654
6655 Type *LengthTy = MI.getLength()->getType();
6656 unsigned ElemSz = MI.getElementSizeInBytes();
6657 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6658 SDValue MC =
6659 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6660 isTC, MachinePointerInfo(MI.getRawDest()));
6661 updateDAGForMaybeTailCall(MC);
6662 return;
6663 }
6664 case Intrinsic::call_preallocated_setup: {
6665 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6666 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6667 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6668 getRoot(), SrcValue);
6669 setValue(&I, Res);
6670 DAG.setRoot(Res);
6671 return;
6672 }
6673 case Intrinsic::call_preallocated_arg: {
6674 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6675 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6676 SDValue Ops[3];
6677 Ops[0] = getRoot();
6678 Ops[1] = SrcValue;
6679 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6680 MVT::i32); // arg index
6681 SDValue Res = DAG.getNode(
6682 ISD::PREALLOCATED_ARG, sdl,
6683 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6684 setValue(&I, Res);
6685 DAG.setRoot(Res.getValue(1));
6686 return;
6687 }
6688
6689 case Intrinsic::eh_typeid_for: {
6690 // Find the type id for the given typeinfo.
6691 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6692 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6693 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6694 setValue(&I, Res);
6695 return;
6696 }
6697
6698 case Intrinsic::eh_return_i32:
6699 case Intrinsic::eh_return_i64:
6700 DAG.getMachineFunction().setCallsEHReturn(true);
6701 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6702 MVT::Other,
6704 getValue(I.getArgOperand(0)),
6705 getValue(I.getArgOperand(1))));
6706 return;
6707 case Intrinsic::eh_unwind_init:
6708 DAG.getMachineFunction().setCallsUnwindInit(true);
6709 return;
6710 case Intrinsic::eh_dwarf_cfa:
6711 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6712 TLI.getPointerTy(DAG.getDataLayout()),
6713 getValue(I.getArgOperand(0))));
6714 return;
6715 case Intrinsic::eh_sjlj_callsite: {
6716 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6717 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6718
6719 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6720 return;
6721 }
6722 case Intrinsic::eh_sjlj_functioncontext: {
6723 // Get and store the index of the function context.
6724 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6725 AllocaInst *FnCtx =
6726 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6727 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6729 return;
6730 }
6731 case Intrinsic::eh_sjlj_setjmp: {
6732 SDValue Ops[2];
6733 Ops[0] = getRoot();
6734 Ops[1] = getValue(I.getArgOperand(0));
6735 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6736 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6737 setValue(&I, Op.getValue(0));
6738 DAG.setRoot(Op.getValue(1));
6739 return;
6740 }
6741 case Intrinsic::eh_sjlj_longjmp:
6742 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6743 getRoot(), getValue(I.getArgOperand(0))));
6744 return;
6745 case Intrinsic::eh_sjlj_setup_dispatch:
6746 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6747 getRoot()));
6748 return;
6749 case Intrinsic::masked_gather:
6750 visitMaskedGather(I);
6751 return;
6752 case Intrinsic::masked_load:
6753 visitMaskedLoad(I);
6754 return;
6755 case Intrinsic::masked_scatter:
6756 visitMaskedScatter(I);
6757 return;
6758 case Intrinsic::masked_store:
6759 visitMaskedStore(I);
6760 return;
6761 case Intrinsic::masked_expandload:
6762 visitMaskedLoad(I, true /* IsExpanding */);
6763 return;
6764 case Intrinsic::masked_compressstore:
6765 visitMaskedStore(I, true /* IsCompressing */);
6766 return;
6767 case Intrinsic::powi:
6768 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6769 getValue(I.getArgOperand(1)), DAG));
6770 return;
6771 case Intrinsic::log:
6772 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6773 return;
6774 case Intrinsic::log2:
6775 setValue(&I,
6776 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6777 return;
6778 case Intrinsic::log10:
6779 setValue(&I,
6780 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6781 return;
6782 case Intrinsic::exp:
6783 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6784 return;
6785 case Intrinsic::exp2:
6786 setValue(&I,
6787 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6788 return;
6789 case Intrinsic::pow:
6790 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6791 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6792 return;
6793 case Intrinsic::sqrt:
6794 case Intrinsic::fabs:
6795 case Intrinsic::sin:
6796 case Intrinsic::cos:
6797 case Intrinsic::tan:
6798 case Intrinsic::asin:
6799 case Intrinsic::acos:
6800 case Intrinsic::atan:
6801 case Intrinsic::sinh:
6802 case Intrinsic::cosh:
6803 case Intrinsic::tanh:
6804 case Intrinsic::exp10:
6805 case Intrinsic::floor:
6806 case Intrinsic::ceil:
6807 case Intrinsic::trunc:
6808 case Intrinsic::rint:
6809 case Intrinsic::nearbyint:
6810 case Intrinsic::round:
6811 case Intrinsic::roundeven:
6812 case Intrinsic::canonicalize: {
6813 unsigned Opcode;
6814 // clang-format off
6815 switch (Intrinsic) {
6816 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6817 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6818 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6819 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6820 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6821 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6822 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6823 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6824 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6825 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6826 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6827 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6828 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6829 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6830 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6831 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6832 case Intrinsic::rint: Opcode = ISD::FRINT; break;
6833 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6834 case Intrinsic::round: Opcode = ISD::FROUND; break;
6835 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6836 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6837 }
6838 // clang-format on
6839
6840 setValue(&I, DAG.getNode(Opcode, sdl,
6841 getValue(I.getArgOperand(0)).getValueType(),
6842 getValue(I.getArgOperand(0)), Flags));
6843 return;
6844 }
6845 case Intrinsic::atan2:
6846 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
6847 getValue(I.getArgOperand(0)).getValueType(),
6848 getValue(I.getArgOperand(0)),
6849 getValue(I.getArgOperand(1)), Flags));
6850 return;
6851 case Intrinsic::lround:
6852 case Intrinsic::llround:
6853 case Intrinsic::lrint:
6854 case Intrinsic::llrint: {
6855 unsigned Opcode;
6856 // clang-format off
6857 switch (Intrinsic) {
6858 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6859 case Intrinsic::lround: Opcode = ISD::LROUND; break;
6860 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6861 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
6862 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
6863 }
6864 // clang-format on
6865
6866 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6867 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6868 getValue(I.getArgOperand(0))));
6869 return;
6870 }
6871 case Intrinsic::minnum:
6872 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6873 getValue(I.getArgOperand(0)).getValueType(),
6874 getValue(I.getArgOperand(0)),
6875 getValue(I.getArgOperand(1)), Flags));
6876 return;
6877 case Intrinsic::maxnum:
6878 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6879 getValue(I.getArgOperand(0)).getValueType(),
6880 getValue(I.getArgOperand(0)),
6881 getValue(I.getArgOperand(1)), Flags));
6882 return;
6883 case Intrinsic::minimum:
6884 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
6885 getValue(I.getArgOperand(0)).getValueType(),
6886 getValue(I.getArgOperand(0)),
6887 getValue(I.getArgOperand(1)), Flags));
6888 return;
6889 case Intrinsic::maximum:
6890 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
6891 getValue(I.getArgOperand(0)).getValueType(),
6892 getValue(I.getArgOperand(0)),
6893 getValue(I.getArgOperand(1)), Flags));
6894 return;
6895 case Intrinsic::minimumnum:
6896 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
6897 getValue(I.getArgOperand(0)).getValueType(),
6898 getValue(I.getArgOperand(0)),
6899 getValue(I.getArgOperand(1)), Flags));
6900 return;
6901 case Intrinsic::maximumnum:
6902 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
6903 getValue(I.getArgOperand(0)).getValueType(),
6904 getValue(I.getArgOperand(0)),
6905 getValue(I.getArgOperand(1)), Flags));
6906 return;
6907 case Intrinsic::copysign:
6908 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
6909 getValue(I.getArgOperand(0)).getValueType(),
6910 getValue(I.getArgOperand(0)),
6911 getValue(I.getArgOperand(1)), Flags));
6912 return;
6913 case Intrinsic::ldexp:
6914 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
6915 getValue(I.getArgOperand(0)).getValueType(),
6916 getValue(I.getArgOperand(0)),
6917 getValue(I.getArgOperand(1)), Flags));
6918 return;
6919 case Intrinsic::modf:
6920 case Intrinsic::sincos:
6921 case Intrinsic::sincospi:
6922 case Intrinsic::frexp: {
6923 unsigned Opcode;
6924 switch (Intrinsic) {
6925 default:
6926 llvm_unreachable("unexpected intrinsic");
6927 case Intrinsic::sincos:
6928 Opcode = ISD::FSINCOS;
6929 break;
6930 case Intrinsic::sincospi:
6931 Opcode = ISD::FSINCOSPI;
6932 break;
6933 case Intrinsic::modf:
6934 Opcode = ISD::FMODF;
6935 break;
6936 case Intrinsic::frexp:
6937 Opcode = ISD::FFREXP;
6938 break;
6939 }
6940 SmallVector<EVT, 2> ValueVTs;
6941 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
6942 SDVTList VTs = DAG.getVTList(ValueVTs);
6943 setValue(
6944 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
6945 return;
6946 }
6947 case Intrinsic::arithmetic_fence: {
6948 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
6949 getValue(I.getArgOperand(0)).getValueType(),
6950 getValue(I.getArgOperand(0)), Flags));
6951 return;
6952 }
6953 case Intrinsic::fma:
6954 setValue(&I, DAG.getNode(
6955 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
6956 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
6957 getValue(I.getArgOperand(2)), Flags));
6958 return;
6959#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
6960 case Intrinsic::INTRINSIC:
6961#include "llvm/IR/ConstrainedOps.def"
6962 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
6963 return;
6964#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
6965#include "llvm/IR/VPIntrinsics.def"
6966 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
6967 return;
6968 case Intrinsic::fptrunc_round: {
6969 // Get the last argument, the metadata and convert it to an integer in the
6970 // call
6971 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
6972 std::optional<RoundingMode> RoundMode =
6973 convertStrToRoundingMode(cast<MDString>(MD)->getString());
6974
6975 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6976
6977 // Propagate fast-math-flags from IR to node(s).
6978 SDNodeFlags Flags;
6979 Flags.copyFMF(*cast<FPMathOperator>(&I));
6980 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
6981
6983 Result = DAG.getNode(
6984 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
6985 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
6986 setValue(&I, Result);
6987
6988 return;
6989 }
6990 case Intrinsic::fmuladd: {
6991 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6992 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
6993 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
6994 setValue(&I, DAG.getNode(ISD::FMA, sdl,
6995 getValue(I.getArgOperand(0)).getValueType(),
6996 getValue(I.getArgOperand(0)),
6997 getValue(I.getArgOperand(1)),
6998 getValue(I.getArgOperand(2)), Flags));
6999 } else {
7000 // TODO: Intrinsic calls should have fast-math-flags.
7001 SDValue Mul = DAG.getNode(
7002 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7003 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7004 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7005 getValue(I.getArgOperand(0)).getValueType(),
7006 Mul, getValue(I.getArgOperand(2)), Flags);
7007 setValue(&I, Add);
7008 }
7009 return;
7010 }
7011 case Intrinsic::convert_to_fp16:
7012 setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16,
7013 DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16,
7014 getValue(I.getArgOperand(0)),
7015 DAG.getTargetConstant(0, sdl,
7016 MVT::i32))));
7017 return;
7018 case Intrinsic::convert_from_fp16:
7019 setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl,
7020 TLI.getValueType(DAG.getDataLayout(), I.getType()),
7021 DAG.getNode(ISD::BITCAST, sdl, MVT::f16,
7022 getValue(I.getArgOperand(0)))));
7023 return;
7024 case Intrinsic::fptosi_sat: {
7025 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7026 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7027 getValue(I.getArgOperand(0)),
7028 DAG.getValueType(VT.getScalarType())));
7029 return;
7030 }
7031 case Intrinsic::fptoui_sat: {
7032 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7033 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7034 getValue(I.getArgOperand(0)),
7035 DAG.getValueType(VT.getScalarType())));
7036 return;
7037 }
7038 case Intrinsic::set_rounding:
7039 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7040 {getRoot(), getValue(I.getArgOperand(0))});
7041 setValue(&I, Res);
7042 DAG.setRoot(Res.getValue(0));
7043 return;
7044 case Intrinsic::is_fpclass: {
7045 const DataLayout DLayout = DAG.getDataLayout();
7046 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7047 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7048 FPClassTest Test = static_cast<FPClassTest>(
7049 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7050 MachineFunction &MF = DAG.getMachineFunction();
7051 const Function &F = MF.getFunction();
7052 SDValue Op = getValue(I.getArgOperand(0));
7053 SDNodeFlags Flags;
7054 Flags.setNoFPExcept(
7055 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7056 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7057 // expansion can use illegal types. Making expansion early allows
7058 // legalizing these types prior to selection.
7059 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7060 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7061 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7062 setValue(&I, Result);
7063 return;
7064 }
7065
7066 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7067 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7068 setValue(&I, V);
7069 return;
7070 }
7071 case Intrinsic::get_fpenv: {
7072 const DataLayout DLayout = DAG.getDataLayout();
7073 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7074 Align TempAlign = DAG.getEVTAlign(EnvVT);
7075 SDValue Chain = getRoot();
7076 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7077 // and temporary storage in stack.
7078 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7079 Res = DAG.getNode(
7080 ISD::GET_FPENV, sdl,
7081 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7082 MVT::Other),
7083 Chain);
7084 } else {
7085 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7086 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7087 auto MPI =
7088 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7089 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7091 TempAlign);
7092 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7093 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7094 }
7095 setValue(&I, Res);
7096 DAG.setRoot(Res.getValue(1));
7097 return;
7098 }
7099 case Intrinsic::set_fpenv: {
7100 const DataLayout DLayout = DAG.getDataLayout();
7101 SDValue Env = getValue(I.getArgOperand(0));
7102 EVT EnvVT = Env.getValueType();
7103 Align TempAlign = DAG.getEVTAlign(EnvVT);
7104 SDValue Chain = getRoot();
7105 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7106 // environment from memory.
7107 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7108 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7109 } else {
7110 // Allocate space in stack, copy environment bits into it and use this
7111 // memory in SET_FPENV_MEM.
7112 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7113 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7114 auto MPI =
7115 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7116 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7118 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7120 TempAlign);
7121 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7122 }
7123 DAG.setRoot(Chain);
7124 return;
7125 }
7126 case Intrinsic::reset_fpenv:
7127 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7128 return;
7129 case Intrinsic::get_fpmode:
7130 Res = DAG.getNode(
7131 ISD::GET_FPMODE, sdl,
7132 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7133 MVT::Other),
7134 DAG.getRoot());
7135 setValue(&I, Res);
7136 DAG.setRoot(Res.getValue(1));
7137 return;
7138 case Intrinsic::set_fpmode:
7139 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7140 getValue(I.getArgOperand(0)));
7141 DAG.setRoot(Res);
7142 return;
7143 case Intrinsic::reset_fpmode: {
7144 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7145 DAG.setRoot(Res);
7146 return;
7147 }
7148 case Intrinsic::pcmarker: {
7149 SDValue Tmp = getValue(I.getArgOperand(0));
7150 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7151 return;
7152 }
7153 case Intrinsic::readcyclecounter: {
7154 SDValue Op = getRoot();
7155 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7156 DAG.getVTList(MVT::i64, MVT::Other), Op);
7157 setValue(&I, Res);
7158 DAG.setRoot(Res.getValue(1));
7159 return;
7160 }
7161 case Intrinsic::readsteadycounter: {
7162 SDValue Op = getRoot();
7163 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7164 DAG.getVTList(MVT::i64, MVT::Other), Op);
7165 setValue(&I, Res);
7166 DAG.setRoot(Res.getValue(1));
7167 return;
7168 }
7169 case Intrinsic::bitreverse:
7170 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7171 getValue(I.getArgOperand(0)).getValueType(),
7172 getValue(I.getArgOperand(0))));
7173 return;
7174 case Intrinsic::bswap:
7175 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7176 getValue(I.getArgOperand(0)).getValueType(),
7177 getValue(I.getArgOperand(0))));
7178 return;
7179 case Intrinsic::cttz: {
7180 SDValue Arg = getValue(I.getArgOperand(0));
7181 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7182 EVT Ty = Arg.getValueType();
7183 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
7184 sdl, Ty, Arg));
7185 return;
7186 }
7187 case Intrinsic::ctlz: {
7188 SDValue Arg = getValue(I.getArgOperand(0));
7189 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7190 EVT Ty = Arg.getValueType();
7191 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
7192 sdl, Ty, Arg));
7193 return;
7194 }
7195 case Intrinsic::ctpop: {
7196 SDValue Arg = getValue(I.getArgOperand(0));
7197 EVT Ty = Arg.getValueType();
7198 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7199 return;
7200 }
7201 case Intrinsic::fshl:
7202 case Intrinsic::fshr: {
7203 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7204 SDValue X = getValue(I.getArgOperand(0));
7205 SDValue Y = getValue(I.getArgOperand(1));
7206 SDValue Z = getValue(I.getArgOperand(2));
7207 EVT VT = X.getValueType();
7208
7209 if (X == Y) {
7210 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7211 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7212 } else {
7213 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7214 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7215 }
7216 return;
7217 }
7218 case Intrinsic::sadd_sat: {
7219 SDValue Op1 = getValue(I.getArgOperand(0));
7220 SDValue Op2 = getValue(I.getArgOperand(1));
7221 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7222 return;
7223 }
7224 case Intrinsic::uadd_sat: {
7225 SDValue Op1 = getValue(I.getArgOperand(0));
7226 SDValue Op2 = getValue(I.getArgOperand(1));
7227 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7228 return;
7229 }
7230 case Intrinsic::ssub_sat: {
7231 SDValue Op1 = getValue(I.getArgOperand(0));
7232 SDValue Op2 = getValue(I.getArgOperand(1));
7233 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7234 return;
7235 }
7236 case Intrinsic::usub_sat: {
7237 SDValue Op1 = getValue(I.getArgOperand(0));
7238 SDValue Op2 = getValue(I.getArgOperand(1));
7239 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7240 return;
7241 }
7242 case Intrinsic::sshl_sat: {
7243 SDValue Op1 = getValue(I.getArgOperand(0));
7244 SDValue Op2 = getValue(I.getArgOperand(1));
7245 setValue(&I, DAG.getNode(ISD::SSHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7246 return;
7247 }
7248 case Intrinsic::ushl_sat: {
7249 SDValue Op1 = getValue(I.getArgOperand(0));
7250 SDValue Op2 = getValue(I.getArgOperand(1));
7251 setValue(&I, DAG.getNode(ISD::USHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7252 return;
7253 }
7254 case Intrinsic::smul_fix:
7255 case Intrinsic::umul_fix:
7256 case Intrinsic::smul_fix_sat:
7257 case Intrinsic::umul_fix_sat: {
7258 SDValue Op1 = getValue(I.getArgOperand(0));
7259 SDValue Op2 = getValue(I.getArgOperand(1));
7260 SDValue Op3 = getValue(I.getArgOperand(2));
7261 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7262 Op1.getValueType(), Op1, Op2, Op3));
7263 return;
7264 }
7265 case Intrinsic::sdiv_fix:
7266 case Intrinsic::udiv_fix:
7267 case Intrinsic::sdiv_fix_sat:
7268 case Intrinsic::udiv_fix_sat: {
7269 SDValue Op1 = getValue(I.getArgOperand(0));
7270 SDValue Op2 = getValue(I.getArgOperand(1));
7271 SDValue Op3 = getValue(I.getArgOperand(2));
7273 Op1, Op2, Op3, DAG, TLI));
7274 return;
7275 }
7276 case Intrinsic::smax: {
7277 SDValue Op1 = getValue(I.getArgOperand(0));
7278 SDValue Op2 = getValue(I.getArgOperand(1));
7279 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7280 return;
7281 }
7282 case Intrinsic::smin: {
7283 SDValue Op1 = getValue(I.getArgOperand(0));
7284 SDValue Op2 = getValue(I.getArgOperand(1));
7285 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7286 return;
7287 }
7288 case Intrinsic::umax: {
7289 SDValue Op1 = getValue(I.getArgOperand(0));
7290 SDValue Op2 = getValue(I.getArgOperand(1));
7291 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7292 return;
7293 }
7294 case Intrinsic::umin: {
7295 SDValue Op1 = getValue(I.getArgOperand(0));
7296 SDValue Op2 = getValue(I.getArgOperand(1));
7297 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7298 return;
7299 }
7300 case Intrinsic::abs: {
7301 // TODO: Preserve "int min is poison" arg in SDAG?
7302 SDValue Op1 = getValue(I.getArgOperand(0));
7303 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7304 return;
7305 }
7306 case Intrinsic::scmp: {
7307 SDValue Op1 = getValue(I.getArgOperand(0));
7308 SDValue Op2 = getValue(I.getArgOperand(1));
7309 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7310 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7311 break;
7312 }
7313 case Intrinsic::ucmp: {
7314 SDValue Op1 = getValue(I.getArgOperand(0));
7315 SDValue Op2 = getValue(I.getArgOperand(1));
7316 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7317 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7318 break;
7319 }
7320 case Intrinsic::stacksave: {
7321 SDValue Op = getRoot();
7322 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7323 Res = DAG.getNode(ISD::STACKSAVE, sdl, DAG.getVTList(VT, MVT::Other), Op);
7324 setValue(&I, Res);
7325 DAG.setRoot(Res.getValue(1));
7326 return;
7327 }
7328 case Intrinsic::stackrestore:
7329 Res = getValue(I.getArgOperand(0));
7330 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7331 return;
7332 case Intrinsic::get_dynamic_area_offset: {
7333 SDValue Op = getRoot();
7334 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7335 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7336 Op);
7337 DAG.setRoot(Op);
7338 setValue(&I, Res);
7339 return;
7340 }
7341 case Intrinsic::stackguard: {
7342 MachineFunction &MF = DAG.getMachineFunction();
7343 const Module &M = *MF.getFunction().getParent();
7344 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7345 SDValue Chain = getRoot();
7346 if (TLI.useLoadStackGuardNode(M)) {
7347 Res = getLoadStackGuard(DAG, sdl, Chain);
7348 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7349 } else {
7350 const Value *Global = TLI.getSDagStackGuard(M);
7351 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7352 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7353 MachinePointerInfo(Global, 0), Align,
7355 }
7356 if (TLI.useStackGuardXorFP())
7357 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7358 DAG.setRoot(Chain);
7359 setValue(&I, Res);
7360 return;
7361 }
7362 case Intrinsic::stackprotector: {
7363 // Emit code into the DAG to store the stack guard onto the stack.
7364 MachineFunction &MF = DAG.getMachineFunction();
7365 MachineFrameInfo &MFI = MF.getFrameInfo();
7366 const Module &M = *MF.getFunction().getParent();
7367 SDValue Src, Chain = getRoot();
7368
7369 if (TLI.useLoadStackGuardNode(M))
7370 Src = getLoadStackGuard(DAG, sdl, Chain);
7371 else
7372 Src = getValue(I.getArgOperand(0)); // The guard's value.
7373
7374 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7375
7376 int FI = FuncInfo.StaticAllocaMap[Slot];
7377 MFI.setStackProtectorIndex(FI);
7378 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7379
7380 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7381
7382 // Store the stack protector onto the stack.
7383 Res = DAG.getStore(
7384 Chain, sdl, Src, FIN,
7385 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7386 MaybeAlign(), MachineMemOperand::MOVolatile);
7387 setValue(&I, Res);
7388 DAG.setRoot(Res);
7389 return;
7390 }
7391 case Intrinsic::objectsize:
7392 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7393
7394 case Intrinsic::is_constant:
7395 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7396
7397 case Intrinsic::annotation:
7398 case Intrinsic::ptr_annotation:
7399 case Intrinsic::launder_invariant_group:
7400 case Intrinsic::strip_invariant_group:
7401 // Drop the intrinsic, but forward the value
7402 setValue(&I, getValue(I.getOperand(0)));
7403 return;
7404
7405 case Intrinsic::type_test:
7406 case Intrinsic::public_type_test:
7407 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7408 return;
7409
7410 case Intrinsic::assume:
7411 case Intrinsic::experimental_noalias_scope_decl:
7412 case Intrinsic::var_annotation:
7413 case Intrinsic::sideeffect:
7414 // Discard annotate attributes, noalias scope declarations, assumptions, and
7415 // artificial side-effects.
7416 return;
7417
7418 case Intrinsic::codeview_annotation: {
7419 // Emit a label associated with this metadata.
7420 MachineFunction &MF = DAG.getMachineFunction();
7421 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7422 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7423 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7424 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7425 DAG.setRoot(Res);
7426 return;
7427 }
7428
7429 case Intrinsic::init_trampoline: {
7430 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7431
7432 SDValue Ops[6];
7433 Ops[0] = getRoot();
7434 Ops[1] = getValue(I.getArgOperand(0));
7435 Ops[2] = getValue(I.getArgOperand(1));
7436 Ops[3] = getValue(I.getArgOperand(2));
7437 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7438 Ops[5] = DAG.getSrcValue(F);
7439
7440 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7441
7442 DAG.setRoot(Res);
7443 return;
7444 }
7445 case Intrinsic::adjust_trampoline:
7446 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7447 TLI.getPointerTy(DAG.getDataLayout()),
7448 getValue(I.getArgOperand(0))));
7449 return;
7450 case Intrinsic::gcroot: {
7451 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7452 "only valid in functions with gc specified, enforced by Verifier");
7453 assert(GFI && "implied by previous");
7454 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7455 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7456
7457 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7458 GFI->addStackRoot(FI->getIndex(), TypeMap);
7459 return;
7460 }
7461 case Intrinsic::gcread:
7462 case Intrinsic::gcwrite:
7463 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7464 case Intrinsic::get_rounding:
7465 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7466 setValue(&I, Res);
7467 DAG.setRoot(Res.getValue(1));
7468 return;
7469
7470 case Intrinsic::expect:
7471 case Intrinsic::expect_with_probability:
7472 // Just replace __builtin_expect(exp, c) and
7473 // __builtin_expect_with_probability(exp, c, p) with EXP.
7474 setValue(&I, getValue(I.getArgOperand(0)));
7475 return;
7476
7477 case Intrinsic::ubsantrap:
7478 case Intrinsic::debugtrap:
7479 case Intrinsic::trap: {
7480 StringRef TrapFuncName =
7481 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7482 if (TrapFuncName.empty()) {
7483 switch (Intrinsic) {
7484 case Intrinsic::trap:
7485 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7486 break;
7487 case Intrinsic::debugtrap:
7488 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7489 break;
7490 case Intrinsic::ubsantrap:
7491 DAG.setRoot(DAG.getNode(
7492 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7493 DAG.getTargetConstant(
7494 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7495 MVT::i32)));
7496 break;
7497 default: llvm_unreachable("unknown trap intrinsic");
7498 }
7499 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7500 I.hasFnAttr(Attribute::NoMerge));
7501 return;
7502 }
7504 if (Intrinsic == Intrinsic::ubsantrap) {
7505 Value *Arg = I.getArgOperand(0);
7506 Args.emplace_back(Arg, getValue(Arg));
7507 }
7508
7509 TargetLowering::CallLoweringInfo CLI(DAG);
7510 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7511 CallingConv::C, I.getType(),
7512 DAG.getExternalSymbol(TrapFuncName.data(),
7513 TLI.getPointerTy(DAG.getDataLayout())),
7514 std::move(Args));
7515 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7516 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7517 DAG.setRoot(Result.second);
7518 return;
7519 }
7520
7521 case Intrinsic::allow_runtime_check:
7522 case Intrinsic::allow_ubsan_check:
7523 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7524 return;
7525
7526 case Intrinsic::uadd_with_overflow:
7527 case Intrinsic::sadd_with_overflow:
7528 case Intrinsic::usub_with_overflow:
7529 case Intrinsic::ssub_with_overflow:
7530 case Intrinsic::umul_with_overflow:
7531 case Intrinsic::smul_with_overflow: {
7533 switch (Intrinsic) {
7534 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7535 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7536 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7537 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7538 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7539 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7540 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7541 }
7542 SDValue Op1 = getValue(I.getArgOperand(0));
7543 SDValue Op2 = getValue(I.getArgOperand(1));
7544
7545 EVT ResultVT = Op1.getValueType();
7546 EVT OverflowVT = MVT::i1;
7547 if (ResultVT.isVector())
7548 OverflowVT = EVT::getVectorVT(
7549 *Context, OverflowVT, ResultVT.getVectorElementCount());
7550
7551 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7552 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7553 return;
7554 }
7555 case Intrinsic::prefetch: {
7556 SDValue Ops[5];
7557 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7559 Ops[0] = DAG.getRoot();
7560 Ops[1] = getValue(I.getArgOperand(0));
7561 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7562 MVT::i32);
7563 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7564 MVT::i32);
7565 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7566 MVT::i32);
7567 SDValue Result = DAG.getMemIntrinsicNode(
7568 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7569 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7570 /* align */ std::nullopt, Flags);
7571
7572 // Chain the prefetch in parallel with any pending loads, to stay out of
7573 // the way of later optimizations.
7574 PendingLoads.push_back(Result);
7575 Result = getRoot();
7576 DAG.setRoot(Result);
7577 return;
7578 }
7579 case Intrinsic::lifetime_start:
7580 case Intrinsic::lifetime_end: {
7581 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7582 // Stack coloring is not enabled in O0, discard region information.
7583 if (TM.getOptLevel() == CodeGenOptLevel::None)
7584 return;
7585
7586 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7587 if (!LifetimeObject)
7588 return;
7589
7590 // First check that the Alloca is static, otherwise it won't have a
7591 // valid frame index.
7592 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7593 if (SI == FuncInfo.StaticAllocaMap.end())
7594 return;
7595
7596 const int FrameIndex = SI->second;
7597 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7598 DAG.setRoot(Res);
7599 return;
7600 }
7601 case Intrinsic::pseudoprobe: {
7602 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7603 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7604 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7605 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7606 DAG.setRoot(Res);
7607 return;
7608 }
7609 case Intrinsic::invariant_start:
7610 // Discard region information.
7611 setValue(&I,
7612 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7613 return;
7614 case Intrinsic::invariant_end:
7615 // Discard region information.
7616 return;
7617 case Intrinsic::clear_cache: {
7618 SDValue InputChain = DAG.getRoot();
7619 SDValue StartVal = getValue(I.getArgOperand(0));
7620 SDValue EndVal = getValue(I.getArgOperand(1));
7621 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7622 {InputChain, StartVal, EndVal});
7623 setValue(&I, Res);
7624 DAG.setRoot(Res);
7625 return;
7626 }
7627 case Intrinsic::donothing:
7628 case Intrinsic::seh_try_begin:
7629 case Intrinsic::seh_scope_begin:
7630 case Intrinsic::seh_try_end:
7631 case Intrinsic::seh_scope_end:
7632 // ignore
7633 return;
7634 case Intrinsic::experimental_stackmap:
7635 visitStackmap(I);
7636 return;
7637 case Intrinsic::experimental_patchpoint_void:
7638 case Intrinsic::experimental_patchpoint:
7639 visitPatchpoint(I);
7640 return;
7641 case Intrinsic::experimental_gc_statepoint:
7643 return;
7644 case Intrinsic::experimental_gc_result:
7645 visitGCResult(cast<GCResultInst>(I));
7646 return;
7647 case Intrinsic::experimental_gc_relocate:
7648 visitGCRelocate(cast<GCRelocateInst>(I));
7649 return;
7650 case Intrinsic::instrprof_cover:
7651 llvm_unreachable("instrprof failed to lower a cover");
7652 case Intrinsic::instrprof_increment:
7653 llvm_unreachable("instrprof failed to lower an increment");
7654 case Intrinsic::instrprof_timestamp:
7655 llvm_unreachable("instrprof failed to lower a timestamp");
7656 case Intrinsic::instrprof_value_profile:
7657 llvm_unreachable("instrprof failed to lower a value profiling call");
7658 case Intrinsic::instrprof_mcdc_parameters:
7659 llvm_unreachable("instrprof failed to lower mcdc parameters");
7660 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7661 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7662 case Intrinsic::localescape: {
7663 MachineFunction &MF = DAG.getMachineFunction();
7664 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7665
7666 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7667 // is the same on all targets.
7668 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7669 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7670 if (isa<ConstantPointerNull>(Arg))
7671 continue; // Skip null pointers. They represent a hole in index space.
7672 AllocaInst *Slot = cast<AllocaInst>(Arg);
7673 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7674 "can only escape static allocas");
7675 int FI = FuncInfo.StaticAllocaMap[Slot];
7676 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7678 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7679 TII->get(TargetOpcode::LOCAL_ESCAPE))
7680 .addSym(FrameAllocSym)
7681 .addFrameIndex(FI);
7682 }
7683
7684 return;
7685 }
7686
7687 case Intrinsic::localrecover: {
7688 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7689 MachineFunction &MF = DAG.getMachineFunction();
7690
7691 // Get the symbol that defines the frame offset.
7692 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7693 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7694 unsigned IdxVal =
7695 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7696 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7698
7699 Value *FP = I.getArgOperand(1);
7700 SDValue FPVal = getValue(FP);
7701 EVT PtrVT = FPVal.getValueType();
7702
7703 // Create a MCSymbol for the label to avoid any target lowering
7704 // that would make this PC relative.
7705 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7706 SDValue OffsetVal =
7707 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7708
7709 // Add the offset to the FP.
7710 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7711 setValue(&I, Add);
7712
7713 return;
7714 }
7715
7716 case Intrinsic::fake_use: {
7717 Value *V = I.getArgOperand(0);
7718 SDValue Ops[2];
7719 // For Values not declared or previously used in this basic block, the
7720 // NodeMap will not have an entry, and `getValue` will assert if V has no
7721 // valid register value.
7722 auto FakeUseValue = [&]() -> SDValue {
7723 SDValue &N = NodeMap[V];
7724 if (N.getNode())
7725 return N;
7726
7727 // If there's a virtual register allocated and initialized for this
7728 // value, use it.
7729 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7730 return copyFromReg;
7731 // FIXME: Do we want to preserve constants? It seems pointless.
7732 if (isa<Constant>(V))
7733 return getValue(V);
7734 return SDValue();
7735 }();
7736 if (!FakeUseValue || FakeUseValue.isUndef())
7737 return;
7738 Ops[0] = getRoot();
7739 Ops[1] = FakeUseValue;
7740 // Also, do not translate a fake use with an undef operand, or any other
7741 // empty SDValues.
7742 if (!Ops[1] || Ops[1].isUndef())
7743 return;
7744 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7745 return;
7746 }
7747
7748 case Intrinsic::eh_exceptionpointer:
7749 case Intrinsic::eh_exceptioncode: {
7750 // Get the exception pointer vreg, copy from it, and resize it to fit.
7751 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7752 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7753 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7754 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7755 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7756 if (Intrinsic == Intrinsic::eh_exceptioncode)
7757 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7758 setValue(&I, N);
7759 return;
7760 }
7761 case Intrinsic::xray_customevent: {
7762 // Here we want to make sure that the intrinsic behaves as if it has a
7763 // specific calling convention.
7764 const auto &Triple = DAG.getTarget().getTargetTriple();
7765 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7766 return;
7767
7769
7770 // We want to say that we always want the arguments in registers.
7771 SDValue LogEntryVal = getValue(I.getArgOperand(0));
7772 SDValue StrSizeVal = getValue(I.getArgOperand(1));
7773 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7774 SDValue Chain = getRoot();
7775 Ops.push_back(LogEntryVal);
7776 Ops.push_back(StrSizeVal);
7777 Ops.push_back(Chain);
7778
7779 // We need to enforce the calling convention for the callsite, so that
7780 // argument ordering is enforced correctly, and that register allocation can
7781 // see that some registers may be assumed clobbered and have to preserve
7782 // them across calls to the intrinsic.
7783 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7784 sdl, NodeTys, Ops);
7785 SDValue patchableNode = SDValue(MN, 0);
7786 DAG.setRoot(patchableNode);
7787 setValue(&I, patchableNode);
7788 return;
7789 }
7790 case Intrinsic::xray_typedevent: {
7791 // Here we want to make sure that the intrinsic behaves as if it has a
7792 // specific calling convention.
7793 const auto &Triple = DAG.getTarget().getTargetTriple();
7794 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7795 return;
7796
7798
7799 // We want to say that we always want the arguments in registers.
7800 // It's unclear to me how manipulating the selection DAG here forces callers
7801 // to provide arguments in registers instead of on the stack.
7802 SDValue LogTypeId = getValue(I.getArgOperand(0));
7803 SDValue LogEntryVal = getValue(I.getArgOperand(1));
7804 SDValue StrSizeVal = getValue(I.getArgOperand(2));
7805 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7806 SDValue Chain = getRoot();
7807 Ops.push_back(LogTypeId);
7808 Ops.push_back(LogEntryVal);
7809 Ops.push_back(StrSizeVal);
7810 Ops.push_back(Chain);
7811
7812 // We need to enforce the calling convention for the callsite, so that
7813 // argument ordering is enforced correctly, and that register allocation can
7814 // see that some registers may be assumed clobbered and have to preserve
7815 // them across calls to the intrinsic.
7816 MachineSDNode *MN = DAG.getMachineNode(
7817 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
7818 SDValue patchableNode = SDValue(MN, 0);
7819 DAG.setRoot(patchableNode);
7820 setValue(&I, patchableNode);
7821 return;
7822 }
7823 case Intrinsic::experimental_deoptimize:
7825 return;
7826 case Intrinsic::stepvector:
7827 visitStepVector(I);
7828 return;
7829 case Intrinsic::vector_reduce_fadd:
7830 case Intrinsic::vector_reduce_fmul:
7831 case Intrinsic::vector_reduce_add:
7832 case Intrinsic::vector_reduce_mul:
7833 case Intrinsic::vector_reduce_and:
7834 case Intrinsic::vector_reduce_or:
7835 case Intrinsic::vector_reduce_xor:
7836 case Intrinsic::vector_reduce_smax:
7837 case Intrinsic::vector_reduce_smin:
7838 case Intrinsic::vector_reduce_umax:
7839 case Intrinsic::vector_reduce_umin:
7840 case Intrinsic::vector_reduce_fmax:
7841 case Intrinsic::vector_reduce_fmin:
7842 case Intrinsic::vector_reduce_fmaximum:
7843 case Intrinsic::vector_reduce_fminimum:
7844 visitVectorReduce(I, Intrinsic);
7845 return;
7846
7847 case Intrinsic::icall_branch_funnel: {
7849 Ops.push_back(getValue(I.getArgOperand(0)));
7850
7851 int64_t Offset;
7853 I.getArgOperand(1), Offset, DAG.getDataLayout()));
7854 if (!Base)
7856 "llvm.icall.branch.funnel operand must be a GlobalValue");
7857 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
7858
7859 struct BranchFunnelTarget {
7860 int64_t Offset;
7862 };
7864
7865 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
7867 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
7868 if (ElemBase != Base)
7869 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
7870 "to the same GlobalValue");
7871
7872 SDValue Val = getValue(I.getArgOperand(Op + 1));
7873 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
7874 if (!GA)
7876 "llvm.icall.branch.funnel operand must be a GlobalValue");
7877 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
7878 GA->getGlobal(), sdl, Val.getValueType(),
7879 GA->getOffset())});
7880 }
7881 llvm::sort(Targets,
7882 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
7883 return T1.Offset < T2.Offset;
7884 });
7885
7886 for (auto &T : Targets) {
7887 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
7888 Ops.push_back(T.Target);
7889 }
7890
7891 Ops.push_back(DAG.getRoot()); // Chain
7892 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
7893 MVT::Other, Ops),
7894 0);
7895 DAG.setRoot(N);
7896 setValue(&I, N);
7897 HasTailCall = true;
7898 return;
7899 }
7900
7901 case Intrinsic::wasm_landingpad_index:
7902 // Information this intrinsic contained has been transferred to
7903 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
7904 // delete it now.
7905 return;
7906
7907 case Intrinsic::aarch64_settag:
7908 case Intrinsic::aarch64_settag_zero: {
7909 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
7910 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
7912 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
7913 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
7914 ZeroMemory);
7915 DAG.setRoot(Val);
7916 setValue(&I, Val);
7917 return;
7918 }
7919 case Intrinsic::amdgcn_cs_chain: {
7920 // At this point we don't care if it's amdgpu_cs_chain or
7921 // amdgpu_cs_chain_preserve.
7923
7924 Type *RetTy = I.getType();
7925 assert(RetTy->isVoidTy() && "Should not return");
7926
7927 SDValue Callee = getValue(I.getOperand(0));
7928
7929 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
7930 // We'll also tack the value of the EXEC mask at the end.
7932 Args.reserve(3);
7933
7934 for (unsigned Idx : {2, 3, 1}) {
7935 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
7936 I.getOperand(Idx)->getType());
7937 Arg.setAttributes(&I, Idx);
7938 Args.push_back(Arg);
7939 }
7940
7941 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
7942 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
7943 Args[2].IsInReg = true; // EXEC should be inreg
7944
7945 // Forward the flags and any additional arguments.
7946 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
7947 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
7948 I.getOperand(Idx)->getType());
7949 Arg.setAttributes(&I, Idx);
7950 Args.push_back(Arg);
7951 }
7952
7953 TargetLowering::CallLoweringInfo CLI(DAG);
7954 CLI.setDebugLoc(getCurSDLoc())
7955 .setChain(getRoot())
7956 .setCallee(CC, RetTy, Callee, std::move(Args))
7957 .setNoReturn(true)
7958 .setTailCall(true)
7959 .setConvergent(I.isConvergent());
7960 CLI.CB = &I;
7961 std::pair<SDValue, SDValue> Result =
7962 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
7963 (void)Result;
7964 assert(!Result.first.getNode() && !Result.second.getNode() &&
7965 "Should've lowered as tail call");
7966
7967 HasTailCall = true;
7968 return;
7969 }
7970 case Intrinsic::amdgcn_call_whole_wave: {
7972 bool isTailCall = I.isTailCall();
7973
7974 // The first argument is the callee. Skip it when assembling the call args.
7975 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
7976 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
7977 I.getArgOperand(Idx)->getType());
7978 Arg.setAttributes(&I, Idx);
7979
7980 // If we have an explicit sret argument that is an Instruction, (i.e., it
7981 // might point to function-local memory), we can't meaningfully tail-call.
7982 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
7983 isTailCall = false;
7984
7985 Args.push_back(Arg);
7986 }
7987
7988 SDValue ConvControlToken;
7989 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
7990 auto *Token = Bundle->Inputs[0].get();
7991 ConvControlToken = getValue(Token);
7992 }
7993
7994 TargetLowering::CallLoweringInfo CLI(DAG);
7995 CLI.setDebugLoc(getCurSDLoc())
7996 .setChain(getRoot())
7997 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
7998 getValue(I.getArgOperand(0)), std::move(Args))
7999 .setTailCall(isTailCall && canTailCall(I))
8000 .setIsPreallocated(
8001 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8002 .setConvergent(I.isConvergent())
8003 .setConvergenceControlToken(ConvControlToken);
8004 CLI.CB = &I;
8005
8006 std::pair<SDValue, SDValue> Result =
8007 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8008
8009 if (Result.first.getNode())
8010 setValue(&I, Result.first);
8011 return;
8012 }
8013 case Intrinsic::ptrmask: {
8014 SDValue Ptr = getValue(I.getOperand(0));
8015 SDValue Mask = getValue(I.getOperand(1));
8016
8017 // On arm64_32, pointers are 32 bits when stored in memory, but
8018 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8019 // match the index type, but the pointer is 64 bits, so the mask must be
8020 // zero-extended up to 64 bits to match the pointer.
8021 EVT PtrVT =
8022 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8023 EVT MemVT =
8024 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8025 assert(PtrVT == Ptr.getValueType());
8026 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8027 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8028 // 128-bit, so we have to pad the mask with ones for unused bits.
8029 auto HighOnes = DAG.getNode(
8030 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8031 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8032 PtrVT, sdl));
8033 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8034 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8035 } else if (Mask.getValueType() != PtrVT)
8036 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8037
8038 assert(Mask.getValueType() == PtrVT);
8039 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8040 return;
8041 }
8042 case Intrinsic::threadlocal_address: {
8043 setValue(&I, getValue(I.getOperand(0)));
8044 return;
8045 }
8046 case Intrinsic::get_active_lane_mask: {
8047 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8048 SDValue Index = getValue(I.getOperand(0));
8049 SDValue TripCount = getValue(I.getOperand(1));
8050 EVT ElementVT = Index.getValueType();
8051
8052 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8053 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8054 TripCount));
8055 return;
8056 }
8057
8058 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8059 CCVT.getVectorElementCount());
8060
8061 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8062 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8063 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8064 SDValue VectorInduction = DAG.getNode(
8065 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8066 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8067 VectorTripCount, ISD::CondCode::SETULT);
8068 setValue(&I, SetCC);
8069 return;
8070 }
8071 case Intrinsic::experimental_get_vector_length: {
8072 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8073 "Expected positive VF");
8074 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8075 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8076
8077 SDValue Count = getValue(I.getOperand(0));
8078 EVT CountVT = Count.getValueType();
8079
8080 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8081 visitTargetIntrinsic(I, Intrinsic);
8082 return;
8083 }
8084
8085 // Expand to a umin between the trip count and the maximum elements the type
8086 // can hold.
8087 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8088
8089 // Extend the trip count to at least the result VT.
8090 if (CountVT.bitsLT(VT)) {
8091 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8092 CountVT = VT;
8093 }
8094
8095 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8096 ElementCount::get(VF, IsScalable));
8097
8098 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8099 // Clip to the result type if needed.
8100 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8101
8102 setValue(&I, Trunc);
8103 return;
8104 }
8105 case Intrinsic::vector_partial_reduce_add: {
8107 visitTargetIntrinsic(I, Intrinsic);
8108 return;
8109 }
8110 SDValue Acc = getValue(I.getOperand(0));
8111 SDValue Input = getValue(I.getOperand(1));
8112 setValue(&I,
8113 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8114 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8115 return;
8116 }
8117 case Intrinsic::experimental_cttz_elts: {
8118 auto DL = getCurSDLoc();
8119 SDValue Op = getValue(I.getOperand(0));
8120 EVT OpVT = Op.getValueType();
8121
8122 if (!TLI.shouldExpandCttzElements(OpVT)) {
8123 visitTargetIntrinsic(I, Intrinsic);
8124 return;
8125 }
8126
8127 if (OpVT.getScalarType() != MVT::i1) {
8128 // Compare the input vector elements to zero & use to count trailing zeros
8129 SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8130 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8131 OpVT.getVectorElementCount());
8132 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8133 }
8134
8135 // If the zero-is-poison flag is set, we can assume the upper limit
8136 // of the result is VF-1.
8137 bool ZeroIsPoison =
8138 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8139 ConstantRange VScaleRange(1, true); // Dummy value.
8140 if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8141 VScaleRange = getVScaleRange(I.getCaller(), 64);
8142 unsigned EltWidth = TLI.getBitWidthForCttzElements(
8143 I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8144
8145 MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8146
8147 // Create the new vector type & get the vector length
8148 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8149 OpVT.getVectorElementCount());
8150
8151 SDValue VL =
8152 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8153
8154 SDValue StepVec = DAG.getStepVector(DL, NewVT);
8155 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8156 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8157 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8158 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8159 SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8160 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8161
8162 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8163 SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
8164
8165 setValue(&I, Ret);
8166 return;
8167 }
8168 case Intrinsic::vector_insert: {
8169 SDValue Vec = getValue(I.getOperand(0));
8170 SDValue SubVec = getValue(I.getOperand(1));
8171 SDValue Index = getValue(I.getOperand(2));
8172
8173 // The intrinsic's index type is i64, but the SDNode requires an index type
8174 // suitable for the target. Convert the index as required.
8175 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8176 if (Index.getValueType() != VectorIdxTy)
8177 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8178
8179 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8180 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8181 Index));
8182 return;
8183 }
8184 case Intrinsic::vector_extract: {
8185 SDValue Vec = getValue(I.getOperand(0));
8186 SDValue Index = getValue(I.getOperand(1));
8187 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8188
8189 // The intrinsic's index type is i64, but the SDNode requires an index type
8190 // suitable for the target. Convert the index as required.
8191 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8192 if (Index.getValueType() != VectorIdxTy)
8193 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8194
8195 setValue(&I,
8196 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8197 return;
8198 }
8199 case Intrinsic::experimental_vector_match: {
8200 SDValue Op1 = getValue(I.getOperand(0));
8201 SDValue Op2 = getValue(I.getOperand(1));
8202 SDValue Mask = getValue(I.getOperand(2));
8203 EVT Op1VT = Op1.getValueType();
8204 EVT Op2VT = Op2.getValueType();
8205 EVT ResVT = Mask.getValueType();
8206 unsigned SearchSize = Op2VT.getVectorNumElements();
8207
8208 // If the target has native support for this vector match operation, lower
8209 // the intrinsic untouched; otherwise, expand it below.
8210 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8211 visitTargetIntrinsic(I, Intrinsic);
8212 return;
8213 }
8214
8215 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8216
8217 for (unsigned i = 0; i < SearchSize; ++i) {
8218 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8219 Op2VT.getVectorElementType(), Op2,
8220 DAG.getVectorIdxConstant(i, sdl));
8221 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8222 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8223 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8224 }
8225
8226 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8227 return;
8228 }
8229 case Intrinsic::vector_reverse:
8230 visitVectorReverse(I);
8231 return;
8232 case Intrinsic::vector_splice:
8233 visitVectorSplice(I);
8234 return;
8235 case Intrinsic::callbr_landingpad:
8236 visitCallBrLandingPad(I);
8237 return;
8238 case Intrinsic::vector_interleave2:
8239 visitVectorInterleave(I, 2);
8240 return;
8241 case Intrinsic::vector_interleave3:
8242 visitVectorInterleave(I, 3);
8243 return;
8244 case Intrinsic::vector_interleave4:
8245 visitVectorInterleave(I, 4);
8246 return;
8247 case Intrinsic::vector_interleave5:
8248 visitVectorInterleave(I, 5);
8249 return;
8250 case Intrinsic::vector_interleave6:
8251 visitVectorInterleave(I, 6);
8252 return;
8253 case Intrinsic::vector_interleave7:
8254 visitVectorInterleave(I, 7);
8255 return;
8256 case Intrinsic::vector_interleave8:
8257 visitVectorInterleave(I, 8);
8258 return;
8259 case Intrinsic::vector_deinterleave2:
8260 visitVectorDeinterleave(I, 2);
8261 return;
8262 case Intrinsic::vector_deinterleave3:
8263 visitVectorDeinterleave(I, 3);
8264 return;
8265 case Intrinsic::vector_deinterleave4:
8266 visitVectorDeinterleave(I, 4);
8267 return;
8268 case Intrinsic::vector_deinterleave5:
8269 visitVectorDeinterleave(I, 5);
8270 return;
8271 case Intrinsic::vector_deinterleave6:
8272 visitVectorDeinterleave(I, 6);
8273 return;
8274 case Intrinsic::vector_deinterleave7:
8275 visitVectorDeinterleave(I, 7);
8276 return;
8277 case Intrinsic::vector_deinterleave8:
8278 visitVectorDeinterleave(I, 8);
8279 return;
8280 case Intrinsic::experimental_vector_compress:
8281 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8282 getValue(I.getArgOperand(0)).getValueType(),
8283 getValue(I.getArgOperand(0)),
8284 getValue(I.getArgOperand(1)),
8285 getValue(I.getArgOperand(2)), Flags));
8286 return;
8287 case Intrinsic::experimental_convergence_anchor:
8288 case Intrinsic::experimental_convergence_entry:
8289 case Intrinsic::experimental_convergence_loop:
8290 visitConvergenceControl(I, Intrinsic);
8291 return;
8292 case Intrinsic::experimental_vector_histogram_add: {
8293 visitVectorHistogram(I, Intrinsic);
8294 return;
8295 }
8296 case Intrinsic::experimental_vector_extract_last_active: {
8297 visitVectorExtractLastActive(I, Intrinsic);
8298 return;
8299 }
8300 case Intrinsic::loop_dependence_war_mask:
8301 setValue(&I,
8303 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8304 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8305 return;
8306 case Intrinsic::loop_dependence_raw_mask:
8307 setValue(&I,
8309 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8310 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8311 return;
8312 }
8313}
8314
8315void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8316 const ConstrainedFPIntrinsic &FPI) {
8317 SDLoc sdl = getCurSDLoc();
8318
8319 // We do not need to serialize constrained FP intrinsics against
8320 // each other or against (nonvolatile) loads, so they can be
8321 // chained like loads.
8322 SDValue Chain = DAG.getRoot();
8324 Opers.push_back(Chain);
8325 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8326 Opers.push_back(getValue(FPI.getArgOperand(I)));
8327
8328 auto pushOutChain = [this](SDValue Result, fp::ExceptionBehavior EB) {
8329 assert(Result.getNode()->getNumValues() == 2);
8330
8331 // Push node to the appropriate list so that future instructions can be
8332 // chained up correctly.
8333 SDValue OutChain = Result.getValue(1);
8334 switch (EB) {
8336 // The only reason why ebIgnore nodes still need to be chained is that
8337 // they might depend on the current rounding mode, and therefore must
8338 // not be moved across instruction that may change that mode.
8339 [[fallthrough]];
8341 // These must not be moved across calls or instructions that may change
8342 // floating-point exception masks.
8343 PendingConstrainedFP.push_back(OutChain);
8344 break;
8346 // These must not be moved across calls or instructions that may change
8347 // floating-point exception masks or read floating-point exception flags.
8348 // In addition, they cannot be optimized out even if unused.
8349 PendingConstrainedFPStrict.push_back(OutChain);
8350 break;
8351 }
8352 };
8353
8354 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8355 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8356 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8358
8359 SDNodeFlags Flags;
8361 Flags.setNoFPExcept(true);
8362
8363 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8364 Flags.copyFMF(*FPOp);
8365
8366 unsigned Opcode;
8367 switch (FPI.getIntrinsicID()) {
8368 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8369#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8370 case Intrinsic::INTRINSIC: \
8371 Opcode = ISD::STRICT_##DAGN; \
8372 break;
8373#include "llvm/IR/ConstrainedOps.def"
8374 case Intrinsic::experimental_constrained_fmuladd: {
8375 Opcode = ISD::STRICT_FMA;
8376 // Break fmuladd into fmul and fadd.
8377 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8378 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8379 Opers.pop_back();
8380 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8381 pushOutChain(Mul, EB);
8382 Opcode = ISD::STRICT_FADD;
8383 Opers.clear();
8384 Opers.push_back(Mul.getValue(1));
8385 Opers.push_back(Mul.getValue(0));
8386 Opers.push_back(getValue(FPI.getArgOperand(2)));
8387 }
8388 break;
8389 }
8390 }
8391
8392 // A few strict DAG nodes carry additional operands that are not
8393 // set up by the default code above.
8394 switch (Opcode) {
8395 default: break;
8397 Opers.push_back(
8398 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8399 break;
8400 case ISD::STRICT_FSETCC:
8401 case ISD::STRICT_FSETCCS: {
8403 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8404 if (TM.Options.NoNaNsFPMath)
8405 Condition = getFCmpCodeWithoutNaN(Condition);
8406 Opers.push_back(DAG.getCondCode(Condition));
8407 break;
8408 }
8409 }
8410
8411 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8412 pushOutChain(Result, EB);
8413
8414 SDValue FPResult = Result.getValue(0);
8415 setValue(&FPI, FPResult);
8416}
8417
8418static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8419 std::optional<unsigned> ResOPC;
8420 switch (VPIntrin.getIntrinsicID()) {
8421 case Intrinsic::vp_ctlz: {
8422 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8423 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8424 break;
8425 }
8426 case Intrinsic::vp_cttz: {
8427 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8428 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8429 break;
8430 }
8431 case Intrinsic::vp_cttz_elts: {
8432 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8433 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8434 break;
8435 }
8436#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8437 case Intrinsic::VPID: \
8438 ResOPC = ISD::VPSD; \
8439 break;
8440#include "llvm/IR/VPIntrinsics.def"
8441 }
8442
8443 if (!ResOPC)
8445 "Inconsistency: no SDNode available for this VPIntrinsic!");
8446
8447 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8448 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8449 if (VPIntrin.getFastMathFlags().allowReassoc())
8450 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8451 : ISD::VP_REDUCE_FMUL;
8452 }
8453
8454 return *ResOPC;
8455}
8456
8457void SelectionDAGBuilder::visitVPLoad(
8458 const VPIntrinsic &VPIntrin, EVT VT,
8459 const SmallVectorImpl<SDValue> &OpValues) {
8460 SDLoc DL = getCurSDLoc();
8461 Value *PtrOperand = VPIntrin.getArgOperand(0);
8462 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8463 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8464 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8465 SDValue LD;
8466 // Do not serialize variable-length loads of constant memory with
8467 // anything.
8468 if (!Alignment)
8469 Alignment = DAG.getEVTAlign(VT);
8470 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8471 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8472 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8473 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8474 MachineMemOperand::Flags MMOFlags =
8475 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8476 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8477 MachinePointerInfo(PtrOperand), MMOFlags,
8478 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8479 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8480 MMO, false /*IsExpanding */);
8481 if (AddToChain)
8482 PendingLoads.push_back(LD.getValue(1));
8483 setValue(&VPIntrin, LD);
8484}
8485
8486void SelectionDAGBuilder::visitVPLoadFF(
8487 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8488 const SmallVectorImpl<SDValue> &OpValues) {
8489 assert(OpValues.size() == 3 && "Unexpected number of operands");
8490 SDLoc DL = getCurSDLoc();
8491 Value *PtrOperand = VPIntrin.getArgOperand(0);
8492 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8493 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8494 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8495 SDValue LD;
8496 // Do not serialize variable-length loads of constant memory with
8497 // anything.
8498 if (!Alignment)
8499 Alignment = DAG.getEVTAlign(VT);
8500 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8501 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8502 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8503 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8504 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8505 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8506 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8507 MMO);
8508 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8509 if (AddToChain)
8510 PendingLoads.push_back(LD.getValue(2));
8511 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8512}
8513
8514void SelectionDAGBuilder::visitVPGather(
8515 const VPIntrinsic &VPIntrin, EVT VT,
8516 const SmallVectorImpl<SDValue> &OpValues) {
8517 SDLoc DL = getCurSDLoc();
8518 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8519 Value *PtrOperand = VPIntrin.getArgOperand(0);
8520 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8521 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8522 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8523 SDValue LD;
8524 if (!Alignment)
8525 Alignment = DAG.getEVTAlign(VT.getScalarType());
8526 unsigned AS =
8527 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8528 MachineMemOperand::Flags MMOFlags =
8529 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8530 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8531 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8532 *Alignment, AAInfo, Ranges);
8533 SDValue Base, Index, Scale;
8534 bool UniformBase =
8535 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8536 VT.getScalarStoreSize());
8537 if (!UniformBase) {
8538 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8539 Index = getValue(PtrOperand);
8540 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8541 }
8542 EVT IdxVT = Index.getValueType();
8543 EVT EltTy = IdxVT.getVectorElementType();
8544 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8545 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8546 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8547 }
8548 LD = DAG.getGatherVP(
8549 DAG.getVTList(VT, MVT::Other), VT, DL,
8550 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8552 PendingLoads.push_back(LD.getValue(1));
8553 setValue(&VPIntrin, LD);
8554}
8555
8556void SelectionDAGBuilder::visitVPStore(
8557 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8558 SDLoc DL = getCurSDLoc();
8559 Value *PtrOperand = VPIntrin.getArgOperand(1);
8560 EVT VT = OpValues[0].getValueType();
8561 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8562 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8563 SDValue ST;
8564 if (!Alignment)
8565 Alignment = DAG.getEVTAlign(VT);
8566 SDValue Ptr = OpValues[1];
8567 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8568 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8569 MachineMemOperand::Flags MMOFlags =
8570 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8571 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8572 MachinePointerInfo(PtrOperand), MMOFlags,
8573 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8574 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8575 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8576 /* IsTruncating */ false, /*IsCompressing*/ false);
8577 DAG.setRoot(ST);
8578 setValue(&VPIntrin, ST);
8579}
8580
8581void SelectionDAGBuilder::visitVPScatter(
8582 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8583 SDLoc DL = getCurSDLoc();
8584 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8585 Value *PtrOperand = VPIntrin.getArgOperand(1);
8586 EVT VT = OpValues[0].getValueType();
8587 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8588 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8589 SDValue ST;
8590 if (!Alignment)
8591 Alignment = DAG.getEVTAlign(VT.getScalarType());
8592 unsigned AS =
8593 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8594 MachineMemOperand::Flags MMOFlags =
8595 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8596 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8597 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8598 *Alignment, AAInfo);
8599 SDValue Base, Index, Scale;
8600 bool UniformBase =
8601 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8602 VT.getScalarStoreSize());
8603 if (!UniformBase) {
8604 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8605 Index = getValue(PtrOperand);
8606 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8607 }
8608 EVT IdxVT = Index.getValueType();
8609 EVT EltTy = IdxVT.getVectorElementType();
8610 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8611 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8612 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8613 }
8614 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8615 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8616 OpValues[2], OpValues[3]},
8617 MMO, ISD::SIGNED_SCALED);
8618 DAG.setRoot(ST);
8619 setValue(&VPIntrin, ST);
8620}
8621
8622void SelectionDAGBuilder::visitVPStridedLoad(
8623 const VPIntrinsic &VPIntrin, EVT VT,
8624 const SmallVectorImpl<SDValue> &OpValues) {
8625 SDLoc DL = getCurSDLoc();
8626 Value *PtrOperand = VPIntrin.getArgOperand(0);
8627 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8628 if (!Alignment)
8629 Alignment = DAG.getEVTAlign(VT.getScalarType());
8630 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8631 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8632 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8633 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8634 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8635 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8636 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8637 MachineMemOperand::Flags MMOFlags =
8638 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8639 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8640 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8641 *Alignment, AAInfo, Ranges);
8642
8643 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8644 OpValues[2], OpValues[3], MMO,
8645 false /*IsExpanding*/);
8646
8647 if (AddToChain)
8648 PendingLoads.push_back(LD.getValue(1));
8649 setValue(&VPIntrin, LD);
8650}
8651
8652void SelectionDAGBuilder::visitVPStridedStore(
8653 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8654 SDLoc DL = getCurSDLoc();
8655 Value *PtrOperand = VPIntrin.getArgOperand(1);
8656 EVT VT = OpValues[0].getValueType();
8657 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8658 if (!Alignment)
8659 Alignment = DAG.getEVTAlign(VT.getScalarType());
8660 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8661 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8662 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8663 MachineMemOperand::Flags MMOFlags =
8664 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8665 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8666 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8667 *Alignment, AAInfo);
8668
8669 SDValue ST = DAG.getStridedStoreVP(
8670 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8671 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8672 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8673 /*IsCompressing*/ false);
8674
8675 DAG.setRoot(ST);
8676 setValue(&VPIntrin, ST);
8677}
8678
8679void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8680 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8681 SDLoc DL = getCurSDLoc();
8682
8683 ISD::CondCode Condition;
8685 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8686 if (IsFP) {
8687 // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
8688 // flags, but calls that don't return floating-point types can't be
8689 // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
8690 Condition = getFCmpCondCode(CondCode);
8691 if (TM.Options.NoNaNsFPMath)
8692 Condition = getFCmpCodeWithoutNaN(Condition);
8693 } else {
8694 Condition = getICmpCondCode(CondCode);
8695 }
8696
8697 SDValue Op1 = getValue(VPIntrin.getOperand(0));
8698 SDValue Op2 = getValue(VPIntrin.getOperand(1));
8699 // #2 is the condition code
8700 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8701 SDValue EVL = getValue(VPIntrin.getOperand(4));
8702 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8703 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8704 "Unexpected target EVL type");
8705 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8706
8707 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8708 VPIntrin.getType());
8709 setValue(&VPIntrin,
8710 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8711}
8712
8713void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8714 const VPIntrinsic &VPIntrin) {
8715 SDLoc DL = getCurSDLoc();
8716 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8717
8718 auto IID = VPIntrin.getIntrinsicID();
8719
8720 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8721 return visitVPCmp(*CmpI);
8722
8723 SmallVector<EVT, 4> ValueVTs;
8724 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8725 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8726 SDVTList VTs = DAG.getVTList(ValueVTs);
8727
8728 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8729
8730 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8731 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8732 "Unexpected target EVL type");
8733
8734 // Request operands.
8735 SmallVector<SDValue, 7> OpValues;
8736 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8737 auto Op = getValue(VPIntrin.getArgOperand(I));
8738 if (I == EVLParamPos)
8739 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8740 OpValues.push_back(Op);
8741 }
8742
8743 switch (Opcode) {
8744 default: {
8745 SDNodeFlags SDFlags;
8746 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8747 SDFlags.copyFMF(*FPMO);
8748 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8749 setValue(&VPIntrin, Result);
8750 break;
8751 }
8752 case ISD::VP_LOAD:
8753 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8754 break;
8755 case ISD::VP_LOAD_FF:
8756 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8757 break;
8758 case ISD::VP_GATHER:
8759 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8760 break;
8761 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8762 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8763 break;
8764 case ISD::VP_STORE:
8765 visitVPStore(VPIntrin, OpValues);
8766 break;
8767 case ISD::VP_SCATTER:
8768 visitVPScatter(VPIntrin, OpValues);
8769 break;
8770 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8771 visitVPStridedStore(VPIntrin, OpValues);
8772 break;
8773 case ISD::VP_FMULADD: {
8774 assert(OpValues.size() == 5 && "Unexpected number of operands");
8775 SDNodeFlags SDFlags;
8776 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8777 SDFlags.copyFMF(*FPMO);
8778 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8779 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8780 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8781 } else {
8782 SDValue Mul = DAG.getNode(
8783 ISD::VP_FMUL, DL, VTs,
8784 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8785 SDValue Add =
8786 DAG.getNode(ISD::VP_FADD, DL, VTs,
8787 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8788 setValue(&VPIntrin, Add);
8789 }
8790 break;
8791 }
8792 case ISD::VP_IS_FPCLASS: {
8793 const DataLayout DLayout = DAG.getDataLayout();
8794 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8795 auto Constant = OpValues[1]->getAsZExtVal();
8796 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8797 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8798 {OpValues[0], Check, OpValues[2], OpValues[3]});
8799 setValue(&VPIntrin, V);
8800 return;
8801 }
8802 case ISD::VP_INTTOPTR: {
8803 SDValue N = OpValues[0];
8804 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8805 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8806 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8807 OpValues[2]);
8808 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8809 OpValues[2]);
8810 setValue(&VPIntrin, N);
8811 break;
8812 }
8813 case ISD::VP_PTRTOINT: {
8814 SDValue N = OpValues[0];
8815 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8816 VPIntrin.getType());
8817 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
8818 VPIntrin.getOperand(0)->getType());
8819 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8820 OpValues[2]);
8821 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8822 OpValues[2]);
8823 setValue(&VPIntrin, N);
8824 break;
8825 }
8826 case ISD::VP_ABS:
8827 case ISD::VP_CTLZ:
8828 case ISD::VP_CTLZ_ZERO_UNDEF:
8829 case ISD::VP_CTTZ:
8830 case ISD::VP_CTTZ_ZERO_UNDEF:
8831 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
8832 case ISD::VP_CTTZ_ELTS: {
8833 SDValue Result =
8834 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
8835 setValue(&VPIntrin, Result);
8836 break;
8837 }
8838 }
8839}
8840
8841SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
8842 const BasicBlock *EHPadBB,
8843 MCSymbol *&BeginLabel) {
8844 MachineFunction &MF = DAG.getMachineFunction();
8845
8846 // Insert a label before the invoke call to mark the try range. This can be
8847 // used to detect deletion of the invoke via the MachineModuleInfo.
8848 BeginLabel = MF.getContext().createTempSymbol();
8849
8850 // For SjLj, keep track of which landing pads go with which invokes
8851 // so as to maintain the ordering of pads in the LSDA.
8852 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
8853 if (CallSiteIndex) {
8854 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
8855 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
8856
8857 // Now that the call site is handled, stop tracking it.
8858 FuncInfo.setCurrentCallSite(0);
8859 }
8860
8861 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
8862}
8863
8864SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
8865 const BasicBlock *EHPadBB,
8866 MCSymbol *BeginLabel) {
8867 assert(BeginLabel && "BeginLabel should've been set");
8868
8869 MachineFunction &MF = DAG.getMachineFunction();
8870
8871 // Insert a label at the end of the invoke call to mark the try range. This
8872 // can be used to detect deletion of the invoke via the MachineModuleInfo.
8873 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
8874 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
8875
8876 // Inform MachineModuleInfo of range.
8877 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
8878 // There is a platform (e.g. wasm) that uses funclet style IR but does not
8879 // actually use outlined funclets and their LSDA info style.
8880 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
8881 assert(II && "II should've been set");
8882 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
8883 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
8884 } else if (!isScopedEHPersonality(Pers)) {
8885 assert(EHPadBB);
8886 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
8887 }
8888
8889 return Chain;
8890}
8891
8892std::pair<SDValue, SDValue>
8894 const BasicBlock *EHPadBB) {
8895 MCSymbol *BeginLabel = nullptr;
8896
8897 if (EHPadBB) {
8898 // Both PendingLoads and PendingExports must be flushed here;
8899 // this call might not return.
8900 (void)getRoot();
8901 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
8902 CLI.setChain(getRoot());
8903 }
8904
8905 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8906 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
8907
8908 assert((CLI.IsTailCall || Result.second.getNode()) &&
8909 "Non-null chain expected with non-tail call!");
8910 assert((Result.second.getNode() || !Result.first.getNode()) &&
8911 "Null value expected with tail call!");
8912
8913 if (!Result.second.getNode()) {
8914 // As a special case, a null chain means that a tail call has been emitted
8915 // and the DAG root is already updated.
8916 HasTailCall = true;
8917
8918 // Since there's no actual continuation from this block, nothing can be
8919 // relying on us setting vregs for them.
8920 PendingExports.clear();
8921 } else {
8922 DAG.setRoot(Result.second);
8923 }
8924
8925 if (EHPadBB) {
8926 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
8927 BeginLabel));
8928 Result.second = getRoot();
8929 }
8930
8931 return Result;
8932}
8933
8935 bool isMustTailCall = CB.isMustTailCall();
8936
8937 // Avoid emitting tail calls in functions with the disable-tail-calls
8938 // attribute.
8939 const Function *Caller = CB.getParent()->getParent();
8940 if (Caller->getFnAttribute("disable-tail-calls").getValueAsString() ==
8941 "true" &&
8942 !isMustTailCall)
8943 return false;
8944
8945 // We can't tail call inside a function with a swifterror argument. Lowering
8946 // does not support this yet. It would have to move into the swifterror
8947 // register before the call.
8948 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
8949 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
8950 return false;
8951
8952 // Check if target-independent constraints permit a tail call here.
8953 // Target-dependent constraints are checked within TLI->LowerCallTo.
8954 return isInTailCallPosition(CB, DAG.getTarget());
8955}
8956
8958 bool isTailCall, bool isMustTailCall,
8959 const BasicBlock *EHPadBB,
8960 const TargetLowering::PtrAuthInfo *PAI) {
8961 auto &DL = DAG.getDataLayout();
8962 FunctionType *FTy = CB.getFunctionType();
8963 Type *RetTy = CB.getType();
8964
8966 Args.reserve(CB.arg_size());
8967
8968 const Value *SwiftErrorVal = nullptr;
8969 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8970
8971 if (isTailCall)
8972 isTailCall = canTailCall(CB);
8973
8974 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
8975 const Value *V = *I;
8976
8977 // Skip empty types
8978 if (V->getType()->isEmptyTy())
8979 continue;
8980
8981 SDValue ArgNode = getValue(V);
8982 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
8983 Entry.setAttributes(&CB, I - CB.arg_begin());
8984
8985 // Use swifterror virtual register as input to the call.
8986 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
8987 SwiftErrorVal = V;
8988 // We find the virtual register for the actual swifterror argument.
8989 // Instead of using the Value, we use the virtual register instead.
8990 Entry.Node =
8991 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
8992 EVT(TLI.getPointerTy(DL)));
8993 }
8994
8995 Args.push_back(Entry);
8996
8997 // If we have an explicit sret argument that is an Instruction, (i.e., it
8998 // might point to function-local memory), we can't meaningfully tail-call.
8999 if (Entry.IsSRet && isa<Instruction>(V))
9000 isTailCall = false;
9001 }
9002
9003 // If call site has a cfguardtarget operand bundle, create and add an
9004 // additional ArgListEntry.
9005 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9006 Value *V = Bundle->Inputs[0];
9008 Entry.IsCFGuardTarget = true;
9009 Args.push_back(Entry);
9010 }
9011
9012 // Disable tail calls if there is an swifterror argument. Targets have not
9013 // been updated to support tail calls.
9014 if (TLI.supportSwiftError() && SwiftErrorVal)
9015 isTailCall = false;
9016
9017 ConstantInt *CFIType = nullptr;
9018 if (CB.isIndirectCall()) {
9019 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9020 if (!TLI.supportKCFIBundles())
9022 "Target doesn't support calls with kcfi operand bundles.");
9023 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9024 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9025 }
9026 }
9027
9028 SDValue ConvControlToken;
9029 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9030 auto *Token = Bundle->Inputs[0].get();
9031 ConvControlToken = getValue(Token);
9032 }
9033
9036 .setChain(getRoot())
9037 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9038 .setTailCall(isTailCall)
9042 .setCFIType(CFIType)
9043 .setConvergenceControlToken(ConvControlToken);
9044
9045 // Set the pointer authentication info if we have it.
9046 if (PAI) {
9047 if (!TLI.supportPtrAuthBundles())
9049 "This target doesn't support calls with ptrauth operand bundles.");
9050 CLI.setPtrAuth(*PAI);
9051 }
9052
9053 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9054
9055 if (Result.first.getNode()) {
9056 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9057 setValue(&CB, Result.first);
9058 }
9059
9060 // The last element of CLI.InVals has the SDValue for swifterror return.
9061 // Here we copy it to a virtual register and update SwiftErrorMap for
9062 // book-keeping.
9063 if (SwiftErrorVal && TLI.supportSwiftError()) {
9064 // Get the last element of InVals.
9065 SDValue Src = CLI.InVals.back();
9066 Register VReg =
9067 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9068 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9069 DAG.setRoot(CopyNode);
9070 }
9071}
9072
9073static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9074 SelectionDAGBuilder &Builder) {
9075 // Check to see if this load can be trivially constant folded, e.g. if the
9076 // input is from a string literal.
9077 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9078 // Cast pointer to the type we really want to load.
9079 Type *LoadTy =
9080 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9081 if (LoadVT.isVector())
9082 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9083 if (const Constant *LoadCst =
9084 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9085 LoadTy, Builder.DAG.getDataLayout()))
9086 return Builder.getValue(LoadCst);
9087 }
9088
9089 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9090 // still constant memory, the input chain can be the entry node.
9091 SDValue Root;
9092 bool ConstantMemory = false;
9093
9094 // Do not serialize (non-volatile) loads of constant memory with anything.
9095 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9096 Root = Builder.DAG.getEntryNode();
9097 ConstantMemory = true;
9098 } else {
9099 // Do not serialize non-volatile loads against each other.
9100 Root = Builder.DAG.getRoot();
9101 }
9102
9103 SDValue Ptr = Builder.getValue(PtrVal);
9104 SDValue LoadVal =
9105 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9106 MachinePointerInfo(PtrVal), Align(1));
9107
9108 if (!ConstantMemory)
9109 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9110 return LoadVal;
9111}
9112
9113/// Record the value for an instruction that produces an integer result,
9114/// converting the type where necessary.
9115void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9116 SDValue Value,
9117 bool IsSigned) {
9118 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9119 I.getType(), true);
9120 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9121 setValue(&I, Value);
9122}
9123
9124/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9125/// true and lower it. Otherwise return false, and it will be lowered like a
9126/// normal call.
9127/// The caller already checked that \p I calls the appropriate LibFunc with a
9128/// correct prototype.
9129bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9130 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9131 const Value *Size = I.getArgOperand(2);
9132 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9133 if (CSize && CSize->getZExtValue() == 0) {
9134 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9135 I.getType(), true);
9136 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9137 return true;
9138 }
9139
9140 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9141 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9142 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9143 getValue(Size), &I);
9144 if (Res.first.getNode()) {
9145 processIntegerCallValue(I, Res.first, true);
9146 PendingLoads.push_back(Res.second);
9147 return true;
9148 }
9149
9150 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9151 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9152 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9153 return false;
9154
9155 // If the target has a fast compare for the given size, it will return a
9156 // preferred load type for that size. Require that the load VT is legal and
9157 // that the target supports unaligned loads of that type. Otherwise, return
9158 // INVALID.
9159 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9160 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9161 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9162 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9163 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9164 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9165 // TODO: Check alignment of src and dest ptrs.
9166 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9167 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9168 if (!TLI.isTypeLegal(LVT) ||
9169 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9170 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9172 }
9173
9174 return LVT;
9175 };
9176
9177 // This turns into unaligned loads. We only do this if the target natively
9178 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9179 // we'll only produce a small number of byte loads.
9180 MVT LoadVT;
9181 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9182 switch (NumBitsToCompare) {
9183 default:
9184 return false;
9185 case 16:
9186 LoadVT = MVT::i16;
9187 break;
9188 case 32:
9189 LoadVT = MVT::i32;
9190 break;
9191 case 64:
9192 case 128:
9193 case 256:
9194 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9195 break;
9196 }
9197
9198 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9199 return false;
9200
9201 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9202 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9203
9204 // Bitcast to a wide integer type if the loads are vectors.
9205 if (LoadVT.isVector()) {
9206 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9207 LoadL = DAG.getBitcast(CmpVT, LoadL);
9208 LoadR = DAG.getBitcast(CmpVT, LoadR);
9209 }
9210
9211 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9212 processIntegerCallValue(I, Cmp, false);
9213 return true;
9214}
9215
9216/// See if we can lower a memchr call into an optimized form. If so, return
9217/// true and lower it. Otherwise return false, and it will be lowered like a
9218/// normal call.
9219/// The caller already checked that \p I calls the appropriate LibFunc with a
9220/// correct prototype.
9221bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9222 const Value *Src = I.getArgOperand(0);
9223 const Value *Char = I.getArgOperand(1);
9224 const Value *Length = I.getArgOperand(2);
9225
9226 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9227 std::pair<SDValue, SDValue> Res =
9228 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9229 getValue(Src), getValue(Char), getValue(Length),
9230 MachinePointerInfo(Src));
9231 if (Res.first.getNode()) {
9232 setValue(&I, Res.first);
9233 PendingLoads.push_back(Res.second);
9234 return true;
9235 }
9236
9237 return false;
9238}
9239
9240/// See if we can lower a mempcpy call into an optimized form. If so, return
9241/// true and lower it. Otherwise return false, and it will be lowered like a
9242/// normal call.
9243/// The caller already checked that \p I calls the appropriate LibFunc with a
9244/// correct prototype.
9245bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9246 SDValue Dst = getValue(I.getArgOperand(0));
9247 SDValue Src = getValue(I.getArgOperand(1));
9248 SDValue Size = getValue(I.getArgOperand(2));
9249
9250 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9251 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9252 // DAG::getMemcpy needs Alignment to be defined.
9253 Align Alignment = std::min(DstAlign, SrcAlign);
9254
9255 SDLoc sdl = getCurSDLoc();
9256
9257 // In the mempcpy context we need to pass in a false value for isTailCall
9258 // because the return pointer needs to be adjusted by the size of
9259 // the copied memory.
9260 SDValue Root = getMemoryRoot();
9261 SDValue MC = DAG.getMemcpy(
9262 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9263 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9264 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9265 assert(MC.getNode() != nullptr &&
9266 "** memcpy should not be lowered as TailCall in mempcpy context **");
9267 DAG.setRoot(MC);
9268
9269 // Check if Size needs to be truncated or extended.
9270 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9271
9272 // Adjust return pointer to point just past the last dst byte.
9273 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9274 setValue(&I, DstPlusSize);
9275 return true;
9276}
9277
9278/// See if we can lower a strcpy call into an optimized form. If so, return
9279/// true and lower it, otherwise return false and it will be lowered like a
9280/// normal call.
9281/// The caller already checked that \p I calls the appropriate LibFunc with a
9282/// correct prototype.
9283bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9284 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9285
9286 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9287 std::pair<SDValue, SDValue> Res =
9289 getValue(Arg0), getValue(Arg1),
9290 MachinePointerInfo(Arg0),
9291 MachinePointerInfo(Arg1), isStpcpy);
9292 if (Res.first.getNode()) {
9293 setValue(&I, Res.first);
9294 DAG.setRoot(Res.second);
9295 return true;
9296 }
9297
9298 return false;
9299}
9300
9301/// See if we can lower a strcmp call into an optimized form. If so, return
9302/// true and lower it, otherwise return false and it will be lowered like a
9303/// normal call.
9304/// The caller already checked that \p I calls the appropriate LibFunc with a
9305/// correct prototype.
9306bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9307 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9308
9309 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9310 std::pair<SDValue, SDValue> Res =
9311 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
9312 getValue(Arg0), getValue(Arg1),
9313 MachinePointerInfo(Arg0),
9314 MachinePointerInfo(Arg1));
9315 if (Res.first.getNode()) {
9316 processIntegerCallValue(I, Res.first, true);
9317 PendingLoads.push_back(Res.second);
9318 return true;
9319 }
9320
9321 return false;
9322}
9323
9324/// See if we can lower a strlen call into an optimized form. If so, return
9325/// true and lower it, otherwise return false and it will be lowered like a
9326/// normal call.
9327/// The caller already checked that \p I calls the appropriate LibFunc with a
9328/// correct prototype.
9329bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9330 const Value *Arg0 = I.getArgOperand(0);
9331
9332 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9333 std::pair<SDValue, SDValue> Res =
9334 TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(),
9335 getValue(Arg0), MachinePointerInfo(Arg0));
9336 if (Res.first.getNode()) {
9337 processIntegerCallValue(I, Res.first, false);
9338 PendingLoads.push_back(Res.second);
9339 return true;
9340 }
9341
9342 return false;
9343}
9344
9345/// See if we can lower a strnlen call into an optimized form. If so, return
9346/// true and lower it, otherwise return false and it will be lowered like a
9347/// normal call.
9348/// The caller already checked that \p I calls the appropriate LibFunc with a
9349/// correct prototype.
9350bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9351 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9352
9353 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9354 std::pair<SDValue, SDValue> Res =
9355 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9356 getValue(Arg0), getValue(Arg1),
9357 MachinePointerInfo(Arg0));
9358 if (Res.first.getNode()) {
9359 processIntegerCallValue(I, Res.first, false);
9360 PendingLoads.push_back(Res.second);
9361 return true;
9362 }
9363
9364 return false;
9365}
9366
9367/// See if we can lower a unary floating-point operation into an SDNode with
9368/// the specified Opcode. If so, return true and lower it, otherwise return
9369/// false and it will be lowered like a normal call.
9370/// The caller already checked that \p I calls the appropriate LibFunc with a
9371/// correct prototype.
9372bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9373 unsigned Opcode) {
9374 // We already checked this call's prototype; verify it doesn't modify errno.
9375 if (!I.onlyReadsMemory())
9376 return false;
9377
9378 SDNodeFlags Flags;
9379 Flags.copyFMF(cast<FPMathOperator>(I));
9380
9381 SDValue Tmp = getValue(I.getArgOperand(0));
9382 setValue(&I,
9383 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9384 return true;
9385}
9386
9387/// See if we can lower a binary floating-point operation into an SDNode with
9388/// the specified Opcode. If so, return true and lower it. Otherwise return
9389/// false, and it will be lowered like a normal call.
9390/// The caller already checked that \p I calls the appropriate LibFunc with a
9391/// correct prototype.
9392bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9393 unsigned Opcode) {
9394 // We already checked this call's prototype; verify it doesn't modify errno.
9395 if (!I.onlyReadsMemory())
9396 return false;
9397
9398 SDNodeFlags Flags;
9399 Flags.copyFMF(cast<FPMathOperator>(I));
9400
9401 SDValue Tmp0 = getValue(I.getArgOperand(0));
9402 SDValue Tmp1 = getValue(I.getArgOperand(1));
9403 EVT VT = Tmp0.getValueType();
9404 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9405 return true;
9406}
9407
9408void SelectionDAGBuilder::visitCall(const CallInst &I) {
9409 // Handle inline assembly differently.
9410 if (I.isInlineAsm()) {
9411 visitInlineAsm(I);
9412 return;
9413 }
9414
9416
9417 if (Function *F = I.getCalledFunction()) {
9418 if (F->isDeclaration()) {
9419 // Is this an LLVM intrinsic?
9420 if (unsigned IID = F->getIntrinsicID()) {
9421 visitIntrinsicCall(I, IID);
9422 return;
9423 }
9424 }
9425
9426 // Check for well-known libc/libm calls. If the function is internal, it
9427 // can't be a library call. Don't do the check if marked as nobuiltin for
9428 // some reason or the call site requires strict floating point semantics.
9429 LibFunc Func;
9430 if (!I.isNoBuiltin() && !I.isStrictFP() && !F->hasLocalLinkage() &&
9431 F->hasName() && LibInfo->getLibFunc(*F, Func) &&
9432 LibInfo->hasOptimizedCodeGen(Func)) {
9433 switch (Func) {
9434 default: break;
9435 case LibFunc_bcmp:
9436 if (visitMemCmpBCmpCall(I))
9437 return;
9438 break;
9439 case LibFunc_copysign:
9440 case LibFunc_copysignf:
9441 case LibFunc_copysignl:
9442 // We already checked this call's prototype; verify it doesn't modify
9443 // errno.
9444 if (I.onlyReadsMemory()) {
9445 SDValue LHS = getValue(I.getArgOperand(0));
9446 SDValue RHS = getValue(I.getArgOperand(1));
9448 LHS.getValueType(), LHS, RHS));
9449 return;
9450 }
9451 break;
9452 case LibFunc_fabs:
9453 case LibFunc_fabsf:
9454 case LibFunc_fabsl:
9455 if (visitUnaryFloatCall(I, ISD::FABS))
9456 return;
9457 break;
9458 case LibFunc_fmin:
9459 case LibFunc_fminf:
9460 case LibFunc_fminl:
9461 if (visitBinaryFloatCall(I, ISD::FMINNUM))
9462 return;
9463 break;
9464 case LibFunc_fmax:
9465 case LibFunc_fmaxf:
9466 case LibFunc_fmaxl:
9467 if (visitBinaryFloatCall(I, ISD::FMAXNUM))
9468 return;
9469 break;
9470 case LibFunc_fminimum_num:
9471 case LibFunc_fminimum_numf:
9472 case LibFunc_fminimum_numl:
9473 if (visitBinaryFloatCall(I, ISD::FMINIMUMNUM))
9474 return;
9475 break;
9476 case LibFunc_fmaximum_num:
9477 case LibFunc_fmaximum_numf:
9478 case LibFunc_fmaximum_numl:
9479 if (visitBinaryFloatCall(I, ISD::FMAXIMUMNUM))
9480 return;
9481 break;
9482 case LibFunc_sin:
9483 case LibFunc_sinf:
9484 case LibFunc_sinl:
9485 if (visitUnaryFloatCall(I, ISD::FSIN))
9486 return;
9487 break;
9488 case LibFunc_cos:
9489 case LibFunc_cosf:
9490 case LibFunc_cosl:
9491 if (visitUnaryFloatCall(I, ISD::FCOS))
9492 return;
9493 break;
9494 case LibFunc_tan:
9495 case LibFunc_tanf:
9496 case LibFunc_tanl:
9497 if (visitUnaryFloatCall(I, ISD::FTAN))
9498 return;
9499 break;
9500 case LibFunc_asin:
9501 case LibFunc_asinf:
9502 case LibFunc_asinl:
9503 if (visitUnaryFloatCall(I, ISD::FASIN))
9504 return;
9505 break;
9506 case LibFunc_acos:
9507 case LibFunc_acosf:
9508 case LibFunc_acosl:
9509 if (visitUnaryFloatCall(I, ISD::FACOS))
9510 return;
9511 break;
9512 case LibFunc_atan:
9513 case LibFunc_atanf:
9514 case LibFunc_atanl:
9515 if (visitUnaryFloatCall(I, ISD::FATAN))
9516 return;
9517 break;
9518 case LibFunc_atan2:
9519 case LibFunc_atan2f:
9520 case LibFunc_atan2l:
9521 if (visitBinaryFloatCall(I, ISD::FATAN2))
9522 return;
9523 break;
9524 case LibFunc_sinh:
9525 case LibFunc_sinhf:
9526 case LibFunc_sinhl:
9527 if (visitUnaryFloatCall(I, ISD::FSINH))
9528 return;
9529 break;
9530 case LibFunc_cosh:
9531 case LibFunc_coshf:
9532 case LibFunc_coshl:
9533 if (visitUnaryFloatCall(I, ISD::FCOSH))
9534 return;
9535 break;
9536 case LibFunc_tanh:
9537 case LibFunc_tanhf:
9538 case LibFunc_tanhl:
9539 if (visitUnaryFloatCall(I, ISD::FTANH))
9540 return;
9541 break;
9542 case LibFunc_sqrt:
9543 case LibFunc_sqrtf:
9544 case LibFunc_sqrtl:
9545 case LibFunc_sqrt_finite:
9546 case LibFunc_sqrtf_finite:
9547 case LibFunc_sqrtl_finite:
9548 if (visitUnaryFloatCall(I, ISD::FSQRT))
9549 return;
9550 break;
9551 case LibFunc_floor:
9552 case LibFunc_floorf:
9553 case LibFunc_floorl:
9554 if (visitUnaryFloatCall(I, ISD::FFLOOR))
9555 return;
9556 break;
9557 case LibFunc_nearbyint:
9558 case LibFunc_nearbyintf:
9559 case LibFunc_nearbyintl:
9560 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
9561 return;
9562 break;
9563 case LibFunc_ceil:
9564 case LibFunc_ceilf:
9565 case LibFunc_ceill:
9566 if (visitUnaryFloatCall(I, ISD::FCEIL))
9567 return;
9568 break;
9569 case LibFunc_rint:
9570 case LibFunc_rintf:
9571 case LibFunc_rintl:
9572 if (visitUnaryFloatCall(I, ISD::FRINT))
9573 return;
9574 break;
9575 case LibFunc_round:
9576 case LibFunc_roundf:
9577 case LibFunc_roundl:
9578 if (visitUnaryFloatCall(I, ISD::FROUND))
9579 return;
9580 break;
9581 case LibFunc_trunc:
9582 case LibFunc_truncf:
9583 case LibFunc_truncl:
9584 if (visitUnaryFloatCall(I, ISD::FTRUNC))
9585 return;
9586 break;
9587 case LibFunc_log2:
9588 case LibFunc_log2f:
9589 case LibFunc_log2l:
9590 if (visitUnaryFloatCall(I, ISD::FLOG2))
9591 return;
9592 break;
9593 case LibFunc_exp2:
9594 case LibFunc_exp2f:
9595 case LibFunc_exp2l:
9596 if (visitUnaryFloatCall(I, ISD::FEXP2))
9597 return;
9598 break;
9599 case LibFunc_exp10:
9600 case LibFunc_exp10f:
9601 case LibFunc_exp10l:
9602 if (visitUnaryFloatCall(I, ISD::FEXP10))
9603 return;
9604 break;
9605 case LibFunc_ldexp:
9606 case LibFunc_ldexpf:
9607 case LibFunc_ldexpl:
9608 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9609 return;
9610 break;
9611 case LibFunc_memcmp:
9612 if (visitMemCmpBCmpCall(I))
9613 return;
9614 break;
9615 case LibFunc_mempcpy:
9616 if (visitMemPCpyCall(I))
9617 return;
9618 break;
9619 case LibFunc_memchr:
9620 if (visitMemChrCall(I))
9621 return;
9622 break;
9623 case LibFunc_strcpy:
9624 if (visitStrCpyCall(I, false))
9625 return;
9626 break;
9627 case LibFunc_stpcpy:
9628 if (visitStrCpyCall(I, true))
9629 return;
9630 break;
9631 case LibFunc_strcmp:
9632 if (visitStrCmpCall(I))
9633 return;
9634 break;
9635 case LibFunc_strlen:
9636 if (visitStrLenCall(I))
9637 return;
9638 break;
9639 case LibFunc_strnlen:
9640 if (visitStrNLenCall(I))
9641 return;
9642 break;
9643 }
9644 }
9645 }
9646
9647 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9648 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9649 return;
9650 }
9651
9652 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9653 // have to do anything here to lower funclet bundles.
9654 // CFGuardTarget bundles are lowered in LowerCallTo.
9656 I, "calls",
9661
9662 SDValue Callee = getValue(I.getCalledOperand());
9663
9664 if (I.hasDeoptState())
9665 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9666 else
9667 // Check if we can potentially perform a tail call. More detailed checking
9668 // is be done within LowerCallTo, after more information about the call is
9669 // known.
9670 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9671}
9672
9674 const CallBase &CB, const BasicBlock *EHPadBB) {
9675 auto PAB = CB.getOperandBundle("ptrauth");
9676 const Value *CalleeV = CB.getCalledOperand();
9677
9678 // Gather the call ptrauth data from the operand bundle:
9679 // [ i32 <key>, i64 <discriminator> ]
9680 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9681 const Value *Discriminator = PAB->Inputs[1];
9682
9683 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9684 assert(Discriminator->getType()->isIntegerTy(64) &&
9685 "Invalid ptrauth discriminator");
9686
9687 // Look through ptrauth constants to find the raw callee.
9688 // Do a direct unauthenticated call if we found it and everything matches.
9689 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9690 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9691 DAG.getDataLayout()))
9692 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9693 CB.isMustTailCall(), EHPadBB);
9694
9695 // Functions should never be ptrauth-called directly.
9696 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9697
9698 // Otherwise, do an authenticated indirect call.
9699 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9700 getValue(Discriminator)};
9701
9702 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9703 EHPadBB, &PAI);
9704}
9705
9706namespace {
9707
9708/// AsmOperandInfo - This contains information for each constraint that we are
9709/// lowering.
9710class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9711public:
9712 /// CallOperand - If this is the result output operand or a clobber
9713 /// this is null, otherwise it is the incoming operand to the CallInst.
9714 /// This gets modified as the asm is processed.
9715 SDValue CallOperand;
9716
9717 /// AssignedRegs - If this is a register or register class operand, this
9718 /// contains the set of register corresponding to the operand.
9719 RegsForValue AssignedRegs;
9720
9721 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9722 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9723 }
9724
9725 /// Whether or not this operand accesses memory
9726 bool hasMemory(const TargetLowering &TLI) const {
9727 // Indirect operand accesses access memory.
9728 if (isIndirect)
9729 return true;
9730
9731 for (const auto &Code : Codes)
9733 return true;
9734
9735 return false;
9736 }
9737};
9738
9739
9740} // end anonymous namespace
9741
9742/// Make sure that the output operand \p OpInfo and its corresponding input
9743/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9744/// out).
9745static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9746 SDISelAsmOperandInfo &MatchingOpInfo,
9747 SelectionDAG &DAG) {
9748 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9749 return;
9750
9752 const auto &TLI = DAG.getTargetLoweringInfo();
9753
9754 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9755 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9756 OpInfo.ConstraintVT);
9757 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9758 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9759 MatchingOpInfo.ConstraintVT);
9760 const bool OutOpIsIntOrFP =
9761 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9762 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9763 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9764 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9765 // FIXME: error out in a more elegant fashion
9766 report_fatal_error("Unsupported asm: input constraint"
9767 " with a matching output constraint of"
9768 " incompatible type!");
9769 }
9770 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9771}
9772
9773/// Get a direct memory input to behave well as an indirect operand.
9774/// This may introduce stores, hence the need for a \p Chain.
9775/// \return The (possibly updated) chain.
9776static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9777 SDISelAsmOperandInfo &OpInfo,
9778 SelectionDAG &DAG) {
9779 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9780
9781 // If we don't have an indirect input, put it in the constpool if we can,
9782 // otherwise spill it to a stack slot.
9783 // TODO: This isn't quite right. We need to handle these according to
9784 // the addressing mode that the constraint wants. Also, this may take
9785 // an additional register for the computation and we don't want that
9786 // either.
9787
9788 // If the operand is a float, integer, or vector constant, spill to a
9789 // constant pool entry to get its address.
9790 const Value *OpVal = OpInfo.CallOperandVal;
9791 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9793 OpInfo.CallOperand = DAG.getConstantPool(
9794 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9795 return Chain;
9796 }
9797
9798 // Otherwise, create a stack slot and emit a store to it before the asm.
9799 Type *Ty = OpVal->getType();
9800 auto &DL = DAG.getDataLayout();
9801 TypeSize TySize = DL.getTypeAllocSize(Ty);
9804 int StackID = 0;
9805 if (TySize.isScalable())
9806 StackID = TFI->getStackIDForScalableVectors();
9807 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9808 DL.getPrefTypeAlign(Ty), false,
9809 nullptr, StackID);
9810 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9811 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9813 TLI.getMemValueType(DL, Ty));
9814 OpInfo.CallOperand = StackSlot;
9815
9816 return Chain;
9817}
9818
9819/// GetRegistersForValue - Assign registers (virtual or physical) for the
9820/// specified operand. We prefer to assign virtual registers, to allow the
9821/// register allocator to handle the assignment process. However, if the asm
9822/// uses features that we can't model on machineinstrs, we have SDISel do the
9823/// allocation. This produces generally horrible, but correct, code.
9824///
9825/// OpInfo describes the operand
9826/// RefOpInfo describes the matching operand if any, the operand otherwise
9827static std::optional<unsigned>
9829 SDISelAsmOperandInfo &OpInfo,
9830 SDISelAsmOperandInfo &RefOpInfo) {
9831 LLVMContext &Context = *DAG.getContext();
9832 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9833
9837
9838 // No work to do for memory/address operands.
9839 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9840 OpInfo.ConstraintType == TargetLowering::C_Address)
9841 return std::nullopt;
9842
9843 // If this is a constraint for a single physreg, or a constraint for a
9844 // register class, find it.
9845 unsigned AssignedReg;
9846 const TargetRegisterClass *RC;
9847 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
9848 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
9849 // RC is unset only on failure. Return immediately.
9850 if (!RC)
9851 return std::nullopt;
9852
9853 // Get the actual register value type. This is important, because the user
9854 // may have asked for (e.g.) the AX register in i32 type. We need to
9855 // remember that AX is actually i16 to get the right extension.
9856 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
9857
9858 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
9859 // If this is an FP operand in an integer register (or visa versa), or more
9860 // generally if the operand value disagrees with the register class we plan
9861 // to stick it in, fix the operand type.
9862 //
9863 // If this is an input value, the bitcast to the new type is done now.
9864 // Bitcast for output value is done at the end of visitInlineAsm().
9865 if ((OpInfo.Type == InlineAsm::isOutput ||
9866 OpInfo.Type == InlineAsm::isInput) &&
9867 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
9868 // Try to convert to the first EVT that the reg class contains. If the
9869 // types are identical size, use a bitcast to convert (e.g. two differing
9870 // vector types). Note: output bitcast is done at the end of
9871 // visitInlineAsm().
9872 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
9873 // Exclude indirect inputs while they are unsupported because the code
9874 // to perform the load is missing and thus OpInfo.CallOperand still
9875 // refers to the input address rather than the pointed-to value.
9876 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
9877 OpInfo.CallOperand =
9878 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
9879 OpInfo.ConstraintVT = RegVT;
9880 // If the operand is an FP value and we want it in integer registers,
9881 // use the corresponding integer type. This turns an f64 value into
9882 // i64, which can be passed with two i32 values on a 32-bit machine.
9883 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
9884 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
9885 if (OpInfo.Type == InlineAsm::isInput)
9886 OpInfo.CallOperand =
9887 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
9888 OpInfo.ConstraintVT = VT;
9889 }
9890 }
9891 }
9892
9893 // No need to allocate a matching input constraint since the constraint it's
9894 // matching to has already been allocated.
9895 if (OpInfo.isMatchingInputConstraint())
9896 return std::nullopt;
9897
9898 EVT ValueVT = OpInfo.ConstraintVT;
9899 if (OpInfo.ConstraintVT == MVT::Other)
9900 ValueVT = RegVT;
9901
9902 // Initialize NumRegs.
9903 unsigned NumRegs = 1;
9904 if (OpInfo.ConstraintVT != MVT::Other)
9905 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
9906
9907 // If this is a constraint for a specific physical register, like {r17},
9908 // assign it now.
9909
9910 // If this associated to a specific register, initialize iterator to correct
9911 // place. If virtual, make sure we have enough registers
9912
9913 // Initialize iterator if necessary
9916
9917 // Do not check for single registers.
9918 if (AssignedReg) {
9919 I = std::find(I, RC->end(), AssignedReg);
9920 if (I == RC->end()) {
9921 // RC does not contain the selected register, which indicates a
9922 // mismatch between the register and the required type/bitwidth.
9923 return {AssignedReg};
9924 }
9925 }
9926
9927 for (; NumRegs; --NumRegs, ++I) {
9928 assert(I != RC->end() && "Ran out of registers to allocate!");
9929 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
9930 Regs.push_back(R);
9931 }
9932
9933 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
9934 return std::nullopt;
9935}
9936
9937static unsigned
9939 const std::vector<SDValue> &AsmNodeOperands) {
9940 // Scan until we find the definition we already emitted of this operand.
9941 unsigned CurOp = InlineAsm::Op_FirstOperand;
9942 for (; OperandNo; --OperandNo) {
9943 // Advance to the next operand.
9944 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
9945 const InlineAsm::Flag F(OpFlag);
9946 assert(
9947 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
9948 "Skipped past definitions?");
9949 CurOp += F.getNumOperandRegisters() + 1;
9950 }
9951 return CurOp;
9952}
9953
9954namespace {
9955
9956class ExtraFlags {
9957 unsigned Flags = 0;
9958
9959public:
9960 explicit ExtraFlags(const CallBase &Call) {
9961 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
9962 if (IA->hasSideEffects())
9964 if (IA->isAlignStack())
9966 if (Call.isConvergent())
9968 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
9969 }
9970
9971 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
9972 // Ideally, we would only check against memory constraints. However, the
9973 // meaning of an Other constraint can be target-specific and we can't easily
9974 // reason about it. Therefore, be conservative and set MayLoad/MayStore
9975 // for Other constraints as well.
9978 if (OpInfo.Type == InlineAsm::isInput)
9980 else if (OpInfo.Type == InlineAsm::isOutput)
9982 else if (OpInfo.Type == InlineAsm::isClobber)
9984 }
9985 }
9986
9987 unsigned get() const { return Flags; }
9988};
9989
9990} // end anonymous namespace
9991
9992static bool isFunction(SDValue Op) {
9993 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
9994 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
9995 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
9996
9997 // In normal "call dllimport func" instruction (non-inlineasm) it force
9998 // indirect access by specifing call opcode. And usually specially print
9999 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10000 // not do in this way now. (In fact, this is similar with "Data Access"
10001 // action). So here we ignore dllimport function.
10002 if (Fn && !Fn->hasDLLImportStorageClass())
10003 return true;
10004 }
10005 }
10006 return false;
10007}
10008
10009/// visitInlineAsm - Handle a call to an InlineAsm object.
10010void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10011 const BasicBlock *EHPadBB) {
10012 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10013
10014 /// ConstraintOperands - Information about all of the constraints.
10015 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10016
10017 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10019 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10020
10021 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
10022 // AsmDialect, MayLoad, MayStore).
10023 bool HasSideEffect = IA->hasSideEffects();
10024 ExtraFlags ExtraInfo(Call);
10025
10026 for (auto &T : TargetConstraints) {
10027 ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10028 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
10029
10030 if (OpInfo.CallOperandVal)
10031 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
10032
10033 if (!HasSideEffect)
10034 HasSideEffect = OpInfo.hasMemory(TLI);
10035
10036 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10037 // FIXME: Could we compute this on OpInfo rather than T?
10038
10039 // Compute the constraint code and ConstraintType to use.
10041
10042 if (T.ConstraintType == TargetLowering::C_Immediate &&
10043 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
10044 // We've delayed emitting a diagnostic like the "n" constraint because
10045 // inlining could cause an integer showing up.
10046 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
10047 "' expects an integer constant "
10048 "expression");
10049
10050 ExtraInfo.update(T);
10051 }
10052
10053 // We won't need to flush pending loads if this asm doesn't touch
10054 // memory and is nonvolatile.
10055 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
10056
10057 bool EmitEHLabels = isa<InvokeInst>(Call);
10058 if (EmitEHLabels) {
10059 assert(EHPadBB && "InvokeInst must have an EHPadBB");
10060 }
10061 bool IsCallBr = isa<CallBrInst>(Call);
10062
10063 if (IsCallBr || EmitEHLabels) {
10064 // If this is a callbr or invoke we need to flush pending exports since
10065 // inlineasm_br and invoke are terminators.
10066 // We need to do this before nodes are glued to the inlineasm_br node.
10067 Chain = getControlRoot();
10068 }
10069
10070 MCSymbol *BeginLabel = nullptr;
10071 if (EmitEHLabels) {
10072 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
10073 }
10074
10075 int OpNo = -1;
10076 SmallVector<StringRef> AsmStrs;
10077 IA->collectAsmStrs(AsmStrs);
10078
10079 // Second pass over the constraints: compute which constraint option to use.
10080 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10081 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10082 OpNo++;
10083
10084 // If this is an output operand with a matching input operand, look up the
10085 // matching input. If their types mismatch, e.g. one is an integer, the
10086 // other is floating point, or their sizes are different, flag it as an
10087 // error.
10088 if (OpInfo.hasMatchingInput()) {
10089 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
10090 patchMatchingInput(OpInfo, Input, DAG);
10091 }
10092
10093 // Compute the constraint code and ConstraintType to use.
10094 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10095
10096 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10097 OpInfo.Type == InlineAsm::isClobber) ||
10098 OpInfo.ConstraintType == TargetLowering::C_Address)
10099 continue;
10100
10101 // In Linux PIC model, there are 4 cases about value/label addressing:
10102 //
10103 // 1: Function call or Label jmp inside the module.
10104 // 2: Data access (such as global variable, static variable) inside module.
10105 // 3: Function call or Label jmp outside the module.
10106 // 4: Data access (such as global variable) outside the module.
10107 //
10108 // Due to current llvm inline asm architecture designed to not "recognize"
10109 // the asm code, there are quite troubles for us to treat mem addressing
10110 // differently for same value/adress used in different instuctions.
10111 // For example, in pic model, call a func may in plt way or direclty
10112 // pc-related, but lea/mov a function adress may use got.
10113 //
10114 // Here we try to "recognize" function call for the case 1 and case 3 in
10115 // inline asm. And try to adjust the constraint for them.
10116 //
10117 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10118 // label, so here we don't handle jmp function label now, but we need to
10119 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10120 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10121 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10122 TM.getCodeModel() != CodeModel::Large) {
10123 OpInfo.isIndirect = false;
10124 OpInfo.ConstraintType = TargetLowering::C_Address;
10125 }
10126
10127 // If this is a memory input, and if the operand is not indirect, do what we
10128 // need to provide an address for the memory input.
10129 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10130 !OpInfo.isIndirect) {
10131 assert((OpInfo.isMultipleAlternative ||
10132 (OpInfo.Type == InlineAsm::isInput)) &&
10133 "Can only indirectify direct input operands!");
10134
10135 // Memory operands really want the address of the value.
10136 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
10137
10138 // There is no longer a Value* corresponding to this operand.
10139 OpInfo.CallOperandVal = nullptr;
10140
10141 // It is now an indirect operand.
10142 OpInfo.isIndirect = true;
10143 }
10144
10145 }
10146
10147 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10148 std::vector<SDValue> AsmNodeOperands;
10149 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10150 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10151 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10152
10153 // If we have a !srcloc metadata node associated with it, we want to attach
10154 // this to the ultimately generated inline asm machineinstr. To do this, we
10155 // pass in the third operand as this (potentially null) inline asm MDNode.
10156 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10157 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10158
10159 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10160 // bits as operand 3.
10161 AsmNodeOperands.push_back(DAG.getTargetConstant(
10162 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10163
10164 // Third pass: Loop over operands to prepare DAG-level operands.. As part of
10165 // this, assign virtual and physical registers for inputs and otput.
10166 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10167 // Assign Registers.
10168 SDISelAsmOperandInfo &RefOpInfo =
10169 OpInfo.isMatchingInputConstraint()
10170 ? ConstraintOperands[OpInfo.getMatchedOperand()]
10171 : OpInfo;
10172 const auto RegError =
10173 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
10174 if (RegError) {
10175 const MachineFunction &MF = DAG.getMachineFunction();
10176 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10177 const char *RegName = TRI.getName(*RegError);
10178 emitInlineAsmError(Call, "register '" + Twine(RegName) +
10179 "' allocated for constraint '" +
10180 Twine(OpInfo.ConstraintCode) +
10181 "' does not match required type");
10182 return;
10183 }
10184
10185 auto DetectWriteToReservedRegister = [&]() {
10186 const MachineFunction &MF = DAG.getMachineFunction();
10187 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10188 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10189 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10190 const char *RegName = TRI.getName(Reg);
10191 emitInlineAsmError(Call, "write to reserved register '" +
10192 Twine(RegName) + "'");
10193 return true;
10194 }
10195 }
10196 return false;
10197 };
10198 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10199 (OpInfo.Type == InlineAsm::isInput &&
10200 !OpInfo.isMatchingInputConstraint())) &&
10201 "Only address as input operand is allowed.");
10202
10203 switch (OpInfo.Type) {
10205 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10206 const InlineAsm::ConstraintCode ConstraintID =
10207 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10209 "Failed to convert memory constraint code to constraint id.");
10210
10211 // Add information to the INLINEASM node to know about this output.
10212 InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1);
10213 OpFlags.setMemConstraint(ConstraintID);
10214 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
10215 MVT::i32));
10216 AsmNodeOperands.push_back(OpInfo.CallOperand);
10217 } else {
10218 // Otherwise, this outputs to a register (directly for C_Register /
10219 // C_RegisterClass, and a target-defined fashion for
10220 // C_Immediate/C_Other). Find a register that we can use.
10221 if (OpInfo.AssignedRegs.Regs.empty()) {
10222 emitInlineAsmError(
10223 Call, "couldn't allocate output register for constraint '" +
10224 Twine(OpInfo.ConstraintCode) + "'");
10225 return;
10226 }
10227
10228 if (DetectWriteToReservedRegister())
10229 return;
10230
10231 // Add information to the INLINEASM node to know that this register is
10232 // set.
10233 OpInfo.AssignedRegs.AddInlineAsmOperands(
10234 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10236 false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
10237 }
10238 break;
10239
10240 case InlineAsm::isInput:
10241 case InlineAsm::isLabel: {
10242 SDValue InOperandVal = OpInfo.CallOperand;
10243
10244 if (OpInfo.isMatchingInputConstraint()) {
10245 // If this is required to match an output register we have already set,
10246 // just use its register.
10247 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10248 AsmNodeOperands);
10249 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
10250 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10251 if (OpInfo.isIndirect) {
10252 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10253 emitInlineAsmError(Call, "inline asm not supported yet: "
10254 "don't know how to handle tied "
10255 "indirect register inputs");
10256 return;
10257 }
10258
10260 MachineFunction &MF = DAG.getMachineFunction();
10261 MachineRegisterInfo &MRI = MF.getRegInfo();
10262 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10263 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
10264 Register TiedReg = R->getReg();
10265 MVT RegVT = R->getSimpleValueType(0);
10266 const TargetRegisterClass *RC =
10267 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10268 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10269 : TRI.getMinimalPhysRegClass(TiedReg);
10270 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
10271 Regs.push_back(MRI.createVirtualRegister(RC));
10272
10273 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10274
10275 SDLoc dl = getCurSDLoc();
10276 // Use the produced MatchedRegs object to
10277 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
10278 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
10279 OpInfo.getMatchedOperand(), dl, DAG,
10280 AsmNodeOperands);
10281 break;
10282 }
10283
10284 assert(Flag.isMemKind() && "Unknown matching constraint!");
10285 assert(Flag.getNumOperandRegisters() == 1 &&
10286 "Unexpected number of operands");
10287 // Add information to the INLINEASM node to know about this input.
10288 // See InlineAsm.h isUseOperandTiedToDef.
10289 Flag.clearMemConstraint();
10290 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10291 AsmNodeOperands.push_back(DAG.getTargetConstant(
10292 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10293 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
10294 break;
10295 }
10296
10297 // Treat indirect 'X' constraint as memory.
10298 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10299 OpInfo.isIndirect)
10300 OpInfo.ConstraintType = TargetLowering::C_Memory;
10301
10302 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10303 OpInfo.ConstraintType == TargetLowering::C_Other) {
10304 std::vector<SDValue> Ops;
10305 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10306 Ops, DAG);
10307 if (Ops.empty()) {
10308 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10309 if (isa<ConstantSDNode>(InOperandVal)) {
10310 emitInlineAsmError(Call, "value out of range for constraint '" +
10311 Twine(OpInfo.ConstraintCode) + "'");
10312 return;
10313 }
10314
10315 emitInlineAsmError(Call,
10316 "invalid operand for inline asm constraint '" +
10317 Twine(OpInfo.ConstraintCode) + "'");
10318 return;
10319 }
10320
10321 // Add information to the INLINEASM node to know about this input.
10322 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10323 AsmNodeOperands.push_back(DAG.getTargetConstant(
10324 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10325 llvm::append_range(AsmNodeOperands, Ops);
10326 break;
10327 }
10328
10329 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10330 assert((OpInfo.isIndirect ||
10331 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10332 "Operand must be indirect to be a mem!");
10333 assert(InOperandVal.getValueType() ==
10334 TLI.getPointerTy(DAG.getDataLayout()) &&
10335 "Memory operands expect pointer values");
10336
10337 const InlineAsm::ConstraintCode ConstraintID =
10338 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10340 "Failed to convert memory constraint code to constraint id.");
10341
10342 // Add information to the INLINEASM node to know about this input.
10343 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10344 ResOpType.setMemConstraint(ConstraintID);
10345 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10346 getCurSDLoc(),
10347 MVT::i32));
10348 AsmNodeOperands.push_back(InOperandVal);
10349 break;
10350 }
10351
10352 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10353 const InlineAsm::ConstraintCode ConstraintID =
10354 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10356 "Failed to convert memory constraint code to constraint id.");
10357
10358 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10359
10360 SDValue AsmOp = InOperandVal;
10361 if (isFunction(InOperandVal)) {
10362 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10363 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10364 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10365 InOperandVal.getValueType(),
10366 GA->getOffset());
10367 }
10368
10369 // Add information to the INLINEASM node to know about this input.
10370 ResOpType.setMemConstraint(ConstraintID);
10371
10372 AsmNodeOperands.push_back(
10373 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10374
10375 AsmNodeOperands.push_back(AsmOp);
10376 break;
10377 }
10378
10379 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10380 OpInfo.ConstraintType != TargetLowering::C_Register) {
10381 emitInlineAsmError(Call, "unknown asm constraint '" +
10382 Twine(OpInfo.ConstraintCode) + "'");
10383 return;
10384 }
10385
10386 // TODO: Support this.
10387 if (OpInfo.isIndirect) {
10388 emitInlineAsmError(
10389 Call, "Don't know how to handle indirect register inputs yet "
10390 "for constraint '" +
10391 Twine(OpInfo.ConstraintCode) + "'");
10392 return;
10393 }
10394
10395 // Copy the input into the appropriate registers.
10396 if (OpInfo.AssignedRegs.Regs.empty()) {
10397 emitInlineAsmError(Call,
10398 "couldn't allocate input reg for constraint '" +
10399 Twine(OpInfo.ConstraintCode) + "'");
10400 return;
10401 }
10402
10403 if (DetectWriteToReservedRegister())
10404 return;
10405
10406 SDLoc dl = getCurSDLoc();
10407
10408 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10409 &Call);
10410
10411 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10412 0, dl, DAG, AsmNodeOperands);
10413 break;
10414 }
10416 // Add the clobbered value to the operand list, so that the register
10417 // allocator is aware that the physreg got clobbered.
10418 if (!OpInfo.AssignedRegs.Regs.empty())
10420 false, 0, getCurSDLoc(), DAG,
10421 AsmNodeOperands);
10422 break;
10423 }
10424 }
10425
10426 // Finish up input operands. Set the input chain and add the flag last.
10427 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10428 if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10429
10430 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10431 Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10432 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10433 Glue = Chain.getValue(1);
10434
10435 // Do additional work to generate outputs.
10436
10437 SmallVector<EVT, 1> ResultVTs;
10438 SmallVector<SDValue, 1> ResultValues;
10439 SmallVector<SDValue, 8> OutChains;
10440
10441 llvm::Type *CallResultType = Call.getType();
10442 ArrayRef<Type *> ResultTypes;
10443 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10444 ResultTypes = StructResult->elements();
10445 else if (!CallResultType->isVoidTy())
10446 ResultTypes = ArrayRef(CallResultType);
10447
10448 auto CurResultType = ResultTypes.begin();
10449 auto handleRegAssign = [&](SDValue V) {
10450 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10451 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10452 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10453 ++CurResultType;
10454 // If the type of the inline asm call site return value is different but has
10455 // same size as the type of the asm output bitcast it. One example of this
10456 // is for vectors with different width / number of elements. This can
10457 // happen for register classes that can contain multiple different value
10458 // types. The preg or vreg allocated may not have the same VT as was
10459 // expected.
10460 //
10461 // This can also happen for a return value that disagrees with the register
10462 // class it is put in, eg. a double in a general-purpose register on a
10463 // 32-bit machine.
10464 if (ResultVT != V.getValueType() &&
10465 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10466 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10467 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10468 V.getValueType().isInteger()) {
10469 // If a result value was tied to an input value, the computed result
10470 // may have a wider width than the expected result. Extract the
10471 // relevant portion.
10472 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10473 }
10474 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10475 ResultVTs.push_back(ResultVT);
10476 ResultValues.push_back(V);
10477 };
10478
10479 // Deal with output operands.
10480 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10481 if (OpInfo.Type == InlineAsm::isOutput) {
10482 SDValue Val;
10483 // Skip trivial output operands.
10484 if (OpInfo.AssignedRegs.Regs.empty())
10485 continue;
10486
10487 switch (OpInfo.ConstraintType) {
10490 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10491 Chain, &Glue, &Call);
10492 break;
10495 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10496 OpInfo, DAG);
10497 break;
10499 break; // Already handled.
10501 break; // Silence warning.
10503 assert(false && "Unexpected unknown constraint");
10504 }
10505
10506 // Indirect output manifest as stores. Record output chains.
10507 if (OpInfo.isIndirect) {
10508 const Value *Ptr = OpInfo.CallOperandVal;
10509 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10510 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10511 MachinePointerInfo(Ptr));
10512 OutChains.push_back(Store);
10513 } else {
10514 // generate CopyFromRegs to associated registers.
10515 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10516 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10517 for (const SDValue &V : Val->op_values())
10518 handleRegAssign(V);
10519 } else
10520 handleRegAssign(Val);
10521 }
10522 }
10523 }
10524
10525 // Set results.
10526 if (!ResultValues.empty()) {
10527 assert(CurResultType == ResultTypes.end() &&
10528 "Mismatch in number of ResultTypes");
10529 assert(ResultValues.size() == ResultTypes.size() &&
10530 "Mismatch in number of output operands in asm result");
10531
10533 DAG.getVTList(ResultVTs), ResultValues);
10534 setValue(&Call, V);
10535 }
10536
10537 // Collect store chains.
10538 if (!OutChains.empty())
10539 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10540
10541 if (EmitEHLabels) {
10542 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10543 }
10544
10545 // Only Update Root if inline assembly has a memory effect.
10546 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10547 EmitEHLabels)
10548 DAG.setRoot(Chain);
10549}
10550
10551void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10552 const Twine &Message) {
10553 LLVMContext &Ctx = *DAG.getContext();
10554 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10555
10556 // Make sure we leave the DAG in a valid state
10557 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10558 SmallVector<EVT, 1> ValueVTs;
10559 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10560
10561 if (ValueVTs.empty())
10562 return;
10563
10565 for (const EVT &VT : ValueVTs)
10566 Ops.push_back(DAG.getUNDEF(VT));
10567
10568 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10569}
10570
10571void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10572 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10573 MVT::Other, getRoot(),
10574 getValue(I.getArgOperand(0)),
10575 DAG.getSrcValue(I.getArgOperand(0))));
10576}
10577
10578void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10579 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10580 const DataLayout &DL = DAG.getDataLayout();
10581 SDValue V = DAG.getVAArg(
10582 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10583 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10584 DL.getABITypeAlign(I.getType()).value());
10585 DAG.setRoot(V.getValue(1));
10586
10587 if (I.getType()->isPointerTy())
10588 V = DAG.getPtrExtOrTrunc(
10589 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10590 setValue(&I, V);
10591}
10592
10593void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10594 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10595 MVT::Other, getRoot(),
10596 getValue(I.getArgOperand(0)),
10597 DAG.getSrcValue(I.getArgOperand(0))));
10598}
10599
10600void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10601 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10602 MVT::Other, getRoot(),
10603 getValue(I.getArgOperand(0)),
10604 getValue(I.getArgOperand(1)),
10605 DAG.getSrcValue(I.getArgOperand(0)),
10606 DAG.getSrcValue(I.getArgOperand(1))));
10607}
10608
10610 const Instruction &I,
10611 SDValue Op) {
10612 std::optional<ConstantRange> CR = getRange(I);
10613
10614 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10615 return Op;
10616
10617 APInt Lo = CR->getUnsignedMin();
10618 if (!Lo.isMinValue())
10619 return Op;
10620
10621 APInt Hi = CR->getUnsignedMax();
10622 unsigned Bits = std::max(Hi.getActiveBits(),
10623 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10624
10625 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10626
10627 SDLoc SL = getCurSDLoc();
10628
10629 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10630 DAG.getValueType(SmallVT));
10631 unsigned NumVals = Op.getNode()->getNumValues();
10632 if (NumVals == 1)
10633 return ZExt;
10634
10636
10637 Ops.push_back(ZExt);
10638 for (unsigned I = 1; I != NumVals; ++I)
10639 Ops.push_back(Op.getValue(I));
10640
10641 return DAG.getMergeValues(Ops, SL);
10642}
10643
10644/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10645/// the call being lowered.
10646///
10647/// This is a helper for lowering intrinsics that follow a target calling
10648/// convention or require stack pointer adjustment. Only a subset of the
10649/// intrinsic's operands need to participate in the calling convention.
10652 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10653 AttributeSet RetAttrs, bool IsPatchPoint) {
10655 Args.reserve(NumArgs);
10656
10657 // Populate the argument list.
10658 // Attributes for args start at offset 1, after the return attribute.
10659 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10660 ArgI != ArgE; ++ArgI) {
10661 const Value *V = Call->getOperand(ArgI);
10662
10663 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10664
10665 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10666 Entry.setAttributes(Call, ArgI);
10667 Args.push_back(Entry);
10668 }
10669
10671 .setChain(getRoot())
10672 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10673 RetAttrs)
10674 .setDiscardResult(Call->use_empty())
10675 .setIsPatchPoint(IsPatchPoint)
10677 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10678}
10679
10680/// Add a stack map intrinsic call's live variable operands to a stackmap
10681/// or patchpoint target node's operand list.
10682///
10683/// Constants are converted to TargetConstants purely as an optimization to
10684/// avoid constant materialization and register allocation.
10685///
10686/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10687/// generate addess computation nodes, and so FinalizeISel can convert the
10688/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10689/// address materialization and register allocation, but may also be required
10690/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10691/// alloca in the entry block, then the runtime may assume that the alloca's
10692/// StackMap location can be read immediately after compilation and that the
10693/// location is valid at any point during execution (this is similar to the
10694/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10695/// only available in a register, then the runtime would need to trap when
10696/// execution reaches the StackMap in order to read the alloca's location.
10697static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10699 SelectionDAGBuilder &Builder) {
10700 SelectionDAG &DAG = Builder.DAG;
10701 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10702 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10703
10704 // Things on the stack are pointer-typed, meaning that they are already
10705 // legal and can be emitted directly to target nodes.
10707 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10708 } else {
10709 // Otherwise emit a target independent node to be legalised.
10710 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10711 }
10712 }
10713}
10714
10715/// Lower llvm.experimental.stackmap.
10716void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10717 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10718 // [live variables...])
10719
10720 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10721
10722 SDValue Chain, InGlue, Callee;
10724
10725 SDLoc DL = getCurSDLoc();
10727
10728 // The stackmap intrinsic only records the live variables (the arguments
10729 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10730 // intrinsic, this won't be lowered to a function call. This means we don't
10731 // have to worry about calling conventions and target specific lowering code.
10732 // Instead we perform the call lowering right here.
10733 //
10734 // chain, flag = CALLSEQ_START(chain, 0, 0)
10735 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10736 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10737 //
10738 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10739 InGlue = Chain.getValue(1);
10740
10741 // Add the STACKMAP operands, starting with DAG house-keeping.
10742 Ops.push_back(Chain);
10743 Ops.push_back(InGlue);
10744
10745 // Add the <id>, <numShadowBytes> operands.
10746 //
10747 // These do not require legalisation, and can be emitted directly to target
10748 // constant nodes.
10750 assert(ID.getValueType() == MVT::i64);
10751 SDValue IDConst =
10752 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10753 Ops.push_back(IDConst);
10754
10755 SDValue Shad = getValue(CI.getArgOperand(1));
10756 assert(Shad.getValueType() == MVT::i32);
10757 SDValue ShadConst =
10758 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
10759 Ops.push_back(ShadConst);
10760
10761 // Add the live variables.
10762 addStackMapLiveVars(CI, 2, DL, Ops, *this);
10763
10764 // Create the STACKMAP node.
10765 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10766 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10767 InGlue = Chain.getValue(1);
10768
10769 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10770
10771 // Stackmaps don't generate values, so nothing goes into the NodeMap.
10772
10773 // Set the root to the target-lowered call chain.
10774 DAG.setRoot(Chain);
10775
10776 // Inform the Frame Information that we have a stackmap in this function.
10777 FuncInfo.MF->getFrameInfo().setHasStackMap();
10778}
10779
10780/// Lower llvm.experimental.patchpoint directly to its target opcode.
10781void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10782 const BasicBlock *EHPadBB) {
10783 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10784 // i32 <numBytes>,
10785 // i8* <target>,
10786 // i32 <numArgs>,
10787 // [Args...],
10788 // [live variables...])
10789
10791 bool IsAnyRegCC = CC == CallingConv::AnyReg;
10792 bool HasDef = !CB.getType()->isVoidTy();
10793 SDLoc dl = getCurSDLoc();
10795
10796 // Handle immediate and symbolic callees.
10797 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10798 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10799 /*isTarget=*/true);
10800 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
10801 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
10802 SDLoc(SymbolicCallee),
10803 SymbolicCallee->getValueType(0));
10804
10805 // Get the real number of arguments participating in the call <numArgs>
10807 unsigned NumArgs = NArgVal->getAsZExtVal();
10808
10809 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
10810 // Intrinsics include all meta-operands up to but not including CC.
10811 unsigned NumMetaOpers = PatchPointOpers::CCPos;
10812 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
10813 "Not enough arguments provided to the patchpoint intrinsic");
10814
10815 // For AnyRegCC the arguments are lowered later on manually.
10816 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
10817 Type *ReturnTy =
10818 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
10819
10820 TargetLowering::CallLoweringInfo CLI(DAG);
10821 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
10822 ReturnTy, CB.getAttributes().getRetAttrs(), true);
10823 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
10824
10825 SDNode *CallEnd = Result.second.getNode();
10826 if (CallEnd->getOpcode() == ISD::EH_LABEL)
10827 CallEnd = CallEnd->getOperand(0).getNode();
10828 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
10829 CallEnd = CallEnd->getOperand(0).getNode();
10830
10831 /// Get a call instruction from the call sequence chain.
10832 /// Tail calls are not allowed.
10833 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
10834 "Expected a callseq node.");
10835 SDNode *Call = CallEnd->getOperand(0).getNode();
10836 bool HasGlue = Call->getGluedNode();
10837
10838 // Replace the target specific call node with the patchable intrinsic.
10840
10841 // Push the chain.
10842 Ops.push_back(*(Call->op_begin()));
10843
10844 // Optionally, push the glue (if any).
10845 if (HasGlue)
10846 Ops.push_back(*(Call->op_end() - 1));
10847
10848 // Push the register mask info.
10849 if (HasGlue)
10850 Ops.push_back(*(Call->op_end() - 2));
10851 else
10852 Ops.push_back(*(Call->op_end() - 1));
10853
10854 // Add the <id> and <numBytes> constants.
10856 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
10858 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
10859
10860 // Add the callee.
10861 Ops.push_back(Callee);
10862
10863 // Adjust <numArgs> to account for any arguments that have been passed on the
10864 // stack instead.
10865 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
10866 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
10867 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
10868 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
10869
10870 // Add the calling convention
10871 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
10872
10873 // Add the arguments we omitted previously. The register allocator should
10874 // place these in any free register.
10875 if (IsAnyRegCC)
10876 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
10877 Ops.push_back(getValue(CB.getArgOperand(i)));
10878
10879 // Push the arguments from the call instruction.
10880 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
10881 Ops.append(Call->op_begin() + 2, e);
10882
10883 // Push live variables for the stack map.
10884 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
10885
10886 SDVTList NodeTys;
10887 if (IsAnyRegCC && HasDef) {
10888 // Create the return types based on the intrinsic definition
10889 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10890 SmallVector<EVT, 3> ValueVTs;
10891 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
10892 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
10893
10894 // There is always a chain and a glue type at the end
10895 ValueVTs.push_back(MVT::Other);
10896 ValueVTs.push_back(MVT::Glue);
10897 NodeTys = DAG.getVTList(ValueVTs);
10898 } else
10899 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10900
10901 // Replace the target specific call node with a PATCHPOINT node.
10902 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
10903
10904 // Update the NodeMap.
10905 if (HasDef) {
10906 if (IsAnyRegCC)
10907 setValue(&CB, SDValue(PPV.getNode(), 0));
10908 else
10909 setValue(&CB, Result.first);
10910 }
10911
10912 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
10913 // call sequence. Furthermore the location of the chain and glue can change
10914 // when the AnyReg calling convention is used and the intrinsic returns a
10915 // value.
10916 if (IsAnyRegCC && HasDef) {
10917 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
10918 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
10919 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
10920 } else
10921 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
10922 DAG.DeleteNode(Call);
10923
10924 // Inform the Frame Information that we have a patchpoint in this function.
10925 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
10926}
10927
10928void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
10929 unsigned Intrinsic) {
10930 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10931 SDValue Op1 = getValue(I.getArgOperand(0));
10932 SDValue Op2;
10933 if (I.arg_size() > 1)
10934 Op2 = getValue(I.getArgOperand(1));
10935 SDLoc dl = getCurSDLoc();
10936 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
10937 SDValue Res;
10938 SDNodeFlags SDFlags;
10939 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
10940 SDFlags.copyFMF(*FPMO);
10941
10942 switch (Intrinsic) {
10943 case Intrinsic::vector_reduce_fadd:
10944 if (SDFlags.hasAllowReassociation())
10945 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
10946 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
10947 SDFlags);
10948 else
10949 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
10950 break;
10951 case Intrinsic::vector_reduce_fmul:
10952 if (SDFlags.hasAllowReassociation())
10953 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
10954 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
10955 SDFlags);
10956 else
10957 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
10958 break;
10959 case Intrinsic::vector_reduce_add:
10960 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
10961 break;
10962 case Intrinsic::vector_reduce_mul:
10963 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
10964 break;
10965 case Intrinsic::vector_reduce_and:
10966 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
10967 break;
10968 case Intrinsic::vector_reduce_or:
10969 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
10970 break;
10971 case Intrinsic::vector_reduce_xor:
10972 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
10973 break;
10974 case Intrinsic::vector_reduce_smax:
10975 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
10976 break;
10977 case Intrinsic::vector_reduce_smin:
10978 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
10979 break;
10980 case Intrinsic::vector_reduce_umax:
10981 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
10982 break;
10983 case Intrinsic::vector_reduce_umin:
10984 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
10985 break;
10986 case Intrinsic::vector_reduce_fmax:
10987 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
10988 break;
10989 case Intrinsic::vector_reduce_fmin:
10990 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
10991 break;
10992 case Intrinsic::vector_reduce_fmaximum:
10993 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
10994 break;
10995 case Intrinsic::vector_reduce_fminimum:
10996 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
10997 break;
10998 default:
10999 llvm_unreachable("Unhandled vector reduce intrinsic");
11000 }
11001 setValue(&I, Res);
11002}
11003
11004/// Returns an AttributeList representing the attributes applied to the return
11005/// value of the given call.
11008 if (CLI.RetSExt)
11009 Attrs.push_back(Attribute::SExt);
11010 if (CLI.RetZExt)
11011 Attrs.push_back(Attribute::ZExt);
11012 if (CLI.IsInReg)
11013 Attrs.push_back(Attribute::InReg);
11014
11015 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11016 Attrs);
11017}
11018
11019/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11020/// implementation, which just calls LowerCall.
11021/// FIXME: When all targets are
11022/// migrated to using LowerCall, this hook should be integrated into SDISel.
11023std::pair<SDValue, SDValue>
11025 LLVMContext &Context = CLI.RetTy->getContext();
11026
11027 // Handle the incoming return values from the call.
11028 CLI.Ins.clear();
11029 SmallVector<Type *, 4> RetOrigTys;
11031 auto &DL = CLI.DAG.getDataLayout();
11032 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11033
11034 SmallVector<EVT, 4> RetVTs;
11035 if (CLI.RetTy != CLI.OrigRetTy) {
11036 assert(RetOrigTys.size() == 1 &&
11037 "Only supported for non-aggregate returns");
11038 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11039 } else {
11040 for (Type *Ty : RetOrigTys)
11041 RetVTs.push_back(getValueType(DL, Ty));
11042 }
11043
11044 if (CLI.IsPostTypeLegalization) {
11045 // If we are lowering a libcall after legalization, split the return type.
11046 SmallVector<Type *, 4> OldRetOrigTys;
11047 SmallVector<EVT, 4> OldRetVTs;
11048 SmallVector<TypeSize, 4> OldOffsets;
11049 RetOrigTys.swap(OldRetOrigTys);
11050 RetVTs.swap(OldRetVTs);
11051 Offsets.swap(OldOffsets);
11052
11053 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11054 EVT RetVT = OldRetVTs[i];
11055 uint64_t Offset = OldOffsets[i];
11056 MVT RegisterVT = getRegisterType(Context, RetVT);
11057 unsigned NumRegs = getNumRegisters(Context, RetVT);
11058 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11059 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11060 RetVTs.append(NumRegs, RegisterVT);
11061 for (unsigned j = 0; j != NumRegs; ++j)
11062 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11063 }
11064 }
11065
11067 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11068
11069 bool CanLowerReturn =
11071 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11072
11073 SDValue DemoteStackSlot;
11074 int DemoteStackIdx = -100;
11075 if (!CanLowerReturn) {
11076 // FIXME: equivalent assert?
11077 // assert(!CS.hasInAllocaArgument() &&
11078 // "sret demotion is incompatible with inalloca");
11079 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11080 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11082 DemoteStackIdx =
11083 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11084 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11085
11086 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11087 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11088 Entry.IsSRet = true;
11089 Entry.Alignment = Alignment;
11090 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11091 CLI.NumFixedArgs += 1;
11092 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11093 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11094
11095 // sret demotion isn't compatible with tail-calls, since the sret argument
11096 // points into the callers stack frame.
11097 CLI.IsTailCall = false;
11098 } else {
11099 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11100 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11101 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11102 ISD::ArgFlagsTy Flags;
11103 if (NeedsRegBlock) {
11104 Flags.setInConsecutiveRegs();
11105 if (I == RetVTs.size() - 1)
11106 Flags.setInConsecutiveRegsLast();
11107 }
11108 EVT VT = RetVTs[I];
11109 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11110 unsigned NumRegs =
11111 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11112 for (unsigned i = 0; i != NumRegs; ++i) {
11113 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11115 if (CLI.RetTy->isPointerTy()) {
11116 Ret.Flags.setPointer();
11117 Ret.Flags.setPointerAddrSpace(
11118 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11119 }
11120 if (CLI.RetSExt)
11121 Ret.Flags.setSExt();
11122 if (CLI.RetZExt)
11123 Ret.Flags.setZExt();
11124 if (CLI.IsInReg)
11125 Ret.Flags.setInReg();
11126 CLI.Ins.push_back(Ret);
11127 }
11128 }
11129 }
11130
11131 // We push in swifterror return as the last element of CLI.Ins.
11132 ArgListTy &Args = CLI.getArgs();
11133 if (supportSwiftError()) {
11134 for (const ArgListEntry &Arg : Args) {
11135 if (Arg.IsSwiftError) {
11136 ISD::ArgFlagsTy Flags;
11137 Flags.setSwiftError();
11139 PointerType::getUnqual(Context),
11140 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11141 CLI.Ins.push_back(Ret);
11142 }
11143 }
11144 }
11145
11146 // Handle all of the outgoing arguments.
11147 CLI.Outs.clear();
11148 CLI.OutVals.clear();
11149 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11150 SmallVector<Type *, 4> OrigArgTys;
11151 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11152 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11153 Type *FinalType = Args[i].Ty;
11154 if (Args[i].IsByVal)
11155 FinalType = Args[i].IndirectType;
11156 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11157 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11158 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11159 ++Value) {
11160 Type *OrigArgTy = OrigArgTys[Value];
11161 Type *ArgTy = OrigArgTy;
11162 if (Args[i].Ty != Args[i].OrigTy) {
11163 assert(Value == 0 && "Only supported for non-aggregate arguments");
11164 ArgTy = Args[i].Ty;
11165 }
11166
11167 EVT VT = getValueType(DL, ArgTy);
11168 SDValue Op = SDValue(Args[i].Node.getNode(),
11169 Args[i].Node.getResNo() + Value);
11170 ISD::ArgFlagsTy Flags;
11171
11172 // Certain targets (such as MIPS), may have a different ABI alignment
11173 // for a type depending on the context. Give the target a chance to
11174 // specify the alignment it wants.
11175 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11176 Flags.setOrigAlign(OriginalAlignment);
11177
11178 if (i >= CLI.NumFixedArgs)
11179 Flags.setVarArg();
11180 if (ArgTy->isPointerTy()) {
11181 Flags.setPointer();
11182 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11183 }
11184 if (Args[i].IsZExt)
11185 Flags.setZExt();
11186 if (Args[i].IsSExt)
11187 Flags.setSExt();
11188 if (Args[i].IsNoExt)
11189 Flags.setNoExt();
11190 if (Args[i].IsInReg) {
11191 // If we are using vectorcall calling convention, a structure that is
11192 // passed InReg - is surely an HVA
11194 isa<StructType>(FinalType)) {
11195 // The first value of a structure is marked
11196 if (0 == Value)
11197 Flags.setHvaStart();
11198 Flags.setHva();
11199 }
11200 // Set InReg Flag
11201 Flags.setInReg();
11202 }
11203 if (Args[i].IsSRet)
11204 Flags.setSRet();
11205 if (Args[i].IsSwiftSelf)
11206 Flags.setSwiftSelf();
11207 if (Args[i].IsSwiftAsync)
11208 Flags.setSwiftAsync();
11209 if (Args[i].IsSwiftError)
11210 Flags.setSwiftError();
11211 if (Args[i].IsCFGuardTarget)
11212 Flags.setCFGuardTarget();
11213 if (Args[i].IsByVal)
11214 Flags.setByVal();
11215 if (Args[i].IsByRef)
11216 Flags.setByRef();
11217 if (Args[i].IsPreallocated) {
11218 Flags.setPreallocated();
11219 // Set the byval flag for CCAssignFn callbacks that don't know about
11220 // preallocated. This way we can know how many bytes we should've
11221 // allocated and how many bytes a callee cleanup function will pop. If
11222 // we port preallocated to more targets, we'll have to add custom
11223 // preallocated handling in the various CC lowering callbacks.
11224 Flags.setByVal();
11225 }
11226 if (Args[i].IsInAlloca) {
11227 Flags.setInAlloca();
11228 // Set the byval flag for CCAssignFn callbacks that don't know about
11229 // inalloca. This way we can know how many bytes we should've allocated
11230 // and how many bytes a callee cleanup function will pop. If we port
11231 // inalloca to more targets, we'll have to add custom inalloca handling
11232 // in the various CC lowering callbacks.
11233 Flags.setByVal();
11234 }
11235 Align MemAlign;
11236 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11237 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11238 Flags.setByValSize(FrameSize);
11239
11240 // info is not there but there are cases it cannot get right.
11241 if (auto MA = Args[i].Alignment)
11242 MemAlign = *MA;
11243 else
11244 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11245 } else if (auto MA = Args[i].Alignment) {
11246 MemAlign = *MA;
11247 } else {
11248 MemAlign = OriginalAlignment;
11249 }
11250 Flags.setMemAlign(MemAlign);
11251 if (Args[i].IsNest)
11252 Flags.setNest();
11253 if (NeedsRegBlock)
11254 Flags.setInConsecutiveRegs();
11255
11256 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11257 unsigned NumParts =
11258 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11259 SmallVector<SDValue, 4> Parts(NumParts);
11260 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11261
11262 if (Args[i].IsSExt)
11263 ExtendKind = ISD::SIGN_EXTEND;
11264 else if (Args[i].IsZExt)
11265 ExtendKind = ISD::ZERO_EXTEND;
11266
11267 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11268 // for now.
11269 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11271 assert((CLI.RetTy == Args[i].Ty ||
11272 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11274 Args[i].Ty->getPointerAddressSpace())) &&
11275 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11276 // Before passing 'returned' to the target lowering code, ensure that
11277 // either the register MVT and the actual EVT are the same size or that
11278 // the return value and argument are extended in the same way; in these
11279 // cases it's safe to pass the argument register value unchanged as the
11280 // return register value (although it's at the target's option whether
11281 // to do so)
11282 // TODO: allow code generation to take advantage of partially preserved
11283 // registers rather than clobbering the entire register when the
11284 // parameter extension method is not compatible with the return
11285 // extension method
11286 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11287 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11288 CLI.RetZExt == Args[i].IsZExt))
11289 Flags.setReturned();
11290 }
11291
11292 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11293 CLI.CallConv, ExtendKind);
11294
11295 for (unsigned j = 0; j != NumParts; ++j) {
11296 // if it isn't first piece, alignment must be 1
11297 // For scalable vectors the scalable part is currently handled
11298 // by individual targets, so we just use the known minimum size here.
11299 ISD::OutputArg MyFlags(
11300 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11301 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11302 if (NumParts > 1 && j == 0)
11303 MyFlags.Flags.setSplit();
11304 else if (j != 0) {
11305 MyFlags.Flags.setOrigAlign(Align(1));
11306 if (j == NumParts - 1)
11307 MyFlags.Flags.setSplitEnd();
11308 }
11309
11310 CLI.Outs.push_back(MyFlags);
11311 CLI.OutVals.push_back(Parts[j]);
11312 }
11313
11314 if (NeedsRegBlock && Value == NumValues - 1)
11315 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11316 }
11317 }
11318
11320 CLI.Chain = LowerCall(CLI, InVals);
11321
11322 // Update CLI.InVals to use outside of this function.
11323 CLI.InVals = InVals;
11324
11325 // Verify that the target's LowerCall behaved as expected.
11326 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11327 "LowerCall didn't return a valid chain!");
11328 assert((!CLI.IsTailCall || InVals.empty()) &&
11329 "LowerCall emitted a return value for a tail call!");
11330 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11331 "LowerCall didn't emit the correct number of values!");
11332
11333 // For a tail call, the return value is merely live-out and there aren't
11334 // any nodes in the DAG representing it. Return a special value to
11335 // indicate that a tail call has been emitted and no more Instructions
11336 // should be processed in the current block.
11337 if (CLI.IsTailCall) {
11338 CLI.DAG.setRoot(CLI.Chain);
11339 return std::make_pair(SDValue(), SDValue());
11340 }
11341
11342#ifndef NDEBUG
11343 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11344 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11345 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11346 "LowerCall emitted a value with the wrong type!");
11347 }
11348#endif
11349
11350 SmallVector<SDValue, 4> ReturnValues;
11351 if (!CanLowerReturn) {
11352 // The instruction result is the result of loading from the
11353 // hidden sret parameter.
11354 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11355
11356 unsigned NumValues = RetVTs.size();
11357 ReturnValues.resize(NumValues);
11358 SmallVector<SDValue, 4> Chains(NumValues);
11359
11360 // An aggregate return value cannot wrap around the address space, so
11361 // offsets to its parts don't wrap either.
11363 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11364 for (unsigned i = 0; i < NumValues; ++i) {
11366 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11368 SDValue L = CLI.DAG.getLoad(
11369 RetVTs[i], CLI.DL, CLI.Chain, Add,
11371 DemoteStackIdx, Offsets[i]),
11372 HiddenSRetAlign);
11373 ReturnValues[i] = L;
11374 Chains[i] = L.getValue(1);
11375 }
11376
11377 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11378 } else {
11379 // Collect the legal value parts into potentially illegal values
11380 // that correspond to the original function's return values.
11381 std::optional<ISD::NodeType> AssertOp;
11382 if (CLI.RetSExt)
11383 AssertOp = ISD::AssertSext;
11384 else if (CLI.RetZExt)
11385 AssertOp = ISD::AssertZext;
11386 unsigned CurReg = 0;
11387 for (EVT VT : RetVTs) {
11388 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11389 unsigned NumRegs =
11390 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11391
11392 ReturnValues.push_back(getCopyFromParts(
11393 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11394 CLI.Chain, CLI.CallConv, AssertOp));
11395 CurReg += NumRegs;
11396 }
11397
11398 // For a function returning void, there is no return value. We can't create
11399 // such a node, so we just return a null return value in that case. In
11400 // that case, nothing will actually look at the value.
11401 if (ReturnValues.empty())
11402 return std::make_pair(SDValue(), CLI.Chain);
11403 }
11404
11405 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11406 CLI.DAG.getVTList(RetVTs), ReturnValues);
11407 return std::make_pair(Res, CLI.Chain);
11408}
11409
11410/// Places new result values for the node in Results (their number
11411/// and types must exactly match those of the original return values of
11412/// the node), or leaves Results empty, which indicates that the node is not
11413/// to be custom lowered after all.
11416 SelectionDAG &DAG) const {
11417 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11418
11419 if (!Res.getNode())
11420 return;
11421
11422 // If the original node has one result, take the return value from
11423 // LowerOperation as is. It might not be result number 0.
11424 if (N->getNumValues() == 1) {
11425 Results.push_back(Res);
11426 return;
11427 }
11428
11429 // If the original node has multiple results, then the return node should
11430 // have the same number of results.
11431 assert((N->getNumValues() == Res->getNumValues()) &&
11432 "Lowering returned the wrong number of results!");
11433
11434 // Places new result values base on N result number.
11435 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11436 Results.push_back(Res.getValue(I));
11437}
11438
11440 llvm_unreachable("LowerOperation not implemented for this target!");
11441}
11442
11444 Register Reg,
11445 ISD::NodeType ExtendType) {
11447 assert((Op.getOpcode() != ISD::CopyFromReg ||
11448 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11449 "Copy from a reg to the same reg!");
11450 assert(!Reg.isPhysical() && "Is a physreg");
11451
11452 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11453 // If this is an InlineAsm we have to match the registers required, not the
11454 // notional registers required by the type.
11455
11456 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11457 std::nullopt); // This is not an ABI copy.
11458 SDValue Chain = DAG.getEntryNode();
11459
11460 if (ExtendType == ISD::ANY_EXTEND) {
11461 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11462 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11463 ExtendType = PreferredExtendIt->second;
11464 }
11465 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11466 PendingExports.push_back(Chain);
11467}
11468
11470
11471/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11472/// entry block, return true. This includes arguments used by switches, since
11473/// the switch may expand into multiple basic blocks.
11474static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11475 // With FastISel active, we may be splitting blocks, so force creation
11476 // of virtual registers for all non-dead arguments.
11477 if (FastISel)
11478 return A->use_empty();
11479
11480 const BasicBlock &Entry = A->getParent()->front();
11481 for (const User *U : A->users())
11482 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11483 return false; // Use not in entry block.
11484
11485 return true;
11486}
11487
11489 DenseMap<const Argument *,
11490 std::pair<const AllocaInst *, const StoreInst *>>;
11491
11492/// Scan the entry block of the function in FuncInfo for arguments that look
11493/// like copies into a local alloca. Record any copied arguments in
11494/// ArgCopyElisionCandidates.
11495static void
11497 FunctionLoweringInfo *FuncInfo,
11498 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11499 // Record the state of every static alloca used in the entry block. Argument
11500 // allocas are all used in the entry block, so we need approximately as many
11501 // entries as we have arguments.
11502 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11504 unsigned NumArgs = FuncInfo->Fn->arg_size();
11505 StaticAllocas.reserve(NumArgs * 2);
11506
11507 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11508 if (!V)
11509 return nullptr;
11510 V = V->stripPointerCasts();
11511 const auto *AI = dyn_cast<AllocaInst>(V);
11512 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11513 return nullptr;
11514 auto Iter = StaticAllocas.insert({AI, Unknown});
11515 return &Iter.first->second;
11516 };
11517
11518 // Look for stores of arguments to static allocas. Look through bitcasts and
11519 // GEPs to handle type coercions, as long as the alloca is fully initialized
11520 // by the store. Any non-store use of an alloca escapes it and any subsequent
11521 // unanalyzed store might write it.
11522 // FIXME: Handle structs initialized with multiple stores.
11523 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11524 // Look for stores, and handle non-store uses conservatively.
11525 const auto *SI = dyn_cast<StoreInst>(&I);
11526 if (!SI) {
11527 // We will look through cast uses, so ignore them completely.
11528 if (I.isCast())
11529 continue;
11530 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11531 // to allocas.
11532 if (I.isDebugOrPseudoInst())
11533 continue;
11534 // This is an unknown instruction. Assume it escapes or writes to all
11535 // static alloca operands.
11536 for (const Use &U : I.operands()) {
11537 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11538 *Info = StaticAllocaInfo::Clobbered;
11539 }
11540 continue;
11541 }
11542
11543 // If the stored value is a static alloca, mark it as escaped.
11544 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11545 *Info = StaticAllocaInfo::Clobbered;
11546
11547 // Check if the destination is a static alloca.
11548 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11549 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11550 if (!Info)
11551 continue;
11552 const AllocaInst *AI = cast<AllocaInst>(Dst);
11553
11554 // Skip allocas that have been initialized or clobbered.
11555 if (*Info != StaticAllocaInfo::Unknown)
11556 continue;
11557
11558 // Check if the stored value is an argument, and that this store fully
11559 // initializes the alloca.
11560 // If the argument type has padding bits we can't directly forward a pointer
11561 // as the upper bits may contain garbage.
11562 // Don't elide copies from the same argument twice.
11563 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11564 const auto *Arg = dyn_cast<Argument>(Val);
11565 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11566 Arg->getType()->isEmptyTy() ||
11567 DL.getTypeStoreSize(Arg->getType()) !=
11568 DL.getTypeAllocSize(AI->getAllocatedType()) ||
11569 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11570 ArgCopyElisionCandidates.count(Arg)) {
11571 *Info = StaticAllocaInfo::Clobbered;
11572 continue;
11573 }
11574
11575 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11576 << '\n');
11577
11578 // Mark this alloca and store for argument copy elision.
11579 *Info = StaticAllocaInfo::Elidable;
11580 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11581
11582 // Stop scanning if we've seen all arguments. This will happen early in -O0
11583 // builds, which is useful, because -O0 builds have large entry blocks and
11584 // many allocas.
11585 if (ArgCopyElisionCandidates.size() == NumArgs)
11586 break;
11587 }
11588}
11589
11590/// Try to elide argument copies from memory into a local alloca. Succeeds if
11591/// ArgVal is a load from a suitable fixed stack object.
11594 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11595 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11596 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11597 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11598 // Check if this is a load from a fixed stack object.
11599 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11600 if (!LNode)
11601 return;
11602 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11603 if (!FINode)
11604 return;
11605
11606 // Check that the fixed stack object is the right size and alignment.
11607 // Look at the alignment that the user wrote on the alloca instead of looking
11608 // at the stack object.
11609 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11610 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11611 const AllocaInst *AI = ArgCopyIter->second.first;
11612 int FixedIndex = FINode->getIndex();
11613 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11614 int OldIndex = AllocaIndex;
11615 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11616 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11617 LLVM_DEBUG(
11618 dbgs() << " argument copy elision failed due to bad fixed stack "
11619 "object size\n");
11620 return;
11621 }
11622 Align RequiredAlignment = AI->getAlign();
11623 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11624 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11625 "greater than stack argument alignment ("
11626 << DebugStr(RequiredAlignment) << " vs "
11627 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11628 return;
11629 }
11630
11631 // Perform the elision. Delete the old stack object and replace its only use
11632 // in the variable info map. Mark the stack object as mutable and aliased.
11633 LLVM_DEBUG({
11634 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11635 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11636 << '\n';
11637 });
11638 MFI.RemoveStackObject(OldIndex);
11639 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11640 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11641 AllocaIndex = FixedIndex;
11642 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11643 for (SDValue ArgVal : ArgVals)
11644 Chains.push_back(ArgVal.getValue(1));
11645
11646 // Avoid emitting code for the store implementing the copy.
11647 const StoreInst *SI = ArgCopyIter->second.second;
11648 ElidedArgCopyInstrs.insert(SI);
11649
11650 // Check for uses of the argument again so that we can avoid exporting ArgVal
11651 // if it is't used by anything other than the store.
11652 for (const Value *U : Arg.users()) {
11653 if (U != SI) {
11654 ArgHasUses = true;
11655 break;
11656 }
11657 }
11658}
11659
11660void SelectionDAGISel::LowerArguments(const Function &F) {
11661 SelectionDAG &DAG = SDB->DAG;
11662 SDLoc dl = SDB->getCurSDLoc();
11663 const DataLayout &DL = DAG.getDataLayout();
11665
11666 // In Naked functions we aren't going to save any registers.
11667 if (F.hasFnAttribute(Attribute::Naked))
11668 return;
11669
11670 if (!FuncInfo->CanLowerReturn) {
11671 // Put in an sret pointer parameter before all the other parameters.
11672 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11673
11674 ISD::ArgFlagsTy Flags;
11675 Flags.setSRet();
11676 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11677 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11679 Ins.push_back(RetArg);
11680 }
11681
11682 // Look for stores of arguments to static allocas. Mark such arguments with a
11683 // flag to ask the target to give us the memory location of that argument if
11684 // available.
11685 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11687 ArgCopyElisionCandidates);
11688
11689 // Set up the incoming argument description vector.
11690 for (const Argument &Arg : F.args()) {
11691 unsigned ArgNo = Arg.getArgNo();
11693 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11694 bool isArgValueUsed = !Arg.use_empty();
11695 unsigned PartBase = 0;
11696 Type *FinalType = Arg.getType();
11697 if (Arg.hasAttribute(Attribute::ByVal))
11698 FinalType = Arg.getParamByValType();
11699 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11700 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11701 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11702 ++Value) {
11703 Type *ArgTy = Types[Value];
11704 EVT VT = TLI->getValueType(DL, ArgTy);
11705 ISD::ArgFlagsTy Flags;
11706
11707 if (ArgTy->isPointerTy()) {
11708 Flags.setPointer();
11709 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11710 }
11711 if (Arg.hasAttribute(Attribute::ZExt))
11712 Flags.setZExt();
11713 if (Arg.hasAttribute(Attribute::SExt))
11714 Flags.setSExt();
11715 if (Arg.hasAttribute(Attribute::InReg)) {
11716 // If we are using vectorcall calling convention, a structure that is
11717 // passed InReg - is surely an HVA
11718 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11719 isa<StructType>(Arg.getType())) {
11720 // The first value of a structure is marked
11721 if (0 == Value)
11722 Flags.setHvaStart();
11723 Flags.setHva();
11724 }
11725 // Set InReg Flag
11726 Flags.setInReg();
11727 }
11728 if (Arg.hasAttribute(Attribute::StructRet))
11729 Flags.setSRet();
11730 if (Arg.hasAttribute(Attribute::SwiftSelf))
11731 Flags.setSwiftSelf();
11732 if (Arg.hasAttribute(Attribute::SwiftAsync))
11733 Flags.setSwiftAsync();
11734 if (Arg.hasAttribute(Attribute::SwiftError))
11735 Flags.setSwiftError();
11736 if (Arg.hasAttribute(Attribute::ByVal))
11737 Flags.setByVal();
11738 if (Arg.hasAttribute(Attribute::ByRef))
11739 Flags.setByRef();
11740 if (Arg.hasAttribute(Attribute::InAlloca)) {
11741 Flags.setInAlloca();
11742 // Set the byval flag for CCAssignFn callbacks that don't know about
11743 // inalloca. This way we can know how many bytes we should've allocated
11744 // and how many bytes a callee cleanup function will pop. If we port
11745 // inalloca to more targets, we'll have to add custom inalloca handling
11746 // in the various CC lowering callbacks.
11747 Flags.setByVal();
11748 }
11749 if (Arg.hasAttribute(Attribute::Preallocated)) {
11750 Flags.setPreallocated();
11751 // Set the byval flag for CCAssignFn callbacks that don't know about
11752 // preallocated. This way we can know how many bytes we should've
11753 // allocated and how many bytes a callee cleanup function will pop. If
11754 // we port preallocated to more targets, we'll have to add custom
11755 // preallocated handling in the various CC lowering callbacks.
11756 Flags.setByVal();
11757 }
11758
11759 // Certain targets (such as MIPS), may have a different ABI alignment
11760 // for a type depending on the context. Give the target a chance to
11761 // specify the alignment it wants.
11762 const Align OriginalAlignment(
11763 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
11764 Flags.setOrigAlign(OriginalAlignment);
11765
11766 Align MemAlign;
11767 Type *ArgMemTy = nullptr;
11768 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11769 Flags.isByRef()) {
11770 if (!ArgMemTy)
11771 ArgMemTy = Arg.getPointeeInMemoryValueType();
11772
11773 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11774
11775 // For in-memory arguments, size and alignment should be passed from FE.
11776 // BE will guess if this info is not there but there are cases it cannot
11777 // get right.
11778 if (auto ParamAlign = Arg.getParamStackAlign())
11779 MemAlign = *ParamAlign;
11780 else if ((ParamAlign = Arg.getParamAlign()))
11781 MemAlign = *ParamAlign;
11782 else
11783 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
11784 if (Flags.isByRef())
11785 Flags.setByRefSize(MemSize);
11786 else
11787 Flags.setByValSize(MemSize);
11788 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11789 MemAlign = *ParamAlign;
11790 } else {
11791 MemAlign = OriginalAlignment;
11792 }
11793 Flags.setMemAlign(MemAlign);
11794
11795 if (Arg.hasAttribute(Attribute::Nest))
11796 Flags.setNest();
11797 if (NeedsRegBlock)
11798 Flags.setInConsecutiveRegs();
11799 if (ArgCopyElisionCandidates.count(&Arg))
11800 Flags.setCopyElisionCandidate();
11801 if (Arg.hasAttribute(Attribute::Returned))
11802 Flags.setReturned();
11803
11804 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
11805 *CurDAG->getContext(), F.getCallingConv(), VT);
11806 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
11807 *CurDAG->getContext(), F.getCallingConv(), VT);
11808 for (unsigned i = 0; i != NumRegs; ++i) {
11809 // For scalable vectors, use the minimum size; individual targets
11810 // are responsible for handling scalable vector arguments and
11811 // return values.
11812 ISD::InputArg MyFlags(
11813 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
11814 PartBase + i * RegisterVT.getStoreSize().getKnownMinValue());
11815 if (NumRegs > 1 && i == 0)
11816 MyFlags.Flags.setSplit();
11817 // if it isn't first piece, alignment must be 1
11818 else if (i > 0) {
11819 MyFlags.Flags.setOrigAlign(Align(1));
11820 if (i == NumRegs - 1)
11821 MyFlags.Flags.setSplitEnd();
11822 }
11823 Ins.push_back(MyFlags);
11824 }
11825 if (NeedsRegBlock && Value == NumValues - 1)
11826 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
11827 PartBase += VT.getStoreSize().getKnownMinValue();
11828 }
11829 }
11830
11831 // Call the target to set up the argument values.
11833 SDValue NewRoot = TLI->LowerFormalArguments(
11834 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
11835
11836 // Verify that the target's LowerFormalArguments behaved as expected.
11837 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
11838 "LowerFormalArguments didn't return a valid chain!");
11839 assert(InVals.size() == Ins.size() &&
11840 "LowerFormalArguments didn't emit the correct number of values!");
11841 LLVM_DEBUG({
11842 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
11843 assert(InVals[i].getNode() &&
11844 "LowerFormalArguments emitted a null value!");
11845 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
11846 "LowerFormalArguments emitted a value with the wrong type!");
11847 }
11848 });
11849
11850 // Update the DAG with the new chain value resulting from argument lowering.
11851 DAG.setRoot(NewRoot);
11852
11853 // Set up the argument values.
11854 unsigned i = 0;
11855 if (!FuncInfo->CanLowerReturn) {
11856 // Create a virtual register for the sret pointer, and put in a copy
11857 // from the sret argument into it.
11858 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11859 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
11860 std::optional<ISD::NodeType> AssertOp;
11861 SDValue ArgValue =
11862 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
11863 F.getCallingConv(), AssertOp);
11864
11865 MachineFunction& MF = SDB->DAG.getMachineFunction();
11866 MachineRegisterInfo& RegInfo = MF.getRegInfo();
11867 Register SRetReg =
11868 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
11869 FuncInfo->DemoteRegister = SRetReg;
11870 NewRoot =
11871 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
11872 DAG.setRoot(NewRoot);
11873
11874 // i indexes lowered arguments. Bump it past the hidden sret argument.
11875 ++i;
11876 }
11877
11879 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
11880 for (const Argument &Arg : F.args()) {
11881 SmallVector<SDValue, 4> ArgValues;
11882 SmallVector<EVT, 4> ValueVTs;
11883 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
11884 unsigned NumValues = ValueVTs.size();
11885 if (NumValues == 0)
11886 continue;
11887
11888 bool ArgHasUses = !Arg.use_empty();
11889
11890 // Elide the copying store if the target loaded this argument from a
11891 // suitable fixed stack object.
11892 if (Ins[i].Flags.isCopyElisionCandidate()) {
11893 unsigned NumParts = 0;
11894 for (EVT VT : ValueVTs)
11895 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
11896 F.getCallingConv(), VT);
11897
11898 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
11899 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
11900 ArrayRef(&InVals[i], NumParts), ArgHasUses);
11901 }
11902
11903 // If this argument is unused then remember its value. It is used to generate
11904 // debugging information.
11905 bool isSwiftErrorArg =
11906 TLI->supportSwiftError() &&
11907 Arg.hasAttribute(Attribute::SwiftError);
11908 if (!ArgHasUses && !isSwiftErrorArg) {
11909 SDB->setUnusedArgValue(&Arg, InVals[i]);
11910
11911 // Also remember any frame index for use in FastISel.
11912 if (FrameIndexSDNode *FI =
11914 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11915 }
11916
11917 for (unsigned Val = 0; Val != NumValues; ++Val) {
11918 EVT VT = ValueVTs[Val];
11919 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
11920 F.getCallingConv(), VT);
11921 unsigned NumParts = TLI->getNumRegistersForCallingConv(
11922 *CurDAG->getContext(), F.getCallingConv(), VT);
11923
11924 // Even an apparent 'unused' swifterror argument needs to be returned. So
11925 // we do generate a copy for it that can be used on return from the
11926 // function.
11927 if (ArgHasUses || isSwiftErrorArg) {
11928 std::optional<ISD::NodeType> AssertOp;
11929 if (Arg.hasAttribute(Attribute::SExt))
11930 AssertOp = ISD::AssertSext;
11931 else if (Arg.hasAttribute(Attribute::ZExt))
11932 AssertOp = ISD::AssertZext;
11933
11934 SDValue OutVal =
11935 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
11936 NewRoot, F.getCallingConv(), AssertOp);
11937
11938 FPClassTest NoFPClass = Arg.getNoFPClass();
11939 if (NoFPClass != fcNone) {
11940 SDValue SDNoFPClass = DAG.getTargetConstant(
11941 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
11942 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
11943 OutVal, SDNoFPClass);
11944 }
11945 ArgValues.push_back(OutVal);
11946 }
11947
11948 i += NumParts;
11949 }
11950
11951 // We don't need to do anything else for unused arguments.
11952 if (ArgValues.empty())
11953 continue;
11954
11955 // Note down frame index.
11956 if (FrameIndexSDNode *FI =
11957 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
11958 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11959
11960 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
11961 SDB->getCurSDLoc());
11962
11963 SDB->setValue(&Arg, Res);
11964 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
11965 // We want to associate the argument with the frame index, among
11966 // involved operands, that correspond to the lowest address. The
11967 // getCopyFromParts function, called earlier, is swapping the order of
11968 // the operands to BUILD_PAIR depending on endianness. The result of
11969 // that swapping is that the least significant bits of the argument will
11970 // be in the first operand of the BUILD_PAIR node, and the most
11971 // significant bits will be in the second operand.
11972 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
11973 if (LoadSDNode *LNode =
11974 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
11975 if (FrameIndexSDNode *FI =
11976 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
11977 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11978 }
11979
11980 // Analyses past this point are naive and don't expect an assertion.
11981 if (Res.getOpcode() == ISD::AssertZext)
11982 Res = Res.getOperand(0);
11983
11984 // Update the SwiftErrorVRegDefMap.
11985 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
11986 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
11987 if (Reg.isVirtual())
11988 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
11989 Reg);
11990 }
11991
11992 // If this argument is live outside of the entry block, insert a copy from
11993 // wherever we got it to the vreg that other BB's will reference it as.
11994 if (Res.getOpcode() == ISD::CopyFromReg) {
11995 // If we can, though, try to skip creating an unnecessary vreg.
11996 // FIXME: This isn't very clean... it would be nice to make this more
11997 // general.
11998 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
11999 if (Reg.isVirtual()) {
12000 FuncInfo->ValueMap[&Arg] = Reg;
12001 continue;
12002 }
12003 }
12004 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12005 FuncInfo->InitializeRegForValue(&Arg);
12006 SDB->CopyToExportRegsIfNeeded(&Arg);
12007 }
12008 }
12009
12010 if (!Chains.empty()) {
12011 Chains.push_back(NewRoot);
12012 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12013 }
12014
12015 DAG.setRoot(NewRoot);
12016
12017 assert(i == InVals.size() && "Argument register count mismatch!");
12018
12019 // If any argument copy elisions occurred and we have debug info, update the
12020 // stale frame indices used in the dbg.declare variable info table.
12021 if (!ArgCopyElisionFrameIndexMap.empty()) {
12022 for (MachineFunction::VariableDbgInfo &VI :
12023 MF->getInStackSlotVariableDbgInfo()) {
12024 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12025 if (I != ArgCopyElisionFrameIndexMap.end())
12026 VI.updateStackSlot(I->second);
12027 }
12028 }
12029
12030 // Finally, if the target has anything special to do, allow it to do so.
12032}
12033
12034/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12035/// ensure constants are generated when needed. Remember the virtual registers
12036/// that need to be added to the Machine PHI nodes as input. We cannot just
12037/// directly add them, because expansion might result in multiple MBB's for one
12038/// BB. As such, the start of the BB might correspond to a different MBB than
12039/// the end.
12040void
12041SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12042 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12043
12044 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12045
12046 // Check PHI nodes in successors that expect a value to be available from this
12047 // block.
12048 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12049 if (!isa<PHINode>(SuccBB->begin())) continue;
12050 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12051
12052 // If this terminator has multiple identical successors (common for
12053 // switches), only handle each succ once.
12054 if (!SuccsHandled.insert(SuccMBB).second)
12055 continue;
12056
12058
12059 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12060 // nodes and Machine PHI nodes, but the incoming operands have not been
12061 // emitted yet.
12062 for (const PHINode &PN : SuccBB->phis()) {
12063 // Ignore dead phi's.
12064 if (PN.use_empty())
12065 continue;
12066
12067 // Skip empty types
12068 if (PN.getType()->isEmptyTy())
12069 continue;
12070
12071 Register Reg;
12072 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12073
12074 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12075 Register &RegOut = ConstantsOut[C];
12076 if (!RegOut) {
12077 RegOut = FuncInfo.CreateRegs(&PN);
12078 // We need to zero/sign extend ConstantInt phi operands to match
12079 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12080 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12081 if (auto *CI = dyn_cast<ConstantInt>(C))
12082 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12084 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12085 }
12086 Reg = RegOut;
12087 } else {
12089 FuncInfo.ValueMap.find(PHIOp);
12090 if (I != FuncInfo.ValueMap.end())
12091 Reg = I->second;
12092 else {
12093 assert(isa<AllocaInst>(PHIOp) &&
12094 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12095 "Didn't codegen value into a register!??");
12096 Reg = FuncInfo.CreateRegs(&PN);
12098 }
12099 }
12100
12101 // Remember that this register needs to added to the machine PHI node as
12102 // the input for this MBB.
12103 SmallVector<EVT, 4> ValueVTs;
12104 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12105 for (EVT VT : ValueVTs) {
12106 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12107 for (unsigned i = 0; i != NumRegisters; ++i)
12108 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12109 Reg += NumRegisters;
12110 }
12111 }
12112 }
12113
12114 ConstantsOut.clear();
12115}
12116
12117MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12119 if (++I == FuncInfo.MF->end())
12120 return nullptr;
12121 return &*I;
12122}
12123
12124/// During lowering new call nodes can be created (such as memset, etc.).
12125/// Those will become new roots of the current DAG, but complications arise
12126/// when they are tail calls. In such cases, the call lowering will update
12127/// the root, but the builder still needs to know that a tail call has been
12128/// lowered in order to avoid generating an additional return.
12129void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12130 // If the node is null, we do have a tail call.
12131 if (MaybeTC.getNode() != nullptr)
12132 DAG.setRoot(MaybeTC);
12133 else
12134 HasTailCall = true;
12135}
12136
12137void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12138 MachineBasicBlock *SwitchMBB,
12139 MachineBasicBlock *DefaultMBB) {
12140 MachineFunction *CurMF = FuncInfo.MF;
12141 MachineBasicBlock *NextMBB = nullptr;
12143 if (++BBI != FuncInfo.MF->end())
12144 NextMBB = &*BBI;
12145
12146 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12147
12148 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12149
12150 if (Size == 2 && W.MBB == SwitchMBB) {
12151 // If any two of the cases has the same destination, and if one value
12152 // is the same as the other, but has one bit unset that the other has set,
12153 // use bit manipulation to do two compares at once. For example:
12154 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12155 // TODO: This could be extended to merge any 2 cases in switches with 3
12156 // cases.
12157 // TODO: Handle cases where W.CaseBB != SwitchBB.
12158 CaseCluster &Small = *W.FirstCluster;
12159 CaseCluster &Big = *W.LastCluster;
12160
12161 if (Small.Low == Small.High && Big.Low == Big.High &&
12162 Small.MBB == Big.MBB) {
12163 const APInt &SmallValue = Small.Low->getValue();
12164 const APInt &BigValue = Big.Low->getValue();
12165
12166 // Check that there is only one bit different.
12167 APInt CommonBit = BigValue ^ SmallValue;
12168 if (CommonBit.isPowerOf2()) {
12169 SDValue CondLHS = getValue(Cond);
12170 EVT VT = CondLHS.getValueType();
12171 SDLoc DL = getCurSDLoc();
12172
12173 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12174 DAG.getConstant(CommonBit, DL, VT));
12175 SDValue Cond = DAG.getSetCC(
12176 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12177 ISD::SETEQ);
12178
12179 // Update successor info.
12180 // Both Small and Big will jump to Small.BB, so we sum up the
12181 // probabilities.
12182 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12183 if (BPI)
12184 addSuccessorWithProb(
12185 SwitchMBB, DefaultMBB,
12186 // The default destination is the first successor in IR.
12187 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12188 else
12189 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12190
12191 // Insert the true branch.
12192 SDValue BrCond =
12193 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12194 DAG.getBasicBlock(Small.MBB));
12195 // Insert the false branch.
12196 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12197 DAG.getBasicBlock(DefaultMBB));
12198
12199 DAG.setRoot(BrCond);
12200 return;
12201 }
12202 }
12203 }
12204
12205 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12206 // Here, we order cases by probability so the most likely case will be
12207 // checked first. However, two clusters can have the same probability in
12208 // which case their relative ordering is non-deterministic. So we use Low
12209 // as a tie-breaker as clusters are guaranteed to never overlap.
12210 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12211 [](const CaseCluster &a, const CaseCluster &b) {
12212 return a.Prob != b.Prob ?
12213 a.Prob > b.Prob :
12214 a.Low->getValue().slt(b.Low->getValue());
12215 });
12216
12217 // Rearrange the case blocks so that the last one falls through if possible
12218 // without changing the order of probabilities.
12219 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12220 --I;
12221 if (I->Prob > W.LastCluster->Prob)
12222 break;
12223 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12224 std::swap(*I, *W.LastCluster);
12225 break;
12226 }
12227 }
12228 }
12229
12230 // Compute total probability.
12231 BranchProbability DefaultProb = W.DefaultProb;
12232 BranchProbability UnhandledProbs = DefaultProb;
12233 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12234 UnhandledProbs += I->Prob;
12235
12236 MachineBasicBlock *CurMBB = W.MBB;
12237 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12238 bool FallthroughUnreachable = false;
12239 MachineBasicBlock *Fallthrough;
12240 if (I == W.LastCluster) {
12241 // For the last cluster, fall through to the default destination.
12242 Fallthrough = DefaultMBB;
12243 FallthroughUnreachable = isa<UnreachableInst>(
12244 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12245 } else {
12246 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12247 CurMF->insert(BBI, Fallthrough);
12248 // Put Cond in a virtual register to make it available from the new blocks.
12250 }
12251 UnhandledProbs -= I->Prob;
12252
12253 switch (I->Kind) {
12254 case CC_JumpTable: {
12255 // FIXME: Optimize away range check based on pivot comparisons.
12256 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12257 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12258
12259 // The jump block hasn't been inserted yet; insert it here.
12260 MachineBasicBlock *JumpMBB = JT->MBB;
12261 CurMF->insert(BBI, JumpMBB);
12262
12263 auto JumpProb = I->Prob;
12264 auto FallthroughProb = UnhandledProbs;
12265
12266 // If the default statement is a target of the jump table, we evenly
12267 // distribute the default probability to successors of CurMBB. Also
12268 // update the probability on the edge from JumpMBB to Fallthrough.
12269 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12270 SE = JumpMBB->succ_end();
12271 SI != SE; ++SI) {
12272 if (*SI == DefaultMBB) {
12273 JumpProb += DefaultProb / 2;
12274 FallthroughProb -= DefaultProb / 2;
12275 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12276 JumpMBB->normalizeSuccProbs();
12277 break;
12278 }
12279 }
12280
12281 // If the default clause is unreachable, propagate that knowledge into
12282 // JTH->FallthroughUnreachable which will use it to suppress the range
12283 // check.
12284 //
12285 // However, don't do this if we're doing branch target enforcement,
12286 // because a table branch _without_ a range check can be a tempting JOP
12287 // gadget - out-of-bounds inputs that are impossible in correct
12288 // execution become possible again if an attacker can influence the
12289 // control flow. So if an attacker doesn't already have a BTI bypass
12290 // available, we don't want them to be able to get one out of this
12291 // table branch.
12292 if (FallthroughUnreachable) {
12293 Function &CurFunc = CurMF->getFunction();
12294 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12295 JTH->FallthroughUnreachable = true;
12296 }
12297
12298 if (!JTH->FallthroughUnreachable)
12299 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12300 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12301 CurMBB->normalizeSuccProbs();
12302
12303 // The jump table header will be inserted in our current block, do the
12304 // range check, and fall through to our fallthrough block.
12305 JTH->HeaderBB = CurMBB;
12306 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12307
12308 // If we're in the right place, emit the jump table header right now.
12309 if (CurMBB == SwitchMBB) {
12310 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12311 JTH->Emitted = true;
12312 }
12313 break;
12314 }
12315 case CC_BitTests: {
12316 // FIXME: Optimize away range check based on pivot comparisons.
12317 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12318
12319 // The bit test blocks haven't been inserted yet; insert them here.
12320 for (BitTestCase &BTC : BTB->Cases)
12321 CurMF->insert(BBI, BTC.ThisBB);
12322
12323 // Fill in fields of the BitTestBlock.
12324 BTB->Parent = CurMBB;
12325 BTB->Default = Fallthrough;
12326
12327 BTB->DefaultProb = UnhandledProbs;
12328 // If the cases in bit test don't form a contiguous range, we evenly
12329 // distribute the probability on the edge to Fallthrough to two
12330 // successors of CurMBB.
12331 if (!BTB->ContiguousRange) {
12332 BTB->Prob += DefaultProb / 2;
12333 BTB->DefaultProb -= DefaultProb / 2;
12334 }
12335
12336 if (FallthroughUnreachable)
12337 BTB->FallthroughUnreachable = true;
12338
12339 // If we're in the right place, emit the bit test header right now.
12340 if (CurMBB == SwitchMBB) {
12341 visitBitTestHeader(*BTB, SwitchMBB);
12342 BTB->Emitted = true;
12343 }
12344 break;
12345 }
12346 case CC_Range: {
12347 const Value *RHS, *LHS, *MHS;
12348 ISD::CondCode CC;
12349 if (I->Low == I->High) {
12350 // Check Cond == I->Low.
12351 CC = ISD::SETEQ;
12352 LHS = Cond;
12353 RHS=I->Low;
12354 MHS = nullptr;
12355 } else {
12356 // Check I->Low <= Cond <= I->High.
12357 CC = ISD::SETLE;
12358 LHS = I->Low;
12359 MHS = Cond;
12360 RHS = I->High;
12361 }
12362
12363 // If Fallthrough is unreachable, fold away the comparison.
12364 if (FallthroughUnreachable)
12365 CC = ISD::SETTRUE;
12366
12367 // The false probability is the sum of all unhandled cases.
12368 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12369 getCurSDLoc(), I->Prob, UnhandledProbs);
12370
12371 if (CurMBB == SwitchMBB)
12372 visitSwitchCase(CB, SwitchMBB);
12373 else
12374 SL->SwitchCases.push_back(CB);
12375
12376 break;
12377 }
12378 }
12379 CurMBB = Fallthrough;
12380 }
12381}
12382
12383void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12384 const SwitchWorkListItem &W,
12385 Value *Cond,
12386 MachineBasicBlock *SwitchMBB) {
12387 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12388 "Clusters not sorted?");
12389 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12390
12391 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12392 SL->computeSplitWorkItemInfo(W);
12393
12394 // Use the first element on the right as pivot since we will make less-than
12395 // comparisons against it.
12396 CaseClusterIt PivotCluster = FirstRight;
12397 assert(PivotCluster > W.FirstCluster);
12398 assert(PivotCluster <= W.LastCluster);
12399
12400 CaseClusterIt FirstLeft = W.FirstCluster;
12401 CaseClusterIt LastRight = W.LastCluster;
12402
12403 const ConstantInt *Pivot = PivotCluster->Low;
12404
12405 // New blocks will be inserted immediately after the current one.
12407 ++BBI;
12408
12409 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12410 // we can branch to its destination directly if it's squeezed exactly in
12411 // between the known lower bound and Pivot - 1.
12412 MachineBasicBlock *LeftMBB;
12413 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12414 FirstLeft->Low == W.GE &&
12415 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12416 LeftMBB = FirstLeft->MBB;
12417 } else {
12418 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12419 FuncInfo.MF->insert(BBI, LeftMBB);
12420 WorkList.push_back(
12421 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12422 // Put Cond in a virtual register to make it available from the new blocks.
12424 }
12425
12426 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12427 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12428 // directly if RHS.High equals the current upper bound.
12429 MachineBasicBlock *RightMBB;
12430 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12431 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12432 RightMBB = FirstRight->MBB;
12433 } else {
12434 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12435 FuncInfo.MF->insert(BBI, RightMBB);
12436 WorkList.push_back(
12437 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12438 // Put Cond in a virtual register to make it available from the new blocks.
12440 }
12441
12442 // Create the CaseBlock record that will be used to lower the branch.
12443 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12444 getCurSDLoc(), LeftProb, RightProb);
12445
12446 if (W.MBB == SwitchMBB)
12447 visitSwitchCase(CB, SwitchMBB);
12448 else
12449 SL->SwitchCases.push_back(CB);
12450}
12451
12452// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12453// from the swith statement.
12455 BranchProbability PeeledCaseProb) {
12456 if (PeeledCaseProb == BranchProbability::getOne())
12458 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12459
12460 uint32_t Numerator = CaseProb.getNumerator();
12461 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12462 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12463}
12464
12465// Try to peel the top probability case if it exceeds the threshold.
12466// Return current MachineBasicBlock for the switch statement if the peeling
12467// does not occur.
12468// If the peeling is performed, return the newly created MachineBasicBlock
12469// for the peeled switch statement. Also update Clusters to remove the peeled
12470// case. PeeledCaseProb is the BranchProbability for the peeled case.
12471MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12472 const SwitchInst &SI, CaseClusterVector &Clusters,
12473 BranchProbability &PeeledCaseProb) {
12474 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12475 // Don't perform if there is only one cluster or optimizing for size.
12476 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12477 TM.getOptLevel() == CodeGenOptLevel::None ||
12478 SwitchMBB->getParent()->getFunction().hasMinSize())
12479 return SwitchMBB;
12480
12481 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12482 unsigned PeeledCaseIndex = 0;
12483 bool SwitchPeeled = false;
12484 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12485 CaseCluster &CC = Clusters[Index];
12486 if (CC.Prob < TopCaseProb)
12487 continue;
12488 TopCaseProb = CC.Prob;
12489 PeeledCaseIndex = Index;
12490 SwitchPeeled = true;
12491 }
12492 if (!SwitchPeeled)
12493 return SwitchMBB;
12494
12495 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12496 << TopCaseProb << "\n");
12497
12498 // Record the MBB for the peeled switch statement.
12499 MachineFunction::iterator BBI(SwitchMBB);
12500 ++BBI;
12501 MachineBasicBlock *PeeledSwitchMBB =
12502 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12503 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12504
12505 ExportFromCurrentBlock(SI.getCondition());
12506 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12507 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12508 nullptr, nullptr, TopCaseProb.getCompl()};
12509 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12510
12511 Clusters.erase(PeeledCaseIt);
12512 for (CaseCluster &CC : Clusters) {
12513 LLVM_DEBUG(
12514 dbgs() << "Scale the probablity for one cluster, before scaling: "
12515 << CC.Prob << "\n");
12516 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12517 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12518 }
12519 PeeledCaseProb = TopCaseProb;
12520 return PeeledSwitchMBB;
12521}
12522
12523void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12524 // Extract cases from the switch.
12525 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12526 CaseClusterVector Clusters;
12527 Clusters.reserve(SI.getNumCases());
12528 for (auto I : SI.cases()) {
12529 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12530 const ConstantInt *CaseVal = I.getCaseValue();
12531 BranchProbability Prob =
12532 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12533 : BranchProbability(1, SI.getNumCases() + 1);
12534 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12535 }
12536
12537 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12538
12539 // Cluster adjacent cases with the same destination. We do this at all
12540 // optimization levels because it's cheap to do and will make codegen faster
12541 // if there are many clusters.
12542 sortAndRangeify(Clusters);
12543
12544 // The branch probablity of the peeled case.
12545 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12546 MachineBasicBlock *PeeledSwitchMBB =
12547 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12548
12549 // If there is only the default destination, jump there directly.
12550 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12551 if (Clusters.empty()) {
12552 assert(PeeledSwitchMBB == SwitchMBB);
12553 SwitchMBB->addSuccessor(DefaultMBB);
12554 if (DefaultMBB != NextBlock(SwitchMBB)) {
12555 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12556 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12557 }
12558 return;
12559 }
12560
12561 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12562 DAG.getBFI());
12563 SL->findBitTestClusters(Clusters, &SI);
12564
12565 LLVM_DEBUG({
12566 dbgs() << "Case clusters: ";
12567 for (const CaseCluster &C : Clusters) {
12568 if (C.Kind == CC_JumpTable)
12569 dbgs() << "JT:";
12570 if (C.Kind == CC_BitTests)
12571 dbgs() << "BT:";
12572
12573 C.Low->getValue().print(dbgs(), true);
12574 if (C.Low != C.High) {
12575 dbgs() << '-';
12576 C.High->getValue().print(dbgs(), true);
12577 }
12578 dbgs() << ' ';
12579 }
12580 dbgs() << '\n';
12581 });
12582
12583 assert(!Clusters.empty());
12584 SwitchWorkList WorkList;
12585 CaseClusterIt First = Clusters.begin();
12586 CaseClusterIt Last = Clusters.end() - 1;
12587 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12588 // Scale the branchprobability for DefaultMBB if the peel occurs and
12589 // DefaultMBB is not replaced.
12590 if (PeeledCaseProb != BranchProbability::getZero() &&
12591 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12592 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12593 WorkList.push_back(
12594 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12595
12596 while (!WorkList.empty()) {
12597 SwitchWorkListItem W = WorkList.pop_back_val();
12598 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12599
12600 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12601 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12602 // For optimized builds, lower large range as a balanced binary tree.
12603 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12604 continue;
12605 }
12606
12607 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12608 }
12609}
12610
12611void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12612 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12613 auto DL = getCurSDLoc();
12614 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12615 setValue(&I, DAG.getStepVector(DL, ResultVT));
12616}
12617
12618void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12619 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12620 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12621
12622 SDLoc DL = getCurSDLoc();
12623 SDValue V = getValue(I.getOperand(0));
12624 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12625
12626 if (VT.isScalableVector()) {
12627 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12628 return;
12629 }
12630
12631 // Use VECTOR_SHUFFLE for the fixed-length vector
12632 // to maintain existing behavior.
12633 SmallVector<int, 8> Mask;
12634 unsigned NumElts = VT.getVectorMinNumElements();
12635 for (unsigned i = 0; i != NumElts; ++i)
12636 Mask.push_back(NumElts - 1 - i);
12637
12638 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12639}
12640
12641void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12642 unsigned Factor) {
12643 auto DL = getCurSDLoc();
12644 SDValue InVec = getValue(I.getOperand(0));
12645
12646 SmallVector<EVT, 4> ValueVTs;
12647 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12648 ValueVTs);
12649
12650 EVT OutVT = ValueVTs[0];
12651 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12652
12653 SmallVector<SDValue, 4> SubVecs(Factor);
12654 for (unsigned i = 0; i != Factor; ++i) {
12655 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12656 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12657 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12658 }
12659
12660 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12661 // from existing legalisation and combines.
12662 if (OutVT.isFixedLengthVector() && Factor == 2) {
12663 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12664 createStrideMask(0, 2, OutNumElts));
12665 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12666 createStrideMask(1, 2, OutNumElts));
12667 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12668 setValue(&I, Res);
12669 return;
12670 }
12671
12672 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12673 DAG.getVTList(ValueVTs), SubVecs);
12674 setValue(&I, Res);
12675}
12676
12677void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12678 unsigned Factor) {
12679 auto DL = getCurSDLoc();
12680 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12681 EVT InVT = getValue(I.getOperand(0)).getValueType();
12682 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12683
12684 SmallVector<SDValue, 8> InVecs(Factor);
12685 for (unsigned i = 0; i < Factor; ++i) {
12686 InVecs[i] = getValue(I.getOperand(i));
12687 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12688 "Expected VTs to be the same");
12689 }
12690
12691 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12692 // from existing legalisation and combines.
12693 if (OutVT.isFixedLengthVector() && Factor == 2) {
12694 unsigned NumElts = InVT.getVectorMinNumElements();
12695 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12696 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12697 createInterleaveMask(NumElts, 2)));
12698 return;
12699 }
12700
12701 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12702 SDValue Res =
12703 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12704
12706 for (unsigned i = 0; i < Factor; ++i)
12707 Results[i] = Res.getValue(i);
12708
12709 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12710 setValue(&I, Res);
12711}
12712
12713void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12714 SmallVector<EVT, 4> ValueVTs;
12715 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12716 ValueVTs);
12717 unsigned NumValues = ValueVTs.size();
12718 if (NumValues == 0) return;
12719
12720 SmallVector<SDValue, 4> Values(NumValues);
12721 SDValue Op = getValue(I.getOperand(0));
12722
12723 for (unsigned i = 0; i != NumValues; ++i)
12724 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12725 SDValue(Op.getNode(), Op.getResNo() + i));
12726
12728 DAG.getVTList(ValueVTs), Values));
12729}
12730
12731void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12732 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12733 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12734
12735 SDLoc DL = getCurSDLoc();
12736 SDValue V1 = getValue(I.getOperand(0));
12737 SDValue V2 = getValue(I.getOperand(1));
12738 int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue();
12739
12740 // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
12741 if (VT.isScalableVector()) {
12742 setValue(
12743 &I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2,
12744 DAG.getSignedConstant(
12745 Imm, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
12746 return;
12747 }
12748
12749 unsigned NumElts = VT.getVectorNumElements();
12750
12751 uint64_t Idx = (NumElts + Imm) % NumElts;
12752
12753 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12754 SmallVector<int, 8> Mask;
12755 for (unsigned i = 0; i < NumElts; ++i)
12756 Mask.push_back(Idx + i);
12757 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12758}
12759
12760// Consider the following MIR after SelectionDAG, which produces output in
12761// phyregs in the first case or virtregs in the second case.
12762//
12763// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12764// %5:gr32 = COPY $ebx
12765// %6:gr32 = COPY $edx
12766// %1:gr32 = COPY %6:gr32
12767// %0:gr32 = COPY %5:gr32
12768//
12769// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12770// %1:gr32 = COPY %6:gr32
12771// %0:gr32 = COPY %5:gr32
12772//
12773// Given %0, we'd like to return $ebx in the first case and %5 in the second.
12774// Given %1, we'd like to return $edx in the first case and %6 in the second.
12775//
12776// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12777// to a single virtreg (such as %0). The remaining outputs monotonically
12778// increase in virtreg number from there. If a callbr has no outputs, then it
12779// should not have a corresponding callbr landingpad; in fact, the callbr
12780// landingpad would not even be able to refer to such a callbr.
12782 MachineInstr *MI = MRI.def_begin(Reg)->getParent();
12783 // There is definitely at least one copy.
12784 assert(MI->getOpcode() == TargetOpcode::COPY &&
12785 "start of copy chain MUST be COPY");
12786 Reg = MI->getOperand(1).getReg();
12787
12788 // If the copied register in the first copy must be virtual.
12789 assert(Reg.isVirtual() && "expected COPY of virtual register");
12790 MI = MRI.def_begin(Reg)->getParent();
12791
12792 // There may be an optional second copy.
12793 if (MI->getOpcode() == TargetOpcode::COPY) {
12794 assert(Reg.isVirtual() && "expected COPY of virtual register");
12795 Reg = MI->getOperand(1).getReg();
12796 assert(Reg.isPhysical() && "expected COPY of physical register");
12797 } else {
12798 // The start of the chain must be an INLINEASM_BR.
12799 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12800 "end of copy chain MUST be INLINEASM_BR");
12801 }
12802
12803 return Reg;
12804}
12805
12806// We must do this walk rather than the simpler
12807// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
12808// otherwise we will end up with copies of virtregs only valid along direct
12809// edges.
12810void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
12811 SmallVector<EVT, 8> ResultVTs;
12812 SmallVector<SDValue, 8> ResultValues;
12813 const auto *CBR =
12814 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
12815
12816 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12817 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
12818 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
12819
12820 Register InitialDef = FuncInfo.ValueMap[CBR];
12821 SDValue Chain = DAG.getRoot();
12822
12823 // Re-parse the asm constraints string.
12824 TargetLowering::AsmOperandInfoVector TargetConstraints =
12825 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
12826 for (auto &T : TargetConstraints) {
12827 SDISelAsmOperandInfo OpInfo(T);
12828 if (OpInfo.Type != InlineAsm::isOutput)
12829 continue;
12830
12831 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
12832 // individual constraint.
12833 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
12834
12835 switch (OpInfo.ConstraintType) {
12838 // Fill in OpInfo.AssignedRegs.Regs.
12839 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
12840
12841 // getRegistersForValue may produce 1 to many registers based on whether
12842 // the OpInfo.ConstraintVT is legal on the target or not.
12843 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
12844 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
12845 if (OriginalDef.isPhysical())
12846 FuncInfo.MBB->addLiveIn(OriginalDef);
12847 // Update the assigned registers to use the original defs.
12848 Reg = OriginalDef;
12849 }
12850
12851 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
12852 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
12853 ResultValues.push_back(V);
12854 ResultVTs.push_back(OpInfo.ConstraintVT);
12855 break;
12856 }
12858 SDValue Flag;
12859 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
12860 OpInfo, DAG);
12861 ++InitialDef;
12862 ResultValues.push_back(V);
12863 ResultVTs.push_back(OpInfo.ConstraintVT);
12864 break;
12865 }
12866 default:
12867 break;
12868 }
12869 }
12871 DAG.getVTList(ResultVTs), ResultValues);
12872 setValue(&I, V);
12873}
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,...)
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
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:666
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:678
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:199
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:165
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
bool empty() const
Definition DenseMap.h:107
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:214
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:112
Diagnostic information for inline asm reporting.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:64
An instruction for ordering other memory operations.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp: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:1077
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:137
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:107
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:182
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 > 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 std::pair< SDValue, SDValue > EmitTargetCodeForStrlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, MachinePointerInfo SrcPtrInfo) const
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:151
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:148
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 shouldExpandPartialReductionIntrinsic(const IntrinsicInst *I) const
Return true if the @llvm.vector.partial.reduce.
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:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
Definition Type.cpp: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:1101
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h: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:774
@ 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:587
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:765
@ 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:835
@ 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:862
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:571
@ 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:738
@ 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:826
@ 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:773
@ 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:622
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:528
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:535
@ 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:778
@ 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:663
@ 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:952
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:756
@ 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:601
@ 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:563
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:832
@ 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:718
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:627
@ 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:787
@ 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:908
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:730
@ 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:726
@ 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:552
@ 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:648
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:941
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:690
@ 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:927
@ 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:838
@ 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:521
@ 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:611
@ 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:543
@ 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:66
constexpr double e
Definition MathExtras.h:47
constexpr float ln2f
Definition MathExtras.h:64
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:262
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:355
@ 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:1705
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:1657
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:649
@ 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:2116
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:252
auto cast_or_null(const Y &Val)
Definition Casting.h:720
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:557
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:1152
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h: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:759
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:1712
LLVM_ABI llvm::SmallVector< int, 16 > createStrideMask(unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_FMINNUM
Unsigned maximum.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
detail::zippy< detail::zip_first, T, U, Args... > zip_first(T &&t, U &&u, Args &&...args)
zip iterator that, for the sake of efficiency, assumes the first iteratee to be the shortest.
Definition STLExtras.h:854
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1624
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:302
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:548
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:155
@ 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:565
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:2100
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:1877
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:212
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:2088
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:299
@ 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:853
#define N
#define NC
Definition regutils.h:42
static LLVM_ABI const fltSemantics & IEEEsingle() LLVM_READNONE
Definition APFloat.cpp:266
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:85
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:241
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:141
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:249
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)