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"
58#include "llvm/IR/Argument.h"
59#include "llvm/IR/Attributes.h"
60#include "llvm/IR/BasicBlock.h"
61#include "llvm/IR/CFG.h"
62#include "llvm/IR/CallingConv.h"
63#include "llvm/IR/Constant.h"
65#include "llvm/IR/Constants.h"
66#include "llvm/IR/DataLayout.h"
67#include "llvm/IR/DebugInfo.h"
72#include "llvm/IR/Function.h"
74#include "llvm/IR/InlineAsm.h"
75#include "llvm/IR/InstrTypes.h"
78#include "llvm/IR/Intrinsics.h"
79#include "llvm/IR/IntrinsicsAArch64.h"
80#include "llvm/IR/IntrinsicsAMDGPU.h"
81#include "llvm/IR/IntrinsicsWebAssembly.h"
82#include "llvm/IR/LLVMContext.h"
84#include "llvm/IR/Metadata.h"
85#include "llvm/IR/Module.h"
86#include "llvm/IR/Operator.h"
88#include "llvm/IR/Statepoint.h"
89#include "llvm/IR/Type.h"
90#include "llvm/IR/User.h"
91#include "llvm/IR/Value.h"
92#include "llvm/MC/MCContext.h"
97#include "llvm/Support/Debug.h"
105#include <cstddef>
106#include <limits>
107#include <optional>
108#include <tuple>
109
110using namespace llvm;
111using namespace PatternMatch;
112using namespace SwitchCG;
113
114#define DEBUG_TYPE "isel"
115
116/// LimitFloatPrecision - Generate low-precision inline sequences for
117/// some float libcalls (6, 8 or 12 bits).
118static unsigned LimitFloatPrecision;
119
120static cl::opt<bool>
121 InsertAssertAlign("insert-assert-align", cl::init(true),
122 cl::desc("Insert the experimental `assertalign` node."),
124
126 LimitFPPrecision("limit-float-precision",
127 cl::desc("Generate low-precision inline sequences "
128 "for some float libcalls"),
130 cl::init(0));
131
133 "switch-peel-threshold", cl::Hidden, cl::init(66),
134 cl::desc("Set the case probability threshold for peeling the case from a "
135 "switch statement. A value greater than 100 will void this "
136 "optimization"));
137
138// Limit the width of DAG chains. This is important in general to prevent
139// DAG-based analysis from blowing up. For example, alias analysis and
140// load clustering may not complete in reasonable time. It is difficult to
141// recognize and avoid this situation within each individual analysis, and
142// future analyses are likely to have the same behavior. Limiting DAG width is
143// the safe approach and will be especially important with global DAGs.
144//
145// MaxParallelChains default is arbitrarily high to avoid affecting
146// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
147// sequence over this should have been converted to llvm.memcpy by the
148// frontend. It is easy to induce this behavior with .ll code such as:
149// %buffer = alloca [4096 x i8]
150// %data = load [4096 x i8]* %argPtr
151// store [4096 x i8] %data, [4096 x i8]* %buffer
152static const unsigned MaxParallelChains = 64;
153
155 const SDValue *Parts, unsigned NumParts,
156 MVT PartVT, EVT ValueVT, const Value *V,
157 SDValue InChain,
158 std::optional<CallingConv::ID> CC);
159
160/// getCopyFromParts - Create a value that contains the specified legal parts
161/// combined into the value they represent. If the parts combine to a type
162/// larger than ValueVT then AssertOp can be used to specify whether the extra
163/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
164/// (ISD::AssertSext).
165static SDValue
166getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts,
167 unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V,
168 SDValue InChain,
169 std::optional<CallingConv::ID> CC = std::nullopt,
170 std::optional<ISD::NodeType> AssertOp = std::nullopt) {
171 // Let the target assemble the parts if it wants to
172 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
173 if (SDValue Val = TLI.joinRegisterPartsIntoValue(DAG, DL, Parts, NumParts,
174 PartVT, ValueVT, CC))
175 return Val;
176
177 if (ValueVT.isVector())
178 return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT, V,
179 InChain, CC);
180
181 assert(NumParts > 0 && "No parts to assemble!");
182 SDValue Val = Parts[0];
183
184 if (NumParts > 1) {
185 // Assemble the value from multiple parts.
186 if (ValueVT.isInteger()) {
187 unsigned PartBits = PartVT.getSizeInBits();
188 unsigned ValueBits = ValueVT.getSizeInBits();
189
190 // Assemble the power of 2 part.
191 unsigned RoundParts = llvm::bit_floor(NumParts);
192 unsigned RoundBits = PartBits * RoundParts;
193 EVT RoundVT = RoundBits == ValueBits ?
194 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
195 SDValue Lo, Hi;
196
197 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
198
199 if (RoundParts > 2) {
200 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, PartVT, HalfVT, V,
201 InChain);
202 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, RoundParts / 2,
203 PartVT, HalfVT, V, InChain);
204 } else {
205 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
206 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
207 }
208
209 if (DAG.getDataLayout().isBigEndian())
210 std::swap(Lo, Hi);
211
212 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
213
214 if (RoundParts < NumParts) {
215 // Assemble the trailing non-power-of-2 part.
216 unsigned OddParts = NumParts - RoundParts;
217 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
218 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts, OddParts, PartVT,
219 OddVT, V, InChain, CC);
220
221 // Combine the round and odd parts.
222 Lo = Val;
223 if (DAG.getDataLayout().isBigEndian())
224 std::swap(Lo, Hi);
225 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
226 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
227 Hi = DAG.getNode(
228 ISD::SHL, DL, TotalVT, Hi,
229 DAG.getShiftAmountConstant(Lo.getValueSizeInBits(), TotalVT, DL));
230 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
231 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
232 }
233 } else if (PartVT.isFloatingPoint()) {
234 // FP split into multiple FP parts (for ppcf128)
235 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
236 "Unexpected split");
237 SDValue Lo, Hi;
238 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
239 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
240 if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
241 std::swap(Lo, Hi);
242 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
243 } else {
244 // FP split into integer parts (soft fp)
245 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
246 !PartVT.isVector() && "Unexpected split");
247 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
248 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V,
249 InChain, CC);
250 }
251 }
252
253 // There is now one part, held in Val. Correct it to match ValueVT.
254 // PartEVT is the type of the register class that holds the value.
255 // ValueVT is the type of the inline asm operation.
256 EVT PartEVT = Val.getValueType();
257
258 if (PartEVT == ValueVT)
259 return Val;
260
261 if (PartEVT.isInteger() && ValueVT.isFloatingPoint() &&
262 ValueVT.bitsLT(PartEVT)) {
263 // For an FP value in an integer part, we need to truncate to the right
264 // width first.
265 PartEVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
266 Val = DAG.getNode(ISD::TRUNCATE, DL, PartEVT, Val);
267 }
268
269 // Handle types that have the same size.
270 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
271 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
272
273 // Handle types with different sizes.
274 if (PartEVT.isInteger() && ValueVT.isInteger()) {
275 if (ValueVT.bitsLT(PartEVT)) {
276 // For a truncate, see if we have any information to
277 // indicate whether the truncated bits will always be
278 // zero or sign-extension.
279 if (AssertOp)
280 Val = DAG.getNode(*AssertOp, DL, PartEVT, Val,
281 DAG.getValueType(ValueVT));
282 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
283 }
284 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
285 }
286
287 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
288 // FP_ROUND's are always exact here.
289 if (ValueVT.bitsLT(Val.getValueType())) {
290
291 SDValue NoChange =
293
294 if (DAG.getMachineFunction().getFunction().getAttributes().hasFnAttr(
295 llvm::Attribute::StrictFP)) {
296 return DAG.getNode(ISD::STRICT_FP_ROUND, DL,
297 DAG.getVTList(ValueVT, MVT::Other), InChain, Val,
298 NoChange);
299 }
300
301 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, NoChange);
302 }
303
304 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
305 }
306
307 // Handle MMX to a narrower integer type by bitcasting MMX to integer and
308 // then truncating.
309 if (PartEVT == MVT::x86mmx && ValueVT.isInteger() &&
310 ValueVT.bitsLT(PartEVT)) {
311 Val = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Val);
312 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
313 }
314
315 report_fatal_error("Unknown mismatch in getCopyFromParts!");
316}
317
319 const Twine &ErrMsg) {
321 if (!I)
322 return Ctx.emitError(ErrMsg);
323
324 if (const CallInst *CI = dyn_cast<CallInst>(I))
325 if (CI->isInlineAsm()) {
326 return Ctx.diagnose(DiagnosticInfoInlineAsm(
327 *CI, ErrMsg + ", possible invalid constraint for vector type"));
328 }
329
330 return Ctx.emitError(I, ErrMsg);
331}
332
333/// getCopyFromPartsVector - Create a value that contains the specified legal
334/// parts combined into the value they represent. If the parts combine to a
335/// type larger than ValueVT then AssertOp can be used to specify whether the
336/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
337/// ValueVT (ISD::AssertSext).
339 const SDValue *Parts, unsigned NumParts,
340 MVT PartVT, EVT ValueVT, const Value *V,
341 SDValue InChain,
342 std::optional<CallingConv::ID> CallConv) {
343 assert(ValueVT.isVector() && "Not a vector value");
344 assert(NumParts > 0 && "No parts to assemble!");
345 const bool IsABIRegCopy = CallConv.has_value();
346
347 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
348 SDValue Val = Parts[0];
349
350 // Handle a multi-element vector.
351 if (NumParts > 1) {
352 EVT IntermediateVT;
353 MVT RegisterVT;
354 unsigned NumIntermediates;
355 unsigned NumRegs;
356
357 if (IsABIRegCopy) {
359 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT,
360 NumIntermediates, RegisterVT);
361 } else {
362 NumRegs =
363 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
364 NumIntermediates, RegisterVT);
365 }
366
367 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
368 NumParts = NumRegs; // Silence a compiler warning.
369 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
370 assert(RegisterVT.getSizeInBits() ==
371 Parts[0].getSimpleValueType().getSizeInBits() &&
372 "Part type sizes don't match!");
373
374 // Assemble the parts into intermediate operands.
375 SmallVector<SDValue, 8> Ops(NumIntermediates);
376 if (NumIntermediates == NumParts) {
377 // If the register was not expanded, truncate or copy the value,
378 // as appropriate.
379 for (unsigned i = 0; i != NumParts; ++i)
380 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, PartVT, IntermediateVT,
381 V, InChain, CallConv);
382 } else if (NumParts > 0) {
383 // If the intermediate type was expanded, build the intermediate
384 // operands from the parts.
385 assert(NumParts % NumIntermediates == 0 &&
386 "Must expand into a divisible number of parts!");
387 unsigned Factor = NumParts / NumIntermediates;
388 for (unsigned i = 0; i != NumIntermediates; ++i)
389 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, PartVT,
390 IntermediateVT, V, InChain, CallConv);
391 }
392
393 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
394 // intermediate operands.
395 EVT BuiltVectorTy =
396 IntermediateVT.isVector()
398 *DAG.getContext(), IntermediateVT.getScalarType(),
399 IntermediateVT.getVectorElementCount() * NumParts)
401 IntermediateVT.getScalarType(),
402 NumIntermediates);
403 Val = DAG.getNode(IntermediateVT.isVector() ? ISD::CONCAT_VECTORS
405 DL, BuiltVectorTy, Ops);
406 }
407
408 // There is now one part, held in Val. Correct it to match ValueVT.
409 EVT PartEVT = Val.getValueType();
410
411 if (PartEVT == ValueVT)
412 return Val;
413
414 if (PartEVT.isVector()) {
415 // Vector/Vector bitcast.
416 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
417 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
418
419 // If the parts vector has more elements than the value vector, then we
420 // have a vector widening case (e.g. <2 x float> -> <4 x float>).
421 // Extract the elements we want.
422 if (PartEVT.getVectorElementCount() != ValueVT.getVectorElementCount()) {
425 (PartEVT.getVectorElementCount().isScalable() ==
426 ValueVT.getVectorElementCount().isScalable()) &&
427 "Cannot narrow, it would be a lossy transformation");
428 PartEVT =
430 ValueVT.getVectorElementCount());
431 Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, PartEVT, Val,
432 DAG.getVectorIdxConstant(0, DL));
433 if (PartEVT == ValueVT)
434 return Val;
435 if (PartEVT.isInteger() && ValueVT.isFloatingPoint())
436 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
437
438 // Vector/Vector bitcast (e.g. <2 x bfloat> -> <2 x half>).
439 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
440 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
441 }
442
443 // Promoted vector extract
444 return DAG.getAnyExtOrTrunc(Val, DL, ValueVT);
445 }
446
447 // Trivial bitcast if the types are the same size and the destination
448 // vector type is legal.
449 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
450 TLI.isTypeLegal(ValueVT))
451 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
452
453 if (ValueVT.getVectorNumElements() != 1) {
454 // Certain ABIs require that vectors are passed as integers. For vectors
455 // are the same size, this is an obvious bitcast.
456 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) {
457 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
458 } else if (ValueVT.bitsLT(PartEVT)) {
459 const uint64_t ValueSize = ValueVT.getFixedSizeInBits();
460 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
461 // Drop the extra bits.
462 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
463 return DAG.getBitcast(ValueVT, Val);
464 }
465
467 *DAG.getContext(), V, "non-trivial scalar-to-vector conversion");
468 return DAG.getUNDEF(ValueVT);
469 }
470
471 // Handle cases such as i8 -> <1 x i1>
472 EVT ValueSVT = ValueVT.getVectorElementType();
473 if (ValueVT.getVectorNumElements() == 1 && ValueSVT != PartEVT) {
474 unsigned ValueSize = ValueSVT.getSizeInBits();
475 if (ValueSize == PartEVT.getSizeInBits()) {
476 Val = DAG.getNode(ISD::BITCAST, DL, ValueSVT, Val);
477 } else if (ValueSVT.isFloatingPoint() && PartEVT.isInteger()) {
478 // It's possible a scalar floating point type gets softened to integer and
479 // then promoted to a larger integer. If PartEVT is the larger integer
480 // we need to truncate it and then bitcast to the FP type.
481 assert(ValueSVT.bitsLT(PartEVT) && "Unexpected types");
482 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
483 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
484 Val = DAG.getBitcast(ValueSVT, Val);
485 } else {
486 Val = ValueVT.isFloatingPoint()
487 ? DAG.getFPExtendOrRound(Val, DL, ValueSVT)
488 : DAG.getAnyExtOrTrunc(Val, DL, ValueSVT);
489 }
490 }
491
492 return DAG.getBuildVector(ValueVT, DL, Val);
493}
494
495static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl,
496 SDValue Val, SDValue *Parts, unsigned NumParts,
497 MVT PartVT, const Value *V,
498 std::optional<CallingConv::ID> CallConv);
499
500/// getCopyToParts - Create a series of nodes that contain the specified value
501/// split into legal parts. If the parts contain more bits than Val, then, for
502/// integers, ExtendKind can be used to specify how to generate the extra bits.
503static void
505 unsigned NumParts, MVT PartVT, const Value *V,
506 std::optional<CallingConv::ID> CallConv = std::nullopt,
507 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
508 // Let the target split the parts if it wants to
509 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
510 if (TLI.splitValueIntoRegisterParts(DAG, DL, Val, Parts, NumParts, PartVT,
511 CallConv))
512 return;
513 EVT ValueVT = Val.getValueType();
514
515 // Handle the vector case separately.
516 if (ValueVT.isVector())
517 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V,
518 CallConv);
519
520 unsigned OrigNumParts = NumParts;
522 "Copying to an illegal type!");
523
524 if (NumParts == 0)
525 return;
526
527 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
528 EVT PartEVT = PartVT;
529 if (PartEVT == ValueVT) {
530 assert(NumParts == 1 && "No-op copy with multiple parts!");
531 Parts[0] = Val;
532 return;
533 }
534
535 unsigned PartBits = PartVT.getSizeInBits();
536 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
537 // If the parts cover more bits than the value has, promote the value.
538 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
539 assert(NumParts == 1 && "Do not know what to promote to!");
540 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
541 } else {
542 if (ValueVT.isFloatingPoint()) {
543 // FP values need to be bitcast, then extended if they are being put
544 // into a larger container.
545 ValueVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
546 Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
547 }
548 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
549 ValueVT.isInteger() &&
550 "Unknown mismatch!");
551 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
552 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
553 if (PartVT == MVT::x86mmx)
554 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
555 }
556 } else if (PartBits == ValueVT.getSizeInBits()) {
557 // Different types of the same size.
558 assert(NumParts == 1 && PartEVT != ValueVT);
559 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
560 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
561 // If the parts cover less bits than value has, truncate the value.
562 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
563 ValueVT.isInteger() &&
564 "Unknown mismatch!");
565 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
566 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
567 if (PartVT == MVT::x86mmx)
568 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
569 }
570
571 // The value may have changed - recompute ValueVT.
572 ValueVT = Val.getValueType();
573 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
574 "Failed to tile the value with PartVT!");
575
576 if (NumParts == 1) {
577 if (PartEVT != ValueVT) {
579 "scalar-to-vector conversion failed");
580 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
581 }
582
583 Parts[0] = Val;
584 return;
585 }
586
587 // Expand the value into multiple parts.
588 if (NumParts & (NumParts - 1)) {
589 // The number of parts is not a power of 2. Split off and copy the tail.
590 assert(PartVT.isInteger() && ValueVT.isInteger() &&
591 "Do not know what to expand to!");
592 unsigned RoundParts = llvm::bit_floor(NumParts);
593 unsigned RoundBits = RoundParts * PartBits;
594 unsigned OddParts = NumParts - RoundParts;
595 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
596 DAG.getShiftAmountConstant(RoundBits, ValueVT, DL));
597
598 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V,
599 CallConv);
600
601 if (DAG.getDataLayout().isBigEndian())
602 // The odd parts were reversed by getCopyToParts - unreverse them.
603 std::reverse(Parts + RoundParts, Parts + NumParts);
604
605 NumParts = RoundParts;
606 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
607 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
608 }
609
610 // The number of parts is a power of 2. Repeatedly bisect the value using
611 // EXTRACT_ELEMENT.
612 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
614 ValueVT.getSizeInBits()),
615 Val);
616
617 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
618 for (unsigned i = 0; i < NumParts; i += StepSize) {
619 unsigned ThisBits = StepSize * PartBits / 2;
620 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
621 SDValue &Part0 = Parts[i];
622 SDValue &Part1 = Parts[i+StepSize/2];
623
624 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
625 ThisVT, Part0, DAG.getIntPtrConstant(1, DL));
626 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
627 ThisVT, Part0, DAG.getIntPtrConstant(0, DL));
628
629 if (ThisBits == PartBits && ThisVT != PartVT) {
630 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
631 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
632 }
633 }
634 }
635
636 if (DAG.getDataLayout().isBigEndian())
637 std::reverse(Parts, Parts + OrigNumParts);
638}
639
641 const SDLoc &DL, EVT PartVT) {
642 if (!PartVT.isVector())
643 return SDValue();
644
645 EVT ValueVT = Val.getValueType();
646 EVT PartEVT = PartVT.getVectorElementType();
647 EVT ValueEVT = ValueVT.getVectorElementType();
648 ElementCount PartNumElts = PartVT.getVectorElementCount();
649 ElementCount ValueNumElts = ValueVT.getVectorElementCount();
650
651 // We only support widening vectors with equivalent element types and
652 // fixed/scalable properties. If a target needs to widen a fixed-length type
653 // to a scalable one, it should be possible to use INSERT_SUBVECTOR below.
654 if (ElementCount::isKnownLE(PartNumElts, ValueNumElts) ||
655 PartNumElts.isScalable() != ValueNumElts.isScalable())
656 return SDValue();
657
658 // Have a try for bf16 because some targets share its ABI with fp16.
659 if (ValueEVT == MVT::bf16 && PartEVT == MVT::f16) {
661 "Cannot widen to illegal type");
662 Val = DAG.getNode(ISD::BITCAST, DL,
663 ValueVT.changeVectorElementType(MVT::f16), Val);
664 } else if (PartEVT != ValueEVT) {
665 return SDValue();
666 }
667
668 // Widening a scalable vector to another scalable vector is done by inserting
669 // the vector into a larger undef one.
670 if (PartNumElts.isScalable())
671 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT),
672 Val, DAG.getVectorIdxConstant(0, DL));
673
674 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
675 // undef elements.
677 DAG.ExtractVectorElements(Val, Ops);
678 SDValue EltUndef = DAG.getUNDEF(PartEVT);
679 Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef);
680
681 // FIXME: Use CONCAT for 2x -> 4x.
682 return DAG.getBuildVector(PartVT, DL, Ops);
683}
684
685/// getCopyToPartsVector - Create a series of nodes that contain the specified
686/// value split into legal parts.
687static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL,
688 SDValue Val, SDValue *Parts, unsigned NumParts,
689 MVT PartVT, const Value *V,
690 std::optional<CallingConv::ID> CallConv) {
691 EVT ValueVT = Val.getValueType();
692 assert(ValueVT.isVector() && "Not a vector");
693 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
694 const bool IsABIRegCopy = CallConv.has_value();
695
696 if (NumParts == 1) {
697 EVT PartEVT = PartVT;
698 if (PartEVT == ValueVT) {
699 // Nothing to do.
700 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
701 // Bitconvert vector->vector case.
702 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
703 } else if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, PartVT)) {
704 Val = Widened;
705 } else if (PartVT.isVector() &&
707 ValueVT.getVectorElementType()) &&
708 PartEVT.getVectorElementCount() ==
709 ValueVT.getVectorElementCount()) {
710
711 // Promoted vector extract
712 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
713 } else if (PartEVT.isVector() &&
714 PartEVT.getVectorElementType() !=
715 ValueVT.getVectorElementType() &&
716 TLI.getTypeAction(*DAG.getContext(), ValueVT) ==
718 // Combination of widening and promotion.
719 EVT WidenVT =
721 PartVT.getVectorElementCount());
722 SDValue Widened = widenVectorToPartType(DAG, Val, DL, WidenVT);
723 Val = DAG.getAnyExtOrTrunc(Widened, DL, PartVT);
724 } else {
725 // Don't extract an integer from a float vector. This can happen if the
726 // FP type gets softened to integer and then promoted. The promotion
727 // prevents it from being picked up by the earlier bitcast case.
728 if (ValueVT.getVectorElementCount().isScalar() &&
729 (!ValueVT.isFloatingPoint() || !PartVT.isInteger())) {
730 // If we reach this condition and PartVT is FP, this means that
731 // ValueVT is also FP and both have a different size, otherwise we
732 // would have bitcasted them. Producing an EXTRACT_VECTOR_ELT here
733 // would be invalid since that would mean the smaller FP type has to
734 // be extended to the larger one.
735 if (PartVT.isFloatingPoint()) {
736 Val = DAG.getBitcast(ValueVT.getScalarType(), Val);
737 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
738 } else
739 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val,
740 DAG.getVectorIdxConstant(0, DL));
741 } else {
742 uint64_t ValueSize = ValueVT.getFixedSizeInBits();
743 assert(PartVT.getFixedSizeInBits() > ValueSize &&
744 "lossy conversion of vector to scalar type");
745 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
746 Val = DAG.getBitcast(IntermediateType, Val);
747 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
748 }
749 }
750
751 assert(Val.getValueType() == PartVT && "Unexpected vector part value type");
752 Parts[0] = Val;
753 return;
754 }
755
756 // Handle a multi-element vector.
757 EVT IntermediateVT;
758 MVT RegisterVT;
759 unsigned NumIntermediates;
760 unsigned NumRegs;
761 if (IsABIRegCopy) {
763 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT, NumIntermediates,
764 RegisterVT);
765 } else {
766 NumRegs =
767 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
768 NumIntermediates, RegisterVT);
769 }
770
771 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
772 NumParts = NumRegs; // Silence a compiler warning.
773 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
774
775 assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() &&
776 "Mixing scalable and fixed vectors when copying in parts");
777
778 std::optional<ElementCount> DestEltCnt;
779
780 if (IntermediateVT.isVector())
781 DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates;
782 else
783 DestEltCnt = ElementCount::getFixed(NumIntermediates);
784
785 EVT BuiltVectorTy = EVT::getVectorVT(
786 *DAG.getContext(), IntermediateVT.getScalarType(), *DestEltCnt);
787
788 if (ValueVT == BuiltVectorTy) {
789 // Nothing to do.
790 } else if (ValueVT.getSizeInBits() == BuiltVectorTy.getSizeInBits()) {
791 // Bitconvert vector->vector case.
792 Val = DAG.getNode(ISD::BITCAST, DL, BuiltVectorTy, Val);
793 } else {
794 if (BuiltVectorTy.getVectorElementType().bitsGT(
795 ValueVT.getVectorElementType())) {
796 // Integer promotion.
797 ValueVT = EVT::getVectorVT(*DAG.getContext(),
798 BuiltVectorTy.getVectorElementType(),
799 ValueVT.getVectorElementCount());
800 Val = DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
801 }
802
803 if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy)) {
804 Val = Widened;
805 }
806 }
807
808 assert(Val.getValueType() == BuiltVectorTy && "Unexpected vector value type");
809
810 // Split the vector into intermediate operands.
811 SmallVector<SDValue, 8> Ops(NumIntermediates);
812 for (unsigned i = 0; i != NumIntermediates; ++i) {
813 if (IntermediateVT.isVector()) {
814 // This does something sensible for scalable vectors - see the
815 // definition of EXTRACT_SUBVECTOR for further details.
816 unsigned IntermediateNumElts = IntermediateVT.getVectorMinNumElements();
817 Ops[i] =
818 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val,
819 DAG.getVectorIdxConstant(i * IntermediateNumElts, DL));
820 } else {
821 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val,
822 DAG.getVectorIdxConstant(i, DL));
823 }
824 }
825
826 // Split the intermediate operands into legal parts.
827 if (NumParts == NumIntermediates) {
828 // If the register was not expanded, promote or copy the value,
829 // as appropriate.
830 for (unsigned i = 0; i != NumParts; ++i)
831 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V, CallConv);
832 } else if (NumParts > 0) {
833 // If the intermediate type was expanded, split each the value into
834 // legal parts.
835 assert(NumIntermediates != 0 && "division by zero");
836 assert(NumParts % NumIntermediates == 0 &&
837 "Must expand into a divisible number of parts!");
838 unsigned Factor = NumParts / NumIntermediates;
839 for (unsigned i = 0; i != NumIntermediates; ++i)
840 getCopyToParts(DAG, DL, Ops[i], &Parts[i * Factor], Factor, PartVT, V,
841 CallConv);
842 }
843}
844
845static void failForInvalidBundles(const CallBase &I, StringRef Name,
846 ArrayRef<uint32_t> AllowedBundles) {
847 if (I.hasOperandBundlesOtherThan(AllowedBundles)) {
848 ListSeparator LS;
849 std::string Error;
851 for (unsigned i = 0, e = I.getNumOperandBundles(); i != e; ++i) {
852 OperandBundleUse U = I.getOperandBundleAt(i);
853 if (!is_contained(AllowedBundles, U.getTagID()))
854 OS << LS << U.getTagName();
855 }
857 Twine("cannot lower ", Name)
858 .concat(Twine(" with arbitrary operand bundles: ", Error)));
859 }
860}
861
863 EVT valuevt, std::optional<CallingConv::ID> CC)
864 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs),
865 RegCount(1, regs.size()), CallConv(CC) {}
866
868 const DataLayout &DL, Register Reg, Type *Ty,
869 std::optional<CallingConv::ID> CC) {
870 ComputeValueVTs(TLI, DL, Ty, ValueVTs);
871
872 CallConv = CC;
873
874 for (EVT ValueVT : ValueVTs) {
875 unsigned NumRegs =
877 ? TLI.getNumRegistersForCallingConv(Context, *CC, ValueVT)
878 : TLI.getNumRegisters(Context, ValueVT);
879 MVT RegisterVT =
881 ? TLI.getRegisterTypeForCallingConv(Context, *CC, ValueVT)
882 : TLI.getRegisterType(Context, ValueVT);
883 for (unsigned i = 0; i != NumRegs; ++i)
884 Regs.push_back(Reg + i);
885 RegVTs.push_back(RegisterVT);
886 RegCount.push_back(NumRegs);
887 Reg = Reg.id() + NumRegs;
888 }
889}
890
892 FunctionLoweringInfo &FuncInfo,
893 const SDLoc &dl, SDValue &Chain,
894 SDValue *Glue, const Value *V) const {
895 // A Value with type {} or [0 x %t] needs no registers.
896 if (ValueVTs.empty())
897 return SDValue();
898
899 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
900
901 // Assemble the legal parts into the final values.
902 SmallVector<SDValue, 4> Values(ValueVTs.size());
904 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
905 // Copy the legal parts from the registers.
906 EVT ValueVT = ValueVTs[Value];
907 unsigned NumRegs = RegCount[Value];
908 MVT RegisterVT = isABIMangled()
910 *DAG.getContext(), *CallConv, RegVTs[Value])
911 : RegVTs[Value];
912
913 Parts.resize(NumRegs);
914 for (unsigned i = 0; i != NumRegs; ++i) {
915 SDValue P;
916 if (!Glue) {
917 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
918 } else {
919 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Glue);
920 *Glue = P.getValue(2);
921 }
922
923 Chain = P.getValue(1);
924 Parts[i] = P;
925
926 // If the source register was virtual and if we know something about it,
927 // add an assert node.
928 if (!Regs[Part + i].isVirtual() || !RegisterVT.isInteger())
929 continue;
930
932 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
933 if (!LOI)
934 continue;
935
936 unsigned RegSize = RegisterVT.getScalarSizeInBits();
937 unsigned NumSignBits = LOI->NumSignBits;
938 unsigned NumZeroBits = LOI->Known.countMinLeadingZeros();
939
940 if (NumZeroBits == RegSize) {
941 // The current value is a zero.
942 // Explicitly express that as it would be easier for
943 // optimizations to kick in.
944 Parts[i] = DAG.getConstant(0, dl, RegisterVT);
945 continue;
946 }
947
948 // FIXME: We capture more information than the dag can represent. For
949 // now, just use the tightest assertzext/assertsext possible.
950 bool isSExt;
951 EVT FromVT(MVT::Other);
952 if (NumZeroBits) {
953 FromVT = EVT::getIntegerVT(*DAG.getContext(), RegSize - NumZeroBits);
954 isSExt = false;
955 } else if (NumSignBits > 1) {
956 FromVT =
957 EVT::getIntegerVT(*DAG.getContext(), RegSize - NumSignBits + 1);
958 isSExt = true;
959 } else {
960 continue;
961 }
962 // Add an assertion node.
963 assert(FromVT != MVT::Other);
964 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
965 RegisterVT, P, DAG.getValueType(FromVT));
966 }
967
968 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), NumRegs,
969 RegisterVT, ValueVT, V, Chain, CallConv);
970 Part += NumRegs;
971 Parts.clear();
972 }
973
974 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values);
975}
976
978 const SDLoc &dl, SDValue &Chain, SDValue *Glue,
979 const Value *V,
980 ISD::NodeType PreferredExtendType) const {
981 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
982 ISD::NodeType ExtendKind = PreferredExtendType;
983
984 // Get the list of the values's legal parts.
985 unsigned NumRegs = Regs.size();
986 SmallVector<SDValue, 8> Parts(NumRegs);
987 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
988 unsigned NumParts = RegCount[Value];
989
990 MVT RegisterVT = isABIMangled()
992 *DAG.getContext(), *CallConv, RegVTs[Value])
993 : RegVTs[Value];
994
995 if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT))
996 ExtendKind = ISD::ZERO_EXTEND;
997
998 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
999 NumParts, RegisterVT, V, CallConv, ExtendKind);
1000 Part += NumParts;
1001 }
1002
1003 // Copy the parts into the registers.
1004 SmallVector<SDValue, 8> Chains(NumRegs);
1005 for (unsigned i = 0; i != NumRegs; ++i) {
1006 SDValue Part;
1007 if (!Glue) {
1008 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
1009 } else {
1010 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue);
1011 *Glue = Part.getValue(1);
1012 }
1013
1014 Chains[i] = Part.getValue(0);
1015 }
1016
1017 if (NumRegs == 1 || Glue)
1018 // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is
1019 // flagged to it. That is the CopyToReg nodes and the user are considered
1020 // a single scheduling unit. If we create a TokenFactor and return it as
1021 // chain, then the TokenFactor is both a predecessor (operand) of the
1022 // user as well as a successor (the TF operands are flagged to the user).
1023 // c1, f1 = CopyToReg
1024 // c2, f2 = CopyToReg
1025 // c3 = TokenFactor c1, c2
1026 // ...
1027 // = op c3, ..., f2
1028 Chain = Chains[NumRegs-1];
1029 else
1030 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1031}
1032
1034 unsigned MatchingIdx, const SDLoc &dl,
1035 SelectionDAG &DAG,
1036 std::vector<SDValue> &Ops) const {
1037 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1038
1039 InlineAsm::Flag Flag(Code, Regs.size());
1040 if (HasMatching)
1041 Flag.setMatchingOp(MatchingIdx);
1042 else if (!Regs.empty() && Regs.front().isVirtual()) {
1043 // Put the register class of the virtual registers in the flag word. That
1044 // way, later passes can recompute register class constraints for inline
1045 // assembly as well as normal instructions.
1046 // Don't do this for tied operands that can use the regclass information
1047 // from the def.
1049 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
1050 Flag.setRegClass(RC->getID());
1051 }
1052
1053 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
1054 Ops.push_back(Res);
1055
1056 if (Code == InlineAsm::Kind::Clobber) {
1057 // Clobbers should always have a 1:1 mapping with registers, and may
1058 // reference registers that have illegal (e.g. vector) types. Hence, we
1059 // shouldn't try to apply any sort of splitting logic to them.
1060 assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
1061 "No 1:1 mapping from clobbers to regs?");
1063 (void)SP;
1064 for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
1065 Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
1066 assert(
1067 (Regs[I] != SP ||
1069 "If we clobbered the stack pointer, MFI should know about it.");
1070 }
1071 return;
1072 }
1073
1074 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1075 MVT RegisterVT = RegVTs[Value];
1076 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1077 RegisterVT);
1078 for (unsigned i = 0; i != NumRegs; ++i) {
1079 assert(Reg < Regs.size() && "Mismatch in # registers expected");
1080 Register TheReg = Regs[Reg++];
1081 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1082 }
1083 }
1084}
1085
1089 unsigned I = 0;
1090 for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1091 unsigned RegCount = std::get<0>(CountAndVT);
1092 MVT RegisterVT = std::get<1>(CountAndVT);
1093 TypeSize RegisterSize = RegisterVT.getSizeInBits();
1094 for (unsigned E = I + RegCount; I != E; ++I)
1095 OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1096 }
1097 return OutVec;
1098}
1099
1101 AssumptionCache *ac, const TargetLibraryInfo *li,
1102 const TargetTransformInfo &TTI) {
1103 BatchAA = aa;
1104 AC = ac;
1105 GFI = gfi;
1106 LibInfo = li;
1107 Context = DAG.getContext();
1108 LPadToCallSiteMap.clear();
1109 this->TTI = &TTI;
1110 SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1111 AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
1112 *DAG.getMachineFunction().getFunction().getParent());
1113}
1114
1116 NodeMap.clear();
1117 UnusedArgNodeMap.clear();
1118 PendingLoads.clear();
1119 PendingExports.clear();
1120 PendingConstrainedFP.clear();
1121 PendingConstrainedFPStrict.clear();
1122 CurInst = nullptr;
1123 HasTailCall = false;
1124 SDNodeOrder = LowestSDNodeOrder;
1125 StatepointLowering.clear();
1126}
1127
1129 DanglingDebugInfoMap.clear();
1130}
1131
1132// Update DAG root to include dependencies on Pending chains.
1133SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1134 SDValue Root = DAG.getRoot();
1135
1136 if (Pending.empty())
1137 return Root;
1138
1139 // Add current root to PendingChains, unless we already indirectly
1140 // depend on it.
1141 if (Root.getOpcode() != ISD::EntryToken) {
1142 unsigned i = 0, e = Pending.size();
1143 for (; i != e; ++i) {
1144 assert(Pending[i].getNode()->getNumOperands() > 1);
1145 if (Pending[i].getNode()->getOperand(0) == Root)
1146 break; // Don't add the root if we already indirectly depend on it.
1147 }
1148
1149 if (i == e)
1150 Pending.push_back(Root);
1151 }
1152
1153 if (Pending.size() == 1)
1154 Root = Pending[0];
1155 else
1156 Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1157
1158 DAG.setRoot(Root);
1159 Pending.clear();
1160 return Root;
1161}
1162
1166
1168 // If the new exception behavior differs from that of the pending
1169 // ones, chain up them and update the root.
1170 switch (EB) {
1173 // Floating-point exceptions produced by such operations are not intended
1174 // to be observed, so the sequence of these operations does not need to be
1175 // preserved.
1176 //
1177 // They however must not be mixed with the instructions that have strict
1178 // exception behavior. Placing an operation with 'ebIgnore' behavior between
1179 // 'ebStrict' operations could distort the observed exception behavior.
1180 if (!PendingConstrainedFPStrict.empty()) {
1181 assert(PendingConstrainedFP.empty());
1182 updateRoot(PendingConstrainedFPStrict);
1183 }
1184 break;
1186 // Floating-point exception produced by these operations may be observed, so
1187 // they must be correctly chained. If trapping on FP exceptions is
1188 // disabled, the exceptions can be observed only by functions that read
1189 // exception flags, like 'llvm.get_fpenv' or 'fetestexcept'. It means that
1190 // the order of operations is not significant between barriers.
1191 //
1192 // If trapping is enabled, each operation becomes an implicit observation
1193 // point, so the operations must be sequenced according their original
1194 // source order.
1195 if (!PendingConstrainedFP.empty()) {
1196 assert(PendingConstrainedFPStrict.empty());
1197 updateRoot(PendingConstrainedFP);
1198 }
1199 // TODO: Add support for trapping-enabled scenarios.
1200 }
1201 return DAG.getRoot();
1202}
1203
1205 // Chain up all pending constrained intrinsics together with all
1206 // pending loads, by simply appending them to PendingLoads and
1207 // then calling getMemoryRoot().
1208 PendingLoads.reserve(PendingLoads.size() +
1209 PendingConstrainedFP.size() +
1210 PendingConstrainedFPStrict.size());
1211 PendingLoads.append(PendingConstrainedFP.begin(),
1212 PendingConstrainedFP.end());
1213 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1214 PendingConstrainedFPStrict.end());
1215 PendingConstrainedFP.clear();
1216 PendingConstrainedFPStrict.clear();
1217 return getMemoryRoot();
1218}
1219
1221 // We need to emit pending fpexcept.strict constrained intrinsics,
1222 // so append them to the PendingExports list.
1223 PendingExports.append(PendingConstrainedFPStrict.begin(),
1224 PendingConstrainedFPStrict.end());
1225 PendingConstrainedFPStrict.clear();
1226 return updateRoot(PendingExports);
1227}
1228
1230 DILocalVariable *Variable,
1232 DebugLoc DL) {
1233 assert(Variable && "Missing variable");
1234
1235 // Check if address has undef value.
1236 if (!Address || isa<UndefValue>(Address) ||
1237 (Address->use_empty() && !isa<Argument>(Address))) {
1238 LLVM_DEBUG(
1239 dbgs()
1240 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1241 return;
1242 }
1243
1244 bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1245
1246 SDValue &N = NodeMap[Address];
1247 if (!N.getNode() && isa<Argument>(Address))
1248 // Check unused arguments map.
1249 N = UnusedArgNodeMap[Address];
1250 SDDbgValue *SDV;
1251 if (N.getNode()) {
1252 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1253 Address = BCI->getOperand(0);
1254 // Parameters are handled specially.
1255 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1256 if (IsParameter && FINode) {
1257 // Byval parameter. We have a frame index at this point.
1258 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1259 /*IsIndirect*/ true, DL, SDNodeOrder);
1260 } else if (isa<Argument>(Address)) {
1261 // Address is an argument, so try to emit its dbg value using
1262 // virtual register info from the FuncInfo.ValueMap.
1263 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1264 FuncArgumentDbgValueKind::Declare, N);
1265 return;
1266 } else {
1267 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1268 true, DL, SDNodeOrder);
1269 }
1270 DAG.AddDbgValue(SDV, IsParameter);
1271 } else {
1272 // If Address is an argument then try to emit its dbg value using
1273 // virtual register info from the FuncInfo.ValueMap.
1274 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1275 FuncArgumentDbgValueKind::Declare, N)) {
1276 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1277 << " (could not emit func-arg dbg_value)\n");
1278 }
1279 }
1280}
1281
1283 // Add SDDbgValue nodes for any var locs here. Do so before updating
1284 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1285 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1286 // Add SDDbgValue nodes for any var locs here. Do so before updating
1287 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1288 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1289 It != End; ++It) {
1290 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1291 dropDanglingDebugInfo(Var, It->Expr);
1292 if (It->Values.isKillLocation(It->Expr)) {
1293 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1294 continue;
1295 }
1296 SmallVector<Value *> Values(It->Values.location_ops());
1297 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1298 It->Values.hasArgList())) {
1299 SmallVector<Value *, 4> Vals(It->Values.location_ops());
1301 FnVarLocs->getDILocalVariable(It->VariableID),
1302 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1303 }
1304 }
1305 }
1306
1307 // We must skip DbgVariableRecords if they've already been processed above as
1308 // we have just emitted the debug values resulting from assignment tracking
1309 // analysis, making any existing DbgVariableRecords redundant (and probably
1310 // less correct). We still need to process DbgLabelRecords. This does sink
1311 // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1312 // be important as it does so deterministcally and ordering between
1313 // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1314 // printing).
1315 bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1316 // Is there is any debug-info attached to this instruction, in the form of
1317 // DbgRecord non-instruction debug-info records.
1318 for (DbgRecord &DR : I.getDbgRecordRange()) {
1319 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1320 assert(DLR->getLabel() && "Missing label");
1321 SDDbgLabel *SDV =
1322 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1323 DAG.AddDbgLabel(SDV);
1324 continue;
1325 }
1326
1327 if (SkipDbgVariableRecords)
1328 continue;
1330 DILocalVariable *Variable = DVR.getVariable();
1333
1335 if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1336 continue;
1337 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1338 << "\n");
1340 DVR.getDebugLoc());
1341 continue;
1342 }
1343
1344 // A DbgVariableRecord with no locations is a kill location.
1346 if (Values.empty()) {
1348 SDNodeOrder);
1349 continue;
1350 }
1351
1352 // A DbgVariableRecord with an undef or absent location is also a kill
1353 // location.
1354 if (llvm::any_of(Values,
1355 [](Value *V) { return !V || isa<UndefValue>(V); })) {
1357 SDNodeOrder);
1358 continue;
1359 }
1360
1361 bool IsVariadic = DVR.hasArgList();
1362 if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1363 SDNodeOrder, IsVariadic)) {
1364 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1365 DVR.getDebugLoc(), SDNodeOrder);
1366 }
1367 }
1368}
1369
1371 visitDbgInfo(I);
1372
1373 // Set up outgoing PHI node register values before emitting the terminator.
1374 if (I.isTerminator()) {
1375 HandlePHINodesInSuccessorBlocks(I.getParent());
1376 }
1377
1378 ++SDNodeOrder;
1379 CurInst = &I;
1380
1381 // Set inserted listener only if required.
1382 bool NodeInserted = false;
1383 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1384 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1385 MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1386 if (PCSectionsMD || MMRA) {
1387 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1388 DAG, [&](SDNode *) { NodeInserted = true; });
1389 }
1390
1391 visit(I.getOpcode(), I);
1392
1393 if (!I.isTerminator() && !HasTailCall &&
1394 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1396
1397 // Handle metadata.
1398 if (PCSectionsMD || MMRA) {
1399 auto It = NodeMap.find(&I);
1400 if (It != NodeMap.end()) {
1401 if (PCSectionsMD)
1402 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1403 if (MMRA)
1404 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1405 } else if (NodeInserted) {
1406 // This should not happen; if it does, don't let it go unnoticed so we can
1407 // fix it. Relevant visit*() function is probably missing a setValue().
1408 errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1409 << I.getModule()->getName() << "]\n";
1410 LLVM_DEBUG(I.dump());
1411 assert(false);
1412 }
1413 }
1414
1415 CurInst = nullptr;
1416}
1417
1418void SelectionDAGBuilder::visitPHI(const PHINode &) {
1419 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1420}
1421
1422void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1423 // Note: this doesn't use InstVisitor, because it has to work with
1424 // ConstantExpr's in addition to instructions.
1425 switch (Opcode) {
1426 default: llvm_unreachable("Unknown instruction type encountered!");
1427 // Build the switch statement using the Instruction.def file.
1428#define HANDLE_INST(NUM, OPCODE, CLASS) \
1429 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1430#include "llvm/IR/Instruction.def"
1431 }
1432}
1433
1435 DILocalVariable *Variable,
1436 DebugLoc DL, unsigned Order,
1439 // For variadic dbg_values we will now insert poison.
1440 // FIXME: We can potentially recover these!
1442 for (const Value *V : Values) {
1443 auto *Poison = PoisonValue::get(V->getType());
1445 }
1446 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1447 /*IsIndirect=*/false, DL, Order,
1448 /*IsVariadic=*/true);
1449 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1450 return true;
1451}
1452
1454 DILocalVariable *Var,
1455 DIExpression *Expr,
1456 bool IsVariadic, DebugLoc DL,
1457 unsigned Order) {
1458 if (IsVariadic) {
1459 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1460 return;
1461 }
1462 // TODO: Dangling debug info will eventually either be resolved or produce
1463 // a poison DBG_VALUE. However in the resolution case, a gap may appear
1464 // between the original dbg.value location and its resolved DBG_VALUE,
1465 // which we should ideally fill with an extra poison DBG_VALUE.
1466 assert(Values.size() == 1);
1467 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1468}
1469
1471 const DIExpression *Expr) {
1472 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1473 DIVariable *DanglingVariable = DDI.getVariable();
1474 DIExpression *DanglingExpr = DDI.getExpression();
1475 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1476 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1477 << printDDI(nullptr, DDI) << "\n");
1478 return true;
1479 }
1480 return false;
1481 };
1482
1483 for (auto &DDIMI : DanglingDebugInfoMap) {
1484 DanglingDebugInfoVector &DDIV = DDIMI.second;
1485
1486 // If debug info is to be dropped, run it through final checks to see
1487 // whether it can be salvaged.
1488 for (auto &DDI : DDIV)
1489 if (isMatchingDbgValue(DDI))
1490 salvageUnresolvedDbgValue(DDIMI.first, DDI);
1491
1492 erase_if(DDIV, isMatchingDbgValue);
1493 }
1494}
1495
1496// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1497// generate the debug data structures now that we've seen its definition.
1499 SDValue Val) {
1500 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1501 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1502 return;
1503
1504 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1505 for (auto &DDI : DDIV) {
1506 DebugLoc DL = DDI.getDebugLoc();
1507 unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1508 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1509 DILocalVariable *Variable = DDI.getVariable();
1510 DIExpression *Expr = DDI.getExpression();
1511 assert(Variable->isValidLocationForIntrinsic(DL) &&
1512 "Expected inlined-at fields to agree");
1513 SDDbgValue *SDV;
1514 if (Val.getNode()) {
1515 // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1516 // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1517 // we couldn't resolve it directly when examining the DbgValue intrinsic
1518 // in the first place we should not be more successful here). Unless we
1519 // have some test case that prove this to be correct we should avoid
1520 // calling EmitFuncArgumentDbgValue here.
1521 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1522 FuncArgumentDbgValueKind::Value, Val)) {
1523 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1524 << printDDI(V, DDI) << "\n");
1525 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump());
1526 // Increase the SDNodeOrder for the DbgValue here to make sure it is
1527 // inserted after the definition of Val when emitting the instructions
1528 // after ISel. An alternative could be to teach
1529 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1530 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1531 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1532 << ValSDNodeOrder << "\n");
1533 SDV = getDbgValue(Val, Variable, Expr, DL,
1534 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1535 DAG.AddDbgValue(SDV, false);
1536 } else
1537 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1538 << printDDI(V, DDI)
1539 << " in EmitFuncArgumentDbgValue\n");
1540 } else {
1541 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1542 << "\n");
1543 auto Poison = PoisonValue::get(V->getType());
1544 auto SDV =
1545 DAG.getConstantDbgValue(Variable, Expr, Poison, DL, DbgSDNodeOrder);
1546 DAG.AddDbgValue(SDV, false);
1547 }
1548 }
1549 DDIV.clear();
1550}
1551
1553 DanglingDebugInfo &DDI) {
1554 // TODO: For the variadic implementation, instead of only checking the fail
1555 // state of `handleDebugValue`, we need know specifically which values were
1556 // invalid, so that we attempt to salvage only those values when processing
1557 // a DIArgList.
1558 const Value *OrigV = V;
1559 DILocalVariable *Var = DDI.getVariable();
1560 DIExpression *Expr = DDI.getExpression();
1561 DebugLoc DL = DDI.getDebugLoc();
1562 unsigned SDOrder = DDI.getSDNodeOrder();
1563
1564 // Currently we consider only dbg.value intrinsics -- we tell the salvager
1565 // that DW_OP_stack_value is desired.
1566 bool StackValue = true;
1567
1568 // Can this Value can be encoded without any further work?
1569 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1570 return;
1571
1572 // Attempt to salvage back through as many instructions as possible. Bail if
1573 // a non-instruction is seen, such as a constant expression or global
1574 // variable. FIXME: Further work could recover those too.
1575 while (isa<Instruction>(V)) {
1576 const Instruction &VAsInst = *cast<const Instruction>(V);
1577 // Temporary "0", awaiting real implementation.
1579 SmallVector<Value *, 4> AdditionalValues;
1580 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1581 Expr->getNumLocationOperands(), Ops,
1582 AdditionalValues);
1583 // If we cannot salvage any further, and haven't yet found a suitable debug
1584 // expression, bail out.
1585 if (!V)
1586 break;
1587
1588 // TODO: If AdditionalValues isn't empty, then the salvage can only be
1589 // represented with a DBG_VALUE_LIST, so we give up. When we have support
1590 // here for variadic dbg_values, remove that condition.
1591 if (!AdditionalValues.empty())
1592 break;
1593
1594 // New value and expr now represent this debuginfo.
1595 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1596
1597 // Some kind of simplification occurred: check whether the operand of the
1598 // salvaged debug expression can be encoded in this DAG.
1599 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1600 LLVM_DEBUG(
1601 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n"
1602 << *OrigV << "\nBy stripping back to:\n " << *V << "\n");
1603 return;
1604 }
1605 }
1606
1607 // This was the final opportunity to salvage this debug information, and it
1608 // couldn't be done. Place a poison DBG_VALUE at this location to terminate
1609 // any earlier variable location.
1610 assert(OrigV && "V shouldn't be null");
1611 auto *Poison = PoisonValue::get(OrigV->getType());
1612 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Poison, DL, SDNodeOrder);
1613 DAG.AddDbgValue(SDV, false);
1614 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n "
1615 << printDDI(OrigV, DDI) << "\n");
1616}
1617
1619 DIExpression *Expr,
1620 DebugLoc DbgLoc,
1621 unsigned Order) {
1625 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1626 /*IsVariadic*/ false);
1627}
1628
1630 DILocalVariable *Var,
1631 DIExpression *Expr, DebugLoc DbgLoc,
1632 unsigned Order, bool IsVariadic) {
1633 if (Values.empty())
1634 return true;
1635
1636 // Filter EntryValue locations out early.
1637 if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1638 return true;
1639
1640 SmallVector<SDDbgOperand> LocationOps;
1641 SmallVector<SDNode *> Dependencies;
1642 for (const Value *V : Values) {
1643 // Constant value.
1646 LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1647 continue;
1648 }
1649
1650 // Look through IntToPtr constants.
1651 if (auto *CE = dyn_cast<ConstantExpr>(V))
1652 if (CE->getOpcode() == Instruction::IntToPtr) {
1653 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1654 continue;
1655 }
1656
1657 // If the Value is a frame index, we can create a FrameIndex debug value
1658 // without relying on the DAG at all.
1659 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1660 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1661 if (SI != FuncInfo.StaticAllocaMap.end()) {
1662 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1663 continue;
1664 }
1665 }
1666
1667 // Do not use getValue() in here; we don't want to generate code at
1668 // this point if it hasn't been done yet.
1669 SDValue N = NodeMap[V];
1670 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1671 N = UnusedArgNodeMap[V];
1672
1673 if (N.getNode()) {
1674 // Only emit func arg dbg value for non-variadic dbg.values for now.
1675 if (!IsVariadic &&
1676 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1677 FuncArgumentDbgValueKind::Value, N))
1678 return true;
1679 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1680 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1681 // describe stack slot locations.
1682 //
1683 // Consider "int x = 0; int *px = &x;". There are two kinds of
1684 // interesting debug values here after optimization:
1685 //
1686 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
1687 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1688 //
1689 // Both describe the direct values of their associated variables.
1690 Dependencies.push_back(N.getNode());
1691 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1692 continue;
1693 }
1694 LocationOps.emplace_back(
1695 SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1696 continue;
1697 }
1698
1699 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1700 // Special rules apply for the first dbg.values of parameter variables in a
1701 // function. Identify them by the fact they reference Argument Values, that
1702 // they're parameters, and they are parameters of the current function. We
1703 // need to let them dangle until they get an SDNode.
1704 bool IsParamOfFunc =
1705 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1706 if (IsParamOfFunc)
1707 return false;
1708
1709 // The value is not used in this block yet (or it would have an SDNode).
1710 // We still want the value to appear for the user if possible -- if it has
1711 // an associated VReg, we can refer to that instead.
1712 auto VMI = FuncInfo.ValueMap.find(V);
1713 if (VMI != FuncInfo.ValueMap.end()) {
1714 Register Reg = VMI->second;
1715 // If this is a PHI node, it may be split up into several MI PHI nodes
1716 // (in FunctionLoweringInfo::set).
1717 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1718 V->getType(), std::nullopt);
1719 if (RFV.occupiesMultipleRegs()) {
1720 // FIXME: We could potentially support variadic dbg_values here.
1721 if (IsVariadic)
1722 return false;
1723 unsigned Offset = 0;
1724 unsigned BitsToDescribe = 0;
1725 if (auto VarSize = Var->getSizeInBits())
1726 BitsToDescribe = *VarSize;
1727 if (auto Fragment = Expr->getFragmentInfo())
1728 BitsToDescribe = Fragment->SizeInBits;
1729 for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1730 // Bail out if all bits are described already.
1731 if (Offset >= BitsToDescribe)
1732 break;
1733 // TODO: handle scalable vectors.
1734 unsigned RegisterSize = RegAndSize.second;
1735 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1736 ? BitsToDescribe - Offset
1737 : RegisterSize;
1738 auto FragmentExpr = DIExpression::createFragmentExpression(
1739 Expr, Offset, FragmentSize);
1740 if (!FragmentExpr)
1741 continue;
1742 SDDbgValue *SDV = DAG.getVRegDbgValue(
1743 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1744 DAG.AddDbgValue(SDV, false);
1745 Offset += RegisterSize;
1746 }
1747 return true;
1748 }
1749 // We can use simple vreg locations for variadic dbg_values as well.
1750 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1751 continue;
1752 }
1753 // We failed to create a SDDbgOperand for V.
1754 return false;
1755 }
1756
1757 // We have created a SDDbgOperand for each Value in Values.
1758 assert(!LocationOps.empty());
1759 SDDbgValue *SDV =
1760 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1761 /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1762 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1763 return true;
1764}
1765
1767 // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1768 for (auto &Pair : DanglingDebugInfoMap)
1769 for (auto &DDI : Pair.second)
1770 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1772}
1773
1774/// getCopyFromRegs - If there was virtual register allocated for the value V
1775/// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1778 SDValue Result;
1779
1780 if (It != FuncInfo.ValueMap.end()) {
1781 Register InReg = It->second;
1782
1783 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1784 DAG.getDataLayout(), InReg, Ty,
1785 std::nullopt); // This is not an ABI copy.
1786 SDValue Chain = DAG.getEntryNode();
1787 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1788 V);
1789 resolveDanglingDebugInfo(V, Result);
1790 }
1791
1792 return Result;
1793}
1794
1795/// getValue - Return an SDValue for the given Value.
1797 // If we already have an SDValue for this value, use it. It's important
1798 // to do this first, so that we don't create a CopyFromReg if we already
1799 // have a regular SDValue.
1800 SDValue &N = NodeMap[V];
1801 if (N.getNode()) return N;
1802
1803 // If there's a virtual register allocated and initialized for this
1804 // value, use it.
1805 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1806 return copyFromReg;
1807
1808 // Otherwise create a new SDValue and remember it.
1809 SDValue Val = getValueImpl(V);
1810 NodeMap[V] = Val;
1812 return Val;
1813}
1814
1815/// getNonRegisterValue - Return an SDValue for the given Value, but
1816/// don't look in FuncInfo.ValueMap for a virtual register.
1818 // If we already have an SDValue for this value, use it.
1819 SDValue &N = NodeMap[V];
1820 if (N.getNode()) {
1821 if (isIntOrFPConstant(N)) {
1822 // Remove the debug location from the node as the node is about to be used
1823 // in a location which may differ from the original debug location. This
1824 // is relevant to Constant and ConstantFP nodes because they can appear
1825 // as constant expressions inside PHI nodes.
1826 N->setDebugLoc(DebugLoc());
1827 }
1828 return N;
1829 }
1830
1831 // Otherwise create a new SDValue and remember it.
1832 SDValue Val = getValueImpl(V);
1833 NodeMap[V] = Val;
1835 return Val;
1836}
1837
1838/// getValueImpl - Helper function for getValue and getNonRegisterValue.
1839/// Create an SDValue for the given value.
1841 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1842
1843 if (const Constant *C = dyn_cast<Constant>(V)) {
1844 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1845
1846 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1847 SDLoc DL = getCurSDLoc();
1848
1849 // DAG.getConstant() may attempt to legalise the vector constant which can
1850 // significantly change the combines applied to the DAG. To reduce the
1851 // divergence when enabling ConstantInt based vectors we try to construct
1852 // the DAG in the same way as shufflevector based splats. TODO: The
1853 // divergence sometimes leads to better optimisations. Ideally we should
1854 // prevent DAG.getConstant() from legalising too early but there are some
1855 // degradations preventing this.
1856 if (VT.isScalableVector())
1857 return DAG.getNode(
1858 ISD::SPLAT_VECTOR, DL, VT,
1859 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1860 if (VT.isFixedLengthVector())
1861 return DAG.getSplatBuildVector(
1862 VT, DL,
1863 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1864 return DAG.getConstant(*CI, DL, VT);
1865 }
1866
1867 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1868 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1869
1870 if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1871 return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1872 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1873 getValue(CPA->getAddrDiscriminator()),
1874 getValue(CPA->getDiscriminator()));
1875 }
1876
1878 return DAG.getConstant(0, getCurSDLoc(), VT);
1879
1880 if (match(C, m_VScale()))
1881 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1882
1883 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1884 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1885
1886 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1887 return isa<PoisonValue>(C) ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
1888
1889 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1890 visit(CE->getOpcode(), *CE);
1891 SDValue N1 = NodeMap[V];
1892 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1893 return N1;
1894 }
1895
1897 SmallVector<SDValue, 4> Constants;
1898 for (const Use &U : C->operands()) {
1899 SDNode *Val = getValue(U).getNode();
1900 // If the operand is an empty aggregate, there are no values.
1901 if (!Val) continue;
1902 // Add each leaf value from the operand to the Constants list
1903 // to form a flattened list of all the values.
1904 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1905 Constants.push_back(SDValue(Val, i));
1906 }
1907
1908 return DAG.getMergeValues(Constants, getCurSDLoc());
1909 }
1910
1911 if (const ConstantDataSequential *CDS =
1914 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1915 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1916 // Add each leaf value from the operand to the Constants list
1917 // to form a flattened list of all the values.
1918 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1919 Ops.push_back(SDValue(Val, i));
1920 }
1921
1922 if (isa<ArrayType>(CDS->getType()))
1923 return DAG.getMergeValues(Ops, getCurSDLoc());
1924 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1925 }
1926
1927 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1929 "Unknown struct or array constant!");
1930
1931 SmallVector<EVT, 4> ValueVTs;
1932 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1933 unsigned NumElts = ValueVTs.size();
1934 if (NumElts == 0)
1935 return SDValue(); // empty struct
1936 SmallVector<SDValue, 4> Constants(NumElts);
1937 for (unsigned i = 0; i != NumElts; ++i) {
1938 EVT EltVT = ValueVTs[i];
1939 if (isa<UndefValue>(C))
1940 Constants[i] = DAG.getUNDEF(EltVT);
1941 else if (EltVT.isFloatingPoint())
1942 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1943 else
1944 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1945 }
1946
1947 return DAG.getMergeValues(Constants, getCurSDLoc());
1948 }
1949
1950 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1951 return DAG.getBlockAddress(BA, VT);
1952
1953 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1954 return getValue(Equiv->getGlobalValue());
1955
1956 if (const auto *NC = dyn_cast<NoCFIValue>(C))
1957 return getValue(NC->getGlobalValue());
1958
1959 if (VT == MVT::aarch64svcount) {
1960 assert(C->isNullValue() && "Can only zero this target type!");
1961 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1962 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1963 }
1964
1965 if (VT.isRISCVVectorTuple()) {
1966 assert(C->isNullValue() && "Can only zero this target type!");
1967 return DAG.getNode(
1968 ISD::BITCAST, getCurSDLoc(), VT,
1969 DAG.getNode(
1971 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1972 VT.getSizeInBits().getKnownMinValue() / 8, true),
1973 DAG.getConstant(0, getCurSDLoc(), MVT::getIntegerVT(8))));
1974 }
1975
1976 VectorType *VecTy = cast<VectorType>(V->getType());
1977
1978 // Now that we know the number and type of the elements, get that number of
1979 // elements into the Ops array based on what kind of constant it is.
1980 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1982 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1983 for (unsigned i = 0; i != NumElements; ++i)
1984 Ops.push_back(getValue(CV->getOperand(i)));
1985
1986 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1987 }
1988
1990 EVT EltVT =
1991 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1992
1993 SDValue Op;
1994 if (EltVT.isFloatingPoint())
1995 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1996 else
1997 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1998
1999 return DAG.getSplat(VT, getCurSDLoc(), Op);
2000 }
2001
2002 llvm_unreachable("Unknown vector constant");
2003 }
2004
2005 // If this is a static alloca, generate it as the frameindex instead of
2006 // computation.
2007 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
2009 FuncInfo.StaticAllocaMap.find(AI);
2010 if (SI != FuncInfo.StaticAllocaMap.end())
2011 return DAG.getFrameIndex(
2012 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
2013 }
2014
2015 // If this is an instruction which fast-isel has deferred, select it now.
2016 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2017 Register InReg = FuncInfo.InitializeRegForValue(Inst);
2018
2019 std::optional<CallingConv::ID> CallConv;
2020 auto *CB = dyn_cast<CallBase>(Inst);
2021 if (CB && !CB->isInlineAsm())
2022 CallConv = CB->getCallingConv();
2023
2024 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
2025 Inst->getType(), CallConv);
2026 SDValue Chain = DAG.getEntryNode();
2027 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
2028 }
2029
2030 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
2031 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
2032
2033 if (const auto *BB = dyn_cast<BasicBlock>(V))
2034 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
2035
2036 llvm_unreachable("Can't get register for value!");
2037}
2038
2039void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
2041 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
2042 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
2043 bool IsSEH = isAsynchronousEHPersonality(Pers);
2044 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2045 if (IsSEH) {
2046 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2047 CatchPadMBB->setIsEHContTarget(true);
2049 } else
2050 CatchPadMBB->setIsEHScopeEntry();
2051 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2052 if (IsMSVCCXX || IsCoreCLR)
2053 CatchPadMBB->setIsEHFuncletEntry();
2054}
2055
2056void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2057 // Update machine-CFG edge.
2058 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2059 FuncInfo.MBB->addSuccessor(TargetMBB);
2060
2061 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2062 bool IsSEH = isAsynchronousEHPersonality(Pers);
2063 if (IsSEH) {
2064 // If this is not a fall-through branch or optimizations are switched off,
2065 // emit the branch.
2066 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2067 TM.getOptLevel() == CodeGenOptLevel::None)
2068 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2069 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2070 return;
2071 }
2072
2073 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2074 TargetMBB->setIsEHContTarget(true);
2075 DAG.getMachineFunction().setHasEHContTarget(true);
2076
2077 // Figure out the funclet membership for the catchret's successor.
2078 // This will be used by the FuncletLayout pass to determine how to order the
2079 // BB's.
2080 // A 'catchret' returns to the outer scope's color.
2081 Value *ParentPad = I.getCatchSwitchParentPad();
2082 const BasicBlock *SuccessorColor;
2083 if (isa<ConstantTokenNone>(ParentPad))
2084 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2085 else
2086 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2087 assert(SuccessorColor && "No parent funclet for catchret!");
2088 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2089 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2090
2091 // Create the terminator node.
2092 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2093 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2094 DAG.getBasicBlock(SuccessorColorMBB));
2095 DAG.setRoot(Ret);
2096}
2097
2098void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2099 // Don't emit any special code for the cleanuppad instruction. It just marks
2100 // the start of an EH scope/funclet.
2101 FuncInfo.MBB->setIsEHScopeEntry();
2102 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2103 if (Pers != EHPersonality::Wasm_CXX) {
2104 FuncInfo.MBB->setIsEHFuncletEntry();
2105 FuncInfo.MBB->setIsCleanupFuncletEntry();
2106 }
2107}
2108
2109/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2110/// many places it could ultimately go. In the IR, we have a single unwind
2111/// destination, but in the machine CFG, we enumerate all the possible blocks.
2112/// This function skips over imaginary basic blocks that hold catchswitch
2113/// instructions, and finds all the "real" machine
2114/// basic block destinations. As those destinations may not be successors of
2115/// EHPadBB, here we also calculate the edge probability to those destinations.
2116/// The passed-in Prob is the edge probability to EHPadBB.
2118 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2119 BranchProbability Prob,
2120 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2121 &UnwindDests) {
2122 EHPersonality Personality =
2124 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2125 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2126 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2127 bool IsSEH = isAsynchronousEHPersonality(Personality);
2128
2129 while (EHPadBB) {
2131 BasicBlock *NewEHPadBB = nullptr;
2132 if (isa<LandingPadInst>(Pad)) {
2133 // Stop on landingpads. They are not funclets.
2134 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2135 break;
2136 } else if (isa<CleanupPadInst>(Pad)) {
2137 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2138 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2139 // which always catches an exception.
2140 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2141 UnwindDests.back().first->setIsEHScopeEntry();
2142 // In Wasm, EH scopes are not funclets
2143 if (!IsWasmCXX)
2144 UnwindDests.back().first->setIsEHFuncletEntry();
2145 break;
2146 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2147 // Add the catchpad handlers to the possible destinations.
2148 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2149 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2150 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2151 if (IsMSVCCXX || IsCoreCLR)
2152 UnwindDests.back().first->setIsEHFuncletEntry();
2153 if (!IsSEH)
2154 UnwindDests.back().first->setIsEHScopeEntry();
2155 }
2156 NewEHPadBB = CatchSwitch->getUnwindDest();
2157 } else {
2158 continue;
2159 }
2160
2161 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2162 if (BPI && NewEHPadBB)
2163 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2164 EHPadBB = NewEHPadBB;
2165 }
2166}
2167
2168void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2169 // Update successor info.
2171 auto UnwindDest = I.getUnwindDest();
2172 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2173 BranchProbability UnwindDestProb =
2174 (BPI && UnwindDest)
2175 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2177 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2178 for (auto &UnwindDest : UnwindDests) {
2179 UnwindDest.first->setIsEHPad();
2180 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2181 }
2182 FuncInfo.MBB->normalizeSuccProbs();
2183
2184 // Create the terminator node.
2185 MachineBasicBlock *CleanupPadMBB =
2186 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2187 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
2188 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2189 DAG.setRoot(Ret);
2190}
2191
2192void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2193 report_fatal_error("visitCatchSwitch not yet implemented!");
2194}
2195
2196void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2197 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2198 auto &DL = DAG.getDataLayout();
2199 SDValue Chain = getControlRoot();
2202
2203 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2204 // lower
2205 //
2206 // %val = call <ty> @llvm.experimental.deoptimize()
2207 // ret <ty> %val
2208 //
2209 // differently.
2210 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2212 return;
2213 }
2214
2215 if (!FuncInfo.CanLowerReturn) {
2216 Register DemoteReg = FuncInfo.DemoteRegister;
2217
2218 // Emit a store of the return value through the virtual register.
2219 // Leave Outs empty so that LowerReturn won't try to load return
2220 // registers the usual way.
2221 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2222 SDValue RetPtr =
2223 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2224 SDValue RetOp = getValue(I.getOperand(0));
2225
2226 SmallVector<EVT, 4> ValueVTs, MemVTs;
2227 SmallVector<uint64_t, 4> Offsets;
2228 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2229 &Offsets, 0);
2230 unsigned NumValues = ValueVTs.size();
2231
2232 SmallVector<SDValue, 4> Chains(NumValues);
2233 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2234 for (unsigned i = 0; i != NumValues; ++i) {
2235 // An aggregate return value cannot wrap around the address space, so
2236 // offsets to its parts don't wrap either.
2237 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2238 TypeSize::getFixed(Offsets[i]));
2239
2240 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2241 if (MemVTs[i] != ValueVTs[i])
2242 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2243 Chains[i] = DAG.getStore(
2244 Chain, getCurSDLoc(), Val,
2245 // FIXME: better loc info would be nice.
2246 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2247 commonAlignment(BaseAlign, Offsets[i]));
2248 }
2249
2250 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2251 MVT::Other, Chains);
2252 } else if (I.getNumOperands() != 0) {
2254 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2255 unsigned NumValues = Types.size();
2256 if (NumValues) {
2257 SDValue RetOp = getValue(I.getOperand(0));
2258
2259 const Function *F = I.getParent()->getParent();
2260
2261 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2262 I.getOperand(0)->getType(), F->getCallingConv(),
2263 /*IsVarArg*/ false, DL);
2264
2265 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2266 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2267 ExtendKind = ISD::SIGN_EXTEND;
2268 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2269 ExtendKind = ISD::ZERO_EXTEND;
2270
2271 LLVMContext &Context = F->getContext();
2272 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2273
2274 for (unsigned j = 0; j != NumValues; ++j) {
2275 EVT VT = TLI.getValueType(DL, Types[j]);
2276
2277 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2278 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2279
2280 CallingConv::ID CC = F->getCallingConv();
2281
2282 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2283 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2284 SmallVector<SDValue, 4> Parts(NumParts);
2286 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2287 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2288
2289 // 'inreg' on function refers to return value
2290 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2291 if (RetInReg)
2292 Flags.setInReg();
2293
2294 if (I.getOperand(0)->getType()->isPointerTy()) {
2295 Flags.setPointer();
2296 Flags.setPointerAddrSpace(
2297 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2298 }
2299
2300 if (NeedsRegBlock) {
2301 Flags.setInConsecutiveRegs();
2302 if (j == NumValues - 1)
2303 Flags.setInConsecutiveRegsLast();
2304 }
2305
2306 // Propagate extension type if any
2307 if (ExtendKind == ISD::SIGN_EXTEND)
2308 Flags.setSExt();
2309 else if (ExtendKind == ISD::ZERO_EXTEND)
2310 Flags.setZExt();
2311 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2312 Flags.setNoExt();
2313
2314 for (unsigned i = 0; i < NumParts; ++i) {
2315 Outs.push_back(ISD::OutputArg(Flags,
2316 Parts[i].getValueType().getSimpleVT(),
2317 VT, Types[j], 0, 0));
2318 OutVals.push_back(Parts[i]);
2319 }
2320 }
2321 }
2322 }
2323
2324 // Push in swifterror virtual register as the last element of Outs. This makes
2325 // sure swifterror virtual register will be returned in the swifterror
2326 // physical register.
2327 const Function *F = I.getParent()->getParent();
2328 if (TLI.supportSwiftError() &&
2329 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2330 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2331 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2332 Flags.setSwiftError();
2333 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2334 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2335 PointerType::getUnqual(*DAG.getContext()),
2336 /*origidx=*/1, /*partOffs=*/0));
2337 // Create SDNode for the swifterror virtual register.
2338 OutVals.push_back(
2339 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2340 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2341 EVT(TLI.getPointerTy(DL))));
2342 }
2343
2344 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2345 CallingConv::ID CallConv =
2346 DAG.getMachineFunction().getFunction().getCallingConv();
2347 Chain = DAG.getTargetLoweringInfo().LowerReturn(
2348 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2349
2350 // Verify that the target's LowerReturn behaved as expected.
2351 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2352 "LowerReturn didn't return a valid chain!");
2353
2354 // Update the DAG with the new chain value resulting from return lowering.
2355 DAG.setRoot(Chain);
2356}
2357
2358/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2359/// created for it, emit nodes to copy the value into the virtual
2360/// registers.
2362 // Skip empty types
2363 if (V->getType()->isEmptyTy())
2364 return;
2365
2367 if (VMI != FuncInfo.ValueMap.end()) {
2368 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2369 "Unused value assigned virtual registers!");
2370 CopyValueToVirtualRegister(V, VMI->second);
2371 }
2372}
2373
2374/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2375/// the current basic block, add it to ValueMap now so that we'll get a
2376/// CopyTo/FromReg.
2378 // No need to export constants.
2379 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2380
2381 // Already exported?
2382 if (FuncInfo.isExportedInst(V)) return;
2383
2384 Register Reg = FuncInfo.InitializeRegForValue(V);
2386}
2387
2389 const BasicBlock *FromBB) {
2390 // The operands of the setcc have to be in this block. We don't know
2391 // how to export them from some other block.
2392 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2393 // Can export from current BB.
2394 if (VI->getParent() == FromBB)
2395 return true;
2396
2397 // Is already exported, noop.
2398 return FuncInfo.isExportedInst(V);
2399 }
2400
2401 // If this is an argument, we can export it if the BB is the entry block or
2402 // if it is already exported.
2403 if (isa<Argument>(V)) {
2404 if (FromBB->isEntryBlock())
2405 return true;
2406
2407 // Otherwise, can only export this if it is already exported.
2408 return FuncInfo.isExportedInst(V);
2409 }
2410
2411 // Otherwise, constants can always be exported.
2412 return true;
2413}
2414
2415/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2417SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2418 const MachineBasicBlock *Dst) const {
2420 const BasicBlock *SrcBB = Src->getBasicBlock();
2421 const BasicBlock *DstBB = Dst->getBasicBlock();
2422 if (!BPI) {
2423 // If BPI is not available, set the default probability as 1 / N, where N is
2424 // the number of successors.
2425 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2426 return BranchProbability(1, SuccSize);
2427 }
2428 return BPI->getEdgeProbability(SrcBB, DstBB);
2429}
2430
2431void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2432 MachineBasicBlock *Dst,
2433 BranchProbability Prob) {
2434 if (!FuncInfo.BPI)
2435 Src->addSuccessorWithoutProb(Dst);
2436 else {
2437 if (Prob.isUnknown())
2438 Prob = getEdgeProbability(Src, Dst);
2439 Src->addSuccessor(Dst, Prob);
2440 }
2441}
2442
2443static bool InBlock(const Value *V, const BasicBlock *BB) {
2444 if (const Instruction *I = dyn_cast<Instruction>(V))
2445 return I->getParent() == BB;
2446 return true;
2447}
2448
2449/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2450/// This function emits a branch and is used at the leaves of an OR or an
2451/// AND operator tree.
2452void
2455 MachineBasicBlock *FBB,
2456 MachineBasicBlock *CurBB,
2457 MachineBasicBlock *SwitchBB,
2458 BranchProbability TProb,
2459 BranchProbability FProb,
2460 bool InvertCond) {
2461 const BasicBlock *BB = CurBB->getBasicBlock();
2462
2463 // If the leaf of the tree is a comparison, merge the condition into
2464 // the caseblock.
2465 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2466 // The operands of the cmp have to be in this block. We don't know
2467 // how to export them from some other block. If this is the first block
2468 // of the sequence, no exporting is needed.
2469 if (CurBB == SwitchBB ||
2470 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2471 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2472 ISD::CondCode Condition;
2473 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2474 ICmpInst::Predicate Pred =
2475 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2476 Condition = getICmpCondCode(Pred);
2477 } else {
2478 const FCmpInst *FC = cast<FCmpInst>(Cond);
2479 FCmpInst::Predicate Pred =
2480 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2481 Condition = getFCmpCondCode(Pred);
2482 if (TM.Options.NoNaNsFPMath)
2483 Condition = getFCmpCodeWithoutNaN(Condition);
2484 }
2485
2486 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2487 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2488 SL->SwitchCases.push_back(CB);
2489 return;
2490 }
2491 }
2492
2493 // Create a CaseBlock record representing this branch.
2494 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2495 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2496 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2497 SL->SwitchCases.push_back(CB);
2498}
2499
2500// Collect dependencies on V recursively. This is used for the cost analysis in
2501// `shouldKeepJumpConditionsTogether`.
2505 unsigned Depth = 0) {
2506 // Return false if we have an incomplete count.
2508 return false;
2509
2510 auto *I = dyn_cast<Instruction>(V);
2511 if (I == nullptr)
2512 return true;
2513
2514 if (Necessary != nullptr) {
2515 // This instruction is necessary for the other side of the condition so
2516 // don't count it.
2517 if (Necessary->contains(I))
2518 return true;
2519 }
2520
2521 // Already added this dep.
2522 if (!Deps->try_emplace(I, false).second)
2523 return true;
2524
2525 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2526 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2527 Depth + 1))
2528 return false;
2529 return true;
2530}
2531
2534 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2536 if (I.getNumSuccessors() != 2)
2537 return false;
2538
2539 if (!I.isConditional())
2540 return false;
2541
2542 if (Params.BaseCost < 0)
2543 return false;
2544
2545 // Baseline cost.
2546 InstructionCost CostThresh = Params.BaseCost;
2547
2548 BranchProbabilityInfo *BPI = nullptr;
2549 if (Params.LikelyBias || Params.UnlikelyBias)
2550 BPI = FuncInfo.BPI;
2551 if (BPI != nullptr) {
2552 // See if we are either likely to get an early out or compute both lhs/rhs
2553 // of the condition.
2554 BasicBlock *IfFalse = I.getSuccessor(0);
2555 BasicBlock *IfTrue = I.getSuccessor(1);
2556
2557 std::optional<bool> Likely;
2558 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2559 Likely = true;
2560 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2561 Likely = false;
2562
2563 if (Likely) {
2564 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2565 // Its likely we will have to compute both lhs and rhs of condition
2566 CostThresh += Params.LikelyBias;
2567 else {
2568 if (Params.UnlikelyBias < 0)
2569 return false;
2570 // Its likely we will get an early out.
2571 CostThresh -= Params.UnlikelyBias;
2572 }
2573 }
2574 }
2575
2576 if (CostThresh <= 0)
2577 return false;
2578
2579 // Collect "all" instructions that lhs condition is dependent on.
2580 // Use map for stable iteration (to avoid non-determanism of iteration of
2581 // SmallPtrSet). The `bool` value is just a dummy.
2583 collectInstructionDeps(&LhsDeps, Lhs);
2584 // Collect "all" instructions that rhs condition is dependent on AND are
2585 // dependencies of lhs. This gives us an estimate on which instructions we
2586 // stand to save by splitting the condition.
2587 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2588 return false;
2589 // Add the compare instruction itself unless its a dependency on the LHS.
2590 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2591 if (!LhsDeps.contains(RhsI))
2592 RhsDeps.try_emplace(RhsI, false);
2593
2594 InstructionCost CostOfIncluding = 0;
2595 // See if this instruction will need to computed independently of whether RHS
2596 // is.
2597 Value *BrCond = I.getCondition();
2598 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2599 for (const auto *U : Ins->users()) {
2600 // If user is independent of RHS calculation we don't need to count it.
2601 if (auto *UIns = dyn_cast<Instruction>(U))
2602 if (UIns != BrCond && !RhsDeps.contains(UIns))
2603 return false;
2604 }
2605 return true;
2606 };
2607
2608 // Prune instructions from RHS Deps that are dependencies of unrelated
2609 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2610 // arbitrary and just meant to cap the how much time we spend in the pruning
2611 // loop. Its highly unlikely to come into affect.
2612 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2613 // Stop after a certain point. No incorrectness from including too many
2614 // instructions.
2615 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2616 const Instruction *ToDrop = nullptr;
2617 for (const auto &InsPair : RhsDeps) {
2618 if (!ShouldCountInsn(InsPair.first)) {
2619 ToDrop = InsPair.first;
2620 break;
2621 }
2622 }
2623 if (ToDrop == nullptr)
2624 break;
2625 RhsDeps.erase(ToDrop);
2626 }
2627
2628 for (const auto &InsPair : RhsDeps) {
2629 // Finally accumulate latency that we can only attribute to computing the
2630 // RHS condition. Use latency because we are essentially trying to calculate
2631 // the cost of the dependency chain.
2632 // Possible TODO: We could try to estimate ILP and make this more precise.
2633 CostOfIncluding += TTI->getInstructionCost(
2634 InsPair.first, TargetTransformInfo::TCK_Latency);
2635
2636 if (CostOfIncluding > CostThresh)
2637 return false;
2638 }
2639 return true;
2640}
2641
2644 MachineBasicBlock *FBB,
2645 MachineBasicBlock *CurBB,
2646 MachineBasicBlock *SwitchBB,
2648 BranchProbability TProb,
2649 BranchProbability FProb,
2650 bool InvertCond) {
2651 // Skip over not part of the tree and remember to invert op and operands at
2652 // next level.
2653 Value *NotCond;
2654 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2655 InBlock(NotCond, CurBB->getBasicBlock())) {
2656 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2657 !InvertCond);
2658 return;
2659 }
2660
2662 const Value *BOpOp0, *BOpOp1;
2663 // Compute the effective opcode for Cond, taking into account whether it needs
2664 // to be inverted, e.g.
2665 // and (not (or A, B)), C
2666 // gets lowered as
2667 // and (and (not A, not B), C)
2669 if (BOp) {
2670 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2671 ? Instruction::And
2672 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2673 ? Instruction::Or
2675 if (InvertCond) {
2676 if (BOpc == Instruction::And)
2677 BOpc = Instruction::Or;
2678 else if (BOpc == Instruction::Or)
2679 BOpc = Instruction::And;
2680 }
2681 }
2682
2683 // If this node is not part of the or/and tree, emit it as a branch.
2684 // Note that all nodes in the tree should have same opcode.
2685 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2686 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2687 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2688 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2689 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2690 TProb, FProb, InvertCond);
2691 return;
2692 }
2693
2694 // Create TmpBB after CurBB.
2695 MachineFunction::iterator BBI(CurBB);
2696 MachineFunction &MF = DAG.getMachineFunction();
2698 CurBB->getParent()->insert(++BBI, TmpBB);
2699
2700 if (Opc == Instruction::Or) {
2701 // Codegen X | Y as:
2702 // BB1:
2703 // jmp_if_X TBB
2704 // jmp TmpBB
2705 // TmpBB:
2706 // jmp_if_Y TBB
2707 // jmp FBB
2708 //
2709
2710 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2711 // The requirement is that
2712 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2713 // = TrueProb for original BB.
2714 // Assuming the original probabilities are A and B, one choice is to set
2715 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2716 // A/(1+B) and 2B/(1+B). This choice assumes that
2717 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2718 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2719 // TmpBB, but the math is more complicated.
2720
2721 auto NewTrueProb = TProb / 2;
2722 auto NewFalseProb = TProb / 2 + FProb;
2723 // Emit the LHS condition.
2724 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2725 NewFalseProb, InvertCond);
2726
2727 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2728 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2730 // Emit the RHS condition into TmpBB.
2731 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2732 Probs[1], InvertCond);
2733 } else {
2734 assert(Opc == Instruction::And && "Unknown merge op!");
2735 // Codegen X & Y as:
2736 // BB1:
2737 // jmp_if_X TmpBB
2738 // jmp FBB
2739 // TmpBB:
2740 // jmp_if_Y TBB
2741 // jmp FBB
2742 //
2743 // This requires creation of TmpBB after CurBB.
2744
2745 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2746 // The requirement is that
2747 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2748 // = FalseProb for original BB.
2749 // Assuming the original probabilities are A and B, one choice is to set
2750 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2751 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2752 // TrueProb for BB1 * FalseProb for TmpBB.
2753
2754 auto NewTrueProb = TProb + FProb / 2;
2755 auto NewFalseProb = FProb / 2;
2756 // Emit the LHS condition.
2757 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2758 NewFalseProb, InvertCond);
2759
2760 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2761 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2763 // Emit the RHS condition into TmpBB.
2764 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2765 Probs[1], InvertCond);
2766 }
2767}
2768
2769/// If the set of cases should be emitted as a series of branches, return true.
2770/// If we should emit this as a bunch of and/or'd together conditions, return
2771/// false.
2772bool
2773SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2774 if (Cases.size() != 2) return true;
2775
2776 // If this is two comparisons of the same values or'd or and'd together, they
2777 // will get folded into a single comparison, so don't emit two blocks.
2778 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2779 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2780 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2781 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2782 return false;
2783 }
2784
2785 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2786 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2787 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2788 Cases[0].CC == Cases[1].CC &&
2789 isa<Constant>(Cases[0].CmpRHS) &&
2790 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2791 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2792 return false;
2793 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2794 return false;
2795 }
2796
2797 return true;
2798}
2799
2800void SelectionDAGBuilder::visitBr(const BranchInst &I) {
2802
2803 // Update machine-CFG edges.
2804 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2805
2806 if (I.isUnconditional()) {
2807 // Update machine-CFG edges.
2808 BrMBB->addSuccessor(Succ0MBB);
2809
2810 // If this is not a fall-through branch or optimizations are switched off,
2811 // emit the branch.
2812 if (Succ0MBB != NextBlock(BrMBB) ||
2814 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2815 getControlRoot(), DAG.getBasicBlock(Succ0MBB));
2816 setValue(&I, Br);
2817 DAG.setRoot(Br);
2818 }
2819
2820 return;
2821 }
2822
2823 // If this condition is one of the special cases we handle, do special stuff
2824 // now.
2825 const Value *CondVal = I.getCondition();
2826 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2827
2828 // If this is a series of conditions that are or'd or and'd together, emit
2829 // this as a sequence of branches instead of setcc's with and/or operations.
2830 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2831 // unpredictable branches, and vector extracts because those jumps are likely
2832 // expensive for any target), this should improve performance.
2833 // For example, instead of something like:
2834 // cmp A, B
2835 // C = seteq
2836 // cmp D, E
2837 // F = setle
2838 // or C, F
2839 // jnz foo
2840 // Emit:
2841 // cmp A, B
2842 // je foo
2843 // cmp D, E
2844 // jle foo
2845 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2846 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2847 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2848 BOp->hasOneUse() && !IsUnpredictable) {
2849 Value *Vec;
2850 const Value *BOp0, *BOp1;
2852 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2853 Opcode = Instruction::And;
2854 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2855 Opcode = Instruction::Or;
2856
2857 if (Opcode &&
2858 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2859 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2861 FuncInfo, I, Opcode, BOp0, BOp1,
2862 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2863 Opcode, BOp0, BOp1))) {
2864 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2865 getEdgeProbability(BrMBB, Succ0MBB),
2866 getEdgeProbability(BrMBB, Succ1MBB),
2867 /*InvertCond=*/false);
2868 // If the compares in later blocks need to use values not currently
2869 // exported from this block, export them now. This block should always
2870 // be the first entry.
2871 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2872
2873 // Allow some cases to be rejected.
2874 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2875 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2876 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2877 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2878 }
2879
2880 // Emit the branch for this block.
2881 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2882 SL->SwitchCases.erase(SL->SwitchCases.begin());
2883 return;
2884 }
2885
2886 // Okay, we decided not to do this, remove any inserted MBB's and clear
2887 // SwitchCases.
2888 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2889 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2890
2891 SL->SwitchCases.clear();
2892 }
2893 }
2894
2895 // Create a CaseBlock record representing this branch.
2896 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2897 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2899 IsUnpredictable);
2900
2901 // Use visitSwitchCase to actually insert the fast branch sequence for this
2902 // cond branch.
2903 visitSwitchCase(CB, BrMBB);
2904}
2905
2906/// visitSwitchCase - Emits the necessary code to represent a single node in
2907/// the binary search tree resulting from lowering a switch instruction.
2909 MachineBasicBlock *SwitchBB) {
2910 SDValue Cond;
2911 SDValue CondLHS = getValue(CB.CmpLHS);
2912 SDLoc dl = CB.DL;
2913
2914 if (CB.CC == ISD::SETTRUE) {
2915 // Branch or fall through to TrueBB.
2916 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2917 SwitchBB->normalizeSuccProbs();
2918 if (CB.TrueBB != NextBlock(SwitchBB)) {
2919 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2920 DAG.getBasicBlock(CB.TrueBB)));
2921 }
2922 return;
2923 }
2924
2925 auto &TLI = DAG.getTargetLoweringInfo();
2926 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2927
2928 // Build the setcc now.
2929 if (!CB.CmpMHS) {
2930 // Fold "(X == true)" to X and "(X == false)" to !X to
2931 // handle common cases produced by branch lowering.
2932 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2933 CB.CC == ISD::SETEQ)
2934 Cond = CondLHS;
2935 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2936 CB.CC == ISD::SETEQ) {
2937 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2938 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2939 } else {
2940 SDValue CondRHS = getValue(CB.CmpRHS);
2941
2942 // If a pointer's DAG type is larger than its memory type then the DAG
2943 // values are zero-extended. This breaks signed comparisons so truncate
2944 // back to the underlying type before doing the compare.
2945 if (CondLHS.getValueType() != MemVT) {
2946 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2947 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2948 }
2949 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2950 }
2951 } else {
2952 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2953
2954 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2955 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2956
2957 SDValue CmpOp = getValue(CB.CmpMHS);
2958 EVT VT = CmpOp.getValueType();
2959
2960 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2961 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2962 ISD::SETLE);
2963 } else {
2964 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2965 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2966 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2967 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2968 }
2969 }
2970
2971 // Update successor info
2972 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2973 // TrueBB and FalseBB are always different unless the incoming IR is
2974 // degenerate. This only happens when running llc on weird IR.
2975 if (CB.TrueBB != CB.FalseBB)
2976 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2977 SwitchBB->normalizeSuccProbs();
2978
2979 // If the lhs block is the next block, invert the condition so that we can
2980 // fall through to the lhs instead of the rhs block.
2981 if (CB.TrueBB == NextBlock(SwitchBB)) {
2982 std::swap(CB.TrueBB, CB.FalseBB);
2983 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2984 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2985 }
2986
2987 SDNodeFlags Flags;
2989 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2990 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2991
2992 setValue(CurInst, BrCond);
2993
2994 // Insert the false branch. Do this even if it's a fall through branch,
2995 // this makes it easier to do DAG optimizations which require inverting
2996 // the branch condition.
2997 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2998 DAG.getBasicBlock(CB.FalseBB));
2999
3000 DAG.setRoot(BrCond);
3001}
3002
3003/// visitJumpTable - Emit JumpTable node in the current MBB
3005 // Emit the code for the jump table
3006 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3007 assert(JT.Reg && "Should lower JT Header first!");
3008 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
3009 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
3010 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
3011 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
3012 Index.getValue(1), Table, Index);
3013 DAG.setRoot(BrJumpTable);
3014}
3015
3016/// visitJumpTableHeader - This function emits necessary code to produce index
3017/// in the JumpTable from switch case.
3019 JumpTableHeader &JTH,
3020 MachineBasicBlock *SwitchBB) {
3021 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3022 const SDLoc &dl = *JT.SL;
3023
3024 // Subtract the lowest switch case value from the value being switched on.
3025 SDValue SwitchOp = getValue(JTH.SValue);
3026 EVT VT = SwitchOp.getValueType();
3027 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
3028 DAG.getConstant(JTH.First, dl, VT));
3029
3030 // The SDNode we just created, which holds the value being switched on minus
3031 // the smallest case value, needs to be copied to a virtual register so it
3032 // can be used as an index into the jump table in a subsequent basic block.
3033 // This value may be smaller or larger than the target's pointer type, and
3034 // therefore require extension or truncating.
3035 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3036 SwitchOp =
3037 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
3038
3039 Register JumpTableReg =
3040 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
3041 SDValue CopyTo =
3042 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3043 JT.Reg = JumpTableReg;
3044
3045 if (!JTH.FallthroughUnreachable) {
3046 // Emit the range check for the jump table, and branch to the default block
3047 // for the switch statement if the value being switched on exceeds the
3048 // largest case in the switch.
3049 SDValue CMP = DAG.getSetCC(
3050 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3051 Sub.getValueType()),
3052 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3053
3054 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3055 MVT::Other, CopyTo, CMP,
3056 DAG.getBasicBlock(JT.Default));
3057
3058 // Avoid emitting unnecessary branches to the next block.
3059 if (JT.MBB != NextBlock(SwitchBB))
3060 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3061 DAG.getBasicBlock(JT.MBB));
3062
3063 DAG.setRoot(BrCond);
3064 } else {
3065 // Avoid emitting unnecessary branches to the next block.
3066 if (JT.MBB != NextBlock(SwitchBB))
3067 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3068 DAG.getBasicBlock(JT.MBB)));
3069 else
3070 DAG.setRoot(CopyTo);
3071 }
3072}
3073
3074/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3075/// variable if there exists one.
3077 SDValue &Chain) {
3078 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3079 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3080 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3084 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3085 if (Global) {
3086 MachinePointerInfo MPInfo(Global);
3090 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3091 DAG.setNodeMemRefs(Node, {MemRef});
3092 }
3093 if (PtrTy != PtrMemTy)
3094 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3095 return SDValue(Node, 0);
3096}
3097
3098/// Codegen a new tail for a stack protector check ParentMBB which has had its
3099/// tail spliced into a stack protector check success bb.
3100///
3101/// For a high level explanation of how this fits into the stack protector
3102/// generation see the comment on the declaration of class
3103/// StackProtectorDescriptor.
3105 MachineBasicBlock *ParentBB) {
3106
3107 // First create the loads to the guard/stack slot for the comparison.
3108 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3109 auto &DL = DAG.getDataLayout();
3110 EVT PtrTy = TLI.getFrameIndexTy(DL);
3111 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3112
3113 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3114 int FI = MFI.getStackProtectorIndex();
3115
3116 SDValue Guard;
3117 SDLoc dl = getCurSDLoc();
3118 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3119 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3120 Align Align = DL.getPrefTypeAlign(
3121 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3122
3123 // Generate code to load the content of the guard slot.
3124 SDValue GuardVal = DAG.getLoad(
3125 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3126 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3128
3129 if (TLI.useStackGuardXorFP())
3130 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3131
3132 // If we're using function-based instrumentation, call the guard check
3133 // function
3135 // Get the guard check function from the target and verify it exists since
3136 // we're using function-based instrumentation
3137 const Function *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3138 assert(GuardCheckFn && "Guard check function is null");
3139
3140 // The target provides a guard check function to validate the guard value.
3141 // Generate a call to that function with the content of the guard slot as
3142 // argument.
3143 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3144 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3145
3147 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3148 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3149 Entry.IsInReg = true;
3150 Args.push_back(Entry);
3151
3154 .setChain(DAG.getEntryNode())
3155 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3156 getValue(GuardCheckFn), std::move(Args));
3157
3158 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3159 DAG.setRoot(Result.second);
3160 return;
3161 }
3162
3163 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3164 // Otherwise, emit a volatile load to retrieve the stack guard value.
3165 SDValue Chain = DAG.getEntryNode();
3166 if (TLI.useLoadStackGuardNode(M)) {
3167 Guard = getLoadStackGuard(DAG, dl, Chain);
3168 } else {
3169 if (const Value *IRGuard = TLI.getSDagStackGuard(M)) {
3170 SDValue GuardPtr = getValue(IRGuard);
3171 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3172 MachinePointerInfo(IRGuard, 0), Align,
3174 } else {
3175 LLVMContext &Ctx = *DAG.getContext();
3176 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
3177 Guard = DAG.getPOISON(PtrMemTy);
3178 }
3179 }
3180
3181 // Perform the comparison via a getsetcc.
3182 SDValue Cmp = DAG.getSetCC(
3183 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3184 Guard, GuardVal, ISD::SETNE);
3185
3186 // If the guard/stackslot do not equal, branch to failure MBB.
3187 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3188 MVT::Other, GuardVal.getOperand(0),
3189 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3190 // Otherwise branch to success MBB.
3191 SDValue Br = DAG.getNode(ISD::BR, dl,
3192 MVT::Other, BrCond,
3193 DAG.getBasicBlock(SPD.getSuccessMBB()));
3194
3195 DAG.setRoot(Br);
3196}
3197
3198/// Codegen the failure basic block for a stack protector check.
3199///
3200/// A failure stack protector machine basic block consists simply of a call to
3201/// __stack_chk_fail().
3202///
3203/// For a high level explanation of how this fits into the stack protector
3204/// generation see the comment on the declaration of class
3205/// StackProtectorDescriptor.
3208
3209 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3210 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3211 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3212 SDValue Chain;
3213
3214 // For -Oz builds with a guard check function, we use function-based
3215 // instrumentation. Otherwise, if we have a guard check function, we call it
3216 // in the failure block.
3217 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M);
3218 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3219 // First create the loads to the guard/stack slot for the comparison.
3220 auto &DL = DAG.getDataLayout();
3221 EVT PtrTy = TLI.getFrameIndexTy(DL);
3222 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3223
3224 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3225 int FI = MFI.getStackProtectorIndex();
3226
3227 SDLoc dl = getCurSDLoc();
3228 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3229 Align Align = DL.getPrefTypeAlign(
3230 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3231
3232 // Generate code to load the content of the guard slot.
3233 SDValue GuardVal = DAG.getLoad(
3234 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3235 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3237
3238 if (TLI.useStackGuardXorFP())
3239 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3240
3241 // The target provides a guard check function to validate the guard value.
3242 // Generate a call to that function with the content of the guard slot as
3243 // argument.
3244 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3245 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3246
3248 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3249 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3250 Entry.IsInReg = true;
3251 Args.push_back(Entry);
3252
3255 .setChain(DAG.getEntryNode())
3256 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3257 getValue(GuardCheckFn), std::move(Args));
3258
3259 Chain = TLI.LowerCallTo(CLI).second;
3260 } else {
3262 CallOptions.setDiscardResult(true);
3263 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3264 {}, CallOptions, getCurSDLoc())
3265 .second;
3266 }
3267
3268 // Emit a trap instruction if we are required to do so.
3269 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3270 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3271 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3272
3273 DAG.setRoot(Chain);
3274}
3275
3276/// visitBitTestHeader - This function emits necessary code to produce value
3277/// suitable for "bit tests"
3279 MachineBasicBlock *SwitchBB) {
3280 SDLoc dl = getCurSDLoc();
3281
3282 // Subtract the minimum value.
3283 SDValue SwitchOp = getValue(B.SValue);
3284 EVT VT = SwitchOp.getValueType();
3285 SDValue RangeSub =
3286 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3287
3288 // Determine the type of the test operands.
3289 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3290 bool UsePtrType = false;
3291 if (!TLI.isTypeLegal(VT)) {
3292 UsePtrType = true;
3293 } else {
3294 for (const BitTestCase &Case : B.Cases)
3295 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3296 // Switch table case range are encoded into series of masks.
3297 // Just use pointer type, it's guaranteed to fit.
3298 UsePtrType = true;
3299 break;
3300 }
3301 }
3302 SDValue Sub = RangeSub;
3303 if (UsePtrType) {
3304 VT = TLI.getPointerTy(DAG.getDataLayout());
3305 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3306 }
3307
3308 B.RegVT = VT.getSimpleVT();
3309 B.Reg = FuncInfo.CreateReg(B.RegVT);
3310 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3311
3312 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3313
3314 if (!B.FallthroughUnreachable)
3315 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3316 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3317 SwitchBB->normalizeSuccProbs();
3318
3319 SDValue Root = CopyTo;
3320 if (!B.FallthroughUnreachable) {
3321 // Conditional branch to the default block.
3322 SDValue RangeCmp = DAG.getSetCC(dl,
3323 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3324 RangeSub.getValueType()),
3325 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3326 ISD::SETUGT);
3327
3328 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3329 DAG.getBasicBlock(B.Default));
3330 }
3331
3332 // Avoid emitting unnecessary branches to the next block.
3333 if (MBB != NextBlock(SwitchBB))
3334 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3335
3336 DAG.setRoot(Root);
3337}
3338
3339/// visitBitTestCase - this function produces one "bit test"
3341 MachineBasicBlock *NextMBB,
3342 BranchProbability BranchProbToNext,
3343 Register Reg, BitTestCase &B,
3344 MachineBasicBlock *SwitchBB) {
3345 SDLoc dl = getCurSDLoc();
3346 MVT VT = BB.RegVT;
3347 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3348 SDValue Cmp;
3349 unsigned PopCount = llvm::popcount(B.Mask);
3350 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3351 if (PopCount == 1) {
3352 // Testing for a single bit; just compare the shift count with what it
3353 // would need to be to shift a 1 bit in that position.
3354 Cmp = DAG.getSetCC(
3355 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3356 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3357 ISD::SETEQ);
3358 } else if (PopCount == BB.Range) {
3359 // There is only one zero bit in the range, test for it directly.
3360 Cmp = DAG.getSetCC(
3361 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3362 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3363 } else {
3364 // Make desired shift
3365 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3366 DAG.getConstant(1, dl, VT), ShiftOp);
3367
3368 // Emit bit tests and jumps
3369 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3370 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3371 Cmp = DAG.getSetCC(
3372 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3373 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3374 }
3375
3376 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3377 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3378 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3379 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3380 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3381 // one as they are relative probabilities (and thus work more like weights),
3382 // and hence we need to normalize them to let the sum of them become one.
3383 SwitchBB->normalizeSuccProbs();
3384
3385 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3386 MVT::Other, getControlRoot(),
3387 Cmp, DAG.getBasicBlock(B.TargetBB));
3388
3389 // Avoid emitting unnecessary branches to the next block.
3390 if (NextMBB != NextBlock(SwitchBB))
3391 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3392 DAG.getBasicBlock(NextMBB));
3393
3394 DAG.setRoot(BrAnd);
3395}
3396
3397void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3398 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3399
3400 // Retrieve successors. Look through artificial IR level blocks like
3401 // catchswitch for successors.
3402 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3403 const BasicBlock *EHPadBB = I.getSuccessor(1);
3404 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3405
3406 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3407 // have to do anything here to lower funclet bundles.
3408 failForInvalidBundles(I, "invokes",
3414
3415 const Value *Callee(I.getCalledOperand());
3416 const Function *Fn = dyn_cast<Function>(Callee);
3417 if (isa<InlineAsm>(Callee))
3418 visitInlineAsm(I, EHPadBB);
3419 else if (Fn && Fn->isIntrinsic()) {
3420 switch (Fn->getIntrinsicID()) {
3421 default:
3422 llvm_unreachable("Cannot invoke this intrinsic");
3423 case Intrinsic::donothing:
3424 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3425 case Intrinsic::seh_try_begin:
3426 case Intrinsic::seh_scope_begin:
3427 case Intrinsic::seh_try_end:
3428 case Intrinsic::seh_scope_end:
3429 if (EHPadMBB)
3430 // a block referenced by EH table
3431 // so dtor-funclet not removed by opts
3432 EHPadMBB->setMachineBlockAddressTaken();
3433 break;
3434 case Intrinsic::experimental_patchpoint_void:
3435 case Intrinsic::experimental_patchpoint:
3436 visitPatchpoint(I, EHPadBB);
3437 break;
3438 case Intrinsic::experimental_gc_statepoint:
3440 break;
3441 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3442 // but these intrinsics are special because they can be invoked, so we
3443 // manually lower it to a DAG node here.
3444 case Intrinsic::wasm_throw: {
3446 std::array<SDValue, 4> Ops = {
3447 getControlRoot(), // inchain for the terminator node
3448 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3450 getValue(I.getArgOperand(0)), // tag
3451 getValue(I.getArgOperand(1)) // thrown value
3452 };
3453 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3454 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3455 break;
3456 }
3457 case Intrinsic::wasm_rethrow: {
3458 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3459 std::array<SDValue, 2> Ops = {
3460 getControlRoot(), // inchain for the terminator node
3461 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3462 TLI.getPointerTy(DAG.getDataLayout()))};
3463 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3464 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3465 break;
3466 }
3467 }
3468 } else if (I.hasDeoptState()) {
3469 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3470 // Eventually we will support lowering the @llvm.experimental.deoptimize
3471 // intrinsic, and right now there are no plans to support other intrinsics
3472 // with deopt state.
3473 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3474 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3476 } else {
3477 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3478 }
3479
3480 // If the value of the invoke is used outside of its defining block, make it
3481 // available as a virtual register.
3482 // We already took care of the exported value for the statepoint instruction
3483 // during call to the LowerStatepoint.
3484 if (!isa<GCStatepointInst>(I)) {
3486 }
3487
3489 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3490 BranchProbability EHPadBBProb =
3491 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3493 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3494
3495 // Update successor info.
3496 addSuccessorWithProb(InvokeMBB, Return);
3497 for (auto &UnwindDest : UnwindDests) {
3498 UnwindDest.first->setIsEHPad();
3499 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3500 }
3501 InvokeMBB->normalizeSuccProbs();
3502
3503 // Drop into normal successor.
3504 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3505 DAG.getBasicBlock(Return)));
3506}
3507
3508void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3509 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3510
3511 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3512 // have to do anything here to lower funclet bundles.
3513 failForInvalidBundles(I, "callbrs",
3515
3516 assert(I.isInlineAsm() && "Only know how to handle inlineasm callbr");
3517 visitInlineAsm(I);
3519
3520 // Retrieve successors.
3521 SmallPtrSet<BasicBlock *, 8> Dests;
3522 Dests.insert(I.getDefaultDest());
3523 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3524
3525 // Update successor info.
3526 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3527 for (BasicBlock *Dest : I.getIndirectDests()) {
3528 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
3529 Target->setIsInlineAsmBrIndirectTarget();
3530 // If we introduce a type of asm goto statement that is permitted to use an
3531 // indirect call instruction to jump to its labels, then we should add a
3532 // call to Target->setMachineBlockAddressTaken() here, to mark the target
3533 // block as requiring a BTI.
3534
3535 Target->setLabelMustBeEmitted();
3536 // Don't add duplicate machine successors.
3537 if (Dests.insert(Dest).second)
3538 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3539 }
3540 CallBrMBB->normalizeSuccProbs();
3541
3542 // Drop into default successor.
3543 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3544 MVT::Other, getControlRoot(),
3545 DAG.getBasicBlock(Return)));
3546}
3547
3548void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3549 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3550}
3551
3552void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3553 assert(FuncInfo.MBB->isEHPad() &&
3554 "Call to landingpad not in landing pad!");
3555
3556 // If there aren't registers to copy the values into (e.g., during SjLj
3557 // exceptions), then don't bother to create these DAG nodes.
3558 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3559 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3560 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3561 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3562 return;
3563
3564 // If landingpad's return type is token type, we don't create DAG nodes
3565 // for its exception pointer and selector value. The extraction of exception
3566 // pointer or selector value from token type landingpads is not currently
3567 // supported.
3568 if (LP.getType()->isTokenTy())
3569 return;
3570
3571 SmallVector<EVT, 2> ValueVTs;
3572 SDLoc dl = getCurSDLoc();
3573 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3574 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3575
3576 // Get the two live-in registers as SDValues. The physregs have already been
3577 // copied into virtual registers.
3578 SDValue Ops[2];
3579 if (FuncInfo.ExceptionPointerVirtReg) {
3580 Ops[0] = DAG.getZExtOrTrunc(
3581 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3582 FuncInfo.ExceptionPointerVirtReg,
3583 TLI.getPointerTy(DAG.getDataLayout())),
3584 dl, ValueVTs[0]);
3585 } else {
3586 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3587 }
3588 Ops[1] = DAG.getZExtOrTrunc(
3589 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3590 FuncInfo.ExceptionSelectorVirtReg,
3591 TLI.getPointerTy(DAG.getDataLayout())),
3592 dl, ValueVTs[1]);
3593
3594 // Merge into one.
3595 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3596 DAG.getVTList(ValueVTs), Ops);
3597 setValue(&LP, Res);
3598}
3599
3602 // Update JTCases.
3603 for (JumpTableBlock &JTB : SL->JTCases)
3604 if (JTB.first.HeaderBB == First)
3605 JTB.first.HeaderBB = Last;
3606
3607 // Update BitTestCases.
3608 for (BitTestBlock &BTB : SL->BitTestCases)
3609 if (BTB.Parent == First)
3610 BTB.Parent = Last;
3611}
3612
3613void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3614 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3615
3616 // Update machine-CFG edges with unique successors.
3618 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3619 BasicBlock *BB = I.getSuccessor(i);
3620 bool Inserted = Done.insert(BB).second;
3621 if (!Inserted)
3622 continue;
3623
3624 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3625 addSuccessorWithProb(IndirectBrMBB, Succ);
3626 }
3627 IndirectBrMBB->normalizeSuccProbs();
3628
3629 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
3630 MVT::Other, getControlRoot(),
3631 getValue(I.getAddress())));
3632}
3633
3634void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3635 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3636 DAG.getTarget().Options.NoTrapAfterNoreturn))
3637 return;
3638
3639 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3640}
3641
3642void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3643 SDNodeFlags Flags;
3644 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3645 Flags.copyFMF(*FPOp);
3646
3647 SDValue Op = getValue(I.getOperand(0));
3648 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3649 Op, Flags);
3650 setValue(&I, UnNodeValue);
3651}
3652
3653void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3654 SDNodeFlags Flags;
3655 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3656 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3657 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3658 }
3659 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3660 Flags.setExact(ExactOp->isExact());
3661 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3662 Flags.setDisjoint(DisjointOp->isDisjoint());
3663 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3664 Flags.copyFMF(*FPOp);
3665
3666 SDValue Op1 = getValue(I.getOperand(0));
3667 SDValue Op2 = getValue(I.getOperand(1));
3668 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3669 Op1, Op2, Flags);
3670 setValue(&I, BinNodeValue);
3671}
3672
3673void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3674 SDValue Op1 = getValue(I.getOperand(0));
3675 SDValue Op2 = getValue(I.getOperand(1));
3676
3677 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3678 Op1.getValueType(), DAG.getDataLayout());
3679
3680 // Coerce the shift amount to the right type if we can. This exposes the
3681 // truncate or zext to optimization early.
3682 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3684 "Unexpected shift type");
3685 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3686 }
3687
3688 bool nuw = false;
3689 bool nsw = false;
3690 bool exact = false;
3691
3692 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3693
3694 if (const OverflowingBinaryOperator *OFBinOp =
3696 nuw = OFBinOp->hasNoUnsignedWrap();
3697 nsw = OFBinOp->hasNoSignedWrap();
3698 }
3699 if (const PossiblyExactOperator *ExactOp =
3701 exact = ExactOp->isExact();
3702 }
3703 SDNodeFlags Flags;
3704 Flags.setExact(exact);
3705 Flags.setNoSignedWrap(nsw);
3706 Flags.setNoUnsignedWrap(nuw);
3707 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3708 Flags);
3709 setValue(&I, Res);
3710}
3711
3712void SelectionDAGBuilder::visitSDiv(const User &I) {
3713 SDValue Op1 = getValue(I.getOperand(0));
3714 SDValue Op2 = getValue(I.getOperand(1));
3715
3716 SDNodeFlags Flags;
3717 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3718 cast<PossiblyExactOperator>(&I)->isExact());
3719 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3720 Op2, Flags));
3721}
3722
3723void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3724 ICmpInst::Predicate predicate = I.getPredicate();
3725 SDValue Op1 = getValue(I.getOperand(0));
3726 SDValue Op2 = getValue(I.getOperand(1));
3727 ISD::CondCode Opcode = getICmpCondCode(predicate);
3728
3729 auto &TLI = DAG.getTargetLoweringInfo();
3730 EVT MemVT =
3731 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3732
3733 // If a pointer's DAG type is larger than its memory type then the DAG values
3734 // are zero-extended. This breaks signed comparisons so truncate back to the
3735 // underlying type before doing the compare.
3736 if (Op1.getValueType() != MemVT) {
3737 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3738 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3739 }
3740
3741 SDNodeFlags Flags;
3742 Flags.setSameSign(I.hasSameSign());
3743 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3744
3745 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3746 I.getType());
3747 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
3748}
3749
3750void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3751 FCmpInst::Predicate predicate = I.getPredicate();
3752 SDValue Op1 = getValue(I.getOperand(0));
3753 SDValue Op2 = getValue(I.getOperand(1));
3754
3755 ISD::CondCode Condition = getFCmpCondCode(predicate);
3756 auto *FPMO = cast<FPMathOperator>(&I);
3757 if (FPMO->hasNoNaNs() || TM.Options.NoNaNsFPMath)
3758 Condition = getFCmpCodeWithoutNaN(Condition);
3759
3760 SDNodeFlags Flags;
3761 Flags.copyFMF(*FPMO);
3762 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3763
3764 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3765 I.getType());
3766 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
3767}
3768
3769// Check if the condition of the select has one use or two users that are both
3770// selects with the same condition.
3771static bool hasOnlySelectUsers(const Value *Cond) {
3772 return llvm::all_of(Cond->users(), [](const Value *V) {
3773 return isa<SelectInst>(V);
3774 });
3775}
3776
3777void SelectionDAGBuilder::visitSelect(const User &I) {
3778 SmallVector<EVT, 4> ValueVTs;
3779 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3780 ValueVTs);
3781 unsigned NumValues = ValueVTs.size();
3782 if (NumValues == 0) return;
3783
3784 SmallVector<SDValue, 4> Values(NumValues);
3785 SDValue Cond = getValue(I.getOperand(0));
3786 SDValue LHSVal = getValue(I.getOperand(1));
3787 SDValue RHSVal = getValue(I.getOperand(2));
3788 SmallVector<SDValue, 1> BaseOps(1, Cond);
3790 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3791
3792 bool IsUnaryAbs = false;
3793 bool Negate = false;
3794
3795 SDNodeFlags Flags;
3796 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3797 Flags.copyFMF(*FPOp);
3798
3799 Flags.setUnpredictable(
3800 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3801
3802 // Min/max matching is only viable if all output VTs are the same.
3803 if (all_equal(ValueVTs)) {
3804 EVT VT = ValueVTs[0];
3805 LLVMContext &Ctx = *DAG.getContext();
3806 auto &TLI = DAG.getTargetLoweringInfo();
3807
3808 // We care about the legality of the operation after it has been type
3809 // legalized.
3810 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3811 VT = TLI.getTypeToTransformTo(Ctx, VT);
3812
3813 // If the vselect is legal, assume we want to leave this as a vector setcc +
3814 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3815 // min/max is legal on the scalar type.
3816 bool UseScalarMinMax = VT.isVector() &&
3818
3819 // ValueTracking's select pattern matching does not account for -0.0,
3820 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3821 // -0.0 is less than +0.0.
3822 const Value *LHS, *RHS;
3823 auto SPR = matchSelectPattern(&I, LHS, RHS);
3825 switch (SPR.Flavor) {
3826 case SPF_UMAX: Opc = ISD::UMAX; break;
3827 case SPF_UMIN: Opc = ISD::UMIN; break;
3828 case SPF_SMAX: Opc = ISD::SMAX; break;
3829 case SPF_SMIN: Opc = ISD::SMIN; break;
3830 case SPF_FMINNUM:
3831 switch (SPR.NaNBehavior) {
3832 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3833 case SPNB_RETURNS_NAN: break;
3834 case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break;
3835 case SPNB_RETURNS_ANY:
3836 if (TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT) ||
3837 (UseScalarMinMax &&
3838 TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT.getScalarType())))
3839 Opc = ISD::FMINNUM;
3840 break;
3841 }
3842 break;
3843 case SPF_FMAXNUM:
3844 switch (SPR.NaNBehavior) {
3845 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3846 case SPNB_RETURNS_NAN: break;
3847 case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break;
3848 case SPNB_RETURNS_ANY:
3849 if (TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT) ||
3850 (UseScalarMinMax &&
3851 TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT.getScalarType())))
3852 Opc = ISD::FMAXNUM;
3853 break;
3854 }
3855 break;
3856 case SPF_NABS:
3857 Negate = true;
3858 [[fallthrough]];
3859 case SPF_ABS:
3860 IsUnaryAbs = true;
3861 Opc = ISD::ABS;
3862 break;
3863 default: break;
3864 }
3865
3866 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3867 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3868 (UseScalarMinMax &&
3870 // If the underlying comparison instruction is used by any other
3871 // instruction, the consumed instructions won't be destroyed, so it is
3872 // not profitable to convert to a min/max.
3874 OpCode = Opc;
3875 LHSVal = getValue(LHS);
3876 RHSVal = getValue(RHS);
3877 BaseOps.clear();
3878 }
3879
3880 if (IsUnaryAbs) {
3881 OpCode = Opc;
3882 LHSVal = getValue(LHS);
3883 BaseOps.clear();
3884 }
3885 }
3886
3887 if (IsUnaryAbs) {
3888 for (unsigned i = 0; i != NumValues; ++i) {
3889 SDLoc dl = getCurSDLoc();
3890 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3891 Values[i] =
3892 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3893 if (Negate)
3894 Values[i] = DAG.getNegative(Values[i], dl, VT);
3895 }
3896 } else {
3897 for (unsigned i = 0; i != NumValues; ++i) {
3898 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3899 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3900 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3901 Values[i] = DAG.getNode(
3902 OpCode, getCurSDLoc(),
3903 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3904 }
3905 }
3906
3908 DAG.getVTList(ValueVTs), Values));
3909}
3910
3911void SelectionDAGBuilder::visitTrunc(const User &I) {
3912 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3913 SDValue N = getValue(I.getOperand(0));
3914 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3915 I.getType());
3916 SDNodeFlags Flags;
3917 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3918 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
3919 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
3920 }
3921
3922 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
3923}
3924
3925void SelectionDAGBuilder::visitZExt(const User &I) {
3926 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3927 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3928 SDValue N = getValue(I.getOperand(0));
3929 auto &TLI = DAG.getTargetLoweringInfo();
3930 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3931
3932 SDNodeFlags Flags;
3933 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3934 Flags.setNonNeg(PNI->hasNonNeg());
3935
3936 // Eagerly use nonneg information to canonicalize towards sign_extend if
3937 // that is the target's preference.
3938 // TODO: Let the target do this later.
3939 if (Flags.hasNonNeg() &&
3940 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3941 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3942 return;
3943 }
3944
3945 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3946}
3947
3948void SelectionDAGBuilder::visitSExt(const User &I) {
3949 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3950 // SExt also can't be a cast to bool for same reason. So, nothing much to do
3951 SDValue N = getValue(I.getOperand(0));
3952 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3953 I.getType());
3954 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3955}
3956
3957void SelectionDAGBuilder::visitFPTrunc(const User &I) {
3958 // FPTrunc is never a no-op cast, no need to check
3959 SDValue N = getValue(I.getOperand(0));
3960 SDLoc dl = getCurSDLoc();
3961 SDNodeFlags Flags;
3962 if (auto *TruncInst = dyn_cast<FPMathOperator>(&I))
3963 Flags.copyFMF(*TruncInst);
3964 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3965 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3966 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
3967 DAG.getTargetConstant(
3968 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
3969 Flags));
3970}
3971
3972void SelectionDAGBuilder::visitFPExt(const User &I) {
3973 // FPExt is never a no-op cast, no need to check
3974 SDValue N = getValue(I.getOperand(0));
3975 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3976 I.getType());
3977 SDNodeFlags Flags;
3978 if (auto *TruncInst = dyn_cast<FPMathOperator>(&I))
3979 Flags.copyFMF(*TruncInst);
3980 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3981}
3982
3983void SelectionDAGBuilder::visitFPToUI(const User &I) {
3984 // FPToUI is never a no-op cast, no need to check
3985 SDValue N = getValue(I.getOperand(0));
3986 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3987 I.getType());
3988 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
3989}
3990
3991void SelectionDAGBuilder::visitFPToSI(const User &I) {
3992 // FPToSI is never a no-op cast, no need to check
3993 SDValue N = getValue(I.getOperand(0));
3994 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3995 I.getType());
3996 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
3997}
3998
3999void SelectionDAGBuilder::visitUIToFP(const User &I) {
4000 // UIToFP is never a no-op cast, no need to check
4001 SDValue N = getValue(I.getOperand(0));
4002 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4003 I.getType());
4004 SDNodeFlags Flags;
4005 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
4006 Flags.setNonNeg(PNI->hasNonNeg());
4007
4008 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4009}
4010
4011void SelectionDAGBuilder::visitSIToFP(const User &I) {
4012 // SIToFP is never a no-op cast, no need to check
4013 SDValue N = getValue(I.getOperand(0));
4014 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4015 I.getType());
4016 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
4017}
4018
4019void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
4020 SDValue N = getValue(I.getOperand(0));
4021 // By definition the type of the ptrtoaddr must be equal to the address type.
4022 const auto &TLI = DAG.getTargetLoweringInfo();
4023 EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4024 // The address width must be smaller or equal to the pointer representation
4025 // width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
4026 N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
4027 setValue(&I, N);
4028}
4029
4030void SelectionDAGBuilder::visitPtrToInt(const User &I) {
4031 // What to do depends on the size of the integer and the size of the pointer.
4032 // We can either truncate, zero extend, or no-op, accordingly.
4033 SDValue N = getValue(I.getOperand(0));
4034 auto &TLI = DAG.getTargetLoweringInfo();
4035 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4036 I.getType());
4037 EVT PtrMemVT =
4038 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
4039 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4040 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
4041 setValue(&I, N);
4042}
4043
4044void SelectionDAGBuilder::visitIntToPtr(const User &I) {
4045 // What to do depends on the size of the integer and the size of the pointer.
4046 // We can either truncate, zero extend, or no-op, accordingly.
4047 SDValue N = getValue(I.getOperand(0));
4048 auto &TLI = DAG.getTargetLoweringInfo();
4049 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4050 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4051 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4052 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4053 setValue(&I, N);
4054}
4055
4056void SelectionDAGBuilder::visitBitCast(const User &I) {
4057 SDValue N = getValue(I.getOperand(0));
4058 SDLoc dl = getCurSDLoc();
4059 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4060 I.getType());
4061
4062 // BitCast assures us that source and destination are the same size so this is
4063 // either a BITCAST or a no-op.
4064 if (DestVT != N.getValueType())
4065 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
4066 DestVT, N)); // convert types.
4067 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4068 // might fold any kind of constant expression to an integer constant and that
4069 // is not what we are looking for. Only recognize a bitcast of a genuine
4070 // constant integer as an opaque constant.
4071 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4072 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4073 /*isOpaque*/true));
4074 else
4075 setValue(&I, N); // noop cast.
4076}
4077
4078void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4079 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4080 const Value *SV = I.getOperand(0);
4081 SDValue N = getValue(SV);
4082 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4083
4084 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4085 unsigned DestAS = I.getType()->getPointerAddressSpace();
4086
4087 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4088 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4089
4090 setValue(&I, N);
4091}
4092
4093void SelectionDAGBuilder::visitInsertElement(const User &I) {
4094 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4095 SDValue InVec = getValue(I.getOperand(0));
4096 SDValue InVal = getValue(I.getOperand(1));
4097 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4098 TLI.getVectorIdxTy(DAG.getDataLayout()));
4100 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4101 InVec, InVal, InIdx));
4102}
4103
4104void SelectionDAGBuilder::visitExtractElement(const User &I) {
4105 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4106 SDValue InVec = getValue(I.getOperand(0));
4107 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4108 TLI.getVectorIdxTy(DAG.getDataLayout()));
4110 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4111 InVec, InIdx));
4112}
4113
4114void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4115 SDValue Src1 = getValue(I.getOperand(0));
4116 SDValue Src2 = getValue(I.getOperand(1));
4117 ArrayRef<int> Mask;
4118 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4119 Mask = SVI->getShuffleMask();
4120 else
4121 Mask = cast<ConstantExpr>(I).getShuffleMask();
4122 SDLoc DL = getCurSDLoc();
4123 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4124 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4125 EVT SrcVT = Src1.getValueType();
4126
4127 if (all_of(Mask, [](int Elem) { return Elem == 0; }) &&
4128 VT.isScalableVector()) {
4129 // Canonical splat form of first element of first input vector.
4130 SDValue FirstElt =
4131 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4132 DAG.getVectorIdxConstant(0, DL));
4133 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4134 return;
4135 }
4136
4137 // For now, we only handle splats for scalable vectors.
4138 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4139 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4140 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4141
4142 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4143 unsigned MaskNumElts = Mask.size();
4144
4145 if (SrcNumElts == MaskNumElts) {
4146 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4147 return;
4148 }
4149
4150 // Normalize the shuffle vector since mask and vector length don't match.
4151 if (SrcNumElts < MaskNumElts) {
4152 // Mask is longer than the source vectors. We can use concatenate vector to
4153 // make the mask and vectors lengths match.
4154
4155 if (MaskNumElts % SrcNumElts == 0) {
4156 // Mask length is a multiple of the source vector length.
4157 // Check if the shuffle is some kind of concatenation of the input
4158 // vectors.
4159 unsigned NumConcat = MaskNumElts / SrcNumElts;
4160 bool IsConcat = true;
4161 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4162 for (unsigned i = 0; i != MaskNumElts; ++i) {
4163 int Idx = Mask[i];
4164 if (Idx < 0)
4165 continue;
4166 // Ensure the indices in each SrcVT sized piece are sequential and that
4167 // the same source is used for the whole piece.
4168 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4169 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4170 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4171 IsConcat = false;
4172 break;
4173 }
4174 // Remember which source this index came from.
4175 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4176 }
4177
4178 // The shuffle is concatenating multiple vectors together. Just emit
4179 // a CONCAT_VECTORS operation.
4180 if (IsConcat) {
4181 SmallVector<SDValue, 8> ConcatOps;
4182 for (auto Src : ConcatSrcs) {
4183 if (Src < 0)
4184 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4185 else if (Src == 0)
4186 ConcatOps.push_back(Src1);
4187 else
4188 ConcatOps.push_back(Src2);
4189 }
4190 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4191 return;
4192 }
4193 }
4194
4195 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4196 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4197 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4198 PaddedMaskNumElts);
4199
4200 // Pad both vectors with undefs to make them the same length as the mask.
4201 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4202
4203 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4204 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4205 MOps1[0] = Src1;
4206 MOps2[0] = Src2;
4207
4208 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4209 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4210
4211 // Readjust mask for new input vector length.
4212 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4213 for (unsigned i = 0; i != MaskNumElts; ++i) {
4214 int Idx = Mask[i];
4215 if (Idx >= (int)SrcNumElts)
4216 Idx -= SrcNumElts - PaddedMaskNumElts;
4217 MappedOps[i] = Idx;
4218 }
4219
4220 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4221
4222 // If the concatenated vector was padded, extract a subvector with the
4223 // correct number of elements.
4224 if (MaskNumElts != PaddedMaskNumElts)
4225 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4226 DAG.getVectorIdxConstant(0, DL));
4227
4228 setValue(&I, Result);
4229 return;
4230 }
4231
4232 assert(SrcNumElts > MaskNumElts);
4233
4234 // Analyze the access pattern of the vector to see if we can extract
4235 // two subvectors and do the shuffle.
4236 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4237 bool CanExtract = true;
4238 for (int Idx : Mask) {
4239 unsigned Input = 0;
4240 if (Idx < 0)
4241 continue;
4242
4243 if (Idx >= (int)SrcNumElts) {
4244 Input = 1;
4245 Idx -= SrcNumElts;
4246 }
4247
4248 // If all the indices come from the same MaskNumElts sized portion of
4249 // the sources we can use extract. Also make sure the extract wouldn't
4250 // extract past the end of the source.
4251 int NewStartIdx = alignDown(Idx, MaskNumElts);
4252 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4253 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4254 CanExtract = false;
4255 // Make sure we always update StartIdx as we use it to track if all
4256 // elements are undef.
4257 StartIdx[Input] = NewStartIdx;
4258 }
4259
4260 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4261 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4262 return;
4263 }
4264 if (CanExtract) {
4265 // Extract appropriate subvector and generate a vector shuffle
4266 for (unsigned Input = 0; Input < 2; ++Input) {
4267 SDValue &Src = Input == 0 ? Src1 : Src2;
4268 if (StartIdx[Input] < 0)
4269 Src = DAG.getUNDEF(VT);
4270 else {
4271 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4272 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4273 }
4274 }
4275
4276 // Calculate new mask.
4277 SmallVector<int, 8> MappedOps(Mask);
4278 for (int &Idx : MappedOps) {
4279 if (Idx >= (int)SrcNumElts)
4280 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4281 else if (Idx >= 0)
4282 Idx -= StartIdx[0];
4283 }
4284
4285 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4286 return;
4287 }
4288
4289 // We can't use either concat vectors or extract subvectors so fall back to
4290 // replacing the shuffle with extract and build vector.
4291 // to insert and build vector.
4292 EVT EltVT = VT.getVectorElementType();
4294 for (int Idx : Mask) {
4295 SDValue Res;
4296
4297 if (Idx < 0) {
4298 Res = DAG.getUNDEF(EltVT);
4299 } else {
4300 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4301 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4302
4303 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4304 DAG.getVectorIdxConstant(Idx, DL));
4305 }
4306
4307 Ops.push_back(Res);
4308 }
4309
4310 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4311}
4312
4313void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4314 ArrayRef<unsigned> Indices = I.getIndices();
4315 const Value *Op0 = I.getOperand(0);
4316 const Value *Op1 = I.getOperand(1);
4317 Type *AggTy = I.getType();
4318 Type *ValTy = Op1->getType();
4319 bool IntoUndef = isa<UndefValue>(Op0);
4320 bool FromUndef = isa<UndefValue>(Op1);
4321
4322 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4323
4324 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4325 SmallVector<EVT, 4> AggValueVTs;
4326 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4327 SmallVector<EVT, 4> ValValueVTs;
4328 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4329
4330 unsigned NumAggValues = AggValueVTs.size();
4331 unsigned NumValValues = ValValueVTs.size();
4332 SmallVector<SDValue, 4> Values(NumAggValues);
4333
4334 // Ignore an insertvalue that produces an empty object
4335 if (!NumAggValues) {
4336 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4337 return;
4338 }
4339
4340 SDValue Agg = getValue(Op0);
4341 unsigned i = 0;
4342 // Copy the beginning value(s) from the original aggregate.
4343 for (; i != LinearIndex; ++i)
4344 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4345 SDValue(Agg.getNode(), Agg.getResNo() + i);
4346 // Copy values from the inserted value(s).
4347 if (NumValValues) {
4348 SDValue Val = getValue(Op1);
4349 for (; i != LinearIndex + NumValValues; ++i)
4350 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4351 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4352 }
4353 // Copy remaining value(s) from the original aggregate.
4354 for (; i != NumAggValues; ++i)
4355 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4356 SDValue(Agg.getNode(), Agg.getResNo() + i);
4357
4359 DAG.getVTList(AggValueVTs), Values));
4360}
4361
4362void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4363 ArrayRef<unsigned> Indices = I.getIndices();
4364 const Value *Op0 = I.getOperand(0);
4365 Type *AggTy = Op0->getType();
4366 Type *ValTy = I.getType();
4367 bool OutOfUndef = isa<UndefValue>(Op0);
4368
4369 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4370
4371 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4372 SmallVector<EVT, 4> ValValueVTs;
4373 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4374
4375 unsigned NumValValues = ValValueVTs.size();
4376
4377 // Ignore a extractvalue that produces an empty object
4378 if (!NumValValues) {
4379 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4380 return;
4381 }
4382
4383 SmallVector<SDValue, 4> Values(NumValValues);
4384
4385 SDValue Agg = getValue(Op0);
4386 // Copy out the selected value(s).
4387 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4388 Values[i - LinearIndex] =
4389 OutOfUndef ?
4390 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4391 SDValue(Agg.getNode(), Agg.getResNo() + i);
4392
4394 DAG.getVTList(ValValueVTs), Values));
4395}
4396
4397void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4398 Value *Op0 = I.getOperand(0);
4399 // Note that the pointer operand may be a vector of pointers. Take the scalar
4400 // element which holds a pointer.
4401 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4402 SDValue N = getValue(Op0);
4403 SDLoc dl = getCurSDLoc();
4404 auto &TLI = DAG.getTargetLoweringInfo();
4405 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4406
4407 // For a vector GEP, keep the prefix scalar as long as possible, then
4408 // convert any scalars encountered after the first vector operand to vectors.
4409 bool IsVectorGEP = I.getType()->isVectorTy();
4410 ElementCount VectorElementCount =
4411 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4413
4415 GTI != E; ++GTI) {
4416 const Value *Idx = GTI.getOperand();
4417 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4418 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4419 if (Field) {
4420 // N = N + Offset
4421 uint64_t Offset =
4422 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4423
4424 // In an inbounds GEP with an offset that is nonnegative even when
4425 // interpreted as signed, assume there is no unsigned overflow.
4426 SDNodeFlags Flags;
4427 if (NW.hasNoUnsignedWrap() ||
4428 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4430 Flags.setInBounds(NW.isInBounds());
4431
4432 N = DAG.getMemBasePlusOffset(
4433 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4434 }
4435 } else {
4436 // IdxSize is the width of the arithmetic according to IR semantics.
4437 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4438 // (and fix up the result later).
4439 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4440 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4441 TypeSize ElementSize =
4442 GTI.getSequentialElementStride(DAG.getDataLayout());
4443 // We intentionally mask away the high bits here; ElementSize may not
4444 // fit in IdxTy.
4445 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4446 /*isSigned=*/false, /*implicitTrunc=*/true);
4447 bool ElementScalable = ElementSize.isScalable();
4448
4449 // If this is a scalar constant or a splat vector of constants,
4450 // handle it quickly.
4451 const auto *C = dyn_cast<Constant>(Idx);
4452 if (C && isa<VectorType>(C->getType()))
4453 C = C->getSplatValue();
4454
4455 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4456 if (CI && CI->isZero())
4457 continue;
4458 if (CI && !ElementScalable) {
4459 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4460 LLVMContext &Context = *DAG.getContext();
4461 SDValue OffsVal;
4462 if (N.getValueType().isVector())
4463 OffsVal = DAG.getConstant(
4464 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4465 else
4466 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4467
4468 // In an inbounds GEP with an offset that is nonnegative even when
4469 // interpreted as signed, assume there is no unsigned overflow.
4470 SDNodeFlags Flags;
4471 if (NW.hasNoUnsignedWrap() ||
4472 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4473 Flags.setNoUnsignedWrap(true);
4474 Flags.setInBounds(NW.isInBounds());
4475
4476 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4477
4478 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4479 continue;
4480 }
4481
4482 // N = N + Idx * ElementMul;
4483 SDValue IdxN = getValue(Idx);
4484
4485 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4486 if (N.getValueType().isVector()) {
4487 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4488 VectorElementCount);
4489 IdxN = DAG.getSplat(VT, dl, IdxN);
4490 } else {
4491 EVT VT =
4492 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4493 N = DAG.getSplat(VT, dl, N);
4494 }
4495 }
4496
4497 // If the index is smaller or larger than intptr_t, truncate or extend
4498 // it.
4499 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4500
4501 SDNodeFlags ScaleFlags;
4502 // The multiplication of an index by the type size does not wrap the
4503 // pointer index type in a signed sense (mul nsw).
4505
4506 // The multiplication of an index by the type size does not wrap the
4507 // pointer index type in an unsigned sense (mul nuw).
4508 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4509
4510 if (ElementScalable) {
4511 EVT VScaleTy = N.getValueType().getScalarType();
4512 SDValue VScale = DAG.getNode(
4513 ISD::VSCALE, dl, VScaleTy,
4514 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4515 if (N.getValueType().isVector())
4516 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4517 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4518 ScaleFlags);
4519 } else {
4520 // If this is a multiply by a power of two, turn it into a shl
4521 // immediately. This is a very common case.
4522 if (ElementMul != 1) {
4523 if (ElementMul.isPowerOf2()) {
4524 unsigned Amt = ElementMul.logBase2();
4525 IdxN = DAG.getNode(
4526 ISD::SHL, dl, N.getValueType(), IdxN,
4527 DAG.getShiftAmountConstant(Amt, N.getValueType(), dl),
4528 ScaleFlags);
4529 } else {
4530 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4531 IdxN.getValueType());
4532 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4533 ScaleFlags);
4534 }
4535 }
4536 }
4537
4538 // The successive addition of the current address, truncated to the
4539 // pointer index type and interpreted as an unsigned number, and each
4540 // offset, also interpreted as an unsigned number, does not wrap the
4541 // pointer index type (add nuw).
4542 SDNodeFlags AddFlags;
4543 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4544 AddFlags.setInBounds(NW.isInBounds());
4545
4546 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4547 }
4548 }
4549
4550 if (IsVectorGEP && !N.getValueType().isVector()) {
4551 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4552 N = DAG.getSplat(VT, dl, N);
4553 }
4554
4555 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4556 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4557 if (IsVectorGEP) {
4558 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4559 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4560 }
4561
4562 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4563 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4564
4565 setValue(&I, N);
4566}
4567
4568void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4569 // If this is a fixed sized alloca in the entry block of the function,
4570 // allocate it statically on the stack.
4571 if (FuncInfo.StaticAllocaMap.count(&I))
4572 return; // getValue will auto-populate this.
4573
4574 SDLoc dl = getCurSDLoc();
4575 Type *Ty = I.getAllocatedType();
4576 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4577 auto &DL = DAG.getDataLayout();
4578 TypeSize TySize = DL.getTypeAllocSize(Ty);
4579 MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign());
4580
4581 SDValue AllocSize = getValue(I.getArraySize());
4582
4583 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4584 if (AllocSize.getValueType() != IntPtr)
4585 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4586
4587 if (TySize.isScalable())
4588 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4589 DAG.getVScale(dl, IntPtr,
4590 APInt(IntPtr.getScalarSizeInBits(),
4591 TySize.getKnownMinValue())));
4592 else {
4593 SDValue TySizeValue =
4594 DAG.getConstant(TySize.getFixedValue(), dl, MVT::getIntegerVT(64));
4595 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4596 DAG.getZExtOrTrunc(TySizeValue, dl, IntPtr));
4597 }
4598
4599 // Handle alignment. If the requested alignment is less than or equal to
4600 // the stack alignment, ignore it. If the size is greater than or equal to
4601 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4602 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4603 if (*Alignment <= StackAlign)
4604 Alignment = std::nullopt;
4605
4606 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4607 // Round the size of the allocation up to the stack alignment size
4608 // by add SA-1 to the size. This doesn't overflow because we're computing
4609 // an address inside an alloca.
4610 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4611 DAG.getConstant(StackAlignMask, dl, IntPtr),
4613
4614 // Mask out the low bits for alignment purposes.
4615 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4616 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4617
4618 SDValue Ops[] = {
4619 getRoot(), AllocSize,
4620 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4621 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4622 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4623 setValue(&I, DSA);
4624 DAG.setRoot(DSA.getValue(1));
4625
4626 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4627}
4628
4629static const MDNode *getRangeMetadata(const Instruction &I) {
4630 return I.getMetadata(LLVMContext::MD_range);
4631}
4632
4633static std::optional<ConstantRange> getRange(const Instruction &I) {
4634 if (const auto *CB = dyn_cast<CallBase>(&I))
4635 if (std::optional<ConstantRange> CR = CB->getRange())
4636 return CR;
4637 if (const MDNode *Range = getRangeMetadata(I))
4639 return std::nullopt;
4640}
4641
4643 if (const auto *CB = dyn_cast<CallBase>(&I))
4644 return CB->getRetNoFPClass();
4645 return fcNone;
4646}
4647
4648void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4649 if (I.isAtomic())
4650 return visitAtomicLoad(I);
4651
4652 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4653 const Value *SV = I.getOperand(0);
4654 if (TLI.supportSwiftError()) {
4655 // Swifterror values can come from either a function parameter with
4656 // swifterror attribute or an alloca with swifterror attribute.
4657 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4658 if (Arg->hasSwiftErrorAttr())
4659 return visitLoadFromSwiftError(I);
4660 }
4661
4662 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4663 if (Alloca->isSwiftError())
4664 return visitLoadFromSwiftError(I);
4665 }
4666 }
4667
4668 SDValue Ptr = getValue(SV);
4669
4670 Type *Ty = I.getType();
4671 SmallVector<EVT, 4> ValueVTs, MemVTs;
4673 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4674 unsigned NumValues = ValueVTs.size();
4675 if (NumValues == 0)
4676 return;
4677
4678 Align Alignment = I.getAlign();
4679 AAMDNodes AAInfo = I.getAAMetadata();
4680 const MDNode *Ranges = getRangeMetadata(I);
4681 bool isVolatile = I.isVolatile();
4682 MachineMemOperand::Flags MMOFlags =
4683 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4684
4685 SDValue Root;
4686 bool ConstantMemory = false;
4687 if (isVolatile)
4688 // Serialize volatile loads with other side effects.
4689 Root = getRoot();
4690 else if (NumValues > MaxParallelChains)
4691 Root = getMemoryRoot();
4692 else if (BatchAA &&
4693 BatchAA->pointsToConstantMemory(MemoryLocation(
4694 SV,
4695 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4696 AAInfo))) {
4697 // Do not serialize (non-volatile) loads of constant memory with anything.
4698 Root = DAG.getEntryNode();
4699 ConstantMemory = true;
4701 } else {
4702 // Do not serialize non-volatile loads against each other.
4703 Root = DAG.getRoot();
4704 }
4705
4706 SDLoc dl = getCurSDLoc();
4707
4708 if (isVolatile)
4709 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4710
4711 SmallVector<SDValue, 4> Values(NumValues);
4712 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4713
4714 unsigned ChainI = 0;
4715 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4716 // Serializing loads here may result in excessive register pressure, and
4717 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4718 // could recover a bit by hoisting nodes upward in the chain by recognizing
4719 // they are side-effect free or do not alias. The optimizer should really
4720 // avoid this case by converting large object/array copies to llvm.memcpy
4721 // (MaxParallelChains should always remain as failsafe).
4722 if (ChainI == MaxParallelChains) {
4723 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4724 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4725 ArrayRef(Chains.data(), ChainI));
4726 Root = Chain;
4727 ChainI = 0;
4728 }
4729
4730 // TODO: MachinePointerInfo only supports a fixed length offset.
4731 MachinePointerInfo PtrInfo =
4732 !Offsets[i].isScalable() || Offsets[i].isZero()
4733 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4734 : MachinePointerInfo();
4735
4736 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4737 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4738 MMOFlags, AAInfo, Ranges);
4739 Chains[ChainI] = L.getValue(1);
4740
4741 if (MemVTs[i] != ValueVTs[i])
4742 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4743
4744 Values[i] = L;
4745 }
4746
4747 if (!ConstantMemory) {
4748 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4749 ArrayRef(Chains.data(), ChainI));
4750 if (isVolatile)
4751 DAG.setRoot(Chain);
4752 else
4753 PendingLoads.push_back(Chain);
4754 }
4755
4756 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4757 DAG.getVTList(ValueVTs), Values));
4758}
4759
4760void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4761 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4762 "call visitStoreToSwiftError when backend supports swifterror");
4763
4764 SmallVector<EVT, 4> ValueVTs;
4765 SmallVector<uint64_t, 4> Offsets;
4766 const Value *SrcV = I.getOperand(0);
4767 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4768 SrcV->getType(), ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4769 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4770 "expect a single EVT for swifterror");
4771
4772 SDValue Src = getValue(SrcV);
4773 // Create a virtual register, then update the virtual register.
4774 Register VReg =
4775 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4776 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4777 // Chain can be getRoot or getControlRoot.
4778 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4779 SDValue(Src.getNode(), Src.getResNo()));
4780 DAG.setRoot(CopyNode);
4781}
4782
4783void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4784 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4785 "call visitLoadFromSwiftError when backend supports swifterror");
4786
4787 assert(!I.isVolatile() &&
4788 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4789 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4790 "Support volatile, non temporal, invariant for load_from_swift_error");
4791
4792 const Value *SV = I.getOperand(0);
4793 Type *Ty = I.getType();
4794 assert(
4795 (!BatchAA ||
4796 !BatchAA->pointsToConstantMemory(MemoryLocation(
4797 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4798 I.getAAMetadata()))) &&
4799 "load_from_swift_error should not be constant memory");
4800
4801 SmallVector<EVT, 4> ValueVTs;
4802 SmallVector<uint64_t, 4> Offsets;
4803 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4804 ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4805 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4806 "expect a single EVT for swifterror");
4807
4808 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4809 SDValue L = DAG.getCopyFromReg(
4810 getRoot(), getCurSDLoc(),
4811 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4812
4813 setValue(&I, L);
4814}
4815
4816void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4817 if (I.isAtomic())
4818 return visitAtomicStore(I);
4819
4820 const Value *SrcV = I.getOperand(0);
4821 const Value *PtrV = I.getOperand(1);
4822
4823 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4824 if (TLI.supportSwiftError()) {
4825 // Swifterror values can come from either a function parameter with
4826 // swifterror attribute or an alloca with swifterror attribute.
4827 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4828 if (Arg->hasSwiftErrorAttr())
4829 return visitStoreToSwiftError(I);
4830 }
4831
4832 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4833 if (Alloca->isSwiftError())
4834 return visitStoreToSwiftError(I);
4835 }
4836 }
4837
4838 SmallVector<EVT, 4> ValueVTs, MemVTs;
4840 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4841 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4842 unsigned NumValues = ValueVTs.size();
4843 if (NumValues == 0)
4844 return;
4845
4846 // Get the lowered operands. Note that we do this after
4847 // checking if NumResults is zero, because with zero results
4848 // the operands won't have values in the map.
4849 SDValue Src = getValue(SrcV);
4850 SDValue Ptr = getValue(PtrV);
4851
4852 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4853 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4854 SDLoc dl = getCurSDLoc();
4855 Align Alignment = I.getAlign();
4856 AAMDNodes AAInfo = I.getAAMetadata();
4857
4858 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4859
4860 unsigned ChainI = 0;
4861 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4862 // See visitLoad comments.
4863 if (ChainI == MaxParallelChains) {
4864 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4865 ArrayRef(Chains.data(), ChainI));
4866 Root = Chain;
4867 ChainI = 0;
4868 }
4869
4870 // TODO: MachinePointerInfo only supports a fixed length offset.
4871 MachinePointerInfo PtrInfo =
4872 !Offsets[i].isScalable() || Offsets[i].isZero()
4873 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4874 : MachinePointerInfo();
4875
4876 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4877 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4878 if (MemVTs[i] != ValueVTs[i])
4879 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4880 SDValue St =
4881 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4882 Chains[ChainI] = St;
4883 }
4884
4885 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4886 ArrayRef(Chains.data(), ChainI));
4887 setValue(&I, StoreNode);
4888 DAG.setRoot(StoreNode);
4889}
4890
4891void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4892 bool IsCompressing) {
4893 SDLoc sdl = getCurSDLoc();
4894
4895 Value *Src0Operand = I.getArgOperand(0);
4896 Value *PtrOperand = I.getArgOperand(1);
4897 Value *MaskOperand = I.getArgOperand(2);
4898 Align Alignment = I.getParamAlign(1).valueOrOne();
4899
4900 SDValue Ptr = getValue(PtrOperand);
4901 SDValue Src0 = getValue(Src0Operand);
4902 SDValue Mask = getValue(MaskOperand);
4903 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4904
4905 EVT VT = Src0.getValueType();
4906
4907 auto MMOFlags = MachineMemOperand::MOStore;
4908 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4910
4911 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4912 MachinePointerInfo(PtrOperand), MMOFlags,
4913 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4914
4915 const auto &TLI = DAG.getTargetLoweringInfo();
4916
4917 SDValue StoreNode =
4918 !IsCompressing && TTI->hasConditionalLoadStoreForType(
4919 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4920 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4921 Mask)
4922 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4923 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4924 IsCompressing);
4925 DAG.setRoot(StoreNode);
4926 setValue(&I, StoreNode);
4927}
4928
4929// Get a uniform base for the Gather/Scatter intrinsic.
4930// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4931// We try to represent it as a base pointer + vector of indices.
4932// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4933// The first operand of the GEP may be a single pointer or a vector of pointers
4934// Example:
4935// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4936// or
4937// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4938// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4939//
4940// When the first GEP operand is a single pointer - it is the uniform base we
4941// are looking for. If first operand of the GEP is a splat vector - we
4942// extract the splat value and use it as a uniform base.
4943// In all other cases the function returns 'false'.
4944static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4945 SDValue &Scale, SelectionDAGBuilder *SDB,
4946 const BasicBlock *CurBB, uint64_t ElemSize) {
4947 SelectionDAG& DAG = SDB->DAG;
4948 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4949 const DataLayout &DL = DAG.getDataLayout();
4950
4951 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4952
4953 // Handle splat constant pointer.
4954 if (auto *C = dyn_cast<Constant>(Ptr)) {
4955 C = C->getSplatValue();
4956 if (!C)
4957 return false;
4958
4959 Base = SDB->getValue(C);
4960
4961 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
4962 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
4963 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
4964 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4965 return true;
4966 }
4967
4969 if (!GEP || GEP->getParent() != CurBB)
4970 return false;
4971
4972 if (GEP->getNumOperands() != 2)
4973 return false;
4974
4975 const Value *BasePtr = GEP->getPointerOperand();
4976 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
4977
4978 // Make sure the base is scalar and the index is a vector.
4979 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
4980 return false;
4981
4982 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
4983 if (ScaleVal.isScalable())
4984 return false;
4985
4986 // Target may not support the required addressing mode.
4987 if (ScaleVal != 1 &&
4988 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
4989 return false;
4990
4991 Base = SDB->getValue(BasePtr);
4992 Index = SDB->getValue(IndexVal);
4993
4994 Scale =
4995 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4996 return true;
4997}
4998
4999void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
5000 SDLoc sdl = getCurSDLoc();
5001
5002 // llvm.masked.scatter.*(Src0, Ptrs, Mask)
5003 const Value *Ptr = I.getArgOperand(1);
5004 SDValue Src0 = getValue(I.getArgOperand(0));
5005 SDValue Mask = getValue(I.getArgOperand(2));
5006 EVT VT = Src0.getValueType();
5007 Align Alignment = I.getParamAlign(1).valueOrOne();
5008 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5009
5010 SDValue Base;
5011 SDValue Index;
5012 SDValue Scale;
5013 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5014 I.getParent(), VT.getScalarStoreSize());
5015
5016 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5017 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5018 MachinePointerInfo(AS), MachineMemOperand::MOStore,
5019 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
5020 if (!UniformBase) {
5021 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5022 Index = getValue(Ptr);
5023 Scale =
5024 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5025 }
5026
5027 EVT IdxVT = Index.getValueType();
5028 EVT EltTy = IdxVT.getVectorElementType();
5029 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5030 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5031 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5032 }
5033
5034 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
5035 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
5036 Ops, MMO, ISD::SIGNED_SCALED, false);
5037 DAG.setRoot(Scatter);
5038 setValue(&I, Scatter);
5039}
5040
5041void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5042 SDLoc sdl = getCurSDLoc();
5043
5044 Value *PtrOperand = I.getArgOperand(0);
5045 Value *MaskOperand = I.getArgOperand(1);
5046 Value *Src0Operand = I.getArgOperand(2);
5047 Align Alignment = I.getParamAlign(0).valueOrOne();
5048
5049 SDValue Ptr = getValue(PtrOperand);
5050 SDValue Src0 = getValue(Src0Operand);
5051 SDValue Mask = getValue(MaskOperand);
5052 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5053
5054 EVT VT = Src0.getValueType();
5055 AAMDNodes AAInfo = I.getAAMetadata();
5056 const MDNode *Ranges = getRangeMetadata(I);
5057
5058 // Do not serialize masked loads of constant memory with anything.
5059 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5060 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5061
5062 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5063
5064 auto MMOFlags = MachineMemOperand::MOLoad;
5065 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5067 if (I.hasMetadata(LLVMContext::MD_invariant_load))
5069
5070 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5071 MachinePointerInfo(PtrOperand), MMOFlags,
5072 LocationSize::beforeOrAfterPointer(), Alignment, AAInfo, Ranges);
5073
5074 const auto &TLI = DAG.getTargetLoweringInfo();
5075
5076 // The Load/Res may point to different values and both of them are output
5077 // variables.
5078 SDValue Load;
5079 SDValue Res;
5080 if (!IsExpanding &&
5081 TTI->hasConditionalLoadStoreForType(Src0Operand->getType(),
5082 /*IsStore=*/false))
5083 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5084 else
5085 Res = Load =
5086 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5087 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5088 if (AddToChain)
5089 PendingLoads.push_back(Load.getValue(1));
5090 setValue(&I, Res);
5091}
5092
5093void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5094 SDLoc sdl = getCurSDLoc();
5095
5096 // @llvm.masked.gather.*(Ptrs, Mask, Src0)
5097 const Value *Ptr = I.getArgOperand(0);
5098 SDValue Src0 = getValue(I.getArgOperand(2));
5099 SDValue Mask = getValue(I.getArgOperand(1));
5100
5101 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5102 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5103 Align Alignment = I.getParamAlign(0).valueOrOne();
5104
5105 const MDNode *Ranges = getRangeMetadata(I);
5106
5107 SDValue Root = DAG.getRoot();
5108 SDValue Base;
5109 SDValue Index;
5110 SDValue Scale;
5111 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5112 I.getParent(), VT.getScalarStoreSize());
5113 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5114 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5115 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5116 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5117 Ranges);
5118
5119 if (!UniformBase) {
5120 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5121 Index = getValue(Ptr);
5122 Scale =
5123 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5124 }
5125
5126 EVT IdxVT = Index.getValueType();
5127 EVT EltTy = IdxVT.getVectorElementType();
5128 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5129 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5130 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5131 }
5132
5133 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5134 SDValue Gather =
5135 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5137
5138 PendingLoads.push_back(Gather.getValue(1));
5139 setValue(&I, Gather);
5140}
5141
5142void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5143 SDLoc dl = getCurSDLoc();
5144 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5145 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5146 SyncScope::ID SSID = I.getSyncScopeID();
5147
5148 SDValue InChain = getRoot();
5149
5150 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5151 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5152
5153 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5154 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5155
5156 MachineFunction &MF = DAG.getMachineFunction();
5157 MachineMemOperand *MMO = MF.getMachineMemOperand(
5158 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5159 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5160 FailureOrdering);
5161
5162 SDValue L = DAG.getAtomicCmpSwap(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS,
5163 dl, MemVT, VTs, InChain,
5164 getValue(I.getPointerOperand()),
5165 getValue(I.getCompareOperand()),
5166 getValue(I.getNewValOperand()), MMO);
5167
5168 SDValue OutChain = L.getValue(2);
5169
5170 setValue(&I, L);
5171 DAG.setRoot(OutChain);
5172}
5173
5174void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5175 SDLoc dl = getCurSDLoc();
5177 switch (I.getOperation()) {
5178 default: llvm_unreachable("Unknown atomicrmw operation");
5179 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
5180 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
5181 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
5182 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
5183 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
5184 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
5185 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
5186 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
5187 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
5188 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
5189 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
5190 case AtomicRMWInst::FAdd: NT = ISD::ATOMIC_LOAD_FADD; break;
5191 case AtomicRMWInst::FSub: NT = ISD::ATOMIC_LOAD_FSUB; break;
5192 case AtomicRMWInst::FMax: NT = ISD::ATOMIC_LOAD_FMAX; break;
5193 case AtomicRMWInst::FMin: NT = ISD::ATOMIC_LOAD_FMIN; break;
5195 NT = ISD::ATOMIC_LOAD_FMAXIMUM;
5196 break;
5198 NT = ISD::ATOMIC_LOAD_FMINIMUM;
5199 break;
5201 NT = ISD::ATOMIC_LOAD_UINC_WRAP;
5202 break;
5204 NT = ISD::ATOMIC_LOAD_UDEC_WRAP;
5205 break;
5207 NT = ISD::ATOMIC_LOAD_USUB_COND;
5208 break;
5210 NT = ISD::ATOMIC_LOAD_USUB_SAT;
5211 break;
5212 }
5213 AtomicOrdering Ordering = I.getOrdering();
5214 SyncScope::ID SSID = I.getSyncScopeID();
5215
5216 SDValue InChain = getRoot();
5217
5218 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5219 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5220 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5221
5222 MachineFunction &MF = DAG.getMachineFunction();
5223 MachineMemOperand *MMO = MF.getMachineMemOperand(
5224 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5225 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
5226
5227 SDValue L =
5228 DAG.getAtomic(NT, dl, MemVT, InChain,
5229 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5230 MMO);
5231
5232 SDValue OutChain = L.getValue(1);
5233
5234 setValue(&I, L);
5235 DAG.setRoot(OutChain);
5236}
5237
5238void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5239 SDLoc dl = getCurSDLoc();
5240 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5241 SDValue Ops[3];
5242 Ops[0] = getRoot();
5243 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5244 TLI.getFenceOperandTy(DAG.getDataLayout()));
5245 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5246 TLI.getFenceOperandTy(DAG.getDataLayout()));
5247 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5248 setValue(&I, N);
5249 DAG.setRoot(N);
5250}
5251
5252void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5253 SDLoc dl = getCurSDLoc();
5254 AtomicOrdering Order = I.getOrdering();
5255 SyncScope::ID SSID = I.getSyncScopeID();
5256
5257 SDValue InChain = getRoot();
5258
5259 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5260 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5261 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5262
5263 if (!TLI.supportsUnalignedAtomics() &&
5264 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5265 report_fatal_error("Cannot generate unaligned atomic load");
5266
5267 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5268
5269 const MDNode *Ranges = getRangeMetadata(I);
5270 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5271 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5272 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5273
5274 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5275
5276 SDValue Ptr = getValue(I.getPointerOperand());
5277 SDValue L =
5278 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5279
5280 SDValue OutChain = L.getValue(1);
5281 if (MemVT != VT)
5282 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5283
5284 setValue(&I, L);
5285 DAG.setRoot(OutChain);
5286}
5287
5288void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5289 SDLoc dl = getCurSDLoc();
5290
5291 AtomicOrdering Ordering = I.getOrdering();
5292 SyncScope::ID SSID = I.getSyncScopeID();
5293
5294 SDValue InChain = getRoot();
5295
5296 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5297 EVT MemVT =
5298 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5299
5300 if (!TLI.supportsUnalignedAtomics() &&
5301 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5302 report_fatal_error("Cannot generate unaligned atomic store");
5303
5304 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5305
5306 MachineFunction &MF = DAG.getMachineFunction();
5307 MachineMemOperand *MMO = MF.getMachineMemOperand(
5308 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5309 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5310
5311 SDValue Val = getValue(I.getValueOperand());
5312 if (Val.getValueType() != MemVT)
5313 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5314 SDValue Ptr = getValue(I.getPointerOperand());
5315
5316 SDValue OutChain =
5317 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5318
5319 setValue(&I, OutChain);
5320 DAG.setRoot(OutChain);
5321}
5322
5323/// Check if this intrinsic call depends on the chain (1st return value)
5324/// and if it only *loads* memory.
5325/// Ignore the callsite's attributes. A specific call site may be marked with
5326/// readnone, but the lowering code will expect the chain based on the
5327/// definition.
5328std::pair<bool, bool>
5329SelectionDAGBuilder::getTargetIntrinsicCallProperties(const CallBase &I) {
5330 const Function *F = I.getCalledFunction();
5331 bool HasChain = !F->doesNotAccessMemory();
5332 bool OnlyLoad =
5333 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5334
5335 return {HasChain, OnlyLoad};
5336}
5337
5338SmallVector<SDValue, 8> SelectionDAGBuilder::getTargetIntrinsicOperands(
5339 const CallBase &I, bool HasChain, bool OnlyLoad,
5340 TargetLowering::IntrinsicInfo *TgtMemIntrinsicInfo) {
5341 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5342
5343 // Build the operand list.
5345 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5346 if (OnlyLoad) {
5347 // We don't need to serialize loads against other loads.
5348 Ops.push_back(DAG.getRoot());
5349 } else {
5350 Ops.push_back(getRoot());
5351 }
5352 }
5353
5354 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5355 if (!TgtMemIntrinsicInfo || TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_VOID ||
5356 TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_W_CHAIN)
5357 Ops.push_back(DAG.getTargetConstant(I.getIntrinsicID(), getCurSDLoc(),
5358 TLI.getPointerTy(DAG.getDataLayout())));
5359
5360 // Add all operands of the call to the operand list.
5361 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5362 const Value *Arg = I.getArgOperand(i);
5363 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5364 Ops.push_back(getValue(Arg));
5365 continue;
5366 }
5367
5368 // Use TargetConstant instead of a regular constant for immarg.
5369 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5370 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5371 assert(CI->getBitWidth() <= 64 &&
5372 "large intrinsic immediates not handled");
5373 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5374 } else {
5375 Ops.push_back(
5376 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5377 }
5378 }
5379
5380 if (std::optional<OperandBundleUse> Bundle =
5381 I.getOperandBundle(LLVMContext::OB_deactivation_symbol)) {
5382 auto *Sym = Bundle->Inputs[0].get();
5383 SDValue SDSym = getValue(Sym);
5384 SDSym = DAG.getDeactivationSymbol(cast<GlobalValue>(Sym));
5385 Ops.push_back(SDSym);
5386 }
5387
5388 if (std::optional<OperandBundleUse> Bundle =
5389 I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5390 Value *Token = Bundle->Inputs[0].get();
5391 SDValue ConvControlToken = getValue(Token);
5392 assert(Ops.back().getValueType() != MVT::Glue &&
5393 "Did not expect another glue node here.");
5394 ConvControlToken =
5395 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5396 Ops.push_back(ConvControlToken);
5397 }
5398
5399 return Ops;
5400}
5401
5402SDVTList SelectionDAGBuilder::getTargetIntrinsicVTList(const CallBase &I,
5403 bool HasChain) {
5404 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5405
5406 SmallVector<EVT, 4> ValueVTs;
5407 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5408
5409 if (HasChain)
5410 ValueVTs.push_back(MVT::Other);
5411
5412 return DAG.getVTList(ValueVTs);
5413}
5414
5415/// Get an INTRINSIC node for a target intrinsic which does not touch memory.
5416SDValue SelectionDAGBuilder::getTargetNonMemIntrinsicNode(
5417 const Type &IntrinsicVT, bool HasChain, ArrayRef<SDValue> Ops,
5418 const SDVTList &VTs) {
5419 if (!HasChain)
5420 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5421 if (!IntrinsicVT.isVoidTy())
5422 return DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5423 return DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5424}
5425
5426/// Set root, convert return type if necessary and check alignment.
5427SDValue SelectionDAGBuilder::handleTargetIntrinsicRet(const CallBase &I,
5428 bool HasChain,
5429 bool OnlyLoad,
5430 SDValue Result) {
5431 if (HasChain) {
5432 SDValue Chain = Result.getValue(Result.getNode()->getNumValues() - 1);
5433 if (OnlyLoad)
5434 PendingLoads.push_back(Chain);
5435 else
5436 DAG.setRoot(Chain);
5437 }
5438
5439 if (I.getType()->isVoidTy())
5440 return Result;
5441
5442 if (MaybeAlign Alignment = I.getRetAlign(); InsertAssertAlign && Alignment) {
5443 // Insert `assertalign` node if there's an alignment.
5444 Result = DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5445 } else if (!isa<VectorType>(I.getType())) {
5446 Result = lowerRangeToAssertZExt(DAG, I, Result);
5447 }
5448
5449 return Result;
5450}
5451
5452/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5453/// node.
5454void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5455 unsigned Intrinsic) {
5456 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
5457
5458 // Info is set by getTgtMemIntrinsic
5459 TargetLowering::IntrinsicInfo Info;
5460 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5461 bool IsTgtMemIntrinsic =
5462 TLI.getTgtMemIntrinsic(Info, I, DAG.getMachineFunction(), Intrinsic);
5463
5464 SmallVector<SDValue, 8> Ops = getTargetIntrinsicOperands(
5465 I, HasChain, OnlyLoad, IsTgtMemIntrinsic ? &Info : nullptr);
5466 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
5467
5468 // Propagate fast-math-flags from IR to node(s).
5469 SDNodeFlags Flags;
5470 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5471 Flags.copyFMF(*FPMO);
5472 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5473
5474 // Create the node.
5476
5477 // In some cases, custom collection of operands from CallInst I may be needed.
5479 if (IsTgtMemIntrinsic) {
5480 // This is target intrinsic that touches memory
5481 //
5482 // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic
5483 // didn't yield anything useful.
5484 MachinePointerInfo MPI;
5485 if (Info.ptrVal)
5486 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5487 else if (Info.fallbackAddressSpace)
5488 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5489 EVT MemVT = Info.memVT;
5490 LocationSize Size = LocationSize::precise(Info.size);
5491 if (Size.hasValue() && !Size.getValue())
5493 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5494 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5495 MPI, Info.flags, Size, Alignment, I.getAAMetadata(), /*Ranges=*/nullptr,
5496 Info.ssid, Info.order, Info.failureOrder);
5497 Result =
5498 DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops, MemVT, MMO);
5499 } else {
5500 Result = getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
5501 }
5502
5503 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
5504
5505 setValue(&I, Result);
5506}
5507
5508/// GetSignificand - Get the significand and build it into a floating-point
5509/// number with exponent of 1:
5510///
5511/// Op = (Op & 0x007fffff) | 0x3f800000;
5512///
5513/// where Op is the hexadecimal representation of floating point value.
5515 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5516 DAG.getConstant(0x007fffff, dl, MVT::i32));
5517 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5518 DAG.getConstant(0x3f800000, dl, MVT::i32));
5519 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5520}
5521
5522/// GetExponent - Get the exponent:
5523///
5524/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5525///
5526/// where Op is the hexadecimal representation of floating point value.
5528 const TargetLowering &TLI, const SDLoc &dl) {
5529 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5530 DAG.getConstant(0x7f800000, dl, MVT::i32));
5531 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5532 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5533 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5534 DAG.getConstant(127, dl, MVT::i32));
5535 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5536}
5537
5538/// getF32Constant - Get 32-bit floating point constant.
5539static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt,
5540 const SDLoc &dl) {
5541 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5542 MVT::f32);
5543}
5544
5546 SelectionDAG &DAG) {
5547 // TODO: What fast-math-flags should be set on the floating-point nodes?
5548
5549 // IntegerPartOfX = ((int32_t)(t0);
5550 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5551
5552 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5553 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5554 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5555
5556 // IntegerPartOfX <<= 23;
5557 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5558 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5559
5560 SDValue TwoToFractionalPartOfX;
5561 if (LimitFloatPrecision <= 6) {
5562 // For floating-point precision of 6:
5563 //
5564 // TwoToFractionalPartOfX =
5565 // 0.997535578f +
5566 // (0.735607626f + 0.252464424f * x) * x;
5567 //
5568 // error 0.0144103317, which is 6 bits
5569 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5570 getF32Constant(DAG, 0x3e814304, dl));
5571 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5572 getF32Constant(DAG, 0x3f3c50c8, dl));
5573 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5574 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5575 getF32Constant(DAG, 0x3f7f5e7e, dl));
5576 } else if (LimitFloatPrecision <= 12) {
5577 // For floating-point precision of 12:
5578 //
5579 // TwoToFractionalPartOfX =
5580 // 0.999892986f +
5581 // (0.696457318f +
5582 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5583 //
5584 // error 0.000107046256, which is 13 to 14 bits
5585 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5586 getF32Constant(DAG, 0x3da235e3, dl));
5587 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5588 getF32Constant(DAG, 0x3e65b8f3, dl));
5589 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5590 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5591 getF32Constant(DAG, 0x3f324b07, dl));
5592 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5593 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5594 getF32Constant(DAG, 0x3f7ff8fd, dl));
5595 } else { // LimitFloatPrecision <= 18
5596 // For floating-point precision of 18:
5597 //
5598 // TwoToFractionalPartOfX =
5599 // 0.999999982f +
5600 // (0.693148872f +
5601 // (0.240227044f +
5602 // (0.554906021e-1f +
5603 // (0.961591928e-2f +
5604 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5605 // error 2.47208000*10^(-7), which is better than 18 bits
5606 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5607 getF32Constant(DAG, 0x3924b03e, dl));
5608 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5609 getF32Constant(DAG, 0x3ab24b87, dl));
5610 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5611 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5612 getF32Constant(DAG, 0x3c1d8c17, dl));
5613 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5614 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5615 getF32Constant(DAG, 0x3d634a1d, dl));
5616 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5617 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5618 getF32Constant(DAG, 0x3e75fe14, dl));
5619 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5620 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5621 getF32Constant(DAG, 0x3f317234, dl));
5622 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5623 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5624 getF32Constant(DAG, 0x3f800000, dl));
5625 }
5626
5627 // Add the exponent into the result in integer domain.
5628 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5629 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5630 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5631}
5632
5633/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5634/// limited-precision mode.
5636 const TargetLowering &TLI, SDNodeFlags Flags) {
5637 if (Op.getValueType() == MVT::f32 &&
5639
5640 // Put the exponent in the right bit position for later addition to the
5641 // final result:
5642 //
5643 // t0 = Op * log2(e)
5644
5645 // TODO: What fast-math-flags should be set here?
5646 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5647 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5648 return getLimitedPrecisionExp2(t0, dl, DAG);
5649 }
5650
5651 // No special expansion.
5652 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5653}
5654
5655/// expandLog - Lower a log intrinsic. Handles the special sequences for
5656/// limited-precision mode.
5658 const TargetLowering &TLI, SDNodeFlags Flags) {
5659 // TODO: What fast-math-flags should be set on the floating-point nodes?
5660
5661 if (Op.getValueType() == MVT::f32 &&
5663 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5664
5665 // Scale the exponent by log(2).
5666 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5667 SDValue LogOfExponent =
5668 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5669 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5670
5671 // Get the significand and build it into a floating-point number with
5672 // exponent of 1.
5673 SDValue X = GetSignificand(DAG, Op1, dl);
5674
5675 SDValue LogOfMantissa;
5676 if (LimitFloatPrecision <= 6) {
5677 // For floating-point precision of 6:
5678 //
5679 // LogofMantissa =
5680 // -1.1609546f +
5681 // (1.4034025f - 0.23903021f * x) * x;
5682 //
5683 // error 0.0034276066, which is better than 8 bits
5684 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5685 getF32Constant(DAG, 0xbe74c456, dl));
5686 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5687 getF32Constant(DAG, 0x3fb3a2b1, dl));
5688 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5689 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5690 getF32Constant(DAG, 0x3f949a29, dl));
5691 } else if (LimitFloatPrecision <= 12) {
5692 // For floating-point precision of 12:
5693 //
5694 // LogOfMantissa =
5695 // -1.7417939f +
5696 // (2.8212026f +
5697 // (-1.4699568f +
5698 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5699 //
5700 // error 0.000061011436, which is 14 bits
5701 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5702 getF32Constant(DAG, 0xbd67b6d6, dl));
5703 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5704 getF32Constant(DAG, 0x3ee4f4b8, dl));
5705 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5706 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5707 getF32Constant(DAG, 0x3fbc278b, dl));
5708 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5709 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5710 getF32Constant(DAG, 0x40348e95, dl));
5711 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5712 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5713 getF32Constant(DAG, 0x3fdef31a, dl));
5714 } else { // LimitFloatPrecision <= 18
5715 // For floating-point precision of 18:
5716 //
5717 // LogOfMantissa =
5718 // -2.1072184f +
5719 // (4.2372794f +
5720 // (-3.7029485f +
5721 // (2.2781945f +
5722 // (-0.87823314f +
5723 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5724 //
5725 // error 0.0000023660568, which is better than 18 bits
5726 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5727 getF32Constant(DAG, 0xbc91e5ac, dl));
5728 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5729 getF32Constant(DAG, 0x3e4350aa, dl));
5730 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5731 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5732 getF32Constant(DAG, 0x3f60d3e3, dl));
5733 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5734 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5735 getF32Constant(DAG, 0x4011cdf0, dl));
5736 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5737 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5738 getF32Constant(DAG, 0x406cfd1c, dl));
5739 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5740 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5741 getF32Constant(DAG, 0x408797cb, dl));
5742 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5743 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5744 getF32Constant(DAG, 0x4006dcab, dl));
5745 }
5746
5747 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5748 }
5749
5750 // No special expansion.
5751 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5752}
5753
5754/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5755/// limited-precision mode.
5757 const TargetLowering &TLI, SDNodeFlags Flags) {
5758 // TODO: What fast-math-flags should be set on the floating-point nodes?
5759
5760 if (Op.getValueType() == MVT::f32 &&
5762 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5763
5764 // Get the exponent.
5765 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5766
5767 // Get the significand and build it into a floating-point number with
5768 // exponent of 1.
5769 SDValue X = GetSignificand(DAG, Op1, dl);
5770
5771 // Different possible minimax approximations of significand in
5772 // floating-point for various degrees of accuracy over [1,2].
5773 SDValue Log2ofMantissa;
5774 if (LimitFloatPrecision <= 6) {
5775 // For floating-point precision of 6:
5776 //
5777 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5778 //
5779 // error 0.0049451742, which is more than 7 bits
5780 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5781 getF32Constant(DAG, 0xbeb08fe0, dl));
5782 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5783 getF32Constant(DAG, 0x40019463, dl));
5784 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5785 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5786 getF32Constant(DAG, 0x3fd6633d, dl));
5787 } else if (LimitFloatPrecision <= 12) {
5788 // For floating-point precision of 12:
5789 //
5790 // Log2ofMantissa =
5791 // -2.51285454f +
5792 // (4.07009056f +
5793 // (-2.12067489f +
5794 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5795 //
5796 // error 0.0000876136000, which is better than 13 bits
5797 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5798 getF32Constant(DAG, 0xbda7262e, dl));
5799 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5800 getF32Constant(DAG, 0x3f25280b, dl));
5801 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5802 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5803 getF32Constant(DAG, 0x4007b923, dl));
5804 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5805 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5806 getF32Constant(DAG, 0x40823e2f, dl));
5807 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5808 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5809 getF32Constant(DAG, 0x4020d29c, dl));
5810 } else { // LimitFloatPrecision <= 18
5811 // For floating-point precision of 18:
5812 //
5813 // Log2ofMantissa =
5814 // -3.0400495f +
5815 // (6.1129976f +
5816 // (-5.3420409f +
5817 // (3.2865683f +
5818 // (-1.2669343f +
5819 // (0.27515199f -
5820 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5821 //
5822 // error 0.0000018516, which is better than 18 bits
5823 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5824 getF32Constant(DAG, 0xbcd2769e, dl));
5825 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5826 getF32Constant(DAG, 0x3e8ce0b9, dl));
5827 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5828 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5829 getF32Constant(DAG, 0x3fa22ae7, dl));
5830 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5831 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5832 getF32Constant(DAG, 0x40525723, dl));
5833 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5834 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5835 getF32Constant(DAG, 0x40aaf200, dl));
5836 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5837 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5838 getF32Constant(DAG, 0x40c39dad, dl));
5839 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5840 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5841 getF32Constant(DAG, 0x4042902c, dl));
5842 }
5843
5844 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5845 }
5846
5847 // No special expansion.
5848 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5849}
5850
5851/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5852/// limited-precision mode.
5854 const TargetLowering &TLI, SDNodeFlags Flags) {
5855 // TODO: What fast-math-flags should be set on the floating-point nodes?
5856
5857 if (Op.getValueType() == MVT::f32 &&
5859 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5860
5861 // Scale the exponent by log10(2) [0.30102999f].
5862 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5863 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5864 getF32Constant(DAG, 0x3e9a209a, dl));
5865
5866 // Get the significand and build it into a floating-point number with
5867 // exponent of 1.
5868 SDValue X = GetSignificand(DAG, Op1, dl);
5869
5870 SDValue Log10ofMantissa;
5871 if (LimitFloatPrecision <= 6) {
5872 // For floating-point precision of 6:
5873 //
5874 // Log10ofMantissa =
5875 // -0.50419619f +
5876 // (0.60948995f - 0.10380950f * x) * x;
5877 //
5878 // error 0.0014886165, which is 6 bits
5879 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5880 getF32Constant(DAG, 0xbdd49a13, dl));
5881 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5882 getF32Constant(DAG, 0x3f1c0789, dl));
5883 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5884 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5885 getF32Constant(DAG, 0x3f011300, dl));
5886 } else if (LimitFloatPrecision <= 12) {
5887 // For floating-point precision of 12:
5888 //
5889 // Log10ofMantissa =
5890 // -0.64831180f +
5891 // (0.91751397f +
5892 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5893 //
5894 // error 0.00019228036, which is better than 12 bits
5895 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5896 getF32Constant(DAG, 0x3d431f31, dl));
5897 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5898 getF32Constant(DAG, 0x3ea21fb2, dl));
5899 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5900 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5901 getF32Constant(DAG, 0x3f6ae232, dl));
5902 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5903 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5904 getF32Constant(DAG, 0x3f25f7c3, dl));
5905 } else { // LimitFloatPrecision <= 18
5906 // For floating-point precision of 18:
5907 //
5908 // Log10ofMantissa =
5909 // -0.84299375f +
5910 // (1.5327582f +
5911 // (-1.0688956f +
5912 // (0.49102474f +
5913 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5914 //
5915 // error 0.0000037995730, which is better than 18 bits
5916 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5917 getF32Constant(DAG, 0x3c5d51ce, dl));
5918 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5919 getF32Constant(DAG, 0x3e00685a, dl));
5920 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5921 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5922 getF32Constant(DAG, 0x3efb6798, dl));
5923 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5924 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5925 getF32Constant(DAG, 0x3f88d192, dl));
5926 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5927 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5928 getF32Constant(DAG, 0x3fc4316c, dl));
5929 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5930 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5931 getF32Constant(DAG, 0x3f57ce70, dl));
5932 }
5933
5934 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5935 }
5936
5937 // No special expansion.
5938 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5939}
5940
5941/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5942/// limited-precision mode.
5944 const TargetLowering &TLI, SDNodeFlags Flags) {
5945 if (Op.getValueType() == MVT::f32 &&
5947 return getLimitedPrecisionExp2(Op, dl, DAG);
5948
5949 // No special expansion.
5950 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
5951}
5952
5953/// visitPow - Lower a pow intrinsic. Handles the special sequences for
5954/// limited-precision mode with x == 10.0f.
5956 SelectionDAG &DAG, const TargetLowering &TLI,
5957 SDNodeFlags Flags) {
5958 bool IsExp10 = false;
5959 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
5962 APFloat Ten(10.0f);
5963 IsExp10 = LHSC->isExactlyValue(Ten);
5964 }
5965 }
5966
5967 // TODO: What fast-math-flags should be set on the FMUL node?
5968 if (IsExp10) {
5969 // Put the exponent in the right bit position for later addition to the
5970 // final result:
5971 //
5972 // #define LOG2OF10 3.3219281f
5973 // t0 = Op * LOG2OF10;
5974 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
5975 getF32Constant(DAG, 0x40549a78, dl));
5976 return getLimitedPrecisionExp2(t0, dl, DAG);
5977 }
5978
5979 // No special expansion.
5980 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
5981}
5982
5983/// ExpandPowI - Expand a llvm.powi intrinsic.
5985 SelectionDAG &DAG) {
5986 // If RHS is a constant, we can expand this out to a multiplication tree if
5987 // it's beneficial on the target, otherwise we end up lowering to a call to
5988 // __powidf2 (for example).
5990 unsigned Val = RHSC->getSExtValue();
5991
5992 // powi(x, 0) -> 1.0
5993 if (Val == 0)
5994 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
5995
5997 Val, DAG.shouldOptForSize())) {
5998 // Get the exponent as a positive value.
5999 if ((int)Val < 0)
6000 Val = -Val;
6001 // We use the simple binary decomposition method to generate the multiply
6002 // sequence. There are more optimal ways to do this (for example,
6003 // powi(x,15) generates one more multiply than it should), but this has
6004 // the benefit of being both really simple and much better than a libcall.
6005 SDValue Res; // Logically starts equal to 1.0
6006 SDValue CurSquare = LHS;
6007 // TODO: Intrinsics should have fast-math-flags that propagate to these
6008 // nodes.
6009 while (Val) {
6010 if (Val & 1) {
6011 if (Res.getNode())
6012 Res =
6013 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
6014 else
6015 Res = CurSquare; // 1.0*CurSquare.
6016 }
6017
6018 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
6019 CurSquare, CurSquare);
6020 Val >>= 1;
6021 }
6022
6023 // If the original was negative, invert the result, producing 1/(x*x*x).
6024 if (RHSC->getSExtValue() < 0)
6025 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
6026 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
6027 return Res;
6028 }
6029 }
6030
6031 // Otherwise, expand to a libcall.
6032 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
6033}
6034
6035static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
6036 SDValue LHS, SDValue RHS, SDValue Scale,
6037 SelectionDAG &DAG, const TargetLowering &TLI) {
6038 EVT VT = LHS.getValueType();
6039 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
6040 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
6041 LLVMContext &Ctx = *DAG.getContext();
6042
6043 // If the type is legal but the operation isn't, this node might survive all
6044 // the way to operation legalization. If we end up there and we do not have
6045 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
6046 // node.
6047
6048 // Coax the legalizer into expanding the node during type legalization instead
6049 // by bumping the size by one bit. This will force it to Promote, enabling the
6050 // early expansion and avoiding the need to expand later.
6051
6052 // We don't have to do this if Scale is 0; that can always be expanded, unless
6053 // it's a saturating signed operation. Those can experience true integer
6054 // division overflow, a case which we must avoid.
6055
6056 // FIXME: We wouldn't have to do this (or any of the early
6057 // expansion/promotion) if it was possible to expand a libcall of an
6058 // illegal type during operation legalization. But it's not, so things
6059 // get a bit hacky.
6060 unsigned ScaleInt = Scale->getAsZExtVal();
6061 if ((ScaleInt > 0 || (Saturating && Signed)) &&
6062 (TLI.isTypeLegal(VT) ||
6063 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6065 Opcode, VT, ScaleInt);
6066 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6067 EVT PromVT;
6068 if (VT.isScalarInteger())
6069 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6070 else if (VT.isVector()) {
6071 PromVT = VT.getVectorElementType();
6072 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6073 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6074 } else
6075 llvm_unreachable("Wrong VT for DIVFIX?");
6076 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6077 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6078 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6079 // For saturating operations, we need to shift up the LHS to get the
6080 // proper saturation width, and then shift down again afterwards.
6081 if (Saturating)
6082 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6083 DAG.getConstant(1, DL, ShiftTy));
6084 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6085 if (Saturating)
6086 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6087 DAG.getConstant(1, DL, ShiftTy));
6088 return DAG.getZExtOrTrunc(Res, DL, VT);
6089 }
6090 }
6091
6092 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6093}
6094
6095// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6096// bitcasted, or split argument. Returns a list of <Register, size in bits>
6097static void
6098getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6099 const SDValue &N) {
6100 switch (N.getOpcode()) {
6101 case ISD::CopyFromReg: {
6102 SDValue Op = N.getOperand(1);
6103 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6104 Op.getValueType().getSizeInBits());
6105 return;
6106 }
6107 case ISD::BITCAST:
6108 case ISD::AssertZext:
6109 case ISD::AssertSext:
6110 case ISD::TRUNCATE:
6111 getUnderlyingArgRegs(Regs, N.getOperand(0));
6112 return;
6113 case ISD::BUILD_PAIR:
6114 case ISD::BUILD_VECTOR:
6116 for (SDValue Op : N->op_values())
6117 getUnderlyingArgRegs(Regs, Op);
6118 return;
6119 default:
6120 return;
6121 }
6122}
6123
6124/// If the DbgValueInst is a dbg_value of a function argument, create the
6125/// corresponding DBG_VALUE machine instruction for it now. At the end of
6126/// instruction selection, they will be inserted to the entry BB.
6127/// We don't currently support this for variadic dbg_values, as they shouldn't
6128/// appear for function arguments or in the prologue.
6129bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6130 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6131 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6132 const Argument *Arg = dyn_cast<Argument>(V);
6133 if (!Arg)
6134 return false;
6135
6136 MachineFunction &MF = DAG.getMachineFunction();
6137 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6138
6139 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6140 // we've been asked to pursue.
6141 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6142 bool Indirect) {
6143 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6144 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6145 // pointing at the VReg, which will be patched up later.
6146 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6148 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6149 /* isKill */ false, /* isDead */ false,
6150 /* isUndef */ false, /* isEarlyClobber */ false,
6151 /* SubReg */ 0, /* isDebug */ true)});
6152
6153 auto *NewDIExpr = FragExpr;
6154 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6155 // the DIExpression.
6156 if (Indirect)
6157 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6159 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6160 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6161 } else {
6162 // Create a completely standard DBG_VALUE.
6163 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6164 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6165 }
6166 };
6167
6168 if (Kind == FuncArgumentDbgValueKind::Value) {
6169 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6170 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6171 // the entry block.
6172 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6173 if (!IsInEntryBlock)
6174 return false;
6175
6176 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6177 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6178 // variable that also is a param.
6179 //
6180 // Although, if we are at the top of the entry block already, we can still
6181 // emit using ArgDbgValue. This might catch some situations when the
6182 // dbg.value refers to an argument that isn't used in the entry block, so
6183 // any CopyToReg node would be optimized out and the only way to express
6184 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6185 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6186 // we should only emit as ArgDbgValue if the Variable is an argument to the
6187 // current function, and the dbg.value intrinsic is found in the entry
6188 // block.
6189 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6190 !DL->getInlinedAt();
6191 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6192 if (!IsInPrologue && !VariableIsFunctionInputArg)
6193 return false;
6194
6195 // Here we assume that a function argument on IR level only can be used to
6196 // describe one input parameter on source level. If we for example have
6197 // source code like this
6198 //
6199 // struct A { long x, y; };
6200 // void foo(struct A a, long b) {
6201 // ...
6202 // b = a.x;
6203 // ...
6204 // }
6205 //
6206 // and IR like this
6207 //
6208 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6209 // entry:
6210 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6211 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6212 // call void @llvm.dbg.value(metadata i32 %b, "b",
6213 // ...
6214 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6215 // ...
6216 //
6217 // then the last dbg.value is describing a parameter "b" using a value that
6218 // is an argument. But since we already has used %a1 to describe a parameter
6219 // we should not handle that last dbg.value here (that would result in an
6220 // incorrect hoisting of the DBG_VALUE to the function entry).
6221 // Notice that we allow one dbg.value per IR level argument, to accommodate
6222 // for the situation with fragments above.
6223 // If there is no node for the value being handled, we return true to skip
6224 // the normal generation of debug info, as it would kill existing debug
6225 // info for the parameter in case of duplicates.
6226 if (VariableIsFunctionInputArg) {
6227 unsigned ArgNo = Arg->getArgNo();
6228 if (ArgNo >= FuncInfo.DescribedArgs.size())
6229 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6230 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6231 return !NodeMap[V].getNode();
6232 FuncInfo.DescribedArgs.set(ArgNo);
6233 }
6234 }
6235
6236 bool IsIndirect = false;
6237 std::optional<MachineOperand> Op;
6238 // Some arguments' frame index is recorded during argument lowering.
6239 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6240 if (FI != std::numeric_limits<int>::max())
6242
6244 if (!Op && N.getNode()) {
6245 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6246 Register Reg;
6247 if (ArgRegsAndSizes.size() == 1)
6248 Reg = ArgRegsAndSizes.front().first;
6249
6250 if (Reg && Reg.isVirtual()) {
6251 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6252 Register PR = RegInfo.getLiveInPhysReg(Reg);
6253 if (PR)
6254 Reg = PR;
6255 }
6256 if (Reg) {
6258 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6259 }
6260 }
6261
6262 if (!Op && N.getNode()) {
6263 // Check if frame index is available.
6264 SDValue LCandidate = peekThroughBitcasts(N);
6265 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6266 if (FrameIndexSDNode *FINode =
6267 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6268 Op = MachineOperand::CreateFI(FINode->getIndex());
6269 }
6270
6271 if (!Op) {
6272 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6273 auto splitMultiRegDbgValue = [&](ArrayRef<std::pair<Register, TypeSize>>
6274 SplitRegs) {
6275 unsigned Offset = 0;
6276 for (const auto &RegAndSize : SplitRegs) {
6277 // If the expression is already a fragment, the current register
6278 // offset+size might extend beyond the fragment. In this case, only
6279 // the register bits that are inside the fragment are relevant.
6280 int RegFragmentSizeInBits = RegAndSize.second;
6281 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6282 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6283 // The register is entirely outside the expression fragment,
6284 // so is irrelevant for debug info.
6285 if (Offset >= ExprFragmentSizeInBits)
6286 break;
6287 // The register is partially outside the expression fragment, only
6288 // the low bits within the fragment are relevant for debug info.
6289 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6290 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6291 }
6292 }
6293
6294 auto FragmentExpr = DIExpression::createFragmentExpression(
6295 Expr, Offset, RegFragmentSizeInBits);
6296 Offset += RegAndSize.second;
6297 // If a valid fragment expression cannot be created, the variable's
6298 // correct value cannot be determined and so it is set as poison.
6299 if (!FragmentExpr) {
6300 SDDbgValue *SDV = DAG.getConstantDbgValue(
6301 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6302 DAG.AddDbgValue(SDV, false);
6303 continue;
6304 }
6305 MachineInstr *NewMI =
6306 MakeVRegDbgValue(RegAndSize.first, *FragmentExpr,
6307 Kind != FuncArgumentDbgValueKind::Value);
6308 FuncInfo.ArgDbgValues.push_back(NewMI);
6309 }
6310 };
6311
6312 // Check if ValueMap has reg number.
6314 VMI = FuncInfo.ValueMap.find(V);
6315 if (VMI != FuncInfo.ValueMap.end()) {
6316 const auto &TLI = DAG.getTargetLoweringInfo();
6317 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6318 V->getType(), std::nullopt);
6319 if (RFV.occupiesMultipleRegs()) {
6320 splitMultiRegDbgValue(RFV.getRegsAndSizes());
6321 return true;
6322 }
6323
6324 Op = MachineOperand::CreateReg(VMI->second, false);
6325 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6326 } else if (ArgRegsAndSizes.size() > 1) {
6327 // This was split due to the calling convention, and no virtual register
6328 // mapping exists for the value.
6329 splitMultiRegDbgValue(ArgRegsAndSizes);
6330 return true;
6331 }
6332 }
6333
6334 if (!Op)
6335 return false;
6336
6337 assert(Variable->isValidLocationForIntrinsic(DL) &&
6338 "Expected inlined-at fields to agree");
6339 MachineInstr *NewMI = nullptr;
6340
6341 if (Op->isReg())
6342 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6343 else
6344 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6345 Variable, Expr);
6346
6347 // Otherwise, use ArgDbgValues.
6348 FuncInfo.ArgDbgValues.push_back(NewMI);
6349 return true;
6350}
6351
6352/// Return the appropriate SDDbgValue based on N.
6353SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6354 DILocalVariable *Variable,
6355 DIExpression *Expr,
6356 const DebugLoc &dl,
6357 unsigned DbgSDNodeOrder) {
6358 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6359 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6360 // stack slot locations.
6361 //
6362 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6363 // debug values here after optimization:
6364 //
6365 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6366 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6367 //
6368 // Both describe the direct values of their associated variables.
6369 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6370 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6371 }
6372 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6373 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6374}
6375
6376static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6377 switch (Intrinsic) {
6378 case Intrinsic::smul_fix:
6379 return ISD::SMULFIX;
6380 case Intrinsic::umul_fix:
6381 return ISD::UMULFIX;
6382 case Intrinsic::smul_fix_sat:
6383 return ISD::SMULFIXSAT;
6384 case Intrinsic::umul_fix_sat:
6385 return ISD::UMULFIXSAT;
6386 case Intrinsic::sdiv_fix:
6387 return ISD::SDIVFIX;
6388 case Intrinsic::udiv_fix:
6389 return ISD::UDIVFIX;
6390 case Intrinsic::sdiv_fix_sat:
6391 return ISD::SDIVFIXSAT;
6392 case Intrinsic::udiv_fix_sat:
6393 return ISD::UDIVFIXSAT;
6394 default:
6395 llvm_unreachable("Unhandled fixed point intrinsic");
6396 }
6397}
6398
6399/// Given a @llvm.call.preallocated.setup, return the corresponding
6400/// preallocated call.
6401static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6402 assert(cast<CallBase>(PreallocatedSetup)
6404 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6405 "expected call_preallocated_setup Value");
6406 for (const auto *U : PreallocatedSetup->users()) {
6407 auto *UseCall = cast<CallBase>(U);
6408 const Function *Fn = UseCall->getCalledFunction();
6409 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6410 return UseCall;
6411 }
6412 }
6413 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6414}
6415
6416/// If DI is a debug value with an EntryValue expression, lower it using the
6417/// corresponding physical register of the associated Argument value
6418/// (guaranteed to exist by the verifier).
6419bool SelectionDAGBuilder::visitEntryValueDbgValue(
6420 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6421 DIExpression *Expr, DebugLoc DbgLoc) {
6422 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6423 return false;
6424
6425 // These properties are guaranteed by the verifier.
6426 const Argument *Arg = cast<Argument>(Values[0]);
6427 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6428
6429 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6430 if (ArgIt == FuncInfo.ValueMap.end()) {
6431 LLVM_DEBUG(
6432 dbgs() << "Dropping dbg.value: expression is entry_value but "
6433 "couldn't find an associated register for the Argument\n");
6434 return true;
6435 }
6436 Register ArgVReg = ArgIt->getSecond();
6437
6438 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6439 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6440 SDDbgValue *SDV = DAG.getVRegDbgValue(
6441 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6442 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6443 return true;
6444 }
6445 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6446 "couldn't find a physical register\n");
6447 return true;
6448}
6449
6450/// Lower the call to the specified intrinsic function.
6451void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6452 unsigned Intrinsic) {
6453 SDLoc sdl = getCurSDLoc();
6454 switch (Intrinsic) {
6455 case Intrinsic::experimental_convergence_anchor:
6456 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6457 break;
6458 case Intrinsic::experimental_convergence_entry:
6459 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6460 break;
6461 case Intrinsic::experimental_convergence_loop: {
6462 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6463 auto *Token = Bundle->Inputs[0].get();
6464 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6465 getValue(Token)));
6466 break;
6467 }
6468 }
6469}
6470
6471void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6472 unsigned IntrinsicID) {
6473 // For now, we're only lowering an 'add' histogram.
6474 // We can add others later, e.g. saturating adds, min/max.
6475 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6476 "Tried to lower unsupported histogram type");
6477 SDLoc sdl = getCurSDLoc();
6478 Value *Ptr = I.getOperand(0);
6479 SDValue Inc = getValue(I.getOperand(1));
6480 SDValue Mask = getValue(I.getOperand(2));
6481
6482 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6483 DataLayout TargetDL = DAG.getDataLayout();
6484 EVT VT = Inc.getValueType();
6485 Align Alignment = DAG.getEVTAlign(VT);
6486
6487 const MDNode *Ranges = getRangeMetadata(I);
6488
6489 SDValue Root = DAG.getRoot();
6490 SDValue Base;
6491 SDValue Index;
6492 SDValue Scale;
6493 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6494 I.getParent(), VT.getScalarStoreSize());
6495
6496 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6497
6498 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6499 MachinePointerInfo(AS),
6501 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6502
6503 if (!UniformBase) {
6504 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6505 Index = getValue(Ptr);
6506 Scale =
6507 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6508 }
6509
6510 EVT IdxVT = Index.getValueType();
6511 EVT EltTy = IdxVT.getVectorElementType();
6512 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6513 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
6514 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6515 }
6516
6517 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6518
6519 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6520 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6521 Ops, MMO, ISD::SIGNED_SCALED);
6522
6523 setValue(&I, Histogram);
6524 DAG.setRoot(Histogram);
6525}
6526
6527void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6528 unsigned Intrinsic) {
6529 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6530 "Tried lowering invalid vector extract last");
6531 SDLoc sdl = getCurSDLoc();
6532 const DataLayout &Layout = DAG.getDataLayout();
6533 SDValue Data = getValue(I.getOperand(0));
6534 SDValue Mask = getValue(I.getOperand(1));
6535
6536 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6537 EVT ResVT = TLI.getValueType(Layout, I.getType());
6538
6539 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6540 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6541 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6542
6543 Value *Default = I.getOperand(2);
6545 SDValue PassThru = getValue(Default);
6546 EVT BoolVT = Mask.getValueType().getScalarType();
6547 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6548 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6549 }
6550
6551 setValue(&I, Result);
6552}
6553
6554/// Lower the call to the specified intrinsic function.
6555void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6556 unsigned Intrinsic) {
6557 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6558 SDLoc sdl = getCurSDLoc();
6559 DebugLoc dl = getCurDebugLoc();
6560 SDValue Res;
6561
6562 SDNodeFlags Flags;
6563 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6564 Flags.copyFMF(*FPOp);
6565
6566 switch (Intrinsic) {
6567 default:
6568 // By default, turn this into a target intrinsic node.
6569 visitTargetIntrinsic(I, Intrinsic);
6570 return;
6571 case Intrinsic::vscale: {
6572 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6573 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6574 return;
6575 }
6576 case Intrinsic::vastart: visitVAStart(I); return;
6577 case Intrinsic::vaend: visitVAEnd(I); return;
6578 case Intrinsic::vacopy: visitVACopy(I); return;
6579 case Intrinsic::returnaddress:
6580 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6581 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6582 getValue(I.getArgOperand(0))));
6583 return;
6584 case Intrinsic::addressofreturnaddress:
6585 setValue(&I,
6586 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6587 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6588 return;
6589 case Intrinsic::sponentry:
6590 setValue(&I,
6591 DAG.getNode(ISD::SPONENTRY, sdl,
6592 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6593 return;
6594 case Intrinsic::frameaddress:
6595 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6596 TLI.getFrameIndexTy(DAG.getDataLayout()),
6597 getValue(I.getArgOperand(0))));
6598 return;
6599 case Intrinsic::read_volatile_register:
6600 case Intrinsic::read_register: {
6601 Value *Reg = I.getArgOperand(0);
6602 SDValue Chain = getRoot();
6604 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6605 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6606 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6607 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6608 setValue(&I, Res);
6609 DAG.setRoot(Res.getValue(1));
6610 return;
6611 }
6612 case Intrinsic::write_register: {
6613 Value *Reg = I.getArgOperand(0);
6614 Value *RegValue = I.getArgOperand(1);
6615 SDValue Chain = getRoot();
6617 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6618 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6619 RegName, getValue(RegValue)));
6620 return;
6621 }
6622 case Intrinsic::memcpy:
6623 case Intrinsic::memcpy_inline: {
6624 const auto &MCI = cast<MemCpyInst>(I);
6625 SDValue Dst = getValue(I.getArgOperand(0));
6626 SDValue Src = getValue(I.getArgOperand(1));
6627 SDValue Size = getValue(I.getArgOperand(2));
6628 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6629 "memcpy_inline needs constant size");
6630 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6631 Align DstAlign = MCI.getDestAlign().valueOrOne();
6632 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6633 Align Alignment = std::min(DstAlign, SrcAlign);
6634 bool isVol = MCI.isVolatile();
6635 // FIXME: Support passing different dest/src alignments to the memcpy DAG
6636 // node.
6637 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6638 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol,
6639 MCI.isForceInlined(), &I, std::nullopt,
6640 MachinePointerInfo(I.getArgOperand(0)),
6641 MachinePointerInfo(I.getArgOperand(1)),
6642 I.getAAMetadata(), BatchAA);
6643 updateDAGForMaybeTailCall(MC);
6644 return;
6645 }
6646 case Intrinsic::memset:
6647 case Intrinsic::memset_inline: {
6648 const auto &MSII = cast<MemSetInst>(I);
6649 SDValue Dst = getValue(I.getArgOperand(0));
6650 SDValue Value = getValue(I.getArgOperand(1));
6651 SDValue Size = getValue(I.getArgOperand(2));
6652 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6653 "memset_inline needs constant size");
6654 // @llvm.memset defines 0 and 1 to both mean no alignment.
6655 Align DstAlign = MSII.getDestAlign().valueOrOne();
6656 bool isVol = MSII.isVolatile();
6657 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6658 SDValue MC = DAG.getMemset(
6659 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6660 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6661 updateDAGForMaybeTailCall(MC);
6662 return;
6663 }
6664 case Intrinsic::memmove: {
6665 const auto &MMI = cast<MemMoveInst>(I);
6666 SDValue Op1 = getValue(I.getArgOperand(0));
6667 SDValue Op2 = getValue(I.getArgOperand(1));
6668 SDValue Op3 = getValue(I.getArgOperand(2));
6669 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6670 Align DstAlign = MMI.getDestAlign().valueOrOne();
6671 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6672 Align Alignment = std::min(DstAlign, SrcAlign);
6673 bool isVol = MMI.isVolatile();
6674 // FIXME: Support passing different dest/src alignments to the memmove DAG
6675 // node.
6676 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6677 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, &I,
6678 /* OverrideTailCall */ std::nullopt,
6679 MachinePointerInfo(I.getArgOperand(0)),
6680 MachinePointerInfo(I.getArgOperand(1)),
6681 I.getAAMetadata(), BatchAA);
6682 updateDAGForMaybeTailCall(MM);
6683 return;
6684 }
6685 case Intrinsic::memcpy_element_unordered_atomic: {
6686 auto &MI = cast<AnyMemCpyInst>(I);
6687 SDValue Dst = getValue(MI.getRawDest());
6688 SDValue Src = getValue(MI.getRawSource());
6689 SDValue Length = getValue(MI.getLength());
6690
6691 Type *LengthTy = MI.getLength()->getType();
6692 unsigned ElemSz = MI.getElementSizeInBytes();
6693 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6694 SDValue MC =
6695 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6696 isTC, MachinePointerInfo(MI.getRawDest()),
6697 MachinePointerInfo(MI.getRawSource()));
6698 updateDAGForMaybeTailCall(MC);
6699 return;
6700 }
6701 case Intrinsic::memmove_element_unordered_atomic: {
6702 auto &MI = cast<AnyMemMoveInst>(I);
6703 SDValue Dst = getValue(MI.getRawDest());
6704 SDValue Src = getValue(MI.getRawSource());
6705 SDValue Length = getValue(MI.getLength());
6706
6707 Type *LengthTy = MI.getLength()->getType();
6708 unsigned ElemSz = MI.getElementSizeInBytes();
6709 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6710 SDValue MC =
6711 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6712 isTC, MachinePointerInfo(MI.getRawDest()),
6713 MachinePointerInfo(MI.getRawSource()));
6714 updateDAGForMaybeTailCall(MC);
6715 return;
6716 }
6717 case Intrinsic::memset_element_unordered_atomic: {
6718 auto &MI = cast<AnyMemSetInst>(I);
6719 SDValue Dst = getValue(MI.getRawDest());
6720 SDValue Val = getValue(MI.getValue());
6721 SDValue Length = getValue(MI.getLength());
6722
6723 Type *LengthTy = MI.getLength()->getType();
6724 unsigned ElemSz = MI.getElementSizeInBytes();
6725 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6726 SDValue MC =
6727 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6728 isTC, MachinePointerInfo(MI.getRawDest()));
6729 updateDAGForMaybeTailCall(MC);
6730 return;
6731 }
6732 case Intrinsic::call_preallocated_setup: {
6733 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6734 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6735 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6736 getRoot(), SrcValue);
6737 setValue(&I, Res);
6738 DAG.setRoot(Res);
6739 return;
6740 }
6741 case Intrinsic::call_preallocated_arg: {
6742 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6743 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6744 SDValue Ops[3];
6745 Ops[0] = getRoot();
6746 Ops[1] = SrcValue;
6747 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6748 MVT::i32); // arg index
6749 SDValue Res = DAG.getNode(
6750 ISD::PREALLOCATED_ARG, sdl,
6751 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6752 setValue(&I, Res);
6753 DAG.setRoot(Res.getValue(1));
6754 return;
6755 }
6756
6757 case Intrinsic::eh_typeid_for: {
6758 // Find the type id for the given typeinfo.
6759 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6760 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6761 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6762 setValue(&I, Res);
6763 return;
6764 }
6765
6766 case Intrinsic::eh_return_i32:
6767 case Intrinsic::eh_return_i64:
6768 DAG.getMachineFunction().setCallsEHReturn(true);
6769 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6770 MVT::Other,
6772 getValue(I.getArgOperand(0)),
6773 getValue(I.getArgOperand(1))));
6774 return;
6775 case Intrinsic::eh_unwind_init:
6776 DAG.getMachineFunction().setCallsUnwindInit(true);
6777 return;
6778 case Intrinsic::eh_dwarf_cfa:
6779 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6780 TLI.getPointerTy(DAG.getDataLayout()),
6781 getValue(I.getArgOperand(0))));
6782 return;
6783 case Intrinsic::eh_sjlj_callsite: {
6784 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6785 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6786
6787 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6788 return;
6789 }
6790 case Intrinsic::eh_sjlj_functioncontext: {
6791 // Get and store the index of the function context.
6792 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6793 AllocaInst *FnCtx =
6794 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6795 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6797 return;
6798 }
6799 case Intrinsic::eh_sjlj_setjmp: {
6800 SDValue Ops[2];
6801 Ops[0] = getRoot();
6802 Ops[1] = getValue(I.getArgOperand(0));
6803 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6804 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6805 setValue(&I, Op.getValue(0));
6806 DAG.setRoot(Op.getValue(1));
6807 return;
6808 }
6809 case Intrinsic::eh_sjlj_longjmp:
6810 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6811 getRoot(), getValue(I.getArgOperand(0))));
6812 return;
6813 case Intrinsic::eh_sjlj_setup_dispatch:
6814 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6815 getRoot()));
6816 return;
6817 case Intrinsic::masked_gather:
6818 visitMaskedGather(I);
6819 return;
6820 case Intrinsic::masked_load:
6821 visitMaskedLoad(I);
6822 return;
6823 case Intrinsic::masked_scatter:
6824 visitMaskedScatter(I);
6825 return;
6826 case Intrinsic::masked_store:
6827 visitMaskedStore(I);
6828 return;
6829 case Intrinsic::masked_expandload:
6830 visitMaskedLoad(I, true /* IsExpanding */);
6831 return;
6832 case Intrinsic::masked_compressstore:
6833 visitMaskedStore(I, true /* IsCompressing */);
6834 return;
6835 case Intrinsic::powi:
6836 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6837 getValue(I.getArgOperand(1)), DAG));
6838 return;
6839 case Intrinsic::log:
6840 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6841 return;
6842 case Intrinsic::log2:
6843 setValue(&I,
6844 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6845 return;
6846 case Intrinsic::log10:
6847 setValue(&I,
6848 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6849 return;
6850 case Intrinsic::exp:
6851 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6852 return;
6853 case Intrinsic::exp2:
6854 setValue(&I,
6855 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6856 return;
6857 case Intrinsic::pow:
6858 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6859 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6860 return;
6861 case Intrinsic::sqrt:
6862 case Intrinsic::fabs:
6863 case Intrinsic::sin:
6864 case Intrinsic::cos:
6865 case Intrinsic::tan:
6866 case Intrinsic::asin:
6867 case Intrinsic::acos:
6868 case Intrinsic::atan:
6869 case Intrinsic::sinh:
6870 case Intrinsic::cosh:
6871 case Intrinsic::tanh:
6872 case Intrinsic::exp10:
6873 case Intrinsic::floor:
6874 case Intrinsic::ceil:
6875 case Intrinsic::trunc:
6876 case Intrinsic::rint:
6877 case Intrinsic::nearbyint:
6878 case Intrinsic::round:
6879 case Intrinsic::roundeven:
6880 case Intrinsic::canonicalize: {
6881 unsigned Opcode;
6882 // clang-format off
6883 switch (Intrinsic) {
6884 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6885 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6886 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6887 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6888 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6889 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6890 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6891 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6892 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6893 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6894 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6895 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6896 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6897 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6898 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6899 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6900 case Intrinsic::rint: Opcode = ISD::FRINT; break;
6901 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6902 case Intrinsic::round: Opcode = ISD::FROUND; break;
6903 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6904 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6905 }
6906 // clang-format on
6907
6908 setValue(&I, DAG.getNode(Opcode, sdl,
6909 getValue(I.getArgOperand(0)).getValueType(),
6910 getValue(I.getArgOperand(0)), Flags));
6911 return;
6912 }
6913 case Intrinsic::atan2:
6914 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
6915 getValue(I.getArgOperand(0)).getValueType(),
6916 getValue(I.getArgOperand(0)),
6917 getValue(I.getArgOperand(1)), Flags));
6918 return;
6919 case Intrinsic::lround:
6920 case Intrinsic::llround:
6921 case Intrinsic::lrint:
6922 case Intrinsic::llrint: {
6923 unsigned Opcode;
6924 // clang-format off
6925 switch (Intrinsic) {
6926 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6927 case Intrinsic::lround: Opcode = ISD::LROUND; break;
6928 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6929 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
6930 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
6931 }
6932 // clang-format on
6933
6934 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6935 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6936 getValue(I.getArgOperand(0))));
6937 return;
6938 }
6939 case Intrinsic::minnum:
6940 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6941 getValue(I.getArgOperand(0)).getValueType(),
6942 getValue(I.getArgOperand(0)),
6943 getValue(I.getArgOperand(1)), Flags));
6944 return;
6945 case Intrinsic::maxnum:
6946 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6947 getValue(I.getArgOperand(0)).getValueType(),
6948 getValue(I.getArgOperand(0)),
6949 getValue(I.getArgOperand(1)), Flags));
6950 return;
6951 case Intrinsic::minimum:
6952 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
6953 getValue(I.getArgOperand(0)).getValueType(),
6954 getValue(I.getArgOperand(0)),
6955 getValue(I.getArgOperand(1)), Flags));
6956 return;
6957 case Intrinsic::maximum:
6958 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
6959 getValue(I.getArgOperand(0)).getValueType(),
6960 getValue(I.getArgOperand(0)),
6961 getValue(I.getArgOperand(1)), Flags));
6962 return;
6963 case Intrinsic::minimumnum:
6964 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
6965 getValue(I.getArgOperand(0)).getValueType(),
6966 getValue(I.getArgOperand(0)),
6967 getValue(I.getArgOperand(1)), Flags));
6968 return;
6969 case Intrinsic::maximumnum:
6970 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
6971 getValue(I.getArgOperand(0)).getValueType(),
6972 getValue(I.getArgOperand(0)),
6973 getValue(I.getArgOperand(1)), Flags));
6974 return;
6975 case Intrinsic::copysign:
6976 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
6977 getValue(I.getArgOperand(0)).getValueType(),
6978 getValue(I.getArgOperand(0)),
6979 getValue(I.getArgOperand(1)), Flags));
6980 return;
6981 case Intrinsic::ldexp:
6982 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
6983 getValue(I.getArgOperand(0)).getValueType(),
6984 getValue(I.getArgOperand(0)),
6985 getValue(I.getArgOperand(1)), Flags));
6986 return;
6987 case Intrinsic::modf:
6988 case Intrinsic::sincos:
6989 case Intrinsic::sincospi:
6990 case Intrinsic::frexp: {
6991 unsigned Opcode;
6992 switch (Intrinsic) {
6993 default:
6994 llvm_unreachable("unexpected intrinsic");
6995 case Intrinsic::sincos:
6996 Opcode = ISD::FSINCOS;
6997 break;
6998 case Intrinsic::sincospi:
6999 Opcode = ISD::FSINCOSPI;
7000 break;
7001 case Intrinsic::modf:
7002 Opcode = ISD::FMODF;
7003 break;
7004 case Intrinsic::frexp:
7005 Opcode = ISD::FFREXP;
7006 break;
7007 }
7008 SmallVector<EVT, 2> ValueVTs;
7009 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
7010 SDVTList VTs = DAG.getVTList(ValueVTs);
7011 setValue(
7012 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
7013 return;
7014 }
7015 case Intrinsic::arithmetic_fence: {
7016 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
7017 getValue(I.getArgOperand(0)).getValueType(),
7018 getValue(I.getArgOperand(0)), Flags));
7019 return;
7020 }
7021 case Intrinsic::fma:
7022 setValue(&I, DAG.getNode(
7023 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
7024 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
7025 getValue(I.getArgOperand(2)), Flags));
7026 return;
7027#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
7028 case Intrinsic::INTRINSIC:
7029#include "llvm/IR/ConstrainedOps.def"
7030 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
7031 return;
7032#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
7033#include "llvm/IR/VPIntrinsics.def"
7034 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
7035 return;
7036 case Intrinsic::fptrunc_round: {
7037 // Get the last argument, the metadata and convert it to an integer in the
7038 // call
7039 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7040 std::optional<RoundingMode> RoundMode =
7041 convertStrToRoundingMode(cast<MDString>(MD)->getString());
7042
7043 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7044
7045 // Propagate fast-math-flags from IR to node(s).
7046 SDNodeFlags Flags;
7047 Flags.copyFMF(*cast<FPMathOperator>(&I));
7048 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
7049
7051 Result = DAG.getNode(
7052 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
7053 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
7054 setValue(&I, Result);
7055
7056 return;
7057 }
7058 case Intrinsic::fmuladd: {
7059 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7060 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
7061 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
7062 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7063 getValue(I.getArgOperand(0)).getValueType(),
7064 getValue(I.getArgOperand(0)),
7065 getValue(I.getArgOperand(1)),
7066 getValue(I.getArgOperand(2)), Flags));
7067 } else if (TLI.isOperationLegalOrCustom(ISD::FMULADD, VT)) {
7068 // TODO: Support splitting the vector.
7069 setValue(&I, DAG.getNode(ISD::FMULADD, sdl,
7070 getValue(I.getArgOperand(0)).getValueType(),
7071 getValue(I.getArgOperand(0)),
7072 getValue(I.getArgOperand(1)),
7073 getValue(I.getArgOperand(2)), Flags));
7074 } else {
7075 // TODO: Intrinsic calls should have fast-math-flags.
7076 SDValue Mul = DAG.getNode(
7077 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7078 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7079 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7080 getValue(I.getArgOperand(0)).getValueType(),
7081 Mul, getValue(I.getArgOperand(2)), Flags);
7082 setValue(&I, Add);
7083 }
7084 return;
7085 }
7086 case Intrinsic::convert_to_fp16:
7087 setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16,
7088 DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16,
7089 getValue(I.getArgOperand(0)),
7090 DAG.getTargetConstant(0, sdl,
7091 MVT::i32))));
7092 return;
7093 case Intrinsic::convert_from_fp16:
7094 setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl,
7095 TLI.getValueType(DAG.getDataLayout(), I.getType()),
7096 DAG.getNode(ISD::BITCAST, sdl, MVT::f16,
7097 getValue(I.getArgOperand(0)))));
7098 return;
7099 case Intrinsic::fptosi_sat: {
7100 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7101 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7102 getValue(I.getArgOperand(0)),
7103 DAG.getValueType(VT.getScalarType())));
7104 return;
7105 }
7106 case Intrinsic::fptoui_sat: {
7107 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7108 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7109 getValue(I.getArgOperand(0)),
7110 DAG.getValueType(VT.getScalarType())));
7111 return;
7112 }
7113 case Intrinsic::set_rounding:
7114 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7115 {getRoot(), getValue(I.getArgOperand(0))});
7116 setValue(&I, Res);
7117 DAG.setRoot(Res.getValue(0));
7118 return;
7119 case Intrinsic::is_fpclass: {
7120 const DataLayout DLayout = DAG.getDataLayout();
7121 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7122 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7123 FPClassTest Test = static_cast<FPClassTest>(
7124 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7125 MachineFunction &MF = DAG.getMachineFunction();
7126 const Function &F = MF.getFunction();
7127 SDValue Op = getValue(I.getArgOperand(0));
7128 SDNodeFlags Flags;
7129 Flags.setNoFPExcept(
7130 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7131 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7132 // expansion can use illegal types. Making expansion early allows
7133 // legalizing these types prior to selection.
7134 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7135 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7136 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7137 setValue(&I, Result);
7138 return;
7139 }
7140
7141 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7142 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7143 setValue(&I, V);
7144 return;
7145 }
7146 case Intrinsic::get_fpenv: {
7147 const DataLayout DLayout = DAG.getDataLayout();
7148 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7149 Align TempAlign = DAG.getEVTAlign(EnvVT);
7150 SDValue Chain = getRoot();
7151 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7152 // and temporary storage in stack.
7153 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7154 Res = DAG.getNode(
7155 ISD::GET_FPENV, sdl,
7156 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7157 MVT::Other),
7158 Chain);
7159 } else {
7160 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7161 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7162 auto MPI =
7163 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7164 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7166 TempAlign);
7167 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7168 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7169 }
7170 setValue(&I, Res);
7171 DAG.setRoot(Res.getValue(1));
7172 return;
7173 }
7174 case Intrinsic::set_fpenv: {
7175 const DataLayout DLayout = DAG.getDataLayout();
7176 SDValue Env = getValue(I.getArgOperand(0));
7177 EVT EnvVT = Env.getValueType();
7178 Align TempAlign = DAG.getEVTAlign(EnvVT);
7179 SDValue Chain = getRoot();
7180 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7181 // environment from memory.
7182 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7183 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7184 } else {
7185 // Allocate space in stack, copy environment bits into it and use this
7186 // memory in SET_FPENV_MEM.
7187 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7188 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7189 auto MPI =
7190 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7191 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7193 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7195 TempAlign);
7196 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7197 }
7198 DAG.setRoot(Chain);
7199 return;
7200 }
7201 case Intrinsic::reset_fpenv:
7202 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7203 return;
7204 case Intrinsic::get_fpmode:
7205 Res = DAG.getNode(
7206 ISD::GET_FPMODE, sdl,
7207 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7208 MVT::Other),
7209 DAG.getRoot());
7210 setValue(&I, Res);
7211 DAG.setRoot(Res.getValue(1));
7212 return;
7213 case Intrinsic::set_fpmode:
7214 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7215 getValue(I.getArgOperand(0)));
7216 DAG.setRoot(Res);
7217 return;
7218 case Intrinsic::reset_fpmode: {
7219 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7220 DAG.setRoot(Res);
7221 return;
7222 }
7223 case Intrinsic::pcmarker: {
7224 SDValue Tmp = getValue(I.getArgOperand(0));
7225 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7226 return;
7227 }
7228 case Intrinsic::readcyclecounter: {
7229 SDValue Op = getRoot();
7230 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7231 DAG.getVTList(MVT::i64, MVT::Other), Op);
7232 setValue(&I, Res);
7233 DAG.setRoot(Res.getValue(1));
7234 return;
7235 }
7236 case Intrinsic::readsteadycounter: {
7237 SDValue Op = getRoot();
7238 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7239 DAG.getVTList(MVT::i64, MVT::Other), Op);
7240 setValue(&I, Res);
7241 DAG.setRoot(Res.getValue(1));
7242 return;
7243 }
7244 case Intrinsic::bitreverse:
7245 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7246 getValue(I.getArgOperand(0)).getValueType(),
7247 getValue(I.getArgOperand(0))));
7248 return;
7249 case Intrinsic::bswap:
7250 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7251 getValue(I.getArgOperand(0)).getValueType(),
7252 getValue(I.getArgOperand(0))));
7253 return;
7254 case Intrinsic::cttz: {
7255 SDValue Arg = getValue(I.getArgOperand(0));
7256 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7257 EVT Ty = Arg.getValueType();
7258 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
7259 sdl, Ty, Arg));
7260 return;
7261 }
7262 case Intrinsic::ctlz: {
7263 SDValue Arg = getValue(I.getArgOperand(0));
7264 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7265 EVT Ty = Arg.getValueType();
7266 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
7267 sdl, Ty, Arg));
7268 return;
7269 }
7270 case Intrinsic::ctpop: {
7271 SDValue Arg = getValue(I.getArgOperand(0));
7272 EVT Ty = Arg.getValueType();
7273 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7274 return;
7275 }
7276 case Intrinsic::fshl:
7277 case Intrinsic::fshr: {
7278 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7279 SDValue X = getValue(I.getArgOperand(0));
7280 SDValue Y = getValue(I.getArgOperand(1));
7281 SDValue Z = getValue(I.getArgOperand(2));
7282 EVT VT = X.getValueType();
7283
7284 if (X == Y) {
7285 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7286 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7287 } else {
7288 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7289 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7290 }
7291 return;
7292 }
7293 case Intrinsic::sadd_sat: {
7294 SDValue Op1 = getValue(I.getArgOperand(0));
7295 SDValue Op2 = getValue(I.getArgOperand(1));
7296 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7297 return;
7298 }
7299 case Intrinsic::uadd_sat: {
7300 SDValue Op1 = getValue(I.getArgOperand(0));
7301 SDValue Op2 = getValue(I.getArgOperand(1));
7302 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7303 return;
7304 }
7305 case Intrinsic::ssub_sat: {
7306 SDValue Op1 = getValue(I.getArgOperand(0));
7307 SDValue Op2 = getValue(I.getArgOperand(1));
7308 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7309 return;
7310 }
7311 case Intrinsic::usub_sat: {
7312 SDValue Op1 = getValue(I.getArgOperand(0));
7313 SDValue Op2 = getValue(I.getArgOperand(1));
7314 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7315 return;
7316 }
7317 case Intrinsic::sshl_sat: {
7318 SDValue Op1 = getValue(I.getArgOperand(0));
7319 SDValue Op2 = getValue(I.getArgOperand(1));
7320 setValue(&I, DAG.getNode(ISD::SSHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7321 return;
7322 }
7323 case Intrinsic::ushl_sat: {
7324 SDValue Op1 = getValue(I.getArgOperand(0));
7325 SDValue Op2 = getValue(I.getArgOperand(1));
7326 setValue(&I, DAG.getNode(ISD::USHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7327 return;
7328 }
7329 case Intrinsic::smul_fix:
7330 case Intrinsic::umul_fix:
7331 case Intrinsic::smul_fix_sat:
7332 case Intrinsic::umul_fix_sat: {
7333 SDValue Op1 = getValue(I.getArgOperand(0));
7334 SDValue Op2 = getValue(I.getArgOperand(1));
7335 SDValue Op3 = getValue(I.getArgOperand(2));
7336 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7337 Op1.getValueType(), Op1, Op2, Op3));
7338 return;
7339 }
7340 case Intrinsic::sdiv_fix:
7341 case Intrinsic::udiv_fix:
7342 case Intrinsic::sdiv_fix_sat:
7343 case Intrinsic::udiv_fix_sat: {
7344 SDValue Op1 = getValue(I.getArgOperand(0));
7345 SDValue Op2 = getValue(I.getArgOperand(1));
7346 SDValue Op3 = getValue(I.getArgOperand(2));
7348 Op1, Op2, Op3, DAG, TLI));
7349 return;
7350 }
7351 case Intrinsic::smax: {
7352 SDValue Op1 = getValue(I.getArgOperand(0));
7353 SDValue Op2 = getValue(I.getArgOperand(1));
7354 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7355 return;
7356 }
7357 case Intrinsic::smin: {
7358 SDValue Op1 = getValue(I.getArgOperand(0));
7359 SDValue Op2 = getValue(I.getArgOperand(1));
7360 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7361 return;
7362 }
7363 case Intrinsic::umax: {
7364 SDValue Op1 = getValue(I.getArgOperand(0));
7365 SDValue Op2 = getValue(I.getArgOperand(1));
7366 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7367 return;
7368 }
7369 case Intrinsic::umin: {
7370 SDValue Op1 = getValue(I.getArgOperand(0));
7371 SDValue Op2 = getValue(I.getArgOperand(1));
7372 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7373 return;
7374 }
7375 case Intrinsic::abs: {
7376 // TODO: Preserve "int min is poison" arg in SDAG?
7377 SDValue Op1 = getValue(I.getArgOperand(0));
7378 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7379 return;
7380 }
7381 case Intrinsic::scmp: {
7382 SDValue Op1 = getValue(I.getArgOperand(0));
7383 SDValue Op2 = getValue(I.getArgOperand(1));
7384 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7385 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7386 break;
7387 }
7388 case Intrinsic::ucmp: {
7389 SDValue Op1 = getValue(I.getArgOperand(0));
7390 SDValue Op2 = getValue(I.getArgOperand(1));
7391 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7392 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7393 break;
7394 }
7395 case Intrinsic::stacksave: {
7396 SDValue Op = getRoot();
7397 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7398 Res = DAG.getNode(ISD::STACKSAVE, sdl, DAG.getVTList(VT, MVT::Other), Op);
7399 setValue(&I, Res);
7400 DAG.setRoot(Res.getValue(1));
7401 return;
7402 }
7403 case Intrinsic::stackrestore:
7404 Res = getValue(I.getArgOperand(0));
7405 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7406 return;
7407 case Intrinsic::get_dynamic_area_offset: {
7408 SDValue Op = getRoot();
7409 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7410 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7411 Op);
7412 DAG.setRoot(Op);
7413 setValue(&I, Res);
7414 return;
7415 }
7416 case Intrinsic::stackguard: {
7417 MachineFunction &MF = DAG.getMachineFunction();
7418 const Module &M = *MF.getFunction().getParent();
7419 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7420 SDValue Chain = getRoot();
7421 if (TLI.useLoadStackGuardNode(M)) {
7422 Res = getLoadStackGuard(DAG, sdl, Chain);
7423 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7424 } else {
7425 const Value *Global = TLI.getSDagStackGuard(M);
7426 if (!Global) {
7427 LLVMContext &Ctx = *DAG.getContext();
7428 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
7429 setValue(&I, DAG.getPOISON(PtrTy));
7430 return;
7431 }
7432
7433 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7434 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7435 MachinePointerInfo(Global, 0), Align,
7437 }
7438 if (TLI.useStackGuardXorFP())
7439 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7440 DAG.setRoot(Chain);
7441 setValue(&I, Res);
7442 return;
7443 }
7444 case Intrinsic::stackprotector: {
7445 // Emit code into the DAG to store the stack guard onto the stack.
7446 MachineFunction &MF = DAG.getMachineFunction();
7447 MachineFrameInfo &MFI = MF.getFrameInfo();
7448 const Module &M = *MF.getFunction().getParent();
7449 SDValue Src, Chain = getRoot();
7450
7451 if (TLI.useLoadStackGuardNode(M))
7452 Src = getLoadStackGuard(DAG, sdl, Chain);
7453 else
7454 Src = getValue(I.getArgOperand(0)); // The guard's value.
7455
7456 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7457
7458 int FI = FuncInfo.StaticAllocaMap[Slot];
7459 MFI.setStackProtectorIndex(FI);
7460 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7461
7462 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7463
7464 // Store the stack protector onto the stack.
7465 Res = DAG.getStore(
7466 Chain, sdl, Src, FIN,
7467 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7468 MaybeAlign(), MachineMemOperand::MOVolatile);
7469 setValue(&I, Res);
7470 DAG.setRoot(Res);
7471 return;
7472 }
7473 case Intrinsic::objectsize:
7474 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7475
7476 case Intrinsic::is_constant:
7477 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7478
7479 case Intrinsic::annotation:
7480 case Intrinsic::ptr_annotation:
7481 case Intrinsic::launder_invariant_group:
7482 case Intrinsic::strip_invariant_group:
7483 // Drop the intrinsic, but forward the value
7484 setValue(&I, getValue(I.getOperand(0)));
7485 return;
7486
7487 case Intrinsic::type_test:
7488 case Intrinsic::public_type_test:
7489 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7490 return;
7491
7492 case Intrinsic::assume:
7493 case Intrinsic::experimental_noalias_scope_decl:
7494 case Intrinsic::var_annotation:
7495 case Intrinsic::sideeffect:
7496 // Discard annotate attributes, noalias scope declarations, assumptions, and
7497 // artificial side-effects.
7498 return;
7499
7500 case Intrinsic::codeview_annotation: {
7501 // Emit a label associated with this metadata.
7502 MachineFunction &MF = DAG.getMachineFunction();
7503 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7504 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7505 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7506 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7507 DAG.setRoot(Res);
7508 return;
7509 }
7510
7511 case Intrinsic::init_trampoline: {
7512 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7513
7514 SDValue Ops[6];
7515 Ops[0] = getRoot();
7516 Ops[1] = getValue(I.getArgOperand(0));
7517 Ops[2] = getValue(I.getArgOperand(1));
7518 Ops[3] = getValue(I.getArgOperand(2));
7519 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7520 Ops[5] = DAG.getSrcValue(F);
7521
7522 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7523
7524 DAG.setRoot(Res);
7525 return;
7526 }
7527 case Intrinsic::adjust_trampoline:
7528 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7529 TLI.getPointerTy(DAG.getDataLayout()),
7530 getValue(I.getArgOperand(0))));
7531 return;
7532 case Intrinsic::gcroot: {
7533 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7534 "only valid in functions with gc specified, enforced by Verifier");
7535 assert(GFI && "implied by previous");
7536 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7537 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7538
7539 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7540 GFI->addStackRoot(FI->getIndex(), TypeMap);
7541 return;
7542 }
7543 case Intrinsic::gcread:
7544 case Intrinsic::gcwrite:
7545 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7546 case Intrinsic::get_rounding:
7547 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7548 setValue(&I, Res);
7549 DAG.setRoot(Res.getValue(1));
7550 return;
7551
7552 case Intrinsic::expect:
7553 case Intrinsic::expect_with_probability:
7554 // Just replace __builtin_expect(exp, c) and
7555 // __builtin_expect_with_probability(exp, c, p) with EXP.
7556 setValue(&I, getValue(I.getArgOperand(0)));
7557 return;
7558
7559 case Intrinsic::ubsantrap:
7560 case Intrinsic::debugtrap:
7561 case Intrinsic::trap: {
7562 StringRef TrapFuncName =
7563 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7564 if (TrapFuncName.empty()) {
7565 switch (Intrinsic) {
7566 case Intrinsic::trap:
7567 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7568 break;
7569 case Intrinsic::debugtrap:
7570 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7571 break;
7572 case Intrinsic::ubsantrap:
7573 DAG.setRoot(DAG.getNode(
7574 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7575 DAG.getTargetConstant(
7576 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7577 MVT::i32)));
7578 break;
7579 default: llvm_unreachable("unknown trap intrinsic");
7580 }
7581 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7582 I.hasFnAttr(Attribute::NoMerge));
7583 return;
7584 }
7586 if (Intrinsic == Intrinsic::ubsantrap) {
7587 Value *Arg = I.getArgOperand(0);
7588 Args.emplace_back(Arg, getValue(Arg));
7589 }
7590
7591 TargetLowering::CallLoweringInfo CLI(DAG);
7592 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7593 CallingConv::C, I.getType(),
7594 DAG.getExternalSymbol(TrapFuncName.data(),
7595 TLI.getPointerTy(DAG.getDataLayout())),
7596 std::move(Args));
7597 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7598 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7599 DAG.setRoot(Result.second);
7600 return;
7601 }
7602
7603 case Intrinsic::allow_runtime_check:
7604 case Intrinsic::allow_ubsan_check:
7605 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7606 return;
7607
7608 case Intrinsic::uadd_with_overflow:
7609 case Intrinsic::sadd_with_overflow:
7610 case Intrinsic::usub_with_overflow:
7611 case Intrinsic::ssub_with_overflow:
7612 case Intrinsic::umul_with_overflow:
7613 case Intrinsic::smul_with_overflow: {
7615 switch (Intrinsic) {
7616 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7617 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7618 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7619 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7620 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7621 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7622 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7623 }
7624 SDValue Op1 = getValue(I.getArgOperand(0));
7625 SDValue Op2 = getValue(I.getArgOperand(1));
7626
7627 EVT ResultVT = Op1.getValueType();
7628 EVT OverflowVT = MVT::i1;
7629 if (ResultVT.isVector())
7630 OverflowVT = EVT::getVectorVT(
7631 *Context, OverflowVT, ResultVT.getVectorElementCount());
7632
7633 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7634 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7635 return;
7636 }
7637 case Intrinsic::prefetch: {
7638 SDValue Ops[5];
7639 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7641 Ops[0] = DAG.getRoot();
7642 Ops[1] = getValue(I.getArgOperand(0));
7643 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7644 MVT::i32);
7645 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7646 MVT::i32);
7647 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7648 MVT::i32);
7649 SDValue Result = DAG.getMemIntrinsicNode(
7650 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7651 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7652 /* align */ std::nullopt, Flags);
7653
7654 // Chain the prefetch in parallel with any pending loads, to stay out of
7655 // the way of later optimizations.
7656 PendingLoads.push_back(Result);
7657 Result = getRoot();
7658 DAG.setRoot(Result);
7659 return;
7660 }
7661 case Intrinsic::lifetime_start:
7662 case Intrinsic::lifetime_end: {
7663 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7664 // Stack coloring is not enabled in O0, discard region information.
7665 if (TM.getOptLevel() == CodeGenOptLevel::None)
7666 return;
7667
7668 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7669 if (!LifetimeObject)
7670 return;
7671
7672 // First check that the Alloca is static, otherwise it won't have a
7673 // valid frame index.
7674 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7675 if (SI == FuncInfo.StaticAllocaMap.end())
7676 return;
7677
7678 const int FrameIndex = SI->second;
7679 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7680 DAG.setRoot(Res);
7681 return;
7682 }
7683 case Intrinsic::pseudoprobe: {
7684 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7685 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7686 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7687 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7688 DAG.setRoot(Res);
7689 return;
7690 }
7691 case Intrinsic::invariant_start:
7692 // Discard region information.
7693 setValue(&I,
7694 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7695 return;
7696 case Intrinsic::invariant_end:
7697 // Discard region information.
7698 return;
7699 case Intrinsic::clear_cache: {
7700 SDValue InputChain = DAG.getRoot();
7701 SDValue StartVal = getValue(I.getArgOperand(0));
7702 SDValue EndVal = getValue(I.getArgOperand(1));
7703 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7704 {InputChain, StartVal, EndVal});
7705 setValue(&I, Res);
7706 DAG.setRoot(Res);
7707 return;
7708 }
7709 case Intrinsic::donothing:
7710 case Intrinsic::seh_try_begin:
7711 case Intrinsic::seh_scope_begin:
7712 case Intrinsic::seh_try_end:
7713 case Intrinsic::seh_scope_end:
7714 // ignore
7715 return;
7716 case Intrinsic::experimental_stackmap:
7717 visitStackmap(I);
7718 return;
7719 case Intrinsic::experimental_patchpoint_void:
7720 case Intrinsic::experimental_patchpoint:
7721 visitPatchpoint(I);
7722 return;
7723 case Intrinsic::experimental_gc_statepoint:
7725 return;
7726 case Intrinsic::experimental_gc_result:
7727 visitGCResult(cast<GCResultInst>(I));
7728 return;
7729 case Intrinsic::experimental_gc_relocate:
7730 visitGCRelocate(cast<GCRelocateInst>(I));
7731 return;
7732 case Intrinsic::instrprof_cover:
7733 llvm_unreachable("instrprof failed to lower a cover");
7734 case Intrinsic::instrprof_increment:
7735 llvm_unreachable("instrprof failed to lower an increment");
7736 case Intrinsic::instrprof_timestamp:
7737 llvm_unreachable("instrprof failed to lower a timestamp");
7738 case Intrinsic::instrprof_value_profile:
7739 llvm_unreachable("instrprof failed to lower a value profiling call");
7740 case Intrinsic::instrprof_mcdc_parameters:
7741 llvm_unreachable("instrprof failed to lower mcdc parameters");
7742 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7743 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7744 case Intrinsic::localescape: {
7745 MachineFunction &MF = DAG.getMachineFunction();
7746 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7747
7748 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7749 // is the same on all targets.
7750 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7751 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7752 if (isa<ConstantPointerNull>(Arg))
7753 continue; // Skip null pointers. They represent a hole in index space.
7754 AllocaInst *Slot = cast<AllocaInst>(Arg);
7755 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7756 "can only escape static allocas");
7757 int FI = FuncInfo.StaticAllocaMap[Slot];
7758 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7760 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7761 TII->get(TargetOpcode::LOCAL_ESCAPE))
7762 .addSym(FrameAllocSym)
7763 .addFrameIndex(FI);
7764 }
7765
7766 return;
7767 }
7768
7769 case Intrinsic::localrecover: {
7770 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7771 MachineFunction &MF = DAG.getMachineFunction();
7772
7773 // Get the symbol that defines the frame offset.
7774 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7775 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7776 unsigned IdxVal =
7777 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7778 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7780
7781 Value *FP = I.getArgOperand(1);
7782 SDValue FPVal = getValue(FP);
7783 EVT PtrVT = FPVal.getValueType();
7784
7785 // Create a MCSymbol for the label to avoid any target lowering
7786 // that would make this PC relative.
7787 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7788 SDValue OffsetVal =
7789 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7790
7791 // Add the offset to the FP.
7792 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7793 setValue(&I, Add);
7794
7795 return;
7796 }
7797
7798 case Intrinsic::fake_use: {
7799 Value *V = I.getArgOperand(0);
7800 SDValue Ops[2];
7801 // For Values not declared or previously used in this basic block, the
7802 // NodeMap will not have an entry, and `getValue` will assert if V has no
7803 // valid register value.
7804 auto FakeUseValue = [&]() -> SDValue {
7805 SDValue &N = NodeMap[V];
7806 if (N.getNode())
7807 return N;
7808
7809 // If there's a virtual register allocated and initialized for this
7810 // value, use it.
7811 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7812 return copyFromReg;
7813 // FIXME: Do we want to preserve constants? It seems pointless.
7814 if (isa<Constant>(V))
7815 return getValue(V);
7816 return SDValue();
7817 }();
7818 if (!FakeUseValue || FakeUseValue.isUndef())
7819 return;
7820 Ops[0] = getRoot();
7821 Ops[1] = FakeUseValue;
7822 // Also, do not translate a fake use with an undef operand, or any other
7823 // empty SDValues.
7824 if (!Ops[1] || Ops[1].isUndef())
7825 return;
7826 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7827 return;
7828 }
7829
7830 case Intrinsic::reloc_none: {
7831 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7832 StringRef SymbolName = cast<MDString>(MD)->getString();
7833 SDValue Ops[2] = {
7834 getRoot(),
7835 DAG.getTargetExternalSymbol(
7836 SymbolName.data(), TLI.getProgramPointerTy(DAG.getDataLayout()))};
7837 DAG.setRoot(DAG.getNode(ISD::RELOC_NONE, sdl, MVT::Other, Ops));
7838 return;
7839 }
7840
7841 case Intrinsic::eh_exceptionpointer:
7842 case Intrinsic::eh_exceptioncode: {
7843 // Get the exception pointer vreg, copy from it, and resize it to fit.
7844 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7845 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7846 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7847 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7848 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7849 if (Intrinsic == Intrinsic::eh_exceptioncode)
7850 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7851 setValue(&I, N);
7852 return;
7853 }
7854 case Intrinsic::xray_customevent: {
7855 // Here we want to make sure that the intrinsic behaves as if it has a
7856 // specific calling convention.
7857 const auto &Triple = DAG.getTarget().getTargetTriple();
7858 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7859 return;
7860
7862
7863 // We want to say that we always want the arguments in registers.
7864 SDValue LogEntryVal = getValue(I.getArgOperand(0));
7865 SDValue StrSizeVal = getValue(I.getArgOperand(1));
7866 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7867 SDValue Chain = getRoot();
7868 Ops.push_back(LogEntryVal);
7869 Ops.push_back(StrSizeVal);
7870 Ops.push_back(Chain);
7871
7872 // We need to enforce the calling convention for the callsite, so that
7873 // argument ordering is enforced correctly, and that register allocation can
7874 // see that some registers may be assumed clobbered and have to preserve
7875 // them across calls to the intrinsic.
7876 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7877 sdl, NodeTys, Ops);
7878 SDValue patchableNode = SDValue(MN, 0);
7879 DAG.setRoot(patchableNode);
7880 setValue(&I, patchableNode);
7881 return;
7882 }
7883 case Intrinsic::xray_typedevent: {
7884 // Here we want to make sure that the intrinsic behaves as if it has a
7885 // specific calling convention.
7886 const auto &Triple = DAG.getTarget().getTargetTriple();
7887 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7888 return;
7889
7891
7892 // We want to say that we always want the arguments in registers.
7893 // It's unclear to me how manipulating the selection DAG here forces callers
7894 // to provide arguments in registers instead of on the stack.
7895 SDValue LogTypeId = getValue(I.getArgOperand(0));
7896 SDValue LogEntryVal = getValue(I.getArgOperand(1));
7897 SDValue StrSizeVal = getValue(I.getArgOperand(2));
7898 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7899 SDValue Chain = getRoot();
7900 Ops.push_back(LogTypeId);
7901 Ops.push_back(LogEntryVal);
7902 Ops.push_back(StrSizeVal);
7903 Ops.push_back(Chain);
7904
7905 // We need to enforce the calling convention for the callsite, so that
7906 // argument ordering is enforced correctly, and that register allocation can
7907 // see that some registers may be assumed clobbered and have to preserve
7908 // them across calls to the intrinsic.
7909 MachineSDNode *MN = DAG.getMachineNode(
7910 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
7911 SDValue patchableNode = SDValue(MN, 0);
7912 DAG.setRoot(patchableNode);
7913 setValue(&I, patchableNode);
7914 return;
7915 }
7916 case Intrinsic::experimental_deoptimize:
7918 return;
7919 case Intrinsic::stepvector:
7920 visitStepVector(I);
7921 return;
7922 case Intrinsic::vector_reduce_fadd:
7923 case Intrinsic::vector_reduce_fmul:
7924 case Intrinsic::vector_reduce_add:
7925 case Intrinsic::vector_reduce_mul:
7926 case Intrinsic::vector_reduce_and:
7927 case Intrinsic::vector_reduce_or:
7928 case Intrinsic::vector_reduce_xor:
7929 case Intrinsic::vector_reduce_smax:
7930 case Intrinsic::vector_reduce_smin:
7931 case Intrinsic::vector_reduce_umax:
7932 case Intrinsic::vector_reduce_umin:
7933 case Intrinsic::vector_reduce_fmax:
7934 case Intrinsic::vector_reduce_fmin:
7935 case Intrinsic::vector_reduce_fmaximum:
7936 case Intrinsic::vector_reduce_fminimum:
7937 visitVectorReduce(I, Intrinsic);
7938 return;
7939
7940 case Intrinsic::icall_branch_funnel: {
7942 Ops.push_back(getValue(I.getArgOperand(0)));
7943
7944 int64_t Offset;
7946 I.getArgOperand(1), Offset, DAG.getDataLayout()));
7947 if (!Base)
7949 "llvm.icall.branch.funnel operand must be a GlobalValue");
7950 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
7951
7952 struct BranchFunnelTarget {
7953 int64_t Offset;
7955 };
7957
7958 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
7960 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
7961 if (ElemBase != Base)
7962 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
7963 "to the same GlobalValue");
7964
7965 SDValue Val = getValue(I.getArgOperand(Op + 1));
7966 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
7967 if (!GA)
7969 "llvm.icall.branch.funnel operand must be a GlobalValue");
7970 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
7971 GA->getGlobal(), sdl, Val.getValueType(),
7972 GA->getOffset())});
7973 }
7974 llvm::sort(Targets,
7975 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
7976 return T1.Offset < T2.Offset;
7977 });
7978
7979 for (auto &T : Targets) {
7980 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
7981 Ops.push_back(T.Target);
7982 }
7983
7984 Ops.push_back(DAG.getRoot()); // Chain
7985 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
7986 MVT::Other, Ops),
7987 0);
7988 DAG.setRoot(N);
7989 setValue(&I, N);
7990 HasTailCall = true;
7991 return;
7992 }
7993
7994 case Intrinsic::wasm_landingpad_index:
7995 // Information this intrinsic contained has been transferred to
7996 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
7997 // delete it now.
7998 return;
7999
8000 case Intrinsic::aarch64_settag:
8001 case Intrinsic::aarch64_settag_zero: {
8002 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8003 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
8005 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
8006 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
8007 ZeroMemory);
8008 DAG.setRoot(Val);
8009 setValue(&I, Val);
8010 return;
8011 }
8012 case Intrinsic::amdgcn_cs_chain: {
8013 // At this point we don't care if it's amdgpu_cs_chain or
8014 // amdgpu_cs_chain_preserve.
8016
8017 Type *RetTy = I.getType();
8018 assert(RetTy->isVoidTy() && "Should not return");
8019
8020 SDValue Callee = getValue(I.getOperand(0));
8021
8022 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
8023 // We'll also tack the value of the EXEC mask at the end.
8025 Args.reserve(3);
8026
8027 for (unsigned Idx : {2, 3, 1}) {
8028 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8029 I.getOperand(Idx)->getType());
8030 Arg.setAttributes(&I, Idx);
8031 Args.push_back(Arg);
8032 }
8033
8034 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
8035 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
8036 Args[2].IsInReg = true; // EXEC should be inreg
8037
8038 // Forward the flags and any additional arguments.
8039 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
8040 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8041 I.getOperand(Idx)->getType());
8042 Arg.setAttributes(&I, Idx);
8043 Args.push_back(Arg);
8044 }
8045
8046 TargetLowering::CallLoweringInfo CLI(DAG);
8047 CLI.setDebugLoc(getCurSDLoc())
8048 .setChain(getRoot())
8049 .setCallee(CC, RetTy, Callee, std::move(Args))
8050 .setNoReturn(true)
8051 .setTailCall(true)
8052 .setConvergent(I.isConvergent());
8053 CLI.CB = &I;
8054 std::pair<SDValue, SDValue> Result =
8055 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
8056 (void)Result;
8057 assert(!Result.first.getNode() && !Result.second.getNode() &&
8058 "Should've lowered as tail call");
8059
8060 HasTailCall = true;
8061 return;
8062 }
8063 case Intrinsic::amdgcn_call_whole_wave: {
8065 bool isTailCall = I.isTailCall();
8066
8067 // The first argument is the callee. Skip it when assembling the call args.
8068 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
8069 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
8070 I.getArgOperand(Idx)->getType());
8071 Arg.setAttributes(&I, Idx);
8072
8073 // If we have an explicit sret argument that is an Instruction, (i.e., it
8074 // might point to function-local memory), we can't meaningfully tail-call.
8075 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
8076 isTailCall = false;
8077
8078 Args.push_back(Arg);
8079 }
8080
8081 SDValue ConvControlToken;
8082 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8083 auto *Token = Bundle->Inputs[0].get();
8084 ConvControlToken = getValue(Token);
8085 }
8086
8087 TargetLowering::CallLoweringInfo CLI(DAG);
8088 CLI.setDebugLoc(getCurSDLoc())
8089 .setChain(getRoot())
8090 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8091 getValue(I.getArgOperand(0)), std::move(Args))
8092 .setTailCall(isTailCall && canTailCall(I))
8093 .setIsPreallocated(
8094 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8095 .setConvergent(I.isConvergent())
8096 .setConvergenceControlToken(ConvControlToken);
8097 CLI.CB = &I;
8098
8099 std::pair<SDValue, SDValue> Result =
8100 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8101
8102 if (Result.first.getNode())
8103 setValue(&I, Result.first);
8104 return;
8105 }
8106 case Intrinsic::ptrmask: {
8107 SDValue Ptr = getValue(I.getOperand(0));
8108 SDValue Mask = getValue(I.getOperand(1));
8109
8110 // On arm64_32, pointers are 32 bits when stored in memory, but
8111 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8112 // match the index type, but the pointer is 64 bits, so the mask must be
8113 // zero-extended up to 64 bits to match the pointer.
8114 EVT PtrVT =
8115 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8116 EVT MemVT =
8117 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8118 assert(PtrVT == Ptr.getValueType());
8119 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8120 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8121 // 128-bit, so we have to pad the mask with ones for unused bits.
8122 auto HighOnes = DAG.getNode(
8123 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8124 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8125 PtrVT, sdl));
8126 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8127 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8128 } else if (Mask.getValueType() != PtrVT)
8129 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8130
8131 assert(Mask.getValueType() == PtrVT);
8132 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8133 return;
8134 }
8135 case Intrinsic::threadlocal_address: {
8136 setValue(&I, getValue(I.getOperand(0)));
8137 return;
8138 }
8139 case Intrinsic::get_active_lane_mask: {
8140 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8141 SDValue Index = getValue(I.getOperand(0));
8142 SDValue TripCount = getValue(I.getOperand(1));
8143 EVT ElementVT = Index.getValueType();
8144
8145 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8146 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8147 TripCount));
8148 return;
8149 }
8150
8151 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8152 CCVT.getVectorElementCount());
8153
8154 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8155 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8156 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8157 SDValue VectorInduction = DAG.getNode(
8158 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8159 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8160 VectorTripCount, ISD::CondCode::SETULT);
8161 setValue(&I, SetCC);
8162 return;
8163 }
8164 case Intrinsic::experimental_get_vector_length: {
8165 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8166 "Expected positive VF");
8167 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8168 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8169
8170 SDValue Count = getValue(I.getOperand(0));
8171 EVT CountVT = Count.getValueType();
8172
8173 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8174 visitTargetIntrinsic(I, Intrinsic);
8175 return;
8176 }
8177
8178 // Expand to a umin between the trip count and the maximum elements the type
8179 // can hold.
8180 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8181
8182 // Extend the trip count to at least the result VT.
8183 if (CountVT.bitsLT(VT)) {
8184 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8185 CountVT = VT;
8186 }
8187
8188 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8189 ElementCount::get(VF, IsScalable));
8190
8191 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8192 // Clip to the result type if needed.
8193 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8194
8195 setValue(&I, Trunc);
8196 return;
8197 }
8198 case Intrinsic::vector_partial_reduce_add: {
8199 SDValue Acc = getValue(I.getOperand(0));
8200 SDValue Input = getValue(I.getOperand(1));
8201 setValue(&I,
8202 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8203 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8204 return;
8205 }
8206 case Intrinsic::vector_partial_reduce_fadd: {
8207 SDValue Acc = getValue(I.getOperand(0));
8208 SDValue Input = getValue(I.getOperand(1));
8209 setValue(&I, DAG.getNode(
8210 ISD::PARTIAL_REDUCE_FMLA, sdl, Acc.getValueType(), Acc,
8211 Input, DAG.getConstantFP(1.0, sdl, Input.getValueType())));
8212 return;
8213 }
8214 case Intrinsic::experimental_cttz_elts: {
8215 auto DL = getCurSDLoc();
8216 SDValue Op = getValue(I.getOperand(0));
8217 EVT OpVT = Op.getValueType();
8218
8219 if (!TLI.shouldExpandCttzElements(OpVT)) {
8220 visitTargetIntrinsic(I, Intrinsic);
8221 return;
8222 }
8223
8224 if (OpVT.getScalarType() != MVT::i1) {
8225 // Compare the input vector elements to zero & use to count trailing zeros
8226 SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8227 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8228 OpVT.getVectorElementCount());
8229 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8230 }
8231
8232 // If the zero-is-poison flag is set, we can assume the upper limit
8233 // of the result is VF-1.
8234 bool ZeroIsPoison =
8235 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8236 ConstantRange VScaleRange(1, true); // Dummy value.
8237 if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8238 VScaleRange = getVScaleRange(I.getCaller(), 64);
8239 unsigned EltWidth = TLI.getBitWidthForCttzElements(
8240 I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8241
8242 MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8243
8244 // Create the new vector type & get the vector length
8245 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8246 OpVT.getVectorElementCount());
8247
8248 SDValue VL =
8249 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8250
8251 SDValue StepVec = DAG.getStepVector(DL, NewVT);
8252 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8253 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8254 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8255 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8256 SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8257 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8258
8259 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8260 SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
8261
8262 setValue(&I, Ret);
8263 return;
8264 }
8265 case Intrinsic::vector_insert: {
8266 SDValue Vec = getValue(I.getOperand(0));
8267 SDValue SubVec = getValue(I.getOperand(1));
8268 SDValue Index = getValue(I.getOperand(2));
8269
8270 // The intrinsic's index type is i64, but the SDNode requires an index type
8271 // suitable for the target. Convert the index as required.
8272 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8273 if (Index.getValueType() != VectorIdxTy)
8274 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8275
8276 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8277 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8278 Index));
8279 return;
8280 }
8281 case Intrinsic::vector_extract: {
8282 SDValue Vec = getValue(I.getOperand(0));
8283 SDValue Index = getValue(I.getOperand(1));
8284 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8285
8286 // The intrinsic's index type is i64, but the SDNode requires an index type
8287 // suitable for the target. Convert the index as required.
8288 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8289 if (Index.getValueType() != VectorIdxTy)
8290 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8291
8292 setValue(&I,
8293 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8294 return;
8295 }
8296 case Intrinsic::experimental_vector_match: {
8297 SDValue Op1 = getValue(I.getOperand(0));
8298 SDValue Op2 = getValue(I.getOperand(1));
8299 SDValue Mask = getValue(I.getOperand(2));
8300 EVT Op1VT = Op1.getValueType();
8301 EVT Op2VT = Op2.getValueType();
8302 EVT ResVT = Mask.getValueType();
8303 unsigned SearchSize = Op2VT.getVectorNumElements();
8304
8305 // If the target has native support for this vector match operation, lower
8306 // the intrinsic untouched; otherwise, expand it below.
8307 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8308 visitTargetIntrinsic(I, Intrinsic);
8309 return;
8310 }
8311
8312 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8313
8314 for (unsigned i = 0; i < SearchSize; ++i) {
8315 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8316 Op2VT.getVectorElementType(), Op2,
8317 DAG.getVectorIdxConstant(i, sdl));
8318 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8319 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8320 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8321 }
8322
8323 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8324 return;
8325 }
8326 case Intrinsic::vector_reverse:
8327 visitVectorReverse(I);
8328 return;
8329 case Intrinsic::vector_splice:
8330 visitVectorSplice(I);
8331 return;
8332 case Intrinsic::callbr_landingpad:
8333 visitCallBrLandingPad(I);
8334 return;
8335 case Intrinsic::vector_interleave2:
8336 visitVectorInterleave(I, 2);
8337 return;
8338 case Intrinsic::vector_interleave3:
8339 visitVectorInterleave(I, 3);
8340 return;
8341 case Intrinsic::vector_interleave4:
8342 visitVectorInterleave(I, 4);
8343 return;
8344 case Intrinsic::vector_interleave5:
8345 visitVectorInterleave(I, 5);
8346 return;
8347 case Intrinsic::vector_interleave6:
8348 visitVectorInterleave(I, 6);
8349 return;
8350 case Intrinsic::vector_interleave7:
8351 visitVectorInterleave(I, 7);
8352 return;
8353 case Intrinsic::vector_interleave8:
8354 visitVectorInterleave(I, 8);
8355 return;
8356 case Intrinsic::vector_deinterleave2:
8357 visitVectorDeinterleave(I, 2);
8358 return;
8359 case Intrinsic::vector_deinterleave3:
8360 visitVectorDeinterleave(I, 3);
8361 return;
8362 case Intrinsic::vector_deinterleave4:
8363 visitVectorDeinterleave(I, 4);
8364 return;
8365 case Intrinsic::vector_deinterleave5:
8366 visitVectorDeinterleave(I, 5);
8367 return;
8368 case Intrinsic::vector_deinterleave6:
8369 visitVectorDeinterleave(I, 6);
8370 return;
8371 case Intrinsic::vector_deinterleave7:
8372 visitVectorDeinterleave(I, 7);
8373 return;
8374 case Intrinsic::vector_deinterleave8:
8375 visitVectorDeinterleave(I, 8);
8376 return;
8377 case Intrinsic::experimental_vector_compress:
8378 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8379 getValue(I.getArgOperand(0)).getValueType(),
8380 getValue(I.getArgOperand(0)),
8381 getValue(I.getArgOperand(1)),
8382 getValue(I.getArgOperand(2)), Flags));
8383 return;
8384 case Intrinsic::experimental_convergence_anchor:
8385 case Intrinsic::experimental_convergence_entry:
8386 case Intrinsic::experimental_convergence_loop:
8387 visitConvergenceControl(I, Intrinsic);
8388 return;
8389 case Intrinsic::experimental_vector_histogram_add: {
8390 visitVectorHistogram(I, Intrinsic);
8391 return;
8392 }
8393 case Intrinsic::experimental_vector_extract_last_active: {
8394 visitVectorExtractLastActive(I, Intrinsic);
8395 return;
8396 }
8397 case Intrinsic::loop_dependence_war_mask:
8398 setValue(&I,
8400 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8401 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8402 return;
8403 case Intrinsic::loop_dependence_raw_mask:
8404 setValue(&I,
8406 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8407 getValue(I.getOperand(1)), getValue(I.getOperand(2))));
8408 return;
8409 }
8410}
8411
8412void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
8414 assert(Result.getNode()->getNumValues() == 2);
8415 SDValue OutChain = Result.getValue(1);
8416 assert(OutChain.getValueType() == MVT::Other);
8417
8418 // Instead of updating the root immediately, push the produced chain to the
8419 // appropriate list, deferring the update until the root is requested. In this
8420 // case, the nodes from the lists are chained using TokenFactor, indicating
8421 // that the operations are independent.
8422 //
8423 // In particular, the root is updated before any call that might access the
8424 // floating-point environment, except for constrained intrinsics.
8425 switch (EB) {
8428 PendingConstrainedFP.push_back(OutChain);
8429 break;
8431 PendingConstrainedFPStrict.push_back(OutChain);
8432 break;
8433 }
8434}
8435
8436void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8437 const ConstrainedFPIntrinsic &FPI) {
8438 SDLoc sdl = getCurSDLoc();
8439
8440 // We do not need to serialize constrained FP intrinsics against
8441 // each other or against (nonvolatile) loads, so they can be
8442 // chained like loads.
8444 SDValue Chain = getFPOperationRoot(EB);
8446 Opers.push_back(Chain);
8447 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8448 Opers.push_back(getValue(FPI.getArgOperand(I)));
8449
8450 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8451 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8452 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8453
8454 SDNodeFlags Flags;
8456 Flags.setNoFPExcept(true);
8457
8458 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8459 Flags.copyFMF(*FPOp);
8460
8461 unsigned Opcode;
8462 switch (FPI.getIntrinsicID()) {
8463 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8464#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8465 case Intrinsic::INTRINSIC: \
8466 Opcode = ISD::STRICT_##DAGN; \
8467 break;
8468#include "llvm/IR/ConstrainedOps.def"
8469 case Intrinsic::experimental_constrained_fmuladd: {
8470 Opcode = ISD::STRICT_FMA;
8471 // Break fmuladd into fmul and fadd.
8472 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8473 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8474 Opers.pop_back();
8475 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8476 pushFPOpOutChain(Mul, EB);
8477 Opcode = ISD::STRICT_FADD;
8478 Opers.clear();
8479 Opers.push_back(Mul.getValue(1));
8480 Opers.push_back(Mul.getValue(0));
8481 Opers.push_back(getValue(FPI.getArgOperand(2)));
8482 }
8483 break;
8484 }
8485 }
8486
8487 // A few strict DAG nodes carry additional operands that are not
8488 // set up by the default code above.
8489 switch (Opcode) {
8490 default: break;
8492 Opers.push_back(
8493 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8494 break;
8495 case ISD::STRICT_FSETCC:
8496 case ISD::STRICT_FSETCCS: {
8498 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8499 if (TM.Options.NoNaNsFPMath)
8500 Condition = getFCmpCodeWithoutNaN(Condition);
8501 Opers.push_back(DAG.getCondCode(Condition));
8502 break;
8503 }
8504 }
8505
8506 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8507 pushFPOpOutChain(Result, EB);
8508
8509 SDValue FPResult = Result.getValue(0);
8510 setValue(&FPI, FPResult);
8511}
8512
8513static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8514 std::optional<unsigned> ResOPC;
8515 switch (VPIntrin.getIntrinsicID()) {
8516 case Intrinsic::vp_ctlz: {
8517 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8518 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8519 break;
8520 }
8521 case Intrinsic::vp_cttz: {
8522 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8523 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8524 break;
8525 }
8526 case Intrinsic::vp_cttz_elts: {
8527 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8528 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8529 break;
8530 }
8531#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8532 case Intrinsic::VPID: \
8533 ResOPC = ISD::VPSD; \
8534 break;
8535#include "llvm/IR/VPIntrinsics.def"
8536 }
8537
8538 if (!ResOPC)
8540 "Inconsistency: no SDNode available for this VPIntrinsic!");
8541
8542 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8543 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8544 if (VPIntrin.getFastMathFlags().allowReassoc())
8545 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8546 : ISD::VP_REDUCE_FMUL;
8547 }
8548
8549 return *ResOPC;
8550}
8551
8552void SelectionDAGBuilder::visitVPLoad(
8553 const VPIntrinsic &VPIntrin, EVT VT,
8554 const SmallVectorImpl<SDValue> &OpValues) {
8555 SDLoc DL = getCurSDLoc();
8556 Value *PtrOperand = VPIntrin.getArgOperand(0);
8557 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8558 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8559 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8560 SDValue LD;
8561 // Do not serialize variable-length loads of constant memory with
8562 // anything.
8563 if (!Alignment)
8564 Alignment = DAG.getEVTAlign(VT);
8565 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8566 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8567 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
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, Ranges);
8574 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8575 MMO, false /*IsExpanding */);
8576 if (AddToChain)
8577 PendingLoads.push_back(LD.getValue(1));
8578 setValue(&VPIntrin, LD);
8579}
8580
8581void SelectionDAGBuilder::visitVPLoadFF(
8582 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8583 const SmallVectorImpl<SDValue> &OpValues) {
8584 assert(OpValues.size() == 3 && "Unexpected number of operands");
8585 SDLoc DL = getCurSDLoc();
8586 Value *PtrOperand = VPIntrin.getArgOperand(0);
8587 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8588 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8589 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8590 SDValue LD;
8591 // Do not serialize variable-length loads of constant memory with
8592 // anything.
8593 if (!Alignment)
8594 Alignment = DAG.getEVTAlign(VT);
8595 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8596 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8597 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8598 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8599 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8600 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8601 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8602 MMO);
8603 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8604 if (AddToChain)
8605 PendingLoads.push_back(LD.getValue(2));
8606 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8607}
8608
8609void SelectionDAGBuilder::visitVPGather(
8610 const VPIntrinsic &VPIntrin, EVT VT,
8611 const SmallVectorImpl<SDValue> &OpValues) {
8612 SDLoc DL = getCurSDLoc();
8613 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8614 Value *PtrOperand = VPIntrin.getArgOperand(0);
8615 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8616 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8617 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8618 SDValue LD;
8619 if (!Alignment)
8620 Alignment = DAG.getEVTAlign(VT.getScalarType());
8621 unsigned AS =
8622 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8623 MachineMemOperand::Flags MMOFlags =
8624 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8625 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8626 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8627 *Alignment, AAInfo, Ranges);
8628 SDValue Base, Index, Scale;
8629 bool UniformBase =
8630 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8631 VT.getScalarStoreSize());
8632 if (!UniformBase) {
8633 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8634 Index = getValue(PtrOperand);
8635 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8636 }
8637 EVT IdxVT = Index.getValueType();
8638 EVT EltTy = IdxVT.getVectorElementType();
8639 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8640 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8641 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8642 }
8643 LD = DAG.getGatherVP(
8644 DAG.getVTList(VT, MVT::Other), VT, DL,
8645 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8647 PendingLoads.push_back(LD.getValue(1));
8648 setValue(&VPIntrin, LD);
8649}
8650
8651void SelectionDAGBuilder::visitVPStore(
8652 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8653 SDLoc DL = getCurSDLoc();
8654 Value *PtrOperand = VPIntrin.getArgOperand(1);
8655 EVT VT = OpValues[0].getValueType();
8656 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8657 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8658 SDValue ST;
8659 if (!Alignment)
8660 Alignment = DAG.getEVTAlign(VT);
8661 SDValue Ptr = OpValues[1];
8662 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8663 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8664 MachineMemOperand::Flags MMOFlags =
8665 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8666 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8667 MachinePointerInfo(PtrOperand), MMOFlags,
8668 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8669 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8670 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8671 /* IsTruncating */ false, /*IsCompressing*/ false);
8672 DAG.setRoot(ST);
8673 setValue(&VPIntrin, ST);
8674}
8675
8676void SelectionDAGBuilder::visitVPScatter(
8677 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8678 SDLoc DL = getCurSDLoc();
8679 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8680 Value *PtrOperand = VPIntrin.getArgOperand(1);
8681 EVT VT = OpValues[0].getValueType();
8682 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8683 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8684 SDValue ST;
8685 if (!Alignment)
8686 Alignment = DAG.getEVTAlign(VT.getScalarType());
8687 unsigned AS =
8688 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8689 MachineMemOperand::Flags MMOFlags =
8690 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8691 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8692 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8693 *Alignment, AAInfo);
8694 SDValue Base, Index, Scale;
8695 bool UniformBase =
8696 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8697 VT.getScalarStoreSize());
8698 if (!UniformBase) {
8699 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8700 Index = getValue(PtrOperand);
8701 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8702 }
8703 EVT IdxVT = Index.getValueType();
8704 EVT EltTy = IdxVT.getVectorElementType();
8705 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8706 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8707 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8708 }
8709 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8710 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8711 OpValues[2], OpValues[3]},
8712 MMO, ISD::SIGNED_SCALED);
8713 DAG.setRoot(ST);
8714 setValue(&VPIntrin, ST);
8715}
8716
8717void SelectionDAGBuilder::visitVPStridedLoad(
8718 const VPIntrinsic &VPIntrin, EVT VT,
8719 const SmallVectorImpl<SDValue> &OpValues) {
8720 SDLoc DL = getCurSDLoc();
8721 Value *PtrOperand = VPIntrin.getArgOperand(0);
8722 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8723 if (!Alignment)
8724 Alignment = DAG.getEVTAlign(VT.getScalarType());
8725 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8726 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8727 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8728 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8729 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8730 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8731 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8732 MachineMemOperand::Flags MMOFlags =
8733 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8734 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8735 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8736 *Alignment, AAInfo, Ranges);
8737
8738 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8739 OpValues[2], OpValues[3], MMO,
8740 false /*IsExpanding*/);
8741
8742 if (AddToChain)
8743 PendingLoads.push_back(LD.getValue(1));
8744 setValue(&VPIntrin, LD);
8745}
8746
8747void SelectionDAGBuilder::visitVPStridedStore(
8748 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8749 SDLoc DL = getCurSDLoc();
8750 Value *PtrOperand = VPIntrin.getArgOperand(1);
8751 EVT VT = OpValues[0].getValueType();
8752 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8753 if (!Alignment)
8754 Alignment = DAG.getEVTAlign(VT.getScalarType());
8755 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8756 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8757 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8758 MachineMemOperand::Flags MMOFlags =
8759 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8760 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8761 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8762 *Alignment, AAInfo);
8763
8764 SDValue ST = DAG.getStridedStoreVP(
8765 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8766 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8767 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8768 /*IsCompressing*/ false);
8769
8770 DAG.setRoot(ST);
8771 setValue(&VPIntrin, ST);
8772}
8773
8774void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8775 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8776 SDLoc DL = getCurSDLoc();
8777
8778 ISD::CondCode Condition;
8780 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8781 if (IsFP) {
8782 // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
8783 // flags, but calls that don't return floating-point types can't be
8784 // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
8785 Condition = getFCmpCondCode(CondCode);
8786 if (TM.Options.NoNaNsFPMath)
8787 Condition = getFCmpCodeWithoutNaN(Condition);
8788 } else {
8789 Condition = getICmpCondCode(CondCode);
8790 }
8791
8792 SDValue Op1 = getValue(VPIntrin.getOperand(0));
8793 SDValue Op2 = getValue(VPIntrin.getOperand(1));
8794 // #2 is the condition code
8795 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8796 SDValue EVL = getValue(VPIntrin.getOperand(4));
8797 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8798 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8799 "Unexpected target EVL type");
8800 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8801
8802 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8803 VPIntrin.getType());
8804 setValue(&VPIntrin,
8805 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8806}
8807
8808void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8809 const VPIntrinsic &VPIntrin) {
8810 SDLoc DL = getCurSDLoc();
8811 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8812
8813 auto IID = VPIntrin.getIntrinsicID();
8814
8815 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8816 return visitVPCmp(*CmpI);
8817
8818 SmallVector<EVT, 4> ValueVTs;
8819 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8820 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8821 SDVTList VTs = DAG.getVTList(ValueVTs);
8822
8823 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8824
8825 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8826 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8827 "Unexpected target EVL type");
8828
8829 // Request operands.
8830 SmallVector<SDValue, 7> OpValues;
8831 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8832 auto Op = getValue(VPIntrin.getArgOperand(I));
8833 if (I == EVLParamPos)
8834 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8835 OpValues.push_back(Op);
8836 }
8837
8838 switch (Opcode) {
8839 default: {
8840 SDNodeFlags SDFlags;
8841 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8842 SDFlags.copyFMF(*FPMO);
8843 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8844 setValue(&VPIntrin, Result);
8845 break;
8846 }
8847 case ISD::VP_LOAD:
8848 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8849 break;
8850 case ISD::VP_LOAD_FF:
8851 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8852 break;
8853 case ISD::VP_GATHER:
8854 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8855 break;
8856 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8857 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8858 break;
8859 case ISD::VP_STORE:
8860 visitVPStore(VPIntrin, OpValues);
8861 break;
8862 case ISD::VP_SCATTER:
8863 visitVPScatter(VPIntrin, OpValues);
8864 break;
8865 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8866 visitVPStridedStore(VPIntrin, OpValues);
8867 break;
8868 case ISD::VP_FMULADD: {
8869 assert(OpValues.size() == 5 && "Unexpected number of operands");
8870 SDNodeFlags SDFlags;
8871 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8872 SDFlags.copyFMF(*FPMO);
8873 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8874 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8875 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8876 } else {
8877 SDValue Mul = DAG.getNode(
8878 ISD::VP_FMUL, DL, VTs,
8879 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8880 SDValue Add =
8881 DAG.getNode(ISD::VP_FADD, DL, VTs,
8882 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8883 setValue(&VPIntrin, Add);
8884 }
8885 break;
8886 }
8887 case ISD::VP_IS_FPCLASS: {
8888 const DataLayout DLayout = DAG.getDataLayout();
8889 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8890 auto Constant = OpValues[1]->getAsZExtVal();
8891 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8892 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8893 {OpValues[0], Check, OpValues[2], OpValues[3]});
8894 setValue(&VPIntrin, V);
8895 return;
8896 }
8897 case ISD::VP_INTTOPTR: {
8898 SDValue N = OpValues[0];
8899 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8900 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8901 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8902 OpValues[2]);
8903 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8904 OpValues[2]);
8905 setValue(&VPIntrin, N);
8906 break;
8907 }
8908 case ISD::VP_PTRTOINT: {
8909 SDValue N = OpValues[0];
8910 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8911 VPIntrin.getType());
8912 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
8913 VPIntrin.getOperand(0)->getType());
8914 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8915 OpValues[2]);
8916 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8917 OpValues[2]);
8918 setValue(&VPIntrin, N);
8919 break;
8920 }
8921 case ISD::VP_ABS:
8922 case ISD::VP_CTLZ:
8923 case ISD::VP_CTLZ_ZERO_UNDEF:
8924 case ISD::VP_CTTZ:
8925 case ISD::VP_CTTZ_ZERO_UNDEF:
8926 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
8927 case ISD::VP_CTTZ_ELTS: {
8928 SDValue Result =
8929 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
8930 setValue(&VPIntrin, Result);
8931 break;
8932 }
8933 }
8934}
8935
8936SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
8937 const BasicBlock *EHPadBB,
8938 MCSymbol *&BeginLabel) {
8939 MachineFunction &MF = DAG.getMachineFunction();
8940
8941 // Insert a label before the invoke call to mark the try range. This can be
8942 // used to detect deletion of the invoke via the MachineModuleInfo.
8943 BeginLabel = MF.getContext().createTempSymbol();
8944
8945 // For SjLj, keep track of which landing pads go with which invokes
8946 // so as to maintain the ordering of pads in the LSDA.
8947 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
8948 if (CallSiteIndex) {
8949 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
8950 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
8951
8952 // Now that the call site is handled, stop tracking it.
8953 FuncInfo.setCurrentCallSite(0);
8954 }
8955
8956 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
8957}
8958
8959SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
8960 const BasicBlock *EHPadBB,
8961 MCSymbol *BeginLabel) {
8962 assert(BeginLabel && "BeginLabel should've been set");
8963
8964 MachineFunction &MF = DAG.getMachineFunction();
8965
8966 // Insert a label at the end of the invoke call to mark the try range. This
8967 // can be used to detect deletion of the invoke via the MachineModuleInfo.
8968 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
8969 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
8970
8971 // Inform MachineModuleInfo of range.
8972 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
8973 // There is a platform (e.g. wasm) that uses funclet style IR but does not
8974 // actually use outlined funclets and their LSDA info style.
8975 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
8976 assert(II && "II should've been set");
8977 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
8978 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
8979 } else if (!isScopedEHPersonality(Pers)) {
8980 assert(EHPadBB);
8981 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
8982 }
8983
8984 return Chain;
8985}
8986
8987std::pair<SDValue, SDValue>
8989 const BasicBlock *EHPadBB) {
8990 MCSymbol *BeginLabel = nullptr;
8991
8992 if (EHPadBB) {
8993 // Both PendingLoads and PendingExports must be flushed here;
8994 // this call might not return.
8995 (void)getRoot();
8996 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
8997 CLI.setChain(getRoot());
8998 }
8999
9000 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9001 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
9002
9003 assert((CLI.IsTailCall || Result.second.getNode()) &&
9004 "Non-null chain expected with non-tail call!");
9005 assert((Result.second.getNode() || !Result.first.getNode()) &&
9006 "Null value expected with tail call!");
9007
9008 if (!Result.second.getNode()) {
9009 // As a special case, a null chain means that a tail call has been emitted
9010 // and the DAG root is already updated.
9011 HasTailCall = true;
9012
9013 // Since there's no actual continuation from this block, nothing can be
9014 // relying on us setting vregs for them.
9015 PendingExports.clear();
9016 } else {
9017 DAG.setRoot(Result.second);
9018 }
9019
9020 if (EHPadBB) {
9021 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
9022 BeginLabel));
9023 Result.second = getRoot();
9024 }
9025
9026 return Result;
9027}
9028
9030 bool isMustTailCall = CB.isMustTailCall();
9031
9032 // Avoid emitting tail calls in functions with the disable-tail-calls
9033 // attribute.
9034 const Function *Caller = CB.getParent()->getParent();
9035 if (!isMustTailCall &&
9036 Caller->getFnAttribute("disable-tail-calls").getValueAsBool())
9037 return false;
9038
9039 // We can't tail call inside a function with a swifterror argument. Lowering
9040 // does not support this yet. It would have to move into the swifterror
9041 // register before the call.
9042 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
9043 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
9044 return false;
9045
9046 // Check if target-independent constraints permit a tail call here.
9047 // Target-dependent constraints are checked within TLI->LowerCallTo.
9048 return isInTailCallPosition(CB, DAG.getTarget());
9049}
9050
9052 bool isTailCall, bool isMustTailCall,
9053 const BasicBlock *EHPadBB,
9054 const TargetLowering::PtrAuthInfo *PAI) {
9055 auto &DL = DAG.getDataLayout();
9056 FunctionType *FTy = CB.getFunctionType();
9057 Type *RetTy = CB.getType();
9058
9060 Args.reserve(CB.arg_size());
9061
9062 const Value *SwiftErrorVal = nullptr;
9063 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9064
9065 if (isTailCall)
9066 isTailCall = canTailCall(CB);
9067
9068 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
9069 const Value *V = *I;
9070
9071 // Skip empty types
9072 if (V->getType()->isEmptyTy())
9073 continue;
9074
9075 SDValue ArgNode = getValue(V);
9076 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
9077 Entry.setAttributes(&CB, I - CB.arg_begin());
9078
9079 // Use swifterror virtual register as input to the call.
9080 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
9081 SwiftErrorVal = V;
9082 // We find the virtual register for the actual swifterror argument.
9083 // Instead of using the Value, we use the virtual register instead.
9084 Entry.Node =
9085 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
9086 EVT(TLI.getPointerTy(DL)));
9087 }
9088
9089 Args.push_back(Entry);
9090
9091 // If we have an explicit sret argument that is an Instruction, (i.e., it
9092 // might point to function-local memory), we can't meaningfully tail-call.
9093 if (Entry.IsSRet && isa<Instruction>(V))
9094 isTailCall = false;
9095 }
9096
9097 // If call site has a cfguardtarget operand bundle, create and add an
9098 // additional ArgListEntry.
9099 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9100 Value *V = Bundle->Inputs[0];
9102 Entry.IsCFGuardTarget = true;
9103 Args.push_back(Entry);
9104 }
9105
9106 // Disable tail calls if there is an swifterror argument. Targets have not
9107 // been updated to support tail calls.
9108 if (TLI.supportSwiftError() && SwiftErrorVal)
9109 isTailCall = false;
9110
9111 ConstantInt *CFIType = nullptr;
9112 if (CB.isIndirectCall()) {
9113 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9114 if (!TLI.supportKCFIBundles())
9116 "Target doesn't support calls with kcfi operand bundles.");
9117 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9118 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9119 }
9120 }
9121
9122 SDValue ConvControlToken;
9123 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9124 auto *Token = Bundle->Inputs[0].get();
9125 ConvControlToken = getValue(Token);
9126 }
9127
9128 GlobalValue *DeactivationSymbol = nullptr;
9130 DeactivationSymbol = cast<GlobalValue>(Bundle->Inputs[0].get());
9131 }
9132
9135 .setChain(getRoot())
9136 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9137 .setTailCall(isTailCall)
9141 .setCFIType(CFIType)
9142 .setConvergenceControlToken(ConvControlToken)
9143 .setDeactivationSymbol(DeactivationSymbol);
9144
9145 // Set the pointer authentication info if we have it.
9146 if (PAI) {
9147 if (!TLI.supportPtrAuthBundles())
9149 "This target doesn't support calls with ptrauth operand bundles.");
9150 CLI.setPtrAuth(*PAI);
9151 }
9152
9153 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9154
9155 if (Result.first.getNode()) {
9156 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9157 Result.first = lowerNoFPClassToAssertNoFPClass(DAG, CB, Result.first);
9158 setValue(&CB, Result.first);
9159 }
9160
9161 // The last element of CLI.InVals has the SDValue for swifterror return.
9162 // Here we copy it to a virtual register and update SwiftErrorMap for
9163 // book-keeping.
9164 if (SwiftErrorVal && TLI.supportSwiftError()) {
9165 // Get the last element of InVals.
9166 SDValue Src = CLI.InVals.back();
9167 Register VReg =
9168 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9169 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9170 DAG.setRoot(CopyNode);
9171 }
9172}
9173
9174static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9175 SelectionDAGBuilder &Builder) {
9176 // Check to see if this load can be trivially constant folded, e.g. if the
9177 // input is from a string literal.
9178 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9179 // Cast pointer to the type we really want to load.
9180 Type *LoadTy =
9181 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9182 if (LoadVT.isVector())
9183 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9184 if (const Constant *LoadCst =
9185 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9186 LoadTy, Builder.DAG.getDataLayout()))
9187 return Builder.getValue(LoadCst);
9188 }
9189
9190 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9191 // still constant memory, the input chain can be the entry node.
9192 SDValue Root;
9193 bool ConstantMemory = false;
9194
9195 // Do not serialize (non-volatile) loads of constant memory with anything.
9196 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9197 Root = Builder.DAG.getEntryNode();
9198 ConstantMemory = true;
9199 } else {
9200 // Do not serialize non-volatile loads against each other.
9201 Root = Builder.DAG.getRoot();
9202 }
9203
9204 SDValue Ptr = Builder.getValue(PtrVal);
9205 SDValue LoadVal =
9206 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9207 MachinePointerInfo(PtrVal), Align(1));
9208
9209 if (!ConstantMemory)
9210 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9211 return LoadVal;
9212}
9213
9214/// Record the value for an instruction that produces an integer result,
9215/// converting the type where necessary.
9216void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9217 SDValue Value,
9218 bool IsSigned) {
9219 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9220 I.getType(), true);
9221 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9222 setValue(&I, Value);
9223}
9224
9225/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9226/// true and lower it. Otherwise return false, and it will be lowered like a
9227/// normal call.
9228/// The caller already checked that \p I calls the appropriate LibFunc with a
9229/// correct prototype.
9230bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9231 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9232 const Value *Size = I.getArgOperand(2);
9233 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9234 if (CSize && CSize->getZExtValue() == 0) {
9235 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9236 I.getType(), true);
9237 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9238 return true;
9239 }
9240
9241 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9242 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9243 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9244 getValue(Size), &I);
9245 if (Res.first.getNode()) {
9246 processIntegerCallValue(I, Res.first, true);
9247 PendingLoads.push_back(Res.second);
9248 return true;
9249 }
9250
9251 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9252 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9253 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9254 return false;
9255
9256 // If the target has a fast compare for the given size, it will return a
9257 // preferred load type for that size. Require that the load VT is legal and
9258 // that the target supports unaligned loads of that type. Otherwise, return
9259 // INVALID.
9260 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9261 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9262 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9263 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9264 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9265 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9266 // TODO: Check alignment of src and dest ptrs.
9267 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9268 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9269 if (!TLI.isTypeLegal(LVT) ||
9270 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9271 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9273 }
9274
9275 return LVT;
9276 };
9277
9278 // This turns into unaligned loads. We only do this if the target natively
9279 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9280 // we'll only produce a small number of byte loads.
9281 MVT LoadVT;
9282 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9283 switch (NumBitsToCompare) {
9284 default:
9285 return false;
9286 case 16:
9287 LoadVT = MVT::i16;
9288 break;
9289 case 32:
9290 LoadVT = MVT::i32;
9291 break;
9292 case 64:
9293 case 128:
9294 case 256:
9295 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9296 break;
9297 }
9298
9299 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9300 return false;
9301
9302 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9303 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9304
9305 // Bitcast to a wide integer type if the loads are vectors.
9306 if (LoadVT.isVector()) {
9307 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9308 LoadL = DAG.getBitcast(CmpVT, LoadL);
9309 LoadR = DAG.getBitcast(CmpVT, LoadR);
9310 }
9311
9312 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9313 processIntegerCallValue(I, Cmp, false);
9314 return true;
9315}
9316
9317/// See if we can lower a memchr call into an optimized form. If so, return
9318/// true and lower it. Otherwise return false, and it will be lowered like a
9319/// normal call.
9320/// The caller already checked that \p I calls the appropriate LibFunc with a
9321/// correct prototype.
9322bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9323 const Value *Src = I.getArgOperand(0);
9324 const Value *Char = I.getArgOperand(1);
9325 const Value *Length = I.getArgOperand(2);
9326
9327 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9328 std::pair<SDValue, SDValue> Res =
9329 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9330 getValue(Src), getValue(Char), getValue(Length),
9331 MachinePointerInfo(Src));
9332 if (Res.first.getNode()) {
9333 setValue(&I, Res.first);
9334 PendingLoads.push_back(Res.second);
9335 return true;
9336 }
9337
9338 return false;
9339}
9340
9341/// See if we can lower a mempcpy call into an optimized form. If so, return
9342/// true and lower it. Otherwise return false, and it will be lowered like a
9343/// normal call.
9344/// The caller already checked that \p I calls the appropriate LibFunc with a
9345/// correct prototype.
9346bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9347 SDValue Dst = getValue(I.getArgOperand(0));
9348 SDValue Src = getValue(I.getArgOperand(1));
9349 SDValue Size = getValue(I.getArgOperand(2));
9350
9351 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9352 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9353 // DAG::getMemcpy needs Alignment to be defined.
9354 Align Alignment = std::min(DstAlign, SrcAlign);
9355
9356 SDLoc sdl = getCurSDLoc();
9357
9358 // In the mempcpy context we need to pass in a false value for isTailCall
9359 // because the return pointer needs to be adjusted by the size of
9360 // the copied memory.
9361 SDValue Root = getMemoryRoot();
9362 SDValue MC = DAG.getMemcpy(
9363 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9364 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9365 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9366 assert(MC.getNode() != nullptr &&
9367 "** memcpy should not be lowered as TailCall in mempcpy context **");
9368 DAG.setRoot(MC);
9369
9370 // Check if Size needs to be truncated or extended.
9371 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9372
9373 // Adjust return pointer to point just past the last dst byte.
9374 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9375 setValue(&I, DstPlusSize);
9376 return true;
9377}
9378
9379/// See if we can lower a strcpy call into an optimized form. If so, return
9380/// true and lower it, otherwise return false and it will be lowered like a
9381/// normal call.
9382/// The caller already checked that \p I calls the appropriate LibFunc with a
9383/// correct prototype.
9384bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9385 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9386
9387 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9388 std::pair<SDValue, SDValue> Res =
9390 getValue(Arg0), getValue(Arg1),
9391 MachinePointerInfo(Arg0),
9392 MachinePointerInfo(Arg1), isStpcpy);
9393 if (Res.first.getNode()) {
9394 setValue(&I, Res.first);
9395 DAG.setRoot(Res.second);
9396 return true;
9397 }
9398
9399 return false;
9400}
9401
9402/// See if we can lower a strcmp call into an optimized form. If so, return
9403/// true and lower it, otherwise return false and it will be lowered like a
9404/// normal call.
9405/// The caller already checked that \p I calls the appropriate LibFunc with a
9406/// correct prototype.
9407bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9408 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9409
9410 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9411 std::pair<SDValue, SDValue> Res =
9412 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
9413 getValue(Arg0), getValue(Arg1),
9414 MachinePointerInfo(Arg0),
9415 MachinePointerInfo(Arg1));
9416 if (Res.first.getNode()) {
9417 processIntegerCallValue(I, Res.first, true);
9418 PendingLoads.push_back(Res.second);
9419 return true;
9420 }
9421
9422 return false;
9423}
9424
9425/// See if we can lower a strlen call into an optimized form. If so, return
9426/// true and lower it, otherwise return false and it will be lowered like a
9427/// normal call.
9428/// The caller already checked that \p I calls the appropriate LibFunc with a
9429/// correct prototype.
9430bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9431 const Value *Arg0 = I.getArgOperand(0);
9432
9433 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9434 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrlen(
9435 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), &I);
9436 if (Res.first.getNode()) {
9437 processIntegerCallValue(I, Res.first, false);
9438 PendingLoads.push_back(Res.second);
9439 return true;
9440 }
9441
9442 return false;
9443}
9444
9445/// See if we can lower a strnlen call into an optimized form. If so, return
9446/// true and lower it, otherwise return false and it will be lowered like a
9447/// normal call.
9448/// The caller already checked that \p I calls the appropriate LibFunc with a
9449/// correct prototype.
9450bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9451 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9452
9453 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9454 std::pair<SDValue, SDValue> Res =
9455 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9456 getValue(Arg0), getValue(Arg1),
9457 MachinePointerInfo(Arg0));
9458 if (Res.first.getNode()) {
9459 processIntegerCallValue(I, Res.first, false);
9460 PendingLoads.push_back(Res.second);
9461 return true;
9462 }
9463
9464 return false;
9465}
9466
9467/// See if we can lower a unary floating-point operation into an SDNode with
9468/// the specified Opcode. If so, return true and lower it, otherwise return
9469/// false and it will be lowered like a normal call.
9470/// The caller already checked that \p I calls the appropriate LibFunc with a
9471/// correct prototype.
9472bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9473 unsigned Opcode) {
9474 // We already checked this call's prototype; verify it doesn't modify errno.
9475 // Do not perform optimizations for call sites that require strict
9476 // floating-point semantics.
9477 if (!I.onlyReadsMemory() || I.isStrictFP())
9478 return false;
9479
9480 SDNodeFlags Flags;
9481 Flags.copyFMF(cast<FPMathOperator>(I));
9482
9483 SDValue Tmp = getValue(I.getArgOperand(0));
9484 setValue(&I,
9485 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9486 return true;
9487}
9488
9489/// See if we can lower a binary floating-point operation into an SDNode with
9490/// the specified Opcode. If so, return true and lower it. Otherwise return
9491/// false, and it will be lowered like a normal call.
9492/// The caller already checked that \p I calls the appropriate LibFunc with a
9493/// correct prototype.
9494bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9495 unsigned Opcode) {
9496 // We already checked this call's prototype; verify it doesn't modify errno.
9497 // Do not perform optimizations for call sites that require strict
9498 // floating-point semantics.
9499 if (!I.onlyReadsMemory() || I.isStrictFP())
9500 return false;
9501
9502 SDNodeFlags Flags;
9503 Flags.copyFMF(cast<FPMathOperator>(I));
9504
9505 SDValue Tmp0 = getValue(I.getArgOperand(0));
9506 SDValue Tmp1 = getValue(I.getArgOperand(1));
9507 EVT VT = Tmp0.getValueType();
9508 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9509 return true;
9510}
9511
9512void SelectionDAGBuilder::visitCall(const CallInst &I) {
9513 // Handle inline assembly differently.
9514 if (I.isInlineAsm()) {
9515 visitInlineAsm(I);
9516 return;
9517 }
9518
9520
9521 if (Function *F = I.getCalledFunction()) {
9522 if (F->isDeclaration()) {
9523 // Is this an LLVM intrinsic?
9524 if (unsigned IID = F->getIntrinsicID()) {
9525 visitIntrinsicCall(I, IID);
9526 return;
9527 }
9528 }
9529
9530 // Check for well-known libc/libm calls. If the function is internal, it
9531 // can't be a library call. Don't do the check if marked as nobuiltin for
9532 // some reason.
9533 LibFunc Func;
9534 if (!I.isNoBuiltin() && !F->hasLocalLinkage() && F->hasName() &&
9535 LibInfo->getLibFunc(*F, Func) && LibInfo->hasOptimizedCodeGen(Func)) {
9536 switch (Func) {
9537 default: break;
9538 case LibFunc_bcmp:
9539 if (visitMemCmpBCmpCall(I))
9540 return;
9541 break;
9542 case LibFunc_copysign:
9543 case LibFunc_copysignf:
9544 case LibFunc_copysignl:
9545 // We already checked this call's prototype; verify it doesn't modify
9546 // errno.
9547 if (I.onlyReadsMemory()) {
9548 SDValue LHS = getValue(I.getArgOperand(0));
9549 SDValue RHS = getValue(I.getArgOperand(1));
9551 LHS.getValueType(), LHS, RHS));
9552 return;
9553 }
9554 break;
9555 case LibFunc_fabs:
9556 case LibFunc_fabsf:
9557 case LibFunc_fabsl:
9558 if (visitUnaryFloatCall(I, ISD::FABS))
9559 return;
9560 break;
9561 case LibFunc_fmin:
9562 case LibFunc_fminf:
9563 case LibFunc_fminl:
9564 if (visitBinaryFloatCall(I, ISD::FMINNUM))
9565 return;
9566 break;
9567 case LibFunc_fmax:
9568 case LibFunc_fmaxf:
9569 case LibFunc_fmaxl:
9570 if (visitBinaryFloatCall(I, ISD::FMAXNUM))
9571 return;
9572 break;
9573 case LibFunc_fminimum_num:
9574 case LibFunc_fminimum_numf:
9575 case LibFunc_fminimum_numl:
9576 if (visitBinaryFloatCall(I, ISD::FMINIMUMNUM))
9577 return;
9578 break;
9579 case LibFunc_fmaximum_num:
9580 case LibFunc_fmaximum_numf:
9581 case LibFunc_fmaximum_numl:
9582 if (visitBinaryFloatCall(I, ISD::FMAXIMUMNUM))
9583 return;
9584 break;
9585 case LibFunc_sin:
9586 case LibFunc_sinf:
9587 case LibFunc_sinl:
9588 if (visitUnaryFloatCall(I, ISD::FSIN))
9589 return;
9590 break;
9591 case LibFunc_cos:
9592 case LibFunc_cosf:
9593 case LibFunc_cosl:
9594 if (visitUnaryFloatCall(I, ISD::FCOS))
9595 return;
9596 break;
9597 case LibFunc_tan:
9598 case LibFunc_tanf:
9599 case LibFunc_tanl:
9600 if (visitUnaryFloatCall(I, ISD::FTAN))
9601 return;
9602 break;
9603 case LibFunc_asin:
9604 case LibFunc_asinf:
9605 case LibFunc_asinl:
9606 if (visitUnaryFloatCall(I, ISD::FASIN))
9607 return;
9608 break;
9609 case LibFunc_acos:
9610 case LibFunc_acosf:
9611 case LibFunc_acosl:
9612 if (visitUnaryFloatCall(I, ISD::FACOS))
9613 return;
9614 break;
9615 case LibFunc_atan:
9616 case LibFunc_atanf:
9617 case LibFunc_atanl:
9618 if (visitUnaryFloatCall(I, ISD::FATAN))
9619 return;
9620 break;
9621 case LibFunc_atan2:
9622 case LibFunc_atan2f:
9623 case LibFunc_atan2l:
9624 if (visitBinaryFloatCall(I, ISD::FATAN2))
9625 return;
9626 break;
9627 case LibFunc_sinh:
9628 case LibFunc_sinhf:
9629 case LibFunc_sinhl:
9630 if (visitUnaryFloatCall(I, ISD::FSINH))
9631 return;
9632 break;
9633 case LibFunc_cosh:
9634 case LibFunc_coshf:
9635 case LibFunc_coshl:
9636 if (visitUnaryFloatCall(I, ISD::FCOSH))
9637 return;
9638 break;
9639 case LibFunc_tanh:
9640 case LibFunc_tanhf:
9641 case LibFunc_tanhl:
9642 if (visitUnaryFloatCall(I, ISD::FTANH))
9643 return;
9644 break;
9645 case LibFunc_sqrt:
9646 case LibFunc_sqrtf:
9647 case LibFunc_sqrtl:
9648 case LibFunc_sqrt_finite:
9649 case LibFunc_sqrtf_finite:
9650 case LibFunc_sqrtl_finite:
9651 if (visitUnaryFloatCall(I, ISD::FSQRT))
9652 return;
9653 break;
9654 case LibFunc_floor:
9655 case LibFunc_floorf:
9656 case LibFunc_floorl:
9657 if (visitUnaryFloatCall(I, ISD::FFLOOR))
9658 return;
9659 break;
9660 case LibFunc_nearbyint:
9661 case LibFunc_nearbyintf:
9662 case LibFunc_nearbyintl:
9663 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
9664 return;
9665 break;
9666 case LibFunc_ceil:
9667 case LibFunc_ceilf:
9668 case LibFunc_ceill:
9669 if (visitUnaryFloatCall(I, ISD::FCEIL))
9670 return;
9671 break;
9672 case LibFunc_rint:
9673 case LibFunc_rintf:
9674 case LibFunc_rintl:
9675 if (visitUnaryFloatCall(I, ISD::FRINT))
9676 return;
9677 break;
9678 case LibFunc_round:
9679 case LibFunc_roundf:
9680 case LibFunc_roundl:
9681 if (visitUnaryFloatCall(I, ISD::FROUND))
9682 return;
9683 break;
9684 case LibFunc_trunc:
9685 case LibFunc_truncf:
9686 case LibFunc_truncl:
9687 if (visitUnaryFloatCall(I, ISD::FTRUNC))
9688 return;
9689 break;
9690 case LibFunc_log2:
9691 case LibFunc_log2f:
9692 case LibFunc_log2l:
9693 if (visitUnaryFloatCall(I, ISD::FLOG2))
9694 return;
9695 break;
9696 case LibFunc_exp2:
9697 case LibFunc_exp2f:
9698 case LibFunc_exp2l:
9699 if (visitUnaryFloatCall(I, ISD::FEXP2))
9700 return;
9701 break;
9702 case LibFunc_exp10:
9703 case LibFunc_exp10f:
9704 case LibFunc_exp10l:
9705 if (visitUnaryFloatCall(I, ISD::FEXP10))
9706 return;
9707 break;
9708 case LibFunc_ldexp:
9709 case LibFunc_ldexpf:
9710 case LibFunc_ldexpl:
9711 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9712 return;
9713 break;
9714 case LibFunc_memcmp:
9715 if (visitMemCmpBCmpCall(I))
9716 return;
9717 break;
9718 case LibFunc_mempcpy:
9719 if (visitMemPCpyCall(I))
9720 return;
9721 break;
9722 case LibFunc_memchr:
9723 if (visitMemChrCall(I))
9724 return;
9725 break;
9726 case LibFunc_strcpy:
9727 if (visitStrCpyCall(I, false))
9728 return;
9729 break;
9730 case LibFunc_stpcpy:
9731 if (visitStrCpyCall(I, true))
9732 return;
9733 break;
9734 case LibFunc_strcmp:
9735 if (visitStrCmpCall(I))
9736 return;
9737 break;
9738 case LibFunc_strlen:
9739 if (visitStrLenCall(I))
9740 return;
9741 break;
9742 case LibFunc_strnlen:
9743 if (visitStrNLenCall(I))
9744 return;
9745 break;
9746 }
9747 }
9748 }
9749
9750 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9751 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9752 return;
9753 }
9754
9755 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9756 // have to do anything here to lower funclet bundles.
9757 // CFGuardTarget bundles are lowered in LowerCallTo.
9759 I, "calls",
9764
9765 SDValue Callee = getValue(I.getCalledOperand());
9766
9767 if (I.hasDeoptState())
9768 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9769 else
9770 // Check if we can potentially perform a tail call. More detailed checking
9771 // is be done within LowerCallTo, after more information about the call is
9772 // known.
9773 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9774}
9775
9777 const CallBase &CB, const BasicBlock *EHPadBB) {
9778 auto PAB = CB.getOperandBundle("ptrauth");
9779 const Value *CalleeV = CB.getCalledOperand();
9780
9781 // Gather the call ptrauth data from the operand bundle:
9782 // [ i32 <key>, i64 <discriminator> ]
9783 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9784 const Value *Discriminator = PAB->Inputs[1];
9785
9786 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9787 assert(Discriminator->getType()->isIntegerTy(64) &&
9788 "Invalid ptrauth discriminator");
9789
9790 // Look through ptrauth constants to find the raw callee.
9791 // Do a direct unauthenticated call if we found it and everything matches.
9792 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9793 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9794 DAG.getDataLayout()))
9795 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9796 CB.isMustTailCall(), EHPadBB);
9797
9798 // Functions should never be ptrauth-called directly.
9799 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9800
9801 // Otherwise, do an authenticated indirect call.
9802 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9803 getValue(Discriminator)};
9804
9805 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9806 EHPadBB, &PAI);
9807}
9808
9809namespace {
9810
9811/// AsmOperandInfo - This contains information for each constraint that we are
9812/// lowering.
9813class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9814public:
9815 /// CallOperand - If this is the result output operand or a clobber
9816 /// this is null, otherwise it is the incoming operand to the CallInst.
9817 /// This gets modified as the asm is processed.
9818 SDValue CallOperand;
9819
9820 /// AssignedRegs - If this is a register or register class operand, this
9821 /// contains the set of register corresponding to the operand.
9822 RegsForValue AssignedRegs;
9823
9824 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9825 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9826 }
9827
9828 /// Whether or not this operand accesses memory
9829 bool hasMemory(const TargetLowering &TLI) const {
9830 // Indirect operand accesses access memory.
9831 if (isIndirect)
9832 return true;
9833
9834 for (const auto &Code : Codes)
9836 return true;
9837
9838 return false;
9839 }
9840};
9841
9842
9843} // end anonymous namespace
9844
9845/// Make sure that the output operand \p OpInfo and its corresponding input
9846/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9847/// out).
9848static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9849 SDISelAsmOperandInfo &MatchingOpInfo,
9850 SelectionDAG &DAG) {
9851 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9852 return;
9853
9855 const auto &TLI = DAG.getTargetLoweringInfo();
9856
9857 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9858 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9859 OpInfo.ConstraintVT);
9860 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9861 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9862 MatchingOpInfo.ConstraintVT);
9863 const bool OutOpIsIntOrFP =
9864 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9865 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9866 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9867 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9868 // FIXME: error out in a more elegant fashion
9869 report_fatal_error("Unsupported asm: input constraint"
9870 " with a matching output constraint of"
9871 " incompatible type!");
9872 }
9873 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9874}
9875
9876/// Get a direct memory input to behave well as an indirect operand.
9877/// This may introduce stores, hence the need for a \p Chain.
9878/// \return The (possibly updated) chain.
9879static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9880 SDISelAsmOperandInfo &OpInfo,
9881 SelectionDAG &DAG) {
9882 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9883
9884 // If we don't have an indirect input, put it in the constpool if we can,
9885 // otherwise spill it to a stack slot.
9886 // TODO: This isn't quite right. We need to handle these according to
9887 // the addressing mode that the constraint wants. Also, this may take
9888 // an additional register for the computation and we don't want that
9889 // either.
9890
9891 // If the operand is a float, integer, or vector constant, spill to a
9892 // constant pool entry to get its address.
9893 const Value *OpVal = OpInfo.CallOperandVal;
9894 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9896 OpInfo.CallOperand = DAG.getConstantPool(
9897 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9898 return Chain;
9899 }
9900
9901 // Otherwise, create a stack slot and emit a store to it before the asm.
9902 Type *Ty = OpVal->getType();
9903 auto &DL = DAG.getDataLayout();
9904 TypeSize TySize = DL.getTypeAllocSize(Ty);
9907 int StackID = 0;
9908 if (TySize.isScalable())
9909 StackID = TFI->getStackIDForScalableVectors();
9910 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9911 DL.getPrefTypeAlign(Ty), false,
9912 nullptr, StackID);
9913 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9914 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9916 TLI.getMemValueType(DL, Ty));
9917 OpInfo.CallOperand = StackSlot;
9918
9919 return Chain;
9920}
9921
9922/// GetRegistersForValue - Assign registers (virtual or physical) for the
9923/// specified operand. We prefer to assign virtual registers, to allow the
9924/// register allocator to handle the assignment process. However, if the asm
9925/// uses features that we can't model on machineinstrs, we have SDISel do the
9926/// allocation. This produces generally horrible, but correct, code.
9927///
9928/// OpInfo describes the operand
9929/// RefOpInfo describes the matching operand if any, the operand otherwise
9930static std::optional<unsigned>
9932 SDISelAsmOperandInfo &OpInfo,
9933 SDISelAsmOperandInfo &RefOpInfo) {
9934 LLVMContext &Context = *DAG.getContext();
9935 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9936
9940
9941 // No work to do for memory/address operands.
9942 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9943 OpInfo.ConstraintType == TargetLowering::C_Address)
9944 return std::nullopt;
9945
9946 // If this is a constraint for a single physreg, or a constraint for a
9947 // register class, find it.
9948 unsigned AssignedReg;
9949 const TargetRegisterClass *RC;
9950 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
9951 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
9952 // RC is unset only on failure. Return immediately.
9953 if (!RC)
9954 return std::nullopt;
9955
9956 // Get the actual register value type. This is important, because the user
9957 // may have asked for (e.g.) the AX register in i32 type. We need to
9958 // remember that AX is actually i16 to get the right extension.
9959 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
9960
9961 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
9962 // If this is an FP operand in an integer register (or visa versa), or more
9963 // generally if the operand value disagrees with the register class we plan
9964 // to stick it in, fix the operand type.
9965 //
9966 // If this is an input value, the bitcast to the new type is done now.
9967 // Bitcast for output value is done at the end of visitInlineAsm().
9968 if ((OpInfo.Type == InlineAsm::isOutput ||
9969 OpInfo.Type == InlineAsm::isInput) &&
9970 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
9971 // Try to convert to the first EVT that the reg class contains. If the
9972 // types are identical size, use a bitcast to convert (e.g. two differing
9973 // vector types). Note: output bitcast is done at the end of
9974 // visitInlineAsm().
9975 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
9976 // Exclude indirect inputs while they are unsupported because the code
9977 // to perform the load is missing and thus OpInfo.CallOperand still
9978 // refers to the input address rather than the pointed-to value.
9979 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
9980 OpInfo.CallOperand =
9981 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
9982 OpInfo.ConstraintVT = RegVT;
9983 // If the operand is an FP value and we want it in integer registers,
9984 // use the corresponding integer type. This turns an f64 value into
9985 // i64, which can be passed with two i32 values on a 32-bit machine.
9986 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
9987 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
9988 if (OpInfo.Type == InlineAsm::isInput)
9989 OpInfo.CallOperand =
9990 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
9991 OpInfo.ConstraintVT = VT;
9992 }
9993 }
9994 }
9995
9996 // No need to allocate a matching input constraint since the constraint it's
9997 // matching to has already been allocated.
9998 if (OpInfo.isMatchingInputConstraint())
9999 return std::nullopt;
10000
10001 EVT ValueVT = OpInfo.ConstraintVT;
10002 if (OpInfo.ConstraintVT == MVT::Other)
10003 ValueVT = RegVT;
10004
10005 // Initialize NumRegs.
10006 unsigned NumRegs = 1;
10007 if (OpInfo.ConstraintVT != MVT::Other)
10008 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
10009
10010 // If this is a constraint for a specific physical register, like {r17},
10011 // assign it now.
10012
10013 // If this associated to a specific register, initialize iterator to correct
10014 // place. If virtual, make sure we have enough registers
10015
10016 // Initialize iterator if necessary
10019
10020 // Do not check for single registers.
10021 if (AssignedReg) {
10022 I = std::find(I, RC->end(), AssignedReg);
10023 if (I == RC->end()) {
10024 // RC does not contain the selected register, which indicates a
10025 // mismatch between the register and the required type/bitwidth.
10026 return {AssignedReg};
10027 }
10028 }
10029
10030 for (; NumRegs; --NumRegs, ++I) {
10031 assert(I != RC->end() && "Ran out of registers to allocate!");
10032 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
10033 Regs.push_back(R);
10034 }
10035
10036 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
10037 return std::nullopt;
10038}
10039
10040static unsigned
10042 const std::vector<SDValue> &AsmNodeOperands) {
10043 // Scan until we find the definition we already emitted of this operand.
10044 unsigned CurOp = InlineAsm::Op_FirstOperand;
10045 for (; OperandNo; --OperandNo) {
10046 // Advance to the next operand.
10047 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
10048 const InlineAsm::Flag F(OpFlag);
10049 assert(
10050 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
10051 "Skipped past definitions?");
10052 CurOp += F.getNumOperandRegisters() + 1;
10053 }
10054 return CurOp;
10055}
10056
10057namespace {
10058
10059class ExtraFlags {
10060 unsigned Flags = 0;
10061
10062public:
10063 explicit ExtraFlags(const CallBase &Call) {
10064 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10065 if (IA->hasSideEffects())
10067 if (IA->isAlignStack())
10069 if (Call.isConvergent())
10071 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
10072 }
10073
10074 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
10075 // Ideally, we would only check against memory constraints. However, the
10076 // meaning of an Other constraint can be target-specific and we can't easily
10077 // reason about it. Therefore, be conservative and set MayLoad/MayStore
10078 // for Other constraints as well.
10081 if (OpInfo.Type == InlineAsm::isInput)
10083 else if (OpInfo.Type == InlineAsm::isOutput)
10085 else if (OpInfo.Type == InlineAsm::isClobber)
10087 }
10088 }
10089
10090 unsigned get() const { return Flags; }
10091};
10092
10093} // end anonymous namespace
10094
10095static bool isFunction(SDValue Op) {
10096 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
10097 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10098 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10099
10100 // In normal "call dllimport func" instruction (non-inlineasm) it force
10101 // indirect access by specifing call opcode. And usually specially print
10102 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10103 // not do in this way now. (In fact, this is similar with "Data Access"
10104 // action). So here we ignore dllimport function.
10105 if (Fn && !Fn->hasDLLImportStorageClass())
10106 return true;
10107 }
10108 }
10109 return false;
10110}
10111
10112/// visitInlineAsm - Handle a call to an InlineAsm object.
10113void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10114 const BasicBlock *EHPadBB) {
10115 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10116
10117 /// ConstraintOperands - Information about all of the constraints.
10118 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10119
10120 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10122 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10123
10124 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
10125 // AsmDialect, MayLoad, MayStore).
10126 bool HasSideEffect = IA->hasSideEffects();
10127 ExtraFlags ExtraInfo(Call);
10128
10129 for (auto &T : TargetConstraints) {
10130 ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10131 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
10132
10133 if (OpInfo.CallOperandVal)
10134 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
10135
10136 if (!HasSideEffect)
10137 HasSideEffect = OpInfo.hasMemory(TLI);
10138
10139 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10140 // FIXME: Could we compute this on OpInfo rather than T?
10141
10142 // Compute the constraint code and ConstraintType to use.
10144
10145 if (T.ConstraintType == TargetLowering::C_Immediate &&
10146 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
10147 // We've delayed emitting a diagnostic like the "n" constraint because
10148 // inlining could cause an integer showing up.
10149 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
10150 "' expects an integer constant "
10151 "expression");
10152
10153 ExtraInfo.update(T);
10154 }
10155
10156 // We won't need to flush pending loads if this asm doesn't touch
10157 // memory and is nonvolatile.
10158 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
10159
10160 bool EmitEHLabels = isa<InvokeInst>(Call);
10161 if (EmitEHLabels) {
10162 assert(EHPadBB && "InvokeInst must have an EHPadBB");
10163 }
10164 bool IsCallBr = isa<CallBrInst>(Call);
10165
10166 if (IsCallBr || EmitEHLabels) {
10167 // If this is a callbr or invoke we need to flush pending exports since
10168 // inlineasm_br and invoke are terminators.
10169 // We need to do this before nodes are glued to the inlineasm_br node.
10170 Chain = getControlRoot();
10171 }
10172
10173 MCSymbol *BeginLabel = nullptr;
10174 if (EmitEHLabels) {
10175 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
10176 }
10177
10178 int OpNo = -1;
10179 SmallVector<StringRef> AsmStrs;
10180 IA->collectAsmStrs(AsmStrs);
10181
10182 // Second pass over the constraints: compute which constraint option to use.
10183 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10184 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10185 OpNo++;
10186
10187 // If this is an output operand with a matching input operand, look up the
10188 // matching input. If their types mismatch, e.g. one is an integer, the
10189 // other is floating point, or their sizes are different, flag it as an
10190 // error.
10191 if (OpInfo.hasMatchingInput()) {
10192 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
10193 patchMatchingInput(OpInfo, Input, DAG);
10194 }
10195
10196 // Compute the constraint code and ConstraintType to use.
10197 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10198
10199 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10200 OpInfo.Type == InlineAsm::isClobber) ||
10201 OpInfo.ConstraintType == TargetLowering::C_Address)
10202 continue;
10203
10204 // In Linux PIC model, there are 4 cases about value/label addressing:
10205 //
10206 // 1: Function call or Label jmp inside the module.
10207 // 2: Data access (such as global variable, static variable) inside module.
10208 // 3: Function call or Label jmp outside the module.
10209 // 4: Data access (such as global variable) outside the module.
10210 //
10211 // Due to current llvm inline asm architecture designed to not "recognize"
10212 // the asm code, there are quite troubles for us to treat mem addressing
10213 // differently for same value/adress used in different instuctions.
10214 // For example, in pic model, call a func may in plt way or direclty
10215 // pc-related, but lea/mov a function adress may use got.
10216 //
10217 // Here we try to "recognize" function call for the case 1 and case 3 in
10218 // inline asm. And try to adjust the constraint for them.
10219 //
10220 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10221 // label, so here we don't handle jmp function label now, but we need to
10222 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10223 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10224 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10225 TM.getCodeModel() != CodeModel::Large) {
10226 OpInfo.isIndirect = false;
10227 OpInfo.ConstraintType = TargetLowering::C_Address;
10228 }
10229
10230 // If this is a memory input, and if the operand is not indirect, do what we
10231 // need to provide an address for the memory input.
10232 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10233 !OpInfo.isIndirect) {
10234 assert((OpInfo.isMultipleAlternative ||
10235 (OpInfo.Type == InlineAsm::isInput)) &&
10236 "Can only indirectify direct input operands!");
10237
10238 // Memory operands really want the address of the value.
10239 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
10240
10241 // There is no longer a Value* corresponding to this operand.
10242 OpInfo.CallOperandVal = nullptr;
10243
10244 // It is now an indirect operand.
10245 OpInfo.isIndirect = true;
10246 }
10247
10248 }
10249
10250 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10251 std::vector<SDValue> AsmNodeOperands;
10252 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10253 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10254 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10255
10256 // If we have a !srcloc metadata node associated with it, we want to attach
10257 // this to the ultimately generated inline asm machineinstr. To do this, we
10258 // pass in the third operand as this (potentially null) inline asm MDNode.
10259 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10260 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10261
10262 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10263 // bits as operand 3.
10264 AsmNodeOperands.push_back(DAG.getTargetConstant(
10265 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10266
10267 // Third pass: Loop over operands to prepare DAG-level operands.. As part of
10268 // this, assign virtual and physical registers for inputs and otput.
10269 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10270 // Assign Registers.
10271 SDISelAsmOperandInfo &RefOpInfo =
10272 OpInfo.isMatchingInputConstraint()
10273 ? ConstraintOperands[OpInfo.getMatchedOperand()]
10274 : OpInfo;
10275 const auto RegError =
10276 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
10277 if (RegError) {
10278 const MachineFunction &MF = DAG.getMachineFunction();
10279 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10280 const char *RegName = TRI.getName(*RegError);
10281 emitInlineAsmError(Call, "register '" + Twine(RegName) +
10282 "' allocated for constraint '" +
10283 Twine(OpInfo.ConstraintCode) +
10284 "' does not match required type");
10285 return;
10286 }
10287
10288 auto DetectWriteToReservedRegister = [&]() {
10289 const MachineFunction &MF = DAG.getMachineFunction();
10290 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10291 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10292 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10293 const char *RegName = TRI.getName(Reg);
10294 emitInlineAsmError(Call, "write to reserved register '" +
10295 Twine(RegName) + "'");
10296 return true;
10297 }
10298 }
10299 return false;
10300 };
10301 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10302 (OpInfo.Type == InlineAsm::isInput &&
10303 !OpInfo.isMatchingInputConstraint())) &&
10304 "Only address as input operand is allowed.");
10305
10306 switch (OpInfo.Type) {
10308 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10309 const InlineAsm::ConstraintCode ConstraintID =
10310 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10312 "Failed to convert memory constraint code to constraint id.");
10313
10314 // Add information to the INLINEASM node to know about this output.
10315 InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1);
10316 OpFlags.setMemConstraint(ConstraintID);
10317 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
10318 MVT::i32));
10319 AsmNodeOperands.push_back(OpInfo.CallOperand);
10320 } else {
10321 // Otherwise, this outputs to a register (directly for C_Register /
10322 // C_RegisterClass, and a target-defined fashion for
10323 // C_Immediate/C_Other). Find a register that we can use.
10324 if (OpInfo.AssignedRegs.Regs.empty()) {
10325 emitInlineAsmError(
10326 Call, "couldn't allocate output register for constraint '" +
10327 Twine(OpInfo.ConstraintCode) + "'");
10328 return;
10329 }
10330
10331 if (DetectWriteToReservedRegister())
10332 return;
10333
10334 // Add information to the INLINEASM node to know that this register is
10335 // set.
10336 OpInfo.AssignedRegs.AddInlineAsmOperands(
10337 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10339 false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
10340 }
10341 break;
10342
10343 case InlineAsm::isInput:
10344 case InlineAsm::isLabel: {
10345 SDValue InOperandVal = OpInfo.CallOperand;
10346
10347 if (OpInfo.isMatchingInputConstraint()) {
10348 // If this is required to match an output register we have already set,
10349 // just use its register.
10350 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10351 AsmNodeOperands);
10352 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
10353 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10354 if (OpInfo.isIndirect) {
10355 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10356 emitInlineAsmError(Call, "inline asm not supported yet: "
10357 "don't know how to handle tied "
10358 "indirect register inputs");
10359 return;
10360 }
10361
10363 MachineFunction &MF = DAG.getMachineFunction();
10364 MachineRegisterInfo &MRI = MF.getRegInfo();
10365 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10366 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
10367 Register TiedReg = R->getReg();
10368 MVT RegVT = R->getSimpleValueType(0);
10369 const TargetRegisterClass *RC =
10370 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10371 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10372 : TRI.getMinimalPhysRegClass(TiedReg);
10373 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
10374 Regs.push_back(MRI.createVirtualRegister(RC));
10375
10376 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10377
10378 SDLoc dl = getCurSDLoc();
10379 // Use the produced MatchedRegs object to
10380 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
10381 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
10382 OpInfo.getMatchedOperand(), dl, DAG,
10383 AsmNodeOperands);
10384 break;
10385 }
10386
10387 assert(Flag.isMemKind() && "Unknown matching constraint!");
10388 assert(Flag.getNumOperandRegisters() == 1 &&
10389 "Unexpected number of operands");
10390 // Add information to the INLINEASM node to know about this input.
10391 // See InlineAsm.h isUseOperandTiedToDef.
10392 Flag.clearMemConstraint();
10393 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10394 AsmNodeOperands.push_back(DAG.getTargetConstant(
10395 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10396 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
10397 break;
10398 }
10399
10400 // Treat indirect 'X' constraint as memory.
10401 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10402 OpInfo.isIndirect)
10403 OpInfo.ConstraintType = TargetLowering::C_Memory;
10404
10405 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10406 OpInfo.ConstraintType == TargetLowering::C_Other) {
10407 std::vector<SDValue> Ops;
10408 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10409 Ops, DAG);
10410 if (Ops.empty()) {
10411 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10412 if (isa<ConstantSDNode>(InOperandVal)) {
10413 emitInlineAsmError(Call, "value out of range for constraint '" +
10414 Twine(OpInfo.ConstraintCode) + "'");
10415 return;
10416 }
10417
10418 emitInlineAsmError(Call,
10419 "invalid operand for inline asm constraint '" +
10420 Twine(OpInfo.ConstraintCode) + "'");
10421 return;
10422 }
10423
10424 // Add information to the INLINEASM node to know about this input.
10425 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10426 AsmNodeOperands.push_back(DAG.getTargetConstant(
10427 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10428 llvm::append_range(AsmNodeOperands, Ops);
10429 break;
10430 }
10431
10432 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10433 assert((OpInfo.isIndirect ||
10434 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10435 "Operand must be indirect to be a mem!");
10436 assert(InOperandVal.getValueType() ==
10437 TLI.getPointerTy(DAG.getDataLayout()) &&
10438 "Memory operands expect pointer values");
10439
10440 const InlineAsm::ConstraintCode ConstraintID =
10441 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10443 "Failed to convert memory constraint code to constraint id.");
10444
10445 // Add information to the INLINEASM node to know about this input.
10446 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10447 ResOpType.setMemConstraint(ConstraintID);
10448 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10449 getCurSDLoc(),
10450 MVT::i32));
10451 AsmNodeOperands.push_back(InOperandVal);
10452 break;
10453 }
10454
10455 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10456 const InlineAsm::ConstraintCode ConstraintID =
10457 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10459 "Failed to convert memory constraint code to constraint id.");
10460
10461 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10462
10463 SDValue AsmOp = InOperandVal;
10464 if (isFunction(InOperandVal)) {
10465 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10466 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10467 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10468 InOperandVal.getValueType(),
10469 GA->getOffset());
10470 }
10471
10472 // Add information to the INLINEASM node to know about this input.
10473 ResOpType.setMemConstraint(ConstraintID);
10474
10475 AsmNodeOperands.push_back(
10476 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10477
10478 AsmNodeOperands.push_back(AsmOp);
10479 break;
10480 }
10481
10482 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10483 OpInfo.ConstraintType != TargetLowering::C_Register) {
10484 emitInlineAsmError(Call, "unknown asm constraint '" +
10485 Twine(OpInfo.ConstraintCode) + "'");
10486 return;
10487 }
10488
10489 // TODO: Support this.
10490 if (OpInfo.isIndirect) {
10491 emitInlineAsmError(
10492 Call, "Don't know how to handle indirect register inputs yet "
10493 "for constraint '" +
10494 Twine(OpInfo.ConstraintCode) + "'");
10495 return;
10496 }
10497
10498 // Copy the input into the appropriate registers.
10499 if (OpInfo.AssignedRegs.Regs.empty()) {
10500 emitInlineAsmError(Call,
10501 "couldn't allocate input reg for constraint '" +
10502 Twine(OpInfo.ConstraintCode) + "'");
10503 return;
10504 }
10505
10506 if (DetectWriteToReservedRegister())
10507 return;
10508
10509 SDLoc dl = getCurSDLoc();
10510
10511 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10512 &Call);
10513
10514 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10515 0, dl, DAG, AsmNodeOperands);
10516 break;
10517 }
10519 // Add the clobbered value to the operand list, so that the register
10520 // allocator is aware that the physreg got clobbered.
10521 if (!OpInfo.AssignedRegs.Regs.empty())
10523 false, 0, getCurSDLoc(), DAG,
10524 AsmNodeOperands);
10525 break;
10526 }
10527 }
10528
10529 // Finish up input operands. Set the input chain and add the flag last.
10530 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10531 if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10532
10533 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10534 Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10535 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10536 Glue = Chain.getValue(1);
10537
10538 // Do additional work to generate outputs.
10539
10540 SmallVector<EVT, 1> ResultVTs;
10541 SmallVector<SDValue, 1> ResultValues;
10542 SmallVector<SDValue, 8> OutChains;
10543
10544 llvm::Type *CallResultType = Call.getType();
10545 ArrayRef<Type *> ResultTypes;
10546 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10547 ResultTypes = StructResult->elements();
10548 else if (!CallResultType->isVoidTy())
10549 ResultTypes = ArrayRef(CallResultType);
10550
10551 auto CurResultType = ResultTypes.begin();
10552 auto handleRegAssign = [&](SDValue V) {
10553 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10554 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10555 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10556 ++CurResultType;
10557 // If the type of the inline asm call site return value is different but has
10558 // same size as the type of the asm output bitcast it. One example of this
10559 // is for vectors with different width / number of elements. This can
10560 // happen for register classes that can contain multiple different value
10561 // types. The preg or vreg allocated may not have the same VT as was
10562 // expected.
10563 //
10564 // This can also happen for a return value that disagrees with the register
10565 // class it is put in, eg. a double in a general-purpose register on a
10566 // 32-bit machine.
10567 if (ResultVT != V.getValueType() &&
10568 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10569 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10570 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10571 V.getValueType().isInteger()) {
10572 // If a result value was tied to an input value, the computed result
10573 // may have a wider width than the expected result. Extract the
10574 // relevant portion.
10575 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10576 }
10577 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10578 ResultVTs.push_back(ResultVT);
10579 ResultValues.push_back(V);
10580 };
10581
10582 // Deal with output operands.
10583 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10584 if (OpInfo.Type == InlineAsm::isOutput) {
10585 SDValue Val;
10586 // Skip trivial output operands.
10587 if (OpInfo.AssignedRegs.Regs.empty())
10588 continue;
10589
10590 switch (OpInfo.ConstraintType) {
10593 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10594 Chain, &Glue, &Call);
10595 break;
10598 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10599 OpInfo, DAG);
10600 break;
10602 break; // Already handled.
10604 break; // Silence warning.
10606 assert(false && "Unexpected unknown constraint");
10607 }
10608
10609 // Indirect output manifest as stores. Record output chains.
10610 if (OpInfo.isIndirect) {
10611 const Value *Ptr = OpInfo.CallOperandVal;
10612 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10613 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10614 MachinePointerInfo(Ptr));
10615 OutChains.push_back(Store);
10616 } else {
10617 // generate CopyFromRegs to associated registers.
10618 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10619 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10620 for (const SDValue &V : Val->op_values())
10621 handleRegAssign(V);
10622 } else
10623 handleRegAssign(Val);
10624 }
10625 }
10626 }
10627
10628 // Set results.
10629 if (!ResultValues.empty()) {
10630 assert(CurResultType == ResultTypes.end() &&
10631 "Mismatch in number of ResultTypes");
10632 assert(ResultValues.size() == ResultTypes.size() &&
10633 "Mismatch in number of output operands in asm result");
10634
10636 DAG.getVTList(ResultVTs), ResultValues);
10637 setValue(&Call, V);
10638 }
10639
10640 // Collect store chains.
10641 if (!OutChains.empty())
10642 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10643
10644 if (EmitEHLabels) {
10645 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10646 }
10647
10648 // Only Update Root if inline assembly has a memory effect.
10649 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10650 EmitEHLabels)
10651 DAG.setRoot(Chain);
10652}
10653
10654void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10655 const Twine &Message) {
10656 LLVMContext &Ctx = *DAG.getContext();
10657 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10658
10659 // Make sure we leave the DAG in a valid state
10660 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10661 SmallVector<EVT, 1> ValueVTs;
10662 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10663
10664 if (ValueVTs.empty())
10665 return;
10666
10668 for (const EVT &VT : ValueVTs)
10669 Ops.push_back(DAG.getUNDEF(VT));
10670
10671 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10672}
10673
10674void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10675 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10676 MVT::Other, getRoot(),
10677 getValue(I.getArgOperand(0)),
10678 DAG.getSrcValue(I.getArgOperand(0))));
10679}
10680
10681void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10682 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10683 const DataLayout &DL = DAG.getDataLayout();
10684 SDValue V = DAG.getVAArg(
10685 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10686 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10687 DL.getABITypeAlign(I.getType()).value());
10688 DAG.setRoot(V.getValue(1));
10689
10690 if (I.getType()->isPointerTy())
10691 V = DAG.getPtrExtOrTrunc(
10692 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10693 setValue(&I, V);
10694}
10695
10696void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10697 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10698 MVT::Other, getRoot(),
10699 getValue(I.getArgOperand(0)),
10700 DAG.getSrcValue(I.getArgOperand(0))));
10701}
10702
10703void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10704 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10705 MVT::Other, getRoot(),
10706 getValue(I.getArgOperand(0)),
10707 getValue(I.getArgOperand(1)),
10708 DAG.getSrcValue(I.getArgOperand(0)),
10709 DAG.getSrcValue(I.getArgOperand(1))));
10710}
10711
10713 const Instruction &I,
10714 SDValue Op) {
10715 std::optional<ConstantRange> CR = getRange(I);
10716
10717 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10718 return Op;
10719
10720 APInt Lo = CR->getUnsignedMin();
10721 if (!Lo.isMinValue())
10722 return Op;
10723
10724 APInt Hi = CR->getUnsignedMax();
10725 unsigned Bits = std::max(Hi.getActiveBits(),
10726 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10727
10728 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10729
10730 SDLoc SL = getCurSDLoc();
10731
10732 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10733 DAG.getValueType(SmallVT));
10734 unsigned NumVals = Op.getNode()->getNumValues();
10735 if (NumVals == 1)
10736 return ZExt;
10737
10739
10740 Ops.push_back(ZExt);
10741 for (unsigned I = 1; I != NumVals; ++I)
10742 Ops.push_back(Op.getValue(I));
10743
10744 return DAG.getMergeValues(Ops, SL);
10745}
10746
10748 SelectionDAG &DAG, const Instruction &I, SDValue Op) {
10749 FPClassTest Classes = getNoFPClass(I);
10750 if (Classes == fcNone)
10751 return Op;
10752
10753 SDLoc SL = getCurSDLoc();
10754 SDValue TestConst = DAG.getTargetConstant(Classes, SDLoc(), MVT::i32);
10755
10756 if (Op.getOpcode() != ISD::MERGE_VALUES) {
10757 return DAG.getNode(ISD::AssertNoFPClass, SL, Op.getValueType(), Op,
10758 TestConst);
10759 }
10760
10761 SmallVector<SDValue, 8> Ops(Op.getNumOperands());
10762 for (unsigned I = 0, E = Ops.size(); I != E; ++I) {
10763 SDValue MergeOp = Op.getOperand(I);
10764 Ops[I] = DAG.getNode(ISD::AssertNoFPClass, SL, MergeOp.getValueType(),
10765 MergeOp, TestConst);
10766 }
10767
10768 return DAG.getMergeValues(Ops, SL);
10769}
10770
10771/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10772/// the call being lowered.
10773///
10774/// This is a helper for lowering intrinsics that follow a target calling
10775/// convention or require stack pointer adjustment. Only a subset of the
10776/// intrinsic's operands need to participate in the calling convention.
10779 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10780 AttributeSet RetAttrs, bool IsPatchPoint) {
10782 Args.reserve(NumArgs);
10783
10784 // Populate the argument list.
10785 // Attributes for args start at offset 1, after the return attribute.
10786 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10787 ArgI != ArgE; ++ArgI) {
10788 const Value *V = Call->getOperand(ArgI);
10789
10790 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10791
10792 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10793 Entry.setAttributes(Call, ArgI);
10794 Args.push_back(Entry);
10795 }
10796
10798 .setChain(getRoot())
10799 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10800 RetAttrs)
10801 .setDiscardResult(Call->use_empty())
10802 .setIsPatchPoint(IsPatchPoint)
10804 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10805}
10806
10807/// Add a stack map intrinsic call's live variable operands to a stackmap
10808/// or patchpoint target node's operand list.
10809///
10810/// Constants are converted to TargetConstants purely as an optimization to
10811/// avoid constant materialization and register allocation.
10812///
10813/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10814/// generate addess computation nodes, and so FinalizeISel can convert the
10815/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10816/// address materialization and register allocation, but may also be required
10817/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10818/// alloca in the entry block, then the runtime may assume that the alloca's
10819/// StackMap location can be read immediately after compilation and that the
10820/// location is valid at any point during execution (this is similar to the
10821/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10822/// only available in a register, then the runtime would need to trap when
10823/// execution reaches the StackMap in order to read the alloca's location.
10824static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10826 SelectionDAGBuilder &Builder) {
10827 SelectionDAG &DAG = Builder.DAG;
10828 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10829 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10830
10831 // Things on the stack are pointer-typed, meaning that they are already
10832 // legal and can be emitted directly to target nodes.
10834 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10835 } else {
10836 // Otherwise emit a target independent node to be legalised.
10837 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10838 }
10839 }
10840}
10841
10842/// Lower llvm.experimental.stackmap.
10843void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10844 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10845 // [live variables...])
10846
10847 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10848
10849 SDValue Chain, InGlue, Callee;
10851
10852 SDLoc DL = getCurSDLoc();
10854
10855 // The stackmap intrinsic only records the live variables (the arguments
10856 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10857 // intrinsic, this won't be lowered to a function call. This means we don't
10858 // have to worry about calling conventions and target specific lowering code.
10859 // Instead we perform the call lowering right here.
10860 //
10861 // chain, flag = CALLSEQ_START(chain, 0, 0)
10862 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10863 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10864 //
10865 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10866 InGlue = Chain.getValue(1);
10867
10868 // Add the STACKMAP operands, starting with DAG house-keeping.
10869 Ops.push_back(Chain);
10870 Ops.push_back(InGlue);
10871
10872 // Add the <id>, <numShadowBytes> operands.
10873 //
10874 // These do not require legalisation, and can be emitted directly to target
10875 // constant nodes.
10877 assert(ID.getValueType() == MVT::i64);
10878 SDValue IDConst =
10879 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10880 Ops.push_back(IDConst);
10881
10882 SDValue Shad = getValue(CI.getArgOperand(1));
10883 assert(Shad.getValueType() == MVT::i32);
10884 SDValue ShadConst =
10885 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
10886 Ops.push_back(ShadConst);
10887
10888 // Add the live variables.
10889 addStackMapLiveVars(CI, 2, DL, Ops, *this);
10890
10891 // Create the STACKMAP node.
10892 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10893 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10894 InGlue = Chain.getValue(1);
10895
10896 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10897
10898 // Stackmaps don't generate values, so nothing goes into the NodeMap.
10899
10900 // Set the root to the target-lowered call chain.
10901 DAG.setRoot(Chain);
10902
10903 // Inform the Frame Information that we have a stackmap in this function.
10904 FuncInfo.MF->getFrameInfo().setHasStackMap();
10905}
10906
10907/// Lower llvm.experimental.patchpoint directly to its target opcode.
10908void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10909 const BasicBlock *EHPadBB) {
10910 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10911 // i32 <numBytes>,
10912 // i8* <target>,
10913 // i32 <numArgs>,
10914 // [Args...],
10915 // [live variables...])
10916
10918 bool IsAnyRegCC = CC == CallingConv::AnyReg;
10919 bool HasDef = !CB.getType()->isVoidTy();
10920 SDLoc dl = getCurSDLoc();
10922
10923 // Handle immediate and symbolic callees.
10924 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10925 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10926 /*isTarget=*/true);
10927 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
10928 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
10929 SDLoc(SymbolicCallee),
10930 SymbolicCallee->getValueType(0));
10931
10932 // Get the real number of arguments participating in the call <numArgs>
10934 unsigned NumArgs = NArgVal->getAsZExtVal();
10935
10936 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
10937 // Intrinsics include all meta-operands up to but not including CC.
10938 unsigned NumMetaOpers = PatchPointOpers::CCPos;
10939 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
10940 "Not enough arguments provided to the patchpoint intrinsic");
10941
10942 // For AnyRegCC the arguments are lowered later on manually.
10943 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
10944 Type *ReturnTy =
10945 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
10946
10947 TargetLowering::CallLoweringInfo CLI(DAG);
10948 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
10949 ReturnTy, CB.getAttributes().getRetAttrs(), true);
10950 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
10951
10952 SDNode *CallEnd = Result.second.getNode();
10953 if (CallEnd->getOpcode() == ISD::EH_LABEL)
10954 CallEnd = CallEnd->getOperand(0).getNode();
10955 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
10956 CallEnd = CallEnd->getOperand(0).getNode();
10957
10958 /// Get a call instruction from the call sequence chain.
10959 /// Tail calls are not allowed.
10960 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
10961 "Expected a callseq node.");
10962 SDNode *Call = CallEnd->getOperand(0).getNode();
10963 bool HasGlue = Call->getGluedNode();
10964
10965 // Replace the target specific call node with the patchable intrinsic.
10967
10968 // Push the chain.
10969 Ops.push_back(*(Call->op_begin()));
10970
10971 // Optionally, push the glue (if any).
10972 if (HasGlue)
10973 Ops.push_back(*(Call->op_end() - 1));
10974
10975 // Push the register mask info.
10976 if (HasGlue)
10977 Ops.push_back(*(Call->op_end() - 2));
10978 else
10979 Ops.push_back(*(Call->op_end() - 1));
10980
10981 // Add the <id> and <numBytes> constants.
10983 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
10985 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
10986
10987 // Add the callee.
10988 Ops.push_back(Callee);
10989
10990 // Adjust <numArgs> to account for any arguments that have been passed on the
10991 // stack instead.
10992 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
10993 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
10994 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
10995 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
10996
10997 // Add the calling convention
10998 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
10999
11000 // Add the arguments we omitted previously. The register allocator should
11001 // place these in any free register.
11002 if (IsAnyRegCC)
11003 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
11004 Ops.push_back(getValue(CB.getArgOperand(i)));
11005
11006 // Push the arguments from the call instruction.
11007 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
11008 Ops.append(Call->op_begin() + 2, e);
11009
11010 // Push live variables for the stack map.
11011 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
11012
11013 SDVTList NodeTys;
11014 if (IsAnyRegCC && HasDef) {
11015 // Create the return types based on the intrinsic definition
11016 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11017 SmallVector<EVT, 3> ValueVTs;
11018 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
11019 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
11020
11021 // There is always a chain and a glue type at the end
11022 ValueVTs.push_back(MVT::Other);
11023 ValueVTs.push_back(MVT::Glue);
11024 NodeTys = DAG.getVTList(ValueVTs);
11025 } else
11026 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11027
11028 // Replace the target specific call node with a PATCHPOINT node.
11029 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
11030
11031 // Update the NodeMap.
11032 if (HasDef) {
11033 if (IsAnyRegCC)
11034 setValue(&CB, SDValue(PPV.getNode(), 0));
11035 else
11036 setValue(&CB, Result.first);
11037 }
11038
11039 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
11040 // call sequence. Furthermore the location of the chain and glue can change
11041 // when the AnyReg calling convention is used and the intrinsic returns a
11042 // value.
11043 if (IsAnyRegCC && HasDef) {
11044 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
11045 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
11046 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
11047 } else
11048 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
11049 DAG.DeleteNode(Call);
11050
11051 // Inform the Frame Information that we have a patchpoint in this function.
11052 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
11053}
11054
11055void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
11056 unsigned Intrinsic) {
11057 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11058 SDValue Op1 = getValue(I.getArgOperand(0));
11059 SDValue Op2;
11060 if (I.arg_size() > 1)
11061 Op2 = getValue(I.getArgOperand(1));
11062 SDLoc dl = getCurSDLoc();
11063 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11064 SDValue Res;
11065 SDNodeFlags SDFlags;
11066 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
11067 SDFlags.copyFMF(*FPMO);
11068
11069 switch (Intrinsic) {
11070 case Intrinsic::vector_reduce_fadd:
11071 if (SDFlags.hasAllowReassociation())
11072 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
11073 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
11074 SDFlags);
11075 else
11076 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
11077 break;
11078 case Intrinsic::vector_reduce_fmul:
11079 if (SDFlags.hasAllowReassociation())
11080 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
11081 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
11082 SDFlags);
11083 else
11084 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
11085 break;
11086 case Intrinsic::vector_reduce_add:
11087 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
11088 break;
11089 case Intrinsic::vector_reduce_mul:
11090 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
11091 break;
11092 case Intrinsic::vector_reduce_and:
11093 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
11094 break;
11095 case Intrinsic::vector_reduce_or:
11096 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
11097 break;
11098 case Intrinsic::vector_reduce_xor:
11099 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
11100 break;
11101 case Intrinsic::vector_reduce_smax:
11102 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
11103 break;
11104 case Intrinsic::vector_reduce_smin:
11105 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
11106 break;
11107 case Intrinsic::vector_reduce_umax:
11108 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
11109 break;
11110 case Intrinsic::vector_reduce_umin:
11111 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
11112 break;
11113 case Intrinsic::vector_reduce_fmax:
11114 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
11115 break;
11116 case Intrinsic::vector_reduce_fmin:
11117 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
11118 break;
11119 case Intrinsic::vector_reduce_fmaximum:
11120 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
11121 break;
11122 case Intrinsic::vector_reduce_fminimum:
11123 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11124 break;
11125 default:
11126 llvm_unreachable("Unhandled vector reduce intrinsic");
11127 }
11128 setValue(&I, Res);
11129}
11130
11131/// Returns an AttributeList representing the attributes applied to the return
11132/// value of the given call.
11135 if (CLI.RetSExt)
11136 Attrs.push_back(Attribute::SExt);
11137 if (CLI.RetZExt)
11138 Attrs.push_back(Attribute::ZExt);
11139 if (CLI.IsInReg)
11140 Attrs.push_back(Attribute::InReg);
11141
11142 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11143 Attrs);
11144}
11145
11146/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11147/// implementation, which just calls LowerCall.
11148/// FIXME: When all targets are
11149/// migrated to using LowerCall, this hook should be integrated into SDISel.
11150std::pair<SDValue, SDValue>
11152 LLVMContext &Context = CLI.RetTy->getContext();
11153
11154 // Handle the incoming return values from the call.
11155 CLI.Ins.clear();
11156 SmallVector<Type *, 4> RetOrigTys;
11158 auto &DL = CLI.DAG.getDataLayout();
11159 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11160
11161 SmallVector<EVT, 4> RetVTs;
11162 if (CLI.RetTy != CLI.OrigRetTy) {
11163 assert(RetOrigTys.size() == 1 &&
11164 "Only supported for non-aggregate returns");
11165 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11166 } else {
11167 for (Type *Ty : RetOrigTys)
11168 RetVTs.push_back(getValueType(DL, Ty));
11169 }
11170
11171 if (CLI.IsPostTypeLegalization) {
11172 // If we are lowering a libcall after legalization, split the return type.
11173 SmallVector<Type *, 4> OldRetOrigTys;
11174 SmallVector<EVT, 4> OldRetVTs;
11175 SmallVector<TypeSize, 4> OldOffsets;
11176 RetOrigTys.swap(OldRetOrigTys);
11177 RetVTs.swap(OldRetVTs);
11178 Offsets.swap(OldOffsets);
11179
11180 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11181 EVT RetVT = OldRetVTs[i];
11182 uint64_t Offset = OldOffsets[i];
11183 MVT RegisterVT = getRegisterType(Context, RetVT);
11184 unsigned NumRegs = getNumRegisters(Context, RetVT);
11185 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11186 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11187 RetVTs.append(NumRegs, RegisterVT);
11188 for (unsigned j = 0; j != NumRegs; ++j)
11189 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11190 }
11191 }
11192
11194 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11195
11196 bool CanLowerReturn =
11198 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11199
11200 SDValue DemoteStackSlot;
11201 int DemoteStackIdx = -100;
11202 if (!CanLowerReturn) {
11203 // FIXME: equivalent assert?
11204 // assert(!CS.hasInAllocaArgument() &&
11205 // "sret demotion is incompatible with inalloca");
11206 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11207 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11209 DemoteStackIdx =
11210 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11211 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11212
11213 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11214 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11215 Entry.IsSRet = true;
11216 Entry.Alignment = Alignment;
11217 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11218 CLI.NumFixedArgs += 1;
11219 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11220 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11221
11222 // sret demotion isn't compatible with tail-calls, since the sret argument
11223 // points into the callers stack frame.
11224 CLI.IsTailCall = false;
11225 } else {
11226 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11227 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11228 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11229 ISD::ArgFlagsTy Flags;
11230 if (NeedsRegBlock) {
11231 Flags.setInConsecutiveRegs();
11232 if (I == RetVTs.size() - 1)
11233 Flags.setInConsecutiveRegsLast();
11234 }
11235 EVT VT = RetVTs[I];
11236 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11237 unsigned NumRegs =
11238 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11239 for (unsigned i = 0; i != NumRegs; ++i) {
11240 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11242 if (CLI.RetTy->isPointerTy()) {
11243 Ret.Flags.setPointer();
11244 Ret.Flags.setPointerAddrSpace(
11245 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11246 }
11247 if (CLI.RetSExt)
11248 Ret.Flags.setSExt();
11249 if (CLI.RetZExt)
11250 Ret.Flags.setZExt();
11251 if (CLI.IsInReg)
11252 Ret.Flags.setInReg();
11253 CLI.Ins.push_back(Ret);
11254 }
11255 }
11256 }
11257
11258 // We push in swifterror return as the last element of CLI.Ins.
11259 ArgListTy &Args = CLI.getArgs();
11260 if (supportSwiftError()) {
11261 for (const ArgListEntry &Arg : Args) {
11262 if (Arg.IsSwiftError) {
11263 ISD::ArgFlagsTy Flags;
11264 Flags.setSwiftError();
11266 PointerType::getUnqual(Context),
11267 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11268 CLI.Ins.push_back(Ret);
11269 }
11270 }
11271 }
11272
11273 // Handle all of the outgoing arguments.
11274 CLI.Outs.clear();
11275 CLI.OutVals.clear();
11276 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11277 SmallVector<Type *, 4> OrigArgTys;
11278 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11279 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11280 Type *FinalType = Args[i].Ty;
11281 if (Args[i].IsByVal)
11282 FinalType = Args[i].IndirectType;
11283 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11284 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11285 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11286 ++Value) {
11287 Type *OrigArgTy = OrigArgTys[Value];
11288 Type *ArgTy = OrigArgTy;
11289 if (Args[i].Ty != Args[i].OrigTy) {
11290 assert(Value == 0 && "Only supported for non-aggregate arguments");
11291 ArgTy = Args[i].Ty;
11292 }
11293
11294 EVT VT = getValueType(DL, ArgTy);
11295 SDValue Op = SDValue(Args[i].Node.getNode(),
11296 Args[i].Node.getResNo() + Value);
11297 ISD::ArgFlagsTy Flags;
11298
11299 // Certain targets (such as MIPS), may have a different ABI alignment
11300 // for a type depending on the context. Give the target a chance to
11301 // specify the alignment it wants.
11302 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11303 Flags.setOrigAlign(OriginalAlignment);
11304
11305 if (i >= CLI.NumFixedArgs)
11306 Flags.setVarArg();
11307 if (ArgTy->isPointerTy()) {
11308 Flags.setPointer();
11309 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11310 }
11311 if (Args[i].IsZExt)
11312 Flags.setZExt();
11313 if (Args[i].IsSExt)
11314 Flags.setSExt();
11315 if (Args[i].IsNoExt)
11316 Flags.setNoExt();
11317 if (Args[i].IsInReg) {
11318 // If we are using vectorcall calling convention, a structure that is
11319 // passed InReg - is surely an HVA
11321 isa<StructType>(FinalType)) {
11322 // The first value of a structure is marked
11323 if (0 == Value)
11324 Flags.setHvaStart();
11325 Flags.setHva();
11326 }
11327 // Set InReg Flag
11328 Flags.setInReg();
11329 }
11330 if (Args[i].IsSRet)
11331 Flags.setSRet();
11332 if (Args[i].IsSwiftSelf)
11333 Flags.setSwiftSelf();
11334 if (Args[i].IsSwiftAsync)
11335 Flags.setSwiftAsync();
11336 if (Args[i].IsSwiftError)
11337 Flags.setSwiftError();
11338 if (Args[i].IsCFGuardTarget)
11339 Flags.setCFGuardTarget();
11340 if (Args[i].IsByVal)
11341 Flags.setByVal();
11342 if (Args[i].IsByRef)
11343 Flags.setByRef();
11344 if (Args[i].IsPreallocated) {
11345 Flags.setPreallocated();
11346 // Set the byval flag for CCAssignFn callbacks that don't know about
11347 // preallocated. This way we can know how many bytes we should've
11348 // allocated and how many bytes a callee cleanup function will pop. If
11349 // we port preallocated to more targets, we'll have to add custom
11350 // preallocated handling in the various CC lowering callbacks.
11351 Flags.setByVal();
11352 }
11353 if (Args[i].IsInAlloca) {
11354 Flags.setInAlloca();
11355 // Set the byval flag for CCAssignFn callbacks that don't know about
11356 // inalloca. This way we can know how many bytes we should've allocated
11357 // and how many bytes a callee cleanup function will pop. If we port
11358 // inalloca to more targets, we'll have to add custom inalloca handling
11359 // in the various CC lowering callbacks.
11360 Flags.setByVal();
11361 }
11362 Align MemAlign;
11363 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11364 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11365 Flags.setByValSize(FrameSize);
11366
11367 // info is not there but there are cases it cannot get right.
11368 if (auto MA = Args[i].Alignment)
11369 MemAlign = *MA;
11370 else
11371 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11372 } else if (auto MA = Args[i].Alignment) {
11373 MemAlign = *MA;
11374 } else {
11375 MemAlign = OriginalAlignment;
11376 }
11377 Flags.setMemAlign(MemAlign);
11378 if (Args[i].IsNest)
11379 Flags.setNest();
11380 if (NeedsRegBlock)
11381 Flags.setInConsecutiveRegs();
11382
11383 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11384 unsigned NumParts =
11385 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11386 SmallVector<SDValue, 4> Parts(NumParts);
11387 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11388
11389 if (Args[i].IsSExt)
11390 ExtendKind = ISD::SIGN_EXTEND;
11391 else if (Args[i].IsZExt)
11392 ExtendKind = ISD::ZERO_EXTEND;
11393
11394 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11395 // for now.
11396 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11398 assert((CLI.RetTy == Args[i].Ty ||
11399 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11401 Args[i].Ty->getPointerAddressSpace())) &&
11402 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11403 // Before passing 'returned' to the target lowering code, ensure that
11404 // either the register MVT and the actual EVT are the same size or that
11405 // the return value and argument are extended in the same way; in these
11406 // cases it's safe to pass the argument register value unchanged as the
11407 // return register value (although it's at the target's option whether
11408 // to do so)
11409 // TODO: allow code generation to take advantage of partially preserved
11410 // registers rather than clobbering the entire register when the
11411 // parameter extension method is not compatible with the return
11412 // extension method
11413 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11414 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11415 CLI.RetZExt == Args[i].IsZExt))
11416 Flags.setReturned();
11417 }
11418
11419 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11420 CLI.CallConv, ExtendKind);
11421
11422 for (unsigned j = 0; j != NumParts; ++j) {
11423 // if it isn't first piece, alignment must be 1
11424 // For scalable vectors the scalable part is currently handled
11425 // by individual targets, so we just use the known minimum size here.
11426 ISD::OutputArg MyFlags(
11427 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11428 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11429 if (NumParts > 1 && j == 0)
11430 MyFlags.Flags.setSplit();
11431 else if (j != 0) {
11432 MyFlags.Flags.setOrigAlign(Align(1));
11433 if (j == NumParts - 1)
11434 MyFlags.Flags.setSplitEnd();
11435 }
11436
11437 CLI.Outs.push_back(MyFlags);
11438 CLI.OutVals.push_back(Parts[j]);
11439 }
11440
11441 if (NeedsRegBlock && Value == NumValues - 1)
11442 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11443 }
11444 }
11445
11447 CLI.Chain = LowerCall(CLI, InVals);
11448
11449 // Update CLI.InVals to use outside of this function.
11450 CLI.InVals = InVals;
11451
11452 // Verify that the target's LowerCall behaved as expected.
11453 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11454 "LowerCall didn't return a valid chain!");
11455 assert((!CLI.IsTailCall || InVals.empty()) &&
11456 "LowerCall emitted a return value for a tail call!");
11457 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11458 "LowerCall didn't emit the correct number of values!");
11459
11460 // For a tail call, the return value is merely live-out and there aren't
11461 // any nodes in the DAG representing it. Return a special value to
11462 // indicate that a tail call has been emitted and no more Instructions
11463 // should be processed in the current block.
11464 if (CLI.IsTailCall) {
11465 CLI.DAG.setRoot(CLI.Chain);
11466 return std::make_pair(SDValue(), SDValue());
11467 }
11468
11469#ifndef NDEBUG
11470 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11471 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11472 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11473 "LowerCall emitted a value with the wrong type!");
11474 }
11475#endif
11476
11477 SmallVector<SDValue, 4> ReturnValues;
11478 if (!CanLowerReturn) {
11479 // The instruction result is the result of loading from the
11480 // hidden sret parameter.
11481 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11482
11483 unsigned NumValues = RetVTs.size();
11484 ReturnValues.resize(NumValues);
11485 SmallVector<SDValue, 4> Chains(NumValues);
11486
11487 // An aggregate return value cannot wrap around the address space, so
11488 // offsets to its parts don't wrap either.
11490 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11491 for (unsigned i = 0; i < NumValues; ++i) {
11493 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11495 SDValue L = CLI.DAG.getLoad(
11496 RetVTs[i], CLI.DL, CLI.Chain, Add,
11498 DemoteStackIdx, Offsets[i]),
11499 HiddenSRetAlign);
11500 ReturnValues[i] = L;
11501 Chains[i] = L.getValue(1);
11502 }
11503
11504 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11505 } else {
11506 // Collect the legal value parts into potentially illegal values
11507 // that correspond to the original function's return values.
11508 std::optional<ISD::NodeType> AssertOp;
11509 if (CLI.RetSExt)
11510 AssertOp = ISD::AssertSext;
11511 else if (CLI.RetZExt)
11512 AssertOp = ISD::AssertZext;
11513 unsigned CurReg = 0;
11514 for (EVT VT : RetVTs) {
11515 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11516 unsigned NumRegs =
11517 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11518
11519 ReturnValues.push_back(getCopyFromParts(
11520 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11521 CLI.Chain, CLI.CallConv, AssertOp));
11522 CurReg += NumRegs;
11523 }
11524
11525 // For a function returning void, there is no return value. We can't create
11526 // such a node, so we just return a null return value in that case. In
11527 // that case, nothing will actually look at the value.
11528 if (ReturnValues.empty())
11529 return std::make_pair(SDValue(), CLI.Chain);
11530 }
11531
11532 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11533 CLI.DAG.getVTList(RetVTs), ReturnValues);
11534 return std::make_pair(Res, CLI.Chain);
11535}
11536
11537/// Places new result values for the node in Results (their number
11538/// and types must exactly match those of the original return values of
11539/// the node), or leaves Results empty, which indicates that the node is not
11540/// to be custom lowered after all.
11543 SelectionDAG &DAG) const {
11544 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11545
11546 if (!Res.getNode())
11547 return;
11548
11549 // If the original node has one result, take the return value from
11550 // LowerOperation as is. It might not be result number 0.
11551 if (N->getNumValues() == 1) {
11552 Results.push_back(Res);
11553 return;
11554 }
11555
11556 // If the original node has multiple results, then the return node should
11557 // have the same number of results.
11558 assert((N->getNumValues() == Res->getNumValues()) &&
11559 "Lowering returned the wrong number of results!");
11560
11561 // Places new result values base on N result number.
11562 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11563 Results.push_back(Res.getValue(I));
11564}
11565
11567 llvm_unreachable("LowerOperation not implemented for this target!");
11568}
11569
11571 Register Reg,
11572 ISD::NodeType ExtendType) {
11574 assert((Op.getOpcode() != ISD::CopyFromReg ||
11575 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11576 "Copy from a reg to the same reg!");
11577 assert(!Reg.isPhysical() && "Is a physreg");
11578
11579 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11580 // If this is an InlineAsm we have to match the registers required, not the
11581 // notional registers required by the type.
11582
11583 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11584 std::nullopt); // This is not an ABI copy.
11585 SDValue Chain = DAG.getEntryNode();
11586
11587 if (ExtendType == ISD::ANY_EXTEND) {
11588 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11589 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11590 ExtendType = PreferredExtendIt->second;
11591 }
11592 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11593 PendingExports.push_back(Chain);
11594}
11595
11597
11598/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11599/// entry block, return true. This includes arguments used by switches, since
11600/// the switch may expand into multiple basic blocks.
11601static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11602 // With FastISel active, we may be splitting blocks, so force creation
11603 // of virtual registers for all non-dead arguments.
11604 if (FastISel)
11605 return A->use_empty();
11606
11607 const BasicBlock &Entry = A->getParent()->front();
11608 for (const User *U : A->users())
11609 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11610 return false; // Use not in entry block.
11611
11612 return true;
11613}
11614
11616 DenseMap<const Argument *,
11617 std::pair<const AllocaInst *, const StoreInst *>>;
11618
11619/// Scan the entry block of the function in FuncInfo for arguments that look
11620/// like copies into a local alloca. Record any copied arguments in
11621/// ArgCopyElisionCandidates.
11622static void
11624 FunctionLoweringInfo *FuncInfo,
11625 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11626 // Record the state of every static alloca used in the entry block. Argument
11627 // allocas are all used in the entry block, so we need approximately as many
11628 // entries as we have arguments.
11629 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11631 unsigned NumArgs = FuncInfo->Fn->arg_size();
11632 StaticAllocas.reserve(NumArgs * 2);
11633
11634 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11635 if (!V)
11636 return nullptr;
11637 V = V->stripPointerCasts();
11638 const auto *AI = dyn_cast<AllocaInst>(V);
11639 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11640 return nullptr;
11641 auto Iter = StaticAllocas.insert({AI, Unknown});
11642 return &Iter.first->second;
11643 };
11644
11645 // Look for stores of arguments to static allocas. Look through bitcasts and
11646 // GEPs to handle type coercions, as long as the alloca is fully initialized
11647 // by the store. Any non-store use of an alloca escapes it and any subsequent
11648 // unanalyzed store might write it.
11649 // FIXME: Handle structs initialized with multiple stores.
11650 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11651 // Look for stores, and handle non-store uses conservatively.
11652 const auto *SI = dyn_cast<StoreInst>(&I);
11653 if (!SI) {
11654 // We will look through cast uses, so ignore them completely.
11655 if (I.isCast())
11656 continue;
11657 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11658 // to allocas.
11659 if (I.isDebugOrPseudoInst())
11660 continue;
11661 // This is an unknown instruction. Assume it escapes or writes to all
11662 // static alloca operands.
11663 for (const Use &U : I.operands()) {
11664 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11665 *Info = StaticAllocaInfo::Clobbered;
11666 }
11667 continue;
11668 }
11669
11670 // If the stored value is a static alloca, mark it as escaped.
11671 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11672 *Info = StaticAllocaInfo::Clobbered;
11673
11674 // Check if the destination is a static alloca.
11675 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11676 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11677 if (!Info)
11678 continue;
11679 const AllocaInst *AI = cast<AllocaInst>(Dst);
11680
11681 // Skip allocas that have been initialized or clobbered.
11682 if (*Info != StaticAllocaInfo::Unknown)
11683 continue;
11684
11685 // Check if the stored value is an argument, and that this store fully
11686 // initializes the alloca.
11687 // If the argument type has padding bits we can't directly forward a pointer
11688 // as the upper bits may contain garbage.
11689 // Don't elide copies from the same argument twice.
11690 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11691 const auto *Arg = dyn_cast<Argument>(Val);
11692 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11693 Arg->getType()->isEmptyTy() ||
11694 DL.getTypeStoreSize(Arg->getType()) !=
11695 DL.getTypeAllocSize(AI->getAllocatedType()) ||
11696 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11697 ArgCopyElisionCandidates.count(Arg)) {
11698 *Info = StaticAllocaInfo::Clobbered;
11699 continue;
11700 }
11701
11702 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11703 << '\n');
11704
11705 // Mark this alloca and store for argument copy elision.
11706 *Info = StaticAllocaInfo::Elidable;
11707 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11708
11709 // Stop scanning if we've seen all arguments. This will happen early in -O0
11710 // builds, which is useful, because -O0 builds have large entry blocks and
11711 // many allocas.
11712 if (ArgCopyElisionCandidates.size() == NumArgs)
11713 break;
11714 }
11715}
11716
11717/// Try to elide argument copies from memory into a local alloca. Succeeds if
11718/// ArgVal is a load from a suitable fixed stack object.
11721 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11722 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11723 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11724 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11725 // Check if this is a load from a fixed stack object.
11726 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11727 if (!LNode)
11728 return;
11729 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11730 if (!FINode)
11731 return;
11732
11733 // Check that the fixed stack object is the right size and alignment.
11734 // Look at the alignment that the user wrote on the alloca instead of looking
11735 // at the stack object.
11736 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11737 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11738 const AllocaInst *AI = ArgCopyIter->second.first;
11739 int FixedIndex = FINode->getIndex();
11740 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11741 int OldIndex = AllocaIndex;
11742 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11743 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11744 LLVM_DEBUG(
11745 dbgs() << " argument copy elision failed due to bad fixed stack "
11746 "object size\n");
11747 return;
11748 }
11749 Align RequiredAlignment = AI->getAlign();
11750 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11751 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11752 "greater than stack argument alignment ("
11753 << DebugStr(RequiredAlignment) << " vs "
11754 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11755 return;
11756 }
11757
11758 // Perform the elision. Delete the old stack object and replace its only use
11759 // in the variable info map. Mark the stack object as mutable and aliased.
11760 LLVM_DEBUG({
11761 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11762 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11763 << '\n';
11764 });
11765 MFI.RemoveStackObject(OldIndex);
11766 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11767 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11768 AllocaIndex = FixedIndex;
11769 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11770 for (SDValue ArgVal : ArgVals)
11771 Chains.push_back(ArgVal.getValue(1));
11772
11773 // Avoid emitting code for the store implementing the copy.
11774 const StoreInst *SI = ArgCopyIter->second.second;
11775 ElidedArgCopyInstrs.insert(SI);
11776
11777 // Check for uses of the argument again so that we can avoid exporting ArgVal
11778 // if it is't used by anything other than the store.
11779 for (const Value *U : Arg.users()) {
11780 if (U != SI) {
11781 ArgHasUses = true;
11782 break;
11783 }
11784 }
11785}
11786
11787void SelectionDAGISel::LowerArguments(const Function &F) {
11788 SelectionDAG &DAG = SDB->DAG;
11789 SDLoc dl = SDB->getCurSDLoc();
11790 const DataLayout &DL = DAG.getDataLayout();
11792
11793 // In Naked functions we aren't going to save any registers.
11794 if (F.hasFnAttribute(Attribute::Naked))
11795 return;
11796
11797 if (!FuncInfo->CanLowerReturn) {
11798 // Put in an sret pointer parameter before all the other parameters.
11799 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11800
11801 ISD::ArgFlagsTy Flags;
11802 Flags.setSRet();
11803 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11804 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11806 Ins.push_back(RetArg);
11807 }
11808
11809 // Look for stores of arguments to static allocas. Mark such arguments with a
11810 // flag to ask the target to give us the memory location of that argument if
11811 // available.
11812 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11814 ArgCopyElisionCandidates);
11815
11816 // Set up the incoming argument description vector.
11817 for (const Argument &Arg : F.args()) {
11818 unsigned ArgNo = Arg.getArgNo();
11820 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11821 bool isArgValueUsed = !Arg.use_empty();
11822 unsigned PartBase = 0;
11823 Type *FinalType = Arg.getType();
11824 if (Arg.hasAttribute(Attribute::ByVal))
11825 FinalType = Arg.getParamByValType();
11826 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11827 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11828 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11829 ++Value) {
11830 Type *ArgTy = Types[Value];
11831 EVT VT = TLI->getValueType(DL, ArgTy);
11832 ISD::ArgFlagsTy Flags;
11833
11834 if (ArgTy->isPointerTy()) {
11835 Flags.setPointer();
11836 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11837 }
11838 if (Arg.hasAttribute(Attribute::ZExt))
11839 Flags.setZExt();
11840 if (Arg.hasAttribute(Attribute::SExt))
11841 Flags.setSExt();
11842 if (Arg.hasAttribute(Attribute::InReg)) {
11843 // If we are using vectorcall calling convention, a structure that is
11844 // passed InReg - is surely an HVA
11845 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11846 isa<StructType>(Arg.getType())) {
11847 // The first value of a structure is marked
11848 if (0 == Value)
11849 Flags.setHvaStart();
11850 Flags.setHva();
11851 }
11852 // Set InReg Flag
11853 Flags.setInReg();
11854 }
11855 if (Arg.hasAttribute(Attribute::StructRet))
11856 Flags.setSRet();
11857 if (Arg.hasAttribute(Attribute::SwiftSelf))
11858 Flags.setSwiftSelf();
11859 if (Arg.hasAttribute(Attribute::SwiftAsync))
11860 Flags.setSwiftAsync();
11861 if (Arg.hasAttribute(Attribute::SwiftError))
11862 Flags.setSwiftError();
11863 if (Arg.hasAttribute(Attribute::ByVal))
11864 Flags.setByVal();
11865 if (Arg.hasAttribute(Attribute::ByRef))
11866 Flags.setByRef();
11867 if (Arg.hasAttribute(Attribute::InAlloca)) {
11868 Flags.setInAlloca();
11869 // Set the byval flag for CCAssignFn callbacks that don't know about
11870 // inalloca. This way we can know how many bytes we should've allocated
11871 // and how many bytes a callee cleanup function will pop. If we port
11872 // inalloca to more targets, we'll have to add custom inalloca handling
11873 // in the various CC lowering callbacks.
11874 Flags.setByVal();
11875 }
11876 if (Arg.hasAttribute(Attribute::Preallocated)) {
11877 Flags.setPreallocated();
11878 // Set the byval flag for CCAssignFn callbacks that don't know about
11879 // preallocated. This way we can know how many bytes we should've
11880 // allocated and how many bytes a callee cleanup function will pop. If
11881 // we port preallocated to more targets, we'll have to add custom
11882 // preallocated handling in the various CC lowering callbacks.
11883 Flags.setByVal();
11884 }
11885
11886 // Certain targets (such as MIPS), may have a different ABI alignment
11887 // for a type depending on the context. Give the target a chance to
11888 // specify the alignment it wants.
11889 const Align OriginalAlignment(
11890 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
11891 Flags.setOrigAlign(OriginalAlignment);
11892
11893 Align MemAlign;
11894 Type *ArgMemTy = nullptr;
11895 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11896 Flags.isByRef()) {
11897 if (!ArgMemTy)
11898 ArgMemTy = Arg.getPointeeInMemoryValueType();
11899
11900 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11901
11902 // For in-memory arguments, size and alignment should be passed from FE.
11903 // BE will guess if this info is not there but there are cases it cannot
11904 // get right.
11905 if (auto ParamAlign = Arg.getParamStackAlign())
11906 MemAlign = *ParamAlign;
11907 else if ((ParamAlign = Arg.getParamAlign()))
11908 MemAlign = *ParamAlign;
11909 else
11910 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
11911 if (Flags.isByRef())
11912 Flags.setByRefSize(MemSize);
11913 else
11914 Flags.setByValSize(MemSize);
11915 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11916 MemAlign = *ParamAlign;
11917 } else {
11918 MemAlign = OriginalAlignment;
11919 }
11920 Flags.setMemAlign(MemAlign);
11921
11922 if (Arg.hasAttribute(Attribute::Nest))
11923 Flags.setNest();
11924 if (NeedsRegBlock)
11925 Flags.setInConsecutiveRegs();
11926 if (ArgCopyElisionCandidates.count(&Arg))
11927 Flags.setCopyElisionCandidate();
11928 if (Arg.hasAttribute(Attribute::Returned))
11929 Flags.setReturned();
11930
11931 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
11932 *CurDAG->getContext(), F.getCallingConv(), VT);
11933 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
11934 *CurDAG->getContext(), F.getCallingConv(), VT);
11935 for (unsigned i = 0; i != NumRegs; ++i) {
11936 // For scalable vectors, use the minimum size; individual targets
11937 // are responsible for handling scalable vector arguments and
11938 // return values.
11939 ISD::InputArg MyFlags(
11940 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
11941 PartBase + i * RegisterVT.getStoreSize().getKnownMinValue());
11942 if (NumRegs > 1 && i == 0)
11943 MyFlags.Flags.setSplit();
11944 // if it isn't first piece, alignment must be 1
11945 else if (i > 0) {
11946 MyFlags.Flags.setOrigAlign(Align(1));
11947 if (i == NumRegs - 1)
11948 MyFlags.Flags.setSplitEnd();
11949 }
11950 Ins.push_back(MyFlags);
11951 }
11952 if (NeedsRegBlock && Value == NumValues - 1)
11953 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
11954 PartBase += VT.getStoreSize().getKnownMinValue();
11955 }
11956 }
11957
11958 // Call the target to set up the argument values.
11960 SDValue NewRoot = TLI->LowerFormalArguments(
11961 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
11962
11963 // Verify that the target's LowerFormalArguments behaved as expected.
11964 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
11965 "LowerFormalArguments didn't return a valid chain!");
11966 assert(InVals.size() == Ins.size() &&
11967 "LowerFormalArguments didn't emit the correct number of values!");
11968 LLVM_DEBUG({
11969 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
11970 assert(InVals[i].getNode() &&
11971 "LowerFormalArguments emitted a null value!");
11972 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
11973 "LowerFormalArguments emitted a value with the wrong type!");
11974 }
11975 });
11976
11977 // Update the DAG with the new chain value resulting from argument lowering.
11978 DAG.setRoot(NewRoot);
11979
11980 // Set up the argument values.
11981 unsigned i = 0;
11982 if (!FuncInfo->CanLowerReturn) {
11983 // Create a virtual register for the sret pointer, and put in a copy
11984 // from the sret argument into it.
11985 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11986 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
11987 std::optional<ISD::NodeType> AssertOp;
11988 SDValue ArgValue =
11989 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
11990 F.getCallingConv(), AssertOp);
11991
11992 MachineFunction& MF = SDB->DAG.getMachineFunction();
11993 MachineRegisterInfo& RegInfo = MF.getRegInfo();
11994 Register SRetReg =
11995 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
11996 FuncInfo->DemoteRegister = SRetReg;
11997 NewRoot =
11998 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
11999 DAG.setRoot(NewRoot);
12000
12001 // i indexes lowered arguments. Bump it past the hidden sret argument.
12002 ++i;
12003 }
12004
12006 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
12007 for (const Argument &Arg : F.args()) {
12008 SmallVector<SDValue, 4> ArgValues;
12009 SmallVector<EVT, 4> ValueVTs;
12010 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
12011 unsigned NumValues = ValueVTs.size();
12012 if (NumValues == 0)
12013 continue;
12014
12015 bool ArgHasUses = !Arg.use_empty();
12016
12017 // Elide the copying store if the target loaded this argument from a
12018 // suitable fixed stack object.
12019 if (Ins[i].Flags.isCopyElisionCandidate()) {
12020 unsigned NumParts = 0;
12021 for (EVT VT : ValueVTs)
12022 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
12023 F.getCallingConv(), VT);
12024
12025 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
12026 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
12027 ArrayRef(&InVals[i], NumParts), ArgHasUses);
12028 }
12029
12030 // If this argument is unused then remember its value. It is used to generate
12031 // debugging information.
12032 bool isSwiftErrorArg =
12033 TLI->supportSwiftError() &&
12034 Arg.hasAttribute(Attribute::SwiftError);
12035 if (!ArgHasUses && !isSwiftErrorArg) {
12036 SDB->setUnusedArgValue(&Arg, InVals[i]);
12037
12038 // Also remember any frame index for use in FastISel.
12039 if (FrameIndexSDNode *FI =
12041 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12042 }
12043
12044 for (unsigned Val = 0; Val != NumValues; ++Val) {
12045 EVT VT = ValueVTs[Val];
12046 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
12047 F.getCallingConv(), VT);
12048 unsigned NumParts = TLI->getNumRegistersForCallingConv(
12049 *CurDAG->getContext(), F.getCallingConv(), VT);
12050
12051 // Even an apparent 'unused' swifterror argument needs to be returned. So
12052 // we do generate a copy for it that can be used on return from the
12053 // function.
12054 if (ArgHasUses || isSwiftErrorArg) {
12055 std::optional<ISD::NodeType> AssertOp;
12056 if (Arg.hasAttribute(Attribute::SExt))
12057 AssertOp = ISD::AssertSext;
12058 else if (Arg.hasAttribute(Attribute::ZExt))
12059 AssertOp = ISD::AssertZext;
12060
12061 SDValue OutVal =
12062 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
12063 NewRoot, F.getCallingConv(), AssertOp);
12064
12065 FPClassTest NoFPClass = Arg.getNoFPClass();
12066 if (NoFPClass != fcNone) {
12067 SDValue SDNoFPClass = DAG.getTargetConstant(
12068 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
12069 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
12070 OutVal, SDNoFPClass);
12071 }
12072 ArgValues.push_back(OutVal);
12073 }
12074
12075 i += NumParts;
12076 }
12077
12078 // We don't need to do anything else for unused arguments.
12079 if (ArgValues.empty())
12080 continue;
12081
12082 // Note down frame index.
12083 if (FrameIndexSDNode *FI =
12084 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
12085 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12086
12087 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
12088 SDB->getCurSDLoc());
12089
12090 SDB->setValue(&Arg, Res);
12091 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
12092 // We want to associate the argument with the frame index, among
12093 // involved operands, that correspond to the lowest address. The
12094 // getCopyFromParts function, called earlier, is swapping the order of
12095 // the operands to BUILD_PAIR depending on endianness. The result of
12096 // that swapping is that the least significant bits of the argument will
12097 // be in the first operand of the BUILD_PAIR node, and the most
12098 // significant bits will be in the second operand.
12099 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
12100 if (LoadSDNode *LNode =
12101 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
12102 if (FrameIndexSDNode *FI =
12103 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
12104 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12105 }
12106
12107 // Analyses past this point are naive and don't expect an assertion.
12108 if (Res.getOpcode() == ISD::AssertZext)
12109 Res = Res.getOperand(0);
12110
12111 // Update the SwiftErrorVRegDefMap.
12112 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
12113 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12114 if (Reg.isVirtual())
12115 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
12116 Reg);
12117 }
12118
12119 // If this argument is live outside of the entry block, insert a copy from
12120 // wherever we got it to the vreg that other BB's will reference it as.
12121 if (Res.getOpcode() == ISD::CopyFromReg) {
12122 // If we can, though, try to skip creating an unnecessary vreg.
12123 // FIXME: This isn't very clean... it would be nice to make this more
12124 // general.
12125 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12126 if (Reg.isVirtual()) {
12127 FuncInfo->ValueMap[&Arg] = Reg;
12128 continue;
12129 }
12130 }
12131 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12132 FuncInfo->InitializeRegForValue(&Arg);
12133 SDB->CopyToExportRegsIfNeeded(&Arg);
12134 }
12135 }
12136
12137 if (!Chains.empty()) {
12138 Chains.push_back(NewRoot);
12139 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12140 }
12141
12142 DAG.setRoot(NewRoot);
12143
12144 assert(i == InVals.size() && "Argument register count mismatch!");
12145
12146 // If any argument copy elisions occurred and we have debug info, update the
12147 // stale frame indices used in the dbg.declare variable info table.
12148 if (!ArgCopyElisionFrameIndexMap.empty()) {
12149 for (MachineFunction::VariableDbgInfo &VI :
12150 MF->getInStackSlotVariableDbgInfo()) {
12151 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12152 if (I != ArgCopyElisionFrameIndexMap.end())
12153 VI.updateStackSlot(I->second);
12154 }
12155 }
12156
12157 // Finally, if the target has anything special to do, allow it to do so.
12159}
12160
12161/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12162/// ensure constants are generated when needed. Remember the virtual registers
12163/// that need to be added to the Machine PHI nodes as input. We cannot just
12164/// directly add them, because expansion might result in multiple MBB's for one
12165/// BB. As such, the start of the BB might correspond to a different MBB than
12166/// the end.
12167void
12168SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12169 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12170
12171 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12172
12173 // Check PHI nodes in successors that expect a value to be available from this
12174 // block.
12175 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12176 if (!isa<PHINode>(SuccBB->begin())) continue;
12177 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12178
12179 // If this terminator has multiple identical successors (common for
12180 // switches), only handle each succ once.
12181 if (!SuccsHandled.insert(SuccMBB).second)
12182 continue;
12183
12185
12186 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12187 // nodes and Machine PHI nodes, but the incoming operands have not been
12188 // emitted yet.
12189 for (const PHINode &PN : SuccBB->phis()) {
12190 // Ignore dead phi's.
12191 if (PN.use_empty())
12192 continue;
12193
12194 // Skip empty types
12195 if (PN.getType()->isEmptyTy())
12196 continue;
12197
12198 Register Reg;
12199 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12200
12201 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12202 Register &RegOut = ConstantsOut[C];
12203 if (!RegOut) {
12204 RegOut = FuncInfo.CreateRegs(&PN);
12205 // We need to zero/sign extend ConstantInt phi operands to match
12206 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12207 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12208 if (auto *CI = dyn_cast<ConstantInt>(C))
12209 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12211 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12212 }
12213 Reg = RegOut;
12214 } else {
12216 FuncInfo.ValueMap.find(PHIOp);
12217 if (I != FuncInfo.ValueMap.end())
12218 Reg = I->second;
12219 else {
12220 assert(isa<AllocaInst>(PHIOp) &&
12221 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12222 "Didn't codegen value into a register!??");
12223 Reg = FuncInfo.CreateRegs(&PN);
12225 }
12226 }
12227
12228 // Remember that this register needs to added to the machine PHI node as
12229 // the input for this MBB.
12230 SmallVector<EVT, 4> ValueVTs;
12231 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12232 for (EVT VT : ValueVTs) {
12233 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12234 for (unsigned i = 0; i != NumRegisters; ++i)
12235 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12236 Reg += NumRegisters;
12237 }
12238 }
12239 }
12240
12241 ConstantsOut.clear();
12242}
12243
12244MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12246 if (++I == FuncInfo.MF->end())
12247 return nullptr;
12248 return &*I;
12249}
12250
12251/// During lowering new call nodes can be created (such as memset, etc.).
12252/// Those will become new roots of the current DAG, but complications arise
12253/// when they are tail calls. In such cases, the call lowering will update
12254/// the root, but the builder still needs to know that a tail call has been
12255/// lowered in order to avoid generating an additional return.
12256void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12257 // If the node is null, we do have a tail call.
12258 if (MaybeTC.getNode() != nullptr)
12259 DAG.setRoot(MaybeTC);
12260 else
12261 HasTailCall = true;
12262}
12263
12264void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12265 MachineBasicBlock *SwitchMBB,
12266 MachineBasicBlock *DefaultMBB) {
12267 MachineFunction *CurMF = FuncInfo.MF;
12268 MachineBasicBlock *NextMBB = nullptr;
12270 if (++BBI != FuncInfo.MF->end())
12271 NextMBB = &*BBI;
12272
12273 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12274
12275 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12276
12277 if (Size == 2 && W.MBB == SwitchMBB) {
12278 // If any two of the cases has the same destination, and if one value
12279 // is the same as the other, but has one bit unset that the other has set,
12280 // use bit manipulation to do two compares at once. For example:
12281 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12282 // TODO: This could be extended to merge any 2 cases in switches with 3
12283 // cases.
12284 // TODO: Handle cases where W.CaseBB != SwitchBB.
12285 CaseCluster &Small = *W.FirstCluster;
12286 CaseCluster &Big = *W.LastCluster;
12287
12288 if (Small.Low == Small.High && Big.Low == Big.High &&
12289 Small.MBB == Big.MBB) {
12290 const APInt &SmallValue = Small.Low->getValue();
12291 const APInt &BigValue = Big.Low->getValue();
12292
12293 // Check that there is only one bit different.
12294 APInt CommonBit = BigValue ^ SmallValue;
12295 if (CommonBit.isPowerOf2()) {
12296 SDValue CondLHS = getValue(Cond);
12297 EVT VT = CondLHS.getValueType();
12298 SDLoc DL = getCurSDLoc();
12299
12300 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12301 DAG.getConstant(CommonBit, DL, VT));
12302 SDValue Cond = DAG.getSetCC(
12303 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12304 ISD::SETEQ);
12305
12306 // Update successor info.
12307 // Both Small and Big will jump to Small.BB, so we sum up the
12308 // probabilities.
12309 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12310 if (BPI)
12311 addSuccessorWithProb(
12312 SwitchMBB, DefaultMBB,
12313 // The default destination is the first successor in IR.
12314 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12315 else
12316 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12317
12318 // Insert the true branch.
12319 SDValue BrCond =
12320 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12321 DAG.getBasicBlock(Small.MBB));
12322 // Insert the false branch.
12323 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12324 DAG.getBasicBlock(DefaultMBB));
12325
12326 DAG.setRoot(BrCond);
12327 return;
12328 }
12329 }
12330 }
12331
12332 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12333 // Here, we order cases by probability so the most likely case will be
12334 // checked first. However, two clusters can have the same probability in
12335 // which case their relative ordering is non-deterministic. So we use Low
12336 // as a tie-breaker as clusters are guaranteed to never overlap.
12337 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12338 [](const CaseCluster &a, const CaseCluster &b) {
12339 return a.Prob != b.Prob ?
12340 a.Prob > b.Prob :
12341 a.Low->getValue().slt(b.Low->getValue());
12342 });
12343
12344 // Rearrange the case blocks so that the last one falls through if possible
12345 // without changing the order of probabilities.
12346 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12347 --I;
12348 if (I->Prob > W.LastCluster->Prob)
12349 break;
12350 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12351 std::swap(*I, *W.LastCluster);
12352 break;
12353 }
12354 }
12355 }
12356
12357 // Compute total probability.
12358 BranchProbability DefaultProb = W.DefaultProb;
12359 BranchProbability UnhandledProbs = DefaultProb;
12360 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12361 UnhandledProbs += I->Prob;
12362
12363 MachineBasicBlock *CurMBB = W.MBB;
12364 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12365 bool FallthroughUnreachable = false;
12366 MachineBasicBlock *Fallthrough;
12367 if (I == W.LastCluster) {
12368 // For the last cluster, fall through to the default destination.
12369 Fallthrough = DefaultMBB;
12370 FallthroughUnreachable = isa<UnreachableInst>(
12371 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12372 } else {
12373 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12374 CurMF->insert(BBI, Fallthrough);
12375 // Put Cond in a virtual register to make it available from the new blocks.
12377 }
12378 UnhandledProbs -= I->Prob;
12379
12380 switch (I->Kind) {
12381 case CC_JumpTable: {
12382 // FIXME: Optimize away range check based on pivot comparisons.
12383 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12384 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12385
12386 // The jump block hasn't been inserted yet; insert it here.
12387 MachineBasicBlock *JumpMBB = JT->MBB;
12388 CurMF->insert(BBI, JumpMBB);
12389
12390 auto JumpProb = I->Prob;
12391 auto FallthroughProb = UnhandledProbs;
12392
12393 // If the default statement is a target of the jump table, we evenly
12394 // distribute the default probability to successors of CurMBB. Also
12395 // update the probability on the edge from JumpMBB to Fallthrough.
12396 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12397 SE = JumpMBB->succ_end();
12398 SI != SE; ++SI) {
12399 if (*SI == DefaultMBB) {
12400 JumpProb += DefaultProb / 2;
12401 FallthroughProb -= DefaultProb / 2;
12402 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12403 JumpMBB->normalizeSuccProbs();
12404 break;
12405 }
12406 }
12407
12408 // If the default clause is unreachable, propagate that knowledge into
12409 // JTH->FallthroughUnreachable which will use it to suppress the range
12410 // check.
12411 //
12412 // However, don't do this if we're doing branch target enforcement,
12413 // because a table branch _without_ a range check can be a tempting JOP
12414 // gadget - out-of-bounds inputs that are impossible in correct
12415 // execution become possible again if an attacker can influence the
12416 // control flow. So if an attacker doesn't already have a BTI bypass
12417 // available, we don't want them to be able to get one out of this
12418 // table branch.
12419 if (FallthroughUnreachable) {
12420 Function &CurFunc = CurMF->getFunction();
12421 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12422 JTH->FallthroughUnreachable = true;
12423 }
12424
12425 if (!JTH->FallthroughUnreachable)
12426 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12427 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12428 CurMBB->normalizeSuccProbs();
12429
12430 // The jump table header will be inserted in our current block, do the
12431 // range check, and fall through to our fallthrough block.
12432 JTH->HeaderBB = CurMBB;
12433 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12434
12435 // If we're in the right place, emit the jump table header right now.
12436 if (CurMBB == SwitchMBB) {
12437 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12438 JTH->Emitted = true;
12439 }
12440 break;
12441 }
12442 case CC_BitTests: {
12443 // FIXME: Optimize away range check based on pivot comparisons.
12444 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12445
12446 // The bit test blocks haven't been inserted yet; insert them here.
12447 for (BitTestCase &BTC : BTB->Cases)
12448 CurMF->insert(BBI, BTC.ThisBB);
12449
12450 // Fill in fields of the BitTestBlock.
12451 BTB->Parent = CurMBB;
12452 BTB->Default = Fallthrough;
12453
12454 BTB->DefaultProb = UnhandledProbs;
12455 // If the cases in bit test don't form a contiguous range, we evenly
12456 // distribute the probability on the edge to Fallthrough to two
12457 // successors of CurMBB.
12458 if (!BTB->ContiguousRange) {
12459 BTB->Prob += DefaultProb / 2;
12460 BTB->DefaultProb -= DefaultProb / 2;
12461 }
12462
12463 if (FallthroughUnreachable)
12464 BTB->FallthroughUnreachable = true;
12465
12466 // If we're in the right place, emit the bit test header right now.
12467 if (CurMBB == SwitchMBB) {
12468 visitBitTestHeader(*BTB, SwitchMBB);
12469 BTB->Emitted = true;
12470 }
12471 break;
12472 }
12473 case CC_Range: {
12474 const Value *RHS, *LHS, *MHS;
12475 ISD::CondCode CC;
12476 if (I->Low == I->High) {
12477 // Check Cond == I->Low.
12478 CC = ISD::SETEQ;
12479 LHS = Cond;
12480 RHS=I->Low;
12481 MHS = nullptr;
12482 } else {
12483 // Check I->Low <= Cond <= I->High.
12484 CC = ISD::SETLE;
12485 LHS = I->Low;
12486 MHS = Cond;
12487 RHS = I->High;
12488 }
12489
12490 // If Fallthrough is unreachable, fold away the comparison.
12491 if (FallthroughUnreachable)
12492 CC = ISD::SETTRUE;
12493
12494 // The false probability is the sum of all unhandled cases.
12495 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12496 getCurSDLoc(), I->Prob, UnhandledProbs);
12497
12498 if (CurMBB == SwitchMBB)
12499 visitSwitchCase(CB, SwitchMBB);
12500 else
12501 SL->SwitchCases.push_back(CB);
12502
12503 break;
12504 }
12505 }
12506 CurMBB = Fallthrough;
12507 }
12508}
12509
12510void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12511 const SwitchWorkListItem &W,
12512 Value *Cond,
12513 MachineBasicBlock *SwitchMBB) {
12514 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12515 "Clusters not sorted?");
12516 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12517
12518 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12519 SL->computeSplitWorkItemInfo(W);
12520
12521 // Use the first element on the right as pivot since we will make less-than
12522 // comparisons against it.
12523 CaseClusterIt PivotCluster = FirstRight;
12524 assert(PivotCluster > W.FirstCluster);
12525 assert(PivotCluster <= W.LastCluster);
12526
12527 CaseClusterIt FirstLeft = W.FirstCluster;
12528 CaseClusterIt LastRight = W.LastCluster;
12529
12530 const ConstantInt *Pivot = PivotCluster->Low;
12531
12532 // New blocks will be inserted immediately after the current one.
12534 ++BBI;
12535
12536 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12537 // we can branch to its destination directly if it's squeezed exactly in
12538 // between the known lower bound and Pivot - 1.
12539 MachineBasicBlock *LeftMBB;
12540 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12541 FirstLeft->Low == W.GE &&
12542 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12543 LeftMBB = FirstLeft->MBB;
12544 } else {
12545 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12546 FuncInfo.MF->insert(BBI, LeftMBB);
12547 WorkList.push_back(
12548 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12549 // Put Cond in a virtual register to make it available from the new blocks.
12551 }
12552
12553 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12554 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12555 // directly if RHS.High equals the current upper bound.
12556 MachineBasicBlock *RightMBB;
12557 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12558 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12559 RightMBB = FirstRight->MBB;
12560 } else {
12561 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12562 FuncInfo.MF->insert(BBI, RightMBB);
12563 WorkList.push_back(
12564 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12565 // Put Cond in a virtual register to make it available from the new blocks.
12567 }
12568
12569 // Create the CaseBlock record that will be used to lower the branch.
12570 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12571 getCurSDLoc(), LeftProb, RightProb);
12572
12573 if (W.MBB == SwitchMBB)
12574 visitSwitchCase(CB, SwitchMBB);
12575 else
12576 SL->SwitchCases.push_back(CB);
12577}
12578
12579// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12580// from the swith statement.
12582 BranchProbability PeeledCaseProb) {
12583 if (PeeledCaseProb == BranchProbability::getOne())
12585 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12586
12587 uint32_t Numerator = CaseProb.getNumerator();
12588 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12589 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12590}
12591
12592// Try to peel the top probability case if it exceeds the threshold.
12593// Return current MachineBasicBlock for the switch statement if the peeling
12594// does not occur.
12595// If the peeling is performed, return the newly created MachineBasicBlock
12596// for the peeled switch statement. Also update Clusters to remove the peeled
12597// case. PeeledCaseProb is the BranchProbability for the peeled case.
12598MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12599 const SwitchInst &SI, CaseClusterVector &Clusters,
12600 BranchProbability &PeeledCaseProb) {
12601 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12602 // Don't perform if there is only one cluster or optimizing for size.
12603 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12604 TM.getOptLevel() == CodeGenOptLevel::None ||
12605 SwitchMBB->getParent()->getFunction().hasMinSize())
12606 return SwitchMBB;
12607
12608 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12609 unsigned PeeledCaseIndex = 0;
12610 bool SwitchPeeled = false;
12611 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12612 CaseCluster &CC = Clusters[Index];
12613 if (CC.Prob < TopCaseProb)
12614 continue;
12615 TopCaseProb = CC.Prob;
12616 PeeledCaseIndex = Index;
12617 SwitchPeeled = true;
12618 }
12619 if (!SwitchPeeled)
12620 return SwitchMBB;
12621
12622 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12623 << TopCaseProb << "\n");
12624
12625 // Record the MBB for the peeled switch statement.
12626 MachineFunction::iterator BBI(SwitchMBB);
12627 ++BBI;
12628 MachineBasicBlock *PeeledSwitchMBB =
12629 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12630 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12631
12632 ExportFromCurrentBlock(SI.getCondition());
12633 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12634 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12635 nullptr, nullptr, TopCaseProb.getCompl()};
12636 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12637
12638 Clusters.erase(PeeledCaseIt);
12639 for (CaseCluster &CC : Clusters) {
12640 LLVM_DEBUG(
12641 dbgs() << "Scale the probablity for one cluster, before scaling: "
12642 << CC.Prob << "\n");
12643 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12644 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12645 }
12646 PeeledCaseProb = TopCaseProb;
12647 return PeeledSwitchMBB;
12648}
12649
12650void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12651 // Extract cases from the switch.
12652 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12653 CaseClusterVector Clusters;
12654 Clusters.reserve(SI.getNumCases());
12655 for (auto I : SI.cases()) {
12656 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12657 const ConstantInt *CaseVal = I.getCaseValue();
12658 BranchProbability Prob =
12659 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12660 : BranchProbability(1, SI.getNumCases() + 1);
12661 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12662 }
12663
12664 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12665
12666 // Cluster adjacent cases with the same destination. We do this at all
12667 // optimization levels because it's cheap to do and will make codegen faster
12668 // if there are many clusters.
12669 sortAndRangeify(Clusters);
12670
12671 // The branch probablity of the peeled case.
12672 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12673 MachineBasicBlock *PeeledSwitchMBB =
12674 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12675
12676 // If there is only the default destination, jump there directly.
12677 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12678 if (Clusters.empty()) {
12679 assert(PeeledSwitchMBB == SwitchMBB);
12680 SwitchMBB->addSuccessor(DefaultMBB);
12681 if (DefaultMBB != NextBlock(SwitchMBB)) {
12682 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12683 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12684 }
12685 return;
12686 }
12687
12688 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12689 DAG.getBFI());
12690 SL->findBitTestClusters(Clusters, &SI);
12691
12692 LLVM_DEBUG({
12693 dbgs() << "Case clusters: ";
12694 for (const CaseCluster &C : Clusters) {
12695 if (C.Kind == CC_JumpTable)
12696 dbgs() << "JT:";
12697 if (C.Kind == CC_BitTests)
12698 dbgs() << "BT:";
12699
12700 C.Low->getValue().print(dbgs(), true);
12701 if (C.Low != C.High) {
12702 dbgs() << '-';
12703 C.High->getValue().print(dbgs(), true);
12704 }
12705 dbgs() << ' ';
12706 }
12707 dbgs() << '\n';
12708 });
12709
12710 assert(!Clusters.empty());
12711 SwitchWorkList WorkList;
12712 CaseClusterIt First = Clusters.begin();
12713 CaseClusterIt Last = Clusters.end() - 1;
12714 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12715 // Scale the branchprobability for DefaultMBB if the peel occurs and
12716 // DefaultMBB is not replaced.
12717 if (PeeledCaseProb != BranchProbability::getZero() &&
12718 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12719 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12720 WorkList.push_back(
12721 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12722
12723 while (!WorkList.empty()) {
12724 SwitchWorkListItem W = WorkList.pop_back_val();
12725 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12726
12727 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12728 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12729 // For optimized builds, lower large range as a balanced binary tree.
12730 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12731 continue;
12732 }
12733
12734 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12735 }
12736}
12737
12738void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12739 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12740 auto DL = getCurSDLoc();
12741 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12742 setValue(&I, DAG.getStepVector(DL, ResultVT));
12743}
12744
12745void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12746 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12747 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12748
12749 SDLoc DL = getCurSDLoc();
12750 SDValue V = getValue(I.getOperand(0));
12751 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12752
12753 if (VT.isScalableVector()) {
12754 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12755 return;
12756 }
12757
12758 // Use VECTOR_SHUFFLE for the fixed-length vector
12759 // to maintain existing behavior.
12760 SmallVector<int, 8> Mask;
12761 unsigned NumElts = VT.getVectorMinNumElements();
12762 for (unsigned i = 0; i != NumElts; ++i)
12763 Mask.push_back(NumElts - 1 - i);
12764
12765 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12766}
12767
12768void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12769 unsigned Factor) {
12770 auto DL = getCurSDLoc();
12771 SDValue InVec = getValue(I.getOperand(0));
12772
12773 SmallVector<EVT, 4> ValueVTs;
12774 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12775 ValueVTs);
12776
12777 EVT OutVT = ValueVTs[0];
12778 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12779
12780 SmallVector<SDValue, 4> SubVecs(Factor);
12781 for (unsigned i = 0; i != Factor; ++i) {
12782 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12783 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12784 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12785 }
12786
12787 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12788 // from existing legalisation and combines.
12789 if (OutVT.isFixedLengthVector() && Factor == 2) {
12790 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12791 createStrideMask(0, 2, OutNumElts));
12792 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12793 createStrideMask(1, 2, OutNumElts));
12794 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12795 setValue(&I, Res);
12796 return;
12797 }
12798
12799 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12800 DAG.getVTList(ValueVTs), SubVecs);
12801 setValue(&I, Res);
12802}
12803
12804void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12805 unsigned Factor) {
12806 auto DL = getCurSDLoc();
12807 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12808 EVT InVT = getValue(I.getOperand(0)).getValueType();
12809 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12810
12811 SmallVector<SDValue, 8> InVecs(Factor);
12812 for (unsigned i = 0; i < Factor; ++i) {
12813 InVecs[i] = getValue(I.getOperand(i));
12814 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12815 "Expected VTs to be the same");
12816 }
12817
12818 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12819 // from existing legalisation and combines.
12820 if (OutVT.isFixedLengthVector() && Factor == 2) {
12821 unsigned NumElts = InVT.getVectorMinNumElements();
12822 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12823 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12824 createInterleaveMask(NumElts, 2)));
12825 return;
12826 }
12827
12828 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12829 SDValue Res =
12830 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12831
12833 for (unsigned i = 0; i < Factor; ++i)
12834 Results[i] = Res.getValue(i);
12835
12836 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12837 setValue(&I, Res);
12838}
12839
12840void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12841 SmallVector<EVT, 4> ValueVTs;
12842 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12843 ValueVTs);
12844 unsigned NumValues = ValueVTs.size();
12845 if (NumValues == 0) return;
12846
12847 SmallVector<SDValue, 4> Values(NumValues);
12848 SDValue Op = getValue(I.getOperand(0));
12849
12850 for (unsigned i = 0; i != NumValues; ++i)
12851 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12852 SDValue(Op.getNode(), Op.getResNo() + i));
12853
12855 DAG.getVTList(ValueVTs), Values));
12856}
12857
12858void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12859 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12860 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12861
12862 SDLoc DL = getCurSDLoc();
12863 SDValue V1 = getValue(I.getOperand(0));
12864 SDValue V2 = getValue(I.getOperand(1));
12865 int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue();
12866
12867 // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
12868 if (VT.isScalableVector()) {
12869 setValue(
12870 &I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2,
12871 DAG.getSignedConstant(
12872 Imm, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
12873 return;
12874 }
12875
12876 unsigned NumElts = VT.getVectorNumElements();
12877
12878 uint64_t Idx = (NumElts + Imm) % NumElts;
12879
12880 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12881 SmallVector<int, 8> Mask;
12882 for (unsigned i = 0; i < NumElts; ++i)
12883 Mask.push_back(Idx + i);
12884 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12885}
12886
12887// Consider the following MIR after SelectionDAG, which produces output in
12888// phyregs in the first case or virtregs in the second case.
12889//
12890// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12891// %5:gr32 = COPY $ebx
12892// %6:gr32 = COPY $edx
12893// %1:gr32 = COPY %6:gr32
12894// %0:gr32 = COPY %5:gr32
12895//
12896// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12897// %1:gr32 = COPY %6:gr32
12898// %0:gr32 = COPY %5:gr32
12899//
12900// Given %0, we'd like to return $ebx in the first case and %5 in the second.
12901// Given %1, we'd like to return $edx in the first case and %6 in the second.
12902//
12903// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12904// to a single virtreg (such as %0). The remaining outputs monotonically
12905// increase in virtreg number from there. If a callbr has no outputs, then it
12906// should not have a corresponding callbr landingpad; in fact, the callbr
12907// landingpad would not even be able to refer to such a callbr.
12909 MachineInstr *MI = MRI.def_begin(Reg)->getParent();
12910 // There is definitely at least one copy.
12911 assert(MI->getOpcode() == TargetOpcode::COPY &&
12912 "start of copy chain MUST be COPY");
12913 Reg = MI->getOperand(1).getReg();
12914
12915 // If the copied register in the first copy must be virtual.
12916 assert(Reg.isVirtual() && "expected COPY of virtual register");
12917 MI = MRI.def_begin(Reg)->getParent();
12918
12919 // There may be an optional second copy.
12920 if (MI->getOpcode() == TargetOpcode::COPY) {
12921 assert(Reg.isVirtual() && "expected COPY of virtual register");
12922 Reg = MI->getOperand(1).getReg();
12923 assert(Reg.isPhysical() && "expected COPY of physical register");
12924 } else {
12925 // The start of the chain must be an INLINEASM_BR.
12926 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12927 "end of copy chain MUST be INLINEASM_BR");
12928 }
12929
12930 return Reg;
12931}
12932
12933// We must do this walk rather than the simpler
12934// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
12935// otherwise we will end up with copies of virtregs only valid along direct
12936// edges.
12937void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
12938 SmallVector<EVT, 8> ResultVTs;
12939 SmallVector<SDValue, 8> ResultValues;
12940 const auto *CBR =
12941 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
12942
12943 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12944 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
12945 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
12946
12947 Register InitialDef = FuncInfo.ValueMap[CBR];
12948 SDValue Chain = DAG.getRoot();
12949
12950 // Re-parse the asm constraints string.
12951 TargetLowering::AsmOperandInfoVector TargetConstraints =
12952 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
12953 for (auto &T : TargetConstraints) {
12954 SDISelAsmOperandInfo OpInfo(T);
12955 if (OpInfo.Type != InlineAsm::isOutput)
12956 continue;
12957
12958 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
12959 // individual constraint.
12960 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
12961
12962 switch (OpInfo.ConstraintType) {
12965 // Fill in OpInfo.AssignedRegs.Regs.
12966 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
12967
12968 // getRegistersForValue may produce 1 to many registers based on whether
12969 // the OpInfo.ConstraintVT is legal on the target or not.
12970 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
12971 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
12972 if (OriginalDef.isPhysical())
12973 FuncInfo.MBB->addLiveIn(OriginalDef);
12974 // Update the assigned registers to use the original defs.
12975 Reg = OriginalDef;
12976 }
12977
12978 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
12979 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
12980 ResultValues.push_back(V);
12981 ResultVTs.push_back(OpInfo.ConstraintVT);
12982 break;
12983 }
12985 SDValue Flag;
12986 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
12987 OpInfo, DAG);
12988 ++InitialDef;
12989 ResultValues.push_back(V);
12990 ResultVTs.push_back(OpInfo.ConstraintVT);
12991 break;
12992 }
12993 default:
12994 break;
12995 }
12996 }
12998 DAG.getVTList(ResultVTs), ResultValues);
12999 setValue(&I, V);
13000}
unsigned const MachineRegisterInfo * MRI
return SDValue()
static unsigned getIntrinsicID(const SDNode *N)
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Function Alias Analysis Results
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI)
Returns an AttributeList representing the attributes applied to the return value of the given call.
Definition FastISel.cpp:942
#define Check(C,...)
static Value * getCondition(Instruction *I)
Hexagon Common GEP
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
static void getRegistersForValue(MachineFunction &MF, MachineIRBuilder &MIRBuilder, GISelAsmOperandInfo &OpInfo, GISelAsmOperandInfo &RefOpInfo)
Assign virtual/physical registers for the specified register operand.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
lazy value info
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static const Function * getCalledFunction(const Value *V)
This file provides utility analysis objects describing memory locations.
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
#define T
#define T1
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static unsigned getAddressSpace(const Value *V, unsigned MaxLookup)
MachineInstr unsigned OpIdx
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t High
uint64_t IntrinsicInst * II
OptimizedStructLayoutField Field
#define P(N)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static bool hasOnlySelectUsers(const Value *Cond)
static SDValue getLoadStackGuard(SelectionDAG &DAG, const SDLoc &DL, SDValue &Chain)
Create a LOAD_STACK_GUARD node, and let it carry the target specific global variable if there exists ...
static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index, SDValue &Scale, SelectionDAGBuilder *SDB, const BasicBlock *CurBB, uint64_t ElemSize)
static void failForInvalidBundles(const CallBase &I, StringRef Name, ArrayRef< uint32_t > AllowedBundles)
static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx, const SDLoc &DL, SmallVectorImpl< SDValue > &Ops, SelectionDAGBuilder &Builder)
Add a stack map intrinsic call's live variable operands to a stackmap or patchpoint target node's ope...
static const unsigned MaxParallelChains
static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
visitPow - Lower a pow intrinsic.
static const CallBase * FindPreallocatedCall(const Value *PreallocatedSetup)
Given a @llvm.call.preallocated.setup, return the corresponding preallocated call.
static cl::opt< unsigned > SwitchPeelThreshold("switch-peel-threshold", cl::Hidden, cl::init(66), cl::desc("Set the case probability threshold for peeling the case from a " "switch statement. A value greater than 100 will void this " "optimization"))
static cl::opt< bool > InsertAssertAlign("insert-assert-align", cl::init(true), cl::desc("Insert the experimental `assertalign` node."), cl::ReallyHidden)
static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin)
static bool handleDanglingVariadicDebugInfo(SelectionDAG &DAG, DILocalVariable *Variable, DebugLoc DL, unsigned Order, SmallVectorImpl< Value * > &Values, DIExpression *Expression)
static unsigned findMatchingInlineAsmOperand(unsigned OperandNo, const std::vector< SDValue > &AsmNodeOperands)
static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo, SDISelAsmOperandInfo &MatchingOpInfo, SelectionDAG &DAG)
Make sure that the output operand OpInfo and its corresponding input operand MatchingOpInfo have comp...
static void findUnwindDestinations(FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB, BranchProbability Prob, SmallVectorImpl< std::pair< MachineBasicBlock *, BranchProbability > > &UnwindDests)
When an invoke or a cleanupret unwinds to the next EH pad, there are many places it could ultimately ...
static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic)
static BranchProbability scaleCaseProbality(BranchProbability CaseProb, BranchProbability PeeledCaseProb)
static SDValue expandExp2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp2 - Lower an exp2 intrinsic.
static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue Scale, SelectionDAG &DAG, const TargetLowering &TLI)
static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt, const SDLoc &dl)
getF32Constant - Get 32-bit floating point constant.
static SDValue widenVectorToPartType(SelectionDAG &DAG, SDValue Val, const SDLoc &DL, EVT PartVT)
static SDValue expandLog10(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog10 - Lower a log10 intrinsic.
DenseMap< const Argument *, std::pair< const AllocaInst *, const StoreInst * > > ArgCopyElisionMapTy
static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv)
getCopyToPartsVector - Create a series of nodes that contain the specified value split into legal par...
static void getUnderlyingArgRegs(SmallVectorImpl< std::pair< Register, TypeSize > > &Regs, const SDValue &N)
static void getCopyToParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv=std::nullopt, ISD::NodeType ExtendKind=ISD::ANY_EXTEND)
getCopyToParts - Create a series of nodes that contain the specified value split into legal parts.
static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, SelectionDAGBuilder &Builder)
static SDValue expandLog2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog2 - Lower a log2 intrinsic.
static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location, SDISelAsmOperandInfo &OpInfo, SelectionDAG &DAG)
Get a direct memory input to behave well as an indirect operand.
static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel)
isOnlyUsedInEntryBlock - If the specified argument is only used in the entry block,...
static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V, const Twine &ErrMsg)
static bool collectInstructionDeps(SmallMapVector< const Instruction *, bool, 8 > *Deps, const Value *V, SmallMapVector< const Instruction *, bool, 8 > *Necessary=nullptr, unsigned Depth=0)
static void findArgumentCopyElisionCandidates(const DataLayout &DL, FunctionLoweringInfo *FuncInfo, ArgCopyElisionMapTy &ArgCopyElisionCandidates)
Scan the entry block of the function in FuncInfo for arguments that look like copies into a local all...
static bool isFunction(SDValue Op)
static SDValue GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, const SDLoc &dl)
GetExponent - Get the exponent:
static Register FollowCopyChain(MachineRegisterInfo &MRI, Register Reg)
static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS, SelectionDAG &DAG)
ExpandPowI - Expand a llvm.powi intrinsic.
static SDValue expandLog(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog - Lower a log intrinsic.
static SDValue getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC=std::nullopt, std::optional< ISD::NodeType > AssertOp=std::nullopt)
getCopyFromParts - Create a value that contains the specified legal parts combined into the value the...
static SDValue getLimitedPrecisionExp2(SDValue t0, const SDLoc &dl, SelectionDAG &DAG)
static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl)
GetSignificand - Get the significand and build it into a floating-point number with exponent of 1:
static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp - Lower an exp intrinsic.
static const MDNode * getRangeMetadata(const Instruction &I)
static cl::opt< unsigned, true > LimitFPPrecision("limit-float-precision", cl::desc("Generate low-precision inline sequences " "for some float libcalls"), cl::location(LimitFloatPrecision), cl::Hidden, cl::init(0))
static void tryToElideArgumentCopy(FunctionLoweringInfo &FuncInfo, SmallVectorImpl< SDValue > &Chains, DenseMap< int, int > &ArgCopyElisionFrameIndexMap, SmallPtrSetImpl< const Instruction * > &ElidedArgCopyInstrs, ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg, ArrayRef< SDValue > ArgVals, bool &ArgHasUses)
Try to elide argument copies from memory into a local alloca.
static unsigned LimitFloatPrecision
LimitFloatPrecision - Generate low-precision inline sequences for some float libcalls (6,...
static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC)
getCopyFromPartsVector - Create a value that contains the specified legal parts combined into the val...
static bool InBlock(const Value *V, const BasicBlock *BB)
static FPClassTest getNoFPClass(const Instruction &I)
static LLVM_ATTRIBUTE_ALWAYS_INLINE MVT::SimpleValueType getSimpleVT(const unsigned char *MatcherTable, unsigned &MatcherIndex)
getSimpleVT - Decode a value in MatcherTable, if it's a VBR encoded value, use GetVBR to decode it.
This file defines the SmallPtrSet class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
Value * RHS
Value * LHS
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
Class for arbitrary precision integers.
Definition APInt.h:78
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
an instruction to allocate memory on the stack
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Check if an argument has a given attribute.
Definition Function.cpp:339
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
A cache of @llvm.assume calls within a function.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:361
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This class represents a no-op cast from one type to another.
The address of a basic block.
Definition Constants.h:899
Conditional or Unconditional Branch instruction.
Analysis providing branch probability information.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static uint32_t getDenominator()
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
BranchProbability getCompl() const
static BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
CallingConv::ID getCallingConv() const
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI bool isTailCall() const
Tests if this call site is marked as a tail call.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:593
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1125
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:214
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
A signed pointer, in the ptrauth sense.
Definition Constants.h:1032
uint64_t getZExtValue() const
Constant Vector Declarations.
Definition Constants.h:517
This is an important base class in LLVM.
Definition Constant.h:43
This is the common base class for constrained floating point intrinsics.
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI unsigned getNonMetadataArgCount() const
DWARF expression.
LLVM_ABI bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B)
Check if fragments overlap between a pair of FragmentInfos.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI std::optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
LLVM_ABI uint64_t getNumLocationOperands() const
Return the number of unique location operands referred to (via DW_OP_LLVM_arg) in this expression; th...
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
static LLVM_ABI const DIExpression * convertToUndefExpression(const DIExpression *Expr)
Removes all elements from Expr that do not apply to an undef debug value, which includes every operat...
static LLVM_ABI DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
static LLVM_ABI DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
Base class for variables.
LLVM_ABI std::optional< uint64_t > getSizeInBits() const
Determines the size of the variable's type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isBigEndian() const
Definition DataLayout.h:208
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI Value * getVariableLocationOp(unsigned OpIdx) const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
LLVM_ABI iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
A debug info location.
Definition DebugLoc.h:124
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:67
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
bool empty() const
Definition DenseMap.h:109
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:114
Diagnostic information for inline asm reporting.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:64
An instruction for ordering other memory operations.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:802
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
BranchProbabilityInfo * BPI
MachineBasicBlock * getMBB(const BasicBlock *BB) const
DenseMap< const AllocaInst *, int > StaticAllocaMap
StaticAllocaMap - Keep track of frame indices for fixed sized allocas in the entry block.
const LiveOutInfo * GetLiveOutRegInfo(Register Reg)
GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the register is a PHI destinat...
MachineBasicBlock * MBB
MBB - The current block.
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Type * getParamType(unsigned i) const
Parameter type accessors.
Type * getReturnType() const
Data structure describing the variable locations in a function.
const BasicBlock & getEntryBlock() const
Definition Function.h: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:742
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:730
Garbage collection metadata for a single function.
Definition GCMetadata.h:80
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
bool isInBounds() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
bool hasDLLImportStorageClass() const
Module * getParent()
Get the module that this global value is contained inside of...
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
This instruction inserts a struct field of array element value into an aggregate value.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
@ MIN_INT_BITS
Minimum number of bits that can be specified.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
A helper class to return the specified delimiter string after the first invocation of operator String...
An instruction for reading from memory.
static LocationSize precise(uint64_t Value)
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI MCSymbol * getOrCreateFrameAllocSymbol(const Twine &FuncName, unsigned Idx)
Gets a symbol that will be defined to the final stack offset of a local variable after codegen.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1078
Machine Value Type.
@ INVALID_SIMPLE_VALUE_TYPE
uint64_t getScalarSizeInBits() const
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
ElementCount getVectorElementCount() const
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
bool bitsGE(MVT VT) const
Return true if this has no less bits than VT.
bool isScalarInteger() const
Return true if this is an integer, not including vectors.
static MVT getVectorVT(MVT VT, unsigned NumElements)
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
static MVT getIntegerVT(unsigned BitWidth)
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void setIsEHContTarget(bool V=true)
Indicates if this is a target of Windows EH Continuation Guard.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
MachineInstrBundleIterator< MachineInstr > iterator
void setIsEHScopeEntry(bool V=true)
Indicates if this is the entry block of an EH scope, i.e., the block that that used to have a catchpa...
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
int getStackProtectorIndex() const
Return the index for the stack protector object.
void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased)
Set "maybe pointed to by an LLVM IR value" for an object.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
void setFunctionContextIndex(int I)
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site)
Map the begin label for a call site.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD)
Record annotations associated with a particular label.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
void setHasEHContTarget(bool V)
void addInvoke(MachineBasicBlock *LandingPad, MCSymbol *BeginLabel, MCSymbol *EndLabel)
Provide the begin and end labels of an invoke style call and associate it with a try landing pad bloc...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
Representation of each machine instruction.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI MCRegister getLiveInPhysReg(Register VReg) const
getLiveInPhysReg - If VReg is a live-in virtual register, return the corresponding live-in physical r...
An SDNode that represents everything that will be needed to construct a MachineInstr.
bool contains(const KeyT &Key) const
Definition MapVector.h:141
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:111
static MemoryLocation getAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location after Ptr, while remaining within the underlying objec...
Metadata wrapper in the Value hierarchy.
Definition Metadata.h:183
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
Resume the propagation of an exception.
Return a value (possibly void), from a function.
Holds the information from a dbg_label node through SDISel.
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getOpcode() const
SelectionDAGBuilder - This is the common target-independent lowering implementation that is parameter...
SDValue getValue(const Value *V)
getValue - Return an SDValue for the given Value.
DenseMap< const Constant *, Register > ConstantsOut
void addDanglingDebugInfo(SmallVectorImpl< Value * > &Values, DILocalVariable *Var, DIExpression *Expr, bool IsVariadic, DebugLoc DL, unsigned Order)
Register a dbg_value which relies on a Value which we have not yet seen.
void visitDbgInfo(const Instruction &I)
void clearDanglingDebugInfo()
Clear the dangling debug information map.
void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall, bool IsMustTailCall, const BasicBlock *EHPadBB=nullptr, const TargetLowering::PtrAuthInfo *PAI=nullptr)
void clear()
Clear out the current SelectionDAG and the associated state and prepare this SelectionDAGBuilder obje...
void visitBitTestHeader(SwitchCG::BitTestBlock &B, MachineBasicBlock *SwitchBB)
visitBitTestHeader - This function emits necessary code to produce value suitable for "bit tests"
void LowerStatepoint(const GCStatepointInst &I, const BasicBlock *EHPadBB=nullptr)
std::unique_ptr< SDAGSwitchLowering > SL
SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, SDValue Op)
bool HasTailCall
This is set to true if a call in the current block has been translated as a tail call.
bool ShouldEmitAsBranches(const std::vector< SwitchCG::CaseBlock > &Cases)
If the set of cases should be emitted as a series of branches, return true.
void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
EmitBranchForMergedCondition - Helper method for FindMergedConditions.
void LowerDeoptimizeCall(const CallInst *CI)
void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB)
SwiftErrorValueTracking & SwiftError
Information about the swifterror values used throughout the function.
SDValue getNonRegisterValue(const Value *V)
getNonRegisterValue - Return an SDValue for the given Value, but don't look in FuncInfo....
const TargetTransformInfo * TTI
DenseMap< MachineBasicBlock *, SmallVector< unsigned, 4 > > LPadToCallSiteMap
Map a landing pad to the call site indexes.
SDValue lowerNoFPClassToAssertNoFPClass(SelectionDAG &DAG, const Instruction &I, SDValue Op)
void handleDebugDeclare(Value *Address, DILocalVariable *Variable, DIExpression *Expression, DebugLoc DL)
bool shouldKeepJumpConditionsTogether(const FunctionLoweringInfo &FuncInfo, const BranchInst &I, Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs, TargetLoweringBase::CondMergingParams Params) const
StatepointLoweringState StatepointLowering
State used while lowering a statepoint sequence (gc_statepoint, gc_relocate, and gc_result).
void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB, BranchProbability BranchProbToNext, Register Reg, SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB)
visitBitTestCase - this function produces one "bit test"
bool canTailCall(const CallBase &CB) const
void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, AttributeSet RetAttrs, bool IsPatchPoint)
Populate a CallLowerinInfo (into CLI) based on the properties of the call being lowered.
void CopyValueToVirtualRegister(const Value *V, Register Reg, ISD::NodeType ExtendType=ISD::ANY_EXTEND)
void salvageUnresolvedDbgValue(const Value *V, DanglingDebugInfo &DDI)
For the given dangling debuginfo record, perform last-ditch efforts to resolve the debuginfo to somet...
SmallVector< SDValue, 8 > PendingLoads
Loads are not emitted to the program immediately.
GCFunctionInfo * GFI
Garbage collection metadata for the function.
void init(GCFunctionInfo *gfi, BatchAAResults *BatchAA, AssumptionCache *AC, const TargetLibraryInfo *li, const TargetTransformInfo &TTI)
SDValue getRoot()
Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) items.
void ExportFromCurrentBlock(const Value *V)
ExportFromCurrentBlock - If this condition isn't known to be exported from the current basic block,...
void resolveOrClearDbgInfo()
Evict any dangling debug information, attempting to salvage it first.
std::pair< SDValue, SDValue > lowerInvokable(TargetLowering::CallLoweringInfo &CLI, const BasicBlock *EHPadBB=nullptr)
SDValue getMemoryRoot()
Return the current virtual root of the Selection DAG, flushing any PendingLoad items.
void resolveDanglingDebugInfo(const Value *V, SDValue Val)
If we saw an earlier dbg_value referring to V, generate the debug data structures now that we've seen...
void visit(const Instruction &I)
void dropDanglingDebugInfo(const DILocalVariable *Variable, const DIExpression *Expr)
If we have dangling debug info that describes Variable, or an overlapping part of variable considerin...
SDValue getCopyFromRegs(const Value *V, Type *Ty)
If there was virtual register allocated for the value V emit CopyFromReg of the specified type Ty.
void CopyToExportRegsIfNeeded(const Value *V)
CopyToExportRegsIfNeeded - If the given value has virtual registers created for it,...
void handleKillDebugValue(DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order)
Create a record for a kill location debug intrinsic.
void visitJumpTable(SwitchCG::JumpTable &JT)
visitJumpTable - Emit JumpTable node in the current MBB
SDValue getFPOperationRoot(fp::ExceptionBehavior EB)
Return the current virtual root of the Selection DAG, flushing PendingConstrainedFP or PendingConstra...
void visitJumpTableHeader(SwitchCG::JumpTable &JT, SwitchCG::JumpTableHeader &JTH, MachineBasicBlock *SwitchBB)
visitJumpTableHeader - This function emits necessary code to produce index in the JumpTable from swit...
void LowerCallSiteWithPtrAuthBundle(const CallBase &CB, const BasicBlock *EHPadBB)
static const unsigned LowestSDNodeOrder
Lowest valid SDNodeOrder.
FunctionLoweringInfo & FuncInfo
Information about the function as a whole.
void setValue(const Value *V, SDValue NewN)
void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, Instruction::BinaryOps Opc, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
const TargetLibraryInfo * LibInfo
bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB)
void visitSPDescriptorParent(StackProtectorDescriptor &SPD, MachineBasicBlock *ParentBB)
Codegen a new tail for a stack protector check ParentMBB which has had its tail spliced into a stack ...
bool handleDebugValue(ArrayRef< const Value * > Values, DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order, bool IsVariadic)
For a given list of Values, attempt to create and record a SDDbgValue in the SelectionDAG.
SDValue getControlRoot()
Similar to getRoot, but instead of flushing all the PendingLoad items, flush all the PendingExports (...
void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last)
When an MBB was split during scheduling, update the references that need to refer to the last resulti...
SDValue getValueImpl(const Value *V)
getValueImpl - Helper function for getValue and getNonRegisterValue.
void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB)
visitSwitchCase - Emits the necessary code to represent a single node in the binary search tree resul...
void visitSPDescriptorFailure(StackProtectorDescriptor &SPD)
Codegen the failure basic block for a stack protector check.
std::unique_ptr< FunctionLoweringInfo > FuncInfo
SmallPtrSet< const Instruction *, 4 > ElidedArgCopyInstrs
const TargetLowering * TLI
MachineRegisterInfo * RegInfo
std::unique_ptr< SwiftErrorValueTracking > SwiftError
virtual void emitFunctionEntryCode()
std::unique_ptr< SelectionDAGBuilder > SDB
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrnlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, const CallInst *CI) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrcpy(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dest, SDValue Src, MachinePointerInfo DestPtrInfo, MachinePointerInfo SrcPtrInfo, bool isStpcpy) const
Emit target-specific code that performs a strcpy or stpcpy, in cases where that is faster than a libc...
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemchr(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Src, SDValue Char, SDValue Length, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memchr, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, const CallInst *CI) const
Emit target-specific code that performs a memcmp/bcmp, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, MachinePointerInfo Op1PtrInfo, MachinePointerInfo Op2PtrInfo) const
Emit target-specific code that performs a strcmp, in cases where that is faster than a libcall.
virtual SDValue EmitTargetCodeForSetTag(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Addr, SDValue Size, MachinePointerInfo DstPtrInfo, bool ZeroData) const
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SDValue getExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT, unsigned Opcode)
Convert Op, which must be of integer type, to the integer type VT, by either any/sign/zero-extending ...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
const TargetSubtargetInfo & getSubtarget() const
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
const TargetLowering & getTargetLoweringInfo() const
static constexpr unsigned MaxRecursionDepth
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
const DataLayout & getDataLayout() const
SDValue getTargetFrameIndex(int FI, EVT VT)
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
MachineFunction & getMachineFunction() const
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVMContext * getContext() const
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void swap(SmallVectorImpl &RHS)
void resize(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Encapsulates all of the information needed to generate a stack protector check, and signals to isel w...
MachineBasicBlock * getSuccessMBB()
MachineBasicBlock * getFailureMBB()
MachineBasicBlock * getParentMBB()
bool shouldEmitFunctionBasedCheckStackProtector() const
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Multiway switch.
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Provides information about what library functions are available for the current target.
virtual Align getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Returns the desired alignment for ByVal or InAlloca aggregate function arguments in the caller parame...
virtual bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, EVT) const
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
EVT getMemValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
virtual bool useStackGuardXorFP() const
If this function returns true, stack protection checks should XOR the frame pointer (or whichever poi...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
virtual bool isLegalScaleForGatherScatter(uint64_t Scale, uint64_t ElemSize) const
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
virtual unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain targets require unusual breakdowns of certain types.
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
virtual Value * getSDagStackGuard(const Module &M) const
Return the variable that's previously inserted by insertSSPDeclarations, if any, otherwise return nul...
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const
Return the number of registers that this ValueType will eventually require.
unsigned getBitWidthForCttzElements(Type *RetTy, ElementCount EC, bool ZeroIsPoison, const ConstantRange *VScaleRange) const
Return the minimum number of bits required to hold the maximum possible number of trailing zero vecto...
virtual bool shouldExtendGSIndex(EVT VT, EVT &EltTy) const
Returns true if the index type for a masked gather/scatter requires extending.
virtual unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
Function * getSSPStackGuardCheck(const Module &M) const
If the target has a standard stack protection check function that performs validation and error handl...
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
LegalizeAction getFixedPointOperationAction(unsigned Op, EVT VT, unsigned Scale) const
Some fixed point operations may be natively supported by the target but only for specific scales.
MachineMemOperand::Flags getAtomicMemOperandFlags(const Instruction &AI, const DataLayout &DL) const
virtual bool getTgtMemIntrinsic(IntrinsicInfo &, const CallInst &, MachineFunction &, unsigned) const
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
virtual bool allowsMisalignedMemoryAccesses(EVT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *=nullptr) const
Determine if the target supports unaligned memory accesses.
bool isOperationCustom(unsigned Op, EVT VT) const
Return true if the operation uses custom lowering, regardless of whether the type is legal or not.
bool hasBigEndianPartOrdering(EVT VT, const DataLayout &DL) const
When splitting a value of the specified type into parts, does the Lo or Hi part come first?
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
virtual Align getABIAlignmentForCallingConv(Type *ArgTy, const DataLayout &DL) const
Certain targets have context sensitive alignment requirements, where one type has the alignment requi...
MachineMemOperand::Flags getVPIntrinsicMemOperandFlags(const VPIntrinsic &VPIntrin) const
virtual bool shouldExpandGetActiveLaneMask(EVT VT, EVT OpVT) const
Return true if the @llvm.get.active.lane.mask intrinsic should be expanded using generic code in Sele...
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
MachineMemOperand::Flags getLoadMemOperandFlags(const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC=nullptr, const TargetLibraryInfo *LibInfo=nullptr) const
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
MVT getProgramPointerTy(const DataLayout &DL) const
Return the type for code pointers, which is determined by the program address space specified through...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
virtual bool shouldExpandVectorMatch(EVT VT, unsigned SearchSize) const
Return true if the @llvm.experimental.vector.match intrinsic should be expanded for vector type ‘VT’ ...
virtual MVT getFenceOperandTy(const DataLayout &DL) const
Return the type for operands of fence.
virtual bool shouldExpandGetVectorLength(EVT CountVT, unsigned VF, bool IsScalable) const
bool isOperationLegalOrCustom(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
virtual MVT hasFastEqualityCompare(unsigned NumBits) const
Return the preferred operand type if the target has a quick way to compare integer values of the give...
MachineMemOperand::Flags getStoreMemOperandFlags(const StoreInst &SI, const DataLayout &DL) const
virtual bool shouldExpandCttzElements(EVT VT) const
Return true if the @llvm.experimental.cttz.elts intrinsic should be expanded using generic code in Se...
virtual bool signExtendConstant(const ConstantInt *C) const
Return true if this constant should be sign extended when promoting to a larger type.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
virtual Register getExceptionPointerRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception address on entry to an ...
bool supportsUnalignedAtomics() const
Whether the target supports unaligned atomic operations.
std::vector< ArgListEntry > ArgListTy
bool isBeneficialToExpandPowI(int64_t Exponent, bool OptForSize) const
Return true if it is beneficial to expand an @llvm.powi.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual Register getExceptionSelectorRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception typeid on entry to a la...
virtual MVT getPointerMemTy(const DataLayout &DL, uint32_t AS=0) const
Return the in-memory pointer type for the given address space, defaults to the pointer type from the ...
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
virtual MVT getVPExplicitVectorLengthTy() const
Returns the type to be used for the EVL/AVL operand of VP nodes: ISD::VP_ADD, ISD::VP_SUB,...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool supportKCFIBundles() const
Return true if the target supports kcfi operand bundles.
virtual bool supportPtrAuthBundles() const
Return true if the target supports ptrauth operand bundles.
virtual bool supportSwiftError() const
Return true if the target supports swifterror attribute.
virtual SDValue visitMaskedLoad(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue &NewLoad, SDValue Ptr, SDValue PassThru, SDValue Mask) const
virtual SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, const SDLoc &DL) const
virtual EVT getTypeForExtReturn(LLVMContext &Context, EVT VT, ISD::NodeType) const
Return the type that should be used to zero or sign extend a zeroext/signext integer return value.
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT, ArrayRef< SDValue > Ops, MakeLibCallOptions CallOptions, const SDLoc &dl, SDValue Chain=SDValue()) const
Returns a pair of (return value, chain).
virtual InlineAsm::ConstraintCode getInlineAsmMemConstraint(StringRef ConstraintCode) const
std::vector< AsmOperandInfo > AsmOperandInfoVector
SDValue expandIS_FPCLASS(EVT ResultVT, SDValue Op, FPClassTest Test, SDNodeFlags Flags, const SDLoc &DL, SelectionDAG &DAG) const
Expand check for floating point class.
virtual SDValue prepareVolatileOrAtomicLoad(SDValue Chain, const SDLoc &DL, SelectionDAG &DAG) const
This callback is used to prepare for a volatile or atomic load.
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
virtual bool splitValueIntoRegisterParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, std::optional< CallingConv::ID > CC) const
Target-specific splitting of values into parts that fit a register storing a legal type.
virtual SDValue joinRegisterPartsIntoValue(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, std::optional< CallingConv::ID > CC) const
Target-specific combining of register parts into its original value.
virtual SDValue LowerCall(CallLoweringInfo &, SmallVectorImpl< SDValue > &) const
This hook must be implemented to lower calls into the specified DAG.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual SDValue LowerAsmOutputForConstraint(SDValue &Chain, SDValue &Glue, const SDLoc &DL, const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, const CallBase &Call) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const
This callback is invoked for operations that are unsupported by the target, which are registered to u...
virtual bool functionArgumentNeedsConsecutiveRegisters(Type *Ty, CallingConv::ID CallConv, bool isVarArg, const DataLayout &DL) const
For some targets, an LLVM struct type must be broken down into multiple simple types,...
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo,...
virtual void CollectTargetIntrinsicOperands(const CallInst &I, SmallVectorImpl< SDValue > &Ops, SelectionDAG &DAG) const
virtual SDValue visitMaskedStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue Ptr, SDValue Val, SDValue Mask) const
virtual bool useLoadStackGuardNode(const Module &M) const
If this function returns true, SelectionDAGBuilder emits a LOAD_STACK_GUARD node when it is lowering ...
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
virtual void LowerOperationWrapper(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const
This callback is invoked by the type legalizer to legalize nodes with an illegal operand type but leg...
virtual bool isInlineAsmTargetBranch(const SmallVectorImpl< StringRef > &AsmStrs, unsigned OpNo) const
On x86, return true if the operand with index OpNo is a CALL or JUMP instruction, which can use eithe...
virtual MVT getJumpTableRegTy(const DataLayout &DL) const
virtual bool CanLowerReturn(CallingConv::ID, MachineFunction &, bool, const SmallVectorImpl< ISD::OutputArg > &, LLVMContext &, const Type *RetTy) const
This hook should be implemented to check whether the return values described by the Outs array can fi...
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
unsigned getID() const
Return the register class ID number.
iterator begin() const
begin/end - Return all of the registers in this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
@ TCK_Latency
The latency of instruction.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
Definition Type.cpp:180
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:293
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:234
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:300
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_iterator op_begin()
Definition User.h:284
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
op_iterator op_end()
Definition User.h:286
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
LLVM_ABI CmpInst::Predicate getPredicate() const
This is the common base class for vector predication intrinsics.
static LLVM_ABI std::optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
LLVM_ABI MaybeAlign getPointerAlignment() const
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:701
bool use_empty() const
Definition Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
const ParentTy * getParent() const
Definition ilist_node.h:34
A raw_ostream that writes to an std::string.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:780
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:504
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ LOOP_DEPENDENCE_RAW_MASK
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition ISDOpcodes.h:163
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:593
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:771
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:387
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:393
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:841
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ EH_SJLJ_SETUP_DISPATCH
OUTCHAIN = EH_SJLJ_SETUP_DISPATCH(INCHAIN) The target initializes the dispatch table here.
Definition ISDOpcodes.h:167
@ GlobalAddress
Definition ISDOpcodes.h:88
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:868
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:577
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:744
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:521
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition ISDOpcodes.h:508
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:400
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:151
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:832
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition ISDOpcodes.h:117
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:779
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:628
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:534
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:541
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:369
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:784
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:242
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:669
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:225
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:958
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:762
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ PtrAuthGlobalAddress
A ptrauth constant.
Definition ISDOpcodes.h:100
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:607
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:134
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:569
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:838
@ LOCAL_RECOVER
LOCAL_RECOVER - Represents the llvm.localrecover intrinsic.
Definition ISDOpcodes.h:130
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:724
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:633
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:406
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:793
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:145
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:493
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:914
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:736
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:200
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:732
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition ISDOpcodes.h:420
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:236
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:558
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition ISDOpcodes.h:654
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:947
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:696
@ SPONENTRY
SPONENTRY - Represents the llvm.sponentry intrinsic.
Definition ISDOpcodes.h:122
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:933
@ EH_SJLJ_SETJMP
RESULT, OUTCHAIN = EH_SJLJ_SETJMP(INCHAIN, buffer) This corresponds to the eh.sjlj....
Definition ISDOpcodes.h:157
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:844
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:527
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:617
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:208
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:549
@ LOOP_DEPENDENCE_WAR_MASK
Set rounding mode.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Offsets
Offsets in bytes from the start of the input buffer.
std::pair< JumpTableHeader, JumpTable > JumpTableBlock
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::vector< CaseCluster > CaseClusterVector
@ CC_Range
A cluster of adjacent case labels with the same destination, or just one case.
@ CC_JumpTable
A cluster of cases suitable for jump table lowering.
@ CC_BitTests
A cluster of cases suitable for bit test lowering.
SmallVector< SwitchWorkListItem, 4 > SwitchWorkList
CaseClusterVector::iterator CaseClusterIt
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebStrict
This corresponds to "fpexcept.strict".
Definition FPEnv.h:42
@ ebMayTrap
This corresponds to "fpexcept.maytrap".
Definition FPEnv.h:41
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
constexpr float log2ef
Definition MathExtras.h:51
constexpr double e
constexpr float ln2f
Definition MathExtras.h:49
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1655
LLVM_ABI void GetReturnInfo(CallingConv::ID CC, Type *ReturnType, AttributeList attr, SmallVectorImpl< ISD::OutputArg > &Outs, const TargetLowering &TLI, const DataLayout &DL)
Given an LLVM IR type and return type attributes, compute the return value EVTs and flags,...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs=nullptr, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition Analysis.cpp:119
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
auto successors(const MachineBasicBlock *BB)
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
static ConstantRange getRange(Value *Op, SCCPSolver &Solver, const SmallPtrSetImpl< Value * > &InsertedValues)
Helper for getting ranges from Solver.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2136
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
auto cast_or_null(const Y &Val)
Definition Casting.h:714
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:546
gep_type_iterator gep_type_end(const User *GEP)
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
Definition STLExtras.h:1150
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
void ComputeValueTypes(const DataLayout &DL, Type *Ty, SmallVectorImpl< Type * > &Types, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
Given an LLVM IR type, compute non-aggregate subtypes.
Definition Analysis.cpp:72
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
LLVM_ABI llvm::SmallVector< int, 16 > createStrideMask(unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_FMINNUM
Unsigned maximum.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
detail::zippy< detail::zip_first, T, U, Args... > zip_first(T &&t, U &&u, Args &&...args)
zip iterator that, for the sake of efficiency, assumes the first iteratee to be the shortest.
Definition STLExtras.h:852
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
generic_gep_type_iterator<> gep_type_iterator
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
auto succ_size(const MachineBasicBlock *BB)
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition STLExtras.h:300
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred)
getFCmpCondCode - Return the ISD condition code corresponding to the given LLVM IR floating-point con...
Definition Analysis.cpp:207
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI Value * salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Ops, SmallVectorImpl< Value * > &AdditionalValues)
Definition Local.cpp:2274
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Global
Append to llvm.global_dtors.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
LLVM_ABI llvm::SmallVector< int, 16 > createInterleaveMask(unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ And
Bitwise or logical AND of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp: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
ArrayRef(const T &OneElt) -> ArrayRef< T >
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition FPEnv.cpp:25
gep_type_iterator gep_type_begin(const User *GEP)
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2120
GlobalValue * ExtractTypeInfo(Value *V)
ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
Definition Analysis.cpp:185
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1897
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2108
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
unsigned ComputeLinearIndex(Type *Ty, const unsigned *Indices, const unsigned *IndicesEnd, unsigned CurIndex=0)
Compute the linearized index of a member in a nested aggregate/struct/array.
Definition Analysis.cpp:33
T bit_floor(T Value)
Returns the largest integral power of two no greater than Value if Value is nonzero.
Definition bit.h:330
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:180
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
#define N
#define NC
Definition regutils.h:42
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:395
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
uint64_t getScalarStoreSize() const
Definition ValueTypes.h:402
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isRISCVVectorTuple() const
Return true if this is a vector value type.
Definition ValueTypes.h:179
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:381
bool isFixedLengthVector() const
Definition ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:157
EVT changeVectorElementType(EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:102
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
InputArg - This struct carries flags and type information about a single incoming (formal) argument o...
static const unsigned NoArgIndex
Sentinel value for implicit machine-level input arguments.
OutputArg - This struct carries flags and a value for a single outgoing (actual) argument or outgoing...
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber/label.
Definition InlineAsm.h:128
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
A lightweight accessor for an operand bundle meant to be passed around by value.
This struct represents the registers (physical or virtual) that a particular set of values is assigne...
SmallVector< std::pair< Register, TypeSize >, 4 > getRegsAndSizes() const
Return a list of registers and their sizes.
RegsForValue()=default
SmallVector< unsigned, 4 > RegCount
This list holds the number of registers for each value.
SmallVector< EVT, 4 > ValueVTs
The value types of the values, which may not be legal, and may need be promoted or synthesized from o...
SmallVector< Register, 4 > Regs
This list holds the registers assigned to the values.
void AddInlineAsmOperands(InlineAsm::Kind Code, bool HasMatching, unsigned MatchingIdx, const SDLoc &dl, SelectionDAG &DAG, std::vector< SDValue > &Ops) const
Add this value to the specified inlineasm node operand list.
SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr) const
Emit a series of CopyFromReg nodes that copies from this value and returns the result as a ValueVTs v...
SmallVector< MVT, 4 > RegVTs
The value types of the registers.
void getCopyToRegs(SDValue Val, SelectionDAG &DAG, const SDLoc &dl, SDValue &Chain, SDValue *Glue, const Value *V=nullptr, ISD::NodeType PreferredExtendType=ISD::ANY_EXTEND) const
Emit a series of CopyToReg nodes that copies the specified value into the registers specified by this...
std::optional< CallingConv::ID > CallConv
Records if this value needs to be treated in an ABI dependant manner, different to normal type legali...
bool occupiesMultipleRegs() const
Check if the total RegCount is greater than one.
These are IR-level optimization flags that may be propagated to SDNodes.
void copyFMF(const FPMathOperator &FPMO)
Propagate the fast-math-flags from an IR FPMathOperator.
void setUnpredictable(bool b)
bool hasAllowReassociation() const
void setNoUnsignedWrap(bool b)
void setNoSignedWrap(bool b)
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:271
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 & setDeactivationSymbol(GlobalValue *Sym)
CallLoweringInfo & setCFIType(const ConstantInt *Type)
SmallVector< ISD::InputArg, 32 > Ins
Type * OrigRetTy
Original unlegalized return type.
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setIsPatchPoint(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setIsPreallocated(bool Value=true)
CallLoweringInfo & setConvergenceControlToken(SDValue Token)
SmallVector< ISD::OutputArg, 32 > Outs
Type * RetTy
Same as OrigRetTy, or partially legalized for soft float libcalls.
CallLoweringInfo & setChain(SDValue InChain)
CallLoweringInfo & setPtrAuth(PtrAuthInfo Value)
CallLoweringInfo & setCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList, AttributeSet ResultAttrs={})
This structure is used to pass arguments to makeLibCall function.
MakeLibCallOptions & setDiscardResult(bool Value=true)
This structure contains the information necessary for lowering pointer-authenticating indirect calls.
void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)