LLVM 23.0.0git
SelectionDAGBuilder.cpp
Go to the documentation of this file.
1//===- SelectionDAGBuilder.cpp - Selection-DAG building -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements routines for translating from LLVM IR into SelectionDAG IR.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SelectionDAGBuilder.h"
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/Twine.h"
26#include "llvm/Analysis/Loads.h"
58#include "llvm/IR/Argument.h"
59#include "llvm/IR/Attributes.h"
60#include "llvm/IR/BasicBlock.h"
61#include "llvm/IR/CFG.h"
62#include "llvm/IR/CallingConv.h"
63#include "llvm/IR/Constant.h"
65#include "llvm/IR/Constants.h"
66#include "llvm/IR/DataLayout.h"
67#include "llvm/IR/DebugInfo.h"
72#include "llvm/IR/Function.h"
74#include "llvm/IR/InlineAsm.h"
75#include "llvm/IR/InstrTypes.h"
78#include "llvm/IR/Intrinsics.h"
79#include "llvm/IR/IntrinsicsAArch64.h"
80#include "llvm/IR/IntrinsicsAMDGPU.h"
81#include "llvm/IR/IntrinsicsWebAssembly.h"
82#include "llvm/IR/LLVMContext.h"
84#include "llvm/IR/Metadata.h"
85#include "llvm/IR/Module.h"
86#include "llvm/IR/Operator.h"
88#include "llvm/IR/Statepoint.h"
89#include "llvm/IR/Type.h"
90#include "llvm/IR/User.h"
91#include "llvm/IR/Value.h"
92#include "llvm/MC/MCContext.h"
97#include "llvm/Support/Debug.h"
105#include <cstddef>
106#include <limits>
107#include <optional>
108#include <tuple>
109
110using namespace llvm;
111using namespace PatternMatch;
112using namespace SwitchCG;
113
114#define DEBUG_TYPE "isel"
115
116/// LimitFloatPrecision - Generate low-precision inline sequences for
117/// some float libcalls (6, 8 or 12 bits).
118static unsigned LimitFloatPrecision;
119
120static cl::opt<bool>
121 InsertAssertAlign("insert-assert-align", cl::init(true),
122 cl::desc("Insert the experimental `assertalign` node."),
124
126 LimitFPPrecision("limit-float-precision",
127 cl::desc("Generate low-precision inline sequences "
128 "for some float libcalls"),
130 cl::init(0));
131
133 "switch-peel-threshold", cl::Hidden, cl::init(66),
134 cl::desc("Set the case probability threshold for peeling the case from a "
135 "switch statement. A value greater than 100 will void this "
136 "optimization"));
137
138// Limit the width of DAG chains. This is important in general to prevent
139// DAG-based analysis from blowing up. For example, alias analysis and
140// load clustering may not complete in reasonable time. It is difficult to
141// recognize and avoid this situation within each individual analysis, and
142// future analyses are likely to have the same behavior. Limiting DAG width is
143// the safe approach and will be especially important with global DAGs.
144//
145// MaxParallelChains default is arbitrarily high to avoid affecting
146// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
147// sequence over this should have been converted to llvm.memcpy by the
148// frontend. It is easy to induce this behavior with .ll code such as:
149// %buffer = alloca [4096 x i8]
150// %data = load [4096 x i8]* %argPtr
151// store [4096 x i8] %data, [4096 x i8]* %buffer
152static const unsigned MaxParallelChains = 64;
153
155 const SDValue *Parts, unsigned NumParts,
156 MVT PartVT, EVT ValueVT, const Value *V,
157 SDValue InChain,
158 std::optional<CallingConv::ID> CC);
159
160/// getCopyFromParts - Create a value that contains the specified legal parts
161/// combined into the value they represent. If the parts combine to a type
162/// larger than ValueVT then AssertOp can be used to specify whether the extra
163/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
164/// (ISD::AssertSext).
165static SDValue
166getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts,
167 unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V,
168 SDValue InChain,
169 std::optional<CallingConv::ID> CC = std::nullopt,
170 std::optional<ISD::NodeType> AssertOp = std::nullopt) {
171 // Let the target assemble the parts if it wants to
172 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
173 if (SDValue Val = TLI.joinRegisterPartsIntoValue(DAG, DL, Parts, NumParts,
174 PartVT, ValueVT, CC))
175 return Val;
176
177 if (ValueVT.isVector())
178 return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT, V,
179 InChain, CC);
180
181 assert(NumParts > 0 && "No parts to assemble!");
182 SDValue Val = Parts[0];
183
184 if (NumParts > 1) {
185 // Assemble the value from multiple parts.
186 if (ValueVT.isInteger()) {
187 unsigned PartBits = PartVT.getSizeInBits();
188 unsigned ValueBits = ValueVT.getSizeInBits();
189
190 // Assemble the power of 2 part.
191 unsigned RoundParts = llvm::bit_floor(NumParts);
192 unsigned RoundBits = PartBits * RoundParts;
193 EVT RoundVT = RoundBits == ValueBits ?
194 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
195 SDValue Lo, Hi;
196
197 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
198
199 if (RoundParts > 2) {
200 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, PartVT, HalfVT, V,
201 InChain);
202 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, RoundParts / 2,
203 PartVT, HalfVT, V, InChain);
204 } else {
205 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
206 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
207 }
208
209 if (DAG.getDataLayout().isBigEndian())
210 std::swap(Lo, Hi);
211
212 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
213
214 if (RoundParts < NumParts) {
215 // Assemble the trailing non-power-of-2 part.
216 unsigned OddParts = NumParts - RoundParts;
217 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
218 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts, OddParts, PartVT,
219 OddVT, V, InChain, CC);
220
221 // Combine the round and odd parts.
222 Lo = Val;
223 if (DAG.getDataLayout().isBigEndian())
224 std::swap(Lo, Hi);
225 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
226 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
227 Hi = DAG.getNode(
228 ISD::SHL, DL, TotalVT, Hi,
229 DAG.getShiftAmountConstant(Lo.getValueSizeInBits(), TotalVT, DL));
230 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
231 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
232 }
233 } else if (PartVT.isFloatingPoint()) {
234 // FP split into multiple FP parts (for ppcf128)
235 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
236 "Unexpected split");
237 SDValue Lo, Hi;
238 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
239 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
240 if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
241 std::swap(Lo, Hi);
242 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
243 } else {
244 // FP split into integer parts (soft fp)
245 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
246 !PartVT.isVector() && "Unexpected split");
247 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
248 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V,
249 InChain, CC);
250 }
251 }
252
253 // There is now one part, held in Val. Correct it to match ValueVT.
254 // PartEVT is the type of the register class that holds the value.
255 // ValueVT is the type of the inline asm operation.
256 EVT PartEVT = Val.getValueType();
257
258 if (PartEVT == ValueVT)
259 return Val;
260
261 if (PartEVT.isInteger() && ValueVT.isFloatingPoint() &&
262 ValueVT.bitsLT(PartEVT)) {
263 // For an FP value in an integer part, we need to truncate to the right
264 // width first.
265 PartEVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
266 Val = DAG.getNode(ISD::TRUNCATE, DL, PartEVT, Val);
267 }
268
269 // Handle types that have the same size.
270 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
271 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
272
273 // Handle types with different sizes.
274 if (PartEVT.isInteger() && ValueVT.isInteger()) {
275 if (ValueVT.bitsLT(PartEVT)) {
276 // For a truncate, see if we have any information to
277 // indicate whether the truncated bits will always be
278 // zero or sign-extension.
279 if (AssertOp)
280 Val = DAG.getNode(*AssertOp, DL, PartEVT, Val,
281 DAG.getValueType(ValueVT));
282 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
283 }
284 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
285 }
286
287 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
288 // FP_ROUND's are always exact here.
289 if (ValueVT.bitsLT(Val.getValueType())) {
290
291 SDValue NoChange =
293
294 if (DAG.getMachineFunction().getFunction().getAttributes().hasFnAttr(
295 llvm::Attribute::StrictFP)) {
296 return DAG.getNode(ISD::STRICT_FP_ROUND, DL,
297 DAG.getVTList(ValueVT, MVT::Other), InChain, Val,
298 NoChange);
299 }
300
301 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, NoChange);
302 }
303
304 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
305 }
306
307 // Handle MMX to a narrower integer type by bitcasting MMX to integer and
308 // then truncating.
309 if (PartEVT == MVT::x86mmx && ValueVT.isInteger() &&
310 ValueVT.bitsLT(PartEVT)) {
311 Val = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Val);
312 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
313 }
314
315 report_fatal_error("Unknown mismatch in getCopyFromParts!");
316}
317
319 const Twine &ErrMsg) {
321 if (!I)
322 return Ctx.emitError(ErrMsg);
323
324 if (const CallInst *CI = dyn_cast<CallInst>(I))
325 if (CI->isInlineAsm()) {
326 return Ctx.diagnose(DiagnosticInfoInlineAsm(
327 *CI, ErrMsg + ", possible invalid constraint for vector type"));
328 }
329
330 return Ctx.emitError(I, ErrMsg);
331}
332
333/// getCopyFromPartsVector - Create a value that contains the specified legal
334/// parts combined into the value they represent. If the parts combine to a
335/// type larger than ValueVT then AssertOp can be used to specify whether the
336/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
337/// ValueVT (ISD::AssertSext).
339 const SDValue *Parts, unsigned NumParts,
340 MVT PartVT, EVT ValueVT, const Value *V,
341 SDValue InChain,
342 std::optional<CallingConv::ID> CallConv) {
343 assert(ValueVT.isVector() && "Not a vector value");
344 assert(NumParts > 0 && "No parts to assemble!");
345 const bool IsABIRegCopy = CallConv.has_value();
346
347 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
348 SDValue Val = Parts[0];
349
350 // Handle a multi-element vector.
351 if (NumParts > 1) {
352 EVT IntermediateVT;
353 MVT RegisterVT;
354 unsigned NumIntermediates;
355 unsigned NumRegs;
356
357 if (IsABIRegCopy) {
359 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT,
360 NumIntermediates, RegisterVT);
361 } else {
362 NumRegs =
363 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
364 NumIntermediates, RegisterVT);
365 }
366
367 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
368 NumParts = NumRegs; // Silence a compiler warning.
369 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
370 assert(RegisterVT.getSizeInBits() ==
371 Parts[0].getSimpleValueType().getSizeInBits() &&
372 "Part type sizes don't match!");
373
374 // Assemble the parts into intermediate operands.
375 SmallVector<SDValue, 8> Ops(NumIntermediates);
376 if (NumIntermediates == NumParts) {
377 // If the register was not expanded, truncate or copy the value,
378 // as appropriate.
379 for (unsigned i = 0; i != NumParts; ++i)
380 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, PartVT, IntermediateVT,
381 V, InChain, CallConv);
382 } else if (NumParts > 0) {
383 // If the intermediate type was expanded, build the intermediate
384 // operands from the parts.
385 assert(NumParts % NumIntermediates == 0 &&
386 "Must expand into a divisible number of parts!");
387 unsigned Factor = NumParts / NumIntermediates;
388 for (unsigned i = 0; i != NumIntermediates; ++i)
389 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, PartVT,
390 IntermediateVT, V, InChain, CallConv);
391 }
392
393 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
394 // intermediate operands.
395 EVT BuiltVectorTy =
396 IntermediateVT.isVector()
398 *DAG.getContext(), IntermediateVT.getScalarType(),
399 IntermediateVT.getVectorElementCount() * NumParts)
401 IntermediateVT.getScalarType(),
402 NumIntermediates);
403 Val = DAG.getNode(IntermediateVT.isVector() ? ISD::CONCAT_VECTORS
405 DL, BuiltVectorTy, Ops);
406 }
407
408 // There is now one part, held in Val. Correct it to match ValueVT.
409 EVT PartEVT = Val.getValueType();
410
411 if (PartEVT == ValueVT)
412 return Val;
413
414 if (PartEVT.isVector()) {
415 // Vector/Vector bitcast.
416 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
417 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
418
419 // If the parts vector has more elements than the value vector, then we
420 // have a vector widening case (e.g. <2 x float> -> <4 x float>).
421 // Extract the elements we want.
422 if (PartEVT.getVectorElementCount() != ValueVT.getVectorElementCount()) {
425 (PartEVT.getVectorElementCount().isScalable() ==
426 ValueVT.getVectorElementCount().isScalable()) &&
427 "Cannot narrow, it would be a lossy transformation");
428 PartEVT =
430 ValueVT.getVectorElementCount());
431 Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, PartEVT, Val,
432 DAG.getVectorIdxConstant(0, DL));
433 if (PartEVT == ValueVT)
434 return Val;
435 if (PartEVT.isInteger() && ValueVT.isFloatingPoint())
436 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
437
438 // Vector/Vector bitcast (e.g. <2 x bfloat> -> <2 x half>).
439 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
440 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
441 }
442
443 // Promoted vector extract
444 return DAG.getAnyExtOrTrunc(Val, DL, ValueVT);
445 }
446
447 // Trivial bitcast if the types are the same size and the destination
448 // vector type is legal.
449 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
450 TLI.isTypeLegal(ValueVT))
451 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
452
453 if (ValueVT.getVectorNumElements() != 1) {
454 // Certain ABIs require that vectors are passed as integers. For vectors
455 // are the same size, this is an obvious bitcast.
456 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) {
457 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
458 } else if (ValueVT.bitsLT(PartEVT)) {
459 const uint64_t ValueSize = ValueVT.getFixedSizeInBits();
460 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
461 // Drop the extra bits.
462 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
463 return DAG.getBitcast(ValueVT, Val);
464 }
465
467 *DAG.getContext(), V, "non-trivial scalar-to-vector conversion");
468 return DAG.getUNDEF(ValueVT);
469 }
470
471 // Handle cases such as i8 -> <1 x i1>
472 EVT ValueSVT = ValueVT.getVectorElementType();
473 if (ValueVT.getVectorNumElements() == 1 && ValueSVT != PartEVT) {
474 unsigned ValueSize = ValueSVT.getSizeInBits();
475 if (ValueSize == PartEVT.getSizeInBits()) {
476 Val = DAG.getNode(ISD::BITCAST, DL, ValueSVT, Val);
477 } else if (ValueSVT.isFloatingPoint() && PartEVT.isInteger()) {
478 // It's possible a scalar floating point type gets softened to integer and
479 // then promoted to a larger integer. If PartEVT is the larger integer
480 // we need to truncate it and then bitcast to the FP type.
481 assert(ValueSVT.bitsLT(PartEVT) && "Unexpected types");
482 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
483 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
484 Val = DAG.getBitcast(ValueSVT, Val);
485 } else {
486 Val = ValueVT.isFloatingPoint()
487 ? DAG.getFPExtendOrRound(Val, DL, ValueSVT)
488 : DAG.getAnyExtOrTrunc(Val, DL, ValueSVT);
489 }
490 }
491
492 return DAG.getBuildVector(ValueVT, DL, Val);
493}
494
495static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl,
496 SDValue Val, SDValue *Parts, unsigned NumParts,
497 MVT PartVT, const Value *V,
498 std::optional<CallingConv::ID> CallConv);
499
500/// getCopyToParts - Create a series of nodes that contain the specified value
501/// split into legal parts. If the parts contain more bits than Val, then, for
502/// integers, ExtendKind can be used to specify how to generate the extra bits.
503static void
505 unsigned NumParts, MVT PartVT, const Value *V,
506 std::optional<CallingConv::ID> CallConv = std::nullopt,
507 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
508 // Let the target split the parts if it wants to
509 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
510 if (TLI.splitValueIntoRegisterParts(DAG, DL, Val, Parts, NumParts, PartVT,
511 CallConv))
512 return;
513 EVT ValueVT = Val.getValueType();
514
515 // Handle the vector case separately.
516 if (ValueVT.isVector())
517 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V,
518 CallConv);
519
520 unsigned OrigNumParts = NumParts;
522 "Copying to an illegal type!");
523
524 if (NumParts == 0)
525 return;
526
527 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
528 EVT PartEVT = PartVT;
529 if (PartEVT == ValueVT) {
530 assert(NumParts == 1 && "No-op copy with multiple parts!");
531 Parts[0] = Val;
532 return;
533 }
534
535 unsigned PartBits = PartVT.getSizeInBits();
536 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
537 // If the parts cover more bits than the value has, promote the value.
538 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
539 assert(NumParts == 1 && "Do not know what to promote to!");
540 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
541 } else {
542 if (ValueVT.isFloatingPoint()) {
543 // FP values need to be bitcast, then extended if they are being put
544 // into a larger container.
545 ValueVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
546 Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
547 }
548 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
549 ValueVT.isInteger() &&
550 "Unknown mismatch!");
551 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
552 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
553 if (PartVT == MVT::x86mmx)
554 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
555 }
556 } else if (PartBits == ValueVT.getSizeInBits()) {
557 // Different types of the same size.
558 assert(NumParts == 1 && PartEVT != ValueVT);
559 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
560 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
561 // If the parts cover less bits than value has, truncate the value.
562 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
563 ValueVT.isInteger() &&
564 "Unknown mismatch!");
565 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
566 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
567 if (PartVT == MVT::x86mmx)
568 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
569 }
570
571 // The value may have changed - recompute ValueVT.
572 ValueVT = Val.getValueType();
573 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
574 "Failed to tile the value with PartVT!");
575
576 if (NumParts == 1) {
577 if (PartEVT != ValueVT) {
579 "scalar-to-vector conversion failed");
580 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
581 }
582
583 Parts[0] = Val;
584 return;
585 }
586
587 // Expand the value into multiple parts.
588 if (NumParts & (NumParts - 1)) {
589 // The number of parts is not a power of 2. Split off and copy the tail.
590 assert(PartVT.isInteger() && ValueVT.isInteger() &&
591 "Do not know what to expand to!");
592 unsigned RoundParts = llvm::bit_floor(NumParts);
593 unsigned RoundBits = RoundParts * PartBits;
594 unsigned OddParts = NumParts - RoundParts;
595 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
596 DAG.getShiftAmountConstant(RoundBits, ValueVT, DL));
597
598 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V,
599 CallConv);
600
601 if (DAG.getDataLayout().isBigEndian())
602 // The odd parts were reversed by getCopyToParts - unreverse them.
603 std::reverse(Parts + RoundParts, Parts + NumParts);
604
605 NumParts = RoundParts;
606 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
607 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
608 }
609
610 // The number of parts is a power of 2. Repeatedly bisect the value using
611 // EXTRACT_ELEMENT.
612 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
614 ValueVT.getSizeInBits()),
615 Val);
616
617 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
618 for (unsigned i = 0; i < NumParts; i += StepSize) {
619 unsigned ThisBits = StepSize * PartBits / 2;
620 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
621 SDValue &Part0 = Parts[i];
622 SDValue &Part1 = Parts[i+StepSize/2];
623
624 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
625 ThisVT, Part0, DAG.getIntPtrConstant(1, DL));
626 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
627 ThisVT, Part0, DAG.getIntPtrConstant(0, DL));
628
629 if (ThisBits == PartBits && ThisVT != PartVT) {
630 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
631 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
632 }
633 }
634 }
635
636 if (DAG.getDataLayout().isBigEndian())
637 std::reverse(Parts, Parts + OrigNumParts);
638}
639
641 const SDLoc &DL, EVT PartVT) {
642 if (!PartVT.isVector())
643 return SDValue();
644
645 EVT ValueVT = Val.getValueType();
646 EVT PartEVT = PartVT.getVectorElementType();
647 EVT ValueEVT = ValueVT.getVectorElementType();
648 ElementCount PartNumElts = PartVT.getVectorElementCount();
649 ElementCount ValueNumElts = ValueVT.getVectorElementCount();
650
651 // We only support widening vectors with equivalent element types and
652 // fixed/scalable properties. If a target needs to widen a fixed-length type
653 // to a scalable one, it should be possible to use INSERT_SUBVECTOR below.
654 if (ElementCount::isKnownLE(PartNumElts, ValueNumElts) ||
655 PartNumElts.isScalable() != ValueNumElts.isScalable())
656 return SDValue();
657
658 // Have a try for bf16 because some targets share its ABI with fp16.
659 if (ValueEVT == MVT::bf16 && PartEVT == MVT::f16) {
661 "Cannot widen to illegal type");
662 Val = DAG.getNode(
664 ValueVT.changeVectorElementType(*DAG.getContext(), MVT::f16), Val);
665 } else if (PartEVT != ValueEVT) {
666 return SDValue();
667 }
668
669 // Widening a scalable vector to another scalable vector is done by inserting
670 // the vector into a larger undef one.
671 if (PartNumElts.isScalable())
672 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT),
673 Val, DAG.getVectorIdxConstant(0, DL));
674
675 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
676 // undef elements.
678 DAG.ExtractVectorElements(Val, Ops);
679 SDValue EltUndef = DAG.getUNDEF(PartEVT);
680 Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef);
681
682 // FIXME: Use CONCAT for 2x -> 4x.
683 return DAG.getBuildVector(PartVT, DL, Ops);
684}
685
686/// getCopyToPartsVector - Create a series of nodes that contain the specified
687/// value split into legal parts.
688static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL,
689 SDValue Val, SDValue *Parts, unsigned NumParts,
690 MVT PartVT, const Value *V,
691 std::optional<CallingConv::ID> CallConv) {
692 EVT ValueVT = Val.getValueType();
693 assert(ValueVT.isVector() && "Not a vector");
694 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
695 const bool IsABIRegCopy = CallConv.has_value();
696
697 if (NumParts == 1) {
698 EVT PartEVT = PartVT;
699 if (PartEVT == ValueVT) {
700 // Nothing to do.
701 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
702 // Bitconvert vector->vector case.
703 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
704 } else if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, PartVT)) {
705 Val = Widened;
706 } else if (PartVT.isVector() &&
708 ValueVT.getVectorElementType()) &&
709 PartEVT.getVectorElementCount() ==
710 ValueVT.getVectorElementCount()) {
711
712 // Promoted vector extract
713 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
714 } else if (PartEVT.isVector() &&
715 PartEVT.getVectorElementType() !=
716 ValueVT.getVectorElementType() &&
717 TLI.getTypeAction(*DAG.getContext(), ValueVT) ==
719 // Combination of widening and promotion.
720 EVT WidenVT =
722 PartVT.getVectorElementCount());
723 SDValue Widened = widenVectorToPartType(DAG, Val, DL, WidenVT);
724 Val = DAG.getAnyExtOrTrunc(Widened, DL, PartVT);
725 } else {
726 // Don't extract an integer from a float vector. This can happen if the
727 // FP type gets softened to integer and then promoted. The promotion
728 // prevents it from being picked up by the earlier bitcast case.
729 if (ValueVT.getVectorElementCount().isScalar() &&
730 (!ValueVT.isFloatingPoint() || !PartVT.isInteger())) {
731 // If we reach this condition and PartVT is FP, this means that
732 // ValueVT is also FP and both have a different size, otherwise we
733 // would have bitcasted them. Producing an EXTRACT_VECTOR_ELT here
734 // would be invalid since that would mean the smaller FP type has to
735 // be extended to the larger one.
736 if (PartVT.isFloatingPoint()) {
737 Val = DAG.getBitcast(ValueVT.getScalarType(), Val);
738 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
739 } else
740 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val,
741 DAG.getVectorIdxConstant(0, DL));
742 } else {
743 uint64_t ValueSize = ValueVT.getFixedSizeInBits();
744 assert(PartVT.getFixedSizeInBits() > ValueSize &&
745 "lossy conversion of vector to scalar type");
746 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
747 Val = DAG.getBitcast(IntermediateType, Val);
748 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
749 }
750 }
751
752 assert(Val.getValueType() == PartVT && "Unexpected vector part value type");
753 Parts[0] = Val;
754 return;
755 }
756
757 // Handle a multi-element vector.
758 EVT IntermediateVT;
759 MVT RegisterVT;
760 unsigned NumIntermediates;
761 unsigned NumRegs;
762 if (IsABIRegCopy) {
764 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT, NumIntermediates,
765 RegisterVT);
766 } else {
767 NumRegs =
768 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
769 NumIntermediates, RegisterVT);
770 }
771
772 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
773 NumParts = NumRegs; // Silence a compiler warning.
774 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
775
776 assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() &&
777 "Mixing scalable and fixed vectors when copying in parts");
778
779 std::optional<ElementCount> DestEltCnt;
780
781 if (IntermediateVT.isVector())
782 DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates;
783 else
784 DestEltCnt = ElementCount::getFixed(NumIntermediates);
785
786 EVT BuiltVectorTy = EVT::getVectorVT(
787 *DAG.getContext(), IntermediateVT.getScalarType(), *DestEltCnt);
788
789 if (ValueVT == BuiltVectorTy) {
790 // Nothing to do.
791 } else if (ValueVT.getSizeInBits() == BuiltVectorTy.getSizeInBits()) {
792 // Bitconvert vector->vector case.
793 Val = DAG.getNode(ISD::BITCAST, DL, BuiltVectorTy, Val);
794 } else {
795 if (BuiltVectorTy.getVectorElementType().bitsGT(
796 ValueVT.getVectorElementType())) {
797 // Integer promotion.
798 ValueVT = EVT::getVectorVT(*DAG.getContext(),
799 BuiltVectorTy.getVectorElementType(),
800 ValueVT.getVectorElementCount());
801 Val = DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
802 }
803
804 if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy)) {
805 Val = Widened;
806 }
807 }
808
809 assert(Val.getValueType() == BuiltVectorTy && "Unexpected vector value type");
810
811 // Split the vector into intermediate operands.
812 SmallVector<SDValue, 8> Ops(NumIntermediates);
813 for (unsigned i = 0; i != NumIntermediates; ++i) {
814 if (IntermediateVT.isVector()) {
815 // This does something sensible for scalable vectors - see the
816 // definition of EXTRACT_SUBVECTOR for further details.
817 unsigned IntermediateNumElts = IntermediateVT.getVectorMinNumElements();
818 Ops[i] =
819 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val,
820 DAG.getVectorIdxConstant(i * IntermediateNumElts, DL));
821 } else {
822 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val,
823 DAG.getVectorIdxConstant(i, DL));
824 }
825 }
826
827 // Split the intermediate operands into legal parts.
828 if (NumParts == NumIntermediates) {
829 // If the register was not expanded, promote or copy the value,
830 // as appropriate.
831 for (unsigned i = 0; i != NumParts; ++i)
832 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V, CallConv);
833 } else if (NumParts > 0) {
834 // If the intermediate type was expanded, split each the value into
835 // legal parts.
836 assert(NumIntermediates != 0 && "division by zero");
837 assert(NumParts % NumIntermediates == 0 &&
838 "Must expand into a divisible number of parts!");
839 unsigned Factor = NumParts / NumIntermediates;
840 for (unsigned i = 0; i != NumIntermediates; ++i)
841 getCopyToParts(DAG, DL, Ops[i], &Parts[i * Factor], Factor, PartVT, V,
842 CallConv);
843 }
844}
845
846static void failForInvalidBundles(const CallBase &I, StringRef Name,
847 ArrayRef<uint32_t> AllowedBundles) {
848 if (I.hasOperandBundlesOtherThan(AllowedBundles)) {
849 ListSeparator LS;
850 std::string Error;
852 for (unsigned i = 0, e = I.getNumOperandBundles(); i != e; ++i) {
853 OperandBundleUse U = I.getOperandBundleAt(i);
854 if (!is_contained(AllowedBundles, U.getTagID()))
855 OS << LS << U.getTagName();
856 }
858 Twine("cannot lower ", Name)
859 .concat(Twine(" with arbitrary operand bundles: ", Error)));
860 }
861}
862
864 EVT valuevt, std::optional<CallingConv::ID> CC)
865 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs),
866 RegCount(1, regs.size()), CallConv(CC) {}
867
869 const DataLayout &DL, Register Reg, Type *Ty,
870 std::optional<CallingConv::ID> CC) {
871 ComputeValueVTs(TLI, DL, Ty, ValueVTs);
872
873 CallConv = CC;
874
875 for (EVT ValueVT : ValueVTs) {
876 unsigned NumRegs =
878 ? TLI.getNumRegistersForCallingConv(Context, *CC, ValueVT)
879 : TLI.getNumRegisters(Context, ValueVT);
880 MVT RegisterVT =
882 ? TLI.getRegisterTypeForCallingConv(Context, *CC, ValueVT)
883 : TLI.getRegisterType(Context, ValueVT);
884 for (unsigned i = 0; i != NumRegs; ++i)
885 Regs.push_back(Reg + i);
886 RegVTs.push_back(RegisterVT);
887 RegCount.push_back(NumRegs);
888 Reg = Reg.id() + NumRegs;
889 }
890}
891
893 FunctionLoweringInfo &FuncInfo,
894 const SDLoc &dl, SDValue &Chain,
895 SDValue *Glue, const Value *V) const {
896 // A Value with type {} or [0 x %t] needs no registers.
897 if (ValueVTs.empty())
898 return SDValue();
899
900 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
901
902 // Assemble the legal parts into the final values.
905 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
906 // Copy the legal parts from the registers.
907 EVT ValueVT = ValueVTs[Value];
908 unsigned NumRegs = RegCount[Value];
909 MVT RegisterVT = isABIMangled()
911 *DAG.getContext(), *CallConv, RegVTs[Value])
912 : RegVTs[Value];
913
914 Parts.resize(NumRegs);
915 for (unsigned i = 0; i != NumRegs; ++i) {
916 SDValue P;
917 if (!Glue) {
918 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
919 } else {
920 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Glue);
921 *Glue = P.getValue(2);
922 }
923
924 Chain = P.getValue(1);
925 Parts[i] = P;
926
927 // If the source register was virtual and if we know something about it,
928 // add an assert node.
929 if (!Regs[Part + i].isVirtual() || !RegisterVT.isInteger())
930 continue;
931
933 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
934 if (!LOI)
935 continue;
936
937 unsigned RegSize = RegisterVT.getScalarSizeInBits();
938 unsigned NumSignBits = LOI->NumSignBits;
939 unsigned NumZeroBits = LOI->Known.countMinLeadingZeros();
940
941 if (NumZeroBits == RegSize) {
942 // The current value is a zero.
943 // Explicitly express that as it would be easier for
944 // optimizations to kick in.
945 Parts[i] = DAG.getConstant(0, dl, RegisterVT);
946 continue;
947 }
948
949 // FIXME: We capture more information than the dag can represent. For
950 // now, just use the tightest assertzext/assertsext possible.
951 bool isSExt;
952 EVT FromVT(MVT::Other);
953 if (NumZeroBits) {
954 FromVT = EVT::getIntegerVT(*DAG.getContext(), RegSize - NumZeroBits);
955 isSExt = false;
956 } else if (NumSignBits > 1) {
957 FromVT =
958 EVT::getIntegerVT(*DAG.getContext(), RegSize - NumSignBits + 1);
959 isSExt = true;
960 } else {
961 continue;
962 }
963 // Add an assertion node.
964 assert(FromVT != MVT::Other);
965 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
966 RegisterVT, P, DAG.getValueType(FromVT));
967 }
968
969 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), NumRegs,
970 RegisterVT, ValueVT, V, Chain, CallConv);
971 Part += NumRegs;
972 Parts.clear();
973 }
974
975 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values);
976}
977
979 const SDLoc &dl, SDValue &Chain, SDValue *Glue,
980 const Value *V,
981 ISD::NodeType PreferredExtendType) const {
982 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
983 ISD::NodeType ExtendKind = PreferredExtendType;
984
985 // Get the list of the values's legal parts.
986 unsigned NumRegs = Regs.size();
987 SmallVector<SDValue, 8> Parts(NumRegs);
988 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
989 unsigned NumParts = RegCount[Value];
990
991 MVT RegisterVT = isABIMangled()
993 *DAG.getContext(), *CallConv, RegVTs[Value])
994 : RegVTs[Value];
995
996 if (ExtendKind == ISD::ANY_EXTEND)
997 if (TLI.isZExtFree(peekThroughFreeze(Val), RegisterVT))
998 ExtendKind = ISD::ZERO_EXTEND;
999
1000 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
1001 NumParts, RegisterVT, V, CallConv, ExtendKind);
1002 Part += NumParts;
1003 }
1004
1005 // Copy the parts into the registers.
1006 SmallVector<SDValue, 8> Chains(NumRegs);
1007 for (unsigned i = 0; i != NumRegs; ++i) {
1008 SDValue Part;
1009 if (!Glue) {
1010 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
1011 } else {
1012 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue);
1013 *Glue = Part.getValue(1);
1014 }
1015
1016 Chains[i] = Part.getValue(0);
1017 }
1018
1019 if (NumRegs == 1 || Glue)
1020 // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is
1021 // flagged to it. That is the CopyToReg nodes and the user are considered
1022 // a single scheduling unit. If we create a TokenFactor and return it as
1023 // chain, then the TokenFactor is both a predecessor (operand) of the
1024 // user as well as a successor (the TF operands are flagged to the user).
1025 // c1, f1 = CopyToReg
1026 // c2, f2 = CopyToReg
1027 // c3 = TokenFactor c1, c2
1028 // ...
1029 // = op c3, ..., f2
1030 Chain = Chains[NumRegs-1];
1031 else
1032 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1033}
1034
1036 unsigned MatchingIdx, const SDLoc &dl,
1037 SelectionDAG &DAG,
1038 std::vector<SDValue> &Ops) const {
1039 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1040
1041 InlineAsm::Flag Flag(Code, Regs.size());
1042 if (HasMatching)
1043 Flag.setMatchingOp(MatchingIdx);
1044 else if (!Regs.empty() && Regs.front().isVirtual()) {
1045 // Put the register class of the virtual registers in the flag word. That
1046 // way, later passes can recompute register class constraints for inline
1047 // assembly as well as normal instructions.
1048 // Don't do this for tied operands that can use the regclass information
1049 // from the def.
1051 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
1052 Flag.setRegClass(RC->getID());
1053 }
1054
1055 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
1056 Ops.push_back(Res);
1057
1058 if (Code == InlineAsm::Kind::Clobber) {
1059 // Clobbers should always have a 1:1 mapping with registers, and may
1060 // reference registers that have illegal (e.g. vector) types. Hence, we
1061 // shouldn't try to apply any sort of splitting logic to them.
1062 assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
1063 "No 1:1 mapping from clobbers to regs?");
1065 (void)SP;
1066 for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
1067 Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
1068 assert(
1069 (Regs[I] != SP ||
1071 "If we clobbered the stack pointer, MFI should know about it.");
1072 }
1073 return;
1074 }
1075
1076 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1077 MVT RegisterVT = RegVTs[Value];
1078 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1079 RegisterVT);
1080 for (unsigned i = 0; i != NumRegs; ++i) {
1081 assert(Reg < Regs.size() && "Mismatch in # registers expected");
1082 Register TheReg = Regs[Reg++];
1083 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1084 }
1085 }
1086}
1087
1091 unsigned I = 0;
1092 for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1093 unsigned RegCount = std::get<0>(CountAndVT);
1094 MVT RegisterVT = std::get<1>(CountAndVT);
1095 TypeSize RegisterSize = RegisterVT.getSizeInBits();
1096 for (unsigned E = I + RegCount; I != E; ++I)
1097 OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1098 }
1099 return OutVec;
1100}
1101
1103 AssumptionCache *ac, const TargetLibraryInfo *li,
1104 const TargetTransformInfo &TTI) {
1105 BatchAA = aa;
1106 AC = ac;
1107 GFI = gfi;
1108 LibInfo = li;
1109 Context = DAG.getContext();
1110 LPadToCallSiteMap.clear();
1111 this->TTI = &TTI;
1112 SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1113 AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
1114 *DAG.getMachineFunction().getFunction().getParent());
1115}
1116
1118 NodeMap.clear();
1119 UnusedArgNodeMap.clear();
1120 PendingLoads.clear();
1121 PendingExports.clear();
1122 PendingConstrainedFP.clear();
1123 PendingConstrainedFPStrict.clear();
1124 CurInst = nullptr;
1125 HasTailCall = false;
1126 SDNodeOrder = LowestSDNodeOrder;
1127 StatepointLowering.clear();
1128}
1129
1131 DanglingDebugInfoMap.clear();
1132}
1133
1134// Update DAG root to include dependencies on Pending chains.
1135SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1136 SDValue Root = DAG.getRoot();
1137
1138 if (Pending.empty())
1139 return Root;
1140
1141 // Add current root to PendingChains, unless we already indirectly
1142 // depend on it.
1143 if (Root.getOpcode() != ISD::EntryToken) {
1144 unsigned i = 0, e = Pending.size();
1145 for (; i != e; ++i) {
1146 assert(Pending[i].getNode()->getNumOperands() > 1);
1147 if (Pending[i].getNode()->getOperand(0) == Root)
1148 break; // Don't add the root if we already indirectly depend on it.
1149 }
1150
1151 if (i == e)
1152 Pending.push_back(Root);
1153 }
1154
1155 if (Pending.size() == 1)
1156 Root = Pending[0];
1157 else
1158 Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1159
1160 DAG.setRoot(Root);
1161 Pending.clear();
1162 return Root;
1163}
1164
1168
1170 // If the new exception behavior differs from that of the pending
1171 // ones, chain up them and update the root.
1172 switch (EB) {
1175 // Floating-point exceptions produced by such operations are not intended
1176 // to be observed, so the sequence of these operations does not need to be
1177 // preserved.
1178 //
1179 // They however must not be mixed with the instructions that have strict
1180 // exception behavior. Placing an operation with 'ebIgnore' behavior between
1181 // 'ebStrict' operations could distort the observed exception behavior.
1182 if (!PendingConstrainedFPStrict.empty()) {
1183 assert(PendingConstrainedFP.empty());
1184 updateRoot(PendingConstrainedFPStrict);
1185 }
1186 break;
1188 // Floating-point exception produced by these operations may be observed, so
1189 // they must be correctly chained. If trapping on FP exceptions is
1190 // disabled, the exceptions can be observed only by functions that read
1191 // exception flags, like 'llvm.get_fpenv' or 'fetestexcept'. It means that
1192 // the order of operations is not significant between barriers.
1193 //
1194 // If trapping is enabled, each operation becomes an implicit observation
1195 // point, so the operations must be sequenced according their original
1196 // source order.
1197 if (!PendingConstrainedFP.empty()) {
1198 assert(PendingConstrainedFPStrict.empty());
1199 updateRoot(PendingConstrainedFP);
1200 }
1201 // TODO: Add support for trapping-enabled scenarios.
1202 }
1203 return DAG.getRoot();
1204}
1205
1207 // Chain up all pending constrained intrinsics together with all
1208 // pending loads, by simply appending them to PendingLoads and
1209 // then calling getMemoryRoot().
1210 PendingLoads.reserve(PendingLoads.size() +
1211 PendingConstrainedFP.size() +
1212 PendingConstrainedFPStrict.size());
1213 PendingLoads.append(PendingConstrainedFP.begin(),
1214 PendingConstrainedFP.end());
1215 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1216 PendingConstrainedFPStrict.end());
1217 PendingConstrainedFP.clear();
1218 PendingConstrainedFPStrict.clear();
1219 return getMemoryRoot();
1220}
1221
1223 // We need to emit pending fpexcept.strict constrained intrinsics,
1224 // so append them to the PendingExports list.
1225 PendingExports.append(PendingConstrainedFPStrict.begin(),
1226 PendingConstrainedFPStrict.end());
1227 PendingConstrainedFPStrict.clear();
1228 return updateRoot(PendingExports);
1229}
1230
1232 DILocalVariable *Variable,
1234 DebugLoc DL) {
1235 assert(Variable && "Missing variable");
1236
1237 // Check if address has undef value.
1238 if (!Address || isa<UndefValue>(Address) ||
1239 (Address->use_empty() && !isa<Argument>(Address))) {
1240 LLVM_DEBUG(
1241 dbgs()
1242 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1243 return;
1244 }
1245
1246 bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1247
1248 SDValue &N = NodeMap[Address];
1249 if (!N.getNode() && isa<Argument>(Address))
1250 // Check unused arguments map.
1251 N = UnusedArgNodeMap[Address];
1252 SDDbgValue *SDV;
1253 if (N.getNode()) {
1254 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1255 Address = BCI->getOperand(0);
1256 // Parameters are handled specially.
1257 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1258 if (IsParameter && FINode) {
1259 // Byval parameter. We have a frame index at this point.
1260 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1261 /*IsIndirect*/ true, DL, SDNodeOrder);
1262 } else if (isa<Argument>(Address)) {
1263 // Address is an argument, so try to emit its dbg value using
1264 // virtual register info from the FuncInfo.ValueMap.
1265 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1266 FuncArgumentDbgValueKind::Declare, N);
1267 return;
1268 } else {
1269 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1270 true, DL, SDNodeOrder);
1271 }
1272 DAG.AddDbgValue(SDV, IsParameter);
1273 } else {
1274 // If Address is an argument then try to emit its dbg value using
1275 // virtual register info from the FuncInfo.ValueMap.
1276 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1277 FuncArgumentDbgValueKind::Declare, N)) {
1278 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1279 << " (could not emit func-arg dbg_value)\n");
1280 }
1281 }
1282}
1283
1285 // Add SDDbgValue nodes for any var locs here. Do so before updating
1286 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1287 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1288 // Add SDDbgValue nodes for any var locs here. Do so before updating
1289 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1290 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1291 It != End; ++It) {
1292 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1293 dropDanglingDebugInfo(Var, It->Expr);
1294 if (It->Values.isKillLocation(It->Expr)) {
1295 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1296 continue;
1297 }
1298 SmallVector<Value *> Values(It->Values.location_ops());
1299 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1300 It->Values.hasArgList())) {
1301 SmallVector<Value *, 4> Vals(It->Values.location_ops());
1303 FnVarLocs->getDILocalVariable(It->VariableID),
1304 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1305 }
1306 }
1307 }
1308
1309 // We must skip DbgVariableRecords if they've already been processed above as
1310 // we have just emitted the debug values resulting from assignment tracking
1311 // analysis, making any existing DbgVariableRecords redundant (and probably
1312 // less correct). We still need to process DbgLabelRecords. This does sink
1313 // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1314 // be important as it does so deterministcally and ordering between
1315 // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1316 // printing).
1317 bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1318 // Is there is any debug-info attached to this instruction, in the form of
1319 // DbgRecord non-instruction debug-info records.
1320 for (DbgRecord &DR : I.getDbgRecordRange()) {
1321 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1322 assert(DLR->getLabel() && "Missing label");
1323 SDDbgLabel *SDV =
1324 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1325 DAG.AddDbgLabel(SDV);
1326 continue;
1327 }
1328
1329 if (SkipDbgVariableRecords)
1330 continue;
1332 DILocalVariable *Variable = DVR.getVariable();
1335
1337 if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1338 continue;
1339 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1340 << "\n");
1342 DVR.getDebugLoc());
1343 continue;
1344 }
1345
1346 // A DbgVariableRecord with no locations is a kill location.
1348 if (Values.empty()) {
1350 SDNodeOrder);
1351 continue;
1352 }
1353
1354 // A DbgVariableRecord with an undef or absent location is also a kill
1355 // location.
1356 if (llvm::any_of(Values,
1357 [](Value *V) { return !V || isa<UndefValue>(V); })) {
1359 SDNodeOrder);
1360 continue;
1361 }
1362
1363 bool IsVariadic = DVR.hasArgList();
1364 if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1365 SDNodeOrder, IsVariadic)) {
1366 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1367 DVR.getDebugLoc(), SDNodeOrder);
1368 }
1369 }
1370}
1371
1373 visitDbgInfo(I);
1374
1375 // Set up outgoing PHI node register values before emitting the terminator.
1376 if (I.isTerminator()) {
1377 HandlePHINodesInSuccessorBlocks(I.getParent());
1378 }
1379
1380 ++SDNodeOrder;
1381 CurInst = &I;
1382
1383 // Set inserted listener only if required.
1384 bool NodeInserted = false;
1385 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1386 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1387 MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1388 if (PCSectionsMD || MMRA) {
1389 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1390 DAG, [&](SDNode *) { NodeInserted = true; });
1391 }
1392
1393 visit(I.getOpcode(), I);
1394
1395 if (!I.isTerminator() && !HasTailCall &&
1396 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1398
1399 // Handle metadata.
1400 if (PCSectionsMD || MMRA) {
1401 auto It = NodeMap.find(&I);
1402 if (It != NodeMap.end()) {
1403 if (PCSectionsMD)
1404 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1405 if (MMRA)
1406 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1407 } else if (NodeInserted) {
1408 // This should not happen; if it does, don't let it go unnoticed so we can
1409 // fix it. Relevant visit*() function is probably missing a setValue().
1410 errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1411 << I.getModule()->getName() << "]\n";
1412 LLVM_DEBUG(I.dump());
1413 assert(false);
1414 }
1415 }
1416
1417 CurInst = nullptr;
1418}
1419
1420void SelectionDAGBuilder::visitPHI(const PHINode &) {
1421 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1422}
1423
1424void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1425 // Note: this doesn't use InstVisitor, because it has to work with
1426 // ConstantExpr's in addition to instructions.
1427 switch (Opcode) {
1428 default: llvm_unreachable("Unknown instruction type encountered!");
1429 // Build the switch statement using the Instruction.def file.
1430#define HANDLE_INST(NUM, OPCODE, CLASS) \
1431 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1432#include "llvm/IR/Instruction.def"
1433 }
1434}
1435
1437 DILocalVariable *Variable,
1438 DebugLoc DL, unsigned Order,
1441 // For variadic dbg_values we will now insert poison.
1442 // FIXME: We can potentially recover these!
1444 for (const Value *V : Values) {
1445 auto *Poison = PoisonValue::get(V->getType());
1447 }
1448 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1449 /*IsIndirect=*/false, DL, Order,
1450 /*IsVariadic=*/true);
1451 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1452 return true;
1453}
1454
1456 DILocalVariable *Var,
1457 DIExpression *Expr,
1458 bool IsVariadic, DebugLoc DL,
1459 unsigned Order) {
1460 if (IsVariadic) {
1461 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1462 return;
1463 }
1464 // TODO: Dangling debug info will eventually either be resolved or produce
1465 // a poison DBG_VALUE. However in the resolution case, a gap may appear
1466 // between the original dbg.value location and its resolved DBG_VALUE,
1467 // which we should ideally fill with an extra poison DBG_VALUE.
1468 assert(Values.size() == 1);
1469 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1470}
1471
1473 const DIExpression *Expr) {
1474 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1475 DIVariable *DanglingVariable = DDI.getVariable();
1476 DIExpression *DanglingExpr = DDI.getExpression();
1477 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1478 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1479 << printDDI(nullptr, DDI) << "\n");
1480 return true;
1481 }
1482 return false;
1483 };
1484
1485 for (auto &DDIMI : DanglingDebugInfoMap) {
1486 DanglingDebugInfoVector &DDIV = DDIMI.second;
1487
1488 // If debug info is to be dropped, run it through final checks to see
1489 // whether it can be salvaged.
1490 for (auto &DDI : DDIV)
1491 if (isMatchingDbgValue(DDI))
1492 salvageUnresolvedDbgValue(DDIMI.first, DDI);
1493
1494 erase_if(DDIV, isMatchingDbgValue);
1495 }
1496}
1497
1498// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1499// generate the debug data structures now that we've seen its definition.
1501 SDValue Val) {
1502 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1503 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1504 return;
1505
1506 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1507 for (auto &DDI : DDIV) {
1508 DebugLoc DL = DDI.getDebugLoc();
1509 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1510 DILocalVariable *Variable = DDI.getVariable();
1511 DIExpression *Expr = DDI.getExpression();
1512 assert(Variable->isValidLocationForIntrinsic(DL) &&
1513 "Expected inlined-at fields to agree");
1514 SDDbgValue *SDV;
1515 if (Val.getNode()) {
1516 // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1517 // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1518 // we couldn't resolve it directly when examining the DbgValue intrinsic
1519 // in the first place we should not be more successful here). Unless we
1520 // have some test case that prove this to be correct we should avoid
1521 // calling EmitFuncArgumentDbgValue here.
1522 unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1523 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1524 FuncArgumentDbgValueKind::Value, Val)) {
1525 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1526 << printDDI(V, DDI) << "\n");
1527 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump());
1528 // Increase the SDNodeOrder for the DbgValue here to make sure it is
1529 // inserted after the definition of Val when emitting the instructions
1530 // after ISel. An alternative could be to teach
1531 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1532 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1533 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1534 << ValSDNodeOrder << "\n");
1535 SDV = getDbgValue(Val, Variable, Expr, DL,
1536 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1537 DAG.AddDbgValue(SDV, false);
1538 } else
1539 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1540 << printDDI(V, DDI)
1541 << " in EmitFuncArgumentDbgValue\n");
1542 } else {
1543 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1544 << "\n");
1545 auto Poison = PoisonValue::get(V->getType());
1546 auto SDV =
1547 DAG.getConstantDbgValue(Variable, Expr, Poison, DL, DbgSDNodeOrder);
1548 DAG.AddDbgValue(SDV, false);
1549 }
1550 }
1551 DDIV.clear();
1552}
1553
1555 DanglingDebugInfo &DDI) {
1556 // TODO: For the variadic implementation, instead of only checking the fail
1557 // state of `handleDebugValue`, we need know specifically which values were
1558 // invalid, so that we attempt to salvage only those values when processing
1559 // a DIArgList.
1560 const Value *OrigV = V;
1561 DILocalVariable *Var = DDI.getVariable();
1562 DIExpression *Expr = DDI.getExpression();
1563 DebugLoc DL = DDI.getDebugLoc();
1564 unsigned SDOrder = DDI.getSDNodeOrder();
1565
1566 // Currently we consider only dbg.value intrinsics -- we tell the salvager
1567 // that DW_OP_stack_value is desired.
1568 bool StackValue = true;
1569
1570 // Can this Value can be encoded without any further work?
1571 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1572 return;
1573
1574 // Attempt to salvage back through as many instructions as possible. Bail if
1575 // a non-instruction is seen, such as a constant expression or global
1576 // variable. FIXME: Further work could recover those too.
1577 while (isa<Instruction>(V)) {
1578 const Instruction &VAsInst = *cast<const Instruction>(V);
1579 // Temporary "0", awaiting real implementation.
1581 SmallVector<Value *, 4> AdditionalValues;
1582 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1583 Expr->getNumLocationOperands(), Ops,
1584 AdditionalValues);
1585 // If we cannot salvage any further, and haven't yet found a suitable debug
1586 // expression, bail out.
1587 if (!V)
1588 break;
1589
1590 // TODO: If AdditionalValues isn't empty, then the salvage can only be
1591 // represented with a DBG_VALUE_LIST, so we give up. When we have support
1592 // here for variadic dbg_values, remove that condition.
1593 if (!AdditionalValues.empty())
1594 break;
1595
1596 // New value and expr now represent this debuginfo.
1597 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1598
1599 // Some kind of simplification occurred: check whether the operand of the
1600 // salvaged debug expression can be encoded in this DAG.
1601 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1602 LLVM_DEBUG(
1603 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n"
1604 << *OrigV << "\nBy stripping back to:\n " << *V << "\n");
1605 return;
1606 }
1607 }
1608
1609 // This was the final opportunity to salvage this debug information, and it
1610 // couldn't be done. Place a poison DBG_VALUE at this location to terminate
1611 // any earlier variable location.
1612 assert(OrigV && "V shouldn't be null");
1613 auto *Poison = PoisonValue::get(OrigV->getType());
1614 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Poison, DL, SDNodeOrder);
1615 DAG.AddDbgValue(SDV, false);
1616 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n "
1617 << printDDI(OrigV, DDI) << "\n");
1618}
1619
1621 DIExpression *Expr,
1622 DebugLoc DbgLoc,
1623 unsigned Order) {
1627 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1628 /*IsVariadic*/ false);
1629}
1630
1632 DILocalVariable *Var,
1633 DIExpression *Expr, DebugLoc DbgLoc,
1634 unsigned Order, bool IsVariadic) {
1635 if (Values.empty())
1636 return true;
1637
1638 // Filter EntryValue locations out early.
1639 if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1640 return true;
1641
1642 SmallVector<SDDbgOperand> LocationOps;
1643 SmallVector<SDNode *> Dependencies;
1644 for (const Value *V : Values) {
1645 // Constant value.
1648 LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1649 continue;
1650 }
1651
1652 // Look through IntToPtr constants.
1653 if (auto *CE = dyn_cast<ConstantExpr>(V))
1654 if (CE->getOpcode() == Instruction::IntToPtr) {
1655 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1656 continue;
1657 }
1658
1659 // If the Value is a frame index, we can create a FrameIndex debug value
1660 // without relying on the DAG at all.
1661 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1662 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1663 if (SI != FuncInfo.StaticAllocaMap.end()) {
1664 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1665 continue;
1666 }
1667 }
1668
1669 // Do not use getValue() in here; we don't want to generate code at
1670 // this point if it hasn't been done yet.
1671 SDValue N = NodeMap[V];
1672 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1673 N = UnusedArgNodeMap[V];
1674
1675 if (N.getNode()) {
1676 // Only emit func arg dbg value for non-variadic dbg.values for now.
1677 if (!IsVariadic &&
1678 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1679 FuncArgumentDbgValueKind::Value, N))
1680 return true;
1681 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1682 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1683 // describe stack slot locations.
1684 //
1685 // Consider "int x = 0; int *px = &x;". There are two kinds of
1686 // interesting debug values here after optimization:
1687 //
1688 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
1689 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1690 //
1691 // Both describe the direct values of their associated variables.
1692 Dependencies.push_back(N.getNode());
1693 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1694 continue;
1695 }
1696 LocationOps.emplace_back(
1697 SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1698 continue;
1699 }
1700
1701 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1702 // Special rules apply for the first dbg.values of parameter variables in a
1703 // function. Identify them by the fact they reference Argument Values, that
1704 // they're parameters, and they are parameters of the current function. We
1705 // need to let them dangle until they get an SDNode.
1706 bool IsParamOfFunc =
1707 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1708 if (IsParamOfFunc)
1709 return false;
1710
1711 // The value is not used in this block yet (or it would have an SDNode).
1712 // We still want the value to appear for the user if possible -- if it has
1713 // an associated VReg, we can refer to that instead.
1714 auto VMI = FuncInfo.ValueMap.find(V);
1715 if (VMI != FuncInfo.ValueMap.end()) {
1716 Register Reg = VMI->second;
1717 // If this is a PHI node, it may be split up into several MI PHI nodes
1718 // (in FunctionLoweringInfo::set).
1719 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1720 V->getType(), std::nullopt);
1721 if (RFV.occupiesMultipleRegs()) {
1722 // FIXME: We could potentially support variadic dbg_values here.
1723 if (IsVariadic)
1724 return false;
1725 unsigned Offset = 0;
1726 unsigned BitsToDescribe = 0;
1727 if (auto VarSize = Var->getSizeInBits())
1728 BitsToDescribe = *VarSize;
1729 if (auto Fragment = Expr->getFragmentInfo())
1730 BitsToDescribe = Fragment->SizeInBits;
1731 for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1732 // Bail out if all bits are described already.
1733 if (Offset >= BitsToDescribe)
1734 break;
1735 // TODO: handle scalable vectors.
1736 unsigned RegisterSize = RegAndSize.second;
1737 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1738 ? BitsToDescribe - Offset
1739 : RegisterSize;
1740 auto FragmentExpr = DIExpression::createFragmentExpression(
1741 Expr, Offset, FragmentSize);
1742 if (!FragmentExpr)
1743 continue;
1744 SDDbgValue *SDV = DAG.getVRegDbgValue(
1745 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1746 DAG.AddDbgValue(SDV, false);
1747 Offset += RegisterSize;
1748 }
1749 return true;
1750 }
1751 // We can use simple vreg locations for variadic dbg_values as well.
1752 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1753 continue;
1754 }
1755 // We failed to create a SDDbgOperand for V.
1756 return false;
1757 }
1758
1759 // We have created a SDDbgOperand for each Value in Values.
1760 assert(!LocationOps.empty());
1761 SDDbgValue *SDV =
1762 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1763 /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1764 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1765 return true;
1766}
1767
1769 // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1770 for (auto &Pair : DanglingDebugInfoMap)
1771 for (auto &DDI : Pair.second)
1772 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1774}
1775
1776/// getCopyFromRegs - If there was virtual register allocated for the value V
1777/// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1779 auto It = FuncInfo.ValueMap.find(V);
1780 SDValue Result;
1781
1782 if (It != FuncInfo.ValueMap.end()) {
1783 Register InReg = It->second;
1784
1785 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1786 DAG.getDataLayout(), InReg, Ty,
1787 std::nullopt); // This is not an ABI copy.
1788 SDValue Chain = DAG.getEntryNode();
1789 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1790 V);
1791 resolveDanglingDebugInfo(V, Result);
1792 }
1793
1794 return Result;
1795}
1796
1797/// getValue - Return an SDValue for the given Value.
1799 // If we already have an SDValue for this value, use it. It's important
1800 // to do this first, so that we don't create a CopyFromReg if we already
1801 // have a regular SDValue.
1802 SDValue &N = NodeMap[V];
1803 if (N.getNode()) return N;
1804
1805 // If there's a virtual register allocated and initialized for this
1806 // value, use it.
1807 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1808 return copyFromReg;
1809
1810 // Otherwise create a new SDValue and remember it.
1811 SDValue Val = getValueImpl(V);
1812 NodeMap[V] = Val;
1814 return Val;
1815}
1816
1817/// getNonRegisterValue - Return an SDValue for the given Value, but
1818/// don't look in FuncInfo.ValueMap for a virtual register.
1820 // If we already have an SDValue for this value, use it.
1821 SDValue &N = NodeMap[V];
1822 if (N.getNode()) {
1823 if (isIntOrFPConstant(N)) {
1824 // Remove the debug location from the node as the node is about to be used
1825 // in a location which may differ from the original debug location. This
1826 // is relevant to Constant and ConstantFP nodes because they can appear
1827 // as constant expressions inside PHI nodes.
1828 N->setDebugLoc(DebugLoc());
1829 }
1830 return N;
1831 }
1832
1833 // Otherwise create a new SDValue and remember it.
1834 SDValue Val = getValueImpl(V);
1835 NodeMap[V] = Val;
1837 return Val;
1838}
1839
1840/// getValueImpl - Helper function for getValue and getNonRegisterValue.
1841/// Create an SDValue for the given value.
1843 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1844
1845 if (const Constant *C = dyn_cast<Constant>(V)) {
1846 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1847
1848 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1849 SDLoc DL = getCurSDLoc();
1850
1851 // DAG.getConstant() may attempt to legalise the vector constant which can
1852 // significantly change the combines applied to the DAG. To reduce the
1853 // divergence when enabling ConstantInt based vectors we try to construct
1854 // the DAG in the same way as shufflevector based splats. TODO: The
1855 // divergence sometimes leads to better optimisations. Ideally we should
1856 // prevent DAG.getConstant() from legalising too early but there are some
1857 // degradations preventing this.
1858 if (VT.isScalableVector())
1859 return DAG.getNode(
1860 ISD::SPLAT_VECTOR, DL, VT,
1861 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1862 if (VT.isFixedLengthVector())
1863 return DAG.getSplatBuildVector(
1864 VT, DL,
1865 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1866 return DAG.getConstant(*CI, DL, VT);
1867 }
1868
1869 if (const ConstantByte *CB = dyn_cast<ConstantByte>(C))
1870 return DAG.getConstant(CB->getValue(), getCurSDLoc(), VT);
1871
1872 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1873 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1874
1875 if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1876 return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1877 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1878 getValue(CPA->getAddrDiscriminator()),
1879 getValue(CPA->getDiscriminator()));
1880 }
1881
1883 return DAG.getConstant(0, getCurSDLoc(), VT);
1884
1885 if (match(C, m_VScale()))
1886 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1887
1888 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1889 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1890
1891 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1892 return isa<PoisonValue>(C) ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
1893
1894 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1895 visit(CE->getOpcode(), *CE);
1896 SDValue N1 = NodeMap[V];
1897 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1898 return N1;
1899 }
1900
1902 SmallVector<SDValue, 4> Constants;
1903 for (const Use &U : C->operands()) {
1904 SDNode *Val = getValue(U).getNode();
1905 // If the operand is an empty aggregate, there are no values.
1906 if (!Val) continue;
1907 // Add each leaf value from the operand to the Constants list
1908 // to form a flattened list of all the values.
1909 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1910 Constants.push_back(SDValue(Val, i));
1911 }
1912
1913 return DAG.getMergeValues(Constants, getCurSDLoc());
1914 }
1915
1916 if (const ConstantDataSequential *CDS =
1919 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1920 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1921 // Add each leaf value from the operand to the Constants list
1922 // to form a flattened list of all the values.
1923 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1924 Ops.push_back(SDValue(Val, i));
1925 }
1926
1927 if (isa<ArrayType>(CDS->getType()))
1928 return DAG.getMergeValues(Ops, getCurSDLoc());
1929 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1930 }
1931
1932 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1934 "Unknown struct or array constant!");
1935
1936 SmallVector<EVT, 4> ValueVTs;
1937 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1938 unsigned NumElts = ValueVTs.size();
1939 if (NumElts == 0)
1940 return SDValue(); // empty struct
1941 SmallVector<SDValue, 4> Constants(NumElts);
1942 for (unsigned i = 0; i != NumElts; ++i) {
1943 EVT EltVT = ValueVTs[i];
1944 if (isa<UndefValue>(C))
1945 Constants[i] = DAG.getUNDEF(EltVT);
1946 else if (EltVT.isFloatingPoint())
1947 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1948 else
1949 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1950 }
1951
1952 return DAG.getMergeValues(Constants, getCurSDLoc());
1953 }
1954
1955 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1956 return DAG.getBlockAddress(BA, VT);
1957
1958 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1959 return getValue(Equiv->getGlobalValue());
1960
1961 if (const auto *NC = dyn_cast<NoCFIValue>(C))
1962 return getValue(NC->getGlobalValue());
1963
1964 if (VT == MVT::aarch64svcount) {
1965 assert(C->isNullValue() && "Can only zero this target type!");
1966 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1967 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1968 }
1969
1970 if (VT.isRISCVVectorTuple()) {
1971 assert(C->isNullValue() && "Can only zero this target type!");
1972 return DAG.getNode(
1974 DAG.getNode(
1976 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1977 VT.getSizeInBits().getKnownMinValue() / 8, true),
1978 DAG.getConstant(0, getCurSDLoc(), MVT::getIntegerVT(8))));
1979 }
1980
1981 if (VT == MVT::externref || VT == MVT::funcref) {
1982 assert(C->isNullValue() && "Can only zero this target type!");
1983 // The zero value of a WebAssembly reference type is the null reference,
1984 // materialized with ref.null.
1985 Intrinsic::ID IID = VT == MVT::externref ? Intrinsic::wasm_ref_null_extern
1986 : Intrinsic::wasm_ref_null_func;
1987 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VT,
1988 DAG.getTargetConstant(IID, getCurSDLoc(), MVT::i32));
1989 }
1990
1991 VectorType *VecTy = cast<VectorType>(V->getType());
1992
1993 // Now that we know the number and type of the elements, get that number of
1994 // elements into the Ops array based on what kind of constant it is.
1995 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1997 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1998 for (unsigned i = 0; i != NumElements; ++i)
1999 Ops.push_back(getValue(CV->getOperand(i)));
2000
2001 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
2002 }
2003
2005 EVT EltVT =
2006 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
2007
2008 SDValue Op;
2009 if (EltVT.isFloatingPoint())
2010 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
2011 else
2012 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
2013
2014 return DAG.getSplat(VT, getCurSDLoc(), Op);
2015 }
2016
2017 llvm_unreachable("Unknown vector constant");
2018 }
2019
2020 // If this is a static alloca, generate it as the frameindex instead of
2021 // computation.
2022 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
2023 auto SI = FuncInfo.StaticAllocaMap.find(AI);
2024 if (SI != FuncInfo.StaticAllocaMap.end())
2025 return DAG.getFrameIndex(
2026 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
2027 }
2028
2029 // If this is an instruction which fast-isel has deferred, select it now.
2030 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2031 Register InReg = FuncInfo.InitializeRegForValue(Inst);
2032 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
2033 Inst->getType(), std::nullopt);
2034 SDValue Chain = DAG.getEntryNode();
2035 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
2036 }
2037
2038 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
2039 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
2040
2041 if (const auto *BB = dyn_cast<BasicBlock>(V))
2042 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
2043
2044 llvm_unreachable("Can't get register for value!");
2045}
2046
2047void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
2049 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
2050 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
2051 bool IsSEH = isAsynchronousEHPersonality(Pers);
2052 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2053 if (IsSEH) {
2054 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2055 CatchPadMBB->setIsEHContTarget(true);
2057 } else
2058 CatchPadMBB->setIsEHScopeEntry();
2059 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2060 if (IsMSVCCXX || IsCoreCLR)
2061 CatchPadMBB->setIsEHFuncletEntry();
2062}
2063
2064void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2065 // Update machine-CFG edge.
2066 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2067 FuncInfo.MBB->addSuccessor(TargetMBB);
2068
2069 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2070 bool IsSEH = isAsynchronousEHPersonality(Pers);
2071 if (IsSEH) {
2072 // If this is not a fall-through branch or optimizations are switched off,
2073 // emit the branch.
2074 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2075 TM.getOptLevel() == CodeGenOptLevel::None)
2076 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2077 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2078 return;
2079 }
2080
2081 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2082 TargetMBB->setIsEHContTarget(true);
2083 DAG.getMachineFunction().setHasEHContTarget(true);
2084
2085 // Figure out the funclet membership for the catchret's successor.
2086 // This will be used by the FuncletLayout pass to determine how to order the
2087 // BB's.
2088 // A 'catchret' returns to the outer scope's color.
2089 Value *ParentPad = I.getCatchSwitchParentPad();
2090 const BasicBlock *SuccessorColor;
2091 if (isa<ConstantTokenNone>(ParentPad))
2092 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2093 else
2094 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2095 assert(SuccessorColor && "No parent funclet for catchret!");
2096 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2097 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2098
2099 // Create the terminator node.
2100 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2101 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2102 DAG.getBasicBlock(SuccessorColorMBB));
2103 DAG.setRoot(Ret);
2104}
2105
2106void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2107 // Don't emit any special code for the cleanuppad instruction. It just marks
2108 // the start of an EH scope/funclet.
2109 FuncInfo.MBB->setIsEHScopeEntry();
2110 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2111 if (Pers != EHPersonality::Wasm_CXX) {
2112 FuncInfo.MBB->setIsEHFuncletEntry();
2113 FuncInfo.MBB->setIsCleanupFuncletEntry();
2114 }
2115}
2116
2117/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2118/// many places it could ultimately go. In the IR, we have a single unwind
2119/// destination, but in the machine CFG, we enumerate all the possible blocks.
2120/// This function skips over imaginary basic blocks that hold catchswitch
2121/// instructions, and finds all the "real" machine
2122/// basic block destinations. As those destinations may not be successors of
2123/// EHPadBB, here we also calculate the edge probability to those destinations.
2124/// The passed-in Prob is the edge probability to EHPadBB.
2126 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2127 BranchProbability Prob,
2128 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2129 &UnwindDests) {
2130 EHPersonality Personality =
2132 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2133 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2134 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2135 bool IsSEH = isAsynchronousEHPersonality(Personality);
2136
2137 while (EHPadBB) {
2139 BasicBlock *NewEHPadBB = nullptr;
2140 if (isa<LandingPadInst>(Pad)) {
2141 // Stop on landingpads. They are not funclets.
2142 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2143 break;
2144 } else if (isa<CleanupPadInst>(Pad)) {
2145 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2146 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2147 // which always catches an exception.
2148 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2149 UnwindDests.back().first->setIsEHScopeEntry();
2150 // In Wasm, EH scopes are not funclets
2151 if (!IsWasmCXX)
2152 UnwindDests.back().first->setIsEHFuncletEntry();
2153 break;
2154 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2155 // Add the catchpad handlers to the possible destinations.
2156 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2157 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2158 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2159 if (IsMSVCCXX || IsCoreCLR)
2160 UnwindDests.back().first->setIsEHFuncletEntry();
2161 if (!IsSEH)
2162 UnwindDests.back().first->setIsEHScopeEntry();
2163 }
2164 NewEHPadBB = CatchSwitch->getUnwindDest();
2165 } else {
2166 continue;
2167 }
2168
2169 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2170 if (BPI && NewEHPadBB)
2171 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2172 EHPadBB = NewEHPadBB;
2173 }
2174}
2175
2176void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2177 // Update successor info.
2179 auto UnwindDest = I.getUnwindDest();
2180 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2181 BranchProbability UnwindDestProb =
2182 (BPI && UnwindDest)
2183 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2185 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2186 for (auto &UnwindDest : UnwindDests) {
2187 UnwindDest.first->setIsEHPad();
2188 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2189 }
2190 FuncInfo.MBB->normalizeSuccProbs();
2191
2192 // Create the terminator node.
2193 MachineBasicBlock *CleanupPadMBB =
2194 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2195 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
2196 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2197 DAG.setRoot(Ret);
2198}
2199
2200void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2201 report_fatal_error("visitCatchSwitch not yet implemented!");
2202}
2203
2204void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2205 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2206 auto &DL = DAG.getDataLayout();
2207 SDValue Chain = getControlRoot();
2210
2211 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2212 // lower
2213 //
2214 // %val = call <ty> @llvm.experimental.deoptimize()
2215 // ret <ty> %val
2216 //
2217 // differently.
2218 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2220 return;
2221 }
2222
2223 if (!FuncInfo.CanLowerReturn) {
2224 Register DemoteReg = FuncInfo.DemoteRegister;
2225
2226 // Emit a store of the return value through the virtual register.
2227 // Leave Outs empty so that LowerReturn won't try to load return
2228 // registers the usual way.
2229 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2230 SDValue RetPtr =
2231 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2232 SDValue RetOp = getValue(I.getOperand(0));
2233
2234 SmallVector<EVT, 4> ValueVTs, MemVTs;
2235 SmallVector<uint64_t, 4> Offsets;
2236 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2237 &Offsets, 0);
2238 unsigned NumValues = ValueVTs.size();
2239
2240 SmallVector<SDValue, 4> Chains(NumValues);
2241 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2242 for (unsigned i = 0; i != NumValues; ++i) {
2243 // An aggregate return value cannot wrap around the address space, so
2244 // offsets to its parts don't wrap either.
2245 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2246 TypeSize::getFixed(Offsets[i]));
2247
2248 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2249 if (MemVTs[i] != ValueVTs[i])
2250 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2251 Chains[i] = DAG.getStore(
2252 Chain, getCurSDLoc(), Val,
2253 // FIXME: better loc info would be nice.
2254 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2255 commonAlignment(BaseAlign, Offsets[i]));
2256 }
2257
2258 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2259 MVT::Other, Chains);
2260 } else if (I.getNumOperands() != 0) {
2262 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2263 unsigned NumValues = Types.size();
2264 if (NumValues) {
2265 SDValue RetOp = getValue(I.getOperand(0));
2266
2267 const Function *F = I.getParent()->getParent();
2268
2269 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2270 I.getOperand(0)->getType(), F->getCallingConv(),
2271 /*IsVarArg*/ false, DL);
2272
2273 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2274 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2275 ExtendKind = ISD::SIGN_EXTEND;
2276 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2277 ExtendKind = ISD::ZERO_EXTEND;
2278
2279 LLVMContext &Context = F->getContext();
2280 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2281
2282 for (unsigned j = 0; j != NumValues; ++j) {
2283 EVT VT = TLI.getValueType(DL, Types[j]);
2284
2285 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2286 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2287
2288 CallingConv::ID CC = F->getCallingConv();
2289
2290 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2291 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2292 SmallVector<SDValue, 4> Parts(NumParts);
2294 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2295 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2296
2297 // 'inreg' on function refers to return value
2298 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2299 if (RetInReg)
2300 Flags.setInReg();
2301
2302 if (I.getOperand(0)->getType()->isPointerTy()) {
2303 Flags.setPointer();
2304 Flags.setPointerAddrSpace(
2305 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2306 }
2307
2308 if (NeedsRegBlock) {
2309 Flags.setInConsecutiveRegs();
2310 if (j == NumValues - 1)
2311 Flags.setInConsecutiveRegsLast();
2312 }
2313
2314 // Propagate extension type if any
2315 if (ExtendKind == ISD::SIGN_EXTEND)
2316 Flags.setSExt();
2317 else if (ExtendKind == ISD::ZERO_EXTEND)
2318 Flags.setZExt();
2319 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2320 Flags.setNoExt();
2321
2322 for (unsigned i = 0; i < NumParts; ++i) {
2323 Outs.push_back(ISD::OutputArg(Flags,
2324 Parts[i].getValueType().getSimpleVT(),
2325 VT, Types[j], 0, 0));
2326 OutVals.push_back(Parts[i]);
2327 }
2328 }
2329 }
2330 }
2331
2332 // Push in swifterror virtual register as the last element of Outs. This makes
2333 // sure swifterror virtual register will be returned in the swifterror
2334 // physical register.
2335 const Function *F = I.getParent()->getParent();
2336 if (TLI.supportSwiftError() &&
2337 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2338 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2339 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2340 Flags.setSwiftError();
2341 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2342 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2343 PointerType::getUnqual(*DAG.getContext()),
2344 /*origidx=*/1, /*partOffs=*/0));
2345 // Create SDNode for the swifterror virtual register.
2346 OutVals.push_back(
2347 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2348 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2349 EVT(TLI.getPointerTy(DL))));
2350 }
2351
2352 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2353 CallingConv::ID CallConv =
2354 DAG.getMachineFunction().getFunction().getCallingConv();
2355 Chain = DAG.getTargetLoweringInfo().LowerReturn(
2356 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2357
2358 // Verify that the target's LowerReturn behaved as expected.
2359 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2360 "LowerReturn didn't return a valid chain!");
2361
2362 // Update the DAG with the new chain value resulting from return lowering.
2363 DAG.setRoot(Chain);
2364}
2365
2366/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2367/// created for it, emit nodes to copy the value into the virtual
2368/// registers.
2370 // Skip empty types
2371 if (V->getType()->isEmptyTy())
2372 return;
2373
2374 auto VMI = FuncInfo.ValueMap.find(V);
2375 if (VMI != FuncInfo.ValueMap.end()) {
2376 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2377 "Unused value assigned virtual registers!");
2378 CopyValueToVirtualRegister(V, VMI->second);
2379 }
2380}
2381
2382/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2383/// the current basic block, add it to ValueMap now so that we'll get a
2384/// CopyTo/FromReg.
2386 // No need to export constants.
2387 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2388
2389 // Already exported?
2390 if (FuncInfo.isExportedInst(V)) return;
2391
2392 Register Reg = FuncInfo.InitializeRegForValue(V);
2394}
2395
2397 const BasicBlock *FromBB) {
2398 // The operands of the setcc have to be in this block. We don't know
2399 // how to export them from some other block.
2400 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2401 // Can export from current BB.
2402 if (VI->getParent() == FromBB)
2403 return true;
2404
2405 // Is already exported, noop.
2406 return FuncInfo.isExportedInst(V);
2407 }
2408
2409 // If this is an argument, we can export it if the BB is the entry block or
2410 // if it is already exported.
2411 if (isa<Argument>(V)) {
2412 if (FromBB->isEntryBlock())
2413 return true;
2414
2415 // Otherwise, can only export this if it is already exported.
2416 return FuncInfo.isExportedInst(V);
2417 }
2418
2419 // Otherwise, constants can always be exported.
2420 return true;
2421}
2422
2423/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2425SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2426 const MachineBasicBlock *Dst) const {
2428 const BasicBlock *SrcBB = Src->getBasicBlock();
2429 const BasicBlock *DstBB = Dst->getBasicBlock();
2430 if (!BPI) {
2431 // If BPI is not available, set the default probability as 1 / N, where N is
2432 // the number of successors.
2433 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2434 return BranchProbability(1, SuccSize);
2435 }
2436 return BPI->getEdgeProbability(SrcBB, DstBB);
2437}
2438
2439void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2440 MachineBasicBlock *Dst,
2441 BranchProbability Prob) {
2442 if (!FuncInfo.BPI)
2443 Src->addSuccessorWithoutProb(Dst);
2444 else {
2445 if (Prob.isUnknown())
2446 Prob = getEdgeProbability(Src, Dst);
2447 Src->addSuccessor(Dst, Prob);
2448 }
2449}
2450
2451static bool InBlock(const Value *V, const BasicBlock *BB) {
2452 if (const Instruction *I = dyn_cast<Instruction>(V))
2453 return I->getParent() == BB;
2454 return true;
2455}
2456
2457/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2458/// This function emits a branch and is used at the leaves of an OR or an
2459/// AND operator tree.
2460void
2463 MachineBasicBlock *FBB,
2464 MachineBasicBlock *CurBB,
2465 MachineBasicBlock *SwitchBB,
2466 BranchProbability TProb,
2467 BranchProbability FProb,
2468 bool InvertCond) {
2469 const BasicBlock *BB = CurBB->getBasicBlock();
2470
2471 // If the leaf of the tree is a comparison, merge the condition into
2472 // the caseblock.
2473 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2474 // The operands of the cmp have to be in this block. We don't know
2475 // how to export them from some other block. If this is the first block
2476 // of the sequence, no exporting is needed.
2477 if (CurBB == SwitchBB ||
2478 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2479 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2480 ISD::CondCode Condition;
2481 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2482 ICmpInst::Predicate Pred =
2483 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2484 Condition = getICmpCondCode(Pred);
2485 } else {
2486 const FCmpInst *FC = cast<FCmpInst>(Cond);
2487 FCmpInst::Predicate Pred =
2488 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2489 Condition = getFCmpCondCode(Pred);
2490 if (FC->hasNoNaNs() ||
2491 (isKnownNeverNaN(FC->getOperand(0),
2492 SimplifyQuery(DAG.getDataLayout(), FC)) &&
2493 isKnownNeverNaN(FC->getOperand(1),
2494 SimplifyQuery(DAG.getDataLayout(), FC))))
2495 Condition = getFCmpCodeWithoutNaN(Condition);
2496 }
2497
2498 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2499 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2500 SL->SwitchCases.push_back(CB);
2501 return;
2502 }
2503 }
2504
2505 // Create a CaseBlock record representing this branch.
2506 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2507 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2508 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2509 SL->SwitchCases.push_back(CB);
2510}
2511
2512// Collect dependencies on V recursively. This is used for the cost analysis in
2513// `shouldKeepJumpConditionsTogether`.
2517 unsigned Depth = 0) {
2518 // Return false if we have an incomplete count.
2520 return false;
2521
2522 auto *I = dyn_cast<Instruction>(V);
2523 if (I == nullptr)
2524 return true;
2525
2526 if (Necessary != nullptr) {
2527 // This instruction is necessary for the other side of the condition so
2528 // don't count it.
2529 if (Necessary->contains(I))
2530 return true;
2531 }
2532
2533 // Already added this dep.
2534 if (!Deps->try_emplace(I, false).second)
2535 return true;
2536
2537 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2538 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2539 Depth + 1))
2540 return false;
2541 return true;
2542}
2543
2546 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2548 if (Params.BaseCost < 0)
2549 return false;
2550
2551 // Baseline cost.
2552 InstructionCost CostThresh = Params.BaseCost;
2553
2554 BranchProbabilityInfo *BPI = nullptr;
2555 if (Params.LikelyBias || Params.UnlikelyBias)
2556 BPI = FuncInfo.BPI;
2557 if (BPI != nullptr) {
2558 // See if we are either likely to get an early out or compute both lhs/rhs
2559 // of the condition.
2560 BasicBlock *IfFalse = I.getSuccessor(0);
2561 BasicBlock *IfTrue = I.getSuccessor(1);
2562
2563 std::optional<bool> Likely;
2564 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2565 Likely = true;
2566 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2567 Likely = false;
2568
2569 if (Likely) {
2570 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2571 // Its likely we will have to compute both lhs and rhs of condition
2572 CostThresh += Params.LikelyBias;
2573 else {
2574 if (Params.UnlikelyBias < 0)
2575 return false;
2576 // Its likely we will get an early out.
2577 CostThresh -= Params.UnlikelyBias;
2578 }
2579 }
2580 }
2581
2582 if (CostThresh <= 0)
2583 return false;
2584
2585 // Collect "all" instructions that lhs condition is dependent on.
2586 // Use map for stable iteration (to avoid non-determanism of iteration of
2587 // SmallPtrSet). The `bool` value is just a dummy.
2589 collectInstructionDeps(&LhsDeps, Lhs);
2590 // Collect "all" instructions that rhs condition is dependent on AND are
2591 // dependencies of lhs. This gives us an estimate on which instructions we
2592 // stand to save by splitting the condition.
2593 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2594 return false;
2595 // Add the compare instruction itself unless its a dependency on the LHS.
2596 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2597 if (!LhsDeps.contains(RhsI))
2598 RhsDeps.try_emplace(RhsI, false);
2599
2600 InstructionCost CostOfIncluding = 0;
2601 // See if this instruction will need to computed independently of whether RHS
2602 // is.
2603 Value *BrCond = I.getCondition();
2604 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2605 for (const auto *U : Ins->users()) {
2606 // If user is independent of RHS calculation we don't need to count it.
2607 if (auto *UIns = dyn_cast<Instruction>(U))
2608 if (UIns != BrCond && !RhsDeps.contains(UIns))
2609 return false;
2610 }
2611 return true;
2612 };
2613
2614 // Prune instructions from RHS Deps that are dependencies of unrelated
2615 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2616 // arbitrary and just meant to cap the how much time we spend in the pruning
2617 // loop. Its highly unlikely to come into affect.
2618 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2619 // Stop after a certain point. No incorrectness from including too many
2620 // instructions.
2621 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2622 const Instruction *ToDrop = nullptr;
2623 for (const auto &InsPair : RhsDeps) {
2624 if (!ShouldCountInsn(InsPair.first)) {
2625 ToDrop = InsPair.first;
2626 break;
2627 }
2628 }
2629 if (ToDrop == nullptr)
2630 break;
2631 RhsDeps.erase(ToDrop);
2632 }
2633
2634 for (const auto &InsPair : RhsDeps) {
2635 // Finally accumulate latency that we can only attribute to computing the
2636 // RHS condition. Use latency because we are essentially trying to calculate
2637 // the cost of the dependency chain.
2638 // Possible TODO: We could try to estimate ILP and make this more precise.
2639 CostOfIncluding += TTI->getInstructionCost(
2640 InsPair.first, TargetTransformInfo::TCK_Latency);
2641
2642 if (CostOfIncluding > CostThresh)
2643 return false;
2644 }
2645 return true;
2646}
2647
2650 MachineBasicBlock *FBB,
2651 MachineBasicBlock *CurBB,
2652 MachineBasicBlock *SwitchBB,
2654 BranchProbability TProb,
2655 BranchProbability FProb,
2656 bool InvertCond) {
2657 // Skip over not part of the tree and remember to invert op and operands at
2658 // next level.
2659 Value *NotCond;
2660 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2661 InBlock(NotCond, CurBB->getBasicBlock())) {
2662 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2663 !InvertCond);
2664 return;
2665 }
2666
2668 const Value *BOpOp0, *BOpOp1;
2669 // Compute the effective opcode for Cond, taking into account whether it needs
2670 // to be inverted, e.g.
2671 // and (not (or A, B)), C
2672 // gets lowered as
2673 // and (and (not A, not B), C)
2675 if (BOp) {
2676 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2677 ? Instruction::And
2678 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2679 ? Instruction::Or
2681 if (InvertCond) {
2682 if (BOpc == Instruction::And)
2683 BOpc = Instruction::Or;
2684 else if (BOpc == Instruction::Or)
2685 BOpc = Instruction::And;
2686 }
2687 }
2688
2689 // If this node is not part of the or/and tree, emit it as a branch.
2690 // Note that all nodes in the tree should have same opcode.
2691 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2692 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2693 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2694 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2695 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2696 TProb, FProb, InvertCond);
2697 return;
2698 }
2699
2700 // Create TmpBB after CurBB.
2701 MachineFunction::iterator BBI(CurBB);
2702 MachineFunction &MF = DAG.getMachineFunction();
2704 CurBB->getParent()->insert(++BBI, TmpBB);
2705
2706 if (Opc == Instruction::Or) {
2707 // Codegen X | Y as:
2708 // BB1:
2709 // jmp_if_X TBB
2710 // jmp TmpBB
2711 // TmpBB:
2712 // jmp_if_Y TBB
2713 // jmp FBB
2714 //
2715
2716 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2717 // The requirement is that
2718 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2719 // = TrueProb for original BB.
2720 // Assuming the original probabilities are A and B, one choice is to set
2721 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2722 // A/(1+B) and 2B/(1+B). This choice assumes that
2723 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2724 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2725 // TmpBB, but the math is more complicated.
2726
2727 auto NewTrueProb = TProb / 2;
2728 auto NewFalseProb = TProb / 2 + FProb;
2729 // Emit the LHS condition.
2730 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2731 NewFalseProb, InvertCond);
2732
2733 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2734 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2736 // Emit the RHS condition into TmpBB.
2737 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2738 Probs[1], InvertCond);
2739 } else {
2740 assert(Opc == Instruction::And && "Unknown merge op!");
2741 // Codegen X & Y as:
2742 // BB1:
2743 // jmp_if_X TmpBB
2744 // jmp FBB
2745 // TmpBB:
2746 // jmp_if_Y TBB
2747 // jmp FBB
2748 //
2749 // This requires creation of TmpBB after CurBB.
2750
2751 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2752 // The requirement is that
2753 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2754 // = FalseProb for original BB.
2755 // Assuming the original probabilities are A and B, one choice is to set
2756 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2757 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2758 // TrueProb for BB1 * FalseProb for TmpBB.
2759
2760 auto NewTrueProb = TProb + FProb / 2;
2761 auto NewFalseProb = FProb / 2;
2762 // Emit the LHS condition.
2763 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2764 NewFalseProb, InvertCond);
2765
2766 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2767 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2769 // Emit the RHS condition into TmpBB.
2770 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2771 Probs[1], InvertCond);
2772 }
2773}
2774
2775/// If the set of cases should be emitted as a series of branches, return true.
2776/// If we should emit this as a bunch of and/or'd together conditions, return
2777/// false.
2778bool
2779SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2780 if (Cases.size() != 2) return true;
2781
2782 // If this is two comparisons of the same values or'd or and'd together, they
2783 // will get folded into a single comparison, so don't emit two blocks.
2784 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2785 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2786 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2787 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2788 return false;
2789 }
2790
2791 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2792 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2793 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2794 Cases[0].CC == Cases[1].CC &&
2795 isa<Constant>(Cases[0].CmpRHS) &&
2796 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2797 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2798 return false;
2799 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2800 return false;
2801 }
2802
2803 return true;
2804}
2805
2806void SelectionDAGBuilder::visitUncondBr(const UncondBrInst &I) {
2808
2809 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2810
2811 // Update machine-CFG edges.
2812 BrMBB->addSuccessor(Succ0MBB);
2813
2814 // If this is not a fall-through branch or optimizations are switched off,
2815 // emit the branch.
2816 if (Succ0MBB != NextBlock(BrMBB) ||
2818 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
2819 DAG.getBasicBlock(Succ0MBB));
2820 setValue(&I, Br);
2821 DAG.setRoot(Br);
2822 }
2823}
2824
2825void SelectionDAGBuilder::visitCondBr(const CondBrInst &I) {
2826 MachineBasicBlock *BrMBB = FuncInfo.MBB;
2827
2828 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2829
2830 // If this condition is one of the special cases we handle, do special stuff
2831 // now.
2832 const Value *CondVal = I.getCondition();
2833 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2834
2835 // If this is a series of conditions that are or'd or and'd together, emit
2836 // this as a sequence of branches instead of setcc's with and/or operations.
2837 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2838 // unpredictable branches, and vector extracts because those jumps are likely
2839 // expensive for any target), this should improve performance.
2840 // For example, instead of something like:
2841 // cmp A, B
2842 // C = seteq
2843 // cmp D, E
2844 // F = setle
2845 // or C, F
2846 // jnz foo
2847 // Emit:
2848 // cmp A, B
2849 // je foo
2850 // cmp D, E
2851 // jle foo
2852 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2853 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2854 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2855 BOp->hasOneUse() && !IsUnpredictable) {
2856 Value *Vec;
2857 const Value *BOp0, *BOp1;
2859 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2860 Opcode = Instruction::And;
2861 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2862 Opcode = Instruction::Or;
2863
2864 if (Opcode &&
2865 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2866 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2868 FuncInfo, I, Opcode, BOp0, BOp1,
2869 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2870 Opcode, BOp0, BOp1))) {
2871 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2872 getEdgeProbability(BrMBB, Succ0MBB),
2873 getEdgeProbability(BrMBB, Succ1MBB),
2874 /*InvertCond=*/false);
2875 // If the compares in later blocks need to use values not currently
2876 // exported from this block, export them now. This block should always
2877 // be the first entry.
2878 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2879
2880 // Allow some cases to be rejected.
2881 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2882 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2883 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2884 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2885 }
2886
2887 // Emit the branch for this block.
2888 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2889 SL->SwitchCases.erase(SL->SwitchCases.begin());
2890 return;
2891 }
2892
2893 // Okay, we decided not to do this, remove any inserted MBB's and clear
2894 // SwitchCases.
2895 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2896 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2897
2898 SL->SwitchCases.clear();
2899 }
2900 }
2901
2902 // Create a CaseBlock record representing this branch.
2903 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2904 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2906 IsUnpredictable);
2907
2908 // Use visitSwitchCase to actually insert the fast branch sequence for this
2909 // cond branch.
2910 visitSwitchCase(CB, BrMBB);
2911}
2912
2913/// visitSwitchCase - Emits the necessary code to represent a single node in
2914/// the binary search tree resulting from lowering a switch instruction.
2916 MachineBasicBlock *SwitchBB) {
2917 SDValue Cond;
2918 SDValue CondLHS = getValue(CB.CmpLHS);
2919 SDLoc dl = CB.DL;
2920
2921 if (CB.CC == ISD::SETTRUE) {
2922 // Branch or fall through to TrueBB.
2923 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2924 SwitchBB->normalizeSuccProbs();
2925 if (CB.TrueBB != NextBlock(SwitchBB)) {
2926 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2927 DAG.getBasicBlock(CB.TrueBB)));
2928 }
2929 return;
2930 }
2931
2932 auto &TLI = DAG.getTargetLoweringInfo();
2933 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2934
2935 // Build the setcc now.
2936 if (!CB.CmpMHS) {
2937 // Fold "(X == true)" to X and "(X == false)" to !X to
2938 // handle common cases produced by branch lowering.
2939 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2940 CB.CC == ISD::SETEQ)
2941 Cond = CondLHS;
2942 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2943 CB.CC == ISD::SETEQ) {
2944 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2945 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2946 } else {
2947 SDValue CondRHS = getValue(CB.CmpRHS);
2948
2949 // If a pointer's DAG type is larger than its memory type then the DAG
2950 // values are zero-extended. This breaks signed comparisons so truncate
2951 // back to the underlying type before doing the compare.
2952 if (CondLHS.getValueType() != MemVT) {
2953 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2954 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2955 }
2956 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2957 }
2958 } else {
2959 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2960
2961 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2962 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2963
2964 SDValue CmpOp = getValue(CB.CmpMHS);
2965 EVT VT = CmpOp.getValueType();
2966
2967 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2968 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2969 ISD::SETLE);
2970 } else {
2971 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2972 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2973 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2974 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2975 }
2976 }
2977
2978 // Update successor info
2979 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2980 // TrueBB and FalseBB are always different unless the incoming IR is
2981 // degenerate. This only happens when running llc on weird IR.
2982 if (CB.TrueBB != CB.FalseBB)
2983 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2984 SwitchBB->normalizeSuccProbs();
2985
2986 // If the lhs block is the next block, invert the condition so that we can
2987 // fall through to the lhs instead of the rhs block.
2988 if (CB.TrueBB == NextBlock(SwitchBB)) {
2989 std::swap(CB.TrueBB, CB.FalseBB);
2990 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2991 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2992 }
2993
2994 SDNodeFlags Flags;
2996 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2997 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2998
2999 setValue(CurInst, BrCond);
3000
3001 // Insert the false branch. Do this even if it's a fall through branch,
3002 // this makes it easier to do DAG optimizations which require inverting
3003 // the branch condition.
3004 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3005 DAG.getBasicBlock(CB.FalseBB));
3006
3007 DAG.setRoot(BrCond);
3008}
3009
3010/// visitJumpTable - Emit JumpTable node in the current MBB
3012 // Emit the code for the jump table
3013 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3014 assert(JT.Reg && "Should lower JT Header first!");
3015 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
3016 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
3017 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
3018 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
3019 Index.getValue(1), Table, Index);
3020 DAG.setRoot(BrJumpTable);
3021}
3022
3023/// visitJumpTableHeader - This function emits necessary code to produce index
3024/// in the JumpTable from switch case.
3026 JumpTableHeader &JTH,
3027 MachineBasicBlock *SwitchBB) {
3028 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3029 const SDLoc &dl = *JT.SL;
3030
3031 // Subtract the lowest switch case value from the value being switched on.
3032 SDValue SwitchOp = getValue(JTH.SValue);
3033 EVT VT = SwitchOp.getValueType();
3034 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
3035 DAG.getConstant(JTH.First, dl, VT));
3036
3037 // The SDNode we just created, which holds the value being switched on minus
3038 // the smallest case value, needs to be copied to a virtual register so it
3039 // can be used as an index into the jump table in a subsequent basic block.
3040 // This value may be smaller or larger than the target's pointer type, and
3041 // therefore require extension or truncating.
3042 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3043 SwitchOp =
3044 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
3045
3046 Register JumpTableReg =
3047 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
3048 SDValue CopyTo =
3049 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3050 JT.Reg = JumpTableReg;
3051
3052 if (!JTH.FallthroughUnreachable) {
3053 // Emit the range check for the jump table, and branch to the default block
3054 // for the switch statement if the value being switched on exceeds the
3055 // largest case in the switch.
3056 SDValue CMP = DAG.getSetCC(
3057 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3058 Sub.getValueType()),
3059 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3060
3061 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3062 MVT::Other, CopyTo, CMP,
3063 DAG.getBasicBlock(JT.Default));
3064
3065 // Avoid emitting unnecessary branches to the next block.
3066 if (JT.MBB != NextBlock(SwitchBB))
3067 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3068 DAG.getBasicBlock(JT.MBB));
3069
3070 DAG.setRoot(BrCond);
3071 } else {
3072 // Avoid emitting unnecessary branches to the next block.
3073 if (JT.MBB != NextBlock(SwitchBB))
3074 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3075 DAG.getBasicBlock(JT.MBB)));
3076 else
3077 DAG.setRoot(CopyTo);
3078 }
3079}
3080
3081/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3082/// variable if there exists one.
3084 SDValue &Chain) {
3085 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3086 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3087 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3089 Value *Global =
3092 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3093 if (Global) {
3094 MachinePointerInfo MPInfo(Global);
3098 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3099 DAG.setNodeMemRefs(Node, {MemRef});
3100 }
3101 if (PtrTy != PtrMemTy)
3102 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3103 return SDValue(Node, 0);
3104}
3105
3106/// Codegen a new tail for a stack protector check ParentMBB which has had its
3107/// tail spliced into a stack protector check success bb.
3108///
3109/// For a high level explanation of how this fits into the stack protector
3110/// generation see the comment on the declaration of class
3111/// StackProtectorDescriptor.
3113 MachineBasicBlock *ParentBB) {
3114
3115 // First create the loads to the guard/stack slot for the comparison.
3116 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3117 auto &DL = DAG.getDataLayout();
3118 EVT PtrTy = TLI.getFrameIndexTy(DL);
3119 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3120
3121 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3122 int FI = MFI.getStackProtectorIndex();
3123
3124 SDValue Guard;
3125 SDLoc dl = getCurSDLoc();
3126 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3127 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3128 Align Align = DL.getPrefTypeAlign(
3129 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3130
3131 // Generate code to load the content of the guard slot.
3132 SDValue GuardVal = DAG.getLoad(
3133 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3134 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3136
3137 // If cookie mixing is enabled, unmix the stored GuardVal to get back the
3138 // original cookie for comparison. The prologue stored (FP - Cookie) or
3139 // (FP XOR Cookie), so we apply the same operation again to unmix:
3140 // FP - (FP - Cookie) = Cookie, or (FP XOR Cookie) XOR FP = Cookie.
3141 if (TLI.useStackGuardMixFP())
3142 GuardVal = TLI.emitStackGuardMixFP(DAG, GuardVal, dl);
3143
3144 // If we're using function-based instrumentation, call the guard check
3145 // function
3147 // Get the guard check function from the target and verify it exists since
3148 // we're using function-based instrumentation
3149 const Function *GuardCheckFn =
3150 TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3151 assert(GuardCheckFn && "Guard check function is null");
3152
3153 // The target provides a guard check function to validate the guard value.
3154 // Generate a call to that function with the content of the guard slot as
3155 // argument.
3156 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3157 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3158
3160 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3161 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3162 Entry.IsInReg = true;
3163 Args.push_back(Entry);
3164
3167 .setChain(DAG.getEntryNode())
3168 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3169 getValue(GuardCheckFn), std::move(Args));
3170
3171 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3172 DAG.setRoot(Result.second);
3173 return;
3174 }
3175
3176 // Load the fresh guard value for comparison.
3177 // For targets that mix the cookie in LOAD_STACK_GUARD expansion, we need to
3178 // load directly without using LOAD_STACK_GUARD to avoid unwanted mixing.
3179 SDValue Chain = DAG.getEntryNode();
3180 if (TLI.useStackGuardMixFP()) {
3181 // Mixing targets: load cookie directly to avoid mixing in LOAD_STACK_GUARD
3182 if (const Value *IRGuard = TLI.getSDagStackGuard(M, DAG.getLibcalls())) {
3183 SDValue GuardPtr = getValue(IRGuard);
3184 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3185 MachinePointerInfo(IRGuard, 0), Align,
3187 } else {
3188 LLVMContext &Ctx = *DAG.getContext();
3189 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
3190 Guard = DAG.getPOISON(PtrMemTy);
3191 }
3192 } else {
3193 // Non-mixing targets: use LOAD_STACK_GUARD or direct load as usual
3194 if (TLI.useLoadStackGuardNode(M)) {
3195 Guard = getLoadStackGuard(DAG, dl, Chain);
3196 } else {
3197 if (const Value *IRGuard = TLI.getSDagStackGuard(M, DAG.getLibcalls())) {
3198 SDValue GuardPtr = getValue(IRGuard);
3199 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3200 MachinePointerInfo(IRGuard, 0), Align,
3202 } else {
3203 LLVMContext &Ctx = *DAG.getContext();
3204 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
3205 Guard = DAG.getPOISON(PtrMemTy);
3206 }
3207 }
3208 }
3209
3210 // Now both Guard (fresh cookie) and GuardVal (unmixed from stored value)
3211 // contain unmixed cookie values that can be compared directly.
3212
3213 // Perform the comparison via a getsetcc.
3214 SDValue Cmp = DAG.getSetCC(
3215 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3216 Guard, GuardVal, ISD::SETNE);
3217
3218 // If the guard/stackslot do not equal, branch to failure MBB.
3219 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
3220 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3221 // Otherwise branch to success MBB.
3222 SDValue Br = DAG.getNode(ISD::BR, dl,
3223 MVT::Other, BrCond,
3224 DAG.getBasicBlock(SPD.getSuccessMBB()));
3225
3226 DAG.setRoot(Br);
3227}
3228
3229/// Codegen the failure basic block for a stack protector check.
3230///
3231/// A failure stack protector machine basic block consists simply of a call to
3232/// __stack_chk_fail().
3233///
3234/// For a high level explanation of how this fits into the stack protector
3235/// generation see the comment on the declaration of class
3236/// StackProtectorDescriptor.
3239
3240 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3241 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3242 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3243 SDValue Chain;
3244
3245 // For -Oz builds with a guard check function, we use function-based
3246 // instrumentation. Otherwise, if we have a guard check function, we call it
3247 // in the failure block.
3248 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3249 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3250 // First create the loads to the guard/stack slot for the comparison.
3251 auto &DL = DAG.getDataLayout();
3252 EVT PtrTy = TLI.getFrameIndexTy(DL);
3253 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3254
3255 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3256 int FI = MFI.getStackProtectorIndex();
3257
3258 SDLoc dl = getCurSDLoc();
3259 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3260 Align Align = DL.getPrefTypeAlign(
3261 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3262
3263 // Generate code to load the content of the guard slot.
3264 SDValue GuardVal = DAG.getLoad(
3265 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3266 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3268
3269 if (TLI.useStackGuardMixFP())
3270 GuardVal = TLI.emitStackGuardMixFP(DAG, GuardVal, dl);
3271
3272 // The target provides a guard check function to validate the guard value.
3273 // Generate a call to that function with the content of the guard slot as
3274 // argument.
3275 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3276 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3277
3279 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3280 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3281 Entry.IsInReg = true;
3282 Args.push_back(Entry);
3283
3286 .setChain(DAG.getEntryNode())
3287 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3288 getValue(GuardCheckFn), std::move(Args));
3289
3290 Chain = TLI.LowerCallTo(CLI).second;
3291 } else {
3293 CallOptions.setDiscardResult(true);
3294 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3295 {}, CallOptions, getCurSDLoc())
3296 .second;
3297 }
3298
3299 // Emit a trap instruction if we are required to do so.
3300 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3301 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3302 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3303
3304 DAG.setRoot(Chain);
3305}
3306
3307/// visitBitTestHeader - This function emits necessary code to produce value
3308/// suitable for "bit tests"
3310 MachineBasicBlock *SwitchBB) {
3311 SDLoc dl = getCurSDLoc();
3312
3313 // Subtract the minimum value.
3314 SDValue SwitchOp = getValue(B.SValue);
3315 EVT VT = SwitchOp.getValueType();
3316 SDValue RangeSub =
3317 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3318
3319 // Determine the type of the test operands.
3320 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3321 bool UsePtrType = false;
3322 if (!TLI.isTypeLegal(VT)) {
3323 UsePtrType = true;
3324 } else {
3325 for (const BitTestCase &Case : B.Cases)
3326 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3327 // Switch table case range are encoded into series of masks.
3328 // Just use pointer type, it's guaranteed to fit.
3329 UsePtrType = true;
3330 break;
3331 }
3332 }
3333 SDValue Sub = RangeSub;
3334 if (UsePtrType) {
3335 VT = TLI.getPointerTy(DAG.getDataLayout());
3336 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3337 }
3338
3339 B.RegVT = VT.getSimpleVT();
3340 B.Reg = FuncInfo.CreateReg(B.RegVT);
3341 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3342
3343 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3344
3345 if (!B.FallthroughUnreachable)
3346 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3347 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3348 SwitchBB->normalizeSuccProbs();
3349
3350 SDValue Root = CopyTo;
3351 if (!B.FallthroughUnreachable) {
3352 // Conditional branch to the default block.
3353 SDValue RangeCmp = DAG.getSetCC(dl,
3354 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3355 RangeSub.getValueType()),
3356 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3357 ISD::SETUGT);
3358
3359 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3360 DAG.getBasicBlock(B.Default));
3361 }
3362
3363 // Avoid emitting unnecessary branches to the next block.
3364 if (MBB != NextBlock(SwitchBB))
3365 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3366
3367 DAG.setRoot(Root);
3368}
3369
3370/// visitBitTestCase - this function produces one "bit test"
3372 MachineBasicBlock *NextMBB,
3373 BranchProbability BranchProbToNext,
3374 Register Reg, BitTestCase &B,
3375 MachineBasicBlock *SwitchBB) {
3376 SDLoc dl = getCurSDLoc();
3377 MVT VT = BB.RegVT;
3378 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3379 SDValue Cmp;
3380 unsigned PopCount = llvm::popcount(B.Mask);
3381 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3382 if (PopCount == 1) {
3383 // Testing for a single bit; just compare the shift count with what it
3384 // would need to be to shift a 1 bit in that position.
3385 Cmp = DAG.getSetCC(
3386 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3387 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3388 ISD::SETEQ);
3389 } else if (PopCount == BB.Range) {
3390 // There is only one zero bit in the range, test for it directly.
3391 Cmp = DAG.getSetCC(
3392 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3393 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3394 } else {
3395 // Make desired shift
3396 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3397 DAG.getConstant(1, dl, VT), ShiftOp);
3398
3399 // Emit bit tests and jumps
3400 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3401 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3402 Cmp = DAG.getSetCC(
3403 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3404 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3405 }
3406
3407 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3408 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3409 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3410 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3411 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3412 // one as they are relative probabilities (and thus work more like weights),
3413 // and hence we need to normalize them to let the sum of them become one.
3414 SwitchBB->normalizeSuccProbs();
3415
3416 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3417 MVT::Other, getControlRoot(),
3418 Cmp, DAG.getBasicBlock(B.TargetBB));
3419
3420 // Avoid emitting unnecessary branches to the next block.
3421 if (NextMBB != NextBlock(SwitchBB))
3422 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3423 DAG.getBasicBlock(NextMBB));
3424
3425 DAG.setRoot(BrAnd);
3426}
3427
3428void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3429 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3430
3431 // Retrieve successors. Look through artificial IR level blocks like
3432 // catchswitch for successors.
3433 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3434 const BasicBlock *EHPadBB = I.getSuccessor(1);
3435 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3436
3437 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3438 // have to do anything here to lower funclet bundles.
3439 failForInvalidBundles(I, "invokes",
3445
3446 const Value *Callee(I.getCalledOperand());
3447 const Function *Fn = dyn_cast<Function>(Callee);
3448 if (isa<InlineAsm>(Callee))
3449 visitInlineAsm(I, EHPadBB);
3450 else if (Fn && Fn->isIntrinsic()) {
3451 switch (Fn->getIntrinsicID()) {
3452 default:
3453 llvm_unreachable("Cannot invoke this intrinsic");
3454 case Intrinsic::donothing:
3455 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3456 case Intrinsic::seh_try_begin:
3457 case Intrinsic::seh_scope_begin:
3458 case Intrinsic::seh_try_end:
3459 case Intrinsic::seh_scope_end:
3460 if (EHPadMBB)
3461 // a block referenced by EH table
3462 // so dtor-funclet not removed by opts
3463 EHPadMBB->setMachineBlockAddressTaken();
3464 break;
3465 case Intrinsic::experimental_patchpoint_void:
3466 case Intrinsic::experimental_patchpoint:
3467 visitPatchpoint(I, EHPadBB);
3468 break;
3469 case Intrinsic::experimental_gc_statepoint:
3471 break;
3472 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3473 // but these intrinsics are special because they can be invoked, so we
3474 // manually lower it to a DAG node here.
3475 case Intrinsic::wasm_throw: {
3477 std::array<SDValue, 4> Ops = {
3478 getControlRoot(), // inchain for the terminator node
3479 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3481 getValue(I.getArgOperand(0)), // tag
3482 getValue(I.getArgOperand(1)) // thrown value
3483 };
3484 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3485 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3486 break;
3487 }
3488 case Intrinsic::wasm_rethrow: {
3489 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3490 std::array<SDValue, 2> Ops = {
3491 getControlRoot(), // inchain for the terminator node
3492 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3493 TLI.getPointerTy(DAG.getDataLayout()))};
3494 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3495 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3496 break;
3497 }
3498 }
3499 } else if (I.hasDeoptState()) {
3500 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3501 // Eventually we will support lowering the @llvm.experimental.deoptimize
3502 // intrinsic, and right now there are no plans to support other intrinsics
3503 // with deopt state.
3504 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3505 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3507 } else {
3508 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3509 }
3510
3511 // If the value of the invoke is used outside of its defining block, make it
3512 // available as a virtual register.
3513 // We already took care of the exported value for the statepoint instruction
3514 // during call to the LowerStatepoint.
3515 if (!isa<GCStatepointInst>(I)) {
3517 }
3518
3520 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3521 BranchProbability EHPadBBProb =
3522 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3524 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3525
3526 // Update successor info.
3527 addSuccessorWithProb(InvokeMBB, Return);
3528 for (auto &UnwindDest : UnwindDests) {
3529 UnwindDest.first->setIsEHPad();
3530 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3531 }
3532 InvokeMBB->normalizeSuccProbs();
3533
3534 // Drop into normal successor.
3535 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3536 DAG.getBasicBlock(Return)));
3537}
3538
3539/// The intrinsics currently supported by callbr are implicit control flow
3540/// intrinsics such as amdgcn.kill.
3541/// - they should be called (no "dontcall-" attributes)
3542/// - they do not touch memory on the target (= !TLI.getTgtMemIntrinsic())
3543/// - they do not need custom argument handling (no
3544/// TLI.CollectTargetIntrinsicOperands())
3545void SelectionDAGBuilder::visitCallBrIntrinsic(const CallBrInst &I) {
3546#ifndef NDEBUG
3548 DAG.getTargetLoweringInfo().getTgtMemIntrinsic(
3549 Infos, I, DAG.getMachineFunction(), I.getIntrinsicID());
3550 assert(Infos.empty() && "Intrinsic touches memory");
3551#endif
3552
3553 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
3554
3556 getTargetIntrinsicOperands(I, HasChain, OnlyLoad);
3557 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
3558
3559 // Create the node.
3560 SDValue Result =
3561 getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
3562 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
3563
3564 setValue(&I, Result);
3565}
3566
3567void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3568 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3569
3570 if (I.isInlineAsm()) {
3571 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3572 // have to do anything here to lower funclet bundles.
3573 failForInvalidBundles(I, "callbrs",
3575 visitInlineAsm(I);
3576 } else {
3577 assert(!I.hasOperandBundles() &&
3578 "Can't have operand bundles for intrinsics");
3579 visitCallBrIntrinsic(I);
3580 }
3582
3583 // Retrieve successors.
3584 SmallPtrSet<BasicBlock *, 8> Dests;
3585 Dests.insert(I.getDefaultDest());
3586 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3587
3588 // Update successor info.
3589 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3590 // TODO: For most of the cases where there is an intrinsic callbr, we're
3591 // having exactly one indirect target, which will be unreachable. As soon as
3592 // this changes, we might need to enhance
3593 // Target->setIsInlineAsmBrIndirectTarget or add something similar for
3594 // intrinsic indirect branches.
3595 if (I.isInlineAsm()) {
3596 for (BasicBlock *Dest : I.getIndirectDests()) {
3597 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
3598 Target->setIsInlineAsmBrIndirectTarget();
3599 // If we introduce a type of asm goto statement that is permitted to use
3600 // an indirect call instruction to jump to its labels, then we should add
3601 // a call to Target->setMachineBlockAddressTaken() here, to mark the
3602 // target block as requiring a BTI.
3603
3604 Target->setLabelMustBeEmitted();
3605 // Don't add duplicate machine successors.
3606 if (Dests.insert(Dest).second)
3607 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3608 }
3609 }
3610 CallBrMBB->normalizeSuccProbs();
3611
3612 // Drop into default successor.
3613 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3614 MVT::Other, getControlRoot(),
3615 DAG.getBasicBlock(Return)));
3616}
3617
3618void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3619 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3620}
3621
3622void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3623 assert(FuncInfo.MBB->isEHPad() &&
3624 "Call to landingpad not in landing pad!");
3625
3626 // If there aren't registers to copy the values into (e.g., during SjLj
3627 // exceptions), then don't bother to create these DAG nodes.
3628 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3629 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3630 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3631 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3632 return;
3633
3634 // If landingpad's return type is token type, we don't create DAG nodes
3635 // for its exception pointer and selector value. The extraction of exception
3636 // pointer or selector value from token type landingpads is not currently
3637 // supported.
3638 if (LP.getType()->isTokenTy())
3639 return;
3640
3641 SmallVector<EVT, 2> ValueVTs;
3642 SDLoc dl = getCurSDLoc();
3643 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3644 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3645
3646 // Get the two live-in registers as SDValues. The physregs have already been
3647 // copied into virtual registers.
3648 SDValue Ops[2];
3649 if (FuncInfo.ExceptionPointerVirtReg) {
3650 Ops[0] = DAG.getZExtOrTrunc(
3651 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3652 FuncInfo.ExceptionPointerVirtReg,
3653 TLI.getPointerTy(DAG.getDataLayout())),
3654 dl, ValueVTs[0]);
3655 } else {
3656 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3657 }
3658 Ops[1] = DAG.getZExtOrTrunc(
3659 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3660 FuncInfo.ExceptionSelectorVirtReg,
3661 TLI.getPointerTy(DAG.getDataLayout())),
3662 dl, ValueVTs[1]);
3663
3664 // Merge into one.
3665 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3666 DAG.getVTList(ValueVTs), Ops);
3667 setValue(&LP, Res);
3668}
3669
3672 // Update JTCases.
3673 for (JumpTableBlock &JTB : SL->JTCases)
3674 if (JTB.first.HeaderBB == First)
3675 JTB.first.HeaderBB = Last;
3676
3677 // Update BitTestCases.
3678 for (BitTestBlock &BTB : SL->BitTestCases)
3679 if (BTB.Parent == First)
3680 BTB.Parent = Last;
3681}
3682
3683void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3684 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3685
3686 // Update machine-CFG edges with unique successors.
3688 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3689 BasicBlock *BB = I.getSuccessor(i);
3690 bool Inserted = Done.insert(BB).second;
3691 if (!Inserted)
3692 continue;
3693
3694 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3695 addSuccessorWithProb(IndirectBrMBB, Succ);
3696 }
3697 IndirectBrMBB->normalizeSuccProbs();
3698
3700 MVT::Other, getControlRoot(),
3701 getValue(I.getAddress())));
3702}
3703
3704void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3705 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3706 DAG.getTarget().Options.NoTrapAfterNoreturn))
3707 return;
3708
3709 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3710}
3711
3712void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3713 SDNodeFlags Flags;
3714 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3715 Flags.copyFMF(*FPOp);
3716
3717 SDValue Op = getValue(I.getOperand(0));
3718 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3719 Op, Flags);
3720 setValue(&I, UnNodeValue);
3721}
3722
3723void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3724 SDNodeFlags Flags;
3725 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3726 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3727 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3728 }
3729 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3730 Flags.setExact(ExactOp->isExact());
3731 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3732 Flags.setDisjoint(DisjointOp->isDisjoint());
3733 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3734 Flags.copyFMF(*FPOp);
3735
3736 SDValue Op1 = getValue(I.getOperand(0));
3737 SDValue Op2 = getValue(I.getOperand(1));
3738 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3739 Op1, Op2, Flags);
3740 setValue(&I, BinNodeValue);
3741}
3742
3743void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3744 SDValue Op1 = getValue(I.getOperand(0));
3745 SDValue Op2 = getValue(I.getOperand(1));
3746
3747 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3748 Op1.getValueType(), DAG.getDataLayout());
3749
3750 // Coerce the shift amount to the right type if we can. This exposes the
3751 // truncate or zext to optimization early.
3752 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3754 "Unexpected shift type");
3755 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3756 }
3757
3758 bool nuw = false;
3759 bool nsw = false;
3760 bool exact = false;
3761
3762 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3763
3764 if (const OverflowingBinaryOperator *OFBinOp =
3766 nuw = OFBinOp->hasNoUnsignedWrap();
3767 nsw = OFBinOp->hasNoSignedWrap();
3768 }
3769 if (const PossiblyExactOperator *ExactOp =
3771 exact = ExactOp->isExact();
3772 }
3773 SDNodeFlags Flags;
3774 Flags.setExact(exact);
3775 Flags.setNoSignedWrap(nsw);
3776 Flags.setNoUnsignedWrap(nuw);
3777 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3778 Flags);
3779 setValue(&I, Res);
3780}
3781
3782void SelectionDAGBuilder::visitSDiv(const User &I) {
3783 SDValue Op1 = getValue(I.getOperand(0));
3784 SDValue Op2 = getValue(I.getOperand(1));
3785
3786 SDNodeFlags Flags;
3787 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3788 cast<PossiblyExactOperator>(&I)->isExact());
3789 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3790 Op2, Flags));
3791}
3792
3793void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3794 ICmpInst::Predicate predicate = I.getPredicate();
3795 SDValue Op1 = getValue(I.getOperand(0));
3796 SDValue Op2 = getValue(I.getOperand(1));
3797 ISD::CondCode Opcode = getICmpCondCode(predicate);
3798
3799 auto &TLI = DAG.getTargetLoweringInfo();
3800 EVT MemVT =
3801 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3802
3803 // If a pointer's DAG type is larger than its memory type then the DAG values
3804 // are zero-extended. This breaks signed comparisons so truncate back to the
3805 // underlying type before doing the compare.
3806 if (Op1.getValueType() != MemVT) {
3807 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3808 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3809 }
3810
3811 SDNodeFlags Flags;
3812 Flags.setSameSign(I.hasSameSign());
3813
3814 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3815 I.getType());
3816 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode,
3817 /*Chain=*/{}, /*IsSignaling=*/false, Flags));
3818}
3819
3820void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3821 FCmpInst::Predicate predicate = I.getPredicate();
3822 SDValue Op1 = getValue(I.getOperand(0));
3823 SDValue Op2 = getValue(I.getOperand(1));
3824
3825 ISD::CondCode Condition = getFCmpCondCode(predicate);
3826 auto *FPMO = cast<FPMathOperator>(&I);
3827 if (FPMO->hasNoNaNs() ||
3828 (DAG.isKnownNeverNaN(Op1) && DAG.isKnownNeverNaN(Op2)))
3829 Condition = getFCmpCodeWithoutNaN(Condition);
3830
3831 SDNodeFlags Flags;
3832 Flags.copyFMF(*FPMO);
3833
3834 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3835 I.getType());
3836 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition,
3837 /*Chain=*/{}, /*IsSignaling=*/false, Flags));
3838}
3839
3840// Check if the condition of the select has one use or two users that are both
3841// selects with the same condition.
3842static bool hasOnlySelectUsers(const Value *Cond) {
3843 return llvm::all_of(Cond->users(), [](const Value *V) {
3844 return isa<SelectInst>(V);
3845 });
3846}
3847
3848void SelectionDAGBuilder::visitSelect(const User &I) {
3849 SmallVector<EVT, 4> ValueVTs;
3850 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3851 ValueVTs);
3852 unsigned NumValues = ValueVTs.size();
3853 if (NumValues == 0) return;
3854
3856 SDValue Cond = getValue(I.getOperand(0));
3857 SDValue LHSVal = getValue(I.getOperand(1));
3858 SDValue RHSVal = getValue(I.getOperand(2));
3859 SmallVector<SDValue, 1> BaseOps(1, Cond);
3861 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3862
3863 bool IsUnaryAbs = false;
3864 bool Negate = false;
3865
3866 SDNodeFlags Flags;
3867 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3868 Flags.copyFMF(*FPOp);
3869
3870 Flags.setUnpredictable(
3871 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3872
3873 // Min/max matching is only viable if all output VTs are the same.
3874 if (all_equal(ValueVTs)) {
3875 EVT VT = ValueVTs[0];
3876 LLVMContext &Ctx = *DAG.getContext();
3877 auto &TLI = DAG.getTargetLoweringInfo();
3878
3879 // We care about the legality of the operation after it has been type
3880 // legalized.
3881 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3882 VT = TLI.getTypeToTransformTo(Ctx, VT);
3883
3884 // If the vselect is legal, assume we want to leave this as a vector setcc +
3885 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3886 // min/max is legal on the scalar type.
3887 bool UseScalarMinMax = VT.isVector() &&
3889
3890 // ValueTracking's select pattern matching does not account for -0.0,
3891 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3892 // -0.0 is less than +0.0.
3893 const Value *LHS, *RHS;
3894 auto SPR = matchSelectPattern(&I, LHS, RHS);
3896 switch (SPR.Flavor) {
3897 case SPF_UMAX: Opc = ISD::UMAX; break;
3898 case SPF_UMIN: Opc = ISD::UMIN; break;
3899 case SPF_SMAX: Opc = ISD::SMAX; break;
3900 case SPF_SMIN: Opc = ISD::SMIN; break;
3901 case SPF_FMINNUM:
3903 break;
3904
3905 switch (SPR.NaNBehavior) {
3906 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3907 case SPNB_RETURNS_NAN: break;
3908 case SPNB_RETURNS_OTHER:
3910 Flags.setNoSignedZeros(true);
3911 break;
3912 case SPNB_RETURNS_ANY:
3914 (UseScalarMinMax &&
3916 Opc = ISD::FMINNUM;
3917 break;
3918 }
3919 break;
3920 case SPF_FMAXNUM:
3922 break;
3923
3924 switch (SPR.NaNBehavior) {
3925 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3926 case SPNB_RETURNS_NAN: break;
3927 case SPNB_RETURNS_OTHER:
3929 Flags.setNoSignedZeros(true);
3930 break;
3931 case SPNB_RETURNS_ANY:
3933 (UseScalarMinMax &&
3935 Opc = ISD::FMAXNUM;
3936 break;
3937 }
3938 break;
3939 case SPF_NABS:
3940 Negate = true;
3941 [[fallthrough]];
3942 case SPF_ABS:
3943 IsUnaryAbs = true;
3944 Opc = ISD::ABS;
3945 break;
3946 default: break;
3947 }
3948
3949 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3950 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3951 (UseScalarMinMax &&
3953 // If the underlying comparison instruction is used by any other
3954 // instruction, the consumed instructions won't be destroyed, so it is
3955 // not profitable to convert to a min/max.
3957 OpCode = Opc;
3958 LHSVal = getValue(LHS);
3959 RHSVal = getValue(RHS);
3960 BaseOps.clear();
3961 }
3962
3963 if (IsUnaryAbs) {
3964 OpCode = Opc;
3965 LHSVal = getValue(LHS);
3966 BaseOps.clear();
3967 }
3968 }
3969
3970 if (IsUnaryAbs) {
3971 for (unsigned i = 0; i != NumValues; ++i) {
3972 SDLoc dl = getCurSDLoc();
3973 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3974 Values[i] =
3975 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3976 if (Negate)
3977 Values[i] = DAG.getNegative(Values[i], dl, VT);
3978 }
3979 } else {
3980 for (unsigned i = 0; i != NumValues; ++i) {
3981 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3982 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3983 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3984 Values[i] = DAG.getNode(
3985 OpCode, getCurSDLoc(),
3986 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3987 }
3988 }
3989
3991 DAG.getVTList(ValueVTs), Values));
3992}
3993
3994void SelectionDAGBuilder::visitTrunc(const User &I) {
3995 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3996 SDValue N = getValue(I.getOperand(0));
3997 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3998 I.getType());
3999 SDNodeFlags Flags;
4000 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
4001 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
4002 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
4003 }
4004
4005 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
4006}
4007
4008void SelectionDAGBuilder::visitZExt(const User &I) {
4009 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
4010 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
4011 SDValue N = getValue(I.getOperand(0));
4012 auto &TLI = DAG.getTargetLoweringInfo();
4013 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4014
4015 SDNodeFlags Flags;
4016 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
4017 Flags.setNonNeg(PNI->hasNonNeg());
4018
4019 // Eagerly use nonneg information to canonicalize towards sign_extend if
4020 // that is the target's preference.
4021 // TODO: Let the target do this later.
4022 if (Flags.hasNonNeg() &&
4023 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
4024 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
4025 return;
4026 }
4027
4028 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
4029}
4030
4031void SelectionDAGBuilder::visitSExt(const User &I) {
4032 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
4033 // SExt also can't be a cast to bool for same reason. So, nothing much to do
4034 SDValue N = getValue(I.getOperand(0));
4035 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4036 I.getType());
4037 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
4038}
4039
4040void SelectionDAGBuilder::visitFPTrunc(const User &I) {
4041 // FPTrunc is never a no-op cast, no need to check
4042 SDValue N = getValue(I.getOperand(0));
4043 SDLoc dl = getCurSDLoc();
4044 SDNodeFlags Flags;
4045 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4046 Flags.copyFMF(*FPOp);
4047 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4048 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4049 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
4050 DAG.getTargetConstant(
4051 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
4052 Flags));
4053}
4054
4055void SelectionDAGBuilder::visitFPExt(const User &I) {
4056 // FPExt is never a no-op cast, no need to check
4057 SDValue N = getValue(I.getOperand(0));
4058 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4059 I.getType());
4060 SDNodeFlags Flags;
4061 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4062 Flags.copyFMF(*FPOp);
4063 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N, Flags));
4064}
4065
4066void SelectionDAGBuilder::visitFPToUI(const User &I) {
4067 // FPToUI is never a no-op cast, no need to check
4068 SDValue N = getValue(I.getOperand(0));
4069 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4070 I.getType());
4071 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
4072}
4073
4074void SelectionDAGBuilder::visitFPToSI(const User &I) {
4075 // FPToSI is never a no-op cast, no need to check
4076 SDValue N = getValue(I.getOperand(0));
4077 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4078 I.getType());
4079 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
4080}
4081
4082void SelectionDAGBuilder::visitUIToFP(const User &I) {
4083 // UIToFP is never a no-op cast, no need to check
4084 SDValue N = getValue(I.getOperand(0));
4085 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4086 I.getType());
4087 SDNodeFlags Flags;
4088 Flags.setNonNeg(cast<PossiblyNonNegInst>(&I)->hasNonNeg());
4089 Flags.copyFMF(*cast<FPMathOperator>(&I));
4090
4091 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4092}
4093
4094void SelectionDAGBuilder::visitSIToFP(const User &I) {
4095 // SIToFP is never a no-op cast, no need to check
4096 SDValue N = getValue(I.getOperand(0));
4097 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4098 I.getType());
4099 SDNodeFlags Flags;
4100 Flags.copyFMF(*cast<FPMathOperator>(&I));
4101
4102 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4103}
4104
4105void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
4106 SDValue N = getValue(I.getOperand(0));
4107 // By definition the type of the ptrtoaddr must be equal to the address type.
4108 const auto &TLI = DAG.getTargetLoweringInfo();
4109 EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4110 // The address width must be smaller or equal to the pointer representation
4111 // width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
4112 N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
4113 setValue(&I, N);
4114}
4115
4116void SelectionDAGBuilder::visitPtrToInt(const User &I) {
4117 // What to do depends on the size of the integer and the size of the pointer.
4118 // We can either truncate, zero extend, or no-op, accordingly.
4119 SDValue N = getValue(I.getOperand(0));
4120 auto &TLI = DAG.getTargetLoweringInfo();
4121 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4122 I.getType());
4123 EVT PtrMemVT =
4124 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
4125 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4126 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
4127 setValue(&I, N);
4128}
4129
4130void SelectionDAGBuilder::visitIntToPtr(const User &I) {
4131 // What to do depends on the size of the integer and the size of the pointer.
4132 // We can either truncate, zero extend, or no-op, accordingly.
4133 SDValue N = getValue(I.getOperand(0));
4134 auto &TLI = DAG.getTargetLoweringInfo();
4135 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4136 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4137 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4138 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4139 setValue(&I, N);
4140}
4141
4142void SelectionDAGBuilder::visitBitCast(const User &I) {
4143 SDValue N = getValue(I.getOperand(0));
4144 SDLoc dl = getCurSDLoc();
4145 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4146 I.getType());
4147
4148 // BitCast assures us that source and destination are the same size so this is
4149 // either a BITCAST or a no-op.
4150 if (DestVT != N.getValueType())
4151 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
4152 DestVT, N)); // convert types.
4153 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4154 // might fold any kind of constant expression to an integer constant and that
4155 // is not what we are looking for. Only recognize a bitcast of a genuine
4156 // constant integer as an opaque constant.
4157 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4158 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4159 /*isOpaque*/true));
4160 else
4161 setValue(&I, N); // noop cast.
4162}
4163
4164void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4165 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4166 const Value *SV = I.getOperand(0);
4167 SDValue N = getValue(SV);
4168 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4169
4170 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4171 unsigned DestAS = I.getType()->getPointerAddressSpace();
4172
4173 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4174 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4175
4176 setValue(&I, N);
4177}
4178
4179void SelectionDAGBuilder::visitInsertElement(const User &I) {
4180 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4181 SDValue InVec = getValue(I.getOperand(0));
4182 SDValue InVal = getValue(I.getOperand(1));
4183 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4184 TLI.getVectorIdxTy(DAG.getDataLayout()));
4186 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4187 InVec, InVal, InIdx));
4188}
4189
4190void SelectionDAGBuilder::visitExtractElement(const User &I) {
4191 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4192 SDValue InVec = getValue(I.getOperand(0));
4193 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4194 TLI.getVectorIdxTy(DAG.getDataLayout()));
4196 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4197 InVec, InIdx));
4198}
4199
4200void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4201 SDValue Src1 = getValue(I.getOperand(0));
4202 SDValue Src2 = getValue(I.getOperand(1));
4203 ArrayRef<int> Mask;
4204 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4205 Mask = SVI->getShuffleMask();
4206 else
4207 Mask = cast<ConstantExpr>(I).getShuffleMask();
4208 SDLoc DL = getCurSDLoc();
4209 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4210 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4211 EVT SrcVT = Src1.getValueType();
4212
4213 if (all_of(Mask, equal_to(0)) && VT.isScalableVector()) {
4214 // Canonical splat form of first element of first input vector.
4215 SDValue FirstElt =
4216 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4217 DAG.getVectorIdxConstant(0, DL));
4218 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4219 return;
4220 }
4221
4222 // For now, we only handle splats for scalable vectors.
4223 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4224 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4225 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4226
4227 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4228 unsigned MaskNumElts = Mask.size();
4229
4230 if (SrcNumElts == MaskNumElts) {
4231 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4232 return;
4233 }
4234
4235 // Normalize the shuffle vector since mask and vector length don't match.
4236 if (SrcNumElts < MaskNumElts) {
4237 // Mask is longer than the source vectors. We can use concatenate vector to
4238 // make the mask and vectors lengths match.
4239
4240 if (MaskNumElts % SrcNumElts == 0) {
4241 // Mask length is a multiple of the source vector length.
4242 // Check if the shuffle is some kind of concatenation of the input
4243 // vectors.
4244 unsigned NumConcat = MaskNumElts / SrcNumElts;
4245 bool IsConcat = true;
4246 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4247 for (unsigned i = 0; i != MaskNumElts; ++i) {
4248 int Idx = Mask[i];
4249 if (Idx < 0)
4250 continue;
4251 // Ensure the indices in each SrcVT sized piece are sequential and that
4252 // the same source is used for the whole piece.
4253 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4254 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4255 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4256 IsConcat = false;
4257 break;
4258 }
4259 // Remember which source this index came from.
4260 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4261 }
4262
4263 // The shuffle is concatenating multiple vectors together. Just emit
4264 // a CONCAT_VECTORS operation.
4265 if (IsConcat) {
4266 SmallVector<SDValue, 8> ConcatOps;
4267 for (auto Src : ConcatSrcs) {
4268 if (Src < 0)
4269 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4270 else if (Src == 0)
4271 ConcatOps.push_back(Src1);
4272 else
4273 ConcatOps.push_back(Src2);
4274 }
4275 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4276 return;
4277 }
4278 }
4279
4280 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4281 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4282 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4283 PaddedMaskNumElts);
4284
4285 // Pad both vectors with undefs to make them the same length as the mask.
4286 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4287
4288 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4289 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4290 MOps1[0] = Src1;
4291 MOps2[0] = Src2;
4292
4293 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4294 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4295
4296 // Readjust mask for new input vector length.
4297 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4298 for (unsigned i = 0; i != MaskNumElts; ++i) {
4299 int Idx = Mask[i];
4300 if (Idx >= (int)SrcNumElts)
4301 Idx -= SrcNumElts - PaddedMaskNumElts;
4302 MappedOps[i] = Idx;
4303 }
4304
4305 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4306
4307 // If the concatenated vector was padded, extract a subvector with the
4308 // correct number of elements.
4309 if (MaskNumElts != PaddedMaskNumElts)
4310 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4311 DAG.getVectorIdxConstant(0, DL));
4312
4313 setValue(&I, Result);
4314 return;
4315 }
4316
4317 assert(SrcNumElts > MaskNumElts);
4318
4319 // Analyze the access pattern of the vector to see if we can extract
4320 // two subvectors and do the shuffle.
4321 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4322 bool CanExtract = true;
4323 for (int Idx : Mask) {
4324 unsigned Input = 0;
4325 if (Idx < 0)
4326 continue;
4327
4328 if (Idx >= (int)SrcNumElts) {
4329 Input = 1;
4330 Idx -= SrcNumElts;
4331 }
4332
4333 // If all the indices come from the same MaskNumElts sized portion of
4334 // the sources we can use extract. Also make sure the extract wouldn't
4335 // extract past the end of the source.
4336 int NewStartIdx = alignDown(Idx, MaskNumElts);
4337 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4338 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4339 CanExtract = false;
4340 // Make sure we always update StartIdx as we use it to track if all
4341 // elements are undef.
4342 StartIdx[Input] = NewStartIdx;
4343 }
4344
4345 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4346 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4347 return;
4348 }
4349 if (CanExtract) {
4350 // Extract appropriate subvector and generate a vector shuffle
4351 for (unsigned Input = 0; Input < 2; ++Input) {
4352 SDValue &Src = Input == 0 ? Src1 : Src2;
4353 if (StartIdx[Input] < 0)
4354 Src = DAG.getUNDEF(VT);
4355 else {
4356 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4357 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4358 }
4359 }
4360
4361 // Calculate new mask.
4362 SmallVector<int, 8> MappedOps(Mask);
4363 for (int &Idx : MappedOps) {
4364 if (Idx >= (int)SrcNumElts)
4365 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4366 else if (Idx >= 0)
4367 Idx -= StartIdx[0];
4368 }
4369
4370 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4371 return;
4372 }
4373
4374 // We can't use either concat vectors or extract subvectors so fall back to
4375 // replacing the shuffle with extract and build vector.
4376 // to insert and build vector.
4377 EVT EltVT = VT.getVectorElementType();
4379 for (int Idx : Mask) {
4380 SDValue Res;
4381
4382 if (Idx < 0) {
4383 Res = DAG.getUNDEF(EltVT);
4384 } else {
4385 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4386 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4387
4388 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4389 DAG.getVectorIdxConstant(Idx, DL));
4390 }
4391
4392 Ops.push_back(Res);
4393 }
4394
4395 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4396}
4397
4398void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4399 ArrayRef<unsigned> Indices = I.getIndices();
4400 const Value *Op0 = I.getOperand(0);
4401 const Value *Op1 = I.getOperand(1);
4402 Type *AggTy = I.getType();
4403 Type *ValTy = Op1->getType();
4404 bool IntoUndef = isa<UndefValue>(Op0);
4405 bool FromUndef = isa<UndefValue>(Op1);
4406
4407 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4408
4409 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4410 SmallVector<EVT, 4> AggValueVTs;
4411 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4412 SmallVector<EVT, 4> ValValueVTs;
4413 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4414
4415 unsigned NumAggValues = AggValueVTs.size();
4416 unsigned NumValValues = ValValueVTs.size();
4417 SmallVector<SDValue, 4> Values(NumAggValues);
4418
4419 // Ignore an insertvalue that produces an empty object
4420 if (!NumAggValues) {
4421 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4422 return;
4423 }
4424
4425 SDValue Agg = getValue(Op0);
4426 unsigned i = 0;
4427 // Copy the beginning value(s) from the original aggregate.
4428 for (; i != LinearIndex; ++i)
4429 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4430 SDValue(Agg.getNode(), Agg.getResNo() + i);
4431 // Copy values from the inserted value(s).
4432 if (NumValValues) {
4433 SDValue Val = getValue(Op1);
4434 for (; i != LinearIndex + NumValValues; ++i)
4435 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4436 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4437 }
4438 // Copy remaining value(s) from the original aggregate.
4439 for (; i != NumAggValues; ++i)
4440 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4441 SDValue(Agg.getNode(), Agg.getResNo() + i);
4442
4444 DAG.getVTList(AggValueVTs), Values));
4445}
4446
4447void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4448 ArrayRef<unsigned> Indices = I.getIndices();
4449 const Value *Op0 = I.getOperand(0);
4450 Type *AggTy = Op0->getType();
4451 Type *ValTy = I.getType();
4452 bool OutOfUndef = isa<UndefValue>(Op0);
4453
4454 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4455
4456 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4457 SmallVector<EVT, 4> ValValueVTs;
4458 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4459
4460 unsigned NumValValues = ValValueVTs.size();
4461
4462 // Ignore a extractvalue that produces an empty object
4463 if (!NumValValues) {
4464 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4465 return;
4466 }
4467
4468 SmallVector<SDValue, 4> Values(NumValValues);
4469
4470 SDValue Agg = getValue(Op0);
4471 // Copy out the selected value(s).
4472 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4473 Values[i - LinearIndex] =
4474 OutOfUndef ?
4475 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4476 SDValue(Agg.getNode(), Agg.getResNo() + i);
4477
4479 DAG.getVTList(ValValueVTs), Values));
4480}
4481
4482void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4483 Value *Op0 = I.getOperand(0);
4484 // Note that the pointer operand may be a vector of pointers. Take the scalar
4485 // element which holds a pointer.
4486 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4487 SDValue N = getValue(Op0);
4488 SDLoc dl = getCurSDLoc();
4489 auto &TLI = DAG.getTargetLoweringInfo();
4490 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4491
4492 // For a vector GEP, keep the prefix scalar as long as possible, then
4493 // convert any scalars encountered after the first vector operand to vectors.
4494 bool IsVectorGEP = I.getType()->isVectorTy();
4495 ElementCount VectorElementCount =
4496 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4498
4500 GTI != E; ++GTI) {
4501 const Value *Idx = GTI.getOperand();
4502 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4503 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4504 if (Field) {
4505 // N = N + Offset
4506 uint64_t Offset =
4507 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4508
4509 // In an inbounds GEP with an offset that is nonnegative even when
4510 // interpreted as signed, assume there is no unsigned overflow.
4511 SDNodeFlags Flags;
4512 if (NW.hasNoUnsignedWrap() ||
4513 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4515 Flags.setInBounds(NW.isInBounds());
4516
4517 N = DAG.getMemBasePlusOffset(
4518 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4519 }
4520 } else {
4521 // IdxSize is the width of the arithmetic according to IR semantics.
4522 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4523 // (and fix up the result later).
4524 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4525 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4526 TypeSize ElementSize =
4527 GTI.getSequentialElementStride(DAG.getDataLayout());
4528 // We intentionally mask away the high bits here; ElementSize may not
4529 // fit in IdxTy.
4530 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4531 /*isSigned=*/false, /*implicitTrunc=*/true);
4532 bool ElementScalable = ElementSize.isScalable();
4533
4534 // If this is a scalar constant or a splat vector of constants,
4535 // handle it quickly.
4536 const auto *C = dyn_cast<Constant>(Idx);
4537 if (C && isa<VectorType>(C->getType()))
4538 C = C->getSplatValue();
4539
4540 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4541 if (CI && CI->isZero())
4542 continue;
4543 if (CI && !ElementScalable) {
4544 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4545 LLVMContext &Context = *DAG.getContext();
4546 SDValue OffsVal;
4547 if (N.getValueType().isVector())
4548 OffsVal = DAG.getConstant(
4549 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4550 else
4551 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4552
4553 // In an inbounds GEP with an offset that is nonnegative even when
4554 // interpreted as signed, assume there is no unsigned overflow.
4555 SDNodeFlags Flags;
4556 if (NW.hasNoUnsignedWrap() ||
4557 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4558 Flags.setNoUnsignedWrap(true);
4559 Flags.setInBounds(NW.isInBounds());
4560
4561 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4562
4563 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4564 continue;
4565 }
4566
4567 // N = N + Idx * ElementMul;
4568 SDValue IdxN = getValue(Idx);
4569
4570 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4571 if (N.getValueType().isVector()) {
4572 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4573 VectorElementCount);
4574 IdxN = DAG.getSplat(VT, dl, IdxN);
4575 } else {
4576 EVT VT =
4577 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4578 N = DAG.getSplat(VT, dl, N);
4579 }
4580 }
4581
4582 // If the index is smaller or larger than intptr_t, truncate or extend
4583 // it.
4584 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4585
4586 SDNodeFlags ScaleFlags;
4587 // The multiplication of an index by the type size does not wrap the
4588 // pointer index type in a signed sense (mul nsw).
4590
4591 // The multiplication of an index by the type size does not wrap the
4592 // pointer index type in an unsigned sense (mul nuw).
4593 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4594
4595 if (ElementScalable) {
4596 EVT VScaleTy = N.getValueType().getScalarType();
4597 SDValue VScale = DAG.getNode(
4598 ISD::VSCALE, dl, VScaleTy,
4599 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4600 if (N.getValueType().isVector())
4601 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4602 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4603 ScaleFlags);
4604 } else {
4605 // If this is a multiply by a power of two, turn it into a shl
4606 // immediately. This is a very common case.
4607 if (ElementMul != 1) {
4608 if (ElementMul.isPowerOf2()) {
4609 unsigned Amt = ElementMul.logBase2();
4610 IdxN = DAG.getNode(
4611 ISD::SHL, dl, N.getValueType(), IdxN,
4612 DAG.getShiftAmountConstant(Amt, N.getValueType(), dl),
4613 ScaleFlags);
4614 } else {
4615 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4616 IdxN.getValueType());
4617 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4618 ScaleFlags);
4619 }
4620 }
4621 }
4622
4623 // The successive addition of the current address, truncated to the
4624 // pointer index type and interpreted as an unsigned number, and each
4625 // offset, also interpreted as an unsigned number, does not wrap the
4626 // pointer index type (add nuw).
4627 SDNodeFlags AddFlags;
4628 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4629 AddFlags.setInBounds(NW.isInBounds());
4630
4631 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4632 }
4633 }
4634
4635 if (IsVectorGEP && !N.getValueType().isVector()) {
4636 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4637 N = DAG.getSplat(VT, dl, N);
4638 }
4639
4640 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4641 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4642 if (IsVectorGEP) {
4643 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4644 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4645 }
4646
4647 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4648 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4649
4650 setValue(&I, N);
4651}
4652
4653void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4654 // If this is a fixed sized alloca in the entry block of the function,
4655 // allocate it statically on the stack.
4656 if (FuncInfo.StaticAllocaMap.count(&I))
4657 return; // getValue will auto-populate this.
4658
4659 SDLoc dl = getCurSDLoc();
4660 Type *Ty = I.getAllocatedType();
4661 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4662 auto &DL = DAG.getDataLayout();
4663 TypeSize TySize = DL.getTypeAllocSize(Ty);
4664 MaybeAlign Alignment = I.getAlign();
4665
4666 SDValue AllocSize = getValue(I.getArraySize());
4667
4668 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4669 if (AllocSize.getValueType() != IntPtr)
4670 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4671
4672 AllocSize = DAG.getNode(
4673 ISD::MUL, dl, IntPtr, AllocSize,
4674 DAG.getZExtOrTrunc(DAG.getTypeSize(dl, MVT::i64, TySize), dl, IntPtr));
4675
4676 // Handle alignment. If the requested alignment is less than or equal to
4677 // the stack alignment, ignore it. If the size is greater than or equal to
4678 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4679 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4680 if (*Alignment <= StackAlign)
4681 Alignment = std::nullopt;
4682
4683 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4684 // Round the size of the allocation up to the stack alignment size
4685 // by add SA-1 to the size. This doesn't overflow because we're computing
4686 // an address inside an alloca.
4687 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4688 DAG.getConstant(StackAlignMask, dl, IntPtr),
4690
4691 // Mask out the low bits for alignment purposes.
4692 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4693 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4694
4695 SDValue Ops[] = {
4696 getRoot(), AllocSize,
4697 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4698 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4699 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4700 setValue(&I, DSA);
4701 DAG.setRoot(DSA.getValue(1));
4702
4703 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4704}
4705
4706static const MDNode *getRangeMetadata(const Instruction &I) {
4707 return I.getMetadata(LLVMContext::MD_range);
4708}
4709
4710static std::optional<ConstantRange> getRange(const Instruction &I) {
4711 if (const auto *CB = dyn_cast<CallBase>(&I))
4712 if (std::optional<ConstantRange> CR = CB->getRange())
4713 return CR;
4714 if (const MDNode *Range = getRangeMetadata(I))
4716 return std::nullopt;
4717}
4718
4720 if (const auto *CB = dyn_cast<CallBase>(&I))
4721 return CB->getRetNoFPClass();
4722 return fcNone;
4723}
4724
4725void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4726 if (I.isAtomic())
4727 return visitAtomicLoad(I);
4728
4729 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4730 const Value *SV = I.getOperand(0);
4731 if (TLI.supportSwiftError()) {
4732 // Swifterror values can come from either a function parameter with
4733 // swifterror attribute or an alloca with swifterror attribute.
4734 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4735 if (Arg->hasSwiftErrorAttr())
4736 return visitLoadFromSwiftError(I);
4737 }
4738
4739 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4740 if (Alloca->isSwiftError())
4741 return visitLoadFromSwiftError(I);
4742 }
4743 }
4744
4745 SDValue Ptr = getValue(SV);
4746
4747 Type *Ty = I.getType();
4748 SmallVector<EVT, 4> ValueVTs, MemVTs;
4750 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4751 unsigned NumValues = ValueVTs.size();
4752 if (NumValues == 0)
4753 return;
4754
4755 Align Alignment = I.getAlign();
4756 AAMDNodes AAInfo = I.getAAMetadata();
4757 const MDNode *Ranges = getRangeMetadata(I);
4758 bool isVolatile = I.isVolatile();
4759 MachineMemOperand::Flags MMOFlags =
4760 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4761
4762 SDValue Root;
4763 bool ConstantMemory = false;
4764 if (isVolatile)
4765 // Serialize volatile loads with other side effects.
4766 Root = getRoot();
4767 else if (NumValues > MaxParallelChains)
4768 Root = getMemoryRoot();
4769 else if (BatchAA &&
4770 BatchAA->pointsToConstantMemory(MemoryLocation(
4771 SV,
4772 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4773 AAInfo))) {
4774 // Do not serialize (non-volatile) loads of constant memory with anything.
4775 Root = DAG.getEntryNode();
4776 ConstantMemory = true;
4778 } else {
4779 // Do not serialize non-volatile loads against each other.
4780 Root = DAG.getRoot();
4781 }
4782
4783 SDLoc dl = getCurSDLoc();
4784
4785 if (isVolatile)
4786 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4787
4789 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4790
4791 unsigned ChainI = 0;
4792 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4793 // Serializing loads here may result in excessive register pressure, and
4794 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4795 // could recover a bit by hoisting nodes upward in the chain by recognizing
4796 // they are side-effect free or do not alias. The optimizer should really
4797 // avoid this case by converting large object/array copies to llvm.memcpy
4798 // (MaxParallelChains should always remain as failsafe).
4799 if (ChainI == MaxParallelChains) {
4800 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4801 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4802 ArrayRef(Chains.data(), ChainI));
4803 Root = Chain;
4804 ChainI = 0;
4805 }
4806
4807 // TODO: MachinePointerInfo only supports a fixed length offset.
4808 MachinePointerInfo PtrInfo =
4809 !Offsets[i].isScalable() || Offsets[i].isZero()
4810 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4811 : MachinePointerInfo();
4812
4813 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4814 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4815 MMOFlags, AAInfo, Ranges);
4816 Chains[ChainI] = L.getValue(1);
4817
4818 if (MemVTs[i] != ValueVTs[i])
4819 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4820
4821 if (MDNode *NoFPClassMD = I.getMetadata(LLVMContext::MD_nofpclass)) {
4822 uint64_t FPTestInt =
4823 cast<ConstantInt>(
4824 cast<ConstantAsMetadata>(NoFPClassMD->getOperand(0))->getValue())
4825 ->getZExtValue();
4826 if (FPTestInt != fcNone) {
4827 SDValue FPTestConst =
4828 DAG.getTargetConstant(FPTestInt, SDLoc(), MVT::i32);
4829 L = DAG.getNode(ISD::AssertNoFPClass, dl, L.getValueType(), L,
4830 FPTestConst);
4831 }
4832 }
4833 Values[i] = L;
4834 }
4835
4836 if (!ConstantMemory) {
4837 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4838 ArrayRef(Chains.data(), ChainI));
4839 if (isVolatile)
4840 DAG.setRoot(Chain);
4841 else
4842 PendingLoads.push_back(Chain);
4843 }
4844
4845 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4846 DAG.getVTList(ValueVTs), Values));
4847}
4848
4849void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4850 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4851 "call visitStoreToSwiftError when backend supports swifterror");
4852
4853 SmallVector<EVT, 4> ValueVTs;
4854 SmallVector<uint64_t, 4> Offsets;
4855 const Value *SrcV = I.getOperand(0);
4856 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4857 SrcV->getType(), ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4858 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4859 "expect a single EVT for swifterror");
4860
4861 SDValue Src = getValue(SrcV);
4862 // Create a virtual register, then update the virtual register.
4863 Register VReg =
4864 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4865 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4866 // Chain can be getRoot or getControlRoot.
4867 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4868 SDValue(Src.getNode(), Src.getResNo()));
4869 DAG.setRoot(CopyNode);
4870}
4871
4872void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4873 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4874 "call visitLoadFromSwiftError when backend supports swifterror");
4875
4876 assert(!I.isVolatile() &&
4877 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4878 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4879 "Support volatile, non temporal, invariant for load_from_swift_error");
4880
4881 const Value *SV = I.getOperand(0);
4882 Type *Ty = I.getType();
4883 assert(
4884 (!BatchAA ||
4885 !BatchAA->pointsToConstantMemory(MemoryLocation(
4886 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4887 I.getAAMetadata()))) &&
4888 "load_from_swift_error should not be constant memory");
4889
4890 SmallVector<EVT, 4> ValueVTs;
4891 SmallVector<uint64_t, 4> Offsets;
4892 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4893 ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4894 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4895 "expect a single EVT for swifterror");
4896
4897 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4898 SDValue L = DAG.getCopyFromReg(
4899 getRoot(), getCurSDLoc(),
4900 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4901
4902 setValue(&I, L);
4903}
4904
4905void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4906 if (I.isAtomic())
4907 return visitAtomicStore(I);
4908
4909 const Value *SrcV = I.getOperand(0);
4910 const Value *PtrV = I.getOperand(1);
4911
4912 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4913 if (TLI.supportSwiftError()) {
4914 // Swifterror values can come from either a function parameter with
4915 // swifterror attribute or an alloca with swifterror attribute.
4916 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4917 if (Arg->hasSwiftErrorAttr())
4918 return visitStoreToSwiftError(I);
4919 }
4920
4921 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4922 if (Alloca->isSwiftError())
4923 return visitStoreToSwiftError(I);
4924 }
4925 }
4926
4927 SmallVector<EVT, 4> ValueVTs, MemVTs;
4929 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4930 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4931 unsigned NumValues = ValueVTs.size();
4932 if (NumValues == 0)
4933 return;
4934
4935 // Get the lowered operands. Note that we do this after
4936 // checking if NumResults is zero, because with zero results
4937 // the operands won't have values in the map.
4938 SDValue Src = getValue(SrcV);
4939 SDValue Ptr = getValue(PtrV);
4940
4941 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4942 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4943 SDLoc dl = getCurSDLoc();
4944 Align Alignment = I.getAlign();
4945 AAMDNodes AAInfo = I.getAAMetadata();
4946
4947 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4948
4949 unsigned ChainI = 0;
4950 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4951 // See visitLoad comments.
4952 if (ChainI == MaxParallelChains) {
4953 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4954 ArrayRef(Chains.data(), ChainI));
4955 Root = Chain;
4956 ChainI = 0;
4957 }
4958
4959 // TODO: MachinePointerInfo only supports a fixed length offset.
4960 MachinePointerInfo PtrInfo =
4961 !Offsets[i].isScalable() || Offsets[i].isZero()
4962 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4963 : MachinePointerInfo();
4964
4965 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4966 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4967 if (MemVTs[i] != ValueVTs[i])
4968 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4969 SDValue St =
4970 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4971 Chains[ChainI] = St;
4972 }
4973
4974 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4975 ArrayRef(Chains.data(), ChainI));
4976 setValue(&I, StoreNode);
4977 DAG.setRoot(StoreNode);
4978}
4979
4980void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4981 bool IsCompressing) {
4982 SDLoc sdl = getCurSDLoc();
4983
4984 Value *Src0Operand = I.getArgOperand(0);
4985 Value *PtrOperand = I.getArgOperand(1);
4986 Value *MaskOperand = I.getArgOperand(2);
4987 Align Alignment = I.getParamAlign(1).valueOrOne();
4988
4989 SDValue Ptr = getValue(PtrOperand);
4990 SDValue Src0 = getValue(Src0Operand);
4991 SDValue Mask = getValue(MaskOperand);
4992 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4993
4994 EVT VT = Src0.getValueType();
4995
4996 auto MMOFlags = MachineMemOperand::MOStore;
4997 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4999
5000 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5001 MachinePointerInfo(PtrOperand), MMOFlags,
5002 LocationSize::upperBound(VT.getStoreSize()), Alignment,
5003 I.getAAMetadata());
5004
5005 const auto &TLI = DAG.getTargetLoweringInfo();
5006
5007 SDValue StoreNode =
5008 !IsCompressing && TTI->hasConditionalLoadStoreForType(
5009 I.getArgOperand(0)->getType(), /*IsStore=*/true)
5010 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
5011 Mask)
5012 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
5013 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
5014 IsCompressing);
5015 DAG.setRoot(StoreNode);
5016 setValue(&I, StoreNode);
5017}
5018
5019// Get a uniform base for the Gather/Scatter intrinsic.
5020// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
5021// We try to represent it as a base pointer + vector of indices.
5022// Usually, the vector of pointers comes from a 'getelementptr' instruction.
5023// The first operand of the GEP may be a single pointer or a vector of pointers
5024// Example:
5025// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
5026// or
5027// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
5028// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
5029//
5030// When the first GEP operand is a single pointer - it is the uniform base we
5031// are looking for. If first operand of the GEP is a splat vector - we
5032// extract the splat value and use it as a uniform base.
5033// In all other cases the function returns 'false'.
5034static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
5035 SDValue &Scale, SelectionDAGBuilder *SDB,
5036 const BasicBlock *CurBB, uint64_t ElemSize) {
5037 SelectionDAG& DAG = SDB->DAG;
5038 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5039 const DataLayout &DL = DAG.getDataLayout();
5040
5041 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
5042
5043 // Handle splat constant pointer.
5044 if (auto *C = dyn_cast<Constant>(Ptr)) {
5045 C = C->getSplatValue();
5046 if (!C)
5047 return false;
5048
5049 Base = SDB->getValue(C);
5050
5051 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
5052 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
5053 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
5054 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
5055 return true;
5056 }
5057
5059 if (!GEP || GEP->getParent() != CurBB)
5060 return false;
5061
5062 if (GEP->getNumOperands() != 2)
5063 return false;
5064
5065 const Value *BasePtr = GEP->getPointerOperand();
5066 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
5067
5068 // Make sure the base is scalar and the index is a vector.
5069 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
5070 return false;
5071
5072 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
5073 if (ScaleVal.isScalable())
5074 return false;
5075
5076 // Target may not support the required addressing mode.
5077 if (ScaleVal != 1 &&
5078 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
5079 return false;
5080
5081 Base = SDB->getValue(BasePtr);
5082 Index = SDB->getValue(IndexVal);
5083
5084 Scale =
5085 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
5086 return true;
5087}
5088
5089void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
5090 SDLoc sdl = getCurSDLoc();
5091
5092 // llvm.masked.scatter.*(Src0, Ptrs, Mask)
5093 const Value *Ptr = I.getArgOperand(1);
5094 SDValue Src0 = getValue(I.getArgOperand(0));
5095 SDValue Mask = getValue(I.getArgOperand(2));
5096 EVT VT = Src0.getValueType();
5097 Align Alignment = I.getParamAlign(1).valueOrOne();
5098 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5099
5100 SDValue Base;
5101 SDValue Index;
5102 SDValue Scale;
5103 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5104 I.getParent(), VT.getScalarStoreSize());
5105
5106 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5107 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5108 MachinePointerInfo(AS), MachineMemOperand::MOStore,
5109 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
5110 if (!UniformBase) {
5111 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5112 Index = getValue(Ptr);
5113 Scale =
5114 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5115 }
5116
5117 EVT IdxVT = Index.getValueType();
5118 EVT EltTy = IdxVT.getVectorElementType();
5119 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5120 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5121 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5122 }
5123
5124 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
5125 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
5126 Ops, MMO, ISD::SIGNED_SCALED, false);
5127 DAG.setRoot(Scatter);
5128 setValue(&I, Scatter);
5129}
5130
5131void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5132 SDLoc sdl = getCurSDLoc();
5133
5134 Value *PtrOperand = I.getArgOperand(0);
5135 Value *MaskOperand = I.getArgOperand(1);
5136 Value *Src0Operand = I.getArgOperand(2);
5137 Align Alignment = I.getParamAlign(0).valueOrOne();
5138
5139 SDValue Ptr = getValue(PtrOperand);
5140 SDValue Src0 = getValue(Src0Operand);
5141 SDValue Mask = getValue(MaskOperand);
5142 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5143
5144 EVT VT = Src0.getValueType();
5145 AAMDNodes AAInfo = I.getAAMetadata();
5146 const MDNode *Ranges = getRangeMetadata(I);
5147
5148 // Do not serialize masked loads of constant memory with anything.
5149 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5150 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5151
5152 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5153
5154 auto MMOFlags = MachineMemOperand::MOLoad;
5155 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5157 if (I.hasMetadata(LLVMContext::MD_invariant_load))
5159
5160 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5161 MachinePointerInfo(PtrOperand), MMOFlags,
5162 LocationSize::upperBound(VT.getStoreSize()), Alignment, AAInfo, Ranges);
5163
5164 const auto &TLI = DAG.getTargetLoweringInfo();
5165
5166 // The Load/Res may point to different values and both of them are output
5167 // variables.
5168 SDValue Load;
5169 SDValue Res;
5170 if (!IsExpanding &&
5171 TTI->hasConditionalLoadStoreForType(Src0Operand->getType(),
5172 /*IsStore=*/false))
5173 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5174 else
5175 Res = Load =
5176 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5177 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5178 if (AddToChain)
5179 PendingLoads.push_back(Load.getValue(1));
5180 setValue(&I, Res);
5181}
5182
5183void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5184 SDLoc sdl = getCurSDLoc();
5185
5186 // @llvm.masked.gather.*(Ptrs, Mask, Src0)
5187 const Value *Ptr = I.getArgOperand(0);
5188 SDValue Src0 = getValue(I.getArgOperand(2));
5189 SDValue Mask = getValue(I.getArgOperand(1));
5190
5191 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5192 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5193 Align Alignment = I.getParamAlign(0).valueOrOne();
5194
5195 const MDNode *Ranges = getRangeMetadata(I);
5196
5197 SDValue Root = DAG.getRoot();
5198 SDValue Base;
5199 SDValue Index;
5200 SDValue Scale;
5201 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5202 I.getParent(), VT.getScalarStoreSize());
5203 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5204 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5205 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5206 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5207 Ranges);
5208
5209 if (!UniformBase) {
5210 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5211 Index = getValue(Ptr);
5212 Scale =
5213 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5214 }
5215
5216 EVT IdxVT = Index.getValueType();
5217 EVT EltTy = IdxVT.getVectorElementType();
5218 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5219 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5220 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5221 }
5222
5223 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5224 SDValue Gather =
5225 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5227
5228 PendingLoads.push_back(Gather.getValue(1));
5229 setValue(&I, Gather);
5230}
5231
5232void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5233 SDLoc dl = getCurSDLoc();
5234 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5235 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5236 SyncScope::ID SSID = I.getSyncScopeID();
5237
5238 SDValue InChain = getRoot();
5239
5240 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5241 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5242
5243 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5244 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5245
5246 MachineFunction &MF = DAG.getMachineFunction();
5247 MachineMemOperand *MMO =
5248 MF.getMachineMemOperand(MachinePointerInfo(I.getPointerOperand()), Flags,
5249 MemVT.getStoreSize(), I.getAlign(), AAMDNodes(),
5250 nullptr, SSID, SuccessOrdering, FailureOrdering);
5251
5253 dl, MemVT, VTs, InChain,
5254 getValue(I.getPointerOperand()),
5255 getValue(I.getCompareOperand()),
5256 getValue(I.getNewValOperand()), MMO);
5257
5258 SDValue OutChain = L.getValue(2);
5259
5260 setValue(&I, L);
5261 DAG.setRoot(OutChain);
5262}
5263
5264void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5265 SDLoc dl = getCurSDLoc();
5267 switch (I.getOperation()) {
5268 default: llvm_unreachable("Unknown atomicrmw operation");
5286 break;
5289 break;
5292 break;
5295 break;
5298 break;
5301 break;
5304 break;
5307 break;
5308 }
5309 AtomicOrdering Ordering = I.getOrdering();
5310 SyncScope::ID SSID = I.getSyncScopeID();
5311
5312 SDValue InChain = getRoot();
5313
5314 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5315 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5316 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5317
5318 MachineFunction &MF = DAG.getMachineFunction();
5319 MachineMemOperand *MMO = MF.getMachineMemOperand(
5320 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5321 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5322
5323 SDValue L =
5324 DAG.getAtomic(NT, dl, MemVT, InChain,
5325 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5326 MMO);
5327
5328 SDValue OutChain = L.getValue(1);
5329
5330 setValue(&I, L);
5331 DAG.setRoot(OutChain);
5332}
5333
5334void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5335 SDLoc dl = getCurSDLoc();
5336 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5337 SDValue Ops[3];
5338 Ops[0] = getRoot();
5339 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5340 TLI.getFenceOperandTy(DAG.getDataLayout()));
5341 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5342 TLI.getFenceOperandTy(DAG.getDataLayout()));
5343 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5344 setValue(&I, N);
5345 DAG.setRoot(N);
5346}
5347
5348void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5349 SDLoc dl = getCurSDLoc();
5350 AtomicOrdering Order = I.getOrdering();
5351 SyncScope::ID SSID = I.getSyncScopeID();
5352
5353 SDValue InChain = getRoot();
5354
5355 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5356 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5357 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5358
5359 if (!TLI.supportsUnalignedAtomics() &&
5360 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5361 report_fatal_error("Cannot generate unaligned atomic load");
5362
5363 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5364
5365 const MDNode *Ranges = getRangeMetadata(I);
5366 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5367 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5368 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5369
5370 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5371
5372 SDValue Ptr = getValue(I.getPointerOperand());
5373 SDValue L =
5374 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5375
5376 SDValue OutChain = L.getValue(1);
5377 if (MemVT != VT)
5378 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5379
5380 setValue(&I, L);
5381 DAG.setRoot(OutChain);
5382}
5383
5384void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5385 SDLoc dl = getCurSDLoc();
5386
5387 AtomicOrdering Ordering = I.getOrdering();
5388 SyncScope::ID SSID = I.getSyncScopeID();
5389
5390 SDValue InChain = getRoot();
5391
5392 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5393 EVT MemVT =
5394 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5395
5396 if (!TLI.supportsUnalignedAtomics() &&
5397 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5398 report_fatal_error("Cannot generate unaligned atomic store");
5399
5400 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5401
5402 MachineFunction &MF = DAG.getMachineFunction();
5403 MachineMemOperand *MMO = MF.getMachineMemOperand(
5404 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5405 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5406
5407 SDValue Val = getValue(I.getValueOperand());
5408 if (Val.getValueType() != MemVT)
5409 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5410 SDValue Ptr = getValue(I.getPointerOperand());
5411
5412 SDValue OutChain =
5413 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5414
5415 setValue(&I, OutChain);
5416 DAG.setRoot(OutChain);
5417}
5418
5419/// Check if this intrinsic call depends on the chain (1st return value)
5420/// and if it only *loads* memory.
5421/// Ignore the callsite's attributes. A specific call site may be marked with
5422/// readnone, but the lowering code will expect the chain based on the
5423/// definition.
5424std::pair<bool, bool>
5425SelectionDAGBuilder::getTargetIntrinsicCallProperties(const CallBase &I) {
5426 const Function *F = I.getCalledFunction();
5427 bool HasChain = !F->doesNotAccessMemory();
5428 bool OnlyLoad =
5429 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5430
5431 return {HasChain, OnlyLoad};
5432}
5433
5434SmallVector<SDValue, 8> SelectionDAGBuilder::getTargetIntrinsicOperands(
5435 const CallBase &I, bool HasChain, bool OnlyLoad,
5436 TargetLowering::IntrinsicInfo *TgtMemIntrinsicInfo) {
5437 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5438
5439 // Build the operand list.
5441 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5442 if (OnlyLoad) {
5443 // We don't need to serialize loads against other loads.
5444 Ops.push_back(DAG.getRoot());
5445 } else {
5446 Ops.push_back(getRoot());
5447 }
5448 }
5449
5450 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5451 if (!TgtMemIntrinsicInfo || TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_VOID ||
5452 TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_W_CHAIN)
5453 Ops.push_back(DAG.getTargetConstant(I.getIntrinsicID(), getCurSDLoc(),
5454 TLI.getPointerTy(DAG.getDataLayout())));
5455
5456 // Add all operands of the call to the operand list.
5457 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5458 const Value *Arg = I.getArgOperand(i);
5459 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5460 Ops.push_back(getValue(Arg));
5461 continue;
5462 }
5463
5464 // Use TargetConstant instead of a regular constant for immarg.
5465 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5466 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5467 assert(CI->getBitWidth() <= 64 &&
5468 "large intrinsic immediates not handled");
5469 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5470 } else {
5471 Ops.push_back(
5472 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5473 }
5474 }
5475
5476 if (std::optional<OperandBundleUse> Bundle =
5477 I.getOperandBundle(LLVMContext::OB_deactivation_symbol)) {
5478 auto *Sym = Bundle->Inputs[0].get();
5479 SDValue SDSym = getValue(Sym);
5480 SDSym = DAG.getDeactivationSymbol(cast<GlobalValue>(Sym));
5481 Ops.push_back(SDSym);
5482 }
5483
5484 if (std::optional<OperandBundleUse> Bundle =
5485 I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5486 Value *Token = Bundle->Inputs[0].get();
5487 SDValue ConvControlToken = getValue(Token);
5488 assert(Ops.back().getValueType() != MVT::Glue &&
5489 "Did not expect another glue node here.");
5490 ConvControlToken =
5491 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5492 Ops.push_back(ConvControlToken);
5493 }
5494
5495 return Ops;
5496}
5497
5498SDVTList SelectionDAGBuilder::getTargetIntrinsicVTList(const CallBase &I,
5499 bool HasChain) {
5500 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5501
5502 SmallVector<EVT, 4> ValueVTs;
5503 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5504
5505 if (HasChain)
5506 ValueVTs.push_back(MVT::Other);
5507
5508 return DAG.getVTList(ValueVTs);
5509}
5510
5511/// Get an INTRINSIC node for a target intrinsic which does not touch memory.
5512SDValue SelectionDAGBuilder::getTargetNonMemIntrinsicNode(
5513 const Type &IntrinsicVT, bool HasChain, ArrayRef<SDValue> Ops,
5514 const SDVTList &VTs) {
5515 if (!HasChain)
5516 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5517 if (!IntrinsicVT.isVoidTy())
5518 return DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5519 return DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5520}
5521
5522/// Set root, convert return type if necessary and check alignment.
5523SDValue SelectionDAGBuilder::handleTargetIntrinsicRet(const CallBase &I,
5524 bool HasChain,
5525 bool OnlyLoad,
5526 SDValue Result) {
5527 if (HasChain) {
5528 SDValue Chain = Result.getValue(Result.getNode()->getNumValues() - 1);
5529 if (OnlyLoad)
5530 PendingLoads.push_back(Chain);
5531 else
5532 DAG.setRoot(Chain);
5533 }
5534
5535 if (I.getType()->isVoidTy())
5536 return Result;
5537
5538 if (MaybeAlign Alignment = I.getRetAlign(); InsertAssertAlign && Alignment) {
5539 // Insert `assertalign` node if there's an alignment.
5540 Result = DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5541 } else if (!isa<VectorType>(I.getType())) {
5542 Result = lowerRangeToAssertZExt(DAG, I, Result);
5543 }
5544
5545 return Result;
5546}
5547
5548/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5549/// node.
5550void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5551 unsigned Intrinsic) {
5552 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
5553 Intrinsic::ID IntrinsicID = static_cast<Intrinsic::ID>(Intrinsic);
5554
5555 if (!DAG.getMachineFunction().getSubtarget().isIntrinsicSupported(
5556 Intrinsic)) {
5557 SDLoc DL = getCurSDLoc();
5558 DAG.getContext()->diagnose(DiagnosticInfoUnsupportedTargetIntrinsic(
5559 *I.getFunction(), IntrinsicID, DL.getDebugLoc()));
5560
5561 // The intrinsic is not available on this subtarget. Preserve the chain for
5562 // side-effecting intrinsics and lower any result to poison so that
5563 // compilation can continue and collect further diagnostics.
5564 if (HasChain && !OnlyLoad)
5565 DAG.setRoot(getRoot());
5566
5567 if (!I.getType()->isVoidTy()) {
5568 SmallVector<EVT, 4> ValueVTs;
5569 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
5570 I.getType(), ValueVTs);
5572 for (EVT VT : ValueVTs)
5573 Results.push_back(DAG.getPOISON(VT));
5574 setValue(&I, DAG.getMergeValues(Results, DL));
5575 }
5576 return;
5577 }
5578
5579 // Infos is set by getTgtMemIntrinsic.
5581 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5582 TLI.getTgtMemIntrinsic(Infos, I, DAG.getMachineFunction(), Intrinsic);
5583 // Use the first (primary) info determines the node opcode.
5584 TargetLowering::IntrinsicInfo *Info = !Infos.empty() ? &Infos[0] : nullptr;
5585
5587 getTargetIntrinsicOperands(I, HasChain, OnlyLoad, Info);
5588 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
5589
5590 // Propagate fast-math-flags from IR to node(s).
5591 SDNodeFlags Flags;
5592 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5593 Flags.copyFMF(*FPMO);
5594 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5595
5596 // Create the node.
5598
5599 // In some cases, custom collection of operands from CallInst I may be needed.
5601 if (!Infos.empty()) {
5602 // This is target intrinsic that touches memory
5603 // Create MachineMemOperands for each memory access described by the target.
5604 MachineFunction &MF = DAG.getMachineFunction();
5606 for (const auto &Info : Infos) {
5607 // TODO: We currently just fallback to address space 0 if
5608 // getTgtMemIntrinsic didn't yield anything useful.
5609 MachinePointerInfo MPI;
5610 if (Info.ptrVal)
5611 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5612 else if (Info.fallbackAddressSpace)
5613 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5614 EVT MemVT = Info.memVT;
5615 LocationSize Size = LocationSize::precise(Info.size);
5616 if (Size.hasValue() && !Size.getValue())
5618 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5619 MachineMemOperand *MMO = MF.getMachineMemOperand(
5620 MPI, Info.flags, Size, Alignment, I.getAAMetadata(),
5621 /*Ranges=*/nullptr, Info.ssid, Info.order, Info.failureOrder);
5622 MMOs.push_back(MMO);
5623 }
5624
5625 Result = DAG.getMemIntrinsicNode(Info->opc, getCurSDLoc(), VTs, Ops,
5626 Info->memVT, MMOs);
5627 } else {
5628 Result = getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
5629 }
5630
5631 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
5632
5633 setValue(&I, Result);
5634}
5635
5636/// GetSignificand - Get the significand and build it into a floating-point
5637/// number with exponent of 1:
5638///
5639/// Op = (Op & 0x007fffff) | 0x3f800000;
5640///
5641/// where Op is the hexadecimal representation of floating point value.
5643 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5644 DAG.getConstant(0x007fffff, dl, MVT::i32));
5645 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5646 DAG.getConstant(0x3f800000, dl, MVT::i32));
5647 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5648}
5649
5650/// GetExponent - Get the exponent:
5651///
5652/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5653///
5654/// where Op is the hexadecimal representation of floating point value.
5656 const TargetLowering &TLI, const SDLoc &dl) {
5657 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5658 DAG.getConstant(0x7f800000, dl, MVT::i32));
5659 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5660 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5661 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5662 DAG.getConstant(127, dl, MVT::i32));
5663 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5664}
5665
5666/// getF32Constant - Get 32-bit floating point constant.
5667static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt,
5668 const SDLoc &dl) {
5669 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5670 MVT::f32);
5671}
5672
5674 SelectionDAG &DAG) {
5675 // TODO: What fast-math-flags should be set on the floating-point nodes?
5676
5677 // IntegerPartOfX = ((int32_t)(t0);
5678 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5679
5680 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5681 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5682 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5683
5684 // IntegerPartOfX <<= 23;
5685 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5686 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5687
5688 SDValue TwoToFractionalPartOfX;
5689 if (LimitFloatPrecision <= 6) {
5690 // For floating-point precision of 6:
5691 //
5692 // TwoToFractionalPartOfX =
5693 // 0.997535578f +
5694 // (0.735607626f + 0.252464424f * x) * x;
5695 //
5696 // error 0.0144103317, which is 6 bits
5697 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5698 getF32Constant(DAG, 0x3e814304, dl));
5699 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5700 getF32Constant(DAG, 0x3f3c50c8, dl));
5701 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5702 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5703 getF32Constant(DAG, 0x3f7f5e7e, dl));
5704 } else if (LimitFloatPrecision <= 12) {
5705 // For floating-point precision of 12:
5706 //
5707 // TwoToFractionalPartOfX =
5708 // 0.999892986f +
5709 // (0.696457318f +
5710 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5711 //
5712 // error 0.000107046256, which is 13 to 14 bits
5713 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5714 getF32Constant(DAG, 0x3da235e3, dl));
5715 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5716 getF32Constant(DAG, 0x3e65b8f3, dl));
5717 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5718 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5719 getF32Constant(DAG, 0x3f324b07, dl));
5720 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5721 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5722 getF32Constant(DAG, 0x3f7ff8fd, dl));
5723 } else { // LimitFloatPrecision <= 18
5724 // For floating-point precision of 18:
5725 //
5726 // TwoToFractionalPartOfX =
5727 // 0.999999982f +
5728 // (0.693148872f +
5729 // (0.240227044f +
5730 // (0.554906021e-1f +
5731 // (0.961591928e-2f +
5732 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5733 // error 2.47208000*10^(-7), which is better than 18 bits
5734 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5735 getF32Constant(DAG, 0x3924b03e, dl));
5736 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5737 getF32Constant(DAG, 0x3ab24b87, dl));
5738 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5739 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5740 getF32Constant(DAG, 0x3c1d8c17, dl));
5741 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5742 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5743 getF32Constant(DAG, 0x3d634a1d, dl));
5744 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5745 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5746 getF32Constant(DAG, 0x3e75fe14, dl));
5747 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5748 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5749 getF32Constant(DAG, 0x3f317234, dl));
5750 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5751 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5752 getF32Constant(DAG, 0x3f800000, dl));
5753 }
5754
5755 // Add the exponent into the result in integer domain.
5756 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5757 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5758 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5759}
5760
5761/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5762/// limited-precision mode.
5764 const TargetLowering &TLI, SDNodeFlags Flags) {
5765 if (Op.getValueType() == MVT::f32 &&
5767
5768 // Put the exponent in the right bit position for later addition to the
5769 // final result:
5770 //
5771 // t0 = Op * log2(e)
5772
5773 // TODO: What fast-math-flags should be set here?
5774 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5775 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5776 return getLimitedPrecisionExp2(t0, dl, DAG);
5777 }
5778
5779 // No special expansion.
5780 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5781}
5782
5783/// expandLog - Lower a log intrinsic. Handles the special sequences for
5784/// limited-precision mode.
5786 const TargetLowering &TLI, SDNodeFlags Flags) {
5787 // TODO: What fast-math-flags should be set on the floating-point nodes?
5788
5789 if (Op.getValueType() == MVT::f32 &&
5791 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5792
5793 // Scale the exponent by log(2).
5794 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5795 SDValue LogOfExponent =
5796 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5797 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5798
5799 // Get the significand and build it into a floating-point number with
5800 // exponent of 1.
5801 SDValue X = GetSignificand(DAG, Op1, dl);
5802
5803 SDValue LogOfMantissa;
5804 if (LimitFloatPrecision <= 6) {
5805 // For floating-point precision of 6:
5806 //
5807 // LogofMantissa =
5808 // -1.1609546f +
5809 // (1.4034025f - 0.23903021f * x) * x;
5810 //
5811 // error 0.0034276066, which is better than 8 bits
5812 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5813 getF32Constant(DAG, 0xbe74c456, dl));
5814 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5815 getF32Constant(DAG, 0x3fb3a2b1, dl));
5816 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5817 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5818 getF32Constant(DAG, 0x3f949a29, dl));
5819 } else if (LimitFloatPrecision <= 12) {
5820 // For floating-point precision of 12:
5821 //
5822 // LogOfMantissa =
5823 // -1.7417939f +
5824 // (2.8212026f +
5825 // (-1.4699568f +
5826 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5827 //
5828 // error 0.000061011436, which is 14 bits
5829 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5830 getF32Constant(DAG, 0xbd67b6d6, dl));
5831 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5832 getF32Constant(DAG, 0x3ee4f4b8, dl));
5833 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5834 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5835 getF32Constant(DAG, 0x3fbc278b, dl));
5836 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5837 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5838 getF32Constant(DAG, 0x40348e95, dl));
5839 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5840 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5841 getF32Constant(DAG, 0x3fdef31a, dl));
5842 } else { // LimitFloatPrecision <= 18
5843 // For floating-point precision of 18:
5844 //
5845 // LogOfMantissa =
5846 // -2.1072184f +
5847 // (4.2372794f +
5848 // (-3.7029485f +
5849 // (2.2781945f +
5850 // (-0.87823314f +
5851 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5852 //
5853 // error 0.0000023660568, which is better than 18 bits
5854 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5855 getF32Constant(DAG, 0xbc91e5ac, dl));
5856 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5857 getF32Constant(DAG, 0x3e4350aa, dl));
5858 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5859 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5860 getF32Constant(DAG, 0x3f60d3e3, dl));
5861 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5862 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5863 getF32Constant(DAG, 0x4011cdf0, dl));
5864 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5865 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5866 getF32Constant(DAG, 0x406cfd1c, dl));
5867 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5868 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5869 getF32Constant(DAG, 0x408797cb, dl));
5870 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5871 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5872 getF32Constant(DAG, 0x4006dcab, dl));
5873 }
5874
5875 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5876 }
5877
5878 // No special expansion.
5879 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5880}
5881
5882/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5883/// limited-precision mode.
5885 const TargetLowering &TLI, SDNodeFlags Flags) {
5886 // TODO: What fast-math-flags should be set on the floating-point nodes?
5887
5888 if (Op.getValueType() == MVT::f32 &&
5890 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5891
5892 // Get the exponent.
5893 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5894
5895 // Get the significand and build it into a floating-point number with
5896 // exponent of 1.
5897 SDValue X = GetSignificand(DAG, Op1, dl);
5898
5899 // Different possible minimax approximations of significand in
5900 // floating-point for various degrees of accuracy over [1,2].
5901 SDValue Log2ofMantissa;
5902 if (LimitFloatPrecision <= 6) {
5903 // For floating-point precision of 6:
5904 //
5905 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5906 //
5907 // error 0.0049451742, which is more than 7 bits
5908 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5909 getF32Constant(DAG, 0xbeb08fe0, dl));
5910 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5911 getF32Constant(DAG, 0x40019463, dl));
5912 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5913 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5914 getF32Constant(DAG, 0x3fd6633d, dl));
5915 } else if (LimitFloatPrecision <= 12) {
5916 // For floating-point precision of 12:
5917 //
5918 // Log2ofMantissa =
5919 // -2.51285454f +
5920 // (4.07009056f +
5921 // (-2.12067489f +
5922 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5923 //
5924 // error 0.0000876136000, which is better than 13 bits
5925 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5926 getF32Constant(DAG, 0xbda7262e, dl));
5927 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5928 getF32Constant(DAG, 0x3f25280b, dl));
5929 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5930 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5931 getF32Constant(DAG, 0x4007b923, dl));
5932 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5933 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5934 getF32Constant(DAG, 0x40823e2f, dl));
5935 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5936 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5937 getF32Constant(DAG, 0x4020d29c, dl));
5938 } else { // LimitFloatPrecision <= 18
5939 // For floating-point precision of 18:
5940 //
5941 // Log2ofMantissa =
5942 // -3.0400495f +
5943 // (6.1129976f +
5944 // (-5.3420409f +
5945 // (3.2865683f +
5946 // (-1.2669343f +
5947 // (0.27515199f -
5948 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5949 //
5950 // error 0.0000018516, which is better than 18 bits
5951 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5952 getF32Constant(DAG, 0xbcd2769e, dl));
5953 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5954 getF32Constant(DAG, 0x3e8ce0b9, dl));
5955 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5956 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5957 getF32Constant(DAG, 0x3fa22ae7, dl));
5958 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5959 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5960 getF32Constant(DAG, 0x40525723, dl));
5961 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5962 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5963 getF32Constant(DAG, 0x40aaf200, dl));
5964 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5965 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5966 getF32Constant(DAG, 0x40c39dad, dl));
5967 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5968 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5969 getF32Constant(DAG, 0x4042902c, dl));
5970 }
5971
5972 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5973 }
5974
5975 // No special expansion.
5976 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5977}
5978
5979/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5980/// limited-precision mode.
5982 const TargetLowering &TLI, SDNodeFlags Flags) {
5983 // TODO: What fast-math-flags should be set on the floating-point nodes?
5984
5985 if (Op.getValueType() == MVT::f32 &&
5987 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5988
5989 // Scale the exponent by log10(2) [0.30102999f].
5990 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5991 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5992 getF32Constant(DAG, 0x3e9a209a, dl));
5993
5994 // Get the significand and build it into a floating-point number with
5995 // exponent of 1.
5996 SDValue X = GetSignificand(DAG, Op1, dl);
5997
5998 SDValue Log10ofMantissa;
5999 if (LimitFloatPrecision <= 6) {
6000 // For floating-point precision of 6:
6001 //
6002 // Log10ofMantissa =
6003 // -0.50419619f +
6004 // (0.60948995f - 0.10380950f * x) * x;
6005 //
6006 // error 0.0014886165, which is 6 bits
6007 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
6008 getF32Constant(DAG, 0xbdd49a13, dl));
6009 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
6010 getF32Constant(DAG, 0x3f1c0789, dl));
6011 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
6012 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
6013 getF32Constant(DAG, 0x3f011300, dl));
6014 } else if (LimitFloatPrecision <= 12) {
6015 // For floating-point precision of 12:
6016 //
6017 // Log10ofMantissa =
6018 // -0.64831180f +
6019 // (0.91751397f +
6020 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
6021 //
6022 // error 0.00019228036, which is better than 12 bits
6023 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
6024 getF32Constant(DAG, 0x3d431f31, dl));
6025 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
6026 getF32Constant(DAG, 0x3ea21fb2, dl));
6027 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
6028 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
6029 getF32Constant(DAG, 0x3f6ae232, dl));
6030 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
6031 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
6032 getF32Constant(DAG, 0x3f25f7c3, dl));
6033 } else { // LimitFloatPrecision <= 18
6034 // For floating-point precision of 18:
6035 //
6036 // Log10ofMantissa =
6037 // -0.84299375f +
6038 // (1.5327582f +
6039 // (-1.0688956f +
6040 // (0.49102474f +
6041 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
6042 //
6043 // error 0.0000037995730, which is better than 18 bits
6044 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
6045 getF32Constant(DAG, 0x3c5d51ce, dl));
6046 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
6047 getF32Constant(DAG, 0x3e00685a, dl));
6048 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
6049 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
6050 getF32Constant(DAG, 0x3efb6798, dl));
6051 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
6052 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
6053 getF32Constant(DAG, 0x3f88d192, dl));
6054 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
6055 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
6056 getF32Constant(DAG, 0x3fc4316c, dl));
6057 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
6058 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
6059 getF32Constant(DAG, 0x3f57ce70, dl));
6060 }
6061
6062 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
6063 }
6064
6065 // No special expansion.
6066 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
6067}
6068
6069/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
6070/// limited-precision mode.
6072 const TargetLowering &TLI, SDNodeFlags Flags) {
6073 if (Op.getValueType() == MVT::f32 &&
6075 return getLimitedPrecisionExp2(Op, dl, DAG);
6076
6077 // No special expansion.
6078 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
6079}
6080
6081/// visitPow - Lower a pow intrinsic. Handles the special sequences for
6082/// limited-precision mode with x == 10.0f.
6084 SelectionDAG &DAG, const TargetLowering &TLI,
6085 SDNodeFlags Flags) {
6086 bool IsExp10 = false;
6087 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
6090 APFloat Ten(10.0f);
6091 IsExp10 = LHSC->isExactlyValue(Ten);
6092 }
6093 }
6094
6095 // TODO: What fast-math-flags should be set on the FMUL node?
6096 if (IsExp10) {
6097 // Put the exponent in the right bit position for later addition to the
6098 // final result:
6099 //
6100 // #define LOG2OF10 3.3219281f
6101 // t0 = Op * LOG2OF10;
6102 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
6103 getF32Constant(DAG, 0x40549a78, dl));
6104 return getLimitedPrecisionExp2(t0, dl, DAG);
6105 }
6106
6107 // No special expansion.
6108 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
6109}
6110
6111/// ExpandPowI - Expand a llvm.powi intrinsic.
6113 SelectionDAG &DAG) {
6114 // If RHS is a constant, we can expand this out to a multiplication tree if
6115 // it's beneficial on the target, otherwise we end up lowering to a call to
6116 // __powidf2 (for example).
6118 unsigned Val = RHSC->getSExtValue();
6119
6120 // powi(x, 0) -> 1.0
6121 if (Val == 0)
6122 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
6123
6125 Val, DAG.shouldOptForSize())) {
6126 // Get the exponent as a positive value.
6127 if ((int)Val < 0)
6128 Val = -Val;
6129 // We use the simple binary decomposition method to generate the multiply
6130 // sequence. There are more optimal ways to do this (for example,
6131 // powi(x,15) generates one more multiply than it should), but this has
6132 // the benefit of being both really simple and much better than a libcall.
6133 SDValue Res; // Logically starts equal to 1.0
6134 SDValue CurSquare = LHS;
6135 // TODO: Intrinsics should have fast-math-flags that propagate to these
6136 // nodes.
6137 while (Val) {
6138 if (Val & 1) {
6139 if (Res.getNode())
6140 Res =
6141 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
6142 else
6143 Res = CurSquare; // 1.0*CurSquare.
6144 }
6145
6146 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
6147 CurSquare, CurSquare);
6148 Val >>= 1;
6149 }
6150
6151 // If the original was negative, invert the result, producing 1/(x*x*x).
6152 if (RHSC->getSExtValue() < 0)
6153 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
6154 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
6155 return Res;
6156 }
6157 }
6158
6159 // Otherwise, expand to a libcall.
6160 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
6161}
6162
6163static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
6164 SDValue LHS, SDValue RHS, SDValue Scale,
6165 SelectionDAG &DAG, const TargetLowering &TLI) {
6166 EVT VT = LHS.getValueType();
6167 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
6168 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
6169 LLVMContext &Ctx = *DAG.getContext();
6170
6171 // If the type is legal but the operation isn't, this node might survive all
6172 // the way to operation legalization. If we end up there and we do not have
6173 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
6174 // node.
6175
6176 // Coax the legalizer into expanding the node during type legalization instead
6177 // by bumping the size by one bit. This will force it to Promote, enabling the
6178 // early expansion and avoiding the need to expand later.
6179
6180 // We don't have to do this if Scale is 0; that can always be expanded, unless
6181 // it's a saturating signed operation. Those can experience true integer
6182 // division overflow, a case which we must avoid.
6183
6184 // FIXME: We wouldn't have to do this (or any of the early
6185 // expansion/promotion) if it was possible to expand a libcall of an
6186 // illegal type during operation legalization. But it's not, so things
6187 // get a bit hacky.
6188 unsigned ScaleInt = Scale->getAsZExtVal();
6189 if ((ScaleInt > 0 || (Saturating && Signed)) &&
6190 (TLI.isTypeLegal(VT) ||
6191 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6193 Opcode, VT, ScaleInt);
6194 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6195 EVT PromVT;
6196 if (VT.isScalarInteger())
6197 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6198 else if (VT.isVector()) {
6199 PromVT = VT.getVectorElementType();
6200 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6201 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6202 } else
6203 llvm_unreachable("Wrong VT for DIVFIX?");
6204 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6205 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6206 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6207 // For saturating operations, we need to shift up the LHS to get the
6208 // proper saturation width, and then shift down again afterwards.
6209 if (Saturating)
6210 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6211 DAG.getConstant(1, DL, ShiftTy));
6212 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6213 if (Saturating)
6214 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6215 DAG.getConstant(1, DL, ShiftTy));
6216 return DAG.getZExtOrTrunc(Res, DL, VT);
6217 }
6218 }
6219
6220 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6221}
6222
6223// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6224// bitcasted, or split argument. Returns a list of <Register, size in bits>
6225static void
6226getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6227 const SDValue &N) {
6228 switch (N.getOpcode()) {
6229 case ISD::CopyFromReg: {
6230 SDValue Op = N.getOperand(1);
6231 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6232 Op.getValueType().getSizeInBits());
6233 return;
6234 }
6235 case ISD::BITCAST:
6236 case ISD::AssertZext:
6237 case ISD::AssertSext:
6238 case ISD::TRUNCATE:
6239 getUnderlyingArgRegs(Regs, N.getOperand(0));
6240 return;
6241 case ISD::BUILD_PAIR:
6242 case ISD::BUILD_VECTOR:
6244 for (SDValue Op : N->op_values())
6245 getUnderlyingArgRegs(Regs, Op);
6246 return;
6247 default:
6248 return;
6249 }
6250}
6251
6252/// If the DbgValueInst is a dbg_value of a function argument, create the
6253/// corresponding DBG_VALUE machine instruction for it now. At the end of
6254/// instruction selection, they will be inserted to the entry BB.
6255/// We don't currently support this for variadic dbg_values, as they shouldn't
6256/// appear for function arguments or in the prologue.
6257bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6258 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6259 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6260 const Argument *Arg = dyn_cast<Argument>(V);
6261 if (!Arg)
6262 return false;
6263
6264 MachineFunction &MF = DAG.getMachineFunction();
6265 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6266
6267 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6268 // we've been asked to pursue.
6269 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6270 bool Indirect) {
6271 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6272 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6273 // pointing at the VReg, which will be patched up later.
6274 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6276 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6277 /* isKill */ false, /* isDead */ false,
6278 /* isUndef */ false, /* isEarlyClobber */ false,
6279 /* SubReg */ 0, /* isDebug */ true)});
6280
6281 auto *NewDIExpr = FragExpr;
6282 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6283 // the DIExpression.
6284 if (Indirect)
6285 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6287 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6288 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6289 } else {
6290 // Create a completely standard DBG_VALUE.
6291 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6292 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6293 }
6294 };
6295
6296 if (Kind == FuncArgumentDbgValueKind::Value) {
6297 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6298 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6299 // the entry block.
6300 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6301 if (!IsInEntryBlock)
6302 return false;
6303
6304 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6305 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6306 // variable that also is a param.
6307 //
6308 // Although, if we are at the top of the entry block already, we can still
6309 // emit using ArgDbgValue. This might catch some situations when the
6310 // dbg.value refers to an argument that isn't used in the entry block, so
6311 // any CopyToReg node would be optimized out and the only way to express
6312 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6313 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6314 // we should only emit as ArgDbgValue if the Variable is an argument to the
6315 // current function, and the dbg.value intrinsic is found in the entry
6316 // block.
6317 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6318 !DL->getInlinedAt();
6319 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6320 if (!IsInPrologue && !VariableIsFunctionInputArg)
6321 return false;
6322
6323 // Here we assume that a function argument on IR level only can be used to
6324 // describe one input parameter on source level. If we for example have
6325 // source code like this
6326 //
6327 // struct A { long x, y; };
6328 // void foo(struct A a, long b) {
6329 // ...
6330 // b = a.x;
6331 // ...
6332 // }
6333 //
6334 // and IR like this
6335 //
6336 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6337 // entry:
6338 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6339 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6340 // call void @llvm.dbg.value(metadata i32 %b, "b",
6341 // ...
6342 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6343 // ...
6344 //
6345 // then the last dbg.value is describing a parameter "b" using a value that
6346 // is an argument. But since we already has used %a1 to describe a parameter
6347 // we should not handle that last dbg.value here (that would result in an
6348 // incorrect hoisting of the DBG_VALUE to the function entry).
6349 // Notice that we allow one dbg.value per IR level argument, to accommodate
6350 // for the situation with fragments above.
6351 // If there is no node for the value being handled, we return true to skip
6352 // the normal generation of debug info, as it would kill existing debug
6353 // info for the parameter in case of duplicates.
6354 if (VariableIsFunctionInputArg) {
6355 unsigned ArgNo = Arg->getArgNo();
6356 if (ArgNo >= FuncInfo.DescribedArgs.size())
6357 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6358 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6359 return !NodeMap[V].getNode();
6360 FuncInfo.DescribedArgs.set(ArgNo);
6361 }
6362 }
6363
6364 bool IsIndirect = false;
6365 std::optional<MachineOperand> Op;
6366 // Some arguments' frame index is recorded during argument lowering.
6367 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6368 if (FI != std::numeric_limits<int>::max())
6370
6372 if (!Op && N.getNode()) {
6373 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6374 Register Reg;
6375 if (ArgRegsAndSizes.size() == 1)
6376 Reg = ArgRegsAndSizes.front().first;
6377
6378 if (Reg && Reg.isVirtual()) {
6379 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6380 Register PR = RegInfo.getLiveInPhysReg(Reg);
6381 if (PR)
6382 Reg = PR;
6383 }
6384 if (Reg) {
6386 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6387 }
6388 }
6389
6390 if (!Op && N.getNode()) {
6391 // Check if frame index is available.
6392 SDValue LCandidate = peekThroughBitcasts(N);
6393 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6394 if (FrameIndexSDNode *FINode =
6395 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6396 Op = MachineOperand::CreateFI(FINode->getIndex());
6397 }
6398
6399 if (!Op) {
6400 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6401 auto splitMultiRegDbgValue =
6402 [&](ArrayRef<std::pair<Register, TypeSize>> SplitRegs) -> bool {
6403 unsigned Offset = 0;
6404 for (const auto &[Reg, RegSizeInBits] : SplitRegs) {
6405 // FIXME: Scalable sizes are not supported in fragment expressions.
6406 if (RegSizeInBits.isScalable())
6407 return false;
6408
6409 // If the expression is already a fragment, the current register
6410 // offset+size might extend beyond the fragment. In this case, only
6411 // the register bits that are inside the fragment are relevant.
6412 int RegFragmentSizeInBits = RegSizeInBits.getFixedValue();
6413 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6414 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6415 // The register is entirely outside the expression fragment,
6416 // so is irrelevant for debug info.
6417 if (Offset >= ExprFragmentSizeInBits)
6418 break;
6419 // The register is partially outside the expression fragment, only
6420 // the low bits within the fragment are relevant for debug info.
6421 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6422 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6423 }
6424 }
6425
6426 auto FragmentExpr = DIExpression::createFragmentExpression(
6427 Expr, Offset, RegFragmentSizeInBits);
6428 Offset += RegSizeInBits.getFixedValue();
6429 // If a valid fragment expression cannot be created, the variable's
6430 // correct value cannot be determined and so it is set as poison.
6431 if (!FragmentExpr) {
6432 SDDbgValue *SDV = DAG.getConstantDbgValue(
6433 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6434 DAG.AddDbgValue(SDV, false);
6435 continue;
6436 }
6437 MachineInstr *NewMI = MakeVRegDbgValue(
6438 Reg, *FragmentExpr, Kind != FuncArgumentDbgValueKind::Value);
6439 FuncInfo.ArgDbgValues.push_back(NewMI);
6440 }
6441
6442 return true;
6443 };
6444
6445 // Check if ValueMap has reg number.
6447 VMI = FuncInfo.ValueMap.find(V);
6448 if (VMI != FuncInfo.ValueMap.end()) {
6449 const auto &TLI = DAG.getTargetLoweringInfo();
6450 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6451 V->getType(), std::nullopt);
6452 if (RFV.occupiesMultipleRegs())
6453 return splitMultiRegDbgValue(RFV.getRegsAndSizes());
6454
6455 Op = MachineOperand::CreateReg(VMI->second, false);
6456 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6457 } else if (ArgRegsAndSizes.size() > 1) {
6458 // This was split due to the calling convention, and no virtual register
6459 // mapping exists for the value.
6460 return splitMultiRegDbgValue(ArgRegsAndSizes);
6461 }
6462 }
6463
6464 if (!Op)
6465 return false;
6466
6467 assert(Variable->isValidLocationForIntrinsic(DL) &&
6468 "Expected inlined-at fields to agree");
6469 MachineInstr *NewMI = nullptr;
6470
6471 if (Op->isReg())
6472 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6473 else
6474 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6475 Variable, Expr);
6476
6477 // Otherwise, use ArgDbgValues.
6478 FuncInfo.ArgDbgValues.push_back(NewMI);
6479 return true;
6480}
6481
6482/// Return the appropriate SDDbgValue based on N.
6483SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6484 DILocalVariable *Variable,
6485 DIExpression *Expr,
6486 const DebugLoc &dl,
6487 unsigned DbgSDNodeOrder) {
6488 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6489 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6490 // stack slot locations.
6491 //
6492 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6493 // debug values here after optimization:
6494 //
6495 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6496 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6497 //
6498 // Both describe the direct values of their associated variables.
6499 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6500 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6501 }
6502 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6503 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6504}
6505
6506static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6507 switch (Intrinsic) {
6508 case Intrinsic::smul_fix:
6509 return ISD::SMULFIX;
6510 case Intrinsic::umul_fix:
6511 return ISD::UMULFIX;
6512 case Intrinsic::smul_fix_sat:
6513 return ISD::SMULFIXSAT;
6514 case Intrinsic::umul_fix_sat:
6515 return ISD::UMULFIXSAT;
6516 case Intrinsic::sdiv_fix:
6517 return ISD::SDIVFIX;
6518 case Intrinsic::udiv_fix:
6519 return ISD::UDIVFIX;
6520 case Intrinsic::sdiv_fix_sat:
6521 return ISD::SDIVFIXSAT;
6522 case Intrinsic::udiv_fix_sat:
6523 return ISD::UDIVFIXSAT;
6524 default:
6525 llvm_unreachable("Unhandled fixed point intrinsic");
6526 }
6527}
6528
6529/// Given a @llvm.call.preallocated.setup, return the corresponding
6530/// preallocated call.
6531static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6532 assert(cast<CallBase>(PreallocatedSetup)
6534 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6535 "expected call_preallocated_setup Value");
6536 for (const auto *U : PreallocatedSetup->users()) {
6537 auto *UseCall = cast<CallBase>(U);
6538 const Function *Fn = UseCall->getCalledFunction();
6539 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6540 return UseCall;
6541 }
6542 }
6543 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6544}
6545
6546/// If DI is a debug value with an EntryValue expression, lower it using the
6547/// corresponding physical register of the associated Argument value
6548/// (guaranteed to exist by the verifier).
6549bool SelectionDAGBuilder::visitEntryValueDbgValue(
6551 DIExpression *Expr, DebugLoc DbgLoc) {
6552 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6553 return false;
6554
6555 // These properties are guaranteed by the verifier.
6556 const Argument *Arg = cast<Argument>(Values[0]);
6557 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6558
6559 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6560 if (ArgIt == FuncInfo.ValueMap.end()) {
6561 LLVM_DEBUG(
6562 dbgs() << "Dropping dbg.value: expression is entry_value but "
6563 "couldn't find an associated register for the Argument\n");
6564 return true;
6565 }
6566 Register ArgVReg = ArgIt->getSecond();
6567
6568 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6569 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6570 SDDbgValue *SDV = DAG.getVRegDbgValue(
6571 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6572 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6573 return true;
6574 }
6575 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6576 "couldn't find a physical register\n");
6577 return true;
6578}
6579
6580/// Lower the call to the specified intrinsic function.
6581void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6582 unsigned Intrinsic) {
6583 SDLoc sdl = getCurSDLoc();
6584 switch (Intrinsic) {
6585 case Intrinsic::experimental_convergence_anchor:
6586 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6587 break;
6588 case Intrinsic::experimental_convergence_entry:
6589 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6590 break;
6591 case Intrinsic::experimental_convergence_loop: {
6592 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6593 auto *Token = Bundle->Inputs[0].get();
6594 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6595 getValue(Token)));
6596 break;
6597 }
6598 }
6599}
6600
6601void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6602 unsigned IntrinsicID) {
6603 // For now, we're only lowering an 'add' histogram.
6604 // We can add others later, e.g. saturating adds, min/max.
6605 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6606 "Tried to lower unsupported histogram type");
6607 SDLoc sdl = getCurSDLoc();
6608 Value *Ptr = I.getOperand(0);
6609 SDValue Inc = getValue(I.getOperand(1));
6610 SDValue Mask = getValue(I.getOperand(2));
6611
6612 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6613 DataLayout TargetDL = DAG.getDataLayout();
6614 EVT VT = Inc.getValueType();
6615 Align Alignment = DAG.getEVTAlign(VT);
6616
6617 const MDNode *Ranges = getRangeMetadata(I);
6618
6619 SDValue Root = DAG.getRoot();
6620 SDValue Base;
6621 SDValue Index;
6622 SDValue Scale;
6623 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6624 I.getParent(), VT.getScalarStoreSize());
6625
6626 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6627
6628 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6629 MachinePointerInfo(AS),
6631 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6632
6633 if (!UniformBase) {
6634 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6635 Index = getValue(Ptr);
6636 Scale =
6637 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6638 }
6639
6640 EVT IdxVT = Index.getValueType();
6641
6642 // Avoid using e.g. i32 as index type when the increment must be performed
6643 // on i64's.
6644 bool MustExtendIndex = VT.getScalarSizeInBits() > IdxVT.getScalarSizeInBits();
6645 EVT EltTy = MustExtendIndex ? VT : IdxVT.getVectorElementType();
6646 if (MustExtendIndex || TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6647 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
6648 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6649 }
6650
6651 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6652
6653 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6654 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6655 Ops, MMO, ISD::SIGNED_SCALED);
6656
6657 setValue(&I, Histogram);
6658 DAG.setRoot(Histogram);
6659}
6660
6661void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6662 unsigned Intrinsic) {
6663 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6664 "Tried lowering invalid vector extract last");
6665 SDLoc sdl = getCurSDLoc();
6666 const DataLayout &Layout = DAG.getDataLayout();
6667 SDValue Data = getValue(I.getOperand(0));
6668 SDValue Mask = getValue(I.getOperand(1));
6669
6670 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6671 EVT ResVT = TLI.getValueType(Layout, I.getType());
6672
6673 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6674 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6675 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6676
6677 Value *Default = I.getOperand(2);
6679 SDValue PassThru = getValue(Default);
6680 EVT BoolVT = Mask.getValueType().getScalarType();
6681 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6682 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6683 }
6684
6685 setValue(&I, Result);
6686}
6687
6688/// Lower the call to the specified intrinsic function.
6689void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6690 unsigned Intrinsic) {
6691 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6692 SDLoc sdl = getCurSDLoc();
6693 DebugLoc dl = getCurDebugLoc();
6694 SDValue Res;
6695
6696 SDNodeFlags Flags;
6697 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6698 Flags.copyFMF(*FPOp);
6699
6700 switch (Intrinsic) {
6701 default:
6702 // By default, turn this into a target intrinsic node.
6703 visitTargetIntrinsic(I, Intrinsic);
6704 return;
6705 case Intrinsic::vscale: {
6706 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6707 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6708 return;
6709 }
6710 case Intrinsic::vastart: visitVAStart(I); return;
6711 case Intrinsic::vaend: visitVAEnd(I); return;
6712 case Intrinsic::vacopy: visitVACopy(I); return;
6713 case Intrinsic::returnaddress:
6714 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6715 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6716 getValue(I.getArgOperand(0))));
6717 return;
6718 case Intrinsic::addressofreturnaddress:
6719 setValue(&I,
6720 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6721 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6722 return;
6723 case Intrinsic::sponentry:
6724 setValue(&I,
6725 DAG.getNode(ISD::SPONENTRY, sdl,
6726 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6727 return;
6728 case Intrinsic::frameaddress:
6729 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6730 TLI.getFrameIndexTy(DAG.getDataLayout()),
6731 getValue(I.getArgOperand(0))));
6732 return;
6733 case Intrinsic::read_volatile_register:
6734 case Intrinsic::read_register: {
6735 Value *Reg = I.getArgOperand(0);
6736 SDValue Chain = getRoot();
6738 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6739 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6740 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6741 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6742 setValue(&I, Res);
6743 DAG.setRoot(Res.getValue(1));
6744 return;
6745 }
6746 case Intrinsic::write_register: {
6747 Value *Reg = I.getArgOperand(0);
6748 Value *RegValue = I.getArgOperand(1);
6749 SDValue Chain = getRoot();
6751 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6752 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6753 RegName, getValue(RegValue)));
6754 return;
6755 }
6756 case Intrinsic::write_volatile_register: {
6757 Value *Reg = I.getArgOperand(0);
6758 Value *RegValue = I.getArgOperand(1);
6759 SDValue Chain = getRoot();
6760 const MDNode *MD = cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata());
6761 SDValue RegName = DAG.getMDNode(MD);
6762 EVT VT = TLI.getValueType(DAG.getDataLayout(), RegValue->getType());
6763 SDValue WriteChain = DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other,
6764 Chain, RegName, getValue(RegValue));
6765 // FAKE_USE of the physical register marks it live after the WRITE_REGISTER,
6766 // preventing the backend from dead-eliminating the write. This is
6767 // preferred over READ_REGISTER, which would emit extra register copies
6768 // (e.g. fmov xN, dN for FP/SIMD registers).
6769 const MDString *RegStr = cast<MDString>(MD->getOperand(0));
6770 LLT Ty = VT.isSimple() ? getLLTForMVT(VT.getSimpleVT()) : LLT();
6771 const MachineFunction &MF = DAG.getMachineFunction();
6772 Register PhysReg =
6773 TLI.getRegisterByName(RegStr->getString().data(), Ty, MF);
6774 if (PhysReg.isValid()) {
6775 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
6776 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(PhysReg);
6777 MVT RegVT = *TRI->legalclasstypes_begin(*RC);
6778 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other,
6779 {WriteChain, DAG.getRegister(PhysReg, RegVT)}));
6780 } else {
6781 DAG.setRoot(WriteChain);
6782 }
6783 return;
6784 }
6785 case Intrinsic::memcpy:
6786 case Intrinsic::memcpy_inline: {
6787 const auto &MCI = cast<MemCpyInst>(I);
6788 SDValue Dst = getValue(I.getArgOperand(0));
6789 SDValue Src = getValue(I.getArgOperand(1));
6790 SDValue Size = getValue(I.getArgOperand(2));
6791 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6792 "memcpy_inline needs constant size");
6793 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6794 Align DstAlign = MCI.getDestAlign().valueOrOne();
6795 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6796 bool isVol = MCI.isVolatile();
6797 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6798 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, DstAlign, SrcAlign,
6799 isVol, MCI.isForceInlined(), &I, std::nullopt,
6800 MachinePointerInfo(I.getArgOperand(0)),
6801 MachinePointerInfo(I.getArgOperand(1)),
6802 I.getAAMetadata(), BatchAA);
6803 updateDAGForMaybeTailCall(MC);
6804 return;
6805 }
6806 case Intrinsic::memset:
6807 case Intrinsic::memset_inline: {
6808 const auto &MSII = cast<MemSetInst>(I);
6809 SDValue Dst = getValue(I.getArgOperand(0));
6810 SDValue Value = getValue(I.getArgOperand(1));
6811 SDValue Size = getValue(I.getArgOperand(2));
6812 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6813 "memset_inline needs constant size");
6814 // @llvm.memset defines 0 and 1 to both mean no alignment.
6815 Align DstAlign = MSII.getDestAlign().valueOrOne();
6816 bool isVol = MSII.isVolatile();
6817 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6818 SDValue MC = DAG.getMemset(
6819 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6820 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6821 updateDAGForMaybeTailCall(MC);
6822 return;
6823 }
6824 case Intrinsic::memmove: {
6825 const auto &MMI = cast<MemMoveInst>(I);
6826 SDValue Op1 = getValue(I.getArgOperand(0));
6827 SDValue Op2 = getValue(I.getArgOperand(1));
6828 SDValue Op3 = getValue(I.getArgOperand(2));
6829 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6830 Align DstAlign = MMI.getDestAlign().valueOrOne();
6831 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6832 bool isVol = MMI.isVolatile();
6833 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6834 SDValue MM = DAG.getMemmove(
6835 Root, sdl, Op1, Op2, Op3, DstAlign, SrcAlign, isVol, &I,
6836 /* OverrideTailCall */ std::nullopt,
6837 MachinePointerInfo(I.getArgOperand(0)),
6838 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata(), BatchAA);
6839 updateDAGForMaybeTailCall(MM);
6840 return;
6841 }
6842 case Intrinsic::memcpy_element_unordered_atomic: {
6843 auto &MI = cast<AnyMemCpyInst>(I);
6844 SDValue Dst = getValue(MI.getRawDest());
6845 SDValue Src = getValue(MI.getRawSource());
6846 SDValue Length = getValue(MI.getLength());
6847
6848 Type *LengthTy = MI.getLength()->getType();
6849 unsigned ElemSz = MI.getElementSizeInBytes();
6850 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6851 SDValue MC =
6852 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6853 isTC, MachinePointerInfo(MI.getRawDest()),
6854 MachinePointerInfo(MI.getRawSource()));
6855 updateDAGForMaybeTailCall(MC);
6856 return;
6857 }
6858 case Intrinsic::memmove_element_unordered_atomic: {
6859 auto &MI = cast<AnyMemMoveInst>(I);
6860 SDValue Dst = getValue(MI.getRawDest());
6861 SDValue Src = getValue(MI.getRawSource());
6862 SDValue Length = getValue(MI.getLength());
6863
6864 Type *LengthTy = MI.getLength()->getType();
6865 unsigned ElemSz = MI.getElementSizeInBytes();
6866 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6867 SDValue MC =
6868 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6869 isTC, MachinePointerInfo(MI.getRawDest()),
6870 MachinePointerInfo(MI.getRawSource()));
6871 updateDAGForMaybeTailCall(MC);
6872 return;
6873 }
6874 case Intrinsic::memset_element_unordered_atomic: {
6875 auto &MI = cast<AnyMemSetInst>(I);
6876 SDValue Dst = getValue(MI.getRawDest());
6877 SDValue Val = getValue(MI.getValue());
6878 SDValue Length = getValue(MI.getLength());
6879
6880 Type *LengthTy = MI.getLength()->getType();
6881 unsigned ElemSz = MI.getElementSizeInBytes();
6882 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6883 SDValue MC =
6884 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6885 isTC, MachinePointerInfo(MI.getRawDest()));
6886 updateDAGForMaybeTailCall(MC);
6887 return;
6888 }
6889 case Intrinsic::call_preallocated_setup: {
6890 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6891 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6892 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6893 getRoot(), SrcValue);
6894 setValue(&I, Res);
6895 DAG.setRoot(Res);
6896 return;
6897 }
6898 case Intrinsic::call_preallocated_arg: {
6899 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6900 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6901 SDValue Ops[3];
6902 Ops[0] = getRoot();
6903 Ops[1] = SrcValue;
6904 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6905 MVT::i32); // arg index
6906 SDValue Res = DAG.getNode(
6908 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6909 setValue(&I, Res);
6910 DAG.setRoot(Res.getValue(1));
6911 return;
6912 }
6913
6914 case Intrinsic::eh_typeid_for: {
6915 // Find the type id for the given typeinfo.
6916 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6917 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6918 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6919 setValue(&I, Res);
6920 return;
6921 }
6922
6923 case Intrinsic::eh_return_i32:
6924 case Intrinsic::eh_return_i64:
6925 DAG.getMachineFunction().setCallsEHReturn(true);
6926 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6927 MVT::Other,
6929 getValue(I.getArgOperand(0)),
6930 getValue(I.getArgOperand(1))));
6931 return;
6932 case Intrinsic::eh_unwind_init:
6933 DAG.getMachineFunction().setCallsUnwindInit(true);
6934 return;
6935 case Intrinsic::eh_dwarf_cfa:
6936 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6937 TLI.getPointerTy(DAG.getDataLayout()),
6938 getValue(I.getArgOperand(0))));
6939 return;
6940 case Intrinsic::eh_sjlj_callsite: {
6941 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6942 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6943
6944 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6945 return;
6946 }
6947 case Intrinsic::eh_sjlj_functioncontext: {
6948 // Get and store the index of the function context.
6949 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6950 AllocaInst *FnCtx =
6951 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6952 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6954 return;
6955 }
6956 case Intrinsic::eh_sjlj_setjmp: {
6957 SDValue Ops[2];
6958 Ops[0] = getRoot();
6959 Ops[1] = getValue(I.getArgOperand(0));
6960 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6961 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6962 setValue(&I, Op.getValue(0));
6963 DAG.setRoot(Op.getValue(1));
6964 return;
6965 }
6966 case Intrinsic::eh_sjlj_longjmp:
6967 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6968 getRoot(), getValue(I.getArgOperand(0))));
6969 return;
6970 case Intrinsic::eh_sjlj_setup_dispatch:
6971 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6972 getRoot()));
6973 return;
6974 case Intrinsic::masked_gather:
6975 visitMaskedGather(I);
6976 return;
6977 case Intrinsic::masked_load:
6978 visitMaskedLoad(I);
6979 return;
6980 case Intrinsic::masked_scatter:
6981 visitMaskedScatter(I);
6982 return;
6983 case Intrinsic::masked_store:
6984 visitMaskedStore(I);
6985 return;
6986 case Intrinsic::masked_expandload:
6987 visitMaskedLoad(I, true /* IsExpanding */);
6988 return;
6989 case Intrinsic::masked_compressstore:
6990 visitMaskedStore(I, true /* IsCompressing */);
6991 return;
6992 case Intrinsic::powi:
6993 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6994 getValue(I.getArgOperand(1)), DAG));
6995 return;
6996 case Intrinsic::log:
6997 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6998 return;
6999 case Intrinsic::log2:
7000 setValue(&I,
7001 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
7002 return;
7003 case Intrinsic::log10:
7004 setValue(&I,
7005 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
7006 return;
7007 case Intrinsic::exp:
7008 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
7009 return;
7010 case Intrinsic::exp2:
7011 setValue(&I,
7012 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
7013 return;
7014 case Intrinsic::pow:
7015 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
7016 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
7017 return;
7018 case Intrinsic::sqrt:
7019 case Intrinsic::fabs:
7020 case Intrinsic::sin:
7021 case Intrinsic::cos:
7022 case Intrinsic::tan:
7023 case Intrinsic::asin:
7024 case Intrinsic::acos:
7025 case Intrinsic::atan:
7026 case Intrinsic::sinh:
7027 case Intrinsic::cosh:
7028 case Intrinsic::tanh:
7029 case Intrinsic::exp10:
7030 case Intrinsic::floor:
7031 case Intrinsic::ceil:
7032 case Intrinsic::trunc:
7033 case Intrinsic::rint:
7034 case Intrinsic::nearbyint:
7035 case Intrinsic::round:
7036 case Intrinsic::roundeven:
7037 case Intrinsic::canonicalize: {
7038 unsigned Opcode;
7039 // clang-format off
7040 switch (Intrinsic) {
7041 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7042 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
7043 case Intrinsic::fabs: Opcode = ISD::FABS; break;
7044 case Intrinsic::sin: Opcode = ISD::FSIN; break;
7045 case Intrinsic::cos: Opcode = ISD::FCOS; break;
7046 case Intrinsic::tan: Opcode = ISD::FTAN; break;
7047 case Intrinsic::asin: Opcode = ISD::FASIN; break;
7048 case Intrinsic::acos: Opcode = ISD::FACOS; break;
7049 case Intrinsic::atan: Opcode = ISD::FATAN; break;
7050 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
7051 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
7052 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
7053 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
7054 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
7055 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
7056 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
7057 case Intrinsic::rint: Opcode = ISD::FRINT; break;
7058 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
7059 case Intrinsic::round: Opcode = ISD::FROUND; break;
7060 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
7061 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
7062 }
7063 // clang-format on
7064
7065 setValue(&I, DAG.getNode(Opcode, sdl,
7066 getValue(I.getArgOperand(0)).getValueType(),
7067 getValue(I.getArgOperand(0)), Flags));
7068 return;
7069 }
7070 case Intrinsic::atan2:
7071 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
7072 getValue(I.getArgOperand(0)).getValueType(),
7073 getValue(I.getArgOperand(0)),
7074 getValue(I.getArgOperand(1)), Flags));
7075 return;
7076 case Intrinsic::lround:
7077 case Intrinsic::llround:
7078 case Intrinsic::lrint:
7079 case Intrinsic::llrint: {
7080 unsigned Opcode;
7081 // clang-format off
7082 switch (Intrinsic) {
7083 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7084 case Intrinsic::lround: Opcode = ISD::LROUND; break;
7085 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
7086 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
7087 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
7088 }
7089 // clang-format on
7090
7091 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7092 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
7093 getValue(I.getArgOperand(0))));
7094 return;
7095 }
7096 case Intrinsic::minnum:
7097 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
7098 getValue(I.getArgOperand(0)).getValueType(),
7099 getValue(I.getArgOperand(0)),
7100 getValue(I.getArgOperand(1)), Flags));
7101 return;
7102 case Intrinsic::maxnum:
7103 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
7104 getValue(I.getArgOperand(0)).getValueType(),
7105 getValue(I.getArgOperand(0)),
7106 getValue(I.getArgOperand(1)), Flags));
7107 return;
7108 case Intrinsic::minimum:
7109 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
7110 getValue(I.getArgOperand(0)).getValueType(),
7111 getValue(I.getArgOperand(0)),
7112 getValue(I.getArgOperand(1)), Flags));
7113 return;
7114 case Intrinsic::maximum:
7115 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
7116 getValue(I.getArgOperand(0)).getValueType(),
7117 getValue(I.getArgOperand(0)),
7118 getValue(I.getArgOperand(1)), Flags));
7119 return;
7120 case Intrinsic::minimumnum:
7121 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
7122 getValue(I.getArgOperand(0)).getValueType(),
7123 getValue(I.getArgOperand(0)),
7124 getValue(I.getArgOperand(1)), Flags));
7125 return;
7126 case Intrinsic::maximumnum:
7127 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
7128 getValue(I.getArgOperand(0)).getValueType(),
7129 getValue(I.getArgOperand(0)),
7130 getValue(I.getArgOperand(1)), Flags));
7131 return;
7132 case Intrinsic::copysign:
7133 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
7134 getValue(I.getArgOperand(0)).getValueType(),
7135 getValue(I.getArgOperand(0)),
7136 getValue(I.getArgOperand(1)), Flags));
7137 return;
7138 case Intrinsic::ldexp:
7139 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
7140 getValue(I.getArgOperand(0)).getValueType(),
7141 getValue(I.getArgOperand(0)),
7142 getValue(I.getArgOperand(1)), Flags));
7143 return;
7144 case Intrinsic::modf:
7145 case Intrinsic::sincos:
7146 case Intrinsic::sincospi:
7147 case Intrinsic::frexp: {
7148 unsigned Opcode;
7149 switch (Intrinsic) {
7150 default:
7151 llvm_unreachable("unexpected intrinsic");
7152 case Intrinsic::sincos:
7153 Opcode = ISD::FSINCOS;
7154 break;
7155 case Intrinsic::sincospi:
7156 Opcode = ISD::FSINCOSPI;
7157 break;
7158 case Intrinsic::modf:
7159 Opcode = ISD::FMODF;
7160 break;
7161 case Intrinsic::frexp:
7162 Opcode = ISD::FFREXP;
7163 break;
7164 }
7165 SmallVector<EVT, 2> ValueVTs;
7166 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
7167 SDVTList VTs = DAG.getVTList(ValueVTs);
7168 setValue(
7169 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
7170 return;
7171 }
7172 case Intrinsic::arithmetic_fence: {
7173 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
7174 getValue(I.getArgOperand(0)).getValueType(),
7175 getValue(I.getArgOperand(0)), Flags));
7176 return;
7177 }
7178 case Intrinsic::fma:
7179 setValue(&I, DAG.getNode(
7180 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
7181 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
7182 getValue(I.getArgOperand(2)), Flags));
7183 return;
7184#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
7185 case Intrinsic::INTRINSIC:
7186#include "llvm/IR/ConstrainedOps.def"
7187 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
7188 return;
7189#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
7190#include "llvm/IR/VPIntrinsics.def"
7191 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
7192 return;
7193 case Intrinsic::fptrunc_round: {
7194 // Get the last argument, the metadata and convert it to an integer in the
7195 // call
7196 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7197 std::optional<RoundingMode> RoundMode =
7198 convertStrToRoundingMode(cast<MDString>(MD)->getString());
7199
7200 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7201
7202 // Propagate fast-math-flags from IR to node(s).
7203 SDNodeFlags Flags;
7204 Flags.copyFMF(*cast<FPMathOperator>(&I));
7205 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
7206
7208 Result = DAG.getNode(
7209 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
7210 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
7211 setValue(&I, Result);
7212
7213 return;
7214 }
7215 case Intrinsic::fmuladd: {
7216 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7217 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
7218 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
7219 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7220 getValue(I.getArgOperand(0)).getValueType(),
7221 getValue(I.getArgOperand(0)),
7222 getValue(I.getArgOperand(1)),
7223 getValue(I.getArgOperand(2)), Flags));
7224 } else if (TLI.isOperationLegalOrCustom(ISD::FMULADD, VT)) {
7225 // TODO: Support splitting the vector.
7226 setValue(&I, DAG.getNode(ISD::FMULADD, sdl,
7227 getValue(I.getArgOperand(0)).getValueType(),
7228 getValue(I.getArgOperand(0)),
7229 getValue(I.getArgOperand(1)),
7230 getValue(I.getArgOperand(2)), Flags));
7231 } else {
7232 // TODO: Intrinsic calls should have fast-math-flags.
7233 SDValue Mul = DAG.getNode(
7234 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7235 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7236 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7237 getValue(I.getArgOperand(0)).getValueType(),
7238 Mul, getValue(I.getArgOperand(2)), Flags);
7239 setValue(&I, Add);
7240 }
7241 return;
7242 }
7243 case Intrinsic::fptosi_sat: {
7244 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7245 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7246 getValue(I.getArgOperand(0)),
7247 DAG.getValueType(VT.getScalarType())));
7248 return;
7249 }
7250 case Intrinsic::fptoui_sat: {
7251 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7252 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7253 getValue(I.getArgOperand(0)),
7254 DAG.getValueType(VT.getScalarType())));
7255 return;
7256 }
7257 case Intrinsic::convert_from_arbitrary_fp: {
7258 // Extract format metadata and convert to semantics enum.
7259 EVT DstVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7260 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7261 StringRef FormatStr = cast<MDString>(MD)->getString();
7262 const fltSemantics *SrcSem =
7264 if (!SrcSem) {
7265 DAG.getContext()->emitError(
7266 "convert_from_arbitrary_fp: not implemented format '" + FormatStr +
7267 "'");
7268 setValue(&I, DAG.getPOISON(DstVT));
7269 return;
7270 }
7272
7273 SDValue IntVal = getValue(I.getArgOperand(0));
7274
7275 // Emit ISD::CONVERT_FROM_ARBITRARY_FP node.
7276 SDValue SemConst =
7277 DAG.getTargetConstant(static_cast<int>(SemEnum), sdl, MVT::i32);
7278 setValue(&I, DAG.getNode(ISD::CONVERT_FROM_ARBITRARY_FP, sdl, DstVT, IntVal,
7279 SemConst));
7280 return;
7281 }
7282 case Intrinsic::convert_to_arbitrary_fp: {
7283 // Extract format metadata and convert to semantics enum.
7284 EVT DstVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7285 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7286 StringRef FormatStr = cast<MDString>(MD)->getString();
7287 const fltSemantics *DstSem =
7289 if (!DstSem) {
7290 DAG.getContext()->emitError(
7291 "convert_to_arbitrary_fp: not implemented format '" + FormatStr +
7292 "'");
7293 setValue(&I, DAG.getPOISON(DstVT));
7294 return;
7295 }
7297
7298 Metadata *RoundMD =
7299 cast<MetadataAsValue>(I.getArgOperand(2))->getMetadata();
7300 StringRef RoundStr = cast<MDString>(RoundMD)->getString();
7301 std::optional<RoundingMode> RoundMode = convertStrToRoundingMode(RoundStr);
7302 assert(RoundMode && *RoundMode != RoundingMode::Dynamic &&
7303 "Dynamic rounding mode should have been rejected by the verifier");
7304
7305 uint64_t Saturate =
7306 cast<ConstantInt>(I.getArgOperand(3))->getZExtValue() ? 1 : 0;
7307
7308 SDValue FloatVal = getValue(I.getArgOperand(0));
7309
7310 SDValue SemConst =
7311 DAG.getTargetConstant(static_cast<int>(SemEnum), sdl, MVT::i32);
7312 SDValue RoundConst =
7313 DAG.getTargetConstant(static_cast<int>(*RoundMode), sdl, MVT::i32);
7314 SDValue SatConst = DAG.getTargetConstant(Saturate, sdl, MVT::i32);
7315 setValue(&I, DAG.getNode(ISD::CONVERT_TO_ARBITRARY_FP, sdl, DstVT, FloatVal,
7316 SemConst, RoundConst, SatConst));
7317 return;
7318 }
7319 case Intrinsic::set_rounding:
7320 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7321 {getRoot(), getValue(I.getArgOperand(0))});
7322 setValue(&I, Res);
7323 DAG.setRoot(Res.getValue(0));
7324 return;
7325 case Intrinsic::is_fpclass: {
7326 const DataLayout DLayout = DAG.getDataLayout();
7327 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7328 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7329 FPClassTest Test = static_cast<FPClassTest>(
7330 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7331 MachineFunction &MF = DAG.getMachineFunction();
7332 const Function &F = MF.getFunction();
7333 SDValue Op = getValue(I.getArgOperand(0));
7334 SDNodeFlags Flags;
7335 Flags.setNoFPExcept(
7336 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7337 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7338 // expansion can use illegal types. Making expansion early allows
7339 // legalizing these types prior to selection.
7340 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7341 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7342 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7343 setValue(&I, Result);
7344 return;
7345 }
7346
7347 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7348 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7349 setValue(&I, V);
7350 return;
7351 }
7352 case Intrinsic::get_fpenv: {
7353 const DataLayout DLayout = DAG.getDataLayout();
7354 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7355 Align TempAlign = DAG.getEVTAlign(EnvVT);
7356 SDValue Chain = getRoot();
7357 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7358 // and temporary storage in stack.
7359 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7360 Res = DAG.getNode(
7361 ISD::GET_FPENV, sdl,
7362 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7363 MVT::Other),
7364 Chain);
7365 } else {
7366 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7367 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7368 auto MPI =
7369 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7370 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7372 TempAlign);
7373 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7374 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7375 }
7376 setValue(&I, Res);
7377 DAG.setRoot(Res.getValue(1));
7378 return;
7379 }
7380 case Intrinsic::set_fpenv: {
7381 const DataLayout DLayout = DAG.getDataLayout();
7382 SDValue Env = getValue(I.getArgOperand(0));
7383 EVT EnvVT = Env.getValueType();
7384 Align TempAlign = DAG.getEVTAlign(EnvVT);
7385 SDValue Chain = getRoot();
7386 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7387 // environment from memory.
7388 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7389 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7390 } else {
7391 // Allocate space in stack, copy environment bits into it and use this
7392 // memory in SET_FPENV_MEM.
7393 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7394 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7395 auto MPI =
7396 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7397 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7399 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7401 TempAlign);
7402 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7403 }
7404 DAG.setRoot(Chain);
7405 return;
7406 }
7407 case Intrinsic::reset_fpenv:
7408 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7409 return;
7410 case Intrinsic::get_fpmode:
7411 Res = DAG.getNode(
7412 ISD::GET_FPMODE, sdl,
7413 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7414 MVT::Other),
7415 DAG.getRoot());
7416 setValue(&I, Res);
7417 DAG.setRoot(Res.getValue(1));
7418 return;
7419 case Intrinsic::set_fpmode:
7420 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7421 getValue(I.getArgOperand(0)));
7422 DAG.setRoot(Res);
7423 return;
7424 case Intrinsic::reset_fpmode: {
7425 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7426 DAG.setRoot(Res);
7427 return;
7428 }
7429 case Intrinsic::pcmarker: {
7430 SDValue Tmp = getValue(I.getArgOperand(0));
7431 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7432 return;
7433 }
7434 case Intrinsic::readcyclecounter: {
7435 SDValue Op = getRoot();
7436 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7437 DAG.getVTList(MVT::i64, MVT::Other), Op);
7438 setValue(&I, Res);
7439 DAG.setRoot(Res.getValue(1));
7440 return;
7441 }
7442 case Intrinsic::readsteadycounter: {
7443 SDValue Op = getRoot();
7444 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7445 DAG.getVTList(MVT::i64, MVT::Other), Op);
7446 setValue(&I, Res);
7447 DAG.setRoot(Res.getValue(1));
7448 return;
7449 }
7450 case Intrinsic::bitreverse:
7451 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7452 getValue(I.getArgOperand(0)).getValueType(),
7453 getValue(I.getArgOperand(0))));
7454 return;
7455 case Intrinsic::bswap:
7456 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7457 getValue(I.getArgOperand(0)).getValueType(),
7458 getValue(I.getArgOperand(0))));
7459 return;
7460 case Intrinsic::cttz: {
7461 SDValue Arg = getValue(I.getArgOperand(0));
7462 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7463 EVT Ty = Arg.getValueType();
7464 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_POISON,
7465 sdl, Ty, Arg));
7466 return;
7467 }
7468 case Intrinsic::ctlz: {
7469 SDValue Arg = getValue(I.getArgOperand(0));
7470 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7471 EVT Ty = Arg.getValueType();
7472 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_POISON,
7473 sdl, Ty, Arg));
7474 return;
7475 }
7476 case Intrinsic::ctpop: {
7477 SDValue Arg = getValue(I.getArgOperand(0));
7478 EVT Ty = Arg.getValueType();
7479 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7480 return;
7481 }
7482 case Intrinsic::fshl:
7483 case Intrinsic::fshr: {
7484 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7485 SDValue X = getValue(I.getArgOperand(0));
7486 SDValue Y = getValue(I.getArgOperand(1));
7487 SDValue Z = getValue(I.getArgOperand(2));
7488 EVT VT = X.getValueType();
7489
7490 if (X == Y) {
7491 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7492 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7493 } else {
7494 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7495 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7496 }
7497 return;
7498 }
7499 case Intrinsic::clmul: {
7500 SDValue X = getValue(I.getArgOperand(0));
7501 SDValue Y = getValue(I.getArgOperand(1));
7502 setValue(&I, DAG.getNode(ISD::CLMUL, sdl, X.getValueType(), X, Y));
7503 return;
7504 }
7505 case Intrinsic::pext: {
7506 SDValue X = getValue(I.getArgOperand(0));
7507 SDValue Y = getValue(I.getArgOperand(1));
7508 setValue(&I, DAG.getNode(ISD::PEXT, sdl, X.getValueType(), X, Y));
7509 return;
7510 }
7511 case Intrinsic::pdep: {
7512 SDValue X = getValue(I.getArgOperand(0));
7513 SDValue Y = getValue(I.getArgOperand(1));
7514 setValue(&I, DAG.getNode(ISD::PDEP, sdl, X.getValueType(), X, Y));
7515 return;
7516 }
7517 case Intrinsic::sadd_sat: {
7518 SDValue Op1 = getValue(I.getArgOperand(0));
7519 SDValue Op2 = getValue(I.getArgOperand(1));
7520 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7521 return;
7522 }
7523 case Intrinsic::uadd_sat: {
7524 SDValue Op1 = getValue(I.getArgOperand(0));
7525 SDValue Op2 = getValue(I.getArgOperand(1));
7526 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7527 return;
7528 }
7529 case Intrinsic::ssub_sat: {
7530 SDValue Op1 = getValue(I.getArgOperand(0));
7531 SDValue Op2 = getValue(I.getArgOperand(1));
7532 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7533 return;
7534 }
7535 case Intrinsic::usub_sat: {
7536 SDValue Op1 = getValue(I.getArgOperand(0));
7537 SDValue Op2 = getValue(I.getArgOperand(1));
7538 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7539 return;
7540 }
7541 case Intrinsic::sshl_sat:
7542 case Intrinsic::ushl_sat: {
7543 SDValue Op1 = getValue(I.getArgOperand(0));
7544 SDValue Op2 = getValue(I.getArgOperand(1));
7545
7546 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
7547 Op1.getValueType(), DAG.getDataLayout());
7548
7549 // Coerce the shift amount to the right type if we can. This exposes the
7550 // truncate or zext to optimization early.
7551 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
7552 assert(ShiftTy.getSizeInBits() >=
7554 "Unexpected shift type");
7555 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
7556 }
7557
7558 unsigned Opc =
7559 Intrinsic == Intrinsic::sshl_sat ? ISD::SSHLSAT : ISD::USHLSAT;
7560 setValue(&I, DAG.getNode(Opc, sdl, Op1.getValueType(), Op1, Op2));
7561 return;
7562 }
7563 case Intrinsic::smul_fix:
7564 case Intrinsic::umul_fix:
7565 case Intrinsic::smul_fix_sat:
7566 case Intrinsic::umul_fix_sat: {
7567 SDValue Op1 = getValue(I.getArgOperand(0));
7568 SDValue Op2 = getValue(I.getArgOperand(1));
7569 SDValue Op3 = getValue(I.getArgOperand(2));
7570 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7571 Op1.getValueType(), Op1, Op2, Op3));
7572 return;
7573 }
7574 case Intrinsic::sdiv_fix:
7575 case Intrinsic::udiv_fix:
7576 case Intrinsic::sdiv_fix_sat:
7577 case Intrinsic::udiv_fix_sat: {
7578 SDValue Op1 = getValue(I.getArgOperand(0));
7579 SDValue Op2 = getValue(I.getArgOperand(1));
7580 SDValue Op3 = getValue(I.getArgOperand(2));
7582 Op1, Op2, Op3, DAG, TLI));
7583 return;
7584 }
7585 case Intrinsic::smax: {
7586 SDValue Op1 = getValue(I.getArgOperand(0));
7587 SDValue Op2 = getValue(I.getArgOperand(1));
7588 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7589 return;
7590 }
7591 case Intrinsic::smin: {
7592 SDValue Op1 = getValue(I.getArgOperand(0));
7593 SDValue Op2 = getValue(I.getArgOperand(1));
7594 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7595 return;
7596 }
7597 case Intrinsic::umax: {
7598 SDValue Op1 = getValue(I.getArgOperand(0));
7599 SDValue Op2 = getValue(I.getArgOperand(1));
7600 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7601 return;
7602 }
7603 case Intrinsic::umin: {
7604 SDValue Op1 = getValue(I.getArgOperand(0));
7605 SDValue Op2 = getValue(I.getArgOperand(1));
7606 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7607 return;
7608 }
7609 case Intrinsic::abs: {
7610 SDValue Op1 = getValue(I.getArgOperand(0));
7611 bool IntMinIsPoison = cast<ConstantInt>(I.getArgOperand(1))->isOne();
7612 unsigned Opc = IntMinIsPoison ? ISD::ABS_MIN_POISON : ISD::ABS;
7613 setValue(&I, DAG.getNode(Opc, sdl, Op1.getValueType(), Op1));
7614 return;
7615 }
7616 case Intrinsic::scmp: {
7617 SDValue Op1 = getValue(I.getArgOperand(0));
7618 SDValue Op2 = getValue(I.getArgOperand(1));
7619 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7620 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7621 break;
7622 }
7623 case Intrinsic::ucmp: {
7624 SDValue Op1 = getValue(I.getArgOperand(0));
7625 SDValue Op2 = getValue(I.getArgOperand(1));
7626 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7627 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7628 break;
7629 }
7630 case Intrinsic::stackaddress:
7631 case Intrinsic::stacksave: {
7632 unsigned SDOpcode = Intrinsic == Intrinsic::stackaddress ? ISD::STACKADDRESS
7634 SDValue Op = getRoot();
7635 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7636 Res = DAG.getNode(SDOpcode, sdl, DAG.getVTList(VT, MVT::Other), Op);
7637 setValue(&I, Res);
7638 DAG.setRoot(Res.getValue(1));
7639 return;
7640 }
7641 case Intrinsic::stackrestore:
7642 Res = getValue(I.getArgOperand(0));
7643 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7644 return;
7645 case Intrinsic::get_dynamic_area_offset: {
7646 SDValue Op = getRoot();
7647 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7648 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7649 Op);
7650 DAG.setRoot(Op);
7651 setValue(&I, Res);
7652 return;
7653 }
7654 case Intrinsic::stackguard: {
7655 MachineFunction &MF = DAG.getMachineFunction();
7656 const Module &M = *MF.getFunction().getParent();
7657 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7658 SDValue Chain = getRoot();
7659 if (TLI.useLoadStackGuardNode(M)) {
7660 Res = getLoadStackGuard(DAG, sdl, Chain);
7661 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7662 } else {
7663 const Value *Global = TLI.getSDagStackGuard(M, DAG.getLibcalls());
7664 if (!Global) {
7665 LLVMContext &Ctx = *DAG.getContext();
7666 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
7667 setValue(&I, DAG.getPOISON(PtrTy));
7668 return;
7669 }
7670
7671 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7672 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7673 MachinePointerInfo(Global, 0), Align,
7675 }
7676 // Mix the cookie with FP if enabled. Skip if using LOAD_STACK_GUARD
7677 // with post-RA mixing (AArch64 MSVCRT), as the mixing will be done during
7678 // post-RA expansion of LOAD_STACK_GUARD.
7679 if (TLI.useStackGuardMixFP() && !TLI.useLoadStackGuardNode(M))
7680 Res = TLI.emitStackGuardMixFP(DAG, Res, sdl);
7681 DAG.setRoot(Chain);
7682 setValue(&I, Res);
7683 return;
7684 }
7685 case Intrinsic::stackprotector: {
7686 // Emit code into the DAG to store the stack guard onto the stack.
7687 MachineFunction &MF = DAG.getMachineFunction();
7688 MachineFrameInfo &MFI = MF.getFrameInfo();
7689 const Module &M = *MF.getFunction().getParent();
7690 SDValue Src, Chain = getRoot();
7691
7692 if (TLI.useLoadStackGuardNode(M))
7693 Src = getLoadStackGuard(DAG, sdl, Chain);
7694 else
7695 Src = getValue(I.getArgOperand(0)); // The guard's value.
7696
7697 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7698
7699 int FI = FuncInfo.StaticAllocaMap[Slot];
7700 MFI.setStackProtectorIndex(FI);
7701 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7702
7703 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7704
7705 // Store the stack protector onto the stack.
7706 Res = DAG.getStore(
7707 Chain, sdl, Src, FIN,
7708 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7709 MaybeAlign(), MachineMemOperand::MOVolatile);
7710 setValue(&I, Res);
7711 DAG.setRoot(Res);
7712 return;
7713 }
7714 case Intrinsic::objectsize:
7715 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7716
7717 case Intrinsic::is_constant:
7718 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7719
7720 case Intrinsic::annotation:
7721 case Intrinsic::ptr_annotation:
7722 case Intrinsic::launder_invariant_group:
7723 case Intrinsic::strip_invariant_group:
7724 // Drop the intrinsic, but forward the value
7725 setValue(&I, getValue(I.getOperand(0)));
7726 return;
7727
7728 case Intrinsic::type_test:
7729 case Intrinsic::public_type_test:
7730 reportFatalUsageError("llvm.type.test intrinsic must be lowered by the "
7731 "LowerTypeTests pass before code generation");
7732 return;
7733
7734 case Intrinsic::assume:
7735 case Intrinsic::experimental_noalias_scope_decl:
7736 case Intrinsic::var_annotation:
7737 case Intrinsic::sideeffect:
7738 // Discard annotate attributes, noalias scope declarations, assumptions, and
7739 // artificial side-effects.
7740 return;
7741
7742 case Intrinsic::codeview_annotation: {
7743 // Emit a label associated with this metadata.
7744 MachineFunction &MF = DAG.getMachineFunction();
7745 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7746 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7747 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7748 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7749 DAG.setRoot(Res);
7750 return;
7751 }
7752
7753 case Intrinsic::init_trampoline: {
7754 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7755
7756 SDValue Ops[6];
7757 Ops[0] = getRoot();
7758 Ops[1] = getValue(I.getArgOperand(0));
7759 Ops[2] = getValue(I.getArgOperand(1));
7760 Ops[3] = getValue(I.getArgOperand(2));
7761 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7762 Ops[5] = DAG.getSrcValue(F);
7763
7764 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7765
7766 DAG.setRoot(Res);
7767 return;
7768 }
7769 case Intrinsic::adjust_trampoline:
7770 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7771 TLI.getPointerTy(DAG.getDataLayout()),
7772 getValue(I.getArgOperand(0))));
7773 return;
7774 case Intrinsic::gcroot: {
7775 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7776 "only valid in functions with gc specified, enforced by Verifier");
7777 assert(GFI && "implied by previous");
7778 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7779 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7780
7781 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7782 GFI->addStackRoot(FI->getIndex(), TypeMap);
7783 return;
7784 }
7785 case Intrinsic::gcread:
7786 case Intrinsic::gcwrite:
7787 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7788 case Intrinsic::get_rounding:
7789 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7790 setValue(&I, Res);
7791 DAG.setRoot(Res.getValue(1));
7792 return;
7793
7794 case Intrinsic::expect:
7795 case Intrinsic::expect_with_probability:
7796 // Just replace __builtin_expect(exp, c) and
7797 // __builtin_expect_with_probability(exp, c, p) with EXP.
7798 setValue(&I, getValue(I.getArgOperand(0)));
7799 return;
7800
7801 case Intrinsic::ubsantrap:
7802 case Intrinsic::debugtrap:
7803 case Intrinsic::trap: {
7804 StringRef TrapFuncName =
7805 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7806 if (TrapFuncName.empty()) {
7807 switch (Intrinsic) {
7808 case Intrinsic::trap:
7809 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7810 break;
7811 case Intrinsic::debugtrap:
7812 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7813 break;
7814 case Intrinsic::ubsantrap:
7815 DAG.setRoot(DAG.getNode(
7816 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7817 DAG.getTargetConstant(
7818 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7819 MVT::i32)));
7820 break;
7821 default: llvm_unreachable("unknown trap intrinsic");
7822 }
7823 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7824 I.hasFnAttr(Attribute::NoMerge));
7825 return;
7826 }
7828 if (Intrinsic == Intrinsic::ubsantrap) {
7829 Value *Arg = I.getArgOperand(0);
7830 Args.emplace_back(Arg, getValue(Arg));
7831 }
7832
7833 TargetLowering::CallLoweringInfo CLI(DAG);
7834 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7835 CallingConv::C, I.getType(),
7836 DAG.getExternalSymbol(TrapFuncName.data(),
7837 TLI.getPointerTy(DAG.getDataLayout())),
7838 std::move(Args));
7839 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7840 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7841 DAG.setRoot(Result.second);
7842 return;
7843 }
7844
7845 case Intrinsic::allow_runtime_check:
7846 case Intrinsic::allow_ubsan_check:
7847 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7848 return;
7849
7850 case Intrinsic::uadd_with_overflow:
7851 case Intrinsic::sadd_with_overflow:
7852 case Intrinsic::usub_with_overflow:
7853 case Intrinsic::ssub_with_overflow:
7854 case Intrinsic::umul_with_overflow:
7855 case Intrinsic::smul_with_overflow: {
7857 switch (Intrinsic) {
7858 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7859 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7860 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7861 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7862 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7863 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7864 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7865 }
7866 SDValue Op1 = getValue(I.getArgOperand(0));
7867 SDValue Op2 = getValue(I.getArgOperand(1));
7868
7869 EVT ResultVT = Op1.getValueType();
7870 EVT OverflowVT = ResultVT.changeElementType(*Context, MVT::i1);
7871
7872 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7873 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7874 return;
7875 }
7876 case Intrinsic::prefetch: {
7877 SDValue Ops[5];
7878 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7880 Ops[0] = DAG.getRoot();
7881 Ops[1] = getValue(I.getArgOperand(0));
7882 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7883 MVT::i32);
7884 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7885 MVT::i32);
7886 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7887 MVT::i32);
7888 SDValue Result = DAG.getMemIntrinsicNode(
7889 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7890 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7891 /* align */ std::nullopt, Flags);
7892
7893 // Chain the prefetch in parallel with any pending loads, to stay out of
7894 // the way of later optimizations.
7895 PendingLoads.push_back(Result);
7896 Result = getRoot();
7897 DAG.setRoot(Result);
7898 return;
7899 }
7900 case Intrinsic::lifetime_start:
7901 case Intrinsic::lifetime_end: {
7902 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7903 // Stack coloring is not enabled in O0, discard region information.
7904 if (TM.getOptLevel() == CodeGenOptLevel::None)
7905 return;
7906
7907 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7908 if (!LifetimeObject)
7909 return;
7910
7911 // First check that the Alloca is static, otherwise it won't have a
7912 // valid frame index.
7913 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7914 if (SI == FuncInfo.StaticAllocaMap.end())
7915 return;
7916
7917 const int FrameIndex = SI->second;
7918 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7919 DAG.setRoot(Res);
7920 return;
7921 }
7922 case Intrinsic::pseudoprobe: {
7923 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7924 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7925 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7926 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7927 DAG.setRoot(Res);
7928 return;
7929 }
7930 case Intrinsic::invariant_start:
7931 // Discard region information.
7932 setValue(&I,
7933 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7934 return;
7935 case Intrinsic::invariant_end:
7936 // Discard region information.
7937 return;
7938 case Intrinsic::clear_cache: {
7939 SDValue InputChain = DAG.getRoot();
7940 SDValue StartVal = getValue(I.getArgOperand(0));
7941 SDValue EndVal = getValue(I.getArgOperand(1));
7942 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7943 {InputChain, StartVal, EndVal});
7944 setValue(&I, Res);
7945 DAG.setRoot(Res);
7946 return;
7947 }
7948 case Intrinsic::donothing:
7949 case Intrinsic::seh_try_begin:
7950 case Intrinsic::seh_scope_begin:
7951 case Intrinsic::seh_try_end:
7952 case Intrinsic::seh_scope_end:
7953 // ignore
7954 return;
7955 case Intrinsic::experimental_stackmap:
7956 visitStackmap(I);
7957 return;
7958 case Intrinsic::experimental_patchpoint_void:
7959 case Intrinsic::experimental_patchpoint:
7960 visitPatchpoint(I);
7961 return;
7962 case Intrinsic::experimental_gc_statepoint:
7964 return;
7965 case Intrinsic::experimental_gc_result:
7966 visitGCResult(cast<GCResultInst>(I));
7967 return;
7968 case Intrinsic::experimental_gc_relocate:
7969 visitGCRelocate(cast<GCRelocateInst>(I));
7970 return;
7971 case Intrinsic::instrprof_cover:
7972 llvm_unreachable("instrprof failed to lower a cover");
7973 case Intrinsic::instrprof_increment:
7974 llvm_unreachable("instrprof failed to lower an increment");
7975 case Intrinsic::instrprof_timestamp:
7976 llvm_unreachable("instrprof failed to lower a timestamp");
7977 case Intrinsic::instrprof_value_profile:
7978 llvm_unreachable("instrprof failed to lower a value profiling call");
7979 case Intrinsic::instrprof_mcdc_parameters:
7980 llvm_unreachable("instrprof failed to lower mcdc parameters");
7981 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7982 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7983 case Intrinsic::localescape: {
7984 MachineFunction &MF = DAG.getMachineFunction();
7985 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7986
7987 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7988 // is the same on all targets.
7989 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7990 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7991 if (isa<ConstantPointerNull>(Arg))
7992 continue; // Skip null pointers. They represent a hole in index space.
7993 AllocaInst *Slot = cast<AllocaInst>(Arg);
7994 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7995 "can only escape static allocas");
7996 int FI = FuncInfo.StaticAllocaMap[Slot];
7997 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7999 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
8000 TII->get(TargetOpcode::LOCAL_ESCAPE))
8001 .addSym(FrameAllocSym)
8002 .addFrameIndex(FI);
8003 }
8004
8005 return;
8006 }
8007
8008 case Intrinsic::localrecover: {
8009 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
8010 MachineFunction &MF = DAG.getMachineFunction();
8011
8012 // Get the symbol that defines the frame offset.
8013 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
8014 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
8015 unsigned IdxVal =
8016 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
8017 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
8019
8020 Value *FP = I.getArgOperand(1);
8021 SDValue FPVal = getValue(FP);
8022 EVT PtrVT = FPVal.getValueType();
8023
8024 // Create a MCSymbol for the label to avoid any target lowering
8025 // that would make this PC relative.
8026 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
8027 SDValue OffsetVal =
8028 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
8029
8030 // Add the offset to the FP.
8031 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
8032 setValue(&I, Add);
8033
8034 return;
8035 }
8036
8037 case Intrinsic::fake_use: {
8038 Value *V = I.getArgOperand(0);
8039 SDValue Ops[2];
8040 // For Values not declared or previously used in this basic block, the
8041 // NodeMap will not have an entry, and `getValue` will assert if V has no
8042 // valid register value.
8043 auto FakeUseValue = [&]() -> SDValue {
8044 SDValue &N = NodeMap[V];
8045 if (N.getNode())
8046 return N;
8047
8048 // If there's a virtual register allocated and initialized for this
8049 // value, use it.
8050 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
8051 return copyFromReg;
8052 // FIXME: Do we want to preserve constants? It seems pointless.
8053 if (isa<Constant>(V))
8054 return getValue(V);
8055 return SDValue();
8056 }();
8057 if (!FakeUseValue || FakeUseValue.isUndef())
8058 return;
8059 Ops[0] = getRoot();
8060 Ops[1] = FakeUseValue;
8061 // Also, do not translate a fake use with an undef operand, or any other
8062 // empty SDValues.
8063 if (!Ops[1] || Ops[1].isUndef())
8064 return;
8065 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
8066 return;
8067 }
8068
8069 case Intrinsic::reloc_none: {
8070 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
8071 StringRef SymbolName = cast<MDString>(MD)->getString();
8072 SDValue Ops[2] = {
8073 getRoot(),
8074 DAG.getTargetExternalSymbol(
8075 SymbolName.data(), TLI.getProgramPointerTy(DAG.getDataLayout()))};
8076 DAG.setRoot(DAG.getNode(ISD::RELOC_NONE, sdl, MVT::Other, Ops));
8077 return;
8078 }
8079
8080 case Intrinsic::cond_loop: {
8081 SDValue InputChain = DAG.getRoot();
8082 SDValue P = getValue(I.getArgOperand(0));
8083 Res = DAG.getNode(ISD::COND_LOOP, sdl, DAG.getVTList(MVT::Other),
8084 {InputChain, P});
8085 setValue(&I, Res);
8086 DAG.setRoot(Res);
8087 return;
8088 }
8089
8090 case Intrinsic::eh_exceptionpointer:
8091 case Intrinsic::eh_exceptioncode: {
8092 // Get the exception pointer vreg, copy from it, and resize it to fit.
8093 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
8094 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
8095 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
8096 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
8097 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
8098 if (Intrinsic == Intrinsic::eh_exceptioncode)
8099 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
8100 setValue(&I, N);
8101 return;
8102 }
8103 case Intrinsic::xray_customevent: {
8104 // Here we want to make sure that the intrinsic behaves as if it has a
8105 // specific calling convention.
8106 const auto &Triple = DAG.getTarget().getTargetTriple();
8107 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64 &&
8108 Triple.getArch() != Triple::hexagon)
8109 return;
8110
8112
8113 // We want to say that we always want the arguments in registers.
8114 SDValue LogEntryVal = getValue(I.getArgOperand(0));
8115 SDValue StrSizeVal = getValue(I.getArgOperand(1));
8116 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
8117 SDValue Chain = getRoot();
8118 Ops.push_back(LogEntryVal);
8119 Ops.push_back(StrSizeVal);
8120 Ops.push_back(Chain);
8121
8122 // We need to enforce the calling convention for the callsite, so that
8123 // argument ordering is enforced correctly, and that register allocation can
8124 // see that some registers may be assumed clobbered and have to preserve
8125 // them across calls to the intrinsic.
8126 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
8127 sdl, NodeTys, Ops);
8128 SDValue patchableNode = SDValue(MN, 0);
8129 DAG.setRoot(patchableNode);
8130 setValue(&I, patchableNode);
8131 return;
8132 }
8133 case Intrinsic::xray_typedevent: {
8134 // Here we want to make sure that the intrinsic behaves as if it has a
8135 // specific calling convention.
8136 const auto &Triple = DAG.getTarget().getTargetTriple();
8137 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64 &&
8138 Triple.getArch() != Triple::hexagon)
8139 return;
8140
8142
8143 // We want to say that we always want the arguments in registers.
8144 // It's unclear to me how manipulating the selection DAG here forces callers
8145 // to provide arguments in registers instead of on the stack.
8146 SDValue LogTypeId = getValue(I.getArgOperand(0));
8147 SDValue LogEntryVal = getValue(I.getArgOperand(1));
8148 SDValue StrSizeVal = getValue(I.getArgOperand(2));
8149 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
8150 SDValue Chain = getRoot();
8151 Ops.push_back(LogTypeId);
8152 Ops.push_back(LogEntryVal);
8153 Ops.push_back(StrSizeVal);
8154 Ops.push_back(Chain);
8155
8156 // We need to enforce the calling convention for the callsite, so that
8157 // argument ordering is enforced correctly, and that register allocation can
8158 // see that some registers may be assumed clobbered and have to preserve
8159 // them across calls to the intrinsic.
8160 MachineSDNode *MN = DAG.getMachineNode(
8161 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
8162 SDValue patchableNode = SDValue(MN, 0);
8163 DAG.setRoot(patchableNode);
8164 setValue(&I, patchableNode);
8165 return;
8166 }
8167 case Intrinsic::experimental_deoptimize:
8169 return;
8170 case Intrinsic::stepvector:
8171 visitStepVector(I);
8172 return;
8173 case Intrinsic::vector_reduce_fadd:
8174 case Intrinsic::vector_reduce_fmul:
8175 case Intrinsic::vector_reduce_add:
8176 case Intrinsic::vector_reduce_mul:
8177 case Intrinsic::vector_reduce_and:
8178 case Intrinsic::vector_reduce_or:
8179 case Intrinsic::vector_reduce_xor:
8180 case Intrinsic::vector_reduce_smax:
8181 case Intrinsic::vector_reduce_smin:
8182 case Intrinsic::vector_reduce_umax:
8183 case Intrinsic::vector_reduce_umin:
8184 case Intrinsic::vector_reduce_fmax:
8185 case Intrinsic::vector_reduce_fmin:
8186 case Intrinsic::vector_reduce_fmaximum:
8187 case Intrinsic::vector_reduce_fminimum:
8188 visitVectorReduce(I, Intrinsic);
8189 return;
8190
8191 case Intrinsic::icall_branch_funnel: {
8193 Ops.push_back(getValue(I.getArgOperand(0)));
8194
8195 int64_t Offset;
8197 I.getArgOperand(1), Offset, DAG.getDataLayout()));
8198 if (!Base)
8200 "llvm.icall.branch.funnel operand must be a GlobalValue");
8201 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
8202
8203 struct BranchFunnelTarget {
8204 int64_t Offset;
8206 };
8208
8209 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
8211 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
8212 if (ElemBase != Base)
8213 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
8214 "to the same GlobalValue");
8215
8216 SDValue Val = getValue(I.getArgOperand(Op + 1));
8217 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
8218 if (!GA)
8220 "llvm.icall.branch.funnel operand must be a GlobalValue");
8221 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
8222 GA->getGlobal(), sdl, Val.getValueType(),
8223 GA->getOffset())});
8224 }
8225 llvm::sort(Targets,
8226 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
8227 return T1.Offset < T2.Offset;
8228 });
8229
8230 for (auto &T : Targets) {
8231 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
8232 Ops.push_back(T.Target);
8233 }
8234
8235 Ops.push_back(DAG.getRoot()); // Chain
8236 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
8237 MVT::Other, Ops),
8238 0);
8239 DAG.setRoot(N);
8240 setValue(&I, N);
8241 HasTailCall = true;
8242 return;
8243 }
8244
8245 case Intrinsic::wasm_landingpad_index:
8246 // Information this intrinsic contained has been transferred to
8247 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
8248 // delete it now.
8249 return;
8250
8251 case Intrinsic::aarch64_settag:
8252 case Intrinsic::aarch64_settag_zero: {
8253 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8254 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
8256 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
8257 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
8258 ZeroMemory);
8259 DAG.setRoot(Val);
8260 setValue(&I, Val);
8261 return;
8262 }
8263 case Intrinsic::amdgcn_cs_chain: {
8264 // At this point we don't care if it's amdgpu_cs_chain or
8265 // amdgpu_cs_chain_preserve.
8267
8268 Type *RetTy = I.getType();
8269 assert(RetTy->isVoidTy() && "Should not return");
8270
8271 SDValue Callee = getValue(I.getOperand(0));
8272
8273 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
8274 // We'll also tack the value of the EXEC mask at the end.
8276 Args.reserve(3);
8277
8278 for (unsigned Idx : {2, 3, 1}) {
8279 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8280 I.getOperand(Idx)->getType());
8281 Arg.setAttributes(&I, Idx);
8282 Args.push_back(Arg);
8283 }
8284
8285 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
8286 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
8287 Args[2].IsInReg = true; // EXEC should be inreg
8288
8289 // Forward the flags and any additional arguments.
8290 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
8291 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8292 I.getOperand(Idx)->getType());
8293 Arg.setAttributes(&I, Idx);
8294 Args.push_back(Arg);
8295 }
8296
8297 TargetLowering::CallLoweringInfo CLI(DAG);
8298 CLI.setDebugLoc(getCurSDLoc())
8299 .setChain(getRoot())
8300 .setCallee(CC, RetTy, Callee, std::move(Args))
8301 .setNoReturn(true)
8302 .setTailCall(true)
8303 .setConvergent(I.isConvergent());
8304 CLI.CB = &I;
8305 std::pair<SDValue, SDValue> Result =
8306 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
8307 (void)Result;
8308 assert(!Result.first.getNode() && !Result.second.getNode() &&
8309 "Should've lowered as tail call");
8310
8311 HasTailCall = true;
8312 return;
8313 }
8314 case Intrinsic::amdgcn_call_whole_wave: {
8316 bool isTailCall = I.isTailCall();
8317
8318 // The first argument is the callee. Skip it when assembling the call args.
8319 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
8320 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
8321 I.getArgOperand(Idx)->getType());
8322 Arg.setAttributes(&I, Idx);
8323
8324 // If we have an explicit sret argument that is an Instruction, (i.e., it
8325 // might point to function-local memory), we can't meaningfully tail-call.
8326 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
8327 isTailCall = false;
8328
8329 Args.push_back(Arg);
8330 }
8331
8332 SDValue ConvControlToken;
8333 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8334 auto *Token = Bundle->Inputs[0].get();
8335 ConvControlToken = getValue(Token);
8336 }
8337
8338 TargetLowering::CallLoweringInfo CLI(DAG);
8339 CLI.setDebugLoc(getCurSDLoc())
8340 .setChain(getRoot())
8341 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8342 getValue(I.getArgOperand(0)), std::move(Args))
8343 .setTailCall(isTailCall && canTailCall(I))
8344 .setIsPreallocated(
8345 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8346 .setConvergent(I.isConvergent())
8347 .setConvergenceControlToken(ConvControlToken);
8348 CLI.CB = &I;
8349
8350 std::pair<SDValue, SDValue> Result =
8351 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8352
8353 if (Result.first.getNode())
8354 setValue(&I, Result.first);
8355 return;
8356 }
8357 case Intrinsic::ptrmask: {
8358 SDValue Ptr = getValue(I.getOperand(0));
8359 SDValue Mask = getValue(I.getOperand(1));
8360
8361 // On arm64_32, pointers are 32 bits when stored in memory, but
8362 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8363 // match the index type, but the pointer is 64 bits, so the mask must be
8364 // zero-extended up to 64 bits to match the pointer.
8365 EVT PtrVT =
8366 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8367 EVT MemVT =
8368 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8369 assert(PtrVT == Ptr.getValueType());
8370 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8371 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8372 // 128-bit, so we have to pad the mask with ones for unused bits.
8373 auto HighOnes = DAG.getNode(
8374 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8375 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8376 PtrVT, sdl));
8377 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8378 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8379 } else if (Mask.getValueType() != PtrVT)
8380 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8381
8382 assert(Mask.getValueType() == PtrVT);
8383 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8384 return;
8385 }
8386 case Intrinsic::threadlocal_address: {
8387 setValue(&I, getValue(I.getOperand(0)));
8388 return;
8389 }
8390 case Intrinsic::get_active_lane_mask: {
8391 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8392 SDValue Index = getValue(I.getOperand(0));
8393 SDValue TripCount = getValue(I.getOperand(1));
8394 EVT ElementVT = Index.getValueType();
8395
8396 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8397 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8398 TripCount));
8399 return;
8400 }
8401
8402 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8403 CCVT.getVectorElementCount());
8404
8405 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8406 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8407 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8408 SDValue VectorInduction = DAG.getNode(
8409 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8410 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8411 VectorTripCount, ISD::CondCode::SETULT);
8412 setValue(&I, SetCC);
8413 return;
8414 }
8415 case Intrinsic::experimental_get_vector_length: {
8416 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8417 "Expected positive VF");
8418 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8419 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8420
8421 SDValue Count = getValue(I.getOperand(0));
8422 EVT CountVT = Count.getValueType();
8423
8424 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8425 visitTargetIntrinsic(I, Intrinsic);
8426 return;
8427 }
8428
8429 // Expand to a umin between the trip count and the maximum elements the type
8430 // can hold.
8431 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8432
8433 // Extend the trip count to at least the result VT.
8434 if (CountVT.bitsLT(VT)) {
8435 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8436 CountVT = VT;
8437 }
8438
8439 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8440 ElementCount::get(VF, IsScalable));
8441
8442 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8443 // Clip to the result type if needed.
8444 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8445
8446 setValue(&I, Trunc);
8447 return;
8448 }
8449 case Intrinsic::vector_partial_reduce_add: {
8450 SDValue Acc = getValue(I.getOperand(0));
8451 SDValue Input = getValue(I.getOperand(1));
8452 setValue(&I,
8453 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8454 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8455 return;
8456 }
8457 case Intrinsic::vector_partial_reduce_fadd: {
8458 SDValue Acc = getValue(I.getOperand(0));
8459 SDValue Input = getValue(I.getOperand(1));
8460 setValue(&I, DAG.getNode(
8461 ISD::PARTIAL_REDUCE_FMLA, sdl, Acc.getValueType(), Acc,
8462 Input, DAG.getConstantFP(1.0, sdl, Input.getValueType())));
8463 return;
8464 }
8465 case Intrinsic::experimental_cttz_elts: {
8466 SDValue Op = getValue(I.getOperand(0));
8467 EVT OpVT = Op.getValueType();
8468 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8469 bool ZeroIsPoison =
8470 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8471 if (OpVT.getVectorElementType() != MVT::i1) {
8472 // Compare the input vector elements to zero & use to count trailing
8473 // zeros.
8474 SDValue AllZero = DAG.getConstant(0, sdl, OpVT);
8475 EVT I1OpVT = OpVT.changeVectorElementType(*DAG.getContext(), MVT::i1);
8476 Op = DAG.getSetCC(sdl, I1OpVT, Op, AllZero, ISD::SETNE);
8477 }
8478 setValue(&I, DAG.getNode(ZeroIsPoison ? ISD::CTTZ_ELTS_ZERO_POISON
8480 sdl, RetTy, Op));
8481 return;
8482 }
8483 case Intrinsic::vector_insert: {
8484 SDValue Vec = getValue(I.getOperand(0));
8485 SDValue SubVec = getValue(I.getOperand(1));
8486 SDValue Index = getValue(I.getOperand(2));
8487
8488 // The intrinsic's index type is i64, but the SDNode requires an index type
8489 // suitable for the target. Convert the index as required.
8490 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8491 if (Index.getValueType() != VectorIdxTy)
8492 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8493
8494 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8495 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8496 Index));
8497 return;
8498 }
8499 case Intrinsic::vector_extract: {
8500 SDValue Vec = getValue(I.getOperand(0));
8501 SDValue Index = getValue(I.getOperand(1));
8502 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8503
8504 // The intrinsic's index type is i64, but the SDNode requires an index type
8505 // suitable for the target. Convert the index as required.
8506 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8507 if (Index.getValueType() != VectorIdxTy)
8508 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8509
8510 setValue(&I,
8511 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8512 return;
8513 }
8514 case Intrinsic::experimental_vector_match: {
8515 SDValue Op1 = getValue(I.getOperand(0));
8516 SDValue Op2 = getValue(I.getOperand(1));
8517 SDValue Mask = getValue(I.getOperand(2));
8518 EVT Op1VT = Op1.getValueType();
8519 EVT Op2VT = Op2.getValueType();
8520 EVT ResVT = Mask.getValueType();
8521 unsigned SearchSize = Op2VT.getVectorNumElements();
8522
8523 // If the target has native support for this vector match operation, lower
8524 // the intrinsic untouched; otherwise, expand it below.
8525 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8526 visitTargetIntrinsic(I, Intrinsic);
8527 return;
8528 }
8529
8530 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8531
8532 for (unsigned i = 0; i < SearchSize; ++i) {
8533 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8534 Op2VT.getVectorElementType(), Op2,
8535 DAG.getVectorIdxConstant(i, sdl));
8536 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8537 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8538 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8539 }
8540
8541 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8542 return;
8543 }
8544 case Intrinsic::vector_reverse:
8545 visitVectorReverse(I);
8546 return;
8547 case Intrinsic::vector_splice_left:
8548 case Intrinsic::vector_splice_right:
8549 visitVectorSplice(I);
8550 return;
8551 case Intrinsic::callbr_landingpad:
8552 visitCallBrLandingPad(I);
8553 return;
8554 case Intrinsic::vector_interleave2:
8555 visitVectorInterleave(I, 2);
8556 return;
8557 case Intrinsic::vector_interleave3:
8558 visitVectorInterleave(I, 3);
8559 return;
8560 case Intrinsic::vector_interleave4:
8561 visitVectorInterleave(I, 4);
8562 return;
8563 case Intrinsic::vector_interleave5:
8564 visitVectorInterleave(I, 5);
8565 return;
8566 case Intrinsic::vector_interleave6:
8567 visitVectorInterleave(I, 6);
8568 return;
8569 case Intrinsic::vector_interleave7:
8570 visitVectorInterleave(I, 7);
8571 return;
8572 case Intrinsic::vector_interleave8:
8573 visitVectorInterleave(I, 8);
8574 return;
8575 case Intrinsic::vector_deinterleave2:
8576 visitVectorDeinterleave(I, 2);
8577 return;
8578 case Intrinsic::vector_deinterleave3:
8579 visitVectorDeinterleave(I, 3);
8580 return;
8581 case Intrinsic::vector_deinterleave4:
8582 visitVectorDeinterleave(I, 4);
8583 return;
8584 case Intrinsic::vector_deinterleave5:
8585 visitVectorDeinterleave(I, 5);
8586 return;
8587 case Intrinsic::vector_deinterleave6:
8588 visitVectorDeinterleave(I, 6);
8589 return;
8590 case Intrinsic::vector_deinterleave7:
8591 visitVectorDeinterleave(I, 7);
8592 return;
8593 case Intrinsic::vector_deinterleave8:
8594 visitVectorDeinterleave(I, 8);
8595 return;
8596 case Intrinsic::experimental_vector_compress:
8597 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8598 getValue(I.getArgOperand(0)).getValueType(),
8599 getValue(I.getArgOperand(0)),
8600 getValue(I.getArgOperand(1)),
8601 getValue(I.getArgOperand(2)), Flags));
8602 return;
8603 case Intrinsic::experimental_convergence_anchor:
8604 case Intrinsic::experimental_convergence_entry:
8605 case Intrinsic::experimental_convergence_loop:
8606 visitConvergenceControl(I, Intrinsic);
8607 return;
8608 case Intrinsic::experimental_vector_histogram_add: {
8609 visitVectorHistogram(I, Intrinsic);
8610 return;
8611 }
8612 case Intrinsic::experimental_vector_extract_last_active: {
8613 visitVectorExtractLastActive(I, Intrinsic);
8614 return;
8615 }
8616 case Intrinsic::loop_dependence_war_mask:
8617 setValue(&I,
8619 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8620 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8621 DAG.getConstant(0, sdl, MVT::i64)));
8622 return;
8623 case Intrinsic::loop_dependence_raw_mask:
8624 setValue(&I,
8626 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8627 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8628 DAG.getConstant(0, sdl, MVT::i64)));
8629 return;
8630 case Intrinsic::masked_udiv:
8631 setValue(&I,
8632 DAG.getNode(ISD::MASKED_UDIV, sdl, EVT::getEVT(I.getType()),
8633 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8634 getValue(I.getOperand(2))));
8635 return;
8636 case Intrinsic::masked_sdiv:
8637 setValue(&I,
8638 DAG.getNode(ISD::MASKED_SDIV, sdl, EVT::getEVT(I.getType()),
8639 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8640 getValue(I.getOperand(2))));
8641 return;
8642 case Intrinsic::masked_urem:
8643 setValue(&I,
8644 DAG.getNode(ISD::MASKED_UREM, sdl, EVT::getEVT(I.getType()),
8645 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8646 getValue(I.getOperand(2))));
8647 return;
8648 case Intrinsic::masked_srem:
8649 setValue(&I,
8650 DAG.getNode(ISD::MASKED_SREM, sdl, EVT::getEVT(I.getType()),
8651 getValue(I.getOperand(0)), getValue(I.getOperand(1)),
8652 getValue(I.getOperand(2))));
8653 return;
8654 }
8655}
8656
8657void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
8659 assert(Result.getNode()->getNumValues() == 2);
8660 SDValue OutChain = Result.getValue(1);
8661 assert(OutChain.getValueType() == MVT::Other);
8662
8663 // Instead of updating the root immediately, push the produced chain to the
8664 // appropriate list, deferring the update until the root is requested. In this
8665 // case, the nodes from the lists are chained using TokenFactor, indicating
8666 // that the operations are independent.
8667 //
8668 // In particular, the root is updated before any call that might access the
8669 // floating-point environment, except for constrained intrinsics.
8670 switch (EB) {
8673 PendingConstrainedFP.push_back(OutChain);
8674 break;
8676 PendingConstrainedFPStrict.push_back(OutChain);
8677 break;
8678 }
8679}
8680
8681void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8682 const ConstrainedFPIntrinsic &FPI) {
8683 SDLoc sdl = getCurSDLoc();
8684
8685 // We do not need to serialize constrained FP intrinsics against
8686 // each other or against (nonvolatile) loads, so they can be
8687 // chained like loads.
8689 SDValue Chain = getFPOperationRoot(EB);
8691 Opers.push_back(Chain);
8692 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8693 Opers.push_back(getValue(FPI.getArgOperand(I)));
8694
8695 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8696 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8697 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8698
8699 SDNodeFlags Flags;
8701 Flags.setNoFPExcept(true);
8702
8703 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8704 Flags.copyFMF(*FPOp);
8705
8706 unsigned Opcode;
8707 switch (FPI.getIntrinsicID()) {
8708 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8709#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8710 case Intrinsic::INTRINSIC: \
8711 Opcode = ISD::STRICT_##DAGN; \
8712 break;
8713#include "llvm/IR/ConstrainedOps.def"
8714 case Intrinsic::experimental_constrained_fmuladd: {
8715 Opcode = ISD::STRICT_FMA;
8716 // Break fmuladd into fmul and fadd.
8717 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8718 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8719 Opers.pop_back();
8720 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8721 pushFPOpOutChain(Mul, EB);
8722 Opcode = ISD::STRICT_FADD;
8723 Opers.clear();
8724 Opers.push_back(Mul.getValue(1));
8725 Opers.push_back(Mul.getValue(0));
8726 Opers.push_back(getValue(FPI.getArgOperand(2)));
8727 }
8728 break;
8729 }
8730 }
8731
8732 // A few strict DAG nodes carry additional operands that are not
8733 // set up by the default code above.
8734 switch (Opcode) {
8735 default: break;
8737 Opers.push_back(
8738 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8739 break;
8740 case ISD::STRICT_FSETCC:
8741 case ISD::STRICT_FSETCCS: {
8742 auto *FPCmp = dyn_cast<ConstrainedFPCmpIntrinsic>(&FPI);
8743 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8744 if (DAG.isKnownNeverNaN(Opers[1]) && DAG.isKnownNeverNaN(Opers[2]))
8745 Condition = getFCmpCodeWithoutNaN(Condition);
8746 Opers.push_back(DAG.getCondCode(Condition));
8747 break;
8748 }
8749 }
8750
8751 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8752 pushFPOpOutChain(Result, EB);
8753
8754 SDValue FPResult = Result.getValue(0);
8755 setValue(&FPI, FPResult);
8756}
8757
8758static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8759 std::optional<unsigned> ResOPC;
8760 switch (VPIntrin.getIntrinsicID()) {
8761 case Intrinsic::vp_ctlz: {
8762 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8763 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_POISON : ISD::VP_CTLZ;
8764 break;
8765 }
8766 case Intrinsic::vp_cttz: {
8767 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8768 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_POISON : ISD::VP_CTTZ;
8769 break;
8770 }
8771 case Intrinsic::vp_cttz_elts: {
8772 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8773 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_POISON : ISD::VP_CTTZ_ELTS;
8774 break;
8775 }
8776#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8777 case Intrinsic::VPID: \
8778 ResOPC = ISD::VPSD; \
8779 break;
8780#include "llvm/IR/VPIntrinsics.def"
8781 }
8782
8783 if (!ResOPC)
8785 "Inconsistency: no SDNode available for this VPIntrinsic!");
8786
8787 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8788 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8789 if (VPIntrin.getFastMathFlags().allowReassoc())
8790 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8791 : ISD::VP_REDUCE_FMUL;
8792 }
8793
8794 return *ResOPC;
8795}
8796
8797void SelectionDAGBuilder::visitVPLoad(
8798 const VPIntrinsic &VPIntrin, EVT VT,
8799 const SmallVectorImpl<SDValue> &OpValues) {
8800 SDLoc DL = getCurSDLoc();
8801 Value *PtrOperand = VPIntrin.getArgOperand(0);
8802 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8803 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8804 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8805 SDValue LD;
8806 // Do not serialize variable-length loads of constant memory with
8807 // anything.
8808 if (!Alignment)
8809 Alignment = DAG.getEVTAlign(VT);
8810 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8811 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8812 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8813 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8814 MachineMemOperand::Flags MMOFlags =
8815 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8816 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8817 MachinePointerInfo(PtrOperand), MMOFlags,
8818 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8819 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8820 MMO, false /*IsExpanding */);
8821 if (AddToChain)
8822 PendingLoads.push_back(LD.getValue(1));
8823 setValue(&VPIntrin, LD);
8824}
8825
8826void SelectionDAGBuilder::visitVPLoadFF(
8827 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8828 const SmallVectorImpl<SDValue> &OpValues) {
8829 assert(OpValues.size() == 3 && "Unexpected number of operands");
8830 SDLoc DL = getCurSDLoc();
8831 Value *PtrOperand = VPIntrin.getArgOperand(0);
8832 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8833 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8834 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8835 SDValue LD;
8836 // Do not serialize variable-length loads of constant memory with
8837 // anything.
8838 if (!Alignment)
8839 Alignment = DAG.getEVTAlign(VT);
8840 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8841 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8842 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8843 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8844 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8845 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8846 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8847 MMO);
8848 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8849 if (AddToChain)
8850 PendingLoads.push_back(LD.getValue(2));
8851 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8852}
8853
8854void SelectionDAGBuilder::visitVPGather(
8855 const VPIntrinsic &VPIntrin, EVT VT,
8856 const SmallVectorImpl<SDValue> &OpValues) {
8857 SDLoc DL = getCurSDLoc();
8858 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8859 Value *PtrOperand = VPIntrin.getArgOperand(0);
8860 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8861 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8862 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8863 SDValue LD;
8864 if (!Alignment)
8865 Alignment = DAG.getEVTAlign(VT.getScalarType());
8866 unsigned AS =
8867 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8868 MachineMemOperand::Flags MMOFlags =
8869 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8870 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8871 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8872 *Alignment, AAInfo, Ranges);
8873 SDValue Base, Index, Scale;
8874 bool UniformBase =
8875 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8876 VT.getScalarStoreSize());
8877 if (!UniformBase) {
8878 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8879 Index = getValue(PtrOperand);
8880 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8881 }
8882 EVT IdxVT = Index.getValueType();
8883 EVT EltTy = IdxVT.getVectorElementType();
8884 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8885 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8886 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8887 }
8888 LD = DAG.getGatherVP(
8889 DAG.getVTList(VT, MVT::Other), VT, DL,
8890 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8892 PendingLoads.push_back(LD.getValue(1));
8893 setValue(&VPIntrin, LD);
8894}
8895
8896void SelectionDAGBuilder::visitVPStore(
8897 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8898 SDLoc DL = getCurSDLoc();
8899 Value *PtrOperand = VPIntrin.getArgOperand(1);
8900 EVT VT = OpValues[0].getValueType();
8901 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8902 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8903 SDValue ST;
8904 if (!Alignment)
8905 Alignment = DAG.getEVTAlign(VT);
8906 SDValue Ptr = OpValues[1];
8907 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8908 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8909 MachineMemOperand::Flags MMOFlags =
8910 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8911 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8912 MachinePointerInfo(PtrOperand), MMOFlags,
8913 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8914 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8915 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8916 /* IsTruncating */ false, /*IsCompressing*/ false);
8917 DAG.setRoot(ST);
8918 setValue(&VPIntrin, ST);
8919}
8920
8921void SelectionDAGBuilder::visitVPScatter(
8922 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8923 SDLoc DL = getCurSDLoc();
8924 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8925 Value *PtrOperand = VPIntrin.getArgOperand(1);
8926 EVT VT = OpValues[0].getValueType();
8927 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8928 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8929 SDValue ST;
8930 if (!Alignment)
8931 Alignment = DAG.getEVTAlign(VT.getScalarType());
8932 unsigned AS =
8933 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8934 MachineMemOperand::Flags MMOFlags =
8935 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8936 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8937 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8938 *Alignment, AAInfo);
8939 SDValue Base, Index, Scale;
8940 bool UniformBase =
8941 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8942 VT.getScalarStoreSize());
8943 if (!UniformBase) {
8944 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8945 Index = getValue(PtrOperand);
8946 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8947 }
8948 EVT IdxVT = Index.getValueType();
8949 EVT EltTy = IdxVT.getVectorElementType();
8950 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8951 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8952 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8953 }
8954 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8955 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8956 OpValues[2], OpValues[3]},
8957 MMO, ISD::SIGNED_SCALED);
8958 DAG.setRoot(ST);
8959 setValue(&VPIntrin, ST);
8960}
8961
8962void SelectionDAGBuilder::visitVPStridedLoad(
8963 const VPIntrinsic &VPIntrin, EVT VT,
8964 const SmallVectorImpl<SDValue> &OpValues) {
8965 SDLoc DL = getCurSDLoc();
8966 Value *PtrOperand = VPIntrin.getArgOperand(0);
8967 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8968 if (!Alignment)
8969 Alignment = DAG.getEVTAlign(VT.getScalarType());
8970 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8971 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8972 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8973 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8974 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8975 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8976 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8977 MachineMemOperand::Flags MMOFlags =
8978 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8979 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8980 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8981 *Alignment, AAInfo, Ranges);
8982
8983 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8984 OpValues[2], OpValues[3], MMO,
8985 false /*IsExpanding*/);
8986
8987 if (AddToChain)
8988 PendingLoads.push_back(LD.getValue(1));
8989 setValue(&VPIntrin, LD);
8990}
8991
8992void SelectionDAGBuilder::visitVPStridedStore(
8993 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8994 SDLoc DL = getCurSDLoc();
8995 Value *PtrOperand = VPIntrin.getArgOperand(1);
8996 EVT VT = OpValues[0].getValueType();
8997 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8998 if (!Alignment)
8999 Alignment = DAG.getEVTAlign(VT.getScalarType());
9000 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
9001 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
9002 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9003 MachineMemOperand::Flags MMOFlags =
9004 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
9005 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
9006 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
9007 *Alignment, AAInfo);
9008
9009 SDValue ST = DAG.getStridedStoreVP(
9010 getMemoryRoot(), DL, OpValues[0], OpValues[1],
9011 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
9012 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
9013 /*IsCompressing*/ false);
9014
9015 DAG.setRoot(ST);
9016 setValue(&VPIntrin, ST);
9017}
9018
9019void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
9020 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9021 SDLoc DL = getCurSDLoc();
9022
9023 ISD::CondCode Condition;
9025
9026 Value *Op1 = VPIntrin.getOperand(0);
9027 Value *Op2 = VPIntrin.getOperand(1);
9028 // #2 is the condition code
9029 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
9030 SDValue EVL = getValue(VPIntrin.getOperand(4));
9031 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
9032 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
9033 "Unexpected target EVL type");
9034 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
9035
9036 if (VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy()) {
9037 Condition = getFCmpCondCode(CondCode);
9038 SimplifyQuery SQ(DAG.getDataLayout(), &VPIntrin);
9039 if (isKnownNeverNaN(Op2, SQ) && isKnownNeverNaN(Op1, SQ))
9040 Condition = getFCmpCodeWithoutNaN(Condition);
9041 } else {
9042 Condition = getICmpCondCode(CondCode);
9043 }
9044
9045 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9046 VPIntrin.getType());
9047 setValue(&VPIntrin, DAG.getSetCCVP(DL, DestVT, getValue(Op1), getValue(Op2),
9048 Condition, MaskOp, EVL));
9049}
9050
9051void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
9052 const VPIntrinsic &VPIntrin) {
9053 SDLoc DL = getCurSDLoc();
9054 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
9055
9056 auto IID = VPIntrin.getIntrinsicID();
9057
9058 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
9059 return visitVPCmp(*CmpI);
9060
9061 SmallVector<EVT, 4> ValueVTs;
9062 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9063 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
9064 SDVTList VTs = DAG.getVTList(ValueVTs);
9065
9066 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
9067
9068 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
9069 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
9070 "Unexpected target EVL type");
9071
9072 // Request operands.
9073 SmallVector<SDValue, 7> OpValues;
9074 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
9075 auto Op = getValue(VPIntrin.getArgOperand(I));
9076 if (I == EVLParamPos)
9077 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
9078 OpValues.push_back(Op);
9079 }
9080
9081 switch (Opcode) {
9082 default: {
9083 SDNodeFlags SDFlags;
9084 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
9085 SDFlags.copyFMF(*FPMO);
9086 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
9087 setValue(&VPIntrin, Result);
9088 break;
9089 }
9090 case ISD::VP_LOAD:
9091 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
9092 break;
9093 case ISD::VP_LOAD_FF:
9094 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
9095 break;
9096 case ISD::VP_GATHER:
9097 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
9098 break;
9099 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
9100 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
9101 break;
9102 case ISD::VP_STORE:
9103 visitVPStore(VPIntrin, OpValues);
9104 break;
9105 case ISD::VP_SCATTER:
9106 visitVPScatter(VPIntrin, OpValues);
9107 break;
9108 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
9109 visitVPStridedStore(VPIntrin, OpValues);
9110 break;
9111 case ISD::VP_FMULADD: {
9112 assert(OpValues.size() == 5 && "Unexpected number of operands");
9113 SDNodeFlags SDFlags;
9114 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
9115 SDFlags.copyFMF(*FPMO);
9116 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
9117 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
9118 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
9119 } else {
9120 SDValue Mul = DAG.getNode(
9121 ISD::VP_FMUL, DL, VTs,
9122 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
9123 SDValue Add =
9124 DAG.getNode(ISD::VP_FADD, DL, VTs,
9125 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
9126 setValue(&VPIntrin, Add);
9127 }
9128 break;
9129 }
9130 case ISD::VP_IS_FPCLASS: {
9131 const DataLayout DLayout = DAG.getDataLayout();
9132 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
9133 auto Constant = OpValues[1]->getAsZExtVal();
9134 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
9135 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
9136 {OpValues[0], Check, OpValues[2], OpValues[3]});
9137 setValue(&VPIntrin, V);
9138 return;
9139 }
9140 case ISD::VP_INTTOPTR: {
9141 SDValue N = OpValues[0];
9142 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
9143 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
9144 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
9145 OpValues[2]);
9146 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
9147 OpValues[2]);
9148 setValue(&VPIntrin, N);
9149 break;
9150 }
9151 case ISD::VP_PTRTOINT: {
9152 SDValue N = OpValues[0];
9153 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9154 VPIntrin.getType());
9155 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
9156 VPIntrin.getOperand(0)->getType());
9157 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
9158 OpValues[2]);
9159 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
9160 OpValues[2]);
9161 setValue(&VPIntrin, N);
9162 break;
9163 }
9164 case ISD::VP_ABS:
9165 case ISD::VP_CTLZ:
9166 case ISD::VP_CTLZ_ZERO_POISON:
9167 case ISD::VP_CTTZ:
9168 case ISD::VP_CTTZ_ZERO_POISON:
9169 case ISD::VP_CTTZ_ELTS_ZERO_POISON:
9170 case ISD::VP_CTTZ_ELTS: {
9171 SDValue Result =
9172 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
9173 setValue(&VPIntrin, Result);
9174 break;
9175 }
9176 }
9177}
9178
9180 const BasicBlock *EHPadBB,
9181 MCSymbol *&BeginLabel) {
9182 MachineFunction &MF = DAG.getMachineFunction();
9183
9184 // Insert a label before the invoke call to mark the try range. This can be
9185 // used to detect deletion of the invoke via the MachineModuleInfo.
9186 BeginLabel = MF.getContext().createTempSymbol();
9187
9188 // For SjLj, keep track of which landing pads go with which invokes
9189 // so as to maintain the ordering of pads in the LSDA.
9190 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
9191 if (CallSiteIndex) {
9192 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
9193 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
9194
9195 // Now that the call site is handled, stop tracking it.
9196 FuncInfo.setCurrentCallSite(0);
9197 }
9198
9199 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
9200}
9201
9202SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
9203 const BasicBlock *EHPadBB,
9204 MCSymbol *BeginLabel) {
9205 assert(BeginLabel && "BeginLabel should've been set");
9206
9208
9209 // Insert a label at the end of the invoke call to mark the try range. This
9210 // can be used to detect deletion of the invoke via the MachineModuleInfo.
9211 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
9212 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
9213
9214 // Inform MachineModuleInfo of range.
9216 // There is a platform (e.g. wasm) that uses funclet style IR but does not
9217 // actually use outlined funclets and their LSDA info style.
9218 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
9219 assert(II && "II should've been set");
9220 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
9221 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
9222 } else if (!isScopedEHPersonality(Pers)) {
9223 assert(EHPadBB);
9224 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
9225 }
9226
9227 return Chain;
9228}
9229
9230std::pair<SDValue, SDValue>
9232 const BasicBlock *EHPadBB) {
9233 MCSymbol *BeginLabel = nullptr;
9234
9235 if (EHPadBB) {
9236 // Both PendingLoads and PendingExports must be flushed here;
9237 // this call might not return.
9238 (void)getRoot();
9239 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
9240 CLI.setChain(getRoot());
9241 }
9242
9243 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9244 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
9245
9246 assert((CLI.IsTailCall || Result.second.getNode()) &&
9247 "Non-null chain expected with non-tail call!");
9248 assert((Result.second.getNode() || !Result.first.getNode()) &&
9249 "Null value expected with tail call!");
9250
9251 if (!Result.second.getNode()) {
9252 // As a special case, a null chain means that a tail call has been emitted
9253 // and the DAG root is already updated.
9254 HasTailCall = true;
9255
9256 // Since there's no actual continuation from this block, nothing can be
9257 // relying on us setting vregs for them.
9258 PendingExports.clear();
9259 } else {
9260 DAG.setRoot(Result.second);
9261 }
9262
9263 if (EHPadBB) {
9264 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
9265 BeginLabel));
9266 Result.second = getRoot();
9267 }
9268
9269 return Result;
9270}
9271
9273 bool isMustTailCall = CB.isMustTailCall();
9274
9275 // Avoid emitting tail calls in functions with the disable-tail-calls
9276 // attribute.
9277 const Function *Caller = CB.getParent()->getParent();
9278 if (!isMustTailCall &&
9279 Caller->getFnAttribute("disable-tail-calls").getValueAsBool())
9280 return false;
9281
9282 // We can't tail call inside a function with a swifterror argument. Lowering
9283 // does not support this yet. It would have to move into the swifterror
9284 // register before the call.
9285 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
9286 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
9287 return false;
9288
9289 // Check if target-independent constraints permit a tail call here.
9290 // Target-dependent constraints are checked within TLI->LowerCallTo.
9291 return isInTailCallPosition(CB, DAG.getTarget());
9292}
9293
9295 bool isTailCall, bool isMustTailCall,
9296 const BasicBlock *EHPadBB,
9297 const TargetLowering::PtrAuthInfo *PAI) {
9298 auto &DL = DAG.getDataLayout();
9299 FunctionType *FTy = CB.getFunctionType();
9300 Type *RetTy = CB.getType();
9301
9303 Args.reserve(CB.arg_size());
9304
9305 const Value *SwiftErrorVal = nullptr;
9306 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9307
9308 if (isTailCall)
9309 isTailCall = canTailCall(CB);
9310
9311 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
9312 const Value *V = *I;
9313
9314 // Skip empty types
9315 if (V->getType()->isEmptyTy())
9316 continue;
9317
9318 SDValue ArgNode = getValue(V);
9319 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
9320 Entry.setAttributes(&CB, I - CB.arg_begin());
9321
9322 // Use swifterror virtual register as input to the call.
9323 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
9324 SwiftErrorVal = V;
9325 // We find the virtual register for the actual swifterror argument.
9326 // Instead of using the Value, we use the virtual register instead.
9327 Entry.Node =
9328 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
9329 EVT(TLI.getPointerTy(DL)));
9330 }
9331
9332 Args.push_back(Entry);
9333
9334 // If we have an explicit sret argument that is an Instruction, (i.e., it
9335 // might point to function-local memory), we can't meaningfully tail-call.
9336 if (Entry.IsSRet && isa<Instruction>(V))
9337 isTailCall = false;
9338 }
9339
9340 // If call site has a cfguardtarget operand bundle, create and add an
9341 // additional ArgListEntry.
9342 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9343 Value *V = Bundle->Inputs[0];
9345 Entry.IsCFGuardTarget = true;
9346 Args.push_back(Entry);
9347 }
9348
9349 // Disable tail calls if there is an swifterror argument. Targets have not
9350 // been updated to support tail calls.
9351 if (TLI.supportSwiftError() && SwiftErrorVal)
9352 isTailCall = false;
9353
9354 ConstantInt *CFIType = nullptr;
9355 if (CB.isIndirectCall()) {
9356 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9357 if (!TLI.supportKCFIBundles())
9359 "Target doesn't support calls with kcfi operand bundles.");
9360 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9361 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9362 }
9363 }
9364
9365 SDValue ConvControlToken;
9366 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9367 auto *Token = Bundle->Inputs[0].get();
9368 ConvControlToken = getValue(Token);
9369 }
9370
9371 GlobalValue *DeactivationSymbol = nullptr;
9373 DeactivationSymbol = cast<GlobalValue>(Bundle->Inputs[0].get());
9374 }
9375
9378 .setChain(getRoot())
9379 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9380 .setTailCall(isTailCall)
9384 .setCFIType(CFIType)
9385 .setConvergenceControlToken(ConvControlToken)
9386 .setDeactivationSymbol(DeactivationSymbol);
9387
9388 // Set the pointer authentication info if we have it.
9389 if (PAI) {
9390 if (!TLI.supportPtrAuthBundles())
9392 "This target doesn't support calls with ptrauth operand bundles.");
9393 CLI.setPtrAuth(*PAI);
9394 }
9395
9396 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9397
9398 if (Result.first.getNode()) {
9399 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9400 Result.first = lowerNoFPClassToAssertNoFPClass(DAG, CB, Result.first);
9401 setValue(&CB, Result.first);
9402 }
9403
9404 // The last element of CLI.InVals has the SDValue for swifterror return.
9405 // Here we copy it to a virtual register and update SwiftErrorMap for
9406 // book-keeping.
9407 if (SwiftErrorVal && TLI.supportSwiftError()) {
9408 // Get the last element of InVals.
9409 SDValue Src = CLI.InVals.back();
9410 Register VReg =
9411 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9412 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9413 DAG.setRoot(CopyNode);
9414 }
9415}
9416
9417static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9418 SelectionDAGBuilder &Builder) {
9419 // Check to see if this load can be trivially constant folded, e.g. if the
9420 // input is from a string literal.
9421 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9422 // Cast pointer to the type we really want to load.
9423 Type *LoadTy =
9424 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9425 if (LoadVT.isVector())
9426 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9427 if (const Constant *LoadCst =
9428 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9429 LoadTy, Builder.DAG.getDataLayout()))
9430 return Builder.getValue(LoadCst);
9431 }
9432
9433 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9434 // still constant memory, the input chain can be the entry node.
9435 SDValue Root;
9436 bool ConstantMemory = false;
9437
9438 // Do not serialize (non-volatile) loads of constant memory with anything.
9439 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9440 Root = Builder.DAG.getEntryNode();
9441 ConstantMemory = true;
9442 } else {
9443 // Do not serialize non-volatile loads against each other.
9444 Root = Builder.DAG.getRoot();
9445 }
9446
9447 SDValue Ptr = Builder.getValue(PtrVal);
9448 SDValue LoadVal =
9449 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9450 MachinePointerInfo(PtrVal), Align(1));
9451
9452 if (!ConstantMemory)
9453 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9454 return LoadVal;
9455}
9456
9457/// Record the value for an instruction that produces an integer result,
9458/// converting the type where necessary.
9459void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9460 SDValue Value,
9461 bool IsSigned) {
9462 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9463 I.getType(), true);
9464 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9465 setValue(&I, Value);
9466}
9467
9468/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9469/// true and lower it. Otherwise return false, and it will be lowered like a
9470/// normal call.
9471/// The caller already checked that \p I calls the appropriate LibFunc with a
9472/// correct prototype.
9473bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9474 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9475 const Value *Size = I.getArgOperand(2);
9476 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9477 if (CSize && CSize->getZExtValue() == 0) {
9478 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9479 I.getType(), true);
9480 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9481 return true;
9482 }
9483
9484 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9485 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9486 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9487 getValue(Size), &I);
9488 if (Res.first.getNode()) {
9489 processIntegerCallValue(I, Res.first, true);
9490 PendingLoads.push_back(Res.second);
9491 return true;
9492 }
9493
9494 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9495 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9496 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9497 return false;
9498
9499 // If the target has a fast compare for the given size, it will return a
9500 // preferred load type for that size. Require that the load VT is legal and
9501 // that the target supports unaligned loads of that type. Otherwise, return
9502 // INVALID.
9503 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9504 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9505 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9506 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9507 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9508 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9509 // TODO: Check alignment of src and dest ptrs.
9510 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9511 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9512 if (!TLI.isTypeLegal(LVT) ||
9513 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9514 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9516 }
9517
9518 return LVT;
9519 };
9520
9521 // This turns into unaligned loads. We only do this if the target natively
9522 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9523 // we'll only produce a small number of byte loads.
9524 MVT LoadVT;
9525 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9526 switch (NumBitsToCompare) {
9527 default:
9528 return false;
9529 case 16:
9530 LoadVT = MVT::i16;
9531 break;
9532 case 32:
9533 LoadVT = MVT::i32;
9534 break;
9535 case 64:
9536 case 128:
9537 case 256:
9538 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9539 break;
9540 }
9541
9542 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9543 return false;
9544
9545 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9546 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9547
9548 // Bitcast to a wide integer type if the loads are vectors.
9549 if (LoadVT.isVector()) {
9550 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9551 LoadL = DAG.getBitcast(CmpVT, LoadL);
9552 LoadR = DAG.getBitcast(CmpVT, LoadR);
9553 }
9554
9555 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9556 processIntegerCallValue(I, Cmp, false);
9557 return true;
9558}
9559
9560/// See if we can lower a memchr call into an optimized form. If so, return
9561/// true and lower it. Otherwise return false, and it will be lowered like a
9562/// normal call.
9563/// The caller already checked that \p I calls the appropriate LibFunc with a
9564/// correct prototype.
9565bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9566 const Value *Src = I.getArgOperand(0);
9567 const Value *Char = I.getArgOperand(1);
9568 const Value *Length = I.getArgOperand(2);
9569
9570 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9571 std::pair<SDValue, SDValue> Res =
9572 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9573 getValue(Src), getValue(Char), getValue(Length),
9574 MachinePointerInfo(Src));
9575 if (Res.first.getNode()) {
9576 setValue(&I, Res.first);
9577 PendingLoads.push_back(Res.second);
9578 return true;
9579 }
9580
9581 return false;
9582}
9583
9584/// See if we can lower a memccpy call into an optimized form. If so, return
9585/// true and lower it, otherwise return false and it will be lowered like a
9586/// normal call.
9587/// The caller already checked that \p I calls the appropriate LibFunc with a
9588/// correct prototype.
9589bool SelectionDAGBuilder::visitMemCCpyCall(const CallInst &I) {
9590 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9591 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemccpy(
9592 DAG, getCurSDLoc(), DAG.getRoot(), getValue(I.getArgOperand(0)),
9593 getValue(I.getArgOperand(1)), getValue(I.getArgOperand(2)),
9594 getValue(I.getArgOperand(3)), &I);
9595
9596 if (Res.first) {
9597 processIntegerCallValue(I, Res.first, true);
9598 PendingLoads.push_back(Res.second);
9599 return true;
9600 }
9601 return false;
9602}
9603
9604/// See if we can lower a mempcpy call into an optimized form. If so, return
9605/// true and lower it. Otherwise return false, and it will be lowered like a
9606/// normal call.
9607/// The caller already checked that \p I calls the appropriate LibFunc with a
9608/// correct prototype.
9609bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9610 SDValue Dst = getValue(I.getArgOperand(0));
9611 SDValue Src = getValue(I.getArgOperand(1));
9612 SDValue Size = getValue(I.getArgOperand(2));
9613
9614 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9615 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9616
9617 SDLoc sdl = getCurSDLoc();
9618
9619 // In the mempcpy context we need to pass in a false value for isTailCall
9620 // because the return pointer needs to be adjusted by the size of
9621 // the copied memory.
9622 SDValue Root = getMemoryRoot();
9623 SDValue MC = DAG.getMemcpy(
9624 Root, sdl, Dst, Src, Size, DstAlign, SrcAlign, false, false,
9625 /*CI=*/nullptr, std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9626 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9627 assert(MC.getNode() != nullptr &&
9628 "** memcpy should not be lowered as TailCall in mempcpy context **");
9629 DAG.setRoot(MC);
9630
9631 // Check if Size needs to be truncated or extended.
9632 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9633
9634 // Adjust return pointer to point just past the last dst byte.
9635 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9636 setValue(&I, DstPlusSize);
9637 return true;
9638}
9639
9640/// See if we can lower a strcpy call into an optimized form. If so, return
9641/// true and lower it, otherwise return false and it will be lowered like a
9642/// normal call.
9643/// The caller already checked that \p I calls the appropriate LibFunc with a
9644/// correct prototype.
9645bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9646 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9647
9648 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9649 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrcpy(
9650 DAG, getCurSDLoc(), getRoot(), getValue(Arg0), getValue(Arg1),
9651 MachinePointerInfo(Arg0), MachinePointerInfo(Arg1), isStpcpy, &I);
9652 if (Res.first.getNode()) {
9653 setValue(&I, Res.first);
9654 DAG.setRoot(Res.second);
9655 return true;
9656 }
9657
9658 return false;
9659}
9660
9661/// See if we can lower a strcmp call into an optimized form. If so, return
9662/// true and lower it, otherwise return false and it will be lowered like a
9663/// normal call.
9664/// The caller already checked that \p I calls the appropriate LibFunc with a
9665/// correct prototype.
9666bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9667 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9668
9669 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9670 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrcmp(
9671 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), getValue(Arg1),
9672 MachinePointerInfo(Arg0), MachinePointerInfo(Arg1), &I);
9673 if (Res.first.getNode()) {
9674 processIntegerCallValue(I, Res.first, true);
9675 PendingLoads.push_back(Res.second);
9676 return true;
9677 }
9678
9679 return false;
9680}
9681
9682/// See if we can lower a strlen call into an optimized form. If so, return
9683/// true and lower it, otherwise return false and it will be lowered like a
9684/// normal call.
9685/// The caller already checked that \p I calls the appropriate LibFunc with a
9686/// correct prototype.
9687bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9688 const Value *Arg0 = I.getArgOperand(0);
9689
9690 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9691 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrlen(
9692 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), &I);
9693 if (Res.first.getNode()) {
9694 processIntegerCallValue(I, Res.first, false);
9695 PendingLoads.push_back(Res.second);
9696 return true;
9697 }
9698
9699 return false;
9700}
9701
9702/// See if we can lower a strnlen call into an optimized form. If so, return
9703/// true and lower it, otherwise return false and it will be lowered like a
9704/// normal call.
9705/// The caller already checked that \p I calls the appropriate LibFunc with a
9706/// correct prototype.
9707bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9708 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9709
9710 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9711 std::pair<SDValue, SDValue> Res =
9712 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9713 getValue(Arg0), getValue(Arg1),
9714 MachinePointerInfo(Arg0));
9715 if (Res.first.getNode()) {
9716 processIntegerCallValue(I, Res.first, false);
9717 PendingLoads.push_back(Res.second);
9718 return true;
9719 }
9720
9721 return false;
9722}
9723
9724/// See if we can lower a Strstr call into an optimized form. If so, return
9725/// true and lower it, otherwise return false and it will be lowered like a
9726/// normal call.
9727/// The caller already checked that \p I calls the appropriate LibFunc with a
9728/// correct prototype.
9729bool SelectionDAGBuilder::visitStrstrCall(const CallInst &I) {
9730 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9731 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9732 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrstr(
9733 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), getValue(Arg1), &I);
9734 if (Res.first) {
9735 processIntegerCallValue(I, Res.first, false);
9736 PendingLoads.push_back(Res.second);
9737 return true;
9738 }
9739 return false;
9740}
9741
9742/// See if we can lower a unary floating-point operation into an SDNode with
9743/// the specified Opcode. If so, return true and lower it, otherwise return
9744/// false and it will be lowered like a normal call.
9745/// The caller already checked that \p I calls the appropriate LibFunc with a
9746/// correct prototype.
9747bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9748 unsigned Opcode) {
9749 // We already checked this call's prototype; verify it doesn't modify errno.
9750 // Do not perform optimizations for call sites that require strict
9751 // floating-point semantics.
9752 if (!I.onlyReadsMemory() || I.isStrictFP())
9753 return false;
9754
9755 SDNodeFlags Flags;
9756 Flags.copyFMF(cast<FPMathOperator>(I));
9757
9758 SDValue Tmp = getValue(I.getArgOperand(0));
9759 setValue(&I,
9760 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9761 return true;
9762}
9763
9764/// See if we can lower a binary floating-point operation into an SDNode with
9765/// the specified Opcode. If so, return true and lower it. Otherwise return
9766/// false, and it will be lowered like a normal call.
9767/// The caller already checked that \p I calls the appropriate LibFunc with a
9768/// correct prototype.
9769bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9770 unsigned Opcode) {
9771 // We already checked this call's prototype; verify it doesn't modify errno.
9772 // Do not perform optimizations for call sites that require strict
9773 // floating-point semantics.
9774 if (!I.onlyReadsMemory() || I.isStrictFP())
9775 return false;
9776
9777 SDNodeFlags Flags;
9778 Flags.copyFMF(cast<FPMathOperator>(I));
9779
9780 SDValue Tmp0 = getValue(I.getArgOperand(0));
9781 SDValue Tmp1 = getValue(I.getArgOperand(1));
9782 EVT VT = Tmp0.getValueType();
9783 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9784 return true;
9785}
9786
9787void SelectionDAGBuilder::visitCall(const CallInst &I) {
9788 // Handle inline assembly differently.
9789 if (I.isInlineAsm()) {
9790 visitInlineAsm(I);
9791 return;
9792 }
9793
9795
9796 if (Function *F = I.getCalledFunction()) {
9797 if (F->isDeclaration()) {
9798 // Is this an LLVM intrinsic?
9799 if (unsigned IID = F->getIntrinsicID()) {
9800 visitIntrinsicCall(I, IID);
9801 return;
9802 }
9803 }
9804
9805 // Check for well-known libc/libm calls. If the function is internal, it
9806 // can't be a library call. Don't do the check if marked as nobuiltin for
9807 // some reason.
9808 // This code should not handle libcalls that are already canonicalized to
9809 // intrinsics by the middle-end.
9810 LibFunc Func;
9811 if (!I.isNoBuiltin() && !F->hasLocalLinkage() && F->hasName() &&
9812 LibInfo->getLibFunc(*F, Func) && LibInfo->hasOptimizedCodeGen(Func)) {
9813 switch (Func) {
9814 default: break;
9815 case LibFunc_bcmp:
9816 if (visitMemCmpBCmpCall(I))
9817 return;
9818 break;
9819 case LibFunc_copysign:
9820 case LibFunc_copysignf:
9821 case LibFunc_copysignl:
9822 // We already checked this call's prototype; verify it doesn't modify
9823 // errno.
9824 if (I.onlyReadsMemory()) {
9825 SDValue LHS = getValue(I.getArgOperand(0));
9826 SDValue RHS = getValue(I.getArgOperand(1));
9828 LHS.getValueType(), LHS, RHS));
9829 return;
9830 }
9831 break;
9832 case LibFunc_sin:
9833 case LibFunc_sinf:
9834 case LibFunc_sinl:
9835 if (visitUnaryFloatCall(I, ISD::FSIN))
9836 return;
9837 break;
9838 case LibFunc_cos:
9839 case LibFunc_cosf:
9840 case LibFunc_cosl:
9841 if (visitUnaryFloatCall(I, ISD::FCOS))
9842 return;
9843 break;
9844 case LibFunc_tan:
9845 case LibFunc_tanf:
9846 case LibFunc_tanl:
9847 if (visitUnaryFloatCall(I, ISD::FTAN))
9848 return;
9849 break;
9850 case LibFunc_asin:
9851 case LibFunc_asinf:
9852 case LibFunc_asinl:
9853 if (visitUnaryFloatCall(I, ISD::FASIN))
9854 return;
9855 break;
9856 case LibFunc_acos:
9857 case LibFunc_acosf:
9858 case LibFunc_acosl:
9859 if (visitUnaryFloatCall(I, ISD::FACOS))
9860 return;
9861 break;
9862 case LibFunc_atan:
9863 case LibFunc_atanf:
9864 case LibFunc_atanl:
9865 if (visitUnaryFloatCall(I, ISD::FATAN))
9866 return;
9867 break;
9868 case LibFunc_atan2:
9869 case LibFunc_atan2f:
9870 case LibFunc_atan2l:
9871 if (visitBinaryFloatCall(I, ISD::FATAN2))
9872 return;
9873 break;
9874 case LibFunc_sinh:
9875 case LibFunc_sinhf:
9876 case LibFunc_sinhl:
9877 if (visitUnaryFloatCall(I, ISD::FSINH))
9878 return;
9879 break;
9880 case LibFunc_cosh:
9881 case LibFunc_coshf:
9882 case LibFunc_coshl:
9883 if (visitUnaryFloatCall(I, ISD::FCOSH))
9884 return;
9885 break;
9886 case LibFunc_tanh:
9887 case LibFunc_tanhf:
9888 case LibFunc_tanhl:
9889 if (visitUnaryFloatCall(I, ISD::FTANH))
9890 return;
9891 break;
9892 case LibFunc_sqrt:
9893 case LibFunc_sqrtf:
9894 case LibFunc_sqrtl:
9895 case LibFunc_sqrt_finite:
9896 case LibFunc_sqrtf_finite:
9897 case LibFunc_sqrtl_finite:
9898 if (visitUnaryFloatCall(I, ISD::FSQRT))
9899 return;
9900 break;
9901 case LibFunc_log2:
9902 case LibFunc_log2f:
9903 case LibFunc_log2l:
9904 if (visitUnaryFloatCall(I, ISD::FLOG2))
9905 return;
9906 break;
9907 case LibFunc_exp2:
9908 case LibFunc_exp2f:
9909 case LibFunc_exp2l:
9910 if (visitUnaryFloatCall(I, ISD::FEXP2))
9911 return;
9912 break;
9913 case LibFunc_exp10:
9914 case LibFunc_exp10f:
9915 case LibFunc_exp10l:
9916 if (visitUnaryFloatCall(I, ISD::FEXP10))
9917 return;
9918 break;
9919 case LibFunc_ldexp:
9920 case LibFunc_ldexpf:
9921 case LibFunc_ldexpl:
9922 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9923 return;
9924 break;
9925 case LibFunc_strstr:
9926 if (visitStrstrCall(I))
9927 return;
9928 break;
9929 case LibFunc_memcmp:
9930 if (visitMemCmpBCmpCall(I))
9931 return;
9932 break;
9933 case LibFunc_memccpy:
9934 if (visitMemCCpyCall(I))
9935 return;
9936 break;
9937 case LibFunc_mempcpy:
9938 if (visitMemPCpyCall(I))
9939 return;
9940 break;
9941 case LibFunc_memchr:
9942 if (visitMemChrCall(I))
9943 return;
9944 break;
9945 case LibFunc_strcpy:
9946 if (visitStrCpyCall(I, false))
9947 return;
9948 break;
9949 case LibFunc_stpcpy:
9950 if (visitStrCpyCall(I, true))
9951 return;
9952 break;
9953 case LibFunc_strcmp:
9954 if (visitStrCmpCall(I))
9955 return;
9956 break;
9957 case LibFunc_strlen:
9958 if (visitStrLenCall(I))
9959 return;
9960 break;
9961 case LibFunc_strnlen:
9962 if (visitStrNLenCall(I))
9963 return;
9964 break;
9965 }
9966 }
9967 }
9968
9969 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9970 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9971 return;
9972 }
9973
9974 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9975 // have to do anything here to lower funclet bundles.
9976 // CFGuardTarget bundles are lowered in LowerCallTo.
9978 I, "calls",
9983
9984 SDValue Callee = getValue(I.getCalledOperand());
9985
9986 if (I.hasDeoptState())
9987 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9988 else
9989 // Check if we can potentially perform a tail call. More detailed checking
9990 // is be done within LowerCallTo, after more information about the call is
9991 // known.
9992 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9993}
9994
9996 const CallBase &CB, const BasicBlock *EHPadBB) {
9997 auto PAB = CB.getOperandBundle("ptrauth");
9998 const Value *CalleeV = CB.getCalledOperand();
9999
10000 // Gather the call ptrauth data from the operand bundle:
10001 // [ i32 <key>, i64 <discriminator> ]
10002 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
10003 const Value *Discriminator = PAB->Inputs[1];
10004
10005 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
10006 assert(Discriminator->getType()->isIntegerTy(64) &&
10007 "Invalid ptrauth discriminator");
10008
10009 // Look through ptrauth constants to find the raw callee.
10010 // Do a direct unauthenticated call if we found it and everything matches.
10011 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
10012 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
10013 DAG.getDataLayout()))
10014 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
10015 CB.isMustTailCall(), EHPadBB);
10016
10017 // Functions should never be ptrauth-called directly.
10018 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
10019
10020 // Otherwise, do an authenticated indirect call.
10021 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
10022 getValue(Discriminator)};
10023
10024 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
10025 EHPadBB, &PAI);
10026}
10027
10028namespace {
10029
10030/// AsmOperandInfo - This contains information for each constraint that we are
10031/// lowering.
10032class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
10033public:
10034 /// CallOperand - If this is the result output operand or a clobber
10035 /// this is null, otherwise it is the incoming operand to the CallInst.
10036 /// This gets modified as the asm is processed.
10037 SDValue CallOperand;
10038
10039 /// AssignedRegs - If this is a register or register class operand, this
10040 /// contains the set of register corresponding to the operand.
10041 RegsForValue AssignedRegs;
10042
10043 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
10044 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
10045 }
10046
10047 /// Whether or not this operand accesses memory
10048 bool hasMemory(const TargetLowering &TLI) const {
10049 // Indirect operand accesses access memory.
10050 if (isIndirect)
10051 return true;
10052
10053 for (const auto &Code : Codes)
10055 return true;
10056
10057 return false;
10058 }
10059};
10060
10061
10062} // end anonymous namespace
10063
10064/// Make sure that the output operand \p OpInfo and its corresponding input
10065/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
10066/// out).
10067static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
10068 SDISelAsmOperandInfo &MatchingOpInfo,
10069 SelectionDAG &DAG) {
10070 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
10071 return;
10072
10074 const auto &TLI = DAG.getTargetLoweringInfo();
10075
10076 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
10077 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
10078 OpInfo.ConstraintVT);
10079 std::pair<unsigned, const TargetRegisterClass *> InputRC =
10080 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
10081 MatchingOpInfo.ConstraintVT);
10082 const bool OutOpIsIntOrFP =
10083 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
10084 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
10085 MatchingOpInfo.ConstraintVT.isFloatingPoint();
10086 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
10087 // FIXME: error out in a more elegant fashion
10088 report_fatal_error("Unsupported asm: input constraint"
10089 " with a matching output constraint of"
10090 " incompatible type!");
10091 }
10092 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
10093}
10094
10095/// Get a direct memory input to behave well as an indirect operand.
10096/// This may introduce stores, hence the need for a \p Chain.
10097/// \return The (possibly updated) chain.
10098static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
10099 SDISelAsmOperandInfo &OpInfo,
10100 SelectionDAG &DAG) {
10101 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10102
10103 // If we don't have an indirect input, put it in the constpool if we can,
10104 // otherwise spill it to a stack slot.
10105 // TODO: This isn't quite right. We need to handle these according to
10106 // the addressing mode that the constraint wants. Also, this may take
10107 // an additional register for the computation and we don't want that
10108 // either.
10109
10110 // If the operand is a float, integer, or vector constant, spill to a
10111 // constant pool entry to get its address.
10112 const Value *OpVal = OpInfo.CallOperandVal;
10113 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
10115 OpInfo.CallOperand = DAG.getConstantPool(
10116 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
10117 return Chain;
10118 }
10119
10120 // Otherwise, create a stack slot and emit a store to it before the asm.
10121 Type *Ty = OpVal->getType();
10122 auto &DL = DAG.getDataLayout();
10123 TypeSize TySize = DL.getTypeAllocSize(Ty);
10126 int StackID = 0;
10127 if (TySize.isScalable())
10128 StackID = TFI->getStackIDForScalableVectors();
10129 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
10130 DL.getPrefTypeAlign(Ty), false,
10131 nullptr, StackID);
10132 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
10133 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
10135 TLI.getMemValueType(DL, Ty));
10136 OpInfo.CallOperand = StackSlot;
10137
10138 return Chain;
10139}
10140
10141/// GetRegistersForValue - Assign registers (virtual or physical) for the
10142/// specified operand. We prefer to assign virtual registers, to allow the
10143/// register allocator to handle the assignment process. However, if the asm
10144/// uses features that we can't model on machineinstrs, we have SDISel do the
10145/// allocation. This produces generally horrible, but correct, code.
10146///
10147/// OpInfo describes the operand
10148/// RefOpInfo describes the matching operand if any, the operand otherwise
10149static std::optional<unsigned>
10151 SDISelAsmOperandInfo &OpInfo,
10152 SDISelAsmOperandInfo &RefOpInfo) {
10153 LLVMContext &Context = *DAG.getContext();
10154 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10155
10159
10160 // No work to do for memory/address operands.
10161 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
10162 OpInfo.ConstraintType == TargetLowering::C_Address)
10163 return std::nullopt;
10164
10165 // If this is a constraint for a single physreg, or a constraint for a
10166 // register class, find it.
10167 unsigned AssignedReg;
10168 const TargetRegisterClass *RC;
10169 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
10170 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
10171 // RC is unset only on failure. Return immediately.
10172 if (!RC)
10173 return std::nullopt;
10174
10175 // Get the actual register value type. This is important, because the user
10176 // may have asked for (e.g.) the AX register in i32 type. We need to
10177 // remember that AX is actually i16 to get the right extension.
10178 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
10179
10180 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
10181 // If this is an FP operand in an integer register (or visa versa), or more
10182 // generally if the operand value disagrees with the register class we plan
10183 // to stick it in, fix the operand type.
10184 //
10185 // If this is an input value, the bitcast to the new type is done now.
10186 // Bitcast for output value is done at the end of visitInlineAsm().
10187 if ((OpInfo.Type == InlineAsm::isOutput ||
10188 OpInfo.Type == InlineAsm::isInput) &&
10189 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
10190 // Try to convert to the first EVT that the reg class contains. If the
10191 // types are identical size, use a bitcast to convert (e.g. two differing
10192 // vector types). Note: output bitcast is done at the end of
10193 // visitInlineAsm().
10194 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
10195 // Exclude indirect inputs while they are unsupported because the code
10196 // to perform the load is missing and thus OpInfo.CallOperand still
10197 // refers to the input address rather than the pointed-to value.
10198 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
10199 OpInfo.CallOperand =
10200 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
10201 OpInfo.ConstraintVT = RegVT;
10202 // If the operand is an FP value and we want it in integer registers,
10203 // use the corresponding integer type. This turns an f64 value into
10204 // i64, which can be passed with two i32 values on a 32-bit machine.
10205 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
10206 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
10207 if (OpInfo.Type == InlineAsm::isInput)
10208 OpInfo.CallOperand =
10209 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
10210 OpInfo.ConstraintVT = VT;
10211 }
10212 }
10213 }
10214
10215 // No need to allocate a matching input constraint since the constraint it's
10216 // matching to has already been allocated.
10217 if (OpInfo.isMatchingInputConstraint())
10218 return std::nullopt;
10219
10220 EVT ValueVT = OpInfo.ConstraintVT;
10221 if (OpInfo.ConstraintVT == MVT::Other)
10222 ValueVT = RegVT;
10223
10224 // Initialize NumRegs.
10225 unsigned NumRegs = 1;
10226 if (OpInfo.ConstraintVT != MVT::Other)
10227 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
10228
10229 // If this is a constraint for a specific physical register, like {r17},
10230 // assign it now.
10231
10232 // If this associated to a specific register, initialize iterator to correct
10233 // place. If virtual, make sure we have enough registers
10234
10235 // Initialize iterator if necessary
10238
10239 // Do not check for single registers.
10240 if (AssignedReg) {
10241 I = std::find(I, RC->end(), AssignedReg);
10242 if (I == RC->end()) {
10243 // RC does not contain the selected register, which indicates a
10244 // mismatch between the register and the required type/bitwidth.
10245 return {AssignedReg};
10246 }
10247 }
10248
10249 for (; NumRegs; --NumRegs, ++I) {
10250 assert(I != RC->end() && "Ran out of registers to allocate!");
10251 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
10252 Regs.push_back(R);
10253 }
10254
10255 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
10256 return std::nullopt;
10257}
10258
10259static unsigned
10261 const std::vector<SDValue> &AsmNodeOperands) {
10262 // Scan until we find the definition we already emitted of this operand.
10263 unsigned CurOp = InlineAsm::Op_FirstOperand;
10264 for (; OperandNo; --OperandNo) {
10265 // Advance to the next operand.
10266 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
10267 const InlineAsm::Flag F(OpFlag);
10268 assert(
10269 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
10270 "Skipped past definitions?");
10271 CurOp += F.getNumOperandRegisters() + 1;
10272 }
10273 return CurOp;
10274}
10275
10276namespace {
10277
10278class ExtraFlags {
10279 unsigned Flags = 0;
10280
10281public:
10282 explicit ExtraFlags(const CallBase &Call) {
10283 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10284 if (IA->hasSideEffects())
10286 if (IA->isAlignStack())
10288 if (IA->canThrow())
10290 if (Call.isConvergent())
10292 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
10293 }
10294
10295 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
10296 // Ideally, we would only check against memory constraints. However, the
10297 // meaning of an Other constraint can be target-specific and we can't easily
10298 // reason about it. Therefore, be conservative and set MayLoad/MayStore
10299 // for Other constraints as well.
10302 if (OpInfo.Type == InlineAsm::isInput)
10304 else if (OpInfo.Type == InlineAsm::isOutput)
10306 else if (OpInfo.Type == InlineAsm::isClobber)
10308 }
10309 }
10310
10311 unsigned get() const { return Flags; }
10312};
10313
10314} // end anonymous namespace
10315
10316static bool isFunction(SDValue Op) {
10317 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
10318 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10319 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10320
10321 // In normal "call dllimport func" instruction (non-inlineasm) it force
10322 // indirect access by specifing call opcode. And usually specially print
10323 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10324 // not do in this way now. (In fact, this is similar with "Data Access"
10325 // action). So here we ignore dllimport function.
10326 if (Fn && !Fn->hasDLLImportStorageClass())
10327 return true;
10328 }
10329 }
10330 return false;
10331}
10332
10333namespace {
10334
10335struct ConstraintDecisionInfo {
10336 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10337 std::vector<SDValue> AsmNodeOperands;
10338 SDValue Glue, Chain;
10339 bool HasSideEffect = false;
10340 MCSymbol *BeginLabel = nullptr;
10341
10342 SmallVector<char> Buffer;
10343 raw_svector_ostream ErrorMsg;
10344
10345 ConstraintDecisionInfo() : ErrorMsg(Buffer) {}
10346};
10347
10348} // end anonymous namespace
10349
10350/// Construct operand info objects.
10351static bool
10352constructOperandInfo(ConstraintDecisionInfo &Info,
10353 TargetLowering::AsmOperandInfoVector &TargetConstraints,
10354 SelectionDAGBuilder &Builder, const TargetLowering &TLI,
10355 ExtraFlags &ExtraInfo) {
10356 for (auto &T : TargetConstraints) {
10357 Info.ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10358 SDISelAsmOperandInfo &OpInfo = Info.ConstraintOperands.back();
10359
10360 if (OpInfo.CallOperandVal)
10361 OpInfo.CallOperand = Builder.getValue(OpInfo.CallOperandVal);
10362
10363 if (!Info.HasSideEffect)
10364 Info.HasSideEffect = OpInfo.hasMemory(TLI);
10365
10366 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10367 // FIXME: Could we compute this on OpInfo rather than T?
10368
10369 // Compute the constraint code and ConstraintType to use.
10371
10372 if (T.ConstraintType == TargetLowering::C_Immediate && OpInfo.CallOperand &&
10373 !isa<ConstantSDNode>(OpInfo.CallOperand)) {
10374 // We've delayed emitting a diagnostic like the "n" constraint because
10375 // inlining could cause an integer showing up.
10376 Info.ErrorMsg << "constraint '" << T.ConstraintCode
10377 << "' expects an integer constant expression";
10378 return true;
10379 }
10380
10381 ExtraInfo.update(T);
10382 }
10383
10384 return false;
10385}
10386
10387/// Compute which constraint option to use for each operand.
10388static void
10389computeConstraintToUse(ConstraintDecisionInfo &Info, const CallBase &Call,
10390 TargetLowering::AsmOperandInfoVector &TargetConstraints,
10391 SelectionDAGBuilder &Builder, const TargetLowering &TLI,
10392 const TargetMachine &TM, SelectionDAG &DAG) {
10393 const auto *IA = cast<InlineAsm>(Call.getCalledOperand());
10395 IA->collectAsmStrs(AsmStrs);
10396
10397 int OpNo = -1;
10398 for (SDISelAsmOperandInfo &OpInfo : Info.ConstraintOperands) {
10399 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10400 OpNo++;
10401
10402 // If this is an output operand with a matching input operand, look up the
10403 // matching input. If their types mismatch, e.g. one is an integer, the
10404 // other is floating point, or their sizes are different, flag it as an
10405 // error.
10406 if (OpInfo.hasMatchingInput()) {
10407 SDISelAsmOperandInfo &Input =
10408 Info.ConstraintOperands[OpInfo.MatchingInput];
10409 patchMatchingInput(OpInfo, Input, DAG);
10410 }
10411
10412 // Compute the constraint code and ConstraintType to use.
10413 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10414
10415 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10416 OpInfo.Type == InlineAsm::isClobber) ||
10417 OpInfo.ConstraintType == TargetLowering::C_Address)
10418 continue;
10419
10420 // In Linux PIC model, there are 4 cases about value/label addressing:
10421 //
10422 // 1: Function call or Label jmp inside the module.
10423 // 2: Data access (such as global variable, static variable) inside module.
10424 // 3: Function call or Label jmp outside the module.
10425 // 4: Data access (such as global variable) outside the module.
10426 //
10427 // Due to current llvm inline asm architecture designed to not "recognize"
10428 // the asm code, there are quite troubles for us to treat mem addressing
10429 // differently for same value/adress used in different instuctions.
10430 // For example, in pic model, call a func may in plt way or direclty
10431 // pc-related, but lea/mov a function adress may use got.
10432 //
10433 // Here we try to "recognize" function call for the case 1 and case 3 in
10434 // inline asm. And try to adjust the constraint for them.
10435 //
10436 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10437 // label, so here we don't handle jmp function label now, but we need to
10438 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10439 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10440 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10442 OpInfo.isIndirect = false;
10443 OpInfo.ConstraintType = TargetLowering::C_Address;
10444 }
10445
10446 // If this is a memory input, and if the operand is not indirect, do what we
10447 // need to provide an address for the memory input.
10448 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10449 !OpInfo.isIndirect) {
10450 assert((OpInfo.isMultipleAlternative ||
10451 (OpInfo.Type == InlineAsm::isInput)) &&
10452 "Can only indirectify direct input operands!");
10453
10454 // Memory operands really want the address of the value.
10455 Info.Chain = getAddressForMemoryInput(Info.Chain, Builder.getCurSDLoc(),
10456 OpInfo, DAG);
10457
10458 // There is no longer a Value* corresponding to this operand.
10459 OpInfo.CallOperandVal = nullptr;
10460
10461 // It is now an indirect operand.
10462 OpInfo.isIndirect = true;
10463 }
10464 }
10465}
10466
10467/// Prepare DAG-level operands. As part of this, assign virtual and physical
10468/// registers for inputs and output.
10469static bool prepareDAGLevelOperands(ConstraintDecisionInfo &Info,
10470 const CallBase &Call,
10471 SelectionDAGBuilder &Builder,
10472 const TargetLowering &TLI,
10473 SelectionDAG &DAG) {
10474 SDLoc DL = Builder.getCurSDLoc();
10475 for (SDISelAsmOperandInfo &OpInfo : Info.ConstraintOperands) {
10476 // Assign Registers.
10477 SDISelAsmOperandInfo &RefOpInfo =
10478 OpInfo.isMatchingInputConstraint()
10479 ? Info.ConstraintOperands[OpInfo.getMatchedOperand()]
10480 : OpInfo;
10481 const auto RegError = getRegistersForValue(DAG, DL, OpInfo, RefOpInfo);
10482 if (RegError) {
10483 const MachineFunction &MF = DAG.getMachineFunction();
10485 const char *RegName = TRI.getName(*RegError);
10486 Info.ErrorMsg << "register '" << RegName << "' allocated for constraint '"
10487 << OpInfo.ConstraintCode
10488 << "' does not match required type";
10489 return true;
10490 }
10491
10492 auto DetectWriteToReservedRegister = [&]() {
10493 const MachineFunction &MF = DAG.getMachineFunction();
10495
10496 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10497 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10498 Info.ErrorMsg << "write to reserved register '"
10499 << TRI.getRegAsmName(Reg) << "'";
10500 return true;
10501 }
10502 }
10503
10504 return false;
10505 };
10506 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10507 (OpInfo.Type == InlineAsm::isInput &&
10508 !OpInfo.isMatchingInputConstraint())) &&
10509 "Only address as input operand is allowed.");
10510
10511 switch (OpInfo.Type) {
10513 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10514 const InlineAsm::ConstraintCode ConstraintID =
10515 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10517 "Failed to convert memory constraint code to constraint id.");
10518
10519 // Add information to the INLINEASM node to know about this output.
10521 OpFlags.setMemConstraint(ConstraintID);
10522 Info.AsmNodeOperands.push_back(
10523 DAG.getTargetConstant(OpFlags, DL, MVT::i32));
10524 Info.AsmNodeOperands.push_back(OpInfo.CallOperand);
10525 } else {
10526 // Otherwise, this outputs to a register (directly for C_Register /
10527 // C_RegisterClass, and a target-defined fashion for
10528 // C_Immediate/C_Other). Find a register that we can use.
10529 if (OpInfo.AssignedRegs.Regs.empty()) {
10530 Info.ErrorMsg << "could not allocate output register for "
10531 << "constraint '" << OpInfo.ConstraintCode << "'";
10532 return true;
10533 }
10534
10535 if (DetectWriteToReservedRegister())
10536 return true;
10537
10538 // Add information to the INLINEASM node to know that this register is
10539 // set.
10540 OpInfo.AssignedRegs.AddInlineAsmOperands(
10541 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10543 false, 0, DL, DAG, Info.AsmNodeOperands);
10544 }
10545 break;
10546
10547 case InlineAsm::isInput:
10548 case InlineAsm::isLabel: {
10549 SDValue InOperandVal = OpInfo.CallOperand;
10550
10551 if (OpInfo.isMatchingInputConstraint()) {
10552 // If this is required to match an output register we have already set,
10553 // just use its register.
10554 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10555 Info.AsmNodeOperands);
10556 InlineAsm::Flag Flag(Info.AsmNodeOperands[CurOp]->getAsZExtVal());
10557 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10558 if (OpInfo.isIndirect) {
10559 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10560 Info.ErrorMsg << "inline asm not supported yet: cannot handle "
10561 << "tied indirect register inputs";
10562 return true;
10563 }
10564
10567 MachineRegisterInfo &MRI = MF.getRegInfo();
10569 auto *R = cast<RegisterSDNode>(Info.AsmNodeOperands[CurOp + 1]);
10570 Register TiedReg = R->getReg();
10571 MVT RegVT = R->getSimpleValueType(0);
10572 const TargetRegisterClass *RC =
10573 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10574 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10575 : TRI.getMinimalPhysRegClass(TiedReg);
10576 for (unsigned I = 0, E = Flag.getNumOperandRegisters(); I != E; ++I)
10577 Regs.push_back(MRI.createVirtualRegister(RC));
10578
10579 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10580
10581 // Use the produced MatchedRegs object to
10582 MatchedRegs.getCopyToRegs(InOperandVal, DAG, DL, Info.Chain,
10583 &Info.Glue, &Call);
10585 OpInfo.getMatchedOperand(), DL, DAG,
10586 Info.AsmNodeOperands);
10587 break;
10588 }
10589
10590 assert(Flag.isMemKind() && "Unknown matching constraint!");
10591 assert(Flag.getNumOperandRegisters() == 1 &&
10592 "Unexpected number of operands");
10593
10594 // Add information to the INLINEASM node to know about this input.
10595 // See InlineAsm.h isUseOperandTiedToDef.
10596 Flag.clearMemConstraint();
10597 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10598 Info.AsmNodeOperands.push_back(DAG.getTargetConstant(
10599 Flag, DL, TLI.getPointerTy(DAG.getDataLayout())));
10600 Info.AsmNodeOperands.push_back(Info.AsmNodeOperands[CurOp + 1]);
10601 break;
10602 }
10603
10604 // Treat indirect 'X' constraint as memory.
10605 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10606 OpInfo.isIndirect)
10607 OpInfo.ConstraintType = TargetLowering::C_Memory;
10608
10609 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10610 OpInfo.ConstraintType == TargetLowering::C_Other) {
10611 std::vector<SDValue> Ops;
10612 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10613 Ops, DAG);
10614 if (Ops.empty()) {
10615 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10616 if (isa<ConstantSDNode>(InOperandVal)) {
10617 Info.ErrorMsg << "value out of range for constraint '"
10618 << OpInfo.ConstraintCode << "'";
10619 return true;
10620 }
10621
10622 Info.ErrorMsg << "invalid operand for inline asm constraint '"
10623 << OpInfo.ConstraintCode << "'";
10624 return true;
10625 }
10626
10627 // Add information to the INLINEASM node to know about this input.
10628 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10629 Info.AsmNodeOperands.push_back(DAG.getTargetConstant(
10630 ResOpType, DL, TLI.getPointerTy(DAG.getDataLayout())));
10631 llvm::append_range(Info.AsmNodeOperands, Ops);
10632 break;
10633 }
10634
10635 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10636 assert((OpInfo.isIndirect ||
10637 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10638 "Operand must be indirect to be a mem!");
10639 assert(InOperandVal.getValueType() ==
10640 TLI.getPointerTy(DAG.getDataLayout()) &&
10641 "Memory operands expect pointer values");
10642
10643 const InlineAsm::ConstraintCode ConstraintID =
10644 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10646 "Failed to convert memory constraint code to constraint id.");
10647
10648 // Add information to the INLINEASM node to know about this input.
10650 ResOpType.setMemConstraint(ConstraintID);
10651 Info.AsmNodeOperands.push_back(
10652 DAG.getTargetConstant(ResOpType, DL, MVT::i32));
10653 Info.AsmNodeOperands.push_back(InOperandVal);
10654 break;
10655 }
10656
10657 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10658 const InlineAsm::ConstraintCode ConstraintID =
10659 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10661 "Failed to convert memory constraint code to constraint id.");
10662
10664
10665 SDValue AsmOp = InOperandVal;
10666 if (isFunction(InOperandVal)) {
10667 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10668 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10669 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
10670 InOperandVal.getValueType(),
10671 GA->getOffset());
10672 }
10673
10674 // Add information to the INLINEASM node to know about this input.
10675 ResOpType.setMemConstraint(ConstraintID);
10676
10677 Info.AsmNodeOperands.push_back(
10678 DAG.getTargetConstant(ResOpType, DL, MVT::i32));
10679 Info.AsmNodeOperands.push_back(AsmOp);
10680 break;
10681 }
10682
10683 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10684 OpInfo.ConstraintType != TargetLowering::C_Register) {
10685 Info.ErrorMsg << "unknown asm constraint '" << OpInfo.ConstraintCode
10686 << "'";
10687 return true;
10688 }
10689
10690 // TODO: Support this.
10691 if (OpInfo.isIndirect) {
10692 Info.ErrorMsg << "cannot handle indirect register inputs yet for "
10693 << "constraint '" << OpInfo.ConstraintCode << "'";
10694 return true;
10695 }
10696
10697 // Copy the input into the appropriate registers.
10698 if (OpInfo.AssignedRegs.Regs.empty()) {
10699 Info.ErrorMsg << "could not allocate input reg for constraint '"
10700 << OpInfo.ConstraintCode << "'";
10701 return true;
10702 }
10703
10704 if (DetectWriteToReservedRegister())
10705 return true;
10706
10707 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, DL, Info.Chain,
10708 &Info.Glue, &Call);
10709 OpInfo.AssignedRegs.AddInlineAsmOperands(
10710 InlineAsm::Kind::RegUse, false, 0, DL, DAG, Info.AsmNodeOperands);
10711 break;
10712 }
10713
10715 // Add the clobbered value to the operand list, so that the register
10716 // allocator is aware that the physreg got clobbered.
10717 if (!OpInfo.AssignedRegs.Regs.empty())
10718 OpInfo.AssignedRegs.AddInlineAsmOperands(
10719 InlineAsm::Kind::Clobber, false, 0, DL, DAG, Info.AsmNodeOperands);
10720 break;
10721 }
10722 }
10723
10724 return false;
10725}
10726
10727/// DetermineConstraints - Find the constraints to use for inline asm operands.
10728static bool
10729determineConstraints(ConstraintDecisionInfo &Info,
10730 TargetLowering::AsmOperandInfoVector &TargetConstraints,
10731 const CallBase &Call, SelectionDAGBuilder &Builder,
10732 const TargetLowering &TLI, const TargetMachine &TM,
10733 SelectionDAG &DAG, const BasicBlock *EHPadBB) {
10734 const auto *IA = cast<InlineAsm>(Call.getCalledOperand());
10735 ExtraFlags ExtraInfo(Call);
10736
10737 // First pass: Construct operand info objects.
10738 Info.HasSideEffect = IA->hasSideEffects();
10739 if (constructOperandInfo(Info, TargetConstraints, Builder, TLI, ExtraInfo))
10740 return true;
10741
10742 // We won't need to flush pending loads if this asm doesn't touch
10743 // memory and is nonvolatile.
10744 Info.Chain = Info.HasSideEffect ? Builder.getRoot() : DAG.getRoot();
10745
10746 bool IsCallBr = isa<CallBrInst>(Call);
10747 bool EmitEHLabels = isa<InvokeInst>(Call);
10748 if (IsCallBr || EmitEHLabels)
10749 // If this is a callbr or invoke we need to flush pending exports since
10750 // inlineasm_br and invoke are terminators.
10751 // We need to do this before nodes are glued to the inlineasm_br node.
10752 Info.Chain = Builder.getControlRoot();
10753
10754 if (EmitEHLabels)
10755 Info.Chain = Builder.lowerStartEH(Info.Chain, EHPadBB, Info.BeginLabel);
10756
10757 // Second pass: Compute which constraint option to use.
10758 computeConstraintToUse(Info, Call, TargetConstraints, Builder, TLI, TM, DAG);
10759
10760 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10761 Info.AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10762 Info.AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10763 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10764
10765 // If we have a !srcloc metadata node associated with it, we want to attach
10766 // this to the ultimately generated inline asm machineinstr. To do this, we
10767 // pass in the third operand as this (potentially null) inline asm MDNode.
10768 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10769 Info.AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10770
10771 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10772 // bits as operand 3.
10773 Info.AsmNodeOperands.push_back(
10774 DAG.getTargetConstant(ExtraInfo.get(), Builder.getCurSDLoc(),
10775 TLI.getPointerTy(DAG.getDataLayout())));
10776
10777 // Third pass: Prepare DAG-level operands
10778 return prepareDAGLevelOperands(Info, Call, Builder, TLI, DAG);
10779}
10780
10781/// visitInlineAsm - Handle a call to an InlineAsm object.
10782void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10783 const BasicBlock *EHPadBB) {
10784 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10786 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10787
10788 assert((!isa<InvokeInst>(Call) || EHPadBB) &&
10789 "InvokeInst must have an EHPadBB");
10790
10791 ConstraintDecisionInfo Info;
10792 if (determineConstraints(Info, TargetConstraints, Call, *this, TLI, TM, DAG,
10793 EHPadBB))
10794 return emitInlineAsmError(Call, Info.ErrorMsg.str());
10795
10796 SDValue Glue = Info.Glue;
10797 SDValue Chain = Info.Chain;
10798
10799 // Finish up input operands. Set the input chain and add the flag last.
10800 Info.AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10801 if (Glue.getNode())
10802 Info.AsmNodeOperands.push_back(Glue);
10803
10804 bool IsCallBr = isa<CallBrInst>(Call);
10805 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10806 Chain =
10807 DAG.getNode(ISDOpc, getCurSDLoc(), DAG.getVTList(MVT::Other, MVT::Glue),
10808 Info.AsmNodeOperands);
10809 Glue = Chain.getValue(1);
10810
10811 // Do additional work to generate outputs.
10812
10813 SmallVector<EVT, 1> ResultVTs;
10814 SmallVector<SDValue, 1> ResultValues;
10815 SmallVector<SDValue, 8> OutChains;
10816
10817 llvm::Type *CallResultType = Call.getType();
10818 ArrayRef<Type *> ResultTypes;
10819 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10820 ResultTypes = StructResult->elements();
10821 else if (!CallResultType->isVoidTy())
10822 ResultTypes = ArrayRef(CallResultType);
10823
10824 auto CurResultType = ResultTypes.begin();
10825 auto handleRegAssign = [&](SDValue V) {
10826 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10827 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10828 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10829 ++CurResultType;
10830 // If the type of the inline asm call site return value is different but has
10831 // same size as the type of the asm output bitcast it. One example of this
10832 // is for vectors with different width / number of elements. This can
10833 // happen for register classes that can contain multiple different value
10834 // types. The preg or vreg allocated may not have the same VT as was
10835 // expected.
10836 //
10837 // This can also happen for a return value that disagrees with the register
10838 // class it is put in, eg. a double in a general-purpose register on a
10839 // 32-bit machine.
10840 if (ResultVT != V.getValueType() &&
10841 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10842 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10843 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10844 V.getValueType().isInteger()) {
10845 // If a result value was tied to an input value, the computed result
10846 // may have a wider width than the expected result. Extract the
10847 // relevant portion.
10848 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10849 }
10850 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10851 ResultVTs.push_back(ResultVT);
10852 ResultValues.push_back(V);
10853 };
10854
10855 // Deal with output operands.
10856 for (SDISelAsmOperandInfo &OpInfo : Info.ConstraintOperands) {
10857 if (OpInfo.Type == InlineAsm::isOutput) {
10858 SDValue Val;
10859 // Skip trivial output operands.
10860 if (OpInfo.AssignedRegs.Regs.empty())
10861 continue;
10862
10863 switch (OpInfo.ConstraintType) {
10866 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10867 Chain, &Glue, &Call);
10868 break;
10871 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10872 OpInfo, DAG);
10873 break;
10875 break; // Already handled.
10877 break; // Silence warning.
10879 assert(false && "Unexpected unknown constraint");
10880 }
10881
10882 // Indirect output manifest as stores. Record output chains.
10883 if (OpInfo.isIndirect) {
10884 const Value *Ptr = OpInfo.CallOperandVal;
10885 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10886 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10887 MachinePointerInfo(Ptr));
10888 OutChains.push_back(Store);
10889 } else {
10890 // generate CopyFromRegs to associated registers.
10891 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10892 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10893 for (const SDValue &V : Val->op_values())
10894 handleRegAssign(V);
10895 } else
10896 handleRegAssign(Val);
10897 }
10898 }
10899 }
10900
10901 // Set results.
10902 if (!ResultValues.empty()) {
10903 assert(CurResultType == ResultTypes.end() &&
10904 "Mismatch in number of ResultTypes");
10905 assert(ResultValues.size() == ResultTypes.size() &&
10906 "Mismatch in number of output operands in asm result");
10907
10909 DAG.getVTList(ResultVTs), ResultValues);
10910 setValue(&Call, V);
10911 }
10912
10913 // Collect store chains.
10914 if (!OutChains.empty())
10915 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10916
10917 if (const auto *II = dyn_cast<InvokeInst>(&Call))
10918 Chain = lowerEndEH(Chain, II, EHPadBB, Info.BeginLabel);
10919
10920 // Only Update Root if inline assembly has a memory effect.
10921 if (ResultValues.empty() || Info.HasSideEffect || !OutChains.empty() ||
10922 IsCallBr || isa<InvokeInst>(Call))
10923 DAG.setRoot(Chain);
10924}
10925
10926void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10927 const Twine &Message) {
10928 LLVMContext &Ctx = *DAG.getContext();
10929 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10930
10931 // Make sure we leave the DAG in a valid state
10932 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10933 SmallVector<EVT, 1> ValueVTs;
10934 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10935
10936 if (ValueVTs.empty())
10937 return;
10938
10940 for (const EVT &VT : ValueVTs)
10941 Ops.push_back(DAG.getUNDEF(VT));
10942
10943 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10944}
10945
10946void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10947 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10948 MVT::Other, getRoot(),
10949 getValue(I.getArgOperand(0)),
10950 DAG.getSrcValue(I.getArgOperand(0))));
10951}
10952
10953void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10954 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10955 const DataLayout &DL = DAG.getDataLayout();
10956 SDValue V = DAG.getVAArg(
10957 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10958 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10959 DL.getABITypeAlign(I.getType()).value());
10960 DAG.setRoot(V.getValue(1));
10961
10962 if (I.getType()->isPointerTy())
10963 V = DAG.getPtrExtOrTrunc(
10964 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10965 setValue(&I, V);
10966}
10967
10968void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10969 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10970 MVT::Other, getRoot(),
10971 getValue(I.getArgOperand(0)),
10972 DAG.getSrcValue(I.getArgOperand(0))));
10973}
10974
10975void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10976 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10977 MVT::Other, getRoot(),
10978 getValue(I.getArgOperand(0)),
10979 getValue(I.getArgOperand(1)),
10980 DAG.getSrcValue(I.getArgOperand(0)),
10981 DAG.getSrcValue(I.getArgOperand(1))));
10982}
10983
10985 const Instruction &I,
10986 SDValue Op) {
10987 std::optional<ConstantRange> CR = getRange(I);
10988
10989 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10990 return Op;
10991
10992 APInt Hi = CR->getUnsignedMax();
10993 unsigned Bits = std::max(Hi.getActiveBits(),
10994 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10995
10996 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10997
10998 SDLoc SL = getCurSDLoc();
10999
11000 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
11001 DAG.getValueType(SmallVT));
11002 unsigned NumVals = Op.getNode()->getNumValues();
11003 if (NumVals == 1)
11004 return ZExt;
11005
11007
11008 Ops.push_back(ZExt);
11009 for (unsigned I = 1; I != NumVals; ++I)
11010 Ops.push_back(Op.getValue(I));
11011
11012 return DAG.getMergeValues(Ops, SL);
11013}
11014
11016 SelectionDAG &DAG, const Instruction &I, SDValue Op) {
11017 FPClassTest Classes = getNoFPClass(I);
11018 if (Classes == fcNone)
11019 return Op;
11020
11021 SDLoc SL = getCurSDLoc();
11022 SDValue TestConst = DAG.getTargetConstant(Classes, SDLoc(), MVT::i32);
11023
11024 if (Op.getOpcode() != ISD::MERGE_VALUES) {
11025 return DAG.getNode(ISD::AssertNoFPClass, SL, Op.getValueType(), Op,
11026 TestConst);
11027 }
11028
11029 SmallVector<SDValue, 8> Ops(Op.getNumOperands());
11030 for (unsigned I = 0, E = Ops.size(); I != E; ++I) {
11031 SDValue MergeOp = Op.getOperand(I);
11032 Ops[I] = DAG.getNode(ISD::AssertNoFPClass, SL, MergeOp.getValueType(),
11033 MergeOp, TestConst);
11034 }
11035
11036 return DAG.getMergeValues(Ops, SL);
11037}
11038
11039/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
11040/// the call being lowered.
11041///
11042/// This is a helper for lowering intrinsics that follow a target calling
11043/// convention or require stack pointer adjustment. Only a subset of the
11044/// intrinsic's operands need to participate in the calling convention.
11047 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
11048 AttributeSet RetAttrs, bool IsPatchPoint) {
11050 Args.reserve(NumArgs);
11051
11052 // Populate the argument list.
11053 // Attributes for args start at offset 1, after the return attribute.
11054 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
11055 ArgI != ArgE; ++ArgI) {
11056 const Value *V = Call->getOperand(ArgI);
11057
11058 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
11059
11060 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
11061 Entry.setAttributes(Call, ArgI);
11062 Args.push_back(Entry);
11063 }
11064
11066 .setChain(getRoot())
11067 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
11068 RetAttrs)
11069 .setDiscardResult(Call->use_empty())
11070 .setIsPatchPoint(IsPatchPoint)
11072 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
11073}
11074
11075/// Add a stack map intrinsic call's live variable operands to a stackmap
11076/// or patchpoint target node's operand list.
11077///
11078/// Constants are converted to TargetConstants purely as an optimization to
11079/// avoid constant materialization and register allocation.
11080///
11081/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
11082/// generate addess computation nodes, and so FinalizeISel can convert the
11083/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
11084/// address materialization and register allocation, but may also be required
11085/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
11086/// alloca in the entry block, then the runtime may assume that the alloca's
11087/// StackMap location can be read immediately after compilation and that the
11088/// location is valid at any point during execution (this is similar to the
11089/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
11090/// only available in a register, then the runtime would need to trap when
11091/// execution reaches the StackMap in order to read the alloca's location.
11092static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
11094 SelectionDAGBuilder &Builder) {
11095 SelectionDAG &DAG = Builder.DAG;
11096 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
11097 SDValue Op = Builder.getValue(Call.getArgOperand(I));
11098
11099 // Things on the stack are pointer-typed, meaning that they are already
11100 // legal and can be emitted directly to target nodes.
11102 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
11103 } else {
11104 // Otherwise emit a target independent node to be legalised.
11105 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
11106 }
11107 }
11108}
11109
11110/// Lower llvm.experimental.stackmap.
11111void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
11112 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
11113 // [live variables...])
11114
11115 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
11116
11117 SDValue Chain, InGlue, Callee;
11119
11120 SDLoc DL = getCurSDLoc();
11122
11123 // The stackmap intrinsic only records the live variables (the arguments
11124 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
11125 // intrinsic, this won't be lowered to a function call. This means we don't
11126 // have to worry about calling conventions and target specific lowering code.
11127 // Instead we perform the call lowering right here.
11128 //
11129 // chain, flag = CALLSEQ_START(chain, 0, 0)
11130 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
11131 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
11132 //
11133 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
11134 InGlue = Chain.getValue(1);
11135
11136 // Add the STACKMAP operands, starting with DAG house-keeping.
11137 Ops.push_back(Chain);
11138 Ops.push_back(InGlue);
11139
11140 // Add the <id>, <numShadowBytes> operands.
11141 //
11142 // These do not require legalisation, and can be emitted directly to target
11143 // constant nodes.
11145 assert(ID.getValueType() == MVT::i64);
11146 SDValue IDConst =
11147 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
11148 Ops.push_back(IDConst);
11149
11150 SDValue Shad = getValue(CI.getArgOperand(1));
11151 assert(Shad.getValueType() == MVT::i32);
11152 SDValue ShadConst =
11153 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
11154 Ops.push_back(ShadConst);
11155
11156 // Add the live variables.
11157 addStackMapLiveVars(CI, 2, DL, Ops, *this);
11158
11159 // Create the STACKMAP node.
11160 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11161 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
11162 InGlue = Chain.getValue(1);
11163
11164 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
11165
11166 // Stackmaps don't generate values, so nothing goes into the NodeMap.
11167
11168 // Set the root to the target-lowered call chain.
11169 DAG.setRoot(Chain);
11170
11171 // Inform the Frame Information that we have a stackmap in this function.
11172 FuncInfo.MF->getFrameInfo().setHasStackMap();
11173}
11174
11175/// Lower llvm.experimental.patchpoint directly to its target opcode.
11176void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
11177 const BasicBlock *EHPadBB) {
11178 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
11179 // i32 <numBytes>,
11180 // i8* <target>,
11181 // i32 <numArgs>,
11182 // [Args...],
11183 // [live variables...])
11184
11186 bool IsAnyRegCC = CC == CallingConv::AnyReg;
11187 bool HasDef = !CB.getType()->isVoidTy();
11188 SDLoc dl = getCurSDLoc();
11190
11191 // Handle immediate and symbolic callees.
11192 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
11193 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
11194 /*isTarget=*/true);
11195 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
11196 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
11197 SDLoc(SymbolicCallee),
11198 SymbolicCallee->getValueType(0));
11199
11200 // Get the real number of arguments participating in the call <numArgs>
11202 unsigned NumArgs = NArgVal->getAsZExtVal();
11203
11204 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
11205 // Intrinsics include all meta-operands up to but not including CC.
11206 unsigned NumMetaOpers = PatchPointOpers::CCPos;
11207 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
11208 "Not enough arguments provided to the patchpoint intrinsic");
11209
11210 // For AnyRegCC the arguments are lowered later on manually.
11211 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
11212 Type *ReturnTy =
11213 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
11214
11215 TargetLowering::CallLoweringInfo CLI(DAG);
11216 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
11217 ReturnTy, CB.getAttributes().getRetAttrs(), true);
11218 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
11219
11220 SDNode *CallEnd = Result.second.getNode();
11221 if (CallEnd->getOpcode() == ISD::EH_LABEL)
11222 CallEnd = CallEnd->getOperand(0).getNode();
11223 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
11224 CallEnd = CallEnd->getOperand(0).getNode();
11225
11226 /// Get a call instruction from the call sequence chain.
11227 /// Tail calls are not allowed.
11228 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
11229 "Expected a callseq node.");
11230 SDNode *Call = CallEnd->getOperand(0).getNode();
11231 bool HasGlue = Call->getGluedNode();
11232
11233 // Replace the target specific call node with the patchable intrinsic.
11235
11236 // Push the chain.
11237 Ops.push_back(*(Call->op_begin()));
11238
11239 // Optionally, push the glue (if any).
11240 if (HasGlue)
11241 Ops.push_back(*(Call->op_end() - 1));
11242
11243 // Push the register mask info.
11244 if (HasGlue)
11245 Ops.push_back(*(Call->op_end() - 2));
11246 else
11247 Ops.push_back(*(Call->op_end() - 1));
11248
11249 // Add the <id> and <numBytes> constants.
11251 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
11253 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
11254
11255 // Add the callee.
11256 Ops.push_back(Callee);
11257
11258 // Adjust <numArgs> to account for any arguments that have been passed on the
11259 // stack instead.
11260 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
11261 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
11262 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
11263 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
11264
11265 // Add the calling convention
11266 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
11267
11268 // Add the arguments we omitted previously. The register allocator should
11269 // place these in any free register.
11270 if (IsAnyRegCC)
11271 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
11272 Ops.push_back(getValue(CB.getArgOperand(i)));
11273
11274 // Push the arguments from the call instruction.
11275 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
11276 Ops.append(Call->op_begin() + 2, e);
11277
11278 // Push live variables for the stack map.
11279 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
11280
11281 SDVTList NodeTys;
11282 if (IsAnyRegCC && HasDef) {
11283 // Create the return types based on the intrinsic definition
11284 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11285 SmallVector<EVT, 3> ValueVTs;
11286 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
11287 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
11288
11289 // There is always a chain and a glue type at the end
11290 ValueVTs.push_back(MVT::Other);
11291 ValueVTs.push_back(MVT::Glue);
11292 NodeTys = DAG.getVTList(ValueVTs);
11293 } else
11294 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11295
11296 // Replace the target specific call node with a PATCHPOINT node.
11297 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
11298
11299 // Update the NodeMap.
11300 if (HasDef) {
11301 if (IsAnyRegCC)
11302 setValue(&CB, SDValue(PPV.getNode(), 0));
11303 else
11304 setValue(&CB, Result.first);
11305 }
11306
11307 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
11308 // call sequence. Furthermore the location of the chain and glue can change
11309 // when the AnyReg calling convention is used and the intrinsic returns a
11310 // value.
11311 if (IsAnyRegCC && HasDef) {
11312 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
11313 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
11314 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
11315 } else
11316 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
11317 DAG.DeleteNode(Call);
11318
11319 // Inform the Frame Information that we have a patchpoint in this function.
11320 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
11321}
11322
11323void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
11324 unsigned Intrinsic) {
11325 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11326 SDValue Op1 = getValue(I.getArgOperand(0));
11327 SDValue Op2;
11328 if (I.arg_size() > 1)
11329 Op2 = getValue(I.getArgOperand(1));
11330 SDLoc dl = getCurSDLoc();
11331 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11332 SDValue Res;
11333 SDNodeFlags SDFlags;
11334 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
11335 SDFlags.copyFMF(*FPMO);
11336
11337 switch (Intrinsic) {
11338 case Intrinsic::vector_reduce_fadd:
11339 if (SDFlags.hasAllowReassociation())
11340 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
11341 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
11342 SDFlags);
11343 else
11344 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
11345 break;
11346 case Intrinsic::vector_reduce_fmul:
11347 if (SDFlags.hasAllowReassociation())
11348 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
11349 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
11350 SDFlags);
11351 else
11352 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
11353 break;
11354 case Intrinsic::vector_reduce_add:
11355 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
11356 break;
11357 case Intrinsic::vector_reduce_mul:
11358 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
11359 break;
11360 case Intrinsic::vector_reduce_and:
11361 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
11362 break;
11363 case Intrinsic::vector_reduce_or:
11364 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
11365 break;
11366 case Intrinsic::vector_reduce_xor:
11367 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
11368 break;
11369 case Intrinsic::vector_reduce_smax:
11370 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
11371 break;
11372 case Intrinsic::vector_reduce_smin:
11373 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
11374 break;
11375 case Intrinsic::vector_reduce_umax:
11376 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
11377 break;
11378 case Intrinsic::vector_reduce_umin:
11379 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
11380 break;
11381 case Intrinsic::vector_reduce_fmax:
11382 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
11383 break;
11384 case Intrinsic::vector_reduce_fmin:
11385 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
11386 break;
11387 case Intrinsic::vector_reduce_fmaximum:
11388 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
11389 break;
11390 case Intrinsic::vector_reduce_fminimum:
11391 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11392 break;
11393 default:
11394 llvm_unreachable("Unhandled vector reduce intrinsic");
11395 }
11396 setValue(&I, Res);
11397}
11398
11399/// Returns an AttributeList representing the attributes applied to the return
11400/// value of the given call.
11403 if (CLI.RetSExt)
11404 Attrs.push_back(Attribute::SExt);
11405 if (CLI.RetZExt)
11406 Attrs.push_back(Attribute::ZExt);
11407 if (CLI.IsInReg)
11408 Attrs.push_back(Attribute::InReg);
11409
11410 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11411 Attrs);
11412}
11413
11414/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11415/// implementation, which just calls LowerCall.
11416/// FIXME: When all targets are
11417/// migrated to using LowerCall, this hook should be integrated into SDISel.
11418std::pair<SDValue, SDValue>
11420 LLVMContext &Context = CLI.RetTy->getContext();
11421
11422 // Handle the incoming return values from the call.
11423 CLI.Ins.clear();
11424 SmallVector<Type *, 4> RetOrigTys;
11426 auto &DL = CLI.DAG.getDataLayout();
11427 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11428
11429 SmallVector<EVT, 4> RetVTs;
11430 if (CLI.RetTy != CLI.OrigRetTy) {
11431 assert(RetOrigTys.size() == 1 &&
11432 "Only supported for non-aggregate returns");
11433 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11434 } else {
11435 for (Type *Ty : RetOrigTys)
11436 RetVTs.push_back(getValueType(DL, Ty));
11437 }
11438
11439 if (CLI.IsPostTypeLegalization) {
11440 // If we are lowering a libcall after legalization, split the return type.
11441 SmallVector<Type *, 4> OldRetOrigTys;
11442 SmallVector<EVT, 4> OldRetVTs;
11443 SmallVector<TypeSize, 4> OldOffsets;
11444 RetOrigTys.swap(OldRetOrigTys);
11445 RetVTs.swap(OldRetVTs);
11446 Offsets.swap(OldOffsets);
11447
11448 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11449 EVT RetVT = OldRetVTs[i];
11450 uint64_t Offset = OldOffsets[i];
11451 MVT RegisterVT = getRegisterType(Context, RetVT);
11452 unsigned NumRegs = getNumRegisters(Context, RetVT);
11453 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11454 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11455 RetVTs.append(NumRegs, RegisterVT);
11456 for (unsigned j = 0; j != NumRegs; ++j)
11457 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11458 }
11459 }
11460
11462 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11463
11464 bool CanLowerReturn =
11466 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11467
11468 SDValue DemoteStackSlot;
11469 int DemoteStackIdx = -100;
11470 if (!CanLowerReturn) {
11471 // FIXME: equivalent assert?
11472 // assert(!CS.hasInAllocaArgument() &&
11473 // "sret demotion is incompatible with inalloca");
11474 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11475 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11477 DemoteStackIdx =
11478 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11479 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11480
11481 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11482 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11483 Entry.IsSRet = true;
11484 Entry.Alignment = Alignment;
11485 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11486 CLI.NumFixedArgs += 1;
11487 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11488 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11489
11490 // sret demotion isn't compatible with tail-calls, since the sret argument
11491 // points into the callers stack frame.
11492 CLI.IsTailCall = false;
11493 } else {
11494 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11495 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11496 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11497 ISD::ArgFlagsTy Flags;
11498 if (NeedsRegBlock) {
11499 Flags.setInConsecutiveRegs();
11500 if (I == RetVTs.size() - 1)
11501 Flags.setInConsecutiveRegsLast();
11502 }
11503 EVT VT = RetVTs[I];
11504 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11505 unsigned NumRegs =
11506 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11507 for (unsigned i = 0; i != NumRegs; ++i) {
11508 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11510 if (CLI.RetTy->isPointerTy()) {
11511 Ret.Flags.setPointer();
11513 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11514 }
11515 if (CLI.RetSExt)
11516 Ret.Flags.setSExt();
11517 if (CLI.RetZExt)
11518 Ret.Flags.setZExt();
11519 if (CLI.IsInReg)
11520 Ret.Flags.setInReg();
11521 CLI.Ins.push_back(Ret);
11522 }
11523 }
11524 }
11525
11526 // We push in swifterror return as the last element of CLI.Ins.
11527 ArgListTy &Args = CLI.getArgs();
11528 if (supportSwiftError()) {
11529 for (const ArgListEntry &Arg : Args) {
11530 if (Arg.IsSwiftError) {
11531 ISD::ArgFlagsTy Flags;
11532 Flags.setSwiftError();
11534 PointerType::getUnqual(Context),
11535 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11536 CLI.Ins.push_back(Ret);
11537 }
11538 }
11539 }
11540
11541 // Handle all of the outgoing arguments.
11542 CLI.Outs.clear();
11543 CLI.OutVals.clear();
11544 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11545 SmallVector<Type *, 4> OrigArgTys;
11546 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11547 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11548 Type *FinalType = Args[i].Ty;
11549 if (Args[i].IsByVal)
11550 FinalType = Args[i].IndirectType;
11551 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11552 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11553 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11554 ++Value) {
11555 Type *OrigArgTy = OrigArgTys[Value];
11556 Type *ArgTy = OrigArgTy;
11557 if (Args[i].Ty != Args[i].OrigTy) {
11558 assert(Value == 0 && "Only supported for non-aggregate arguments");
11559 ArgTy = Args[i].Ty;
11560 }
11561
11562 EVT VT = getValueType(DL, ArgTy);
11563 SDValue Op = SDValue(Args[i].Node.getNode(),
11564 Args[i].Node.getResNo() + Value);
11565 ISD::ArgFlagsTy Flags;
11566
11567 // Certain targets (such as MIPS), may have a different ABI alignment
11568 // for a type depending on the context. Give the target a chance to
11569 // specify the alignment it wants.
11570 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11571 Flags.setOrigAlign(OriginalAlignment);
11572
11573 if (i >= CLI.NumFixedArgs)
11574 Flags.setVarArg();
11575 if (ArgTy->isPointerTy()) {
11576 Flags.setPointer();
11577 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11578 }
11579 if (Args[i].IsZExt)
11580 Flags.setZExt();
11581 if (Args[i].IsSExt)
11582 Flags.setSExt();
11583 if (Args[i].IsNoExt)
11584 Flags.setNoExt();
11585 if (Args[i].IsInReg) {
11586 // If we are using vectorcall calling convention, a structure that is
11587 // passed InReg - is surely an HVA
11589 isa<StructType>(FinalType)) {
11590 // The first value of a structure is marked
11591 if (0 == Value)
11592 Flags.setHvaStart();
11593 Flags.setHva();
11594 }
11595 // Set InReg Flag
11596 Flags.setInReg();
11597 }
11598 if (Args[i].IsSRet)
11599 Flags.setSRet();
11600 if (Args[i].IsSwiftSelf)
11601 Flags.setSwiftSelf();
11602 if (Args[i].IsSwiftAsync)
11603 Flags.setSwiftAsync();
11604 if (Args[i].IsSwiftError)
11605 Flags.setSwiftError();
11606 if (Args[i].IsCFGuardTarget)
11607 Flags.setCFGuardTarget();
11608 if (Args[i].IsByVal)
11609 Flags.setByVal();
11610 if (Args[i].IsByRef)
11611 Flags.setByRef();
11612 if (Args[i].IsPreallocated) {
11613 Flags.setPreallocated();
11614 // Set the byval flag for CCAssignFn callbacks that don't know about
11615 // preallocated. This way we can know how many bytes we should've
11616 // allocated and how many bytes a callee cleanup function will pop. If
11617 // we port preallocated to more targets, we'll have to add custom
11618 // preallocated handling in the various CC lowering callbacks.
11619 Flags.setByVal();
11620 }
11621 if (Args[i].IsInAlloca) {
11622 Flags.setInAlloca();
11623 // Set the byval flag for CCAssignFn callbacks that don't know about
11624 // inalloca. This way we can know how many bytes we should've allocated
11625 // and how many bytes a callee cleanup function will pop. If we port
11626 // inalloca to more targets, we'll have to add custom inalloca handling
11627 // in the various CC lowering callbacks.
11628 Flags.setByVal();
11629 }
11630 Align MemAlign;
11631 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11632 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11633 Flags.setByValSize(FrameSize);
11634
11635 // info is not there but there are cases it cannot get right.
11636 if (auto MA = Args[i].Alignment)
11637 MemAlign = *MA;
11638 else
11639 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11640 } else if (auto MA = Args[i].Alignment) {
11641 MemAlign = *MA;
11642 } else {
11643 MemAlign = OriginalAlignment;
11644 }
11645 Flags.setMemAlign(MemAlign);
11646 if (Args[i].IsNest)
11647 Flags.setNest();
11648 if (NeedsRegBlock)
11649 Flags.setInConsecutiveRegs();
11650
11651 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11652 unsigned NumParts =
11653 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11654 SmallVector<SDValue, 4> Parts(NumParts);
11655 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11656
11657 if (Args[i].IsSExt)
11658 ExtendKind = ISD::SIGN_EXTEND;
11659 else if (Args[i].IsZExt)
11660 ExtendKind = ISD::ZERO_EXTEND;
11661
11662 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11663 // for now.
11664 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11666 assert((CLI.RetTy == Args[i].Ty ||
11667 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11669 Args[i].Ty->getPointerAddressSpace())) &&
11670 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11671 // Before passing 'returned' to the target lowering code, ensure that
11672 // either the register MVT and the actual EVT are the same size or that
11673 // the return value and argument are extended in the same way; in these
11674 // cases it's safe to pass the argument register value unchanged as the
11675 // return register value (although it's at the target's option whether
11676 // to do so)
11677 // TODO: allow code generation to take advantage of partially preserved
11678 // registers rather than clobbering the entire register when the
11679 // parameter extension method is not compatible with the return
11680 // extension method
11681 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11682 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11683 CLI.RetZExt == Args[i].IsZExt))
11684 Flags.setReturned();
11685 }
11686
11687 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11688 CLI.CallConv, ExtendKind);
11689
11690 for (unsigned j = 0; j != NumParts; ++j) {
11691 // if it isn't first piece, alignment must be 1
11692 // For scalable vectors the scalable part is currently handled
11693 // by individual targets, so we just use the known minimum size here.
11694 ISD::OutputArg MyFlags(
11695 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11696 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11697 if (NumParts > 1 && j == 0)
11698 MyFlags.Flags.setSplit();
11699 else if (j != 0) {
11700 MyFlags.Flags.setOrigAlign(Align(1));
11701 if (j == NumParts - 1)
11702 MyFlags.Flags.setSplitEnd();
11703 }
11704
11705 CLI.Outs.push_back(MyFlags);
11706 CLI.OutVals.push_back(Parts[j]);
11707 }
11708
11709 if (NeedsRegBlock && Value == NumValues - 1)
11710 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11711 }
11712 }
11713
11715 CLI.Chain = LowerCall(CLI, InVals);
11716
11717 // Update CLI.InVals to use outside of this function.
11718 CLI.InVals = InVals;
11719
11720 // Verify that the target's LowerCall behaved as expected.
11721 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11722 "LowerCall didn't return a valid chain!");
11723 assert((!CLI.IsTailCall || InVals.empty()) &&
11724 "LowerCall emitted a return value for a tail call!");
11725 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11726 "LowerCall didn't emit the correct number of values!");
11727
11728 // For a tail call, the return value is merely live-out and there aren't
11729 // any nodes in the DAG representing it. Return a special value to
11730 // indicate that a tail call has been emitted and no more Instructions
11731 // should be processed in the current block.
11732 if (CLI.IsTailCall) {
11733 CLI.DAG.setRoot(CLI.Chain);
11734 return std::make_pair(SDValue(), SDValue());
11735 }
11736
11737#ifndef NDEBUG
11738 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11739 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11740 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11741 "LowerCall emitted a value with the wrong type!");
11742 }
11743#endif
11744
11745 SmallVector<SDValue, 4> ReturnValues;
11746 if (!CanLowerReturn) {
11747 // The instruction result is the result of loading from the
11748 // hidden sret parameter.
11749 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11750
11751 unsigned NumValues = RetVTs.size();
11752 ReturnValues.resize(NumValues);
11753 SmallVector<SDValue, 4> Chains(NumValues);
11754
11755 // An aggregate return value cannot wrap around the address space, so
11756 // offsets to its parts don't wrap either.
11758 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11759 for (unsigned i = 0; i < NumValues; ++i) {
11761 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11763 SDValue L = CLI.DAG.getLoad(
11764 RetVTs[i], CLI.DL, CLI.Chain, Add,
11766 DemoteStackIdx, Offsets[i]),
11767 HiddenSRetAlign);
11768 ReturnValues[i] = L;
11769 Chains[i] = L.getValue(1);
11770 }
11771
11772 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11773 } else {
11774 // Collect the legal value parts into potentially illegal values
11775 // that correspond to the original function's return values.
11776 std::optional<ISD::NodeType> AssertOp;
11777 if (CLI.RetSExt)
11778 AssertOp = ISD::AssertSext;
11779 else if (CLI.RetZExt)
11780 AssertOp = ISD::AssertZext;
11781 unsigned CurReg = 0;
11782 for (EVT VT : RetVTs) {
11783 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11784 unsigned NumRegs =
11785 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11786
11787 ReturnValues.push_back(getCopyFromParts(
11788 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11789 CLI.Chain, CLI.CallConv, AssertOp));
11790 CurReg += NumRegs;
11791 }
11792
11793 // For a function returning void, there is no return value. We can't create
11794 // such a node, so we just return a null return value in that case. In
11795 // that case, nothing will actually look at the value.
11796 if (ReturnValues.empty())
11797 return std::make_pair(SDValue(), CLI.Chain);
11798 }
11799
11800 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11801 CLI.DAG.getVTList(RetVTs), ReturnValues);
11802 return std::make_pair(Res, CLI.Chain);
11803}
11804
11805/// Places new result values for the node in Results (their number
11806/// and types must exactly match those of the original return values of
11807/// the node), or leaves Results empty, which indicates that the node is not
11808/// to be custom lowered after all.
11811 SelectionDAG &DAG) const {
11812 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11813
11814 if (!Res.getNode())
11815 return;
11816
11817 // If the original node has one result, take the return value from
11818 // LowerOperation as is. It might not be result number 0.
11819 if (N->getNumValues() == 1) {
11820 Results.push_back(Res);
11821 return;
11822 }
11823
11824 // If the original node has multiple results, then the return node should
11825 // have the same number of results.
11826 assert((N->getNumValues() == Res->getNumValues()) &&
11827 "Lowering returned the wrong number of results!");
11828
11829 // Places new result values base on N result number.
11830 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11831 Results.push_back(Res.getValue(I));
11832}
11833
11835 llvm_unreachable("LowerOperation not implemented for this target!");
11836}
11837
11839 Register Reg,
11840 ISD::NodeType ExtendType) {
11842 assert((Op.getOpcode() != ISD::CopyFromReg ||
11843 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11844 "Copy from a reg to the same reg!");
11845 assert(!Reg.isPhysical() && "Is a physreg");
11846
11847 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11848 // If this is an InlineAsm we have to match the registers required, not the
11849 // notional registers required by the type.
11850
11851 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11852 std::nullopt); // This is not an ABI copy.
11853 SDValue Chain = DAG.getEntryNode();
11854
11855 if (ExtendType == ISD::ANY_EXTEND) {
11856 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11857 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11858 ExtendType = PreferredExtendIt->second;
11859 }
11860 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11861 PendingExports.push_back(Chain);
11862}
11863
11865
11866/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11867/// entry block, return true. This includes arguments used by switches, since
11868/// the switch may expand into multiple basic blocks.
11869static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11870 // With FastISel active, we may be splitting blocks, so force creation
11871 // of virtual registers for all non-dead arguments.
11872 if (FastISel)
11873 return A->use_empty();
11874
11875 const BasicBlock &Entry = A->getParent()->front();
11876 for (const User *U : A->users())
11877 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11878 return false; // Use not in entry block.
11879
11880 return true;
11881}
11882
11884 DenseMap<const Argument *,
11885 std::pair<const AllocaInst *, const StoreInst *>>;
11886
11887/// Scan the entry block of the function in FuncInfo for arguments that look
11888/// like copies into a local alloca. Record any copied arguments in
11889/// ArgCopyElisionCandidates.
11890static void
11892 FunctionLoweringInfo *FuncInfo,
11893 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11894 // Record the state of every static alloca used in the entry block. Argument
11895 // allocas are all used in the entry block, so we need approximately as many
11896 // entries as we have arguments.
11897 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11899 unsigned NumArgs = FuncInfo->Fn->arg_size();
11900 StaticAllocas.reserve(NumArgs * 2);
11901
11902 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11903 if (!V)
11904 return nullptr;
11905 V = V->stripPointerCasts();
11906 const auto *AI = dyn_cast<AllocaInst>(V);
11907 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11908 return nullptr;
11909 auto Iter = StaticAllocas.insert({AI, Unknown});
11910 return &Iter.first->second;
11911 };
11912
11913 // Look for stores of arguments to static allocas. Look through bitcasts and
11914 // GEPs to handle type coercions, as long as the alloca is fully initialized
11915 // by the store. Any non-store use of an alloca escapes it and any subsequent
11916 // unanalyzed store might write it.
11917 // FIXME: Handle structs initialized with multiple stores.
11918 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11919 // Look for stores, and handle non-store uses conservatively.
11920 const auto *SI = dyn_cast<StoreInst>(&I);
11921 if (!SI) {
11922 // We will look through cast uses, so ignore them completely.
11923 if (I.isCast())
11924 continue;
11925 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11926 // to allocas.
11927 if (I.isDebugOrPseudoInst())
11928 continue;
11929 // This is an unknown instruction. Assume it escapes or writes to all
11930 // static alloca operands.
11931 for (const Use &U : I.operands()) {
11932 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11933 *Info = StaticAllocaInfo::Clobbered;
11934 }
11935 continue;
11936 }
11937
11938 // If the stored value is a static alloca, mark it as escaped.
11939 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11940 *Info = StaticAllocaInfo::Clobbered;
11941
11942 // Check if the destination is a static alloca.
11943 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11944 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11945 if (!Info)
11946 continue;
11947 const AllocaInst *AI = cast<AllocaInst>(Dst);
11948
11949 // Skip allocas that have been initialized or clobbered.
11950 if (*Info != StaticAllocaInfo::Unknown)
11951 continue;
11952
11953 // Check if the stored value is an argument, and that this store fully
11954 // initializes the alloca.
11955 // If the argument type has padding bits we can't directly forward a pointer
11956 // as the upper bits may contain garbage.
11957 // Don't elide copies from the same argument twice.
11958 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11959 const auto *Arg = dyn_cast<Argument>(Val);
11960 std::optional<TypeSize> AllocaSize = AI->getAllocationSize(DL);
11961 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11962 Arg->getType()->isEmptyTy() || !AllocaSize ||
11963 DL.getTypeStoreSize(Arg->getType()) != *AllocaSize ||
11964 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11965 ArgCopyElisionCandidates.count(Arg)) {
11966 *Info = StaticAllocaInfo::Clobbered;
11967 continue;
11968 }
11969
11970 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11971 << '\n');
11972
11973 // Mark this alloca and store for argument copy elision.
11974 *Info = StaticAllocaInfo::Elidable;
11975 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11976
11977 // Stop scanning if we've seen all arguments. This will happen early in -O0
11978 // builds, which is useful, because -O0 builds have large entry blocks and
11979 // many allocas.
11980 if (ArgCopyElisionCandidates.size() == NumArgs)
11981 break;
11982 }
11983}
11984
11985/// Try to elide argument copies from memory into a local alloca. Succeeds if
11986/// ArgVal is a load from a suitable fixed stack object.
11989 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11990 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11991 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11992 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11993 // Check if this is a load from a fixed stack object.
11994 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11995 if (!LNode)
11996 return;
11997 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11998 if (!FINode)
11999 return;
12000
12001 // Check that the fixed stack object is the right size and alignment.
12002 // Look at the alignment that the user wrote on the alloca instead of looking
12003 // at the stack object.
12004 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
12005 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
12006 const AllocaInst *AI = ArgCopyIter->second.first;
12007 int FixedIndex = FINode->getIndex();
12008 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
12009 int OldIndex = AllocaIndex;
12010 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
12011 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
12012 LLVM_DEBUG(
12013 dbgs() << " argument copy elision failed due to bad fixed stack "
12014 "object size\n");
12015 return;
12016 }
12017 Align RequiredAlignment = AI->getAlign();
12018 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
12019 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
12020 "greater than stack argument alignment ("
12021 << DebugStr(RequiredAlignment) << " vs "
12022 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
12023 return;
12024 }
12025
12026 // Perform the elision. Delete the old stack object and replace its only use
12027 // in the variable info map. Mark the stack object as mutable and aliased.
12028 LLVM_DEBUG({
12029 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
12030 << " Replacing frame index " << OldIndex << " with " << FixedIndex
12031 << '\n';
12032 });
12033 MFI.RemoveStackObject(OldIndex);
12034 MFI.setIsImmutableObjectIndex(FixedIndex, false);
12035 MFI.setIsAliasedObjectIndex(FixedIndex, true);
12036 AllocaIndex = FixedIndex;
12037 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
12038 for (SDValue ArgVal : ArgVals)
12039 Chains.push_back(ArgVal.getValue(1));
12040
12041 // Avoid emitting code for the store implementing the copy.
12042 const StoreInst *SI = ArgCopyIter->second.second;
12043 ElidedArgCopyInstrs.insert(SI);
12044
12045 // Check for uses of the argument again so that we can avoid exporting ArgVal
12046 // if it is't used by anything other than the store.
12047 for (const Value *U : Arg.users()) {
12048 if (U != SI) {
12049 ArgHasUses = true;
12050 break;
12051 }
12052 }
12053}
12054
12055void SelectionDAGISel::LowerArguments(const Function &F) {
12056 SelectionDAG &DAG = SDB->DAG;
12057 SDLoc dl = SDB->getCurSDLoc();
12058 const DataLayout &DL = DAG.getDataLayout();
12060
12061 // In Naked functions we aren't going to save any registers.
12062 if (F.hasFnAttribute(Attribute::Naked))
12063 return;
12064
12065 if (!FuncInfo->CanLowerReturn) {
12066 // Put in an sret pointer parameter before all the other parameters.
12067 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
12068
12069 ISD::ArgFlagsTy Flags;
12070 Flags.setSRet();
12071 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
12072 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
12074 Ins.push_back(RetArg);
12075 }
12076
12077 // Look for stores of arguments to static allocas. Mark such arguments with a
12078 // flag to ask the target to give us the memory location of that argument if
12079 // available.
12080 ArgCopyElisionMapTy ArgCopyElisionCandidates;
12082 ArgCopyElisionCandidates);
12083
12084 // Set up the incoming argument description vector.
12085 for (const Argument &Arg : F.args()) {
12086 unsigned ArgNo = Arg.getArgNo();
12088 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
12089 bool isArgValueUsed = !Arg.use_empty();
12090 Type *FinalType = Arg.getType();
12091 if (Arg.hasAttribute(Attribute::ByVal))
12092 FinalType = Arg.getParamByValType();
12093 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
12094 FinalType, F.getCallingConv(), F.isVarArg(), DL);
12095 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
12096 ++Value) {
12097 Type *ArgTy = Types[Value];
12098 EVT VT = TLI->getValueType(DL, ArgTy);
12099 ISD::ArgFlagsTy Flags;
12100
12101 if (ArgTy->isPointerTy()) {
12102 Flags.setPointer();
12103 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
12104 }
12105 if (Arg.hasAttribute(Attribute::ZExt))
12106 Flags.setZExt();
12107 if (Arg.hasAttribute(Attribute::SExt))
12108 Flags.setSExt();
12109 if (Arg.hasAttribute(Attribute::InReg)) {
12110 // If we are using vectorcall calling convention, a structure that is
12111 // passed InReg - is surely an HVA
12112 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
12113 isa<StructType>(Arg.getType())) {
12114 // The first value of a structure is marked
12115 if (0 == Value)
12116 Flags.setHvaStart();
12117 Flags.setHva();
12118 }
12119 // Set InReg Flag
12120 Flags.setInReg();
12121 }
12122 if (Arg.hasAttribute(Attribute::StructRet))
12123 Flags.setSRet();
12124 if (Arg.hasAttribute(Attribute::SwiftSelf))
12125 Flags.setSwiftSelf();
12126 if (Arg.hasAttribute(Attribute::SwiftAsync))
12127 Flags.setSwiftAsync();
12128 if (Arg.hasAttribute(Attribute::SwiftError))
12129 Flags.setSwiftError();
12130 if (Arg.hasAttribute(Attribute::ByVal))
12131 Flags.setByVal();
12132 if (Arg.hasAttribute(Attribute::ByRef))
12133 Flags.setByRef();
12134 if (Arg.hasAttribute(Attribute::InAlloca)) {
12135 Flags.setInAlloca();
12136 // Set the byval flag for CCAssignFn callbacks that don't know about
12137 // inalloca. This way we can know how many bytes we should've allocated
12138 // and how many bytes a callee cleanup function will pop. If we port
12139 // inalloca to more targets, we'll have to add custom inalloca handling
12140 // in the various CC lowering callbacks.
12141 Flags.setByVal();
12142 }
12143 if (Arg.hasAttribute(Attribute::Preallocated)) {
12144 Flags.setPreallocated();
12145 // Set the byval flag for CCAssignFn callbacks that don't know about
12146 // preallocated. This way we can know how many bytes we should've
12147 // allocated and how many bytes a callee cleanup function will pop. If
12148 // we port preallocated to more targets, we'll have to add custom
12149 // preallocated handling in the various CC lowering callbacks.
12150 Flags.setByVal();
12151 }
12152
12153 // Certain targets (such as MIPS), may have a different ABI alignment
12154 // for a type depending on the context. Give the target a chance to
12155 // specify the alignment it wants.
12156 const Align OriginalAlignment(
12157 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
12158 Flags.setOrigAlign(OriginalAlignment);
12159
12160 Align MemAlign;
12161 Type *ArgMemTy = nullptr;
12162 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
12163 Flags.isByRef()) {
12164 if (!ArgMemTy)
12165 ArgMemTy = Arg.getPointeeInMemoryValueType();
12166
12167 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
12168
12169 // For in-memory arguments, size and alignment should be passed from FE.
12170 // BE will guess if this info is not there but there are cases it cannot
12171 // get right.
12172 if (auto ParamAlign = Arg.getParamStackAlign())
12173 MemAlign = *ParamAlign;
12174 else if ((ParamAlign = Arg.getParamAlign()))
12175 MemAlign = *ParamAlign;
12176 else
12177 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
12178 if (Flags.isByRef())
12179 Flags.setByRefSize(MemSize);
12180 else
12181 Flags.setByValSize(MemSize);
12182 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
12183 MemAlign = *ParamAlign;
12184 } else {
12185 MemAlign = OriginalAlignment;
12186 }
12187 Flags.setMemAlign(MemAlign);
12188
12189 if (Arg.hasAttribute(Attribute::Nest))
12190 Flags.setNest();
12191 if (NeedsRegBlock)
12192 Flags.setInConsecutiveRegs();
12193 if (ArgCopyElisionCandidates.count(&Arg))
12194 Flags.setCopyElisionCandidate();
12195 if (Arg.hasAttribute(Attribute::Returned))
12196 Flags.setReturned();
12197
12198 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
12199 *CurDAG->getContext(), F.getCallingConv(), VT);
12200 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
12201 *CurDAG->getContext(), F.getCallingConv(), VT);
12202 for (unsigned i = 0; i != NumRegs; ++i) {
12203 // For scalable vectors, use the minimum size; individual targets
12204 // are responsible for handling scalable vector arguments and
12205 // return values.
12206 ISD::InputArg MyFlags(
12207 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
12208 i * RegisterVT.getStoreSize().getKnownMinValue());
12209 if (NumRegs > 1 && i == 0)
12210 MyFlags.Flags.setSplit();
12211 // if it isn't first piece, alignment must be 1
12212 else if (i > 0) {
12213 MyFlags.Flags.setOrigAlign(Align(1));
12214 if (i == NumRegs - 1)
12215 MyFlags.Flags.setSplitEnd();
12216 }
12217 Ins.push_back(MyFlags);
12218 }
12219 if (NeedsRegBlock && Value == NumValues - 1)
12220 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
12221 }
12222 }
12223
12224 // Call the target to set up the argument values.
12226 SDValue NewRoot = TLI->LowerFormalArguments(
12227 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
12228
12229 // Verify that the target's LowerFormalArguments behaved as expected.
12230 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
12231 "LowerFormalArguments didn't return a valid chain!");
12232 assert(InVals.size() == Ins.size() &&
12233 "LowerFormalArguments didn't emit the correct number of values!");
12234 assert(all_of(InVals, [](SDValue InVal) { return InVal.getNode(); }) &&
12235 "LowerFormalArguments emitted a null value!");
12236
12237 // Update the DAG with the new chain value resulting from argument lowering.
12238 DAG.setRoot(NewRoot);
12239
12240 // Set up the argument values.
12241 unsigned i = 0;
12242 if (!FuncInfo->CanLowerReturn) {
12243 // Create a virtual register for the sret pointer, and put in a copy
12244 // from the sret argument into it.
12245 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
12246 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
12247 std::optional<ISD::NodeType> AssertOp;
12248 SDValue ArgValue =
12249 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
12250 F.getCallingConv(), AssertOp);
12251
12252 MachineFunction& MF = SDB->DAG.getMachineFunction();
12253 MachineRegisterInfo& RegInfo = MF.getRegInfo();
12254 Register SRetReg =
12255 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
12256 FuncInfo->DemoteRegister = SRetReg;
12257 NewRoot =
12258 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
12259 DAG.setRoot(NewRoot);
12260
12261 // i indexes lowered arguments. Bump it past the hidden sret argument.
12262 ++i;
12263 }
12264
12266 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
12267 for (const Argument &Arg : F.args()) {
12268 SmallVector<SDValue, 4> ArgValues;
12269 SmallVector<EVT, 4> ValueVTs;
12270 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
12271 unsigned NumValues = ValueVTs.size();
12272 if (NumValues == 0)
12273 continue;
12274
12275 bool ArgHasUses = !Arg.use_empty();
12276
12277 // Elide the copying store if the target loaded this argument from a
12278 // suitable fixed stack object.
12279 if (Ins[i].Flags.isCopyElisionCandidate()) {
12280 unsigned NumParts = 0;
12281 for (EVT VT : ValueVTs)
12282 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
12283 F.getCallingConv(), VT);
12284
12285 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
12286 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
12287 ArrayRef(&InVals[i], NumParts), ArgHasUses);
12288 }
12289
12290 // If this argument is unused then remember its value. It is used to generate
12291 // debugging information.
12292 bool isSwiftErrorArg =
12293 TLI->supportSwiftError() &&
12294 Arg.hasAttribute(Attribute::SwiftError);
12295 if (!ArgHasUses && !isSwiftErrorArg) {
12296 SDB->setUnusedArgValue(&Arg, InVals[i]);
12297
12298 // Also remember any frame index for use in FastISel.
12299 if (FrameIndexSDNode *FI =
12301 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12302 }
12303
12304 for (unsigned Val = 0; Val != NumValues; ++Val) {
12305 EVT VT = ValueVTs[Val];
12306 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
12307 F.getCallingConv(), VT);
12308 unsigned NumParts = TLI->getNumRegistersForCallingConv(
12309 *CurDAG->getContext(), F.getCallingConv(), VT);
12310
12311 // Even an apparent 'unused' swifterror argument needs to be returned. So
12312 // we do generate a copy for it that can be used on return from the
12313 // function.
12314 if (ArgHasUses || isSwiftErrorArg) {
12315 std::optional<ISD::NodeType> AssertOp;
12316 if (Arg.hasAttribute(Attribute::SExt))
12317 AssertOp = ISD::AssertSext;
12318 else if (Arg.hasAttribute(Attribute::ZExt))
12319 AssertOp = ISD::AssertZext;
12320
12321 SDValue OutVal =
12322 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
12323 NewRoot, F.getCallingConv(), AssertOp);
12324
12325 FPClassTest NoFPClass = Arg.getNoFPClass();
12326 if (NoFPClass != fcNone) {
12327 SDValue SDNoFPClass = DAG.getTargetConstant(
12328 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
12329 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
12330 OutVal, SDNoFPClass);
12331 }
12332 ArgValues.push_back(OutVal);
12333 }
12334
12335 i += NumParts;
12336 }
12337
12338 // We don't need to do anything else for unused arguments.
12339 if (ArgValues.empty())
12340 continue;
12341
12342 // Note down frame index.
12343 if (FrameIndexSDNode *FI =
12344 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
12345 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12346
12347 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
12348 SDB->getCurSDLoc());
12349
12350 SDB->setValue(&Arg, Res);
12351 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
12352 // We want to associate the argument with the frame index, among
12353 // involved operands, that correspond to the lowest address. The
12354 // getCopyFromParts function, called earlier, is swapping the order of
12355 // the operands to BUILD_PAIR depending on endianness. The result of
12356 // that swapping is that the least significant bits of the argument will
12357 // be in the first operand of the BUILD_PAIR node, and the most
12358 // significant bits will be in the second operand.
12359 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
12360 if (LoadSDNode *LNode =
12361 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
12362 if (FrameIndexSDNode *FI =
12363 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
12364 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12365 }
12366
12367 // Analyses past this point are naive and don't expect an assertion.
12368 if (Res.getOpcode() == ISD::AssertZext)
12369 Res = Res.getOperand(0);
12370
12371 // Update the SwiftErrorVRegDefMap.
12372 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
12373 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12374 if (Reg.isVirtual())
12375 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
12376 Reg);
12377 }
12378
12379 // If this argument is live outside of the entry block, insert a copy from
12380 // wherever we got it to the vreg that other BB's will reference it as.
12381 if (Res.getOpcode() == ISD::CopyFromReg) {
12382 // If we can, though, try to skip creating an unnecessary vreg.
12383 // FIXME: This isn't very clean... it would be nice to make this more
12384 // general.
12385 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12386 if (Reg.isVirtual()) {
12387 FuncInfo->ValueMap[&Arg] = Reg;
12388 continue;
12389 }
12390 }
12391 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12392 FuncInfo->InitializeRegForValue(&Arg);
12393 SDB->CopyToExportRegsIfNeeded(&Arg);
12394 }
12395 }
12396
12397 if (!Chains.empty()) {
12398 Chains.push_back(NewRoot);
12399 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12400 }
12401
12402 DAG.setRoot(NewRoot);
12403
12404 assert(i == InVals.size() && "Argument register count mismatch!");
12405
12406 // If any argument copy elisions occurred and we have debug info, update the
12407 // stale frame indices used in the dbg.declare variable info table.
12408 if (!ArgCopyElisionFrameIndexMap.empty()) {
12409 for (MachineFunction::VariableDbgInfo &VI :
12410 MF->getInStackSlotVariableDbgInfo()) {
12411 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12412 if (I != ArgCopyElisionFrameIndexMap.end())
12413 VI.updateStackSlot(I->second);
12414 }
12415 }
12416
12417 // Finally, if the target has anything special to do, allow it to do so.
12419}
12420
12421/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12422/// ensure constants are generated when needed. Remember the virtual registers
12423/// that need to be added to the Machine PHI nodes as input. We cannot just
12424/// directly add them, because expansion might result in multiple MBB's for one
12425/// BB. As such, the start of the BB might correspond to a different MBB than
12426/// the end.
12427void
12428SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12429 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12430
12431 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12432
12433 // Check PHI nodes in successors that expect a value to be available from this
12434 // block.
12435 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12436 if (!isa<PHINode>(SuccBB->begin())) continue;
12437 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12438
12439 // If this terminator has multiple identical successors (common for
12440 // switches), only handle each succ once.
12441 if (!SuccsHandled.insert(SuccMBB).second)
12442 continue;
12443
12445
12446 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12447 // nodes and Machine PHI nodes, but the incoming operands have not been
12448 // emitted yet.
12449 for (const PHINode &PN : SuccBB->phis()) {
12450 // Ignore dead phi's.
12451 if (PN.use_empty())
12452 continue;
12453
12454 // Skip empty types
12455 if (PN.getType()->isEmptyTy())
12456 continue;
12457
12458 Register Reg;
12459 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12460
12461 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12462 Register &RegOut = ConstantsOut[C];
12463 if (!RegOut) {
12464 RegOut = FuncInfo.CreateRegs(&PN);
12465 // We need to zero/sign extend ConstantInt phi operands to match
12466 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12467 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12468 if (auto *CI = dyn_cast<ConstantInt>(C))
12469 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12471 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12472 }
12473 Reg = RegOut;
12474 } else {
12475 auto I = FuncInfo.ValueMap.find(PHIOp);
12476 if (I != FuncInfo.ValueMap.end())
12477 Reg = I->second;
12478 else {
12479 assert(isa<AllocaInst>(PHIOp) &&
12480 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12481 "Didn't codegen value into a register!??");
12482 Reg = FuncInfo.CreateRegs(&PN);
12484 }
12485 }
12486
12487 // Remember that this register needs to added to the machine PHI node as
12488 // the input for this MBB.
12489 SmallVector<EVT, 4> ValueVTs;
12490 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12491 for (EVT VT : ValueVTs) {
12492 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12493 for (unsigned i = 0; i != NumRegisters; ++i)
12494 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12495 Reg += NumRegisters;
12496 }
12497 }
12498 }
12499
12500 ConstantsOut.clear();
12501}
12502
12503MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12505 if (++I == FuncInfo.MF->end())
12506 return nullptr;
12507 return &*I;
12508}
12509
12510/// During lowering new call nodes can be created (such as memset, etc.).
12511/// Those will become new roots of the current DAG, but complications arise
12512/// when they are tail calls. In such cases, the call lowering will update
12513/// the root, but the builder still needs to know that a tail call has been
12514/// lowered in order to avoid generating an additional return.
12515void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12516 // If the node is null, we do have a tail call.
12517 if (MaybeTC.getNode() != nullptr)
12518 DAG.setRoot(MaybeTC);
12519 else
12520 HasTailCall = true;
12521}
12522
12523void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12524 MachineBasicBlock *SwitchMBB,
12525 MachineBasicBlock *DefaultMBB) {
12526 MachineFunction *CurMF = FuncInfo.MF;
12527 MachineBasicBlock *NextMBB = nullptr;
12529 if (++BBI != FuncInfo.MF->end())
12530 NextMBB = &*BBI;
12531
12532 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12533
12534 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12535
12536 if (Size == 2 && W.MBB == SwitchMBB) {
12537 // If any two of the cases has the same destination, and if one value
12538 // is the same as the other, but has one bit unset that the other has set,
12539 // use bit manipulation to do two compares at once. For example:
12540 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12541 // TODO: This could be extended to merge any 2 cases in switches with 3
12542 // cases.
12543 // TODO: Handle cases where W.CaseBB != SwitchBB.
12544 CaseCluster &Small = *W.FirstCluster;
12545 CaseCluster &Big = *W.LastCluster;
12546
12547 if (Small.Low == Small.High && Big.Low == Big.High &&
12548 Small.MBB == Big.MBB) {
12549 const APInt &SmallValue = Small.Low->getValue();
12550 const APInt &BigValue = Big.Low->getValue();
12551
12552 // Check that there is only one bit different.
12553 APInt CommonBit = BigValue ^ SmallValue;
12554 if (CommonBit.isPowerOf2()) {
12555 SDValue CondLHS = getValue(Cond);
12556 EVT VT = CondLHS.getValueType();
12557 SDLoc DL = getCurSDLoc();
12558
12559 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12560 DAG.getConstant(CommonBit, DL, VT));
12561 SDValue Cond = DAG.getSetCC(
12562 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12563 ISD::SETEQ);
12564
12565 // Update successor info.
12566 // Both Small and Big will jump to Small.BB, so we sum up the
12567 // probabilities.
12568 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12569 if (BPI)
12570 addSuccessorWithProb(
12571 SwitchMBB, DefaultMBB,
12572 // The default destination is the first successor in IR.
12573 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12574 else
12575 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12576
12577 // Insert the true branch.
12578 SDValue BrCond =
12579 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12580 DAG.getBasicBlock(Small.MBB));
12581 // Insert the false branch.
12582 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12583 DAG.getBasicBlock(DefaultMBB));
12584
12585 DAG.setRoot(BrCond);
12586 return;
12587 }
12588 }
12589 }
12590
12591 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12592 // Here, we order cases by probability so the most likely case will be
12593 // checked first. However, two clusters can have the same probability in
12594 // which case their relative ordering is non-deterministic. So we use Low
12595 // as a tie-breaker as clusters are guaranteed to never overlap.
12596 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12597 [](const CaseCluster &a, const CaseCluster &b) {
12598 return a.Prob != b.Prob ?
12599 a.Prob > b.Prob :
12600 a.Low->getValue().slt(b.Low->getValue());
12601 });
12602
12603 // Rearrange the case blocks so that the last one falls through if possible
12604 // without changing the order of probabilities.
12605 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12606 --I;
12607 if (I->Prob > W.LastCluster->Prob)
12608 break;
12609 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12610 std::swap(*I, *W.LastCluster);
12611 break;
12612 }
12613 }
12614 }
12615
12616 // Compute total probability.
12617 BranchProbability DefaultProb = W.DefaultProb;
12618 BranchProbability UnhandledProbs = DefaultProb;
12619 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12620 UnhandledProbs += I->Prob;
12621
12622 MachineBasicBlock *CurMBB = W.MBB;
12623 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12624 bool FallthroughUnreachable = false;
12625 MachineBasicBlock *Fallthrough;
12626 if (I == W.LastCluster) {
12627 // For the last cluster, fall through to the default destination.
12628 Fallthrough = DefaultMBB;
12629 FallthroughUnreachable = isa<UnreachableInst>(
12630 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12631 } else {
12632 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12633 CurMF->insert(BBI, Fallthrough);
12634 // Put Cond in a virtual register to make it available from the new blocks.
12636 }
12637 UnhandledProbs -= I->Prob;
12638
12639 switch (I->Kind) {
12640 case CC_JumpTable: {
12641 // FIXME: Optimize away range check based on pivot comparisons.
12642 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12643 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12644
12645 // The jump block hasn't been inserted yet; insert it here.
12646 MachineBasicBlock *JumpMBB = JT->MBB;
12647 CurMF->insert(BBI, JumpMBB);
12648
12649 auto JumpProb = I->Prob;
12650 auto FallthroughProb = UnhandledProbs;
12651
12652 // If the default statement is a target of the jump table, we evenly
12653 // distribute the default probability to successors of CurMBB. Also
12654 // update the probability on the edge from JumpMBB to Fallthrough.
12655 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12656 SE = JumpMBB->succ_end();
12657 SI != SE; ++SI) {
12658 if (*SI == DefaultMBB) {
12659 JumpProb += DefaultProb / 2;
12660 FallthroughProb -= DefaultProb / 2;
12661 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12662 JumpMBB->normalizeSuccProbs();
12663 break;
12664 }
12665 }
12666
12667 // If the default clause is unreachable, propagate that knowledge into
12668 // JTH->FallthroughUnreachable which will use it to suppress the range
12669 // check.
12670 //
12671 // However, don't do this if we're doing branch target enforcement,
12672 // because a table branch _without_ a range check can be a tempting JOP
12673 // gadget - out-of-bounds inputs that are impossible in correct
12674 // execution become possible again if an attacker can influence the
12675 // control flow. So if an attacker doesn't already have a BTI bypass
12676 // available, we don't want them to be able to get one out of this
12677 // table branch.
12678 if (FallthroughUnreachable) {
12679 Function &CurFunc = CurMF->getFunction();
12680 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12681 JTH->FallthroughUnreachable = true;
12682 }
12683
12684 if (!JTH->FallthroughUnreachable)
12685 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12686 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12687 CurMBB->normalizeSuccProbs();
12688
12689 // The jump table header will be inserted in our current block, do the
12690 // range check, and fall through to our fallthrough block.
12691 JTH->HeaderBB = CurMBB;
12692 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12693
12694 // If we're in the right place, emit the jump table header right now.
12695 if (CurMBB == SwitchMBB) {
12696 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12697 JTH->Emitted = true;
12698 }
12699 break;
12700 }
12701 case CC_BitTests: {
12702 // FIXME: Optimize away range check based on pivot comparisons.
12703 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12704
12705 // The bit test blocks haven't been inserted yet; insert them here.
12706 for (BitTestCase &BTC : BTB->Cases)
12707 CurMF->insert(BBI, BTC.ThisBB);
12708
12709 // Fill in fields of the BitTestBlock.
12710 BTB->Parent = CurMBB;
12711 BTB->Default = Fallthrough;
12712
12713 BTB->DefaultProb = UnhandledProbs;
12714 // If the cases in bit test don't form a contiguous range, we evenly
12715 // distribute the probability on the edge to Fallthrough to two
12716 // successors of CurMBB.
12717 if (!BTB->ContiguousRange) {
12718 BTB->Prob += DefaultProb / 2;
12719 BTB->DefaultProb -= DefaultProb / 2;
12720 }
12721
12722 if (FallthroughUnreachable)
12723 BTB->FallthroughUnreachable = true;
12724
12725 // If we're in the right place, emit the bit test header right now.
12726 if (CurMBB == SwitchMBB) {
12727 visitBitTestHeader(*BTB, SwitchMBB);
12728 BTB->Emitted = true;
12729 }
12730 break;
12731 }
12732 case CC_Range: {
12733 const Value *RHS, *LHS, *MHS;
12734 ISD::CondCode CC;
12735 if (I->Low == I->High) {
12736 // Check Cond == I->Low.
12737 CC = ISD::SETEQ;
12738 LHS = Cond;
12739 RHS=I->Low;
12740 MHS = nullptr;
12741 } else {
12742 // Check I->Low <= Cond <= I->High.
12743 CC = ISD::SETLE;
12744 LHS = I->Low;
12745 MHS = Cond;
12746 RHS = I->High;
12747 }
12748
12749 // If Fallthrough is unreachable, fold away the comparison.
12750 if (FallthroughUnreachable)
12751 CC = ISD::SETTRUE;
12752
12753 // The false probability is the sum of all unhandled cases.
12754 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12755 getCurSDLoc(), I->Prob, UnhandledProbs);
12756
12757 if (CurMBB == SwitchMBB)
12758 visitSwitchCase(CB, SwitchMBB);
12759 else
12760 SL->SwitchCases.push_back(CB);
12761
12762 break;
12763 }
12764 }
12765 CurMBB = Fallthrough;
12766 }
12767}
12768
12769void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12770 const SwitchWorkListItem &W,
12771 Value *Cond,
12772 MachineBasicBlock *SwitchMBB) {
12773 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12774 "Clusters not sorted?");
12775 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12776
12777 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12778 SL->computeSplitWorkItemInfo(W);
12779
12780 // Use the first element on the right as pivot since we will make less-than
12781 // comparisons against it.
12782 CaseClusterIt PivotCluster = FirstRight;
12783 assert(PivotCluster > W.FirstCluster);
12784 assert(PivotCluster <= W.LastCluster);
12785
12786 CaseClusterIt FirstLeft = W.FirstCluster;
12787 CaseClusterIt LastRight = W.LastCluster;
12788
12789 const ConstantInt *Pivot = PivotCluster->Low;
12790
12791 // New blocks will be inserted immediately after the current one.
12793 ++BBI;
12794
12795 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12796 // we can branch to its destination directly if it's squeezed exactly in
12797 // between the known lower bound and Pivot - 1.
12798 MachineBasicBlock *LeftMBB;
12799 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12800 FirstLeft->Low == W.GE &&
12801 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12802 LeftMBB = FirstLeft->MBB;
12803 } else {
12804 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12805 FuncInfo.MF->insert(BBI, LeftMBB);
12806 WorkList.push_back(
12807 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12808 // Put Cond in a virtual register to make it available from the new blocks.
12810 }
12811
12812 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12813 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12814 // directly if RHS.High equals the current upper bound.
12815 MachineBasicBlock *RightMBB;
12816 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12817 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12818 RightMBB = FirstRight->MBB;
12819 } else {
12820 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12821 FuncInfo.MF->insert(BBI, RightMBB);
12822 WorkList.push_back(
12823 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12824 // Put Cond in a virtual register to make it available from the new blocks.
12826 }
12827
12828 // Create the CaseBlock record that will be used to lower the branch.
12829 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12830 getCurSDLoc(), LeftProb, RightProb);
12831
12832 if (W.MBB == SwitchMBB)
12833 visitSwitchCase(CB, SwitchMBB);
12834 else
12835 SL->SwitchCases.push_back(CB);
12836}
12837
12838// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12839// from the swith statement.
12841 BranchProbability PeeledCaseProb) {
12842 if (PeeledCaseProb == BranchProbability::getOne())
12844 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12845
12846 uint32_t Numerator = CaseProb.getNumerator();
12847 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12848 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12849}
12850
12851// Try to peel the top probability case if it exceeds the threshold.
12852// Return current MachineBasicBlock for the switch statement if the peeling
12853// does not occur.
12854// If the peeling is performed, return the newly created MachineBasicBlock
12855// for the peeled switch statement. Also update Clusters to remove the peeled
12856// case. PeeledCaseProb is the BranchProbability for the peeled case.
12857MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12858 const SwitchInst &SI, CaseClusterVector &Clusters,
12859 BranchProbability &PeeledCaseProb) {
12860 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12861 // Don't perform if there is only one cluster or optimizing for size.
12862 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12863 TM.getOptLevel() == CodeGenOptLevel::None ||
12864 SwitchMBB->getParent()->getFunction().hasMinSize())
12865 return SwitchMBB;
12866
12867 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12868 unsigned PeeledCaseIndex = 0;
12869 bool SwitchPeeled = false;
12870 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12871 CaseCluster &CC = Clusters[Index];
12872 if (CC.Prob < TopCaseProb)
12873 continue;
12874 TopCaseProb = CC.Prob;
12875 PeeledCaseIndex = Index;
12876 SwitchPeeled = true;
12877 }
12878 if (!SwitchPeeled)
12879 return SwitchMBB;
12880
12881 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12882 << TopCaseProb << "\n");
12883
12884 // Record the MBB for the peeled switch statement.
12885 MachineFunction::iterator BBI(SwitchMBB);
12886 ++BBI;
12887 MachineBasicBlock *PeeledSwitchMBB =
12888 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12889 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12890
12891 ExportFromCurrentBlock(SI.getCondition());
12892 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12893 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12894 nullptr, nullptr, TopCaseProb.getCompl()};
12895 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12896
12897 Clusters.erase(PeeledCaseIt);
12898 for (CaseCluster &CC : Clusters) {
12899 LLVM_DEBUG(
12900 dbgs() << "Scale the probablity for one cluster, before scaling: "
12901 << CC.Prob << "\n");
12902 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12903 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12904 }
12905 PeeledCaseProb = TopCaseProb;
12906 return PeeledSwitchMBB;
12907}
12908
12909void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12910 // Extract cases from the switch.
12911 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12912 CaseClusterVector Clusters;
12913 Clusters.reserve(SI.getNumCases());
12914 for (auto I : SI.cases()) {
12915 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12916 const ConstantInt *CaseVal = I.getCaseValue();
12917 BranchProbability Prob =
12918 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12919 : BranchProbability(1, SI.getNumCases() + 1);
12920 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12921 }
12922
12923 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12924
12925 // Cluster adjacent cases with the same destination. We do this at all
12926 // optimization levels because it's cheap to do and will make codegen faster
12927 // if there are many clusters.
12928 sortAndRangeify(Clusters);
12929
12930 // The branch probablity of the peeled case.
12931 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12932 MachineBasicBlock *PeeledSwitchMBB =
12933 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12934
12935 // If there is only the default destination, jump there directly.
12936 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12937 if (Clusters.empty()) {
12938 assert(PeeledSwitchMBB == SwitchMBB);
12939 SwitchMBB->addSuccessor(DefaultMBB);
12940 if (DefaultMBB != NextBlock(SwitchMBB)) {
12941 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12942 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12943 }
12944 return;
12945 }
12946
12947 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12948 DAG.getBFI());
12949 SL->findBitTestClusters(Clusters, &SI);
12950
12951 LLVM_DEBUG({
12952 dbgs() << "Case clusters: ";
12953 for (const CaseCluster &C : Clusters) {
12954 if (C.Kind == CC_JumpTable)
12955 dbgs() << "JT:";
12956 if (C.Kind == CC_BitTests)
12957 dbgs() << "BT:";
12958
12959 C.Low->getValue().print(dbgs(), true);
12960 if (C.Low != C.High) {
12961 dbgs() << '-';
12962 C.High->getValue().print(dbgs(), true);
12963 }
12964 dbgs() << ' ';
12965 }
12966 dbgs() << '\n';
12967 });
12968
12969 assert(!Clusters.empty());
12970 SwitchWorkList WorkList;
12971 CaseClusterIt First = Clusters.begin();
12972 CaseClusterIt Last = Clusters.end() - 1;
12973 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12974 // Scale the branchprobability for DefaultMBB if the peel occurs and
12975 // DefaultMBB is not replaced.
12976 if (PeeledCaseProb != BranchProbability::getZero() &&
12977 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12978 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12979 WorkList.push_back(
12980 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12981
12982 while (!WorkList.empty()) {
12983 SwitchWorkListItem W = WorkList.pop_back_val();
12984 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12985
12986 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12987 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12988 // For optimized builds, lower large range as a balanced binary tree.
12989 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12990 continue;
12991 }
12992
12993 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12994 }
12995}
12996
12997void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12998 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12999 auto DL = getCurSDLoc();
13000 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
13001 setValue(&I, DAG.getStepVector(DL, ResultVT));
13002}
13003
13004void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
13005 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13006 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
13007
13008 SDLoc DL = getCurSDLoc();
13009 SDValue V = getValue(I.getOperand(0));
13010 assert(VT == V.getValueType() && "Malformed vector.reverse!");
13011
13012 if (VT.isScalableVector()) {
13013 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
13014 return;
13015 }
13016
13017 // Use VECTOR_SHUFFLE for the fixed-length vector
13018 // to maintain existing behavior.
13019 SmallVector<int, 8> Mask;
13020 unsigned NumElts = VT.getVectorMinNumElements();
13021 for (unsigned i = 0; i != NumElts; ++i)
13022 Mask.push_back(NumElts - 1 - i);
13023
13024 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
13025}
13026
13027void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
13028 unsigned Factor) {
13029 auto DL = getCurSDLoc();
13030 SDValue InVec = getValue(I.getOperand(0));
13031
13032 SmallVector<EVT, 4> ValueVTs;
13033 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
13034 ValueVTs);
13035
13036 EVT OutVT = ValueVTs[0];
13037 unsigned OutNumElts = OutVT.getVectorMinNumElements();
13038
13039 SmallVector<SDValue, 4> SubVecs(Factor);
13040 for (unsigned i = 0; i != Factor; ++i) {
13041 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
13042 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
13043 DAG.getVectorIdxConstant(OutNumElts * i, DL));
13044 }
13045
13046 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
13047 // from existing legalisation and combines.
13048 if (OutVT.isFixedLengthVector() && Factor == 2) {
13049 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
13050 createStrideMask(0, 2, OutNumElts));
13051 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
13052 createStrideMask(1, 2, OutNumElts));
13053 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
13054 setValue(&I, Res);
13055 return;
13056 }
13057
13058 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
13059 DAG.getVTList(ValueVTs), SubVecs);
13060 setValue(&I, Res);
13061}
13062
13063void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
13064 unsigned Factor) {
13065 auto DL = getCurSDLoc();
13066 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13067 EVT InVT = getValue(I.getOperand(0)).getValueType();
13068 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
13069
13070 SmallVector<SDValue, 8> InVecs(Factor);
13071 for (unsigned i = 0; i < Factor; ++i) {
13072 InVecs[i] = getValue(I.getOperand(i));
13073 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
13074 "Expected VTs to be the same");
13075 }
13076
13077 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
13078 // from existing legalisation and combines.
13079 if (OutVT.isFixedLengthVector() && Factor == 2) {
13080 unsigned NumElts = InVT.getVectorMinNumElements();
13081 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
13082 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
13083 createInterleaveMask(NumElts, 2)));
13084 return;
13085 }
13086
13087 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
13088 SDValue Res =
13089 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
13090
13092 for (unsigned i = 0; i < Factor; ++i)
13093 Results[i] = Res.getValue(i);
13094
13095 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
13096 setValue(&I, Res);
13097}
13098
13099void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
13100 SmallVector<EVT, 4> ValueVTs;
13101 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
13102 ValueVTs);
13103 unsigned NumValues = ValueVTs.size();
13104 if (NumValues == 0) return;
13105
13107 SDValue Op = getValue(I.getOperand(0));
13108
13109 for (unsigned i = 0; i != NumValues; ++i)
13110 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
13111 SDValue(Op.getNode(), Op.getResNo() + i));
13112
13114 DAG.getVTList(ValueVTs), Values));
13115}
13116
13117void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
13118 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13119 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
13120
13121 SDLoc DL = getCurSDLoc();
13122 SDValue V1 = getValue(I.getOperand(0));
13123 SDValue V2 = getValue(I.getOperand(1));
13124 const bool IsLeft = I.getIntrinsicID() == Intrinsic::vector_splice_left;
13125
13126 // VECTOR_SHUFFLE doesn't support a scalable or non-constant mask.
13127 if (VT.isScalableVector() || !isa<ConstantInt>(I.getOperand(2))) {
13128 SDValue Offset = DAG.getZExtOrTrunc(
13129 getValue(I.getOperand(2)), DL, TLI.getVectorIdxTy(DAG.getDataLayout()));
13130 setValue(&I, DAG.getNode(IsLeft ? ISD::VECTOR_SPLICE_LEFT
13132 DL, VT, V1, V2, Offset));
13133 return;
13134 }
13135 uint64_t Imm = cast<ConstantInt>(I.getOperand(2))->getZExtValue();
13136
13137 unsigned NumElts = VT.getVectorNumElements();
13138
13139 uint64_t Idx = IsLeft ? Imm : NumElts - Imm;
13140
13141 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
13142 SmallVector<int, 8> Mask;
13143 for (unsigned i = 0; i < NumElts; ++i)
13144 Mask.push_back(Idx + i);
13145 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
13146}
13147
13148// Consider the following MIR after SelectionDAG, which produces output in
13149// phyregs in the first case or virtregs in the second case.
13150//
13151// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
13152// %5:gr32 = COPY $ebx
13153// %6:gr32 = COPY $edx
13154// %1:gr32 = COPY %6:gr32
13155// %0:gr32 = COPY %5:gr32
13156//
13157// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
13158// %1:gr32 = COPY %6:gr32
13159// %0:gr32 = COPY %5:gr32
13160//
13161// Given %0, we'd like to return $ebx in the first case and %5 in the second.
13162// Given %1, we'd like to return $edx in the first case and %6 in the second.
13163//
13164// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
13165// to a single virtreg (such as %0). The remaining outputs monotonically
13166// increase in virtreg number from there. If a callbr has no outputs, then it
13167// should not have a corresponding callbr landingpad; in fact, the callbr
13168// landingpad would not even be able to refer to such a callbr.
13171 // There is definitely at least one copy.
13172 assert(MI->getOpcode() == TargetOpcode::COPY &&
13173 "start of copy chain MUST be COPY");
13174 Reg = MI->getOperand(1).getReg();
13175
13176 // If the copied register in the first copy must be virtual.
13177 assert(Reg.isVirtual() && "expected COPY of virtual register");
13178 MI = MRI.def_begin(Reg)->getParent();
13179
13180 // There may be an optional second copy.
13181 if (MI->getOpcode() == TargetOpcode::COPY) {
13182 assert(Reg.isVirtual() && "expected COPY of virtual register");
13183 Reg = MI->getOperand(1).getReg();
13184 assert(Reg.isPhysical() && "expected COPY of physical register");
13185 } else {
13186 // The start of the chain must be an INLINEASM_BR.
13187 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
13188 "end of copy chain MUST be INLINEASM_BR");
13189 }
13190
13191 return Reg;
13192}
13193
13194// We must do this walk rather than the simpler
13195// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
13196// otherwise we will end up with copies of virtregs only valid along direct
13197// edges.
13198void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
13199 SmallVector<EVT, 8> ResultVTs;
13200 SmallVector<SDValue, 8> ResultValues;
13201 const auto *CBR =
13202 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
13203
13204 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13205 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
13206 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
13207
13208 Register InitialDef = FuncInfo.ValueMap[CBR];
13209 SDValue Chain = DAG.getRoot();
13210
13211 // Re-parse the asm constraints string.
13212 TargetLowering::AsmOperandInfoVector TargetConstraints =
13213 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
13214 for (auto &T : TargetConstraints) {
13215 SDISelAsmOperandInfo OpInfo(T);
13216 if (OpInfo.Type != InlineAsm::isOutput)
13217 continue;
13218
13219 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
13220 // individual constraint.
13221 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
13222
13223 switch (OpInfo.ConstraintType) {
13226 // Fill in OpInfo.AssignedRegs.Regs.
13227 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
13228
13229 // getRegistersForValue may produce 1 to many registers based on whether
13230 // the OpInfo.ConstraintVT is legal on the target or not.
13231 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
13232 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
13233 if (OriginalDef.isPhysical())
13234 FuncInfo.MBB->addLiveIn(OriginalDef);
13235 // Update the assigned registers to use the original defs.
13236 Reg = OriginalDef;
13237 }
13238
13239 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
13240 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
13241 ResultValues.push_back(V);
13242 ResultVTs.push_back(OpInfo.ConstraintVT);
13243 break;
13244 }
13246 SDValue Flag;
13247 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
13248 OpInfo, DAG);
13249 ++InitialDef;
13250 ResultValues.push_back(V);
13251 ResultVTs.push_back(OpInfo.ConstraintVT);
13252 break;
13253 }
13254 default:
13255 break;
13256 }
13257 }
13259 DAG.getVTList(ResultVTs), ResultValues);
13260 setValue(&I, V);
13261}
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)
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI)
Returns an AttributeList representing the attributes applied to the return value of the given call.
Definition FastISel.cpp:942
#define Check(C,...)
static Value * getCondition(Instruction *I)
Hexagon Common GEP
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
static void getRegistersForValue(MachineFunction &MF, MachineIRBuilder &MIRBuilder, GISelAsmOperandInfo &OpInfo, GISelAsmOperandInfo &RefOpInfo)
Assign virtual/physical registers for the specified register operand.
static void computeConstraintToUse(const TargetLowering *TLI, TargetLowering::AsmOperandInfo &OpInfo)
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)
if(PassOpts->AAPipeline)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V, bool LookThroughCmp=false)
Returns the "element 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 bool prepareDAGLevelOperands(ConstraintDecisionInfo &Info, const CallBase &Call, SelectionDAGBuilder &Builder, const TargetLowering &TLI, SelectionDAG &DAG)
Prepare DAG-level operands.
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 bool determineConstraints(ConstraintDecisionInfo &Info, TargetLowering::AsmOperandInfoVector &TargetConstraints, const CallBase &Call, SelectionDAGBuilder &Builder, const TargetLowering &TLI, const TargetMachine &TM, SelectionDAG &DAG, const BasicBlock *EHPadBB)
DetermineConstraints - Find the constraints to use for inline asm operands.
static bool constructOperandInfo(ConstraintDecisionInfo &Info, TargetLowering::AsmOperandInfoVector &TargetConstraints, SelectionDAGBuilder &Builder, const TargetLowering &TLI, ExtraFlags &ExtraInfo)
Construct operand info objects.
static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl)
GetSignificand - Get the significand and build it into a floating-point number with exponent of 1:
static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp - Lower an exp intrinsic.
static const MDNode * getRangeMetadata(const Instruction &I)
static cl::opt< unsigned, true > LimitFPPrecision("limit-float-precision", cl::desc("Generate low-precision inline sequences " "for some float libcalls"), cl::location(LimitFloatPrecision), cl::Hidden, cl::init(0))
static void tryToElideArgumentCopy(FunctionLoweringInfo &FuncInfo, SmallVectorImpl< SDValue > &Chains, DenseMap< int, int > &ArgCopyElisionFrameIndexMap, SmallPtrSetImpl< const Instruction * > &ElidedArgCopyInstrs, ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg, ArrayRef< SDValue > ArgVals, bool &ArgHasUses)
Try to elide argument copies from memory into a local alloca.
static unsigned LimitFloatPrecision
LimitFloatPrecision - Generate low-precision inline sequences for some float libcalls (6,...
static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC)
getCopyFromPartsVector - Create a value that contains the specified legal parts combined into the val...
static bool InBlock(const Value *V, const BasicBlock *BB)
static FPClassTest getNoFPClass(const Instruction &I)
static LLVM_ATTRIBUTE_ALWAYS_INLINE MVT::SimpleValueType getSimpleVT(const uint8_t *MatcherTable, size_t &MatcherIndex)
getSimpleVT - Decode a value in MatcherTable, if it's a VBR encoded value, use GetVBR to decode it.
This file defines the SmallPtrSet class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
uint16_t RegSizeInBits(const MCRegisterInfo &MRI, MCRegister RegNo)
Value * RHS
Value * LHS
The Input class is used to parse a yaml document into in-memory structs and vectors.
static const fltSemantics & IEEEsingle()
Definition APFloat.h:297
static LLVM_ABI Semantics SemanticsToEnum(const llvm::fltSemantics &Sem)
Definition APFloat.cpp:160
static LLVM_ABI const fltSemantics * getArbitraryFPSemantics(StringRef Format)
Returns the fltSemantics for a given arbitrary FP format string, or nullptr if invalid.
Definition APFloat.cpp:6009
Class for arbitrary precision integers.
Definition APInt.h:78
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
an instruction to allocate memory on the stack
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
LLVM_ABI std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Check if an argument has a given attribute.
Definition Function.cpp:333
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
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
@ FMaximumNum
*p = maximumnum(old, v) maximumnum matches the behavior of llvm.maximumnum.
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ FMinimumNum
*p = minimumnum(old, v) minimumnum matches the behavior of llvm.minimumnum.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:407
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
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:1088
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:728
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
Conditional Branch instruction.
Class for constant bytes.
Definition Constants.h:281
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:755
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1316
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:219
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
A signed pointer, in the ptrauth sense.
Definition Constants.h:1223
uint64_t getZExtValue() const
Constant Vector Declarations.
Definition Constants.h:674
This is an important base class in LLVM.
Definition Constant.h:43
This is the common base class for constrained floating point intrinsics.
LLVM_ABI std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
LLVM_ABI unsigned getNonMetadataArgCount() const
DWARF expression.
LLVM_ABI bool isEntryValue() const
Check if the expression consists of exactly one entry value operand.
static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B)
Check if fragments overlap between a pair of FragmentInfos.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI std::optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)
Retrieve the details of this fragment expression.
LLVM_ABI uint64_t getNumLocationOperands() const
Return the number of unique location operands referred to (via DW_OP_LLVM_arg) in this expression; th...
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
static LLVM_ABI const DIExpression * convertToUndefExpression(const DIExpression *Expr)
Removes all elements from Expr that do not apply to an undef debug value, which includes every operat...
static LLVM_ABI DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
static LLVM_ABI DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
Base class for variables.
LLVM_ABI std::optional< uint64_t > getSizeInBits() const
Determines the size of the variable's type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isBigEndian() const
Definition DataLayout.h:218
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:126
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:58
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:225
bool empty() const
Definition DenseMap.h:173
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:136
iterator end()
Definition DenseMap.h:143
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:286
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:178
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:867
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:783
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:211
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:246
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:685
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition Function.cpp:735
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:272
Constant * getPersonalityFn() const
Get the personality function associated with this function.
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:328
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:251
size_t arg_size() const
Definition Function.h:875
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:723
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.
void setMemConstraint(ConstraintCode C)
setMemConstraint - Augment an existing flag with the constraint code for a memory constraint.
Definition InlineAsm.h:414
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).
static LocationSize upperBound(uint64_t Value)
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:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1426
LLVM_ABI StringRef getString() const
Definition Metadata.cpp:632
Machine Value Type.
@ INVALID_SIMPLE_VALUE_TYPE
uint64_t getScalarSizeInBits() const
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
ElementCount getVectorElementCount() const
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
bool bitsGE(MVT VT) const
Return true if this has no less bits than VT.
bool isScalarInteger() const
Return true if this is an integer, not including vectors.
static MVT getVectorVT(MVT VT, unsigned NumElements)
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
static MVT getIntegerVT(unsigned BitWidth)
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void setIsEHContTarget(bool V=true)
Indicates if this is a target of Windows EH Continuation Guard.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
MachineInstrBundleIterator< MachineInstr > iterator
void setIsEHScopeEntry(bool V=true)
Indicates if this is the entry block of an EH scope, i.e., the block that that used to have a catchpa...
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
int getStackProtectorIndex() const
Return the index for the stack protector object.
void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased)
Set "maybe pointed to by an LLVM IR value" for an object.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
void setFunctionContextIndex(int I)
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site)
Map the begin label for a call site.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD)
Record annotations associated with a particular label.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
void setHasEHContTarget(bool V)
void addInvoke(MachineBasicBlock *LandingPad, MCSymbol *BeginLabel, MCSymbol *EndLabel)
Provide the begin and end labels of an invoke style call and associate it with a try landing pad bloc...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
Representation of each machine instruction.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
def_iterator def_begin(Register RegNo) const
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI MCRegister getLiveInPhysReg(Register VReg) const
getLiveInPhysReg - If VReg is a live-in virtual register, return the corresponding live-in physical r...
An SDNode that represents everything that will be needed to construct a MachineInstr.
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:118
bool contains(const KeyT &Key) const
Definition MapVector.h:148
static MemoryLocation getAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location after Ptr, while remaining within the underlying objec...
Metadata wrapper in the Value hierarchy.
Definition Metadata.h:184
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
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.
bool shouldKeepJumpConditionsTogether(const FunctionLoweringInfo &FuncInfo, const CondBrInst &I, Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs, TargetLoweringBase::CondMergingParams Params) const
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.
SDValue lowerStartEH(SDValue Chain, const BasicBlock *EHPadBB, MCSymbol *&BeginLabel)
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)
StatepointLoweringState StatepointLowering
State used while lowering a statepoint sequence (gc_statepoint, gc_relocate, and gc_result).
void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB, BranchProbability BranchProbToNext, Register Reg, SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB)
visitBitTestCase - this function produces one "bit test"
bool canTailCall(const CallBase &CB) const
void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, AttributeSet RetAttrs, bool IsPatchPoint)
Populate a CallLowerinInfo (into CLI) based on the properties of the call being lowered.
void CopyValueToVirtualRegister(const Value *V, Register Reg, ISD::NodeType ExtendType=ISD::ANY_EXTEND)
void salvageUnresolvedDbgValue(const Value *V, DanglingDebugInfo &DDI)
For the given dangling debuginfo record, perform last-ditch efforts to resolve the debuginfo to somet...
SmallVector< SDValue, 8 > PendingLoads
Loads are not emitted to the program immediately.
GCFunctionInfo * GFI
Garbage collection metadata for the function.
void init(GCFunctionInfo *gfi, BatchAAResults *BatchAA, AssumptionCache *AC, const TargetLibraryInfo *li, const TargetTransformInfo &TTI)
SDValue getRoot()
Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) items.
void ExportFromCurrentBlock(const Value *V)
ExportFromCurrentBlock - If this condition isn't known to be exported from the current basic block,...
void resolveOrClearDbgInfo()
Evict any dangling debug information, attempting to salvage it first.
std::pair< SDValue, SDValue > lowerInvokable(TargetLowering::CallLoweringInfo &CLI, const BasicBlock *EHPadBB=nullptr)
SDValue getMemoryRoot()
Return the current virtual root of the Selection DAG, flushing any PendingLoad items.
void resolveDanglingDebugInfo(const Value *V, SDValue Val)
If we saw an earlier dbg_value referring to V, generate the debug data structures now that we've seen...
void visit(const Instruction &I)
void dropDanglingDebugInfo(const DILocalVariable *Variable, const DIExpression *Expr)
If we have dangling debug info that describes Variable, or an overlapping part of variable considerin...
SDValue getCopyFromRegs(const Value *V, Type *Ty)
If there was virtual register allocated for the value V emit CopyFromReg of the specified type Ty.
void CopyToExportRegsIfNeeded(const Value *V)
CopyToExportRegsIfNeeded - If the given value has virtual registers created for it,...
void handleKillDebugValue(DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order)
Create a record for a kill location debug intrinsic.
void visitJumpTable(SwitchCG::JumpTable &JT)
visitJumpTable - Emit JumpTable node in the current MBB
SDValue getFPOperationRoot(fp::ExceptionBehavior EB)
Return the current virtual root of the Selection DAG, flushing PendingConstrainedFP or PendingConstra...
void visitJumpTableHeader(SwitchCG::JumpTable &JT, SwitchCG::JumpTableHeader &JTH, MachineBasicBlock *SwitchBB)
visitJumpTableHeader - This function emits necessary code to produce index in the JumpTable from swit...
void LowerCallSiteWithPtrAuthBundle(const CallBase &CB, const BasicBlock *EHPadBB)
static const unsigned LowestSDNodeOrder
Lowest valid SDNodeOrder.
FunctionLoweringInfo & FuncInfo
Information about the function as a whole.
void setValue(const Value *V, SDValue NewN)
void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, Instruction::BinaryOps Opc, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
const TargetLibraryInfo * LibInfo
bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB)
void visitSPDescriptorParent(StackProtectorDescriptor &SPD, MachineBasicBlock *ParentBB)
Codegen a new tail for a stack protector check ParentMBB which has had its tail spliced into a stack ...
bool handleDebugValue(ArrayRef< const Value * > Values, DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order, bool IsVariadic)
For a given list of Values, attempt to create and record a SDDbgValue in the SelectionDAG.
SDValue getControlRoot()
Similar to getRoot, but instead of flushing all the PendingLoad items, flush all the PendingExports (...
void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last)
When an MBB was split during scheduling, update the references that need to refer to the last resulti...
SDValue getValueImpl(const Value *V)
getValueImpl - Helper function for getValue and getNonRegisterValue.
void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB)
visitSwitchCase - Emits the necessary code to represent a single node in the binary search tree resul...
void visitSPDescriptorFailure(StackProtectorDescriptor &SPD)
Codegen the failure basic block for a stack protector check.
std::unique_ptr< FunctionLoweringInfo > FuncInfo
SmallPtrSet< const Instruction *, 4 > ElidedArgCopyInstrs
const TargetLowering * TLI
MachineRegisterInfo * RegInfo
std::unique_ptr< SwiftErrorValueTracking > SwiftError
virtual void emitFunctionEntryCode()
std::unique_ptr< SelectionDAGBuilder > SDB
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemccpy(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, SDValue C, SDValue Size, const CallInst *CI) const
Emit target-specific code that performs a memccpy, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrnlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, const CallInst *CI) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrstr(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, const CallInst *CI) const
Emit target-specific code that performs a strstr, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemchr(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Src, SDValue Char, SDValue Length, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memchr, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, MachinePointerInfo Op1PtrInfo, MachinePointerInfo Op2PtrInfo, const CallInst *CI) const
Emit target-specific code that performs a strcmp, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForMemcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, const CallInst *CI) const
Emit target-specific code that performs a memcmp/bcmp, in cases where that is faster than a libcall.
virtual SDValue EmitTargetCodeForSetTag(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Addr, SDValue Size, MachinePointerInfo DstPtrInfo, bool ZeroData) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrcpy(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dest, SDValue Src, MachinePointerInfo DestPtrInfo, MachinePointerInfo SrcPtrInfo, bool isStpcpy, const CallInst *CI) const
Emit target-specific code that performs a strcpy or stpcpy, in cases where that is faster than a libc...
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SDValue getTargetGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, unsigned TargetFlags=0)
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 getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
const LibcallLoweringInfo & getLibcalls() const
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
MachineFunction & getMachineFunction() const
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVMContext * getContext() const
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
Multiway switch.
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Provides information about what library functions are available for the current target.
virtual Align getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Returns the desired alignment for ByVal or InAlloca aggregate function arguments in the caller parame...
virtual bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, EVT) const
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
EVT getMemValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Function * getSSPStackGuardCheck(const Module &M, const LibcallLoweringInfo &Libcalls) const
If the target has a standard stack protection check function that performs validation and error handl...
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
LegalizeAction
This enum indicates whether operations are valid for a target, and if not, what action should be used...
virtual 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 bool useStackGuardMixFP() const
If this function returns true, stack protection checks should mix the frame pointer (or whichever poi...
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.
MachineMemOperand::Flags getLoadMemOperandFlags(const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC=nullptr, const TargetLibraryInfo *LibInfo=nullptr, CodeGenOptLevel OptLevel=CodeGenOptLevel::Default) const
virtual bool shouldExtendGSIndex(EVT VT, EVT &EltTy) const
Returns true if the index type for a masked gather/scatter requires extending.
virtual unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
LegalizeAction getFixedPointOperationAction(unsigned Op, EVT VT, unsigned Scale) const
Some fixed point operations may be natively supported by the target but only for specific scales.
MachineMemOperand::Flags getAtomicMemOperandFlags(const Instruction &AI, const DataLayout &DL) const
virtual bool allowsMisalignedMemoryAccesses(EVT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *=nullptr) const
Determine if the target supports unaligned memory accesses.
bool isOperationCustom(unsigned Op, EVT VT) const
Return true if the operation uses custom lowering, regardless of whether the type is legal or not.
bool hasBigEndianPartOrdering(EVT VT, const DataLayout &DL) const
When splitting a value of the specified type into parts, does the Lo or Hi part come first?
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
virtual Align getABIAlignmentForCallingConv(Type *ArgTy, const DataLayout &DL) const
Certain targets have context sensitive alignment requirements, where one type has the alignment requi...
MachineMemOperand::Flags getVPIntrinsicMemOperandFlags(const VPIntrinsic &VPIntrin) const
virtual bool shouldExpandGetActiveLaneMask(EVT VT, EVT OpVT) const
Return true if the @llvm.get.active.lane.mask intrinsic should be expanded using generic code in Sele...
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
MVT getProgramPointerTy(const DataLayout &DL) const
Return the type for code pointers, which is determined by the program address space specified through...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
virtual bool shouldExpandVectorMatch(EVT VT, unsigned SearchSize) const
Return true if the @llvm.experimental.vector.match intrinsic should be expanded for vector type ‘VT’ ...
virtual bool isProfitableToCombineMinNumMaxNum(EVT VT) const
virtual MVT getFenceOperandTy(const DataLayout &DL) const
Return the type for operands of fence.
virtual bool shouldExpandGetVectorLength(EVT CountVT, unsigned VF, bool IsScalable) const
bool isOperationLegalOrCustom(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
virtual MVT hasFastEqualityCompare(unsigned NumBits) const
Return the preferred operand type if the target has a quick way to compare integer values of the give...
MachineMemOperand::Flags getStoreMemOperandFlags(const StoreInst &SI, const DataLayout &DL) const
virtual void getTgtMemIntrinsic(SmallVectorImpl< IntrinsicInfo > &Infos, const CallBase &I, MachineFunction &MF, unsigned Intrinsic) const
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
virtual bool signExtendConstant(const ConstantInt *C) const
Return true if this constant should be sign extended when promoting to a larger type.
virtual Value * getSDagStackGuard(const Module &M, const LibcallLoweringInfo &Libcalls) const
Return the variable that's previously inserted by insertSSPDeclarations, if any, otherwise return nul...
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
virtual Register getExceptionPointerRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception address on entry to an ...
bool supportsUnalignedAtomics() const
Whether the target supports unaligned atomic operations.
std::vector< ArgListEntry > ArgListTy
bool isBeneficialToExpandPowI(int64_t Exponent, bool OptForSize) const
Return true if it is beneficial to expand an @llvm.powi.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual Register getExceptionSelectorRegister(const Constant *PersonalityFn) const
If a physical register, this returns the register that receives the exception typeid on entry to a la...
virtual MVT getPointerMemTy(const DataLayout &DL, uint32_t AS=0) const
Return the in-memory pointer type for the given address space, defaults to the pointer type from the ...
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
virtual MVT getVPExplicitVectorLengthTy() const
Returns the type to be used for the EVL/AVL operand of VP nodes: ISD::VP_ADD, ISD::VP_SUB,...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool supportKCFIBundles() const
Return true if the target supports kcfi operand bundles.
virtual bool supportPtrAuthBundles() const
Return true if the target supports ptrauth operand bundles.
virtual bool supportSwiftError() const
Return true if the target supports swifterror attribute.
virtual SDValue visitMaskedLoad(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue &NewLoad, SDValue Ptr, SDValue PassThru, SDValue Mask) const
virtual EVT getTypeForExtReturn(LLVMContext &Context, EVT VT, ISD::NodeType) const
Return the type that should be used to zero or sign extend a zeroext/signext integer return value.
virtual Register getRegisterByName(const char *RegName, LLT Ty, const MachineFunction &MF) const
Return the register ID of the name passed in.
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 SDValue emitStackGuardMixFP(SelectionDAG &DAG, SDValue Val, const SDLoc &DL) const
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
virtual bool splitValueIntoRegisterParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, std::optional< CallingConv::ID > CC) const
Target-specific splitting of values into parts that fit a register storing a legal type.
virtual SDValue joinRegisterPartsIntoValue(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, std::optional< CallingConv::ID > CC) const
Target-specific combining of register parts into its original value.
virtual SDValue LowerCall(CallLoweringInfo &, SmallVectorImpl< SDValue > &) const
This hook must be implemented to lower calls into the specified DAG.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual SDValue LowerAsmOutputForConstraint(SDValue &Chain, SDValue &Glue, const SDLoc &DL, const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, const CallBase &Call) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const
This callback is invoked for operations that are unsupported by the target, which are registered to u...
virtual bool functionArgumentNeedsConsecutiveRegisters(Type *Ty, CallingConv::ID CallConv, bool isVarArg, const DataLayout &DL) const
For some targets, an LLVM struct type must be broken down into multiple simple types,...
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo,...
virtual void CollectTargetIntrinsicOperands(const CallInst &I, SmallVectorImpl< SDValue > &Ops, SelectionDAG &DAG) const
virtual SDValue visitMaskedStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue Ptr, SDValue Val, SDValue Mask) const
virtual bool useLoadStackGuardNode(const Module &M) const
If this function returns true, SelectionDAGBuilder emits a LOAD_STACK_GUARD node when it is lowering ...
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::LibcallImpl LibcallImpl, EVT RetVT, ArrayRef< SDValue > Ops, MakeLibCallOptions CallOptions, const SDLoc &dl, SDValue Chain=SDValue()) const
Returns a pair of (return value, chain).
virtual void LowerOperationWrapper(SDNode *N, SmallVectorImpl< SDValue > &Results, SelectionDAG &DAG) const
This callback is invoked by the type legalizer to legalize nodes with an illegal operand type but leg...
virtual bool isInlineAsmTargetBranch(const SmallVectorImpl< StringRef > &AsmStrs, unsigned OpNo) const
On x86, return true if the operand with index OpNo is a CALL or JUMP instruction, which can use eithe...
virtual MVT getJumpTableRegTy(const DataLayout &DL) const
virtual bool CanLowerReturn(CallingConv::ID, MachineFunction &, bool, const SmallVectorImpl< ISD::OutputArg > &, LLVMContext &, const Type *RetTy) const
This hook should be implemented to check whether the return values described by the Outs array can fi...
Primary interface to the complete machine description for the target machine.
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
CodeModel::Model getCodeModel() const
Returns the code model.
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:46
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:288
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
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:282
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:306
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:236
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:313
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:227
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
Unconditional Branch instruction.
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_iterator op_begin()
Definition User.h:259
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
op_iterator op_end()
Definition User.h:261
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
LLVM_ABI CmpInst::Predicate getPredicate() const
This is the common base class for vector predication intrinsics.
static LLVM_ABI std::optional< unsigned > getVectorLengthParamPos(Intrinsic::ID IntrinsicID)
LLVM_ABI MaybeAlign getPointerAlignment() const
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
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:713
bool use_empty() const
Definition Value.h:346
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
const ParentTy * getParent() const
Definition ilist_node.h:34
A raw_ostream that writes to an std::string.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ STACKRESTORE
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain.
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
@ CONVERGENCECTRL_ANCHOR
The llvm.experimental.convergence.* intrinsics.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:511
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ SET_FPENV
Sets the current floating-point environment.
@ ATOMIC_LOAD_FMINIMUMNUM
@ LOOP_DEPENDENCE_RAW_MASK
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ COND_LOOP
COND_LOOP is a conditional branch to self, used for implementing efficient conditional traps.
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition ISDOpcodes.h:168
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:600
@ STACKADDRESS
STACKADDRESS - Represents the llvm.stackaddress intrinsic.
Definition ISDOpcodes.h:127
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:787
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:394
@ VAEND
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE.
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ RESET_FPENV
Set floating-point environment to default state.
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:400
@ SET_FPMODE
Sets the current dynamic floating-point control modes.
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:861
@ CTTZ_ELTS
Returns the number of number of trailing (least significant) zero elements in a vector.
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:518
@ VECTOR_FIND_LAST_ACTIVE
Finds the index of the last active mask element Operands: Mask.
@ FMODF
FMODF - Decomposes the operand into integral and fractional parts, each having the same type and sign...
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ FSINCOSPI
FSINCOSPI - Compute both the sine and cosine times pi more accurately than FSINCOS(pi*x),...
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ EH_SJLJ_SETUP_DISPATCH
OUTCHAIN = EH_SJLJ_SETUP_DISPATCH(INCHAIN) The target initializes the dispatch table here.
Definition ISDOpcodes.h:172
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:888
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:584
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:747
@ ATOMIC_FENCE
OUTCHAIN = ATOMIC_FENCE(INCHAIN, ordering, scope) This corresponds to the fence instruction.
@ RESET_FPMODE
Sets default dynamic floating-point control modes.
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:528
@ FPTRUNC_ROUND
FPTRUNC_ROUND - This corresponds to the fptrunc_round intrinsic.
Definition ISDOpcodes.h:515
@ FAKE_USE
FAKE_USE represents a use of the operand but does not do anything.
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:778
@ INIT_TRAMPOLINE
INIT_TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:407
@ CONVERT_FROM_ARBITRARY_FP
CONVERT_FROM_ARBITRARY_FP - This operator converts from an arbitrary floating-point represented as an...
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ATOMIC_LOAD_USUB_SAT
@ CTLZ_ZERO_POISON
Definition ISDOpcodes.h:796
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:156
@ ANNOTATION_LABEL
ANNOTATION_LABEL - Represents a mid basic block label used by annotations.
@ SET_ROUNDING
Set rounding mode.
Definition ISDOpcodes.h:983
@ CONVERGENCECTRL_GLUE
This does not correspond to any convergence control intrinsic.
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:852
@ PREALLOCATED_SETUP
PREALLOCATED_SETUP - This has 2 operands: an input chain and a SRCVALUE with the preallocated call Va...
@ READSTEADYCOUNTER
READSTEADYCOUNTER - This corresponds to the readfixedcounter intrinsic.
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition ISDOpcodes.h:117
@ CONVERGENCECTRL_ENTRY
@ BR
Control flow instructions. These all have token chains.
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ PARTIAL_REDUCE_FMLA
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ PREALLOCATED_ARG
PREALLOCATED_ARG - This has 3 operands: an input chain, a SRCVALUE with the preallocated call Value,...
@ BRIND
BRIND - Indirect branch.
@ BR_JT
BR_JT - Jumptable branch.
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:635
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:541
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:548
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:804
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:672
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:978
@ CLEANUPRET
CLEANUPRET - Represents a return from a cleanup block funclet.
@ ATOMIC_LOAD_FMAXIMUM
@ GET_FPMODE
Reads the current dynamic floating-point control modes.
@ GET_FPENV
Gets the current floating-point environment.
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:769
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ PtrAuthGlobalAddress
A ptrauth constant.
Definition ISDOpcodes.h:100
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:614
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:139
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:576
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:858
@ DEBUGTRAP
DEBUGTRAP - Trap intended to get the attention of a debugger.
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ LOCAL_RECOVER
LOCAL_RECOVER - Represents the llvm.localrecover intrinsic.
Definition ISDOpcodes.h:135
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ UBSANTRAP
UBSANTRAP - Trap with an immediate describing the kind of sanitizer failure.
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ PATCHPOINT
The llvm.experimental.patchpoint.
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ ATOMIC_LOAD_FMINIMUM
@ DYNAMIC_STACKALLOC
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary.
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
Definition ISDOpcodes.h:653
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:727
@ MASKED_UDIV
Masked vector arithmetic that returns poison on disabled lanes.
@ VECTOR_REVERSE
VECTOR_REVERSE(VECTOR) - Returns a vector, of the same type as VECTOR, whose elements are shuffled us...
Definition ISDOpcodes.h:640
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:413
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:986
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:813
@ PCMARKER
PCMARKER - This corresponds to the pcmarker intrinsic.
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ ATOMIC_LOAD_FMAXIMUMNUM
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:150
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ ATOMIC_LOAD_UDEC_WRAP
@ PEXT
Parallel bit extract (compress) and parallel bit deposit (expand).
Definition ISDOpcodes.h:783
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:500
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:934
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ RELOC_NONE
Issue a no-op relocation against a given symbol at the current location.
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:739
@ TRAP
TRAP - Trapping instruction.
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:735
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:657
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition ISDOpcodes.h:427
@ STACKMAP
The llvm.experimental.stackmap intrinsic.
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ CTTZ_ZERO_POISON
Bit counting operators with a poisoned result for zero inputs.
Definition ISDOpcodes.h:795
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:967
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:699
@ SPONENTRY
SPONENTRY - Represents the llvm.sponentry intrinsic.
Definition ISDOpcodes.h:122
@ CLEAR_CACHE
llvm.clear_cache intrinsic Operands: Input Chain, Start Addres, End Address Outputs: Output Chain
@ CONVERGENCECTRL_LOOP
@ INLINEASM
INLINEASM - Represents an inline asm block.
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:953
@ VECREDUCE_FMINIMUM
@ EH_SJLJ_SETJMP
RESULT, OUTCHAIN = EH_SJLJ_SETJMP(INCHAIN, buffer) This corresponds to the eh.sjlj....
Definition ISDOpcodes.h:162
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:864
@ BRCOND
BRCOND - Conditional branch.
@ VECREDUCE_SEQ_FMUL
@ CONVERT_TO_ARBITRARY_FP
CONVERT_TO_ARBITRARY_FP - Converts a native FP value to an arbitrary floating-point format,...
@ CATCHRET
CATCHRET - Represents a return from a catch block funclet.
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:534
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:624
@ GET_DYNAMIC_AREA_OFFSET
GET_DYNAMIC_AREA_OFFSET - get offset from native SP to the address of the most recent dynamic alloca.
@ CTTZ_ELTS_ZERO_POISON
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ ADJUST_TRAMPOLINE
ADJUST_TRAMPOLINE - This corresponds to the adjust_trampoline intrinsic.
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ ABS_MIN_POISON
ABS with a poison result for INT_MIN.
Definition ISDOpcodes.h:751
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
@ LOOP_DEPENDENCE_WAR_MASK
The llvm.loop.dependence.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
auto m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
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
LLVM_ABI 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:573
@ Length
Definition DWP.cpp:573
LLVM_ABI ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:237
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
SDValue peekThroughFreeze(SDValue V)
Return the non-frozen source operand of V if it exists.
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.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI 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:315
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
auto successors(const MachineBasicBlock *BB)
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
static ConstantRange getRange(Value *Op, SCCPSolver &Solver, const SmallPtrSetImpl< Value * > &InsertedValues)
Helper for getting ranges from Solver.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
auto cast_or_null(const Y &Val)
Definition Casting.h:714
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:546
gep_type_iterator gep_type_end(const User *GEP)
LLVM_ABI LLT getLLTForMVT(MVT Ty)
Get a rough equivalent of an LLT for a given MVT.
constexpr auto equal_to(T &&Arg)
Functor variant of std::equal_to that can be used as a UnaryPredicate in functional algorithms like a...
Definition STLExtras.h:2173
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:156
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:1151
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
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:204
LLVM_ABI void ComputeValueTypes(const DataLayout &DL, Type *Ty, SmallVectorImpl< Type * > &Types, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
Given an LLVM IR type, compute non-aggregate subtypes.
Definition Analysis.cpp:72
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI llvm::SmallVector< int, 16 > createStrideMask(unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_FMINNUM
Unsigned maximum.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
detail::zippy< detail::zip_first, T, U, Args... > zip_first(T &&t, U &&u, Args &&...args)
zip iterator that, for the sake of efficiency, assumes the first iteratee to be the shortest.
Definition STLExtras.h:853
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
generic_gep_type_iterator<> gep_type_iterator
auto succ_size(const MachineBasicBlock *BB)
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition STLExtras.h:299
LLVM_ABI ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred)
getFCmpCondCode - Return the ISD condition code corresponding to the given LLVM IR floating-point con...
Definition Analysis.cpp:203
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI Value * salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Ops, SmallVectorImpl< Value * > &AdditionalValues)
Definition Local.cpp:2300
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...
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.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
@ 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.
LLVM_ABI bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:539
DWARFExpression::Operation Op
@ Dynamic
Denotes mode unknown at compile time.
LLVM_ABI ISD::CondCode getFCmpCodeWithoutNaN(ISD::CondCode CC)
getFCmpCodeWithoutNaN - Given an ISD condition code comparing floats, return the equivalent code if w...
Definition Analysis.cpp:225
ArrayRef(const T &OneElt) -> ArrayRef< T >
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
LLVM_ABI std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition FPEnv.cpp:25
gep_type_iterator gep_type_begin(const User *GEP)
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2192
LLVM_ABI GlobalValue * ExtractTypeInfo(Value *V)
ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
Definition Analysis.cpp:181
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2166
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
LLVM_ABI 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:347
@ Default
The result value is uniform if and only if all operands are uniform.
Definition Uniformity.h:20
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:862
#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:418
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
uint64_t getScalarStoreSize() const
Definition ValueTypes.h:425
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:307
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:323
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
ElementCount getVectorElementCount() const
Definition ValueTypes.h:373
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:396
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:382
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:408
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
EVT changeVectorElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
Definition ValueTypes.h:98
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:339
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
bool isRISCVVectorTuple() const
Return true if this is a vector value type.
Definition ValueTypes.h:197
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:404
bool isFixedLengthVector() const
Definition ValueTypes.h:199
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:346
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:315
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:187
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:351
EVT changeElementType(LLVMContext &Context, EVT EltVT) const
Return a VT for a type whose attributes match ourselves with the exception of the element type that i...
Definition ValueTypes.h:121
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:165
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:359
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
void setPointerAddrSpace(unsigned AS)
InputArg - This struct carries flags and type information about a single incoming (formal) argument o...
static const unsigned NoArgIndex
Sentinel value for implicit machine-level input arguments.
OutputArg - This struct carries flags and a value for a single outgoing (actual) argument or outgoing...
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber/label.
Definition InlineAsm.h:128
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:262
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:342
This structure is used to communicate between SelectionDAGBuilder and SDISel for the code generation ...
SDLoc DL
The debug location of the instruction this CaseBlock was produced from.
static CaseCluster range(const ConstantInt *Low, const ConstantInt *High, MachineBasicBlock *MBB, BranchProbability Prob)
Register Reg
The virtual register containing the index of the jump table entry to jump to.
MachineBasicBlock * Default
The MBB of the default bb, which is a successor of the range check MBB.
unsigned JTI
The JumpTableIndex for this jump table in the function.
MachineBasicBlock * MBB
The MBB into which to emit the code for the indirect jump.
std::optional< SDLoc > SL
The debug location of the instruction this JumpTable was produced from.
This contains information for each constraint that we are lowering.
TargetLowering::ConstraintType ConstraintType
Information about the constraint code, e.g.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setConvergent(bool Value=true)
CallLoweringInfo & setDeactivationSymbol(GlobalValue *Sym)
CallLoweringInfo & setCFIType(const ConstantInt *Type)
SmallVector< ISD::InputArg, 32 > Ins
Type * OrigRetTy
Original unlegalized return type.
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setIsPatchPoint(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setIsPreallocated(bool Value=true)
CallLoweringInfo & setConvergenceControlToken(SDValue Token)
SmallVector< ISD::OutputArg, 32 > Outs
Type * RetTy
Same as OrigRetTy, or partially legalized for soft float libcalls.
CallLoweringInfo & setChain(SDValue InChain)
CallLoweringInfo & setPtrAuth(PtrAuthInfo Value)
CallLoweringInfo & setCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList, AttributeSet ResultAttrs={})
This structure is used to pass arguments to makeLibCall function.
MakeLibCallOptions & setDiscardResult(bool Value=true)
This structure contains the information necessary for lowering pointer-authenticating indirect calls.
LLVM_ABI void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)