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.
903 SmallVector<SDValue, 4> Values(ValueVTs.size());
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 && TLI.isZExtFree(Val, RegisterVT))
997 ExtendKind = ISD::ZERO_EXTEND;
998
999 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
1000 NumParts, RegisterVT, V, CallConv, ExtendKind);
1001 Part += NumParts;
1002 }
1003
1004 // Copy the parts into the registers.
1005 SmallVector<SDValue, 8> Chains(NumRegs);
1006 for (unsigned i = 0; i != NumRegs; ++i) {
1007 SDValue Part;
1008 if (!Glue) {
1009 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
1010 } else {
1011 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue);
1012 *Glue = Part.getValue(1);
1013 }
1014
1015 Chains[i] = Part.getValue(0);
1016 }
1017
1018 if (NumRegs == 1 || Glue)
1019 // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is
1020 // flagged to it. That is the CopyToReg nodes and the user are considered
1021 // a single scheduling unit. If we create a TokenFactor and return it as
1022 // chain, then the TokenFactor is both a predecessor (operand) of the
1023 // user as well as a successor (the TF operands are flagged to the user).
1024 // c1, f1 = CopyToReg
1025 // c2, f2 = CopyToReg
1026 // c3 = TokenFactor c1, c2
1027 // ...
1028 // = op c3, ..., f2
1029 Chain = Chains[NumRegs-1];
1030 else
1031 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1032}
1033
1035 unsigned MatchingIdx, const SDLoc &dl,
1036 SelectionDAG &DAG,
1037 std::vector<SDValue> &Ops) const {
1038 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1039
1040 InlineAsm::Flag Flag(Code, Regs.size());
1041 if (HasMatching)
1042 Flag.setMatchingOp(MatchingIdx);
1043 else if (!Regs.empty() && Regs.front().isVirtual()) {
1044 // Put the register class of the virtual registers in the flag word. That
1045 // way, later passes can recompute register class constraints for inline
1046 // assembly as well as normal instructions.
1047 // Don't do this for tied operands that can use the regclass information
1048 // from the def.
1050 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
1051 Flag.setRegClass(RC->getID());
1052 }
1053
1054 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
1055 Ops.push_back(Res);
1056
1057 if (Code == InlineAsm::Kind::Clobber) {
1058 // Clobbers should always have a 1:1 mapping with registers, and may
1059 // reference registers that have illegal (e.g. vector) types. Hence, we
1060 // shouldn't try to apply any sort of splitting logic to them.
1061 assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
1062 "No 1:1 mapping from clobbers to regs?");
1064 (void)SP;
1065 for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
1066 Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
1067 assert(
1068 (Regs[I] != SP ||
1070 "If we clobbered the stack pointer, MFI should know about it.");
1071 }
1072 return;
1073 }
1074
1075 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1076 MVT RegisterVT = RegVTs[Value];
1077 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1078 RegisterVT);
1079 for (unsigned i = 0; i != NumRegs; ++i) {
1080 assert(Reg < Regs.size() && "Mismatch in # registers expected");
1081 Register TheReg = Regs[Reg++];
1082 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1083 }
1084 }
1085}
1086
1090 unsigned I = 0;
1091 for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1092 unsigned RegCount = std::get<0>(CountAndVT);
1093 MVT RegisterVT = std::get<1>(CountAndVT);
1094 TypeSize RegisterSize = RegisterVT.getSizeInBits();
1095 for (unsigned E = I + RegCount; I != E; ++I)
1096 OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1097 }
1098 return OutVec;
1099}
1100
1102 AssumptionCache *ac, const TargetLibraryInfo *li,
1103 const TargetTransformInfo &TTI) {
1104 BatchAA = aa;
1105 AC = ac;
1106 GFI = gfi;
1107 LibInfo = li;
1108 Context = DAG.getContext();
1109 LPadToCallSiteMap.clear();
1110 this->TTI = &TTI;
1111 SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1112 AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
1113 *DAG.getMachineFunction().getFunction().getParent());
1114}
1115
1117 NodeMap.clear();
1118 UnusedArgNodeMap.clear();
1119 PendingLoads.clear();
1120 PendingExports.clear();
1121 PendingConstrainedFP.clear();
1122 PendingConstrainedFPStrict.clear();
1123 CurInst = nullptr;
1124 HasTailCall = false;
1125 SDNodeOrder = LowestSDNodeOrder;
1126 StatepointLowering.clear();
1127}
1128
1130 DanglingDebugInfoMap.clear();
1131}
1132
1133// Update DAG root to include dependencies on Pending chains.
1134SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1135 SDValue Root = DAG.getRoot();
1136
1137 if (Pending.empty())
1138 return Root;
1139
1140 // Add current root to PendingChains, unless we already indirectly
1141 // depend on it.
1142 if (Root.getOpcode() != ISD::EntryToken) {
1143 unsigned i = 0, e = Pending.size();
1144 for (; i != e; ++i) {
1145 assert(Pending[i].getNode()->getNumOperands() > 1);
1146 if (Pending[i].getNode()->getOperand(0) == Root)
1147 break; // Don't add the root if we already indirectly depend on it.
1148 }
1149
1150 if (i == e)
1151 Pending.push_back(Root);
1152 }
1153
1154 if (Pending.size() == 1)
1155 Root = Pending[0];
1156 else
1157 Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1158
1159 DAG.setRoot(Root);
1160 Pending.clear();
1161 return Root;
1162}
1163
1167
1169 // If the new exception behavior differs from that of the pending
1170 // ones, chain up them and update the root.
1171 switch (EB) {
1174 // Floating-point exceptions produced by such operations are not intended
1175 // to be observed, so the sequence of these operations does not need to be
1176 // preserved.
1177 //
1178 // They however must not be mixed with the instructions that have strict
1179 // exception behavior. Placing an operation with 'ebIgnore' behavior between
1180 // 'ebStrict' operations could distort the observed exception behavior.
1181 if (!PendingConstrainedFPStrict.empty()) {
1182 assert(PendingConstrainedFP.empty());
1183 updateRoot(PendingConstrainedFPStrict);
1184 }
1185 break;
1187 // Floating-point exception produced by these operations may be observed, so
1188 // they must be correctly chained. If trapping on FP exceptions is
1189 // disabled, the exceptions can be observed only by functions that read
1190 // exception flags, like 'llvm.get_fpenv' or 'fetestexcept'. It means that
1191 // the order of operations is not significant between barriers.
1192 //
1193 // If trapping is enabled, each operation becomes an implicit observation
1194 // point, so the operations must be sequenced according their original
1195 // source order.
1196 if (!PendingConstrainedFP.empty()) {
1197 assert(PendingConstrainedFPStrict.empty());
1198 updateRoot(PendingConstrainedFP);
1199 }
1200 // TODO: Add support for trapping-enabled scenarios.
1201 }
1202 return DAG.getRoot();
1203}
1204
1206 // Chain up all pending constrained intrinsics together with all
1207 // pending loads, by simply appending them to PendingLoads and
1208 // then calling getMemoryRoot().
1209 PendingLoads.reserve(PendingLoads.size() +
1210 PendingConstrainedFP.size() +
1211 PendingConstrainedFPStrict.size());
1212 PendingLoads.append(PendingConstrainedFP.begin(),
1213 PendingConstrainedFP.end());
1214 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1215 PendingConstrainedFPStrict.end());
1216 PendingConstrainedFP.clear();
1217 PendingConstrainedFPStrict.clear();
1218 return getMemoryRoot();
1219}
1220
1222 // We need to emit pending fpexcept.strict constrained intrinsics,
1223 // so append them to the PendingExports list.
1224 PendingExports.append(PendingConstrainedFPStrict.begin(),
1225 PendingConstrainedFPStrict.end());
1226 PendingConstrainedFPStrict.clear();
1227 return updateRoot(PendingExports);
1228}
1229
1231 DILocalVariable *Variable,
1233 DebugLoc DL) {
1234 assert(Variable && "Missing variable");
1235
1236 // Check if address has undef value.
1237 if (!Address || isa<UndefValue>(Address) ||
1238 (Address->use_empty() && !isa<Argument>(Address))) {
1239 LLVM_DEBUG(
1240 dbgs()
1241 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1242 return;
1243 }
1244
1245 bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1246
1247 SDValue &N = NodeMap[Address];
1248 if (!N.getNode() && isa<Argument>(Address))
1249 // Check unused arguments map.
1250 N = UnusedArgNodeMap[Address];
1251 SDDbgValue *SDV;
1252 if (N.getNode()) {
1253 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1254 Address = BCI->getOperand(0);
1255 // Parameters are handled specially.
1256 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1257 if (IsParameter && FINode) {
1258 // Byval parameter. We have a frame index at this point.
1259 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1260 /*IsIndirect*/ true, DL, SDNodeOrder);
1261 } else if (isa<Argument>(Address)) {
1262 // Address is an argument, so try to emit its dbg value using
1263 // virtual register info from the FuncInfo.ValueMap.
1264 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1265 FuncArgumentDbgValueKind::Declare, N);
1266 return;
1267 } else {
1268 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1269 true, DL, SDNodeOrder);
1270 }
1271 DAG.AddDbgValue(SDV, IsParameter);
1272 } else {
1273 // If Address is an argument then try to emit its dbg value using
1274 // virtual register info from the FuncInfo.ValueMap.
1275 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1276 FuncArgumentDbgValueKind::Declare, N)) {
1277 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1278 << " (could not emit func-arg dbg_value)\n");
1279 }
1280 }
1281}
1282
1284 // Add SDDbgValue nodes for any var locs here. Do so before updating
1285 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1286 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1287 // Add SDDbgValue nodes for any var locs here. Do so before updating
1288 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1289 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1290 It != End; ++It) {
1291 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1292 dropDanglingDebugInfo(Var, It->Expr);
1293 if (It->Values.isKillLocation(It->Expr)) {
1294 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1295 continue;
1296 }
1297 SmallVector<Value *> Values(It->Values.location_ops());
1298 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1299 It->Values.hasArgList())) {
1300 SmallVector<Value *, 4> Vals(It->Values.location_ops());
1302 FnVarLocs->getDILocalVariable(It->VariableID),
1303 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1304 }
1305 }
1306 }
1307
1308 // We must skip DbgVariableRecords if they've already been processed above as
1309 // we have just emitted the debug values resulting from assignment tracking
1310 // analysis, making any existing DbgVariableRecords redundant (and probably
1311 // less correct). We still need to process DbgLabelRecords. This does sink
1312 // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1313 // be important as it does so deterministcally and ordering between
1314 // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1315 // printing).
1316 bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1317 // Is there is any debug-info attached to this instruction, in the form of
1318 // DbgRecord non-instruction debug-info records.
1319 for (DbgRecord &DR : I.getDbgRecordRange()) {
1320 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1321 assert(DLR->getLabel() && "Missing label");
1322 SDDbgLabel *SDV =
1323 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1324 DAG.AddDbgLabel(SDV);
1325 continue;
1326 }
1327
1328 if (SkipDbgVariableRecords)
1329 continue;
1331 DILocalVariable *Variable = DVR.getVariable();
1334
1336 if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1337 continue;
1338 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1339 << "\n");
1341 DVR.getDebugLoc());
1342 continue;
1343 }
1344
1345 // A DbgVariableRecord with no locations is a kill location.
1347 if (Values.empty()) {
1349 SDNodeOrder);
1350 continue;
1351 }
1352
1353 // A DbgVariableRecord with an undef or absent location is also a kill
1354 // location.
1355 if (llvm::any_of(Values,
1356 [](Value *V) { return !V || isa<UndefValue>(V); })) {
1358 SDNodeOrder);
1359 continue;
1360 }
1361
1362 bool IsVariadic = DVR.hasArgList();
1363 if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1364 SDNodeOrder, IsVariadic)) {
1365 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1366 DVR.getDebugLoc(), SDNodeOrder);
1367 }
1368 }
1369}
1370
1372 visitDbgInfo(I);
1373
1374 // Set up outgoing PHI node register values before emitting the terminator.
1375 if (I.isTerminator()) {
1376 HandlePHINodesInSuccessorBlocks(I.getParent());
1377 }
1378
1379 ++SDNodeOrder;
1380 CurInst = &I;
1381
1382 // Set inserted listener only if required.
1383 bool NodeInserted = false;
1384 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1385 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1386 MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1387 if (PCSectionsMD || MMRA) {
1388 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1389 DAG, [&](SDNode *) { NodeInserted = true; });
1390 }
1391
1392 visit(I.getOpcode(), I);
1393
1394 if (!I.isTerminator() && !HasTailCall &&
1395 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1397
1398 // Handle metadata.
1399 if (PCSectionsMD || MMRA) {
1400 auto It = NodeMap.find(&I);
1401 if (It != NodeMap.end()) {
1402 if (PCSectionsMD)
1403 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1404 if (MMRA)
1405 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1406 } else if (NodeInserted) {
1407 // This should not happen; if it does, don't let it go unnoticed so we can
1408 // fix it. Relevant visit*() function is probably missing a setValue().
1409 errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1410 << I.getModule()->getName() << "]\n";
1411 LLVM_DEBUG(I.dump());
1412 assert(false);
1413 }
1414 }
1415
1416 CurInst = nullptr;
1417}
1418
1419void SelectionDAGBuilder::visitPHI(const PHINode &) {
1420 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1421}
1422
1423void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1424 // Note: this doesn't use InstVisitor, because it has to work with
1425 // ConstantExpr's in addition to instructions.
1426 switch (Opcode) {
1427 default: llvm_unreachable("Unknown instruction type encountered!");
1428 // Build the switch statement using the Instruction.def file.
1429#define HANDLE_INST(NUM, OPCODE, CLASS) \
1430 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1431#include "llvm/IR/Instruction.def"
1432 }
1433}
1434
1436 DILocalVariable *Variable,
1437 DebugLoc DL, unsigned Order,
1440 // For variadic dbg_values we will now insert poison.
1441 // FIXME: We can potentially recover these!
1443 for (const Value *V : Values) {
1444 auto *Poison = PoisonValue::get(V->getType());
1446 }
1447 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1448 /*IsIndirect=*/false, DL, Order,
1449 /*IsVariadic=*/true);
1450 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1451 return true;
1452}
1453
1455 DILocalVariable *Var,
1456 DIExpression *Expr,
1457 bool IsVariadic, DebugLoc DL,
1458 unsigned Order) {
1459 if (IsVariadic) {
1460 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1461 return;
1462 }
1463 // TODO: Dangling debug info will eventually either be resolved or produce
1464 // a poison DBG_VALUE. However in the resolution case, a gap may appear
1465 // between the original dbg.value location and its resolved DBG_VALUE,
1466 // which we should ideally fill with an extra poison DBG_VALUE.
1467 assert(Values.size() == 1);
1468 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1469}
1470
1472 const DIExpression *Expr) {
1473 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1474 DIVariable *DanglingVariable = DDI.getVariable();
1475 DIExpression *DanglingExpr = DDI.getExpression();
1476 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1477 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1478 << printDDI(nullptr, DDI) << "\n");
1479 return true;
1480 }
1481 return false;
1482 };
1483
1484 for (auto &DDIMI : DanglingDebugInfoMap) {
1485 DanglingDebugInfoVector &DDIV = DDIMI.second;
1486
1487 // If debug info is to be dropped, run it through final checks to see
1488 // whether it can be salvaged.
1489 for (auto &DDI : DDIV)
1490 if (isMatchingDbgValue(DDI))
1491 salvageUnresolvedDbgValue(DDIMI.first, DDI);
1492
1493 erase_if(DDIV, isMatchingDbgValue);
1494 }
1495}
1496
1497// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1498// generate the debug data structures now that we've seen its definition.
1500 SDValue Val) {
1501 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1502 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1503 return;
1504
1505 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1506 for (auto &DDI : DDIV) {
1507 DebugLoc DL = DDI.getDebugLoc();
1508 unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
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 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1523 FuncArgumentDbgValueKind::Value, Val)) {
1524 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1525 << printDDI(V, DDI) << "\n");
1526 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump());
1527 // Increase the SDNodeOrder for the DbgValue here to make sure it is
1528 // inserted after the definition of Val when emitting the instructions
1529 // after ISel. An alternative could be to teach
1530 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1531 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1532 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1533 << ValSDNodeOrder << "\n");
1534 SDV = getDbgValue(Val, Variable, Expr, DL,
1535 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1536 DAG.AddDbgValue(SDV, false);
1537 } else
1538 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1539 << printDDI(V, DDI)
1540 << " in EmitFuncArgumentDbgValue\n");
1541 } else {
1542 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1543 << "\n");
1544 auto Poison = PoisonValue::get(V->getType());
1545 auto SDV =
1546 DAG.getConstantDbgValue(Variable, Expr, Poison, DL, DbgSDNodeOrder);
1547 DAG.AddDbgValue(SDV, false);
1548 }
1549 }
1550 DDIV.clear();
1551}
1552
1554 DanglingDebugInfo &DDI) {
1555 // TODO: For the variadic implementation, instead of only checking the fail
1556 // state of `handleDebugValue`, we need know specifically which values were
1557 // invalid, so that we attempt to salvage only those values when processing
1558 // a DIArgList.
1559 const Value *OrigV = V;
1560 DILocalVariable *Var = DDI.getVariable();
1561 DIExpression *Expr = DDI.getExpression();
1562 DebugLoc DL = DDI.getDebugLoc();
1563 unsigned SDOrder = DDI.getSDNodeOrder();
1564
1565 // Currently we consider only dbg.value intrinsics -- we tell the salvager
1566 // that DW_OP_stack_value is desired.
1567 bool StackValue = true;
1568
1569 // Can this Value can be encoded without any further work?
1570 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1571 return;
1572
1573 // Attempt to salvage back through as many instructions as possible. Bail if
1574 // a non-instruction is seen, such as a constant expression or global
1575 // variable. FIXME: Further work could recover those too.
1576 while (isa<Instruction>(V)) {
1577 const Instruction &VAsInst = *cast<const Instruction>(V);
1578 // Temporary "0", awaiting real implementation.
1580 SmallVector<Value *, 4> AdditionalValues;
1581 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1582 Expr->getNumLocationOperands(), Ops,
1583 AdditionalValues);
1584 // If we cannot salvage any further, and haven't yet found a suitable debug
1585 // expression, bail out.
1586 if (!V)
1587 break;
1588
1589 // TODO: If AdditionalValues isn't empty, then the salvage can only be
1590 // represented with a DBG_VALUE_LIST, so we give up. When we have support
1591 // here for variadic dbg_values, remove that condition.
1592 if (!AdditionalValues.empty())
1593 break;
1594
1595 // New value and expr now represent this debuginfo.
1596 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1597
1598 // Some kind of simplification occurred: check whether the operand of the
1599 // salvaged debug expression can be encoded in this DAG.
1600 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1601 LLVM_DEBUG(
1602 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n"
1603 << *OrigV << "\nBy stripping back to:\n " << *V << "\n");
1604 return;
1605 }
1606 }
1607
1608 // This was the final opportunity to salvage this debug information, and it
1609 // couldn't be done. Place a poison DBG_VALUE at this location to terminate
1610 // any earlier variable location.
1611 assert(OrigV && "V shouldn't be null");
1612 auto *Poison = PoisonValue::get(OrigV->getType());
1613 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Poison, DL, SDNodeOrder);
1614 DAG.AddDbgValue(SDV, false);
1615 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n "
1616 << printDDI(OrigV, DDI) << "\n");
1617}
1618
1620 DIExpression *Expr,
1621 DebugLoc DbgLoc,
1622 unsigned Order) {
1626 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1627 /*IsVariadic*/ false);
1628}
1629
1631 DILocalVariable *Var,
1632 DIExpression *Expr, DebugLoc DbgLoc,
1633 unsigned Order, bool IsVariadic) {
1634 if (Values.empty())
1635 return true;
1636
1637 // Filter EntryValue locations out early.
1638 if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1639 return true;
1640
1641 SmallVector<SDDbgOperand> LocationOps;
1642 SmallVector<SDNode *> Dependencies;
1643 for (const Value *V : Values) {
1644 // Constant value.
1647 LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1648 continue;
1649 }
1650
1651 // Look through IntToPtr constants.
1652 if (auto *CE = dyn_cast<ConstantExpr>(V))
1653 if (CE->getOpcode() == Instruction::IntToPtr) {
1654 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1655 continue;
1656 }
1657
1658 // If the Value is a frame index, we can create a FrameIndex debug value
1659 // without relying on the DAG at all.
1660 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1661 auto SI = FuncInfo.StaticAllocaMap.find(AI);
1662 if (SI != FuncInfo.StaticAllocaMap.end()) {
1663 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1664 continue;
1665 }
1666 }
1667
1668 // Do not use getValue() in here; we don't want to generate code at
1669 // this point if it hasn't been done yet.
1670 SDValue N = NodeMap[V];
1671 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1672 N = UnusedArgNodeMap[V];
1673
1674 if (N.getNode()) {
1675 // Only emit func arg dbg value for non-variadic dbg.values for now.
1676 if (!IsVariadic &&
1677 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1678 FuncArgumentDbgValueKind::Value, N))
1679 return true;
1680 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1681 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1682 // describe stack slot locations.
1683 //
1684 // Consider "int x = 0; int *px = &x;". There are two kinds of
1685 // interesting debug values here after optimization:
1686 //
1687 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
1688 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1689 //
1690 // Both describe the direct values of their associated variables.
1691 Dependencies.push_back(N.getNode());
1692 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1693 continue;
1694 }
1695 LocationOps.emplace_back(
1696 SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1697 continue;
1698 }
1699
1700 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1701 // Special rules apply for the first dbg.values of parameter variables in a
1702 // function. Identify them by the fact they reference Argument Values, that
1703 // they're parameters, and they are parameters of the current function. We
1704 // need to let them dangle until they get an SDNode.
1705 bool IsParamOfFunc =
1706 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1707 if (IsParamOfFunc)
1708 return false;
1709
1710 // The value is not used in this block yet (or it would have an SDNode).
1711 // We still want the value to appear for the user if possible -- if it has
1712 // an associated VReg, we can refer to that instead.
1713 auto VMI = FuncInfo.ValueMap.find(V);
1714 if (VMI != FuncInfo.ValueMap.end()) {
1715 Register Reg = VMI->second;
1716 // If this is a PHI node, it may be split up into several MI PHI nodes
1717 // (in FunctionLoweringInfo::set).
1718 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1719 V->getType(), std::nullopt);
1720 if (RFV.occupiesMultipleRegs()) {
1721 // FIXME: We could potentially support variadic dbg_values here.
1722 if (IsVariadic)
1723 return false;
1724 unsigned Offset = 0;
1725 unsigned BitsToDescribe = 0;
1726 if (auto VarSize = Var->getSizeInBits())
1727 BitsToDescribe = *VarSize;
1728 if (auto Fragment = Expr->getFragmentInfo())
1729 BitsToDescribe = Fragment->SizeInBits;
1730 for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1731 // Bail out if all bits are described already.
1732 if (Offset >= BitsToDescribe)
1733 break;
1734 // TODO: handle scalable vectors.
1735 unsigned RegisterSize = RegAndSize.second;
1736 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1737 ? BitsToDescribe - Offset
1738 : RegisterSize;
1739 auto FragmentExpr = DIExpression::createFragmentExpression(
1740 Expr, Offset, FragmentSize);
1741 if (!FragmentExpr)
1742 continue;
1743 SDDbgValue *SDV = DAG.getVRegDbgValue(
1744 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1745 DAG.AddDbgValue(SDV, false);
1746 Offset += RegisterSize;
1747 }
1748 return true;
1749 }
1750 // We can use simple vreg locations for variadic dbg_values as well.
1751 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1752 continue;
1753 }
1754 // We failed to create a SDDbgOperand for V.
1755 return false;
1756 }
1757
1758 // We have created a SDDbgOperand for each Value in Values.
1759 assert(!LocationOps.empty());
1760 SDDbgValue *SDV =
1761 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1762 /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1763 DAG.AddDbgValue(SDV, /*isParameter=*/false);
1764 return true;
1765}
1766
1768 // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1769 for (auto &Pair : DanglingDebugInfoMap)
1770 for (auto &DDI : Pair.second)
1771 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1773}
1774
1775/// getCopyFromRegs - If there was virtual register allocated for the value V
1776/// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1779 SDValue Result;
1780
1781 if (It != FuncInfo.ValueMap.end()) {
1782 Register InReg = It->second;
1783
1784 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1785 DAG.getDataLayout(), InReg, Ty,
1786 std::nullopt); // This is not an ABI copy.
1787 SDValue Chain = DAG.getEntryNode();
1788 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1789 V);
1790 resolveDanglingDebugInfo(V, Result);
1791 }
1792
1793 return Result;
1794}
1795
1796/// getValue - Return an SDValue for the given Value.
1798 // If we already have an SDValue for this value, use it. It's important
1799 // to do this first, so that we don't create a CopyFromReg if we already
1800 // have a regular SDValue.
1801 SDValue &N = NodeMap[V];
1802 if (N.getNode()) return N;
1803
1804 // If there's a virtual register allocated and initialized for this
1805 // value, use it.
1806 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1807 return copyFromReg;
1808
1809 // Otherwise create a new SDValue and remember it.
1810 SDValue Val = getValueImpl(V);
1811 NodeMap[V] = Val;
1813 return Val;
1814}
1815
1816/// getNonRegisterValue - Return an SDValue for the given Value, but
1817/// don't look in FuncInfo.ValueMap for a virtual register.
1819 // If we already have an SDValue for this value, use it.
1820 SDValue &N = NodeMap[V];
1821 if (N.getNode()) {
1822 if (isIntOrFPConstant(N)) {
1823 // Remove the debug location from the node as the node is about to be used
1824 // in a location which may differ from the original debug location. This
1825 // is relevant to Constant and ConstantFP nodes because they can appear
1826 // as constant expressions inside PHI nodes.
1827 N->setDebugLoc(DebugLoc());
1828 }
1829 return N;
1830 }
1831
1832 // Otherwise create a new SDValue and remember it.
1833 SDValue Val = getValueImpl(V);
1834 NodeMap[V] = Val;
1836 return Val;
1837}
1838
1839/// getValueImpl - Helper function for getValue and getNonRegisterValue.
1840/// Create an SDValue for the given value.
1842 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1843
1844 if (const Constant *C = dyn_cast<Constant>(V)) {
1845 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1846
1847 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
1848 SDLoc DL = getCurSDLoc();
1849
1850 // DAG.getConstant() may attempt to legalise the vector constant which can
1851 // significantly change the combines applied to the DAG. To reduce the
1852 // divergence when enabling ConstantInt based vectors we try to construct
1853 // the DAG in the same way as shufflevector based splats. TODO: The
1854 // divergence sometimes leads to better optimisations. Ideally we should
1855 // prevent DAG.getConstant() from legalising too early but there are some
1856 // degradations preventing this.
1857 if (VT.isScalableVector())
1858 return DAG.getNode(
1859 ISD::SPLAT_VECTOR, DL, VT,
1860 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1861 if (VT.isFixedLengthVector())
1862 return DAG.getSplatBuildVector(
1863 VT, DL,
1864 DAG.getConstant(CI->getValue(), DL, VT.getVectorElementType()));
1865 return DAG.getConstant(*CI, DL, VT);
1866 }
1867
1868 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1869 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1870
1871 if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1872 return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1873 getValue(CPA->getPointer()), getValue(CPA->getKey()),
1874 getValue(CPA->getAddrDiscriminator()),
1875 getValue(CPA->getDiscriminator()));
1876 }
1877
1879 return DAG.getConstant(0, getCurSDLoc(), VT);
1880
1881 if (match(C, m_VScale()))
1882 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1883
1884 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1885 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1886
1887 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1888 return isa<PoisonValue>(C) ? DAG.getPOISON(VT) : DAG.getUNDEF(VT);
1889
1890 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1891 visit(CE->getOpcode(), *CE);
1892 SDValue N1 = NodeMap[V];
1893 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1894 return N1;
1895 }
1896
1898 SmallVector<SDValue, 4> Constants;
1899 for (const Use &U : C->operands()) {
1900 SDNode *Val = getValue(U).getNode();
1901 // If the operand is an empty aggregate, there are no values.
1902 if (!Val) continue;
1903 // Add each leaf value from the operand to the Constants list
1904 // to form a flattened list of all the values.
1905 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1906 Constants.push_back(SDValue(Val, i));
1907 }
1908
1909 return DAG.getMergeValues(Constants, getCurSDLoc());
1910 }
1911
1912 if (const ConstantDataSequential *CDS =
1915 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1916 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1917 // Add each leaf value from the operand to the Constants list
1918 // to form a flattened list of all the values.
1919 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1920 Ops.push_back(SDValue(Val, i));
1921 }
1922
1923 if (isa<ArrayType>(CDS->getType()))
1924 return DAG.getMergeValues(Ops, getCurSDLoc());
1925 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1926 }
1927
1928 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1930 "Unknown struct or array constant!");
1931
1932 SmallVector<EVT, 4> ValueVTs;
1933 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1934 unsigned NumElts = ValueVTs.size();
1935 if (NumElts == 0)
1936 return SDValue(); // empty struct
1937 SmallVector<SDValue, 4> Constants(NumElts);
1938 for (unsigned i = 0; i != NumElts; ++i) {
1939 EVT EltVT = ValueVTs[i];
1940 if (isa<UndefValue>(C))
1941 Constants[i] = DAG.getUNDEF(EltVT);
1942 else if (EltVT.isFloatingPoint())
1943 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1944 else
1945 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1946 }
1947
1948 return DAG.getMergeValues(Constants, getCurSDLoc());
1949 }
1950
1951 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1952 return DAG.getBlockAddress(BA, VT);
1953
1954 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1955 return getValue(Equiv->getGlobalValue());
1956
1957 if (const auto *NC = dyn_cast<NoCFIValue>(C))
1958 return getValue(NC->getGlobalValue());
1959
1960 if (VT == MVT::aarch64svcount) {
1961 assert(C->isNullValue() && "Can only zero this target type!");
1962 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1963 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1964 }
1965
1966 if (VT.isRISCVVectorTuple()) {
1967 assert(C->isNullValue() && "Can only zero this target type!");
1968 return DAG.getNode(
1970 DAG.getNode(
1972 EVT::getVectorVT(*DAG.getContext(), MVT::i8,
1973 VT.getSizeInBits().getKnownMinValue() / 8, true),
1974 DAG.getConstant(0, getCurSDLoc(), MVT::getIntegerVT(8))));
1975 }
1976
1977 VectorType *VecTy = cast<VectorType>(V->getType());
1978
1979 // Now that we know the number and type of the elements, get that number of
1980 // elements into the Ops array based on what kind of constant it is.
1981 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1983 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1984 for (unsigned i = 0; i != NumElements; ++i)
1985 Ops.push_back(getValue(CV->getOperand(i)));
1986
1987 return DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1988 }
1989
1991 EVT EltVT =
1992 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1993
1994 SDValue Op;
1995 if (EltVT.isFloatingPoint())
1996 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1997 else
1998 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1999
2000 return DAG.getSplat(VT, getCurSDLoc(), Op);
2001 }
2002
2003 llvm_unreachable("Unknown vector constant");
2004 }
2005
2006 // If this is a static alloca, generate it as the frameindex instead of
2007 // computation.
2008 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
2010 FuncInfo.StaticAllocaMap.find(AI);
2011 if (SI != FuncInfo.StaticAllocaMap.end())
2012 return DAG.getFrameIndex(
2013 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
2014 }
2015
2016 // If this is an instruction which fast-isel has deferred, select it now.
2017 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
2018 Register InReg = FuncInfo.InitializeRegForValue(Inst);
2019
2020 std::optional<CallingConv::ID> CallConv;
2021 auto *CB = dyn_cast<CallBase>(Inst);
2022 if (CB && !CB->isInlineAsm())
2023 CallConv = CB->getCallingConv();
2024
2025 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
2026 Inst->getType(), CallConv);
2027 SDValue Chain = DAG.getEntryNode();
2028 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
2029 }
2030
2031 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
2032 return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
2033
2034 if (const auto *BB = dyn_cast<BasicBlock>(V))
2035 return DAG.getBasicBlock(FuncInfo.getMBB(BB));
2036
2037 llvm_unreachable("Can't get register for value!");
2038}
2039
2040void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
2042 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
2043 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
2044 bool IsSEH = isAsynchronousEHPersonality(Pers);
2045 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
2046 if (IsSEH) {
2047 // For SEH, EHCont Guard needs to know that this catchpad is a target.
2048 CatchPadMBB->setIsEHContTarget(true);
2050 } else
2051 CatchPadMBB->setIsEHScopeEntry();
2052 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
2053 if (IsMSVCCXX || IsCoreCLR)
2054 CatchPadMBB->setIsEHFuncletEntry();
2055}
2056
2057void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
2058 // Update machine-CFG edge.
2059 MachineBasicBlock *TargetMBB = FuncInfo.getMBB(I.getSuccessor());
2060 FuncInfo.MBB->addSuccessor(TargetMBB);
2061
2062 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2063 bool IsSEH = isAsynchronousEHPersonality(Pers);
2064 if (IsSEH) {
2065 // If this is not a fall-through branch or optimizations are switched off,
2066 // emit the branch.
2067 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
2068 TM.getOptLevel() == CodeGenOptLevel::None)
2069 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2070 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
2071 return;
2072 }
2073
2074 // For non-SEH, EHCont Guard needs to know that this catchret is a target.
2075 TargetMBB->setIsEHContTarget(true);
2076 DAG.getMachineFunction().setHasEHContTarget(true);
2077
2078 // Figure out the funclet membership for the catchret's successor.
2079 // This will be used by the FuncletLayout pass to determine how to order the
2080 // BB's.
2081 // A 'catchret' returns to the outer scope's color.
2082 Value *ParentPad = I.getCatchSwitchParentPad();
2083 const BasicBlock *SuccessorColor;
2084 if (isa<ConstantTokenNone>(ParentPad))
2085 SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2086 else
2087 SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2088 assert(SuccessorColor && "No parent funclet for catchret!");
2089 MachineBasicBlock *SuccessorColorMBB = FuncInfo.getMBB(SuccessorColor);
2090 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2091
2092 // Create the terminator node.
2093 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2094 getControlRoot(), DAG.getBasicBlock(TargetMBB),
2095 DAG.getBasicBlock(SuccessorColorMBB));
2096 DAG.setRoot(Ret);
2097}
2098
2099void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2100 // Don't emit any special code for the cleanuppad instruction. It just marks
2101 // the start of an EH scope/funclet.
2102 FuncInfo.MBB->setIsEHScopeEntry();
2103 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2104 if (Pers != EHPersonality::Wasm_CXX) {
2105 FuncInfo.MBB->setIsEHFuncletEntry();
2106 FuncInfo.MBB->setIsCleanupFuncletEntry();
2107 }
2108}
2109
2110/// When an invoke or a cleanupret unwinds to the next EH pad, there are
2111/// many places it could ultimately go. In the IR, we have a single unwind
2112/// destination, but in the machine CFG, we enumerate all the possible blocks.
2113/// This function skips over imaginary basic blocks that hold catchswitch
2114/// instructions, and finds all the "real" machine
2115/// basic block destinations. As those destinations may not be successors of
2116/// EHPadBB, here we also calculate the edge probability to those destinations.
2117/// The passed-in Prob is the edge probability to EHPadBB.
2119 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2120 BranchProbability Prob,
2121 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2122 &UnwindDests) {
2123 EHPersonality Personality =
2125 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2126 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2127 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2128 bool IsSEH = isAsynchronousEHPersonality(Personality);
2129
2130 while (EHPadBB) {
2132 BasicBlock *NewEHPadBB = nullptr;
2133 if (isa<LandingPadInst>(Pad)) {
2134 // Stop on landingpads. They are not funclets.
2135 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2136 break;
2137 } else if (isa<CleanupPadInst>(Pad)) {
2138 // Stop on cleanup pads. Cleanups are always funclet entries for all known
2139 // personalities except Wasm. And in Wasm this becomes a catch_all(_ref),
2140 // which always catches an exception.
2141 UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
2142 UnwindDests.back().first->setIsEHScopeEntry();
2143 // In Wasm, EH scopes are not funclets
2144 if (!IsWasmCXX)
2145 UnwindDests.back().first->setIsEHFuncletEntry();
2146 break;
2147 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2148 // Add the catchpad handlers to the possible destinations.
2149 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2150 UnwindDests.emplace_back(FuncInfo.getMBB(CatchPadBB), Prob);
2151 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2152 if (IsMSVCCXX || IsCoreCLR)
2153 UnwindDests.back().first->setIsEHFuncletEntry();
2154 if (!IsSEH)
2155 UnwindDests.back().first->setIsEHScopeEntry();
2156 }
2157 NewEHPadBB = CatchSwitch->getUnwindDest();
2158 } else {
2159 continue;
2160 }
2161
2162 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2163 if (BPI && NewEHPadBB)
2164 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2165 EHPadBB = NewEHPadBB;
2166 }
2167}
2168
2169void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2170 // Update successor info.
2172 auto UnwindDest = I.getUnwindDest();
2173 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2174 BranchProbability UnwindDestProb =
2175 (BPI && UnwindDest)
2176 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2178 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2179 for (auto &UnwindDest : UnwindDests) {
2180 UnwindDest.first->setIsEHPad();
2181 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2182 }
2183 FuncInfo.MBB->normalizeSuccProbs();
2184
2185 // Create the terminator node.
2186 MachineBasicBlock *CleanupPadMBB =
2187 FuncInfo.getMBB(I.getCleanupPad()->getParent());
2188 SDValue Ret = DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other,
2189 getControlRoot(), DAG.getBasicBlock(CleanupPadMBB));
2190 DAG.setRoot(Ret);
2191}
2192
2193void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2194 report_fatal_error("visitCatchSwitch not yet implemented!");
2195}
2196
2197void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2198 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2199 auto &DL = DAG.getDataLayout();
2200 SDValue Chain = getControlRoot();
2203
2204 // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2205 // lower
2206 //
2207 // %val = call <ty> @llvm.experimental.deoptimize()
2208 // ret <ty> %val
2209 //
2210 // differently.
2211 if (I.getParent()->getTerminatingDeoptimizeCall()) {
2213 return;
2214 }
2215
2216 if (!FuncInfo.CanLowerReturn) {
2217 Register DemoteReg = FuncInfo.DemoteRegister;
2218
2219 // Emit a store of the return value through the virtual register.
2220 // Leave Outs empty so that LowerReturn won't try to load return
2221 // registers the usual way.
2222 MVT PtrValueVT = TLI.getPointerTy(DL, DL.getAllocaAddrSpace());
2223 SDValue RetPtr =
2224 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVT);
2225 SDValue RetOp = getValue(I.getOperand(0));
2226
2227 SmallVector<EVT, 4> ValueVTs, MemVTs;
2228 SmallVector<uint64_t, 4> Offsets;
2229 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2230 &Offsets, 0);
2231 unsigned NumValues = ValueVTs.size();
2232
2233 SmallVector<SDValue, 4> Chains(NumValues);
2234 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2235 for (unsigned i = 0; i != NumValues; ++i) {
2236 // An aggregate return value cannot wrap around the address space, so
2237 // offsets to its parts don't wrap either.
2238 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2239 TypeSize::getFixed(Offsets[i]));
2240
2241 SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2242 if (MemVTs[i] != ValueVTs[i])
2243 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2244 Chains[i] = DAG.getStore(
2245 Chain, getCurSDLoc(), Val,
2246 // FIXME: better loc info would be nice.
2247 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2248 commonAlignment(BaseAlign, Offsets[i]));
2249 }
2250
2251 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2252 MVT::Other, Chains);
2253 } else if (I.getNumOperands() != 0) {
2255 ComputeValueTypes(DL, I.getOperand(0)->getType(), Types);
2256 unsigned NumValues = Types.size();
2257 if (NumValues) {
2258 SDValue RetOp = getValue(I.getOperand(0));
2259
2260 const Function *F = I.getParent()->getParent();
2261
2262 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2263 I.getOperand(0)->getType(), F->getCallingConv(),
2264 /*IsVarArg*/ false, DL);
2265
2266 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2267 if (F->getAttributes().hasRetAttr(Attribute::SExt))
2268 ExtendKind = ISD::SIGN_EXTEND;
2269 else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2270 ExtendKind = ISD::ZERO_EXTEND;
2271
2272 LLVMContext &Context = F->getContext();
2273 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2274
2275 for (unsigned j = 0; j != NumValues; ++j) {
2276 EVT VT = TLI.getValueType(DL, Types[j]);
2277
2278 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2279 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2280
2281 CallingConv::ID CC = F->getCallingConv();
2282
2283 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2284 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2285 SmallVector<SDValue, 4> Parts(NumParts);
2287 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2288 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2289
2290 // 'inreg' on function refers to return value
2291 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2292 if (RetInReg)
2293 Flags.setInReg();
2294
2295 if (I.getOperand(0)->getType()->isPointerTy()) {
2296 Flags.setPointer();
2297 Flags.setPointerAddrSpace(
2298 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2299 }
2300
2301 if (NeedsRegBlock) {
2302 Flags.setInConsecutiveRegs();
2303 if (j == NumValues - 1)
2304 Flags.setInConsecutiveRegsLast();
2305 }
2306
2307 // Propagate extension type if any
2308 if (ExtendKind == ISD::SIGN_EXTEND)
2309 Flags.setSExt();
2310 else if (ExtendKind == ISD::ZERO_EXTEND)
2311 Flags.setZExt();
2312 else if (F->getAttributes().hasRetAttr(Attribute::NoExt))
2313 Flags.setNoExt();
2314
2315 for (unsigned i = 0; i < NumParts; ++i) {
2316 Outs.push_back(ISD::OutputArg(Flags,
2317 Parts[i].getValueType().getSimpleVT(),
2318 VT, Types[j], 0, 0));
2319 OutVals.push_back(Parts[i]);
2320 }
2321 }
2322 }
2323 }
2324
2325 // Push in swifterror virtual register as the last element of Outs. This makes
2326 // sure swifterror virtual register will be returned in the swifterror
2327 // physical register.
2328 const Function *F = I.getParent()->getParent();
2329 if (TLI.supportSwiftError() &&
2330 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2331 assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2332 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2333 Flags.setSwiftError();
2334 Outs.push_back(ISD::OutputArg(Flags, /*vt=*/TLI.getPointerTy(DL),
2335 /*argvt=*/EVT(TLI.getPointerTy(DL)),
2336 PointerType::getUnqual(*DAG.getContext()),
2337 /*origidx=*/1, /*partOffs=*/0));
2338 // Create SDNode for the swifterror virtual register.
2339 OutVals.push_back(
2340 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2341 &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2342 EVT(TLI.getPointerTy(DL))));
2343 }
2344
2345 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2346 CallingConv::ID CallConv =
2347 DAG.getMachineFunction().getFunction().getCallingConv();
2348 Chain = DAG.getTargetLoweringInfo().LowerReturn(
2349 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2350
2351 // Verify that the target's LowerReturn behaved as expected.
2352 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2353 "LowerReturn didn't return a valid chain!");
2354
2355 // Update the DAG with the new chain value resulting from return lowering.
2356 DAG.setRoot(Chain);
2357}
2358
2359/// CopyToExportRegsIfNeeded - If the given value has virtual registers
2360/// created for it, emit nodes to copy the value into the virtual
2361/// registers.
2363 // Skip empty types
2364 if (V->getType()->isEmptyTy())
2365 return;
2366
2368 if (VMI != FuncInfo.ValueMap.end()) {
2369 assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2370 "Unused value assigned virtual registers!");
2371 CopyValueToVirtualRegister(V, VMI->second);
2372 }
2373}
2374
2375/// ExportFromCurrentBlock - If this condition isn't known to be exported from
2376/// the current basic block, add it to ValueMap now so that we'll get a
2377/// CopyTo/FromReg.
2379 // No need to export constants.
2380 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2381
2382 // Already exported?
2383 if (FuncInfo.isExportedInst(V)) return;
2384
2385 Register Reg = FuncInfo.InitializeRegForValue(V);
2387}
2388
2390 const BasicBlock *FromBB) {
2391 // The operands of the setcc have to be in this block. We don't know
2392 // how to export them from some other block.
2393 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2394 // Can export from current BB.
2395 if (VI->getParent() == FromBB)
2396 return true;
2397
2398 // Is already exported, noop.
2399 return FuncInfo.isExportedInst(V);
2400 }
2401
2402 // If this is an argument, we can export it if the BB is the entry block or
2403 // if it is already exported.
2404 if (isa<Argument>(V)) {
2405 if (FromBB->isEntryBlock())
2406 return true;
2407
2408 // Otherwise, can only export this if it is already exported.
2409 return FuncInfo.isExportedInst(V);
2410 }
2411
2412 // Otherwise, constants can always be exported.
2413 return true;
2414}
2415
2416/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2418SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2419 const MachineBasicBlock *Dst) const {
2421 const BasicBlock *SrcBB = Src->getBasicBlock();
2422 const BasicBlock *DstBB = Dst->getBasicBlock();
2423 if (!BPI) {
2424 // If BPI is not available, set the default probability as 1 / N, where N is
2425 // the number of successors.
2426 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2427 return BranchProbability(1, SuccSize);
2428 }
2429 return BPI->getEdgeProbability(SrcBB, DstBB);
2430}
2431
2432void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2433 MachineBasicBlock *Dst,
2434 BranchProbability Prob) {
2435 if (!FuncInfo.BPI)
2436 Src->addSuccessorWithoutProb(Dst);
2437 else {
2438 if (Prob.isUnknown())
2439 Prob = getEdgeProbability(Src, Dst);
2440 Src->addSuccessor(Dst, Prob);
2441 }
2442}
2443
2444static bool InBlock(const Value *V, const BasicBlock *BB) {
2445 if (const Instruction *I = dyn_cast<Instruction>(V))
2446 return I->getParent() == BB;
2447 return true;
2448}
2449
2450/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2451/// This function emits a branch and is used at the leaves of an OR or an
2452/// AND operator tree.
2453void
2456 MachineBasicBlock *FBB,
2457 MachineBasicBlock *CurBB,
2458 MachineBasicBlock *SwitchBB,
2459 BranchProbability TProb,
2460 BranchProbability FProb,
2461 bool InvertCond) {
2462 const BasicBlock *BB = CurBB->getBasicBlock();
2463
2464 // If the leaf of the tree is a comparison, merge the condition into
2465 // the caseblock.
2466 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2467 // The operands of the cmp have to be in this block. We don't know
2468 // how to export them from some other block. If this is the first block
2469 // of the sequence, no exporting is needed.
2470 if (CurBB == SwitchBB ||
2471 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2472 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2473 ISD::CondCode Condition;
2474 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2475 ICmpInst::Predicate Pred =
2476 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2477 Condition = getICmpCondCode(Pred);
2478 } else {
2479 const FCmpInst *FC = cast<FCmpInst>(Cond);
2480 FCmpInst::Predicate Pred =
2481 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2482 Condition = getFCmpCondCode(Pred);
2483 if (TM.Options.NoNaNsFPMath)
2484 Condition = getFCmpCodeWithoutNaN(Condition);
2485 }
2486
2487 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2488 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2489 SL->SwitchCases.push_back(CB);
2490 return;
2491 }
2492 }
2493
2494 // Create a CaseBlock record representing this branch.
2495 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2496 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2497 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2498 SL->SwitchCases.push_back(CB);
2499}
2500
2501// Collect dependencies on V recursively. This is used for the cost analysis in
2502// `shouldKeepJumpConditionsTogether`.
2506 unsigned Depth = 0) {
2507 // Return false if we have an incomplete count.
2509 return false;
2510
2511 auto *I = dyn_cast<Instruction>(V);
2512 if (I == nullptr)
2513 return true;
2514
2515 if (Necessary != nullptr) {
2516 // This instruction is necessary for the other side of the condition so
2517 // don't count it.
2518 if (Necessary->contains(I))
2519 return true;
2520 }
2521
2522 // Already added this dep.
2523 if (!Deps->try_emplace(I, false).second)
2524 return true;
2525
2526 for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2527 if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2528 Depth + 1))
2529 return false;
2530 return true;
2531}
2532
2535 Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2537 if (I.getNumSuccessors() != 2)
2538 return false;
2539
2540 if (!I.isConditional())
2541 return false;
2542
2543 if (Params.BaseCost < 0)
2544 return false;
2545
2546 // Baseline cost.
2547 InstructionCost CostThresh = Params.BaseCost;
2548
2549 BranchProbabilityInfo *BPI = nullptr;
2550 if (Params.LikelyBias || Params.UnlikelyBias)
2551 BPI = FuncInfo.BPI;
2552 if (BPI != nullptr) {
2553 // See if we are either likely to get an early out or compute both lhs/rhs
2554 // of the condition.
2555 BasicBlock *IfFalse = I.getSuccessor(0);
2556 BasicBlock *IfTrue = I.getSuccessor(1);
2557
2558 std::optional<bool> Likely;
2559 if (BPI->isEdgeHot(I.getParent(), IfTrue))
2560 Likely = true;
2561 else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2562 Likely = false;
2563
2564 if (Likely) {
2565 if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2566 // Its likely we will have to compute both lhs and rhs of condition
2567 CostThresh += Params.LikelyBias;
2568 else {
2569 if (Params.UnlikelyBias < 0)
2570 return false;
2571 // Its likely we will get an early out.
2572 CostThresh -= Params.UnlikelyBias;
2573 }
2574 }
2575 }
2576
2577 if (CostThresh <= 0)
2578 return false;
2579
2580 // Collect "all" instructions that lhs condition is dependent on.
2581 // Use map for stable iteration (to avoid non-determanism of iteration of
2582 // SmallPtrSet). The `bool` value is just a dummy.
2584 collectInstructionDeps(&LhsDeps, Lhs);
2585 // Collect "all" instructions that rhs condition is dependent on AND are
2586 // dependencies of lhs. This gives us an estimate on which instructions we
2587 // stand to save by splitting the condition.
2588 if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2589 return false;
2590 // Add the compare instruction itself unless its a dependency on the LHS.
2591 if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2592 if (!LhsDeps.contains(RhsI))
2593 RhsDeps.try_emplace(RhsI, false);
2594
2595 InstructionCost CostOfIncluding = 0;
2596 // See if this instruction will need to computed independently of whether RHS
2597 // is.
2598 Value *BrCond = I.getCondition();
2599 auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2600 for (const auto *U : Ins->users()) {
2601 // If user is independent of RHS calculation we don't need to count it.
2602 if (auto *UIns = dyn_cast<Instruction>(U))
2603 if (UIns != BrCond && !RhsDeps.contains(UIns))
2604 return false;
2605 }
2606 return true;
2607 };
2608
2609 // Prune instructions from RHS Deps that are dependencies of unrelated
2610 // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2611 // arbitrary and just meant to cap the how much time we spend in the pruning
2612 // loop. Its highly unlikely to come into affect.
2613 const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2614 // Stop after a certain point. No incorrectness from including too many
2615 // instructions.
2616 for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2617 const Instruction *ToDrop = nullptr;
2618 for (const auto &InsPair : RhsDeps) {
2619 if (!ShouldCountInsn(InsPair.first)) {
2620 ToDrop = InsPair.first;
2621 break;
2622 }
2623 }
2624 if (ToDrop == nullptr)
2625 break;
2626 RhsDeps.erase(ToDrop);
2627 }
2628
2629 for (const auto &InsPair : RhsDeps) {
2630 // Finally accumulate latency that we can only attribute to computing the
2631 // RHS condition. Use latency because we are essentially trying to calculate
2632 // the cost of the dependency chain.
2633 // Possible TODO: We could try to estimate ILP and make this more precise.
2634 CostOfIncluding += TTI->getInstructionCost(
2635 InsPair.first, TargetTransformInfo::TCK_Latency);
2636
2637 if (CostOfIncluding > CostThresh)
2638 return false;
2639 }
2640 return true;
2641}
2642
2645 MachineBasicBlock *FBB,
2646 MachineBasicBlock *CurBB,
2647 MachineBasicBlock *SwitchBB,
2649 BranchProbability TProb,
2650 BranchProbability FProb,
2651 bool InvertCond) {
2652 // Skip over not part of the tree and remember to invert op and operands at
2653 // next level.
2654 Value *NotCond;
2655 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2656 InBlock(NotCond, CurBB->getBasicBlock())) {
2657 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2658 !InvertCond);
2659 return;
2660 }
2661
2663 const Value *BOpOp0, *BOpOp1;
2664 // Compute the effective opcode for Cond, taking into account whether it needs
2665 // to be inverted, e.g.
2666 // and (not (or A, B)), C
2667 // gets lowered as
2668 // and (and (not A, not B), C)
2670 if (BOp) {
2671 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2672 ? Instruction::And
2673 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2674 ? Instruction::Or
2676 if (InvertCond) {
2677 if (BOpc == Instruction::And)
2678 BOpc = Instruction::Or;
2679 else if (BOpc == Instruction::Or)
2680 BOpc = Instruction::And;
2681 }
2682 }
2683
2684 // If this node is not part of the or/and tree, emit it as a branch.
2685 // Note that all nodes in the tree should have same opcode.
2686 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2687 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2688 !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2689 !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2690 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2691 TProb, FProb, InvertCond);
2692 return;
2693 }
2694
2695 // Create TmpBB after CurBB.
2696 MachineFunction::iterator BBI(CurBB);
2697 MachineFunction &MF = DAG.getMachineFunction();
2699 CurBB->getParent()->insert(++BBI, TmpBB);
2700
2701 if (Opc == Instruction::Or) {
2702 // Codegen X | Y as:
2703 // BB1:
2704 // jmp_if_X TBB
2705 // jmp TmpBB
2706 // TmpBB:
2707 // jmp_if_Y TBB
2708 // jmp FBB
2709 //
2710
2711 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2712 // The requirement is that
2713 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2714 // = TrueProb for original BB.
2715 // Assuming the original probabilities are A and B, one choice is to set
2716 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2717 // A/(1+B) and 2B/(1+B). This choice assumes that
2718 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2719 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2720 // TmpBB, but the math is more complicated.
2721
2722 auto NewTrueProb = TProb / 2;
2723 auto NewFalseProb = TProb / 2 + FProb;
2724 // Emit the LHS condition.
2725 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2726 NewFalseProb, InvertCond);
2727
2728 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2729 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2731 // Emit the RHS condition into TmpBB.
2732 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2733 Probs[1], InvertCond);
2734 } else {
2735 assert(Opc == Instruction::And && "Unknown merge op!");
2736 // Codegen X & Y as:
2737 // BB1:
2738 // jmp_if_X TmpBB
2739 // jmp FBB
2740 // TmpBB:
2741 // jmp_if_Y TBB
2742 // jmp FBB
2743 //
2744 // This requires creation of TmpBB after CurBB.
2745
2746 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2747 // The requirement is that
2748 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2749 // = FalseProb for original BB.
2750 // Assuming the original probabilities are A and B, one choice is to set
2751 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2752 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2753 // TrueProb for BB1 * FalseProb for TmpBB.
2754
2755 auto NewTrueProb = TProb + FProb / 2;
2756 auto NewFalseProb = FProb / 2;
2757 // Emit the LHS condition.
2758 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2759 NewFalseProb, InvertCond);
2760
2761 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2762 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2764 // Emit the RHS condition into TmpBB.
2765 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2766 Probs[1], InvertCond);
2767 }
2768}
2769
2770/// If the set of cases should be emitted as a series of branches, return true.
2771/// If we should emit this as a bunch of and/or'd together conditions, return
2772/// false.
2773bool
2774SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2775 if (Cases.size() != 2) return true;
2776
2777 // If this is two comparisons of the same values or'd or and'd together, they
2778 // will get folded into a single comparison, so don't emit two blocks.
2779 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2780 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2781 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2782 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2783 return false;
2784 }
2785
2786 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2787 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2788 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2789 Cases[0].CC == Cases[1].CC &&
2790 isa<Constant>(Cases[0].CmpRHS) &&
2791 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2792 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2793 return false;
2794 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2795 return false;
2796 }
2797
2798 return true;
2799}
2800
2801void SelectionDAGBuilder::visitBr(const BranchInst &I) {
2803
2804 // Update machine-CFG edges.
2805 MachineBasicBlock *Succ0MBB = FuncInfo.getMBB(I.getSuccessor(0));
2806
2807 if (I.isUnconditional()) {
2808 // Update machine-CFG edges.
2809 BrMBB->addSuccessor(Succ0MBB);
2810
2811 // If this is not a fall-through branch or optimizations are switched off,
2812 // emit the branch.
2813 if (Succ0MBB != NextBlock(BrMBB) ||
2815 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2816 getControlRoot(), DAG.getBasicBlock(Succ0MBB));
2817 setValue(&I, Br);
2818 DAG.setRoot(Br);
2819 }
2820
2821 return;
2822 }
2823
2824 // If this condition is one of the special cases we handle, do special stuff
2825 // now.
2826 const Value *CondVal = I.getCondition();
2827 MachineBasicBlock *Succ1MBB = FuncInfo.getMBB(I.getSuccessor(1));
2828
2829 // If this is a series of conditions that are or'd or and'd together, emit
2830 // this as a sequence of branches instead of setcc's with and/or operations.
2831 // As long as jumps are not expensive (exceptions for multi-use logic ops,
2832 // unpredictable branches, and vector extracts because those jumps are likely
2833 // expensive for any target), this should improve performance.
2834 // For example, instead of something like:
2835 // cmp A, B
2836 // C = seteq
2837 // cmp D, E
2838 // F = setle
2839 // or C, F
2840 // jnz foo
2841 // Emit:
2842 // cmp A, B
2843 // je foo
2844 // cmp D, E
2845 // jle foo
2846 bool IsUnpredictable = I.hasMetadata(LLVMContext::MD_unpredictable);
2847 const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2848 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2849 BOp->hasOneUse() && !IsUnpredictable) {
2850 Value *Vec;
2851 const Value *BOp0, *BOp1;
2853 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2854 Opcode = Instruction::And;
2855 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2856 Opcode = Instruction::Or;
2857
2858 if (Opcode &&
2859 !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2860 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2862 FuncInfo, I, Opcode, BOp0, BOp1,
2863 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2864 Opcode, BOp0, BOp1))) {
2865 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2866 getEdgeProbability(BrMBB, Succ0MBB),
2867 getEdgeProbability(BrMBB, Succ1MBB),
2868 /*InvertCond=*/false);
2869 // If the compares in later blocks need to use values not currently
2870 // exported from this block, export them now. This block should always
2871 // be the first entry.
2872 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2873
2874 // Allow some cases to be rejected.
2875 if (ShouldEmitAsBranches(SL->SwitchCases)) {
2876 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2877 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2878 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2879 }
2880
2881 // Emit the branch for this block.
2882 visitSwitchCase(SL->SwitchCases[0], BrMBB);
2883 SL->SwitchCases.erase(SL->SwitchCases.begin());
2884 return;
2885 }
2886
2887 // Okay, we decided not to do this, remove any inserted MBB's and clear
2888 // SwitchCases.
2889 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2890 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2891
2892 SL->SwitchCases.clear();
2893 }
2894 }
2895
2896 // Create a CaseBlock record representing this branch.
2897 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2898 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc(),
2900 IsUnpredictable);
2901
2902 // Use visitSwitchCase to actually insert the fast branch sequence for this
2903 // cond branch.
2904 visitSwitchCase(CB, BrMBB);
2905}
2906
2907/// visitSwitchCase - Emits the necessary code to represent a single node in
2908/// the binary search tree resulting from lowering a switch instruction.
2910 MachineBasicBlock *SwitchBB) {
2911 SDValue Cond;
2912 SDValue CondLHS = getValue(CB.CmpLHS);
2913 SDLoc dl = CB.DL;
2914
2915 if (CB.CC == ISD::SETTRUE) {
2916 // Branch or fall through to TrueBB.
2917 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2918 SwitchBB->normalizeSuccProbs();
2919 if (CB.TrueBB != NextBlock(SwitchBB)) {
2920 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2921 DAG.getBasicBlock(CB.TrueBB)));
2922 }
2923 return;
2924 }
2925
2926 auto &TLI = DAG.getTargetLoweringInfo();
2927 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2928
2929 // Build the setcc now.
2930 if (!CB.CmpMHS) {
2931 // Fold "(X == true)" to X and "(X == false)" to !X to
2932 // handle common cases produced by branch lowering.
2933 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2934 CB.CC == ISD::SETEQ)
2935 Cond = CondLHS;
2936 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2937 CB.CC == ISD::SETEQ) {
2938 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2939 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2940 } else {
2941 SDValue CondRHS = getValue(CB.CmpRHS);
2942
2943 // If a pointer's DAG type is larger than its memory type then the DAG
2944 // values are zero-extended. This breaks signed comparisons so truncate
2945 // back to the underlying type before doing the compare.
2946 if (CondLHS.getValueType() != MemVT) {
2947 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2948 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2949 }
2950 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2951 }
2952 } else {
2953 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2954
2955 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2956 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2957
2958 SDValue CmpOp = getValue(CB.CmpMHS);
2959 EVT VT = CmpOp.getValueType();
2960
2961 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2962 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2963 ISD::SETLE);
2964 } else {
2965 SDValue SUB = DAG.getNode(ISD::SUB, dl,
2966 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2967 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2968 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2969 }
2970 }
2971
2972 // Update successor info
2973 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2974 // TrueBB and FalseBB are always different unless the incoming IR is
2975 // degenerate. This only happens when running llc on weird IR.
2976 if (CB.TrueBB != CB.FalseBB)
2977 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2978 SwitchBB->normalizeSuccProbs();
2979
2980 // If the lhs block is the next block, invert the condition so that we can
2981 // fall through to the lhs instead of the rhs block.
2982 if (CB.TrueBB == NextBlock(SwitchBB)) {
2983 std::swap(CB.TrueBB, CB.FalseBB);
2984 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2985 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2986 }
2987
2988 SDNodeFlags Flags;
2990 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
2991 Cond, DAG.getBasicBlock(CB.TrueBB), Flags);
2992
2993 setValue(CurInst, BrCond);
2994
2995 // Insert the false branch. Do this even if it's a fall through branch,
2996 // this makes it easier to do DAG optimizations which require inverting
2997 // the branch condition.
2998 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2999 DAG.getBasicBlock(CB.FalseBB));
3000
3001 DAG.setRoot(BrCond);
3002}
3003
3004/// visitJumpTable - Emit JumpTable node in the current MBB
3006 // Emit the code for the jump table
3007 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3008 assert(JT.Reg && "Should lower JT Header first!");
3009 EVT PTy = DAG.getTargetLoweringInfo().getJumpTableRegTy(DAG.getDataLayout());
3010 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
3011 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
3012 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
3013 Index.getValue(1), Table, Index);
3014 DAG.setRoot(BrJumpTable);
3015}
3016
3017/// visitJumpTableHeader - This function emits necessary code to produce index
3018/// in the JumpTable from switch case.
3020 JumpTableHeader &JTH,
3021 MachineBasicBlock *SwitchBB) {
3022 assert(JT.SL && "Should set SDLoc for SelectionDAG!");
3023 const SDLoc &dl = *JT.SL;
3024
3025 // Subtract the lowest switch case value from the value being switched on.
3026 SDValue SwitchOp = getValue(JTH.SValue);
3027 EVT VT = SwitchOp.getValueType();
3028 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
3029 DAG.getConstant(JTH.First, dl, VT));
3030
3031 // The SDNode we just created, which holds the value being switched on minus
3032 // the smallest case value, needs to be copied to a virtual register so it
3033 // can be used as an index into the jump table in a subsequent basic block.
3034 // This value may be smaller or larger than the target's pointer type, and
3035 // therefore require extension or truncating.
3036 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3037 SwitchOp =
3038 DAG.getZExtOrTrunc(Sub, dl, TLI.getJumpTableRegTy(DAG.getDataLayout()));
3039
3040 Register JumpTableReg =
3041 FuncInfo.CreateReg(TLI.getJumpTableRegTy(DAG.getDataLayout()));
3042 SDValue CopyTo =
3043 DAG.getCopyToReg(getControlRoot(), dl, JumpTableReg, SwitchOp);
3044 JT.Reg = JumpTableReg;
3045
3046 if (!JTH.FallthroughUnreachable) {
3047 // Emit the range check for the jump table, and branch to the default block
3048 // for the switch statement if the value being switched on exceeds the
3049 // largest case in the switch.
3050 SDValue CMP = DAG.getSetCC(
3051 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3052 Sub.getValueType()),
3053 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3054
3055 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3056 MVT::Other, CopyTo, CMP,
3057 DAG.getBasicBlock(JT.Default));
3058
3059 // Avoid emitting unnecessary branches to the next block.
3060 if (JT.MBB != NextBlock(SwitchBB))
3061 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3062 DAG.getBasicBlock(JT.MBB));
3063
3064 DAG.setRoot(BrCond);
3065 } else {
3066 // Avoid emitting unnecessary branches to the next block.
3067 if (JT.MBB != NextBlock(SwitchBB))
3068 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3069 DAG.getBasicBlock(JT.MBB)));
3070 else
3071 DAG.setRoot(CopyTo);
3072 }
3073}
3074
3075/// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3076/// variable if there exists one.
3078 SDValue &Chain) {
3079 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3080 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3081 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3083 Value *Global =
3086 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3087 if (Global) {
3088 MachinePointerInfo MPInfo(Global);
3092 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
3093 DAG.setNodeMemRefs(Node, {MemRef});
3094 }
3095 if (PtrTy != PtrMemTy)
3096 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3097 return SDValue(Node, 0);
3098}
3099
3100/// Codegen a new tail for a stack protector check ParentMBB which has had its
3101/// tail spliced into a stack protector check success bb.
3102///
3103/// For a high level explanation of how this fits into the stack protector
3104/// generation see the comment on the declaration of class
3105/// StackProtectorDescriptor.
3107 MachineBasicBlock *ParentBB) {
3108
3109 // First create the loads to the guard/stack slot for the comparison.
3110 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3111 auto &DL = DAG.getDataLayout();
3112 EVT PtrTy = TLI.getFrameIndexTy(DL);
3113 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3114
3115 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3116 int FI = MFI.getStackProtectorIndex();
3117
3118 SDValue Guard;
3119 SDLoc dl = getCurSDLoc();
3120 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3121 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3122 Align Align = DL.getPrefTypeAlign(
3123 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3124
3125 // Generate code to load the content of the guard slot.
3126 SDValue GuardVal = DAG.getLoad(
3127 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3128 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3130
3131 if (TLI.useStackGuardXorFP())
3132 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3133
3134 // If we're using function-based instrumentation, call the guard check
3135 // function
3137 // Get the guard check function from the target and verify it exists since
3138 // we're using function-based instrumentation
3139 const Function *GuardCheckFn =
3140 TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3141 assert(GuardCheckFn && "Guard check function is null");
3142
3143 // The target provides a guard check function to validate the guard value.
3144 // Generate a call to that function with the content of the guard slot as
3145 // argument.
3146 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3147 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3148
3150 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3151 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3152 Entry.IsInReg = true;
3153 Args.push_back(Entry);
3154
3157 .setChain(DAG.getEntryNode())
3158 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3159 getValue(GuardCheckFn), std::move(Args));
3160
3161 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3162 DAG.setRoot(Result.second);
3163 return;
3164 }
3165
3166 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3167 // Otherwise, emit a volatile load to retrieve the stack guard value.
3168 SDValue Chain = DAG.getEntryNode();
3169 if (TLI.useLoadStackGuardNode(M)) {
3170 Guard = getLoadStackGuard(DAG, dl, Chain);
3171 } else {
3172 if (const Value *IRGuard = TLI.getSDagStackGuard(M, DAG.getLibcalls())) {
3173 SDValue GuardPtr = getValue(IRGuard);
3174 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3175 MachinePointerInfo(IRGuard, 0), Align,
3177 } else {
3178 LLVMContext &Ctx = *DAG.getContext();
3179 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
3180 Guard = DAG.getPOISON(PtrMemTy);
3181 }
3182 }
3183
3184 // Perform the comparison via a getsetcc.
3185 SDValue Cmp = DAG.getSetCC(
3186 dl, TLI.getSetCCResultType(DL, *DAG.getContext(), Guard.getValueType()),
3187 Guard, GuardVal, ISD::SETNE);
3188
3189 // If the guard/stackslot do not equal, branch to failure MBB.
3190 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, MVT::Other, getControlRoot(),
3191 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3192 // Otherwise branch to success MBB.
3193 SDValue Br = DAG.getNode(ISD::BR, dl,
3194 MVT::Other, BrCond,
3195 DAG.getBasicBlock(SPD.getSuccessMBB()));
3196
3197 DAG.setRoot(Br);
3198}
3199
3200/// Codegen the failure basic block for a stack protector check.
3201///
3202/// A failure stack protector machine basic block consists simply of a call to
3203/// __stack_chk_fail().
3204///
3205/// For a high level explanation of how this fits into the stack protector
3206/// generation see the comment on the declaration of class
3207/// StackProtectorDescriptor.
3210
3211 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3212 MachineBasicBlock *ParentBB = SPD.getParentMBB();
3213 const Module &M = *ParentBB->getParent()->getFunction().getParent();
3214 SDValue Chain;
3215
3216 // For -Oz builds with a guard check function, we use function-based
3217 // instrumentation. Otherwise, if we have a guard check function, we call it
3218 // in the failure block.
3219 auto *GuardCheckFn = TLI.getSSPStackGuardCheck(M, DAG.getLibcalls());
3220 if (GuardCheckFn && !SPD.shouldEmitFunctionBasedCheckStackProtector()) {
3221 // First create the loads to the guard/stack slot for the comparison.
3222 auto &DL = DAG.getDataLayout();
3223 EVT PtrTy = TLI.getFrameIndexTy(DL);
3224 EVT PtrMemTy = TLI.getPointerMemTy(DL, DL.getAllocaAddrSpace());
3225
3226 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3227 int FI = MFI.getStackProtectorIndex();
3228
3229 SDLoc dl = getCurSDLoc();
3230 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3231 Align Align = DL.getPrefTypeAlign(
3232 PointerType::get(M.getContext(), DL.getAllocaAddrSpace()));
3233
3234 // Generate code to load the content of the guard slot.
3235 SDValue GuardVal = DAG.getLoad(
3236 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3237 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3239
3240 if (TLI.useStackGuardXorFP())
3241 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3242
3243 // The target provides a guard check function to validate the guard value.
3244 // Generate a call to that function with the content of the guard slot as
3245 // argument.
3246 FunctionType *FnTy = GuardCheckFn->getFunctionType();
3247 assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3248
3250 TargetLowering::ArgListEntry Entry(GuardVal, FnTy->getParamType(0));
3251 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3252 Entry.IsInReg = true;
3253 Args.push_back(Entry);
3254
3257 .setChain(DAG.getEntryNode())
3258 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3259 getValue(GuardCheckFn), std::move(Args));
3260
3261 Chain = TLI.LowerCallTo(CLI).second;
3262 } else {
3264 CallOptions.setDiscardResult(true);
3265 Chain = TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3266 {}, CallOptions, getCurSDLoc())
3267 .second;
3268 }
3269
3270 // Emit a trap instruction if we are required to do so.
3271 const TargetOptions &TargetOpts = DAG.getTarget().Options;
3272 if (TargetOpts.TrapUnreachable && !TargetOpts.NoTrapAfterNoreturn)
3273 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3274
3275 DAG.setRoot(Chain);
3276}
3277
3278/// visitBitTestHeader - This function emits necessary code to produce value
3279/// suitable for "bit tests"
3281 MachineBasicBlock *SwitchBB) {
3282 SDLoc dl = getCurSDLoc();
3283
3284 // Subtract the minimum value.
3285 SDValue SwitchOp = getValue(B.SValue);
3286 EVT VT = SwitchOp.getValueType();
3287 SDValue RangeSub =
3288 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3289
3290 // Determine the type of the test operands.
3291 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3292 bool UsePtrType = false;
3293 if (!TLI.isTypeLegal(VT)) {
3294 UsePtrType = true;
3295 } else {
3296 for (const BitTestCase &Case : B.Cases)
3297 if (!isUIntN(VT.getSizeInBits(), Case.Mask)) {
3298 // Switch table case range are encoded into series of masks.
3299 // Just use pointer type, it's guaranteed to fit.
3300 UsePtrType = true;
3301 break;
3302 }
3303 }
3304 SDValue Sub = RangeSub;
3305 if (UsePtrType) {
3306 VT = TLI.getPointerTy(DAG.getDataLayout());
3307 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3308 }
3309
3310 B.RegVT = VT.getSimpleVT();
3311 B.Reg = FuncInfo.CreateReg(B.RegVT);
3312 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3313
3314 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3315
3316 if (!B.FallthroughUnreachable)
3317 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3318 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3319 SwitchBB->normalizeSuccProbs();
3320
3321 SDValue Root = CopyTo;
3322 if (!B.FallthroughUnreachable) {
3323 // Conditional branch to the default block.
3324 SDValue RangeCmp = DAG.getSetCC(dl,
3325 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3326 RangeSub.getValueType()),
3327 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3328 ISD::SETUGT);
3329
3330 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3331 DAG.getBasicBlock(B.Default));
3332 }
3333
3334 // Avoid emitting unnecessary branches to the next block.
3335 if (MBB != NextBlock(SwitchBB))
3336 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3337
3338 DAG.setRoot(Root);
3339}
3340
3341/// visitBitTestCase - this function produces one "bit test"
3343 MachineBasicBlock *NextMBB,
3344 BranchProbability BranchProbToNext,
3345 Register Reg, BitTestCase &B,
3346 MachineBasicBlock *SwitchBB) {
3347 SDLoc dl = getCurSDLoc();
3348 MVT VT = BB.RegVT;
3349 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3350 SDValue Cmp;
3351 unsigned PopCount = llvm::popcount(B.Mask);
3352 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3353 if (PopCount == 1) {
3354 // Testing for a single bit; just compare the shift count with what it
3355 // would need to be to shift a 1 bit in that position.
3356 Cmp = DAG.getSetCC(
3357 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3358 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3359 ISD::SETEQ);
3360 } else if (PopCount == BB.Range) {
3361 // There is only one zero bit in the range, test for it directly.
3362 Cmp = DAG.getSetCC(
3363 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3364 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3365 } else {
3366 // Make desired shift
3367 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3368 DAG.getConstant(1, dl, VT), ShiftOp);
3369
3370 // Emit bit tests and jumps
3371 SDValue AndOp = DAG.getNode(ISD::AND, dl,
3372 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3373 Cmp = DAG.getSetCC(
3374 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3375 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3376 }
3377
3378 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3379 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3380 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3381 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3382 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3383 // one as they are relative probabilities (and thus work more like weights),
3384 // and hence we need to normalize them to let the sum of them become one.
3385 SwitchBB->normalizeSuccProbs();
3386
3387 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3388 MVT::Other, getControlRoot(),
3389 Cmp, DAG.getBasicBlock(B.TargetBB));
3390
3391 // Avoid emitting unnecessary branches to the next block.
3392 if (NextMBB != NextBlock(SwitchBB))
3393 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3394 DAG.getBasicBlock(NextMBB));
3395
3396 DAG.setRoot(BrAnd);
3397}
3398
3399void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3400 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3401
3402 // Retrieve successors. Look through artificial IR level blocks like
3403 // catchswitch for successors.
3404 MachineBasicBlock *Return = FuncInfo.getMBB(I.getSuccessor(0));
3405 const BasicBlock *EHPadBB = I.getSuccessor(1);
3406 MachineBasicBlock *EHPadMBB = FuncInfo.getMBB(EHPadBB);
3407
3408 // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3409 // have to do anything here to lower funclet bundles.
3410 failForInvalidBundles(I, "invokes",
3416
3417 const Value *Callee(I.getCalledOperand());
3418 const Function *Fn = dyn_cast<Function>(Callee);
3419 if (isa<InlineAsm>(Callee))
3420 visitInlineAsm(I, EHPadBB);
3421 else if (Fn && Fn->isIntrinsic()) {
3422 switch (Fn->getIntrinsicID()) {
3423 default:
3424 llvm_unreachable("Cannot invoke this intrinsic");
3425 case Intrinsic::donothing:
3426 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3427 case Intrinsic::seh_try_begin:
3428 case Intrinsic::seh_scope_begin:
3429 case Intrinsic::seh_try_end:
3430 case Intrinsic::seh_scope_end:
3431 if (EHPadMBB)
3432 // a block referenced by EH table
3433 // so dtor-funclet not removed by opts
3434 EHPadMBB->setMachineBlockAddressTaken();
3435 break;
3436 case Intrinsic::experimental_patchpoint_void:
3437 case Intrinsic::experimental_patchpoint:
3438 visitPatchpoint(I, EHPadBB);
3439 break;
3440 case Intrinsic::experimental_gc_statepoint:
3442 break;
3443 // wasm_throw, wasm_rethrow: This is usually done in visitTargetIntrinsic,
3444 // but these intrinsics are special because they can be invoked, so we
3445 // manually lower it to a DAG node here.
3446 case Intrinsic::wasm_throw: {
3448 std::array<SDValue, 4> Ops = {
3449 getControlRoot(), // inchain for the terminator node
3450 DAG.getTargetConstant(Intrinsic::wasm_throw, getCurSDLoc(),
3452 getValue(I.getArgOperand(0)), // tag
3453 getValue(I.getArgOperand(1)) // thrown value
3454 };
3455 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3456 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3457 break;
3458 }
3459 case Intrinsic::wasm_rethrow: {
3460 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3461 std::array<SDValue, 2> Ops = {
3462 getControlRoot(), // inchain for the terminator node
3463 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3464 TLI.getPointerTy(DAG.getDataLayout()))};
3465 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3466 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3467 break;
3468 }
3469 }
3470 } else if (I.hasDeoptState()) {
3471 // Currently we do not lower any intrinsic calls with deopt operand bundles.
3472 // Eventually we will support lowering the @llvm.experimental.deoptimize
3473 // intrinsic, and right now there are no plans to support other intrinsics
3474 // with deopt state.
3475 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3476 } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3478 } else {
3479 LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3480 }
3481
3482 // If the value of the invoke is used outside of its defining block, make it
3483 // available as a virtual register.
3484 // We already took care of the exported value for the statepoint instruction
3485 // during call to the LowerStatepoint.
3486 if (!isa<GCStatepointInst>(I)) {
3488 }
3489
3491 BranchProbabilityInfo *BPI = FuncInfo.BPI;
3492 BranchProbability EHPadBBProb =
3493 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3495 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3496
3497 // Update successor info.
3498 addSuccessorWithProb(InvokeMBB, Return);
3499 for (auto &UnwindDest : UnwindDests) {
3500 UnwindDest.first->setIsEHPad();
3501 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3502 }
3503 InvokeMBB->normalizeSuccProbs();
3504
3505 // Drop into normal successor.
3506 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3507 DAG.getBasicBlock(Return)));
3508}
3509
3510/// The intrinsics currently supported by callbr are implicit control flow
3511/// intrinsics such as amdgcn.kill.
3512/// - they should be called (no "dontcall-" attributes)
3513/// - they do not touch memory on the target (= !TLI.getTgtMemIntrinsic())
3514/// - they do not need custom argument handling (no
3515/// TLI.CollectTargetIntrinsicOperands())
3516void SelectionDAGBuilder::visitCallBrIntrinsic(const CallBrInst &I) {
3517 TargetLowering::IntrinsicInfo Info;
3518 assert(!DAG.getTargetLoweringInfo().getTgtMemIntrinsic(
3519 Info, I, DAG.getMachineFunction(), I.getIntrinsicID()) &&
3520 "Intrinsic touches memory");
3521
3522 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
3523
3525 getTargetIntrinsicOperands(I, HasChain, OnlyLoad);
3526 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
3527
3528 // Create the node.
3529 SDValue Result =
3530 getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
3531 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
3532
3533 setValue(&I, Result);
3534}
3535
3536void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3537 MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3538
3539 if (I.isInlineAsm()) {
3540 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3541 // have to do anything here to lower funclet bundles.
3542 failForInvalidBundles(I, "callbrs",
3544 visitInlineAsm(I);
3545 } else {
3546 assert(!I.hasOperandBundles() &&
3547 "Can't have operand bundles for intrinsics");
3548 visitCallBrIntrinsic(I);
3549 }
3551
3552 // Retrieve successors.
3553 SmallPtrSet<BasicBlock *, 8> Dests;
3554 Dests.insert(I.getDefaultDest());
3555 MachineBasicBlock *Return = FuncInfo.getMBB(I.getDefaultDest());
3556
3557 // Update successor info.
3558 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3559 // TODO: For most of the cases where there is an intrinsic callbr, we're
3560 // having exactly one indirect target, which will be unreachable. As soon as
3561 // this changes, we might need to enhance
3562 // Target->setIsInlineAsmBrIndirectTarget or add something similar for
3563 // intrinsic indirect branches.
3564 if (I.isInlineAsm()) {
3565 for (BasicBlock *Dest : I.getIndirectDests()) {
3566 MachineBasicBlock *Target = FuncInfo.getMBB(Dest);
3567 Target->setIsInlineAsmBrIndirectTarget();
3568 // If we introduce a type of asm goto statement that is permitted to use
3569 // an indirect call instruction to jump to its labels, then we should add
3570 // a call to Target->setMachineBlockAddressTaken() here, to mark the
3571 // target block as requiring a BTI.
3572
3573 Target->setLabelMustBeEmitted();
3574 // Don't add duplicate machine successors.
3575 if (Dests.insert(Dest).second)
3576 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3577 }
3578 }
3579 CallBrMBB->normalizeSuccProbs();
3580
3581 // Drop into default successor.
3582 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3583 MVT::Other, getControlRoot(),
3584 DAG.getBasicBlock(Return)));
3585}
3586
3587void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3588 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3589}
3590
3591void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3592 assert(FuncInfo.MBB->isEHPad() &&
3593 "Call to landingpad not in landing pad!");
3594
3595 // If there aren't registers to copy the values into (e.g., during SjLj
3596 // exceptions), then don't bother to create these DAG nodes.
3597 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3598 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3599 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3600 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3601 return;
3602
3603 // If landingpad's return type is token type, we don't create DAG nodes
3604 // for its exception pointer and selector value. The extraction of exception
3605 // pointer or selector value from token type landingpads is not currently
3606 // supported.
3607 if (LP.getType()->isTokenTy())
3608 return;
3609
3610 SmallVector<EVT, 2> ValueVTs;
3611 SDLoc dl = getCurSDLoc();
3612 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3613 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3614
3615 // Get the two live-in registers as SDValues. The physregs have already been
3616 // copied into virtual registers.
3617 SDValue Ops[2];
3618 if (FuncInfo.ExceptionPointerVirtReg) {
3619 Ops[0] = DAG.getZExtOrTrunc(
3620 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3621 FuncInfo.ExceptionPointerVirtReg,
3622 TLI.getPointerTy(DAG.getDataLayout())),
3623 dl, ValueVTs[0]);
3624 } else {
3625 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3626 }
3627 Ops[1] = DAG.getZExtOrTrunc(
3628 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3629 FuncInfo.ExceptionSelectorVirtReg,
3630 TLI.getPointerTy(DAG.getDataLayout())),
3631 dl, ValueVTs[1]);
3632
3633 // Merge into one.
3634 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3635 DAG.getVTList(ValueVTs), Ops);
3636 setValue(&LP, Res);
3637}
3638
3641 // Update JTCases.
3642 for (JumpTableBlock &JTB : SL->JTCases)
3643 if (JTB.first.HeaderBB == First)
3644 JTB.first.HeaderBB = Last;
3645
3646 // Update BitTestCases.
3647 for (BitTestBlock &BTB : SL->BitTestCases)
3648 if (BTB.Parent == First)
3649 BTB.Parent = Last;
3650}
3651
3652void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3653 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3654
3655 // Update machine-CFG edges with unique successors.
3657 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3658 BasicBlock *BB = I.getSuccessor(i);
3659 bool Inserted = Done.insert(BB).second;
3660 if (!Inserted)
3661 continue;
3662
3663 MachineBasicBlock *Succ = FuncInfo.getMBB(BB);
3664 addSuccessorWithProb(IndirectBrMBB, Succ);
3665 }
3666 IndirectBrMBB->normalizeSuccProbs();
3667
3669 MVT::Other, getControlRoot(),
3670 getValue(I.getAddress())));
3671}
3672
3673void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3674 if (!I.shouldLowerToTrap(DAG.getTarget().Options.TrapUnreachable,
3675 DAG.getTarget().Options.NoTrapAfterNoreturn))
3676 return;
3677
3678 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3679}
3680
3681void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3682 SDNodeFlags Flags;
3683 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3684 Flags.copyFMF(*FPOp);
3685
3686 SDValue Op = getValue(I.getOperand(0));
3687 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3688 Op, Flags);
3689 setValue(&I, UnNodeValue);
3690}
3691
3692void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3693 SDNodeFlags Flags;
3694 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3695 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3696 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3697 }
3698 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3699 Flags.setExact(ExactOp->isExact());
3700 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3701 Flags.setDisjoint(DisjointOp->isDisjoint());
3702 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3703 Flags.copyFMF(*FPOp);
3704
3705 SDValue Op1 = getValue(I.getOperand(0));
3706 SDValue Op2 = getValue(I.getOperand(1));
3707 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3708 Op1, Op2, Flags);
3709 setValue(&I, BinNodeValue);
3710}
3711
3712void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3713 SDValue Op1 = getValue(I.getOperand(0));
3714 SDValue Op2 = getValue(I.getOperand(1));
3715
3716 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3717 Op1.getValueType(), DAG.getDataLayout());
3718
3719 // Coerce the shift amount to the right type if we can. This exposes the
3720 // truncate or zext to optimization early.
3721 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3723 "Unexpected shift type");
3724 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3725 }
3726
3727 bool nuw = false;
3728 bool nsw = false;
3729 bool exact = false;
3730
3731 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3732
3733 if (const OverflowingBinaryOperator *OFBinOp =
3735 nuw = OFBinOp->hasNoUnsignedWrap();
3736 nsw = OFBinOp->hasNoSignedWrap();
3737 }
3738 if (const PossiblyExactOperator *ExactOp =
3740 exact = ExactOp->isExact();
3741 }
3742 SDNodeFlags Flags;
3743 Flags.setExact(exact);
3744 Flags.setNoSignedWrap(nsw);
3745 Flags.setNoUnsignedWrap(nuw);
3746 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3747 Flags);
3748 setValue(&I, Res);
3749}
3750
3751void SelectionDAGBuilder::visitSDiv(const User &I) {
3752 SDValue Op1 = getValue(I.getOperand(0));
3753 SDValue Op2 = getValue(I.getOperand(1));
3754
3755 SDNodeFlags Flags;
3756 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3757 cast<PossiblyExactOperator>(&I)->isExact());
3758 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3759 Op2, Flags));
3760}
3761
3762void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3763 ICmpInst::Predicate predicate = I.getPredicate();
3764 SDValue Op1 = getValue(I.getOperand(0));
3765 SDValue Op2 = getValue(I.getOperand(1));
3766 ISD::CondCode Opcode = getICmpCondCode(predicate);
3767
3768 auto &TLI = DAG.getTargetLoweringInfo();
3769 EVT MemVT =
3770 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3771
3772 // If a pointer's DAG type is larger than its memory type then the DAG values
3773 // are zero-extended. This breaks signed comparisons so truncate back to the
3774 // underlying type before doing the compare.
3775 if (Op1.getValueType() != MemVT) {
3776 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3777 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3778 }
3779
3780 SDNodeFlags Flags;
3781 Flags.setSameSign(I.hasSameSign());
3782 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3783
3784 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3785 I.getType());
3786 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
3787}
3788
3789void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3790 FCmpInst::Predicate predicate = I.getPredicate();
3791 SDValue Op1 = getValue(I.getOperand(0));
3792 SDValue Op2 = getValue(I.getOperand(1));
3793
3794 ISD::CondCode Condition = getFCmpCondCode(predicate);
3795 auto *FPMO = cast<FPMathOperator>(&I);
3796 if (FPMO->hasNoNaNs() || TM.Options.NoNaNsFPMath)
3797 Condition = getFCmpCodeWithoutNaN(Condition);
3798
3799 SDNodeFlags Flags;
3800 Flags.copyFMF(*FPMO);
3801 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3802
3803 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3804 I.getType());
3805 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
3806}
3807
3808// Check if the condition of the select has one use or two users that are both
3809// selects with the same condition.
3810static bool hasOnlySelectUsers(const Value *Cond) {
3811 return llvm::all_of(Cond->users(), [](const Value *V) {
3812 return isa<SelectInst>(V);
3813 });
3814}
3815
3816void SelectionDAGBuilder::visitSelect(const User &I) {
3817 SmallVector<EVT, 4> ValueVTs;
3818 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3819 ValueVTs);
3820 unsigned NumValues = ValueVTs.size();
3821 if (NumValues == 0) return;
3822
3823 SmallVector<SDValue, 4> Values(NumValues);
3824 SDValue Cond = getValue(I.getOperand(0));
3825 SDValue LHSVal = getValue(I.getOperand(1));
3826 SDValue RHSVal = getValue(I.getOperand(2));
3827 SmallVector<SDValue, 1> BaseOps(1, Cond);
3829 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3830
3831 bool IsUnaryAbs = false;
3832 bool Negate = false;
3833
3834 SDNodeFlags Flags;
3835 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3836 Flags.copyFMF(*FPOp);
3837
3838 Flags.setUnpredictable(
3839 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3840
3841 // Min/max matching is only viable if all output VTs are the same.
3842 if (all_equal(ValueVTs)) {
3843 EVT VT = ValueVTs[0];
3844 LLVMContext &Ctx = *DAG.getContext();
3845 auto &TLI = DAG.getTargetLoweringInfo();
3846
3847 // We care about the legality of the operation after it has been type
3848 // legalized.
3849 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3850 VT = TLI.getTypeToTransformTo(Ctx, VT);
3851
3852 // If the vselect is legal, assume we want to leave this as a vector setcc +
3853 // vselect. Otherwise, if this is going to be scalarized, we want to see if
3854 // min/max is legal on the scalar type.
3855 bool UseScalarMinMax = VT.isVector() &&
3857
3858 // ValueTracking's select pattern matching does not account for -0.0,
3859 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3860 // -0.0 is less than +0.0.
3861 const Value *LHS, *RHS;
3862 auto SPR = matchSelectPattern(&I, LHS, RHS);
3864 switch (SPR.Flavor) {
3865 case SPF_UMAX: Opc = ISD::UMAX; break;
3866 case SPF_UMIN: Opc = ISD::UMIN; break;
3867 case SPF_SMAX: Opc = ISD::SMAX; break;
3868 case SPF_SMIN: Opc = ISD::SMIN; break;
3869 case SPF_FMINNUM:
3870 switch (SPR.NaNBehavior) {
3871 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3872 case SPNB_RETURNS_NAN: break;
3873 case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break;
3874 case SPNB_RETURNS_ANY:
3876 (UseScalarMinMax &&
3878 Opc = ISD::FMINNUM;
3879 break;
3880 }
3881 break;
3882 case SPF_FMAXNUM:
3883 switch (SPR.NaNBehavior) {
3884 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3885 case SPNB_RETURNS_NAN: break;
3886 case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break;
3887 case SPNB_RETURNS_ANY:
3889 (UseScalarMinMax &&
3891 Opc = ISD::FMAXNUM;
3892 break;
3893 }
3894 break;
3895 case SPF_NABS:
3896 Negate = true;
3897 [[fallthrough]];
3898 case SPF_ABS:
3899 IsUnaryAbs = true;
3900 Opc = ISD::ABS;
3901 break;
3902 default: break;
3903 }
3904
3905 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3906 (TLI.isOperationLegalOrCustom(Opc, VT) ||
3907 (UseScalarMinMax &&
3909 // If the underlying comparison instruction is used by any other
3910 // instruction, the consumed instructions won't be destroyed, so it is
3911 // not profitable to convert to a min/max.
3913 OpCode = Opc;
3914 LHSVal = getValue(LHS);
3915 RHSVal = getValue(RHS);
3916 BaseOps.clear();
3917 }
3918
3919 if (IsUnaryAbs) {
3920 OpCode = Opc;
3921 LHSVal = getValue(LHS);
3922 BaseOps.clear();
3923 }
3924 }
3925
3926 if (IsUnaryAbs) {
3927 for (unsigned i = 0; i != NumValues; ++i) {
3928 SDLoc dl = getCurSDLoc();
3929 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3930 Values[i] =
3931 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3932 if (Negate)
3933 Values[i] = DAG.getNegative(Values[i], dl, VT);
3934 }
3935 } else {
3936 for (unsigned i = 0; i != NumValues; ++i) {
3937 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3938 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3939 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3940 Values[i] = DAG.getNode(
3941 OpCode, getCurSDLoc(),
3942 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3943 }
3944 }
3945
3947 DAG.getVTList(ValueVTs), Values));
3948}
3949
3950void SelectionDAGBuilder::visitTrunc(const User &I) {
3951 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3952 SDValue N = getValue(I.getOperand(0));
3953 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3954 I.getType());
3955 SDNodeFlags Flags;
3956 if (auto *Trunc = dyn_cast<TruncInst>(&I)) {
3957 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
3958 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
3959 }
3960
3961 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
3962}
3963
3964void SelectionDAGBuilder::visitZExt(const User &I) {
3965 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3966 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3967 SDValue N = getValue(I.getOperand(0));
3968 auto &TLI = DAG.getTargetLoweringInfo();
3969 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3970
3971 SDNodeFlags Flags;
3972 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3973 Flags.setNonNeg(PNI->hasNonNeg());
3974
3975 // Eagerly use nonneg information to canonicalize towards sign_extend if
3976 // that is the target's preference.
3977 // TODO: Let the target do this later.
3978 if (Flags.hasNonNeg() &&
3979 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3980 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3981 return;
3982 }
3983
3984 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3985}
3986
3987void SelectionDAGBuilder::visitSExt(const User &I) {
3988 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3989 // SExt also can't be a cast to bool for same reason. So, nothing much to do
3990 SDValue N = getValue(I.getOperand(0));
3991 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3992 I.getType());
3993 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3994}
3995
3996void SelectionDAGBuilder::visitFPTrunc(const User &I) {
3997 // FPTrunc is never a no-op cast, no need to check
3998 SDValue N = getValue(I.getOperand(0));
3999 SDLoc dl = getCurSDLoc();
4000 SDNodeFlags Flags;
4001 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4002 Flags.copyFMF(*FPOp);
4003 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4004 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4005 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
4006 DAG.getTargetConstant(
4007 0, dl, TLI.getPointerTy(DAG.getDataLayout())),
4008 Flags));
4009}
4010
4011void SelectionDAGBuilder::visitFPExt(const User &I) {
4012 // FPExt is never a no-op cast, no need to check
4013 SDValue N = getValue(I.getOperand(0));
4014 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4015 I.getType());
4016 SDNodeFlags Flags;
4017 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
4018 Flags.copyFMF(*FPOp);
4019 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N, Flags));
4020}
4021
4022void SelectionDAGBuilder::visitFPToUI(const User &I) {
4023 // FPToUI is never a no-op cast, no need to check
4024 SDValue N = getValue(I.getOperand(0));
4025 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4026 I.getType());
4027 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
4028}
4029
4030void SelectionDAGBuilder::visitFPToSI(const User &I) {
4031 // FPToSI is never a no-op cast, no need to check
4032 SDValue N = getValue(I.getOperand(0));
4033 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4034 I.getType());
4035 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
4036}
4037
4038void SelectionDAGBuilder::visitUIToFP(const User &I) {
4039 // UIToFP is never a no-op cast, no need to check
4040 SDValue N = getValue(I.getOperand(0));
4041 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4042 I.getType());
4043 SDNodeFlags Flags;
4044 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
4045 Flags.setNonNeg(PNI->hasNonNeg());
4046
4047 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
4048}
4049
4050void SelectionDAGBuilder::visitSIToFP(const User &I) {
4051 // SIToFP is never a no-op cast, no need to check
4052 SDValue N = getValue(I.getOperand(0));
4053 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4054 I.getType());
4055 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
4056}
4057
4058void SelectionDAGBuilder::visitPtrToAddr(const User &I) {
4059 SDValue N = getValue(I.getOperand(0));
4060 // By definition the type of the ptrtoaddr must be equal to the address type.
4061 const auto &TLI = DAG.getTargetLoweringInfo();
4062 EVT AddrVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4063 // The address width must be smaller or equal to the pointer representation
4064 // width, so we lower ptrtoaddr as a truncate (possibly folded to a no-op).
4065 N = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), AddrVT, N);
4066 setValue(&I, N);
4067}
4068
4069void SelectionDAGBuilder::visitPtrToInt(const User &I) {
4070 // What to do depends on the size of the integer and the size of the pointer.
4071 // We can either truncate, zero extend, or no-op, accordingly.
4072 SDValue N = getValue(I.getOperand(0));
4073 auto &TLI = DAG.getTargetLoweringInfo();
4074 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4075 I.getType());
4076 EVT PtrMemVT =
4077 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
4078 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4079 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
4080 setValue(&I, N);
4081}
4082
4083void SelectionDAGBuilder::visitIntToPtr(const User &I) {
4084 // What to do depends on the size of the integer and the size of the pointer.
4085 // We can either truncate, zero extend, or no-op, accordingly.
4086 SDValue N = getValue(I.getOperand(0));
4087 auto &TLI = DAG.getTargetLoweringInfo();
4088 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4089 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4090 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
4091 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
4092 setValue(&I, N);
4093}
4094
4095void SelectionDAGBuilder::visitBitCast(const User &I) {
4096 SDValue N = getValue(I.getOperand(0));
4097 SDLoc dl = getCurSDLoc();
4098 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
4099 I.getType());
4100
4101 // BitCast assures us that source and destination are the same size so this is
4102 // either a BITCAST or a no-op.
4103 if (DestVT != N.getValueType())
4104 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
4105 DestVT, N)); // convert types.
4106 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
4107 // might fold any kind of constant expression to an integer constant and that
4108 // is not what we are looking for. Only recognize a bitcast of a genuine
4109 // constant integer as an opaque constant.
4110 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
4111 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
4112 /*isOpaque*/true));
4113 else
4114 setValue(&I, N); // noop cast.
4115}
4116
4117void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
4118 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4119 const Value *SV = I.getOperand(0);
4120 SDValue N = getValue(SV);
4121 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4122
4123 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
4124 unsigned DestAS = I.getType()->getPointerAddressSpace();
4125
4126 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4127 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
4128
4129 setValue(&I, N);
4130}
4131
4132void SelectionDAGBuilder::visitInsertElement(const User &I) {
4133 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4134 SDValue InVec = getValue(I.getOperand(0));
4135 SDValue InVal = getValue(I.getOperand(1));
4136 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
4137 TLI.getVectorIdxTy(DAG.getDataLayout()));
4139 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4140 InVec, InVal, InIdx));
4141}
4142
4143void SelectionDAGBuilder::visitExtractElement(const User &I) {
4144 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4145 SDValue InVec = getValue(I.getOperand(0));
4146 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
4147 TLI.getVectorIdxTy(DAG.getDataLayout()));
4149 TLI.getValueType(DAG.getDataLayout(), I.getType()),
4150 InVec, InIdx));
4151}
4152
4153void SelectionDAGBuilder::visitShuffleVector(const User &I) {
4154 SDValue Src1 = getValue(I.getOperand(0));
4155 SDValue Src2 = getValue(I.getOperand(1));
4156 ArrayRef<int> Mask;
4157 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4158 Mask = SVI->getShuffleMask();
4159 else
4160 Mask = cast<ConstantExpr>(I).getShuffleMask();
4161 SDLoc DL = getCurSDLoc();
4162 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4163 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4164 EVT SrcVT = Src1.getValueType();
4165
4166 if (all_of(Mask, equal_to(0)) && VT.isScalableVector()) {
4167 // Canonical splat form of first element of first input vector.
4168 SDValue FirstElt =
4169 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4170 DAG.getVectorIdxConstant(0, DL));
4171 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4172 return;
4173 }
4174
4175 // For now, we only handle splats for scalable vectors.
4176 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4177 // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4178 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4179
4180 unsigned SrcNumElts = SrcVT.getVectorNumElements();
4181 unsigned MaskNumElts = Mask.size();
4182
4183 if (SrcNumElts == MaskNumElts) {
4184 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4185 return;
4186 }
4187
4188 // Normalize the shuffle vector since mask and vector length don't match.
4189 if (SrcNumElts < MaskNumElts) {
4190 // Mask is longer than the source vectors. We can use concatenate vector to
4191 // make the mask and vectors lengths match.
4192
4193 if (MaskNumElts % SrcNumElts == 0) {
4194 // Mask length is a multiple of the source vector length.
4195 // Check if the shuffle is some kind of concatenation of the input
4196 // vectors.
4197 unsigned NumConcat = MaskNumElts / SrcNumElts;
4198 bool IsConcat = true;
4199 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4200 for (unsigned i = 0; i != MaskNumElts; ++i) {
4201 int Idx = Mask[i];
4202 if (Idx < 0)
4203 continue;
4204 // Ensure the indices in each SrcVT sized piece are sequential and that
4205 // the same source is used for the whole piece.
4206 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4207 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4208 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4209 IsConcat = false;
4210 break;
4211 }
4212 // Remember which source this index came from.
4213 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4214 }
4215
4216 // The shuffle is concatenating multiple vectors together. Just emit
4217 // a CONCAT_VECTORS operation.
4218 if (IsConcat) {
4219 SmallVector<SDValue, 8> ConcatOps;
4220 for (auto Src : ConcatSrcs) {
4221 if (Src < 0)
4222 ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4223 else if (Src == 0)
4224 ConcatOps.push_back(Src1);
4225 else
4226 ConcatOps.push_back(Src2);
4227 }
4228 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4229 return;
4230 }
4231 }
4232
4233 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4234 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4235 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4236 PaddedMaskNumElts);
4237
4238 // Pad both vectors with undefs to make them the same length as the mask.
4239 SDValue UndefVal = DAG.getUNDEF(SrcVT);
4240
4241 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4242 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4243 MOps1[0] = Src1;
4244 MOps2[0] = Src2;
4245
4246 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4247 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4248
4249 // Readjust mask for new input vector length.
4250 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4251 for (unsigned i = 0; i != MaskNumElts; ++i) {
4252 int Idx = Mask[i];
4253 if (Idx >= (int)SrcNumElts)
4254 Idx -= SrcNumElts - PaddedMaskNumElts;
4255 MappedOps[i] = Idx;
4256 }
4257
4258 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4259
4260 // If the concatenated vector was padded, extract a subvector with the
4261 // correct number of elements.
4262 if (MaskNumElts != PaddedMaskNumElts)
4263 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4264 DAG.getVectorIdxConstant(0, DL));
4265
4266 setValue(&I, Result);
4267 return;
4268 }
4269
4270 assert(SrcNumElts > MaskNumElts);
4271
4272 // Analyze the access pattern of the vector to see if we can extract
4273 // two subvectors and do the shuffle.
4274 int StartIdx[2] = {-1, -1}; // StartIdx to extract from
4275 bool CanExtract = true;
4276 for (int Idx : Mask) {
4277 unsigned Input = 0;
4278 if (Idx < 0)
4279 continue;
4280
4281 if (Idx >= (int)SrcNumElts) {
4282 Input = 1;
4283 Idx -= SrcNumElts;
4284 }
4285
4286 // If all the indices come from the same MaskNumElts sized portion of
4287 // the sources we can use extract. Also make sure the extract wouldn't
4288 // extract past the end of the source.
4289 int NewStartIdx = alignDown(Idx, MaskNumElts);
4290 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4291 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4292 CanExtract = false;
4293 // Make sure we always update StartIdx as we use it to track if all
4294 // elements are undef.
4295 StartIdx[Input] = NewStartIdx;
4296 }
4297
4298 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4299 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4300 return;
4301 }
4302 if (CanExtract) {
4303 // Extract appropriate subvector and generate a vector shuffle
4304 for (unsigned Input = 0; Input < 2; ++Input) {
4305 SDValue &Src = Input == 0 ? Src1 : Src2;
4306 if (StartIdx[Input] < 0)
4307 Src = DAG.getUNDEF(VT);
4308 else {
4309 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4310 DAG.getVectorIdxConstant(StartIdx[Input], DL));
4311 }
4312 }
4313
4314 // Calculate new mask.
4315 SmallVector<int, 8> MappedOps(Mask);
4316 for (int &Idx : MappedOps) {
4317 if (Idx >= (int)SrcNumElts)
4318 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4319 else if (Idx >= 0)
4320 Idx -= StartIdx[0];
4321 }
4322
4323 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4324 return;
4325 }
4326
4327 // We can't use either concat vectors or extract subvectors so fall back to
4328 // replacing the shuffle with extract and build vector.
4329 // to insert and build vector.
4330 EVT EltVT = VT.getVectorElementType();
4332 for (int Idx : Mask) {
4333 SDValue Res;
4334
4335 if (Idx < 0) {
4336 Res = DAG.getUNDEF(EltVT);
4337 } else {
4338 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4339 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4340
4341 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4342 DAG.getVectorIdxConstant(Idx, DL));
4343 }
4344
4345 Ops.push_back(Res);
4346 }
4347
4348 setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4349}
4350
4351void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4352 ArrayRef<unsigned> Indices = I.getIndices();
4353 const Value *Op0 = I.getOperand(0);
4354 const Value *Op1 = I.getOperand(1);
4355 Type *AggTy = I.getType();
4356 Type *ValTy = Op1->getType();
4357 bool IntoUndef = isa<UndefValue>(Op0);
4358 bool FromUndef = isa<UndefValue>(Op1);
4359
4360 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4361
4362 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4363 SmallVector<EVT, 4> AggValueVTs;
4364 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4365 SmallVector<EVT, 4> ValValueVTs;
4366 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4367
4368 unsigned NumAggValues = AggValueVTs.size();
4369 unsigned NumValValues = ValValueVTs.size();
4370 SmallVector<SDValue, 4> Values(NumAggValues);
4371
4372 // Ignore an insertvalue that produces an empty object
4373 if (!NumAggValues) {
4374 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4375 return;
4376 }
4377
4378 SDValue Agg = getValue(Op0);
4379 unsigned i = 0;
4380 // Copy the beginning value(s) from the original aggregate.
4381 for (; i != LinearIndex; ++i)
4382 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4383 SDValue(Agg.getNode(), Agg.getResNo() + i);
4384 // Copy values from the inserted value(s).
4385 if (NumValValues) {
4386 SDValue Val = getValue(Op1);
4387 for (; i != LinearIndex + NumValValues; ++i)
4388 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4389 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4390 }
4391 // Copy remaining value(s) from the original aggregate.
4392 for (; i != NumAggValues; ++i)
4393 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4394 SDValue(Agg.getNode(), Agg.getResNo() + i);
4395
4397 DAG.getVTList(AggValueVTs), Values));
4398}
4399
4400void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4401 ArrayRef<unsigned> Indices = I.getIndices();
4402 const Value *Op0 = I.getOperand(0);
4403 Type *AggTy = Op0->getType();
4404 Type *ValTy = I.getType();
4405 bool OutOfUndef = isa<UndefValue>(Op0);
4406
4407 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4408
4409 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4410 SmallVector<EVT, 4> ValValueVTs;
4411 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4412
4413 unsigned NumValValues = ValValueVTs.size();
4414
4415 // Ignore a extractvalue that produces an empty object
4416 if (!NumValValues) {
4417 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4418 return;
4419 }
4420
4421 SmallVector<SDValue, 4> Values(NumValValues);
4422
4423 SDValue Agg = getValue(Op0);
4424 // Copy out the selected value(s).
4425 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4426 Values[i - LinearIndex] =
4427 OutOfUndef ?
4428 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4429 SDValue(Agg.getNode(), Agg.getResNo() + i);
4430
4432 DAG.getVTList(ValValueVTs), Values));
4433}
4434
4435void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4436 Value *Op0 = I.getOperand(0);
4437 // Note that the pointer operand may be a vector of pointers. Take the scalar
4438 // element which holds a pointer.
4439 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4440 SDValue N = getValue(Op0);
4441 SDLoc dl = getCurSDLoc();
4442 auto &TLI = DAG.getTargetLoweringInfo();
4443 GEPNoWrapFlags NW = cast<GEPOperator>(I).getNoWrapFlags();
4444
4445 // For a vector GEP, keep the prefix scalar as long as possible, then
4446 // convert any scalars encountered after the first vector operand to vectors.
4447 bool IsVectorGEP = I.getType()->isVectorTy();
4448 ElementCount VectorElementCount =
4449 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4451
4453 GTI != E; ++GTI) {
4454 const Value *Idx = GTI.getOperand();
4455 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4456 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4457 if (Field) {
4458 // N = N + Offset
4459 uint64_t Offset =
4460 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4461
4462 // In an inbounds GEP with an offset that is nonnegative even when
4463 // interpreted as signed, assume there is no unsigned overflow.
4464 SDNodeFlags Flags;
4465 if (NW.hasNoUnsignedWrap() ||
4466 (int64_t(Offset) >= 0 && NW.hasNoUnsignedSignedWrap()))
4468 Flags.setInBounds(NW.isInBounds());
4469
4470 N = DAG.getMemBasePlusOffset(
4471 N, DAG.getConstant(Offset, dl, N.getValueType()), dl, Flags);
4472 }
4473 } else {
4474 // IdxSize is the width of the arithmetic according to IR semantics.
4475 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4476 // (and fix up the result later).
4477 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4478 MVT IdxTy = MVT::getIntegerVT(IdxSize);
4479 TypeSize ElementSize =
4480 GTI.getSequentialElementStride(DAG.getDataLayout());
4481 // We intentionally mask away the high bits here; ElementSize may not
4482 // fit in IdxTy.
4483 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue(),
4484 /*isSigned=*/false, /*implicitTrunc=*/true);
4485 bool ElementScalable = ElementSize.isScalable();
4486
4487 // If this is a scalar constant or a splat vector of constants,
4488 // handle it quickly.
4489 const auto *C = dyn_cast<Constant>(Idx);
4490 if (C && isa<VectorType>(C->getType()))
4491 C = C->getSplatValue();
4492
4493 const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4494 if (CI && CI->isZero())
4495 continue;
4496 if (CI && !ElementScalable) {
4497 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4498 LLVMContext &Context = *DAG.getContext();
4499 SDValue OffsVal;
4500 if (N.getValueType().isVector())
4501 OffsVal = DAG.getConstant(
4502 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4503 else
4504 OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4505
4506 // In an inbounds GEP with an offset that is nonnegative even when
4507 // interpreted as signed, assume there is no unsigned overflow.
4508 SDNodeFlags Flags;
4509 if (NW.hasNoUnsignedWrap() ||
4510 (Offs.isNonNegative() && NW.hasNoUnsignedSignedWrap()))
4511 Flags.setNoUnsignedWrap(true);
4512 Flags.setInBounds(NW.isInBounds());
4513
4514 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4515
4516 N = DAG.getMemBasePlusOffset(N, OffsVal, dl, Flags);
4517 continue;
4518 }
4519
4520 // N = N + Idx * ElementMul;
4521 SDValue IdxN = getValue(Idx);
4522
4523 if (IdxN.getValueType().isVector() != N.getValueType().isVector()) {
4524 if (N.getValueType().isVector()) {
4525 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4526 VectorElementCount);
4527 IdxN = DAG.getSplat(VT, dl, IdxN);
4528 } else {
4529 EVT VT =
4530 EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4531 N = DAG.getSplat(VT, dl, N);
4532 }
4533 }
4534
4535 // If the index is smaller or larger than intptr_t, truncate or extend
4536 // it.
4537 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4538
4539 SDNodeFlags ScaleFlags;
4540 // The multiplication of an index by the type size does not wrap the
4541 // pointer index type in a signed sense (mul nsw).
4543
4544 // The multiplication of an index by the type size does not wrap the
4545 // pointer index type in an unsigned sense (mul nuw).
4546 ScaleFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4547
4548 if (ElementScalable) {
4549 EVT VScaleTy = N.getValueType().getScalarType();
4550 SDValue VScale = DAG.getNode(
4551 ISD::VSCALE, dl, VScaleTy,
4552 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4553 if (N.getValueType().isVector())
4554 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4555 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale,
4556 ScaleFlags);
4557 } else {
4558 // If this is a multiply by a power of two, turn it into a shl
4559 // immediately. This is a very common case.
4560 if (ElementMul != 1) {
4561 if (ElementMul.isPowerOf2()) {
4562 unsigned Amt = ElementMul.logBase2();
4563 IdxN = DAG.getNode(
4564 ISD::SHL, dl, N.getValueType(), IdxN,
4565 DAG.getShiftAmountConstant(Amt, N.getValueType(), dl),
4566 ScaleFlags);
4567 } else {
4568 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4569 IdxN.getValueType());
4570 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, Scale,
4571 ScaleFlags);
4572 }
4573 }
4574 }
4575
4576 // The successive addition of the current address, truncated to the
4577 // pointer index type and interpreted as an unsigned number, and each
4578 // offset, also interpreted as an unsigned number, does not wrap the
4579 // pointer index type (add nuw).
4580 SDNodeFlags AddFlags;
4581 AddFlags.setNoUnsignedWrap(NW.hasNoUnsignedWrap());
4582 AddFlags.setInBounds(NW.isInBounds());
4583
4584 N = DAG.getMemBasePlusOffset(N, IdxN, dl, AddFlags);
4585 }
4586 }
4587
4588 if (IsVectorGEP && !N.getValueType().isVector()) {
4589 EVT VT = EVT::getVectorVT(*Context, N.getValueType(), VectorElementCount);
4590 N = DAG.getSplat(VT, dl, N);
4591 }
4592
4593 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4594 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4595 if (IsVectorGEP) {
4596 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4597 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4598 }
4599
4600 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4601 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4602
4603 setValue(&I, N);
4604}
4605
4606void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4607 // If this is a fixed sized alloca in the entry block of the function,
4608 // allocate it statically on the stack.
4609 if (FuncInfo.StaticAllocaMap.count(&I))
4610 return; // getValue will auto-populate this.
4611
4612 SDLoc dl = getCurSDLoc();
4613 Type *Ty = I.getAllocatedType();
4614 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4615 auto &DL = DAG.getDataLayout();
4616 TypeSize TySize = DL.getTypeAllocSize(Ty);
4617 MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign());
4618
4619 SDValue AllocSize = getValue(I.getArraySize());
4620
4621 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4622 if (AllocSize.getValueType() != IntPtr)
4623 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4624
4625 AllocSize = DAG.getNode(
4626 ISD::MUL, dl, IntPtr, AllocSize,
4627 DAG.getZExtOrTrunc(DAG.getTypeSize(dl, MVT::i64, TySize), dl, IntPtr));
4628
4629 // Handle alignment. If the requested alignment is less than or equal to
4630 // the stack alignment, ignore it. If the size is greater than or equal to
4631 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4632 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4633 if (*Alignment <= StackAlign)
4634 Alignment = std::nullopt;
4635
4636 const uint64_t StackAlignMask = StackAlign.value() - 1U;
4637 // Round the size of the allocation up to the stack alignment size
4638 // by add SA-1 to the size. This doesn't overflow because we're computing
4639 // an address inside an alloca.
4640 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4641 DAG.getConstant(StackAlignMask, dl, IntPtr),
4643
4644 // Mask out the low bits for alignment purposes.
4645 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4646 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4647
4648 SDValue Ops[] = {
4649 getRoot(), AllocSize,
4650 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4651 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4652 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4653 setValue(&I, DSA);
4654 DAG.setRoot(DSA.getValue(1));
4655
4656 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4657}
4658
4659static const MDNode *getRangeMetadata(const Instruction &I) {
4660 return I.getMetadata(LLVMContext::MD_range);
4661}
4662
4663static std::optional<ConstantRange> getRange(const Instruction &I) {
4664 if (const auto *CB = dyn_cast<CallBase>(&I))
4665 if (std::optional<ConstantRange> CR = CB->getRange())
4666 return CR;
4667 if (const MDNode *Range = getRangeMetadata(I))
4669 return std::nullopt;
4670}
4671
4673 if (const auto *CB = dyn_cast<CallBase>(&I))
4674 return CB->getRetNoFPClass();
4675 return fcNone;
4676}
4677
4678void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4679 if (I.isAtomic())
4680 return visitAtomicLoad(I);
4681
4682 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4683 const Value *SV = I.getOperand(0);
4684 if (TLI.supportSwiftError()) {
4685 // Swifterror values can come from either a function parameter with
4686 // swifterror attribute or an alloca with swifterror attribute.
4687 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4688 if (Arg->hasSwiftErrorAttr())
4689 return visitLoadFromSwiftError(I);
4690 }
4691
4692 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4693 if (Alloca->isSwiftError())
4694 return visitLoadFromSwiftError(I);
4695 }
4696 }
4697
4698 SDValue Ptr = getValue(SV);
4699
4700 Type *Ty = I.getType();
4701 SmallVector<EVT, 4> ValueVTs, MemVTs;
4703 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4704 unsigned NumValues = ValueVTs.size();
4705 if (NumValues == 0)
4706 return;
4707
4708 Align Alignment = I.getAlign();
4709 AAMDNodes AAInfo = I.getAAMetadata();
4710 const MDNode *Ranges = getRangeMetadata(I);
4711 bool isVolatile = I.isVolatile();
4712 MachineMemOperand::Flags MMOFlags =
4713 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4714
4715 SDValue Root;
4716 bool ConstantMemory = false;
4717 if (isVolatile)
4718 // Serialize volatile loads with other side effects.
4719 Root = getRoot();
4720 else if (NumValues > MaxParallelChains)
4721 Root = getMemoryRoot();
4722 else if (BatchAA &&
4723 BatchAA->pointsToConstantMemory(MemoryLocation(
4724 SV,
4725 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4726 AAInfo))) {
4727 // Do not serialize (non-volatile) loads of constant memory with anything.
4728 Root = DAG.getEntryNode();
4729 ConstantMemory = true;
4731 } else {
4732 // Do not serialize non-volatile loads against each other.
4733 Root = DAG.getRoot();
4734 }
4735
4736 SDLoc dl = getCurSDLoc();
4737
4738 if (isVolatile)
4739 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4740
4741 SmallVector<SDValue, 4> Values(NumValues);
4742 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4743
4744 unsigned ChainI = 0;
4745 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4746 // Serializing loads here may result in excessive register pressure, and
4747 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4748 // could recover a bit by hoisting nodes upward in the chain by recognizing
4749 // they are side-effect free or do not alias. The optimizer should really
4750 // avoid this case by converting large object/array copies to llvm.memcpy
4751 // (MaxParallelChains should always remain as failsafe).
4752 if (ChainI == MaxParallelChains) {
4753 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4754 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4755 ArrayRef(Chains.data(), ChainI));
4756 Root = Chain;
4757 ChainI = 0;
4758 }
4759
4760 // TODO: MachinePointerInfo only supports a fixed length offset.
4761 MachinePointerInfo PtrInfo =
4762 !Offsets[i].isScalable() || Offsets[i].isZero()
4763 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4764 : MachinePointerInfo();
4765
4766 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4767 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4768 MMOFlags, AAInfo, Ranges);
4769 Chains[ChainI] = L.getValue(1);
4770
4771 if (MemVTs[i] != ValueVTs[i])
4772 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4773
4774 Values[i] = L;
4775 }
4776
4777 if (!ConstantMemory) {
4778 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4779 ArrayRef(Chains.data(), ChainI));
4780 if (isVolatile)
4781 DAG.setRoot(Chain);
4782 else
4783 PendingLoads.push_back(Chain);
4784 }
4785
4786 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4787 DAG.getVTList(ValueVTs), Values));
4788}
4789
4790void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4791 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4792 "call visitStoreToSwiftError when backend supports swifterror");
4793
4794 SmallVector<EVT, 4> ValueVTs;
4795 SmallVector<uint64_t, 4> Offsets;
4796 const Value *SrcV = I.getOperand(0);
4797 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4798 SrcV->getType(), ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4799 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4800 "expect a single EVT for swifterror");
4801
4802 SDValue Src = getValue(SrcV);
4803 // Create a virtual register, then update the virtual register.
4804 Register VReg =
4805 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4806 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4807 // Chain can be getRoot or getControlRoot.
4808 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4809 SDValue(Src.getNode(), Src.getResNo()));
4810 DAG.setRoot(CopyNode);
4811}
4812
4813void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4814 assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4815 "call visitLoadFromSwiftError when backend supports swifterror");
4816
4817 assert(!I.isVolatile() &&
4818 !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4819 !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4820 "Support volatile, non temporal, invariant for load_from_swift_error");
4821
4822 const Value *SV = I.getOperand(0);
4823 Type *Ty = I.getType();
4824 assert(
4825 (!BatchAA ||
4826 !BatchAA->pointsToConstantMemory(MemoryLocation(
4827 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4828 I.getAAMetadata()))) &&
4829 "load_from_swift_error should not be constant memory");
4830
4831 SmallVector<EVT, 4> ValueVTs;
4832 SmallVector<uint64_t, 4> Offsets;
4833 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4834 ValueVTs, /*MemVTs=*/nullptr, &Offsets, 0);
4835 assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4836 "expect a single EVT for swifterror");
4837
4838 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4839 SDValue L = DAG.getCopyFromReg(
4840 getRoot(), getCurSDLoc(),
4841 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4842
4843 setValue(&I, L);
4844}
4845
4846void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4847 if (I.isAtomic())
4848 return visitAtomicStore(I);
4849
4850 const Value *SrcV = I.getOperand(0);
4851 const Value *PtrV = I.getOperand(1);
4852
4853 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4854 if (TLI.supportSwiftError()) {
4855 // Swifterror values can come from either a function parameter with
4856 // swifterror attribute or an alloca with swifterror attribute.
4857 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4858 if (Arg->hasSwiftErrorAttr())
4859 return visitStoreToSwiftError(I);
4860 }
4861
4862 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4863 if (Alloca->isSwiftError())
4864 return visitStoreToSwiftError(I);
4865 }
4866 }
4867
4868 SmallVector<EVT, 4> ValueVTs, MemVTs;
4870 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4871 SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4872 unsigned NumValues = ValueVTs.size();
4873 if (NumValues == 0)
4874 return;
4875
4876 // Get the lowered operands. Note that we do this after
4877 // checking if NumResults is zero, because with zero results
4878 // the operands won't have values in the map.
4879 SDValue Src = getValue(SrcV);
4880 SDValue Ptr = getValue(PtrV);
4881
4882 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4883 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4884 SDLoc dl = getCurSDLoc();
4885 Align Alignment = I.getAlign();
4886 AAMDNodes AAInfo = I.getAAMetadata();
4887
4888 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4889
4890 unsigned ChainI = 0;
4891 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4892 // See visitLoad comments.
4893 if (ChainI == MaxParallelChains) {
4894 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4895 ArrayRef(Chains.data(), ChainI));
4896 Root = Chain;
4897 ChainI = 0;
4898 }
4899
4900 // TODO: MachinePointerInfo only supports a fixed length offset.
4901 MachinePointerInfo PtrInfo =
4902 !Offsets[i].isScalable() || Offsets[i].isZero()
4903 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4904 : MachinePointerInfo();
4905
4906 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4907 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4908 if (MemVTs[i] != ValueVTs[i])
4909 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4910 SDValue St =
4911 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4912 Chains[ChainI] = St;
4913 }
4914
4915 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4916 ArrayRef(Chains.data(), ChainI));
4917 setValue(&I, StoreNode);
4918 DAG.setRoot(StoreNode);
4919}
4920
4921void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4922 bool IsCompressing) {
4923 SDLoc sdl = getCurSDLoc();
4924
4925 Value *Src0Operand = I.getArgOperand(0);
4926 Value *PtrOperand = I.getArgOperand(1);
4927 Value *MaskOperand = I.getArgOperand(2);
4928 Align Alignment = I.getParamAlign(1).valueOrOne();
4929
4930 SDValue Ptr = getValue(PtrOperand);
4931 SDValue Src0 = getValue(Src0Operand);
4932 SDValue Mask = getValue(MaskOperand);
4933 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4934
4935 EVT VT = Src0.getValueType();
4936
4937 auto MMOFlags = MachineMemOperand::MOStore;
4938 if (I.hasMetadata(LLVMContext::MD_nontemporal))
4940
4941 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4942 MachinePointerInfo(PtrOperand), MMOFlags,
4943 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4944
4945 const auto &TLI = DAG.getTargetLoweringInfo();
4946
4947 SDValue StoreNode =
4948 !IsCompressing && TTI->hasConditionalLoadStoreForType(
4949 I.getArgOperand(0)->getType(), /*IsStore=*/true)
4950 ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4951 Mask)
4952 : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4953 VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4954 IsCompressing);
4955 DAG.setRoot(StoreNode);
4956 setValue(&I, StoreNode);
4957}
4958
4959// Get a uniform base for the Gather/Scatter intrinsic.
4960// The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4961// We try to represent it as a base pointer + vector of indices.
4962// Usually, the vector of pointers comes from a 'getelementptr' instruction.
4963// The first operand of the GEP may be a single pointer or a vector of pointers
4964// Example:
4965// %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4966// or
4967// %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind
4968// %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4969//
4970// When the first GEP operand is a single pointer - it is the uniform base we
4971// are looking for. If first operand of the GEP is a splat vector - we
4972// extract the splat value and use it as a uniform base.
4973// In all other cases the function returns 'false'.
4974static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4975 SDValue &Scale, SelectionDAGBuilder *SDB,
4976 const BasicBlock *CurBB, uint64_t ElemSize) {
4977 SelectionDAG& DAG = SDB->DAG;
4978 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4979 const DataLayout &DL = DAG.getDataLayout();
4980
4981 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4982
4983 // Handle splat constant pointer.
4984 if (auto *C = dyn_cast<Constant>(Ptr)) {
4985 C = C->getSplatValue();
4986 if (!C)
4987 return false;
4988
4989 Base = SDB->getValue(C);
4990
4991 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
4992 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
4993 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
4994 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4995 return true;
4996 }
4997
4999 if (!GEP || GEP->getParent() != CurBB)
5000 return false;
5001
5002 if (GEP->getNumOperands() != 2)
5003 return false;
5004
5005 const Value *BasePtr = GEP->getPointerOperand();
5006 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
5007
5008 // Make sure the base is scalar and the index is a vector.
5009 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
5010 return false;
5011
5012 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
5013 if (ScaleVal.isScalable())
5014 return false;
5015
5016 // Target may not support the required addressing mode.
5017 if (ScaleVal != 1 &&
5018 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
5019 return false;
5020
5021 Base = SDB->getValue(BasePtr);
5022 Index = SDB->getValue(IndexVal);
5023
5024 Scale =
5025 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
5026 return true;
5027}
5028
5029void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
5030 SDLoc sdl = getCurSDLoc();
5031
5032 // llvm.masked.scatter.*(Src0, Ptrs, Mask)
5033 const Value *Ptr = I.getArgOperand(1);
5034 SDValue Src0 = getValue(I.getArgOperand(0));
5035 SDValue Mask = getValue(I.getArgOperand(2));
5036 EVT VT = Src0.getValueType();
5037 Align Alignment = I.getParamAlign(1).valueOrOne();
5038 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5039
5040 SDValue Base;
5041 SDValue Index;
5042 SDValue Scale;
5043 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5044 I.getParent(), VT.getScalarStoreSize());
5045
5046 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5047 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5048 MachinePointerInfo(AS), MachineMemOperand::MOStore,
5049 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
5050 if (!UniformBase) {
5051 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5052 Index = getValue(Ptr);
5053 Scale =
5054 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5055 }
5056
5057 EVT IdxVT = Index.getValueType();
5058 EVT EltTy = IdxVT.getVectorElementType();
5059 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5060 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5061 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5062 }
5063
5064 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
5065 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
5066 Ops, MMO, ISD::SIGNED_SCALED, false);
5067 DAG.setRoot(Scatter);
5068 setValue(&I, Scatter);
5069}
5070
5071void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
5072 SDLoc sdl = getCurSDLoc();
5073
5074 Value *PtrOperand = I.getArgOperand(0);
5075 Value *MaskOperand = I.getArgOperand(1);
5076 Value *Src0Operand = I.getArgOperand(2);
5077 Align Alignment = I.getParamAlign(0).valueOrOne();
5078
5079 SDValue Ptr = getValue(PtrOperand);
5080 SDValue Src0 = getValue(Src0Operand);
5081 SDValue Mask = getValue(MaskOperand);
5082 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
5083
5084 EVT VT = Src0.getValueType();
5085 AAMDNodes AAInfo = I.getAAMetadata();
5086 const MDNode *Ranges = getRangeMetadata(I);
5087
5088 // Do not serialize masked loads of constant memory with anything.
5089 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
5090 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
5091
5092 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
5093
5094 auto MMOFlags = MachineMemOperand::MOLoad;
5095 if (I.hasMetadata(LLVMContext::MD_nontemporal))
5097 if (I.hasMetadata(LLVMContext::MD_invariant_load))
5099
5100 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5101 MachinePointerInfo(PtrOperand), MMOFlags,
5102 LocationSize::beforeOrAfterPointer(), Alignment, AAInfo, Ranges);
5103
5104 const auto &TLI = DAG.getTargetLoweringInfo();
5105
5106 // The Load/Res may point to different values and both of them are output
5107 // variables.
5108 SDValue Load;
5109 SDValue Res;
5110 if (!IsExpanding &&
5111 TTI->hasConditionalLoadStoreForType(Src0Operand->getType(),
5112 /*IsStore=*/false))
5113 Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
5114 else
5115 Res = Load =
5116 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
5117 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
5118 if (AddToChain)
5119 PendingLoads.push_back(Load.getValue(1));
5120 setValue(&I, Res);
5121}
5122
5123void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
5124 SDLoc sdl = getCurSDLoc();
5125
5126 // @llvm.masked.gather.*(Ptrs, Mask, Src0)
5127 const Value *Ptr = I.getArgOperand(0);
5128 SDValue Src0 = getValue(I.getArgOperand(2));
5129 SDValue Mask = getValue(I.getArgOperand(1));
5130
5131 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5132 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5133 Align Alignment = I.getParamAlign(0).valueOrOne();
5134
5135 const MDNode *Ranges = getRangeMetadata(I);
5136
5137 SDValue Root = DAG.getRoot();
5138 SDValue Base;
5139 SDValue Index;
5140 SDValue Scale;
5141 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
5142 I.getParent(), VT.getScalarStoreSize());
5143 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5144 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5145 MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5146 LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5147 Ranges);
5148
5149 if (!UniformBase) {
5150 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5151 Index = getValue(Ptr);
5152 Scale =
5153 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5154 }
5155
5156 EVT IdxVT = Index.getValueType();
5157 EVT EltTy = IdxVT.getVectorElementType();
5158 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5159 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
5160 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5161 }
5162
5163 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5164 SDValue Gather =
5165 DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, Ops, MMO,
5167
5168 PendingLoads.push_back(Gather.getValue(1));
5169 setValue(&I, Gather);
5170}
5171
5172void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5173 SDLoc dl = getCurSDLoc();
5174 AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5175 AtomicOrdering FailureOrdering = I.getFailureOrdering();
5176 SyncScope::ID SSID = I.getSyncScopeID();
5177
5178 SDValue InChain = getRoot();
5179
5180 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5181 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5182
5183 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5184 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5185
5186 MachineFunction &MF = DAG.getMachineFunction();
5187 MachineMemOperand *MMO = MF.getMachineMemOperand(
5188 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5189 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
5190 FailureOrdering);
5191
5193 dl, MemVT, VTs, InChain,
5194 getValue(I.getPointerOperand()),
5195 getValue(I.getCompareOperand()),
5196 getValue(I.getNewValOperand()), MMO);
5197
5198 SDValue OutChain = L.getValue(2);
5199
5200 setValue(&I, L);
5201 DAG.setRoot(OutChain);
5202}
5203
5204void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5205 SDLoc dl = getCurSDLoc();
5207 switch (I.getOperation()) {
5208 default: llvm_unreachable("Unknown atomicrmw operation");
5226 break;
5229 break;
5232 break;
5235 break;
5238 break;
5241 break;
5242 }
5243 AtomicOrdering Ordering = I.getOrdering();
5244 SyncScope::ID SSID = I.getSyncScopeID();
5245
5246 SDValue InChain = getRoot();
5247
5248 auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5249 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5250 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5251
5252 MachineFunction &MF = DAG.getMachineFunction();
5253 MachineMemOperand *MMO = MF.getMachineMemOperand(
5254 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5255 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
5256
5257 SDValue L =
5258 DAG.getAtomic(NT, dl, MemVT, InChain,
5259 getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5260 MMO);
5261
5262 SDValue OutChain = L.getValue(1);
5263
5264 setValue(&I, L);
5265 DAG.setRoot(OutChain);
5266}
5267
5268void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5269 SDLoc dl = getCurSDLoc();
5270 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5271 SDValue Ops[3];
5272 Ops[0] = getRoot();
5273 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5274 TLI.getFenceOperandTy(DAG.getDataLayout()));
5275 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5276 TLI.getFenceOperandTy(DAG.getDataLayout()));
5277 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5278 setValue(&I, N);
5279 DAG.setRoot(N);
5280}
5281
5282void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5283 SDLoc dl = getCurSDLoc();
5284 AtomicOrdering Order = I.getOrdering();
5285 SyncScope::ID SSID = I.getSyncScopeID();
5286
5287 SDValue InChain = getRoot();
5288
5289 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5290 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5291 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5292
5293 if (!TLI.supportsUnalignedAtomics() &&
5294 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5295 report_fatal_error("Cannot generate unaligned atomic load");
5296
5297 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5298
5299 const MDNode *Ranges = getRangeMetadata(I);
5300 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5301 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5302 I.getAlign(), AAMDNodes(), Ranges, SSID, Order);
5303
5304 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5305
5306 SDValue Ptr = getValue(I.getPointerOperand());
5307 SDValue L =
5308 DAG.getAtomicLoad(ISD::NON_EXTLOAD, dl, MemVT, MemVT, InChain, Ptr, MMO);
5309
5310 SDValue OutChain = L.getValue(1);
5311 if (MemVT != VT)
5312 L = DAG.getPtrExtOrTrunc(L, dl, VT);
5313
5314 setValue(&I, L);
5315 DAG.setRoot(OutChain);
5316}
5317
5318void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5319 SDLoc dl = getCurSDLoc();
5320
5321 AtomicOrdering Ordering = I.getOrdering();
5322 SyncScope::ID SSID = I.getSyncScopeID();
5323
5324 SDValue InChain = getRoot();
5325
5326 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5327 EVT MemVT =
5328 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5329
5330 if (!TLI.supportsUnalignedAtomics() &&
5331 I.getAlign().value() < MemVT.getSizeInBits() / 8)
5332 report_fatal_error("Cannot generate unaligned atomic store");
5333
5334 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5335
5336 MachineFunction &MF = DAG.getMachineFunction();
5337 MachineMemOperand *MMO = MF.getMachineMemOperand(
5338 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
5339 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
5340
5341 SDValue Val = getValue(I.getValueOperand());
5342 if (Val.getValueType() != MemVT)
5343 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5344 SDValue Ptr = getValue(I.getPointerOperand());
5345
5346 SDValue OutChain =
5347 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5348
5349 setValue(&I, OutChain);
5350 DAG.setRoot(OutChain);
5351}
5352
5353/// Check if this intrinsic call depends on the chain (1st return value)
5354/// and if it only *loads* memory.
5355/// Ignore the callsite's attributes. A specific call site may be marked with
5356/// readnone, but the lowering code will expect the chain based on the
5357/// definition.
5358std::pair<bool, bool>
5359SelectionDAGBuilder::getTargetIntrinsicCallProperties(const CallBase &I) {
5360 const Function *F = I.getCalledFunction();
5361 bool HasChain = !F->doesNotAccessMemory();
5362 bool OnlyLoad =
5363 HasChain && F->onlyReadsMemory() && F->willReturn() && F->doesNotThrow();
5364
5365 return {HasChain, OnlyLoad};
5366}
5367
5368SmallVector<SDValue, 8> SelectionDAGBuilder::getTargetIntrinsicOperands(
5369 const CallBase &I, bool HasChain, bool OnlyLoad,
5370 TargetLowering::IntrinsicInfo *TgtMemIntrinsicInfo) {
5371 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5372
5373 // Build the operand list.
5375 if (HasChain) { // If this intrinsic has side-effects, chainify it.
5376 if (OnlyLoad) {
5377 // We don't need to serialize loads against other loads.
5378 Ops.push_back(DAG.getRoot());
5379 } else {
5380 Ops.push_back(getRoot());
5381 }
5382 }
5383
5384 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5385 if (!TgtMemIntrinsicInfo || TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_VOID ||
5386 TgtMemIntrinsicInfo->opc == ISD::INTRINSIC_W_CHAIN)
5387 Ops.push_back(DAG.getTargetConstant(I.getIntrinsicID(), getCurSDLoc(),
5388 TLI.getPointerTy(DAG.getDataLayout())));
5389
5390 // Add all operands of the call to the operand list.
5391 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5392 const Value *Arg = I.getArgOperand(i);
5393 if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5394 Ops.push_back(getValue(Arg));
5395 continue;
5396 }
5397
5398 // Use TargetConstant instead of a regular constant for immarg.
5399 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5400 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5401 assert(CI->getBitWidth() <= 64 &&
5402 "large intrinsic immediates not handled");
5403 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5404 } else {
5405 Ops.push_back(
5406 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5407 }
5408 }
5409
5410 if (std::optional<OperandBundleUse> Bundle =
5411 I.getOperandBundle(LLVMContext::OB_deactivation_symbol)) {
5412 auto *Sym = Bundle->Inputs[0].get();
5413 SDValue SDSym = getValue(Sym);
5414 SDSym = DAG.getDeactivationSymbol(cast<GlobalValue>(Sym));
5415 Ops.push_back(SDSym);
5416 }
5417
5418 if (std::optional<OperandBundleUse> Bundle =
5419 I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5420 Value *Token = Bundle->Inputs[0].get();
5421 SDValue ConvControlToken = getValue(Token);
5422 assert(Ops.back().getValueType() != MVT::Glue &&
5423 "Did not expect another glue node here.");
5424 ConvControlToken =
5425 DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5426 Ops.push_back(ConvControlToken);
5427 }
5428
5429 return Ops;
5430}
5431
5432SDVTList SelectionDAGBuilder::getTargetIntrinsicVTList(const CallBase &I,
5433 bool HasChain) {
5434 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5435
5436 SmallVector<EVT, 4> ValueVTs;
5437 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5438
5439 if (HasChain)
5440 ValueVTs.push_back(MVT::Other);
5441
5442 return DAG.getVTList(ValueVTs);
5443}
5444
5445/// Get an INTRINSIC node for a target intrinsic which does not touch memory.
5446SDValue SelectionDAGBuilder::getTargetNonMemIntrinsicNode(
5447 const Type &IntrinsicVT, bool HasChain, ArrayRef<SDValue> Ops,
5448 const SDVTList &VTs) {
5449 if (!HasChain)
5450 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5451 if (!IntrinsicVT.isVoidTy())
5452 return DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5453 return DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5454}
5455
5456/// Set root, convert return type if necessary and check alignment.
5457SDValue SelectionDAGBuilder::handleTargetIntrinsicRet(const CallBase &I,
5458 bool HasChain,
5459 bool OnlyLoad,
5460 SDValue Result) {
5461 if (HasChain) {
5462 SDValue Chain = Result.getValue(Result.getNode()->getNumValues() - 1);
5463 if (OnlyLoad)
5464 PendingLoads.push_back(Chain);
5465 else
5466 DAG.setRoot(Chain);
5467 }
5468
5469 if (I.getType()->isVoidTy())
5470 return Result;
5471
5472 if (MaybeAlign Alignment = I.getRetAlign(); InsertAssertAlign && Alignment) {
5473 // Insert `assertalign` node if there's an alignment.
5474 Result = DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5475 } else if (!isa<VectorType>(I.getType())) {
5476 Result = lowerRangeToAssertZExt(DAG, I, Result);
5477 }
5478
5479 return Result;
5480}
5481
5482/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5483/// node.
5484void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5485 unsigned Intrinsic) {
5486 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(I);
5487
5488 // Info is set by getTgtMemIntrinsic
5489 TargetLowering::IntrinsicInfo Info;
5490 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5491 bool IsTgtMemIntrinsic =
5492 TLI.getTgtMemIntrinsic(Info, I, DAG.getMachineFunction(), Intrinsic);
5493
5494 SmallVector<SDValue, 8> Ops = getTargetIntrinsicOperands(
5495 I, HasChain, OnlyLoad, IsTgtMemIntrinsic ? &Info : nullptr);
5496 SDVTList VTs = getTargetIntrinsicVTList(I, HasChain);
5497
5498 // Propagate fast-math-flags from IR to node(s).
5499 SDNodeFlags Flags;
5500 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5501 Flags.copyFMF(*FPMO);
5502 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5503
5504 // Create the node.
5506
5507 // In some cases, custom collection of operands from CallInst I may be needed.
5509 if (IsTgtMemIntrinsic) {
5510 // This is target intrinsic that touches memory
5511 //
5512 // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic
5513 // didn't yield anything useful.
5514 MachinePointerInfo MPI;
5515 if (Info.ptrVal)
5516 MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5517 else if (Info.fallbackAddressSpace)
5518 MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5519 EVT MemVT = Info.memVT;
5520 LocationSize Size = LocationSize::precise(Info.size);
5521 if (Size.hasValue() && !Size.getValue())
5523 Align Alignment = Info.align.value_or(DAG.getEVTAlign(MemVT));
5524 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5525 MPI, Info.flags, Size, Alignment, I.getAAMetadata(), /*Ranges=*/nullptr,
5526 Info.ssid, Info.order, Info.failureOrder);
5527 Result =
5528 DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops, MemVT, MMO);
5529 } else {
5530 Result = getTargetNonMemIntrinsicNode(*I.getType(), HasChain, Ops, VTs);
5531 }
5532
5533 Result = handleTargetIntrinsicRet(I, HasChain, OnlyLoad, Result);
5534
5535 setValue(&I, Result);
5536}
5537
5538/// GetSignificand - Get the significand and build it into a floating-point
5539/// number with exponent of 1:
5540///
5541/// Op = (Op & 0x007fffff) | 0x3f800000;
5542///
5543/// where Op is the hexadecimal representation of floating point value.
5545 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5546 DAG.getConstant(0x007fffff, dl, MVT::i32));
5547 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5548 DAG.getConstant(0x3f800000, dl, MVT::i32));
5549 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5550}
5551
5552/// GetExponent - Get the exponent:
5553///
5554/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5555///
5556/// where Op is the hexadecimal representation of floating point value.
5558 const TargetLowering &TLI, const SDLoc &dl) {
5559 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5560 DAG.getConstant(0x7f800000, dl, MVT::i32));
5561 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
5562 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5563 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5564 DAG.getConstant(127, dl, MVT::i32));
5565 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5566}
5567
5568/// getF32Constant - Get 32-bit floating point constant.
5569static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt,
5570 const SDLoc &dl) {
5571 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5572 MVT::f32);
5573}
5574
5576 SelectionDAG &DAG) {
5577 // TODO: What fast-math-flags should be set on the floating-point nodes?
5578
5579 // IntegerPartOfX = ((int32_t)(t0);
5580 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5581
5582 // FractionalPartOfX = t0 - (float)IntegerPartOfX;
5583 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5584 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5585
5586 // IntegerPartOfX <<= 23;
5587 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5588 DAG.getShiftAmountConstant(23, MVT::i32, dl));
5589
5590 SDValue TwoToFractionalPartOfX;
5591 if (LimitFloatPrecision <= 6) {
5592 // For floating-point precision of 6:
5593 //
5594 // TwoToFractionalPartOfX =
5595 // 0.997535578f +
5596 // (0.735607626f + 0.252464424f * x) * x;
5597 //
5598 // error 0.0144103317, which is 6 bits
5599 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5600 getF32Constant(DAG, 0x3e814304, dl));
5601 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5602 getF32Constant(DAG, 0x3f3c50c8, dl));
5603 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5604 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5605 getF32Constant(DAG, 0x3f7f5e7e, dl));
5606 } else if (LimitFloatPrecision <= 12) {
5607 // For floating-point precision of 12:
5608 //
5609 // TwoToFractionalPartOfX =
5610 // 0.999892986f +
5611 // (0.696457318f +
5612 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
5613 //
5614 // error 0.000107046256, which is 13 to 14 bits
5615 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5616 getF32Constant(DAG, 0x3da235e3, dl));
5617 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5618 getF32Constant(DAG, 0x3e65b8f3, dl));
5619 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5620 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5621 getF32Constant(DAG, 0x3f324b07, dl));
5622 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5623 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5624 getF32Constant(DAG, 0x3f7ff8fd, dl));
5625 } else { // LimitFloatPrecision <= 18
5626 // For floating-point precision of 18:
5627 //
5628 // TwoToFractionalPartOfX =
5629 // 0.999999982f +
5630 // (0.693148872f +
5631 // (0.240227044f +
5632 // (0.554906021e-1f +
5633 // (0.961591928e-2f +
5634 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5635 // error 2.47208000*10^(-7), which is better than 18 bits
5636 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5637 getF32Constant(DAG, 0x3924b03e, dl));
5638 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5639 getF32Constant(DAG, 0x3ab24b87, dl));
5640 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5641 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5642 getF32Constant(DAG, 0x3c1d8c17, dl));
5643 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5644 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5645 getF32Constant(DAG, 0x3d634a1d, dl));
5646 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5647 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5648 getF32Constant(DAG, 0x3e75fe14, dl));
5649 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5650 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5651 getF32Constant(DAG, 0x3f317234, dl));
5652 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5653 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5654 getF32Constant(DAG, 0x3f800000, dl));
5655 }
5656
5657 // Add the exponent into the result in integer domain.
5658 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5659 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5660 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5661}
5662
5663/// expandExp - Lower an exp intrinsic. Handles the special sequences for
5664/// limited-precision mode.
5666 const TargetLowering &TLI, SDNodeFlags Flags) {
5667 if (Op.getValueType() == MVT::f32 &&
5669
5670 // Put the exponent in the right bit position for later addition to the
5671 // final result:
5672 //
5673 // t0 = Op * log2(e)
5674
5675 // TODO: What fast-math-flags should be set here?
5676 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5677 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5678 return getLimitedPrecisionExp2(t0, dl, DAG);
5679 }
5680
5681 // No special expansion.
5682 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5683}
5684
5685/// expandLog - Lower a log intrinsic. Handles the special sequences for
5686/// limited-precision mode.
5688 const TargetLowering &TLI, SDNodeFlags Flags) {
5689 // TODO: What fast-math-flags should be set on the floating-point nodes?
5690
5691 if (Op.getValueType() == MVT::f32 &&
5693 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5694
5695 // Scale the exponent by log(2).
5696 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5697 SDValue LogOfExponent =
5698 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5699 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5700
5701 // Get the significand and build it into a floating-point number with
5702 // exponent of 1.
5703 SDValue X = GetSignificand(DAG, Op1, dl);
5704
5705 SDValue LogOfMantissa;
5706 if (LimitFloatPrecision <= 6) {
5707 // For floating-point precision of 6:
5708 //
5709 // LogofMantissa =
5710 // -1.1609546f +
5711 // (1.4034025f - 0.23903021f * x) * x;
5712 //
5713 // error 0.0034276066, which is better than 8 bits
5714 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5715 getF32Constant(DAG, 0xbe74c456, dl));
5716 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5717 getF32Constant(DAG, 0x3fb3a2b1, dl));
5718 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5719 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5720 getF32Constant(DAG, 0x3f949a29, dl));
5721 } else if (LimitFloatPrecision <= 12) {
5722 // For floating-point precision of 12:
5723 //
5724 // LogOfMantissa =
5725 // -1.7417939f +
5726 // (2.8212026f +
5727 // (-1.4699568f +
5728 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5729 //
5730 // error 0.000061011436, which is 14 bits
5731 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5732 getF32Constant(DAG, 0xbd67b6d6, dl));
5733 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5734 getF32Constant(DAG, 0x3ee4f4b8, dl));
5735 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5736 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5737 getF32Constant(DAG, 0x3fbc278b, 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, 0x40348e95, dl));
5741 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5742 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5743 getF32Constant(DAG, 0x3fdef31a, dl));
5744 } else { // LimitFloatPrecision <= 18
5745 // For floating-point precision of 18:
5746 //
5747 // LogOfMantissa =
5748 // -2.1072184f +
5749 // (4.2372794f +
5750 // (-3.7029485f +
5751 // (2.2781945f +
5752 // (-0.87823314f +
5753 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5754 //
5755 // error 0.0000023660568, which is better than 18 bits
5756 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5757 getF32Constant(DAG, 0xbc91e5ac, dl));
5758 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5759 getF32Constant(DAG, 0x3e4350aa, dl));
5760 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5761 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5762 getF32Constant(DAG, 0x3f60d3e3, dl));
5763 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5764 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5765 getF32Constant(DAG, 0x4011cdf0, dl));
5766 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5767 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5768 getF32Constant(DAG, 0x406cfd1c, dl));
5769 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5770 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5771 getF32Constant(DAG, 0x408797cb, dl));
5772 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5773 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5774 getF32Constant(DAG, 0x4006dcab, dl));
5775 }
5776
5777 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5778 }
5779
5780 // No special expansion.
5781 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5782}
5783
5784/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5785/// limited-precision mode.
5787 const TargetLowering &TLI, SDNodeFlags Flags) {
5788 // TODO: What fast-math-flags should be set on the floating-point nodes?
5789
5790 if (Op.getValueType() == MVT::f32 &&
5792 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5793
5794 // Get the exponent.
5795 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5796
5797 // Get the significand and build it into a floating-point number with
5798 // exponent of 1.
5799 SDValue X = GetSignificand(DAG, Op1, dl);
5800
5801 // Different possible minimax approximations of significand in
5802 // floating-point for various degrees of accuracy over [1,2].
5803 SDValue Log2ofMantissa;
5804 if (LimitFloatPrecision <= 6) {
5805 // For floating-point precision of 6:
5806 //
5807 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5808 //
5809 // error 0.0049451742, which is more than 7 bits
5810 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5811 getF32Constant(DAG, 0xbeb08fe0, dl));
5812 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5813 getF32Constant(DAG, 0x40019463, dl));
5814 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5815 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5816 getF32Constant(DAG, 0x3fd6633d, dl));
5817 } else if (LimitFloatPrecision <= 12) {
5818 // For floating-point precision of 12:
5819 //
5820 // Log2ofMantissa =
5821 // -2.51285454f +
5822 // (4.07009056f +
5823 // (-2.12067489f +
5824 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5825 //
5826 // error 0.0000876136000, which is better than 13 bits
5827 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5828 getF32Constant(DAG, 0xbda7262e, dl));
5829 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5830 getF32Constant(DAG, 0x3f25280b, dl));
5831 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5832 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5833 getF32Constant(DAG, 0x4007b923, dl));
5834 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5835 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5836 getF32Constant(DAG, 0x40823e2f, dl));
5837 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5838 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5839 getF32Constant(DAG, 0x4020d29c, dl));
5840 } else { // LimitFloatPrecision <= 18
5841 // For floating-point precision of 18:
5842 //
5843 // Log2ofMantissa =
5844 // -3.0400495f +
5845 // (6.1129976f +
5846 // (-5.3420409f +
5847 // (3.2865683f +
5848 // (-1.2669343f +
5849 // (0.27515199f -
5850 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5851 //
5852 // error 0.0000018516, which is better than 18 bits
5853 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5854 getF32Constant(DAG, 0xbcd2769e, dl));
5855 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5856 getF32Constant(DAG, 0x3e8ce0b9, dl));
5857 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5858 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5859 getF32Constant(DAG, 0x3fa22ae7, dl));
5860 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5861 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5862 getF32Constant(DAG, 0x40525723, dl));
5863 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5864 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5865 getF32Constant(DAG, 0x40aaf200, dl));
5866 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5867 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5868 getF32Constant(DAG, 0x40c39dad, dl));
5869 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5870 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5871 getF32Constant(DAG, 0x4042902c, dl));
5872 }
5873
5874 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5875 }
5876
5877 // No special expansion.
5878 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5879}
5880
5881/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5882/// limited-precision mode.
5884 const TargetLowering &TLI, SDNodeFlags Flags) {
5885 // TODO: What fast-math-flags should be set on the floating-point nodes?
5886
5887 if (Op.getValueType() == MVT::f32 &&
5889 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5890
5891 // Scale the exponent by log10(2) [0.30102999f].
5892 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5893 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5894 getF32Constant(DAG, 0x3e9a209a, dl));
5895
5896 // Get the significand and build it into a floating-point number with
5897 // exponent of 1.
5898 SDValue X = GetSignificand(DAG, Op1, dl);
5899
5900 SDValue Log10ofMantissa;
5901 if (LimitFloatPrecision <= 6) {
5902 // For floating-point precision of 6:
5903 //
5904 // Log10ofMantissa =
5905 // -0.50419619f +
5906 // (0.60948995f - 0.10380950f * x) * x;
5907 //
5908 // error 0.0014886165, which is 6 bits
5909 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5910 getF32Constant(DAG, 0xbdd49a13, dl));
5911 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5912 getF32Constant(DAG, 0x3f1c0789, dl));
5913 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5914 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5915 getF32Constant(DAG, 0x3f011300, dl));
5916 } else if (LimitFloatPrecision <= 12) {
5917 // For floating-point precision of 12:
5918 //
5919 // Log10ofMantissa =
5920 // -0.64831180f +
5921 // (0.91751397f +
5922 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5923 //
5924 // error 0.00019228036, which is better than 12 bits
5925 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5926 getF32Constant(DAG, 0x3d431f31, dl));
5927 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5928 getF32Constant(DAG, 0x3ea21fb2, dl));
5929 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5930 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5931 getF32Constant(DAG, 0x3f6ae232, dl));
5932 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5933 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5934 getF32Constant(DAG, 0x3f25f7c3, dl));
5935 } else { // LimitFloatPrecision <= 18
5936 // For floating-point precision of 18:
5937 //
5938 // Log10ofMantissa =
5939 // -0.84299375f +
5940 // (1.5327582f +
5941 // (-1.0688956f +
5942 // (0.49102474f +
5943 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5944 //
5945 // error 0.0000037995730, which is better than 18 bits
5946 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5947 getF32Constant(DAG, 0x3c5d51ce, dl));
5948 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5949 getF32Constant(DAG, 0x3e00685a, dl));
5950 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5951 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5952 getF32Constant(DAG, 0x3efb6798, dl));
5953 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5954 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5955 getF32Constant(DAG, 0x3f88d192, dl));
5956 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5957 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5958 getF32Constant(DAG, 0x3fc4316c, dl));
5959 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5960 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5961 getF32Constant(DAG, 0x3f57ce70, dl));
5962 }
5963
5964 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5965 }
5966
5967 // No special expansion.
5968 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5969}
5970
5971/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5972/// limited-precision mode.
5974 const TargetLowering &TLI, SDNodeFlags Flags) {
5975 if (Op.getValueType() == MVT::f32 &&
5977 return getLimitedPrecisionExp2(Op, dl, DAG);
5978
5979 // No special expansion.
5980 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
5981}
5982
5983/// visitPow - Lower a pow intrinsic. Handles the special sequences for
5984/// limited-precision mode with x == 10.0f.
5986 SelectionDAG &DAG, const TargetLowering &TLI,
5987 SDNodeFlags Flags) {
5988 bool IsExp10 = false;
5989 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
5992 APFloat Ten(10.0f);
5993 IsExp10 = LHSC->isExactlyValue(Ten);
5994 }
5995 }
5996
5997 // TODO: What fast-math-flags should be set on the FMUL node?
5998 if (IsExp10) {
5999 // Put the exponent in the right bit position for later addition to the
6000 // final result:
6001 //
6002 // #define LOG2OF10 3.3219281f
6003 // t0 = Op * LOG2OF10;
6004 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
6005 getF32Constant(DAG, 0x40549a78, dl));
6006 return getLimitedPrecisionExp2(t0, dl, DAG);
6007 }
6008
6009 // No special expansion.
6010 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
6011}
6012
6013/// ExpandPowI - Expand a llvm.powi intrinsic.
6015 SelectionDAG &DAG) {
6016 // If RHS is a constant, we can expand this out to a multiplication tree if
6017 // it's beneficial on the target, otherwise we end up lowering to a call to
6018 // __powidf2 (for example).
6020 unsigned Val = RHSC->getSExtValue();
6021
6022 // powi(x, 0) -> 1.0
6023 if (Val == 0)
6024 return DAG.getConstantFP(1.0, DL, LHS.getValueType());
6025
6027 Val, DAG.shouldOptForSize())) {
6028 // Get the exponent as a positive value.
6029 if ((int)Val < 0)
6030 Val = -Val;
6031 // We use the simple binary decomposition method to generate the multiply
6032 // sequence. There are more optimal ways to do this (for example,
6033 // powi(x,15) generates one more multiply than it should), but this has
6034 // the benefit of being both really simple and much better than a libcall.
6035 SDValue Res; // Logically starts equal to 1.0
6036 SDValue CurSquare = LHS;
6037 // TODO: Intrinsics should have fast-math-flags that propagate to these
6038 // nodes.
6039 while (Val) {
6040 if (Val & 1) {
6041 if (Res.getNode())
6042 Res =
6043 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
6044 else
6045 Res = CurSquare; // 1.0*CurSquare.
6046 }
6047
6048 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
6049 CurSquare, CurSquare);
6050 Val >>= 1;
6051 }
6052
6053 // If the original was negative, invert the result, producing 1/(x*x*x).
6054 if (RHSC->getSExtValue() < 0)
6055 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
6056 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
6057 return Res;
6058 }
6059 }
6060
6061 // Otherwise, expand to a libcall.
6062 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
6063}
6064
6065static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
6066 SDValue LHS, SDValue RHS, SDValue Scale,
6067 SelectionDAG &DAG, const TargetLowering &TLI) {
6068 EVT VT = LHS.getValueType();
6069 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
6070 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
6071 LLVMContext &Ctx = *DAG.getContext();
6072
6073 // If the type is legal but the operation isn't, this node might survive all
6074 // the way to operation legalization. If we end up there and we do not have
6075 // the ability to widen the type (if VT*2 is not legal), we cannot expand the
6076 // node.
6077
6078 // Coax the legalizer into expanding the node during type legalization instead
6079 // by bumping the size by one bit. This will force it to Promote, enabling the
6080 // early expansion and avoiding the need to expand later.
6081
6082 // We don't have to do this if Scale is 0; that can always be expanded, unless
6083 // it's a saturating signed operation. Those can experience true integer
6084 // division overflow, a case which we must avoid.
6085
6086 // FIXME: We wouldn't have to do this (or any of the early
6087 // expansion/promotion) if it was possible to expand a libcall of an
6088 // illegal type during operation legalization. But it's not, so things
6089 // get a bit hacky.
6090 unsigned ScaleInt = Scale->getAsZExtVal();
6091 if ((ScaleInt > 0 || (Saturating && Signed)) &&
6092 (TLI.isTypeLegal(VT) ||
6093 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
6095 Opcode, VT, ScaleInt);
6096 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
6097 EVT PromVT;
6098 if (VT.isScalarInteger())
6099 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
6100 else if (VT.isVector()) {
6101 PromVT = VT.getVectorElementType();
6102 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
6103 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
6104 } else
6105 llvm_unreachable("Wrong VT for DIVFIX?");
6106 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
6107 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
6108 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
6109 // For saturating operations, we need to shift up the LHS to get the
6110 // proper saturation width, and then shift down again afterwards.
6111 if (Saturating)
6112 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
6113 DAG.getConstant(1, DL, ShiftTy));
6114 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
6115 if (Saturating)
6116 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
6117 DAG.getConstant(1, DL, ShiftTy));
6118 return DAG.getZExtOrTrunc(Res, DL, VT);
6119 }
6120 }
6121
6122 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
6123}
6124
6125// getUnderlyingArgRegs - Find underlying registers used for a truncated,
6126// bitcasted, or split argument. Returns a list of <Register, size in bits>
6127static void
6128getUnderlyingArgRegs(SmallVectorImpl<std::pair<Register, TypeSize>> &Regs,
6129 const SDValue &N) {
6130 switch (N.getOpcode()) {
6131 case ISD::CopyFromReg: {
6132 SDValue Op = N.getOperand(1);
6133 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
6134 Op.getValueType().getSizeInBits());
6135 return;
6136 }
6137 case ISD::BITCAST:
6138 case ISD::AssertZext:
6139 case ISD::AssertSext:
6140 case ISD::TRUNCATE:
6141 getUnderlyingArgRegs(Regs, N.getOperand(0));
6142 return;
6143 case ISD::BUILD_PAIR:
6144 case ISD::BUILD_VECTOR:
6146 for (SDValue Op : N->op_values())
6147 getUnderlyingArgRegs(Regs, Op);
6148 return;
6149 default:
6150 return;
6151 }
6152}
6153
6154/// If the DbgValueInst is a dbg_value of a function argument, create the
6155/// corresponding DBG_VALUE machine instruction for it now. At the end of
6156/// instruction selection, they will be inserted to the entry BB.
6157/// We don't currently support this for variadic dbg_values, as they shouldn't
6158/// appear for function arguments or in the prologue.
6159bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6160 const Value *V, DILocalVariable *Variable, DIExpression *Expr,
6161 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
6162 const Argument *Arg = dyn_cast<Argument>(V);
6163 if (!Arg)
6164 return false;
6165
6166 MachineFunction &MF = DAG.getMachineFunction();
6167 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6168
6169 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
6170 // we've been asked to pursue.
6171 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
6172 bool Indirect) {
6173 if (Reg.isVirtual() && MF.useDebugInstrRef()) {
6174 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
6175 // pointing at the VReg, which will be patched up later.
6176 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
6178 /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
6179 /* isKill */ false, /* isDead */ false,
6180 /* isUndef */ false, /* isEarlyClobber */ false,
6181 /* SubReg */ 0, /* isDebug */ true)});
6182
6183 auto *NewDIExpr = FragExpr;
6184 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
6185 // the DIExpression.
6186 if (Indirect)
6187 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6189 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6190 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6191 } else {
6192 // Create a completely standard DBG_VALUE.
6193 auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6194 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6195 }
6196 };
6197
6198 if (Kind == FuncArgumentDbgValueKind::Value) {
6199 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6200 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6201 // the entry block.
6202 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6203 if (!IsInEntryBlock)
6204 return false;
6205
6206 // ArgDbgValues are hoisted to the beginning of the entry block. So we
6207 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6208 // variable that also is a param.
6209 //
6210 // Although, if we are at the top of the entry block already, we can still
6211 // emit using ArgDbgValue. This might catch some situations when the
6212 // dbg.value refers to an argument that isn't used in the entry block, so
6213 // any CopyToReg node would be optimized out and the only way to express
6214 // this DBG_VALUE is by using the physical reg (or FI) as done in this
6215 // method. ArgDbgValues are hoisted to the beginning of the entry block. So
6216 // we should only emit as ArgDbgValue if the Variable is an argument to the
6217 // current function, and the dbg.value intrinsic is found in the entry
6218 // block.
6219 bool VariableIsFunctionInputArg = Variable->isParameter() &&
6220 !DL->getInlinedAt();
6221 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6222 if (!IsInPrologue && !VariableIsFunctionInputArg)
6223 return false;
6224
6225 // Here we assume that a function argument on IR level only can be used to
6226 // describe one input parameter on source level. If we for example have
6227 // source code like this
6228 //
6229 // struct A { long x, y; };
6230 // void foo(struct A a, long b) {
6231 // ...
6232 // b = a.x;
6233 // ...
6234 // }
6235 //
6236 // and IR like this
6237 //
6238 // define void @foo(i32 %a1, i32 %a2, i32 %b) {
6239 // entry:
6240 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6241 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6242 // call void @llvm.dbg.value(metadata i32 %b, "b",
6243 // ...
6244 // call void @llvm.dbg.value(metadata i32 %a1, "b"
6245 // ...
6246 //
6247 // then the last dbg.value is describing a parameter "b" using a value that
6248 // is an argument. But since we already has used %a1 to describe a parameter
6249 // we should not handle that last dbg.value here (that would result in an
6250 // incorrect hoisting of the DBG_VALUE to the function entry).
6251 // Notice that we allow one dbg.value per IR level argument, to accommodate
6252 // for the situation with fragments above.
6253 // If there is no node for the value being handled, we return true to skip
6254 // the normal generation of debug info, as it would kill existing debug
6255 // info for the parameter in case of duplicates.
6256 if (VariableIsFunctionInputArg) {
6257 unsigned ArgNo = Arg->getArgNo();
6258 if (ArgNo >= FuncInfo.DescribedArgs.size())
6259 FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6260 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6261 return !NodeMap[V].getNode();
6262 FuncInfo.DescribedArgs.set(ArgNo);
6263 }
6264 }
6265
6266 bool IsIndirect = false;
6267 std::optional<MachineOperand> Op;
6268 // Some arguments' frame index is recorded during argument lowering.
6269 int FI = FuncInfo.getArgumentFrameIndex(Arg);
6270 if (FI != std::numeric_limits<int>::max())
6272
6274 if (!Op && N.getNode()) {
6275 getUnderlyingArgRegs(ArgRegsAndSizes, N);
6276 Register Reg;
6277 if (ArgRegsAndSizes.size() == 1)
6278 Reg = ArgRegsAndSizes.front().first;
6279
6280 if (Reg && Reg.isVirtual()) {
6281 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6282 Register PR = RegInfo.getLiveInPhysReg(Reg);
6283 if (PR)
6284 Reg = PR;
6285 }
6286 if (Reg) {
6288 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6289 }
6290 }
6291
6292 if (!Op && N.getNode()) {
6293 // Check if frame index is available.
6294 SDValue LCandidate = peekThroughBitcasts(N);
6295 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6296 if (FrameIndexSDNode *FINode =
6297 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6298 Op = MachineOperand::CreateFI(FINode->getIndex());
6299 }
6300
6301 if (!Op) {
6302 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6303 auto splitMultiRegDbgValue =
6304 [&](ArrayRef<std::pair<Register, TypeSize>> SplitRegs) -> bool {
6305 unsigned Offset = 0;
6306 for (const auto &[Reg, RegSizeInBits] : SplitRegs) {
6307 // FIXME: Scalable sizes are not supported in fragment expressions.
6308 if (RegSizeInBits.isScalable())
6309 return false;
6310
6311 // If the expression is already a fragment, the current register
6312 // offset+size might extend beyond the fragment. In this case, only
6313 // the register bits that are inside the fragment are relevant.
6314 int RegFragmentSizeInBits = RegSizeInBits.getFixedValue();
6315 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6316 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6317 // The register is entirely outside the expression fragment,
6318 // so is irrelevant for debug info.
6319 if (Offset >= ExprFragmentSizeInBits)
6320 break;
6321 // The register is partially outside the expression fragment, only
6322 // the low bits within the fragment are relevant for debug info.
6323 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6324 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6325 }
6326 }
6327
6328 auto FragmentExpr = DIExpression::createFragmentExpression(
6329 Expr, Offset, RegFragmentSizeInBits);
6330 Offset += RegSizeInBits.getFixedValue();
6331 // If a valid fragment expression cannot be created, the variable's
6332 // correct value cannot be determined and so it is set as poison.
6333 if (!FragmentExpr) {
6334 SDDbgValue *SDV = DAG.getConstantDbgValue(
6335 Variable, Expr, PoisonValue::get(V->getType()), DL, SDNodeOrder);
6336 DAG.AddDbgValue(SDV, false);
6337 continue;
6338 }
6339 MachineInstr *NewMI = MakeVRegDbgValue(
6340 Reg, *FragmentExpr, Kind != FuncArgumentDbgValueKind::Value);
6341 FuncInfo.ArgDbgValues.push_back(NewMI);
6342 }
6343
6344 return true;
6345 };
6346
6347 // Check if ValueMap has reg number.
6349 VMI = FuncInfo.ValueMap.find(V);
6350 if (VMI != FuncInfo.ValueMap.end()) {
6351 const auto &TLI = DAG.getTargetLoweringInfo();
6352 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6353 V->getType(), std::nullopt);
6354 if (RFV.occupiesMultipleRegs())
6355 return splitMultiRegDbgValue(RFV.getRegsAndSizes());
6356
6357 Op = MachineOperand::CreateReg(VMI->second, false);
6358 IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6359 } else if (ArgRegsAndSizes.size() > 1) {
6360 // This was split due to the calling convention, and no virtual register
6361 // mapping exists for the value.
6362 return splitMultiRegDbgValue(ArgRegsAndSizes);
6363 }
6364 }
6365
6366 if (!Op)
6367 return false;
6368
6369 assert(Variable->isValidLocationForIntrinsic(DL) &&
6370 "Expected inlined-at fields to agree");
6371 MachineInstr *NewMI = nullptr;
6372
6373 if (Op->isReg())
6374 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6375 else
6376 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6377 Variable, Expr);
6378
6379 // Otherwise, use ArgDbgValues.
6380 FuncInfo.ArgDbgValues.push_back(NewMI);
6381 return true;
6382}
6383
6384/// Return the appropriate SDDbgValue based on N.
6385SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6386 DILocalVariable *Variable,
6387 DIExpression *Expr,
6388 const DebugLoc &dl,
6389 unsigned DbgSDNodeOrder) {
6390 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6391 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6392 // stack slot locations.
6393 //
6394 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6395 // debug values here after optimization:
6396 //
6397 // dbg.value(i32* %px, !"int *px", !DIExpression()), and
6398 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6399 //
6400 // Both describe the direct values of their associated variables.
6401 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6402 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6403 }
6404 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6405 /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6406}
6407
6408static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6409 switch (Intrinsic) {
6410 case Intrinsic::smul_fix:
6411 return ISD::SMULFIX;
6412 case Intrinsic::umul_fix:
6413 return ISD::UMULFIX;
6414 case Intrinsic::smul_fix_sat:
6415 return ISD::SMULFIXSAT;
6416 case Intrinsic::umul_fix_sat:
6417 return ISD::UMULFIXSAT;
6418 case Intrinsic::sdiv_fix:
6419 return ISD::SDIVFIX;
6420 case Intrinsic::udiv_fix:
6421 return ISD::UDIVFIX;
6422 case Intrinsic::sdiv_fix_sat:
6423 return ISD::SDIVFIXSAT;
6424 case Intrinsic::udiv_fix_sat:
6425 return ISD::UDIVFIXSAT;
6426 default:
6427 llvm_unreachable("Unhandled fixed point intrinsic");
6428 }
6429}
6430
6431/// Given a @llvm.call.preallocated.setup, return the corresponding
6432/// preallocated call.
6433static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6434 assert(cast<CallBase>(PreallocatedSetup)
6436 ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6437 "expected call_preallocated_setup Value");
6438 for (const auto *U : PreallocatedSetup->users()) {
6439 auto *UseCall = cast<CallBase>(U);
6440 const Function *Fn = UseCall->getCalledFunction();
6441 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6442 return UseCall;
6443 }
6444 }
6445 llvm_unreachable("expected corresponding call to preallocated setup/arg");
6446}
6447
6448/// If DI is a debug value with an EntryValue expression, lower it using the
6449/// corresponding physical register of the associated Argument value
6450/// (guaranteed to exist by the verifier).
6451bool SelectionDAGBuilder::visitEntryValueDbgValue(
6452 ArrayRef<const Value *> Values, DILocalVariable *Variable,
6453 DIExpression *Expr, DebugLoc DbgLoc) {
6454 if (!Expr->isEntryValue() || !hasSingleElement(Values))
6455 return false;
6456
6457 // These properties are guaranteed by the verifier.
6458 const Argument *Arg = cast<Argument>(Values[0]);
6459 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6460
6461 auto ArgIt = FuncInfo.ValueMap.find(Arg);
6462 if (ArgIt == FuncInfo.ValueMap.end()) {
6463 LLVM_DEBUG(
6464 dbgs() << "Dropping dbg.value: expression is entry_value but "
6465 "couldn't find an associated register for the Argument\n");
6466 return true;
6467 }
6468 Register ArgVReg = ArgIt->getSecond();
6469
6470 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6471 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6472 SDDbgValue *SDV = DAG.getVRegDbgValue(
6473 Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6474 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6475 return true;
6476 }
6477 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6478 "couldn't find a physical register\n");
6479 return true;
6480}
6481
6482/// Lower the call to the specified intrinsic function.
6483void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6484 unsigned Intrinsic) {
6485 SDLoc sdl = getCurSDLoc();
6486 switch (Intrinsic) {
6487 case Intrinsic::experimental_convergence_anchor:
6488 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6489 break;
6490 case Intrinsic::experimental_convergence_entry:
6491 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6492 break;
6493 case Intrinsic::experimental_convergence_loop: {
6494 auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6495 auto *Token = Bundle->Inputs[0].get();
6496 setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6497 getValue(Token)));
6498 break;
6499 }
6500 }
6501}
6502
6503void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6504 unsigned IntrinsicID) {
6505 // For now, we're only lowering an 'add' histogram.
6506 // We can add others later, e.g. saturating adds, min/max.
6507 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6508 "Tried to lower unsupported histogram type");
6509 SDLoc sdl = getCurSDLoc();
6510 Value *Ptr = I.getOperand(0);
6511 SDValue Inc = getValue(I.getOperand(1));
6512 SDValue Mask = getValue(I.getOperand(2));
6513
6514 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6515 DataLayout TargetDL = DAG.getDataLayout();
6516 EVT VT = Inc.getValueType();
6517 Align Alignment = DAG.getEVTAlign(VT);
6518
6519 const MDNode *Ranges = getRangeMetadata(I);
6520
6521 SDValue Root = DAG.getRoot();
6522 SDValue Base;
6523 SDValue Index;
6524 SDValue Scale;
6525 bool UniformBase = getUniformBase(Ptr, Base, Index, Scale, this,
6526 I.getParent(), VT.getScalarStoreSize());
6527
6528 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6529
6530 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6531 MachinePointerInfo(AS),
6533 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6534
6535 if (!UniformBase) {
6536 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6537 Index = getValue(Ptr);
6538 Scale =
6539 DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6540 }
6541
6542 EVT IdxVT = Index.getValueType();
6543 EVT EltTy = IdxVT.getVectorElementType();
6544 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6545 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
6546 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6547 }
6548
6549 SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6550
6551 SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6552 SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6553 Ops, MMO, ISD::SIGNED_SCALED);
6554
6555 setValue(&I, Histogram);
6556 DAG.setRoot(Histogram);
6557}
6558
6559void SelectionDAGBuilder::visitVectorExtractLastActive(const CallInst &I,
6560 unsigned Intrinsic) {
6561 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6562 "Tried lowering invalid vector extract last");
6563 SDLoc sdl = getCurSDLoc();
6564 const DataLayout &Layout = DAG.getDataLayout();
6565 SDValue Data = getValue(I.getOperand(0));
6566 SDValue Mask = getValue(I.getOperand(1));
6567
6568 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6569 EVT ResVT = TLI.getValueType(Layout, I.getType());
6570
6571 EVT ExtVT = TLI.getVectorIdxTy(Layout);
6572 SDValue Idx = DAG.getNode(ISD::VECTOR_FIND_LAST_ACTIVE, sdl, ExtVT, Mask);
6573 SDValue Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl, ResVT, Data, Idx);
6574
6575 Value *Default = I.getOperand(2);
6577 SDValue PassThru = getValue(Default);
6578 EVT BoolVT = Mask.getValueType().getScalarType();
6579 SDValue AnyActive = DAG.getNode(ISD::VECREDUCE_OR, sdl, BoolVT, Mask);
6580 Result = DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6581 }
6582
6583 setValue(&I, Result);
6584}
6585
6586/// Lower the call to the specified intrinsic function.
6587void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6588 unsigned Intrinsic) {
6589 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6590 SDLoc sdl = getCurSDLoc();
6591 DebugLoc dl = getCurDebugLoc();
6592 SDValue Res;
6593
6594 SDNodeFlags Flags;
6595 if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6596 Flags.copyFMF(*FPOp);
6597
6598 switch (Intrinsic) {
6599 default:
6600 // By default, turn this into a target intrinsic node.
6601 visitTargetIntrinsic(I, Intrinsic);
6602 return;
6603 case Intrinsic::vscale: {
6604 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6605 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6606 return;
6607 }
6608 case Intrinsic::vastart: visitVAStart(I); return;
6609 case Intrinsic::vaend: visitVAEnd(I); return;
6610 case Intrinsic::vacopy: visitVACopy(I); return;
6611 case Intrinsic::returnaddress:
6612 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6613 TLI.getValueType(DAG.getDataLayout(), I.getType()),
6614 getValue(I.getArgOperand(0))));
6615 return;
6616 case Intrinsic::addressofreturnaddress:
6617 setValue(&I,
6618 DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6619 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6620 return;
6621 case Intrinsic::sponentry:
6622 setValue(&I,
6623 DAG.getNode(ISD::SPONENTRY, sdl,
6624 TLI.getValueType(DAG.getDataLayout(), I.getType())));
6625 return;
6626 case Intrinsic::frameaddress:
6627 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6628 TLI.getFrameIndexTy(DAG.getDataLayout()),
6629 getValue(I.getArgOperand(0))));
6630 return;
6631 case Intrinsic::read_volatile_register:
6632 case Intrinsic::read_register: {
6633 Value *Reg = I.getArgOperand(0);
6634 SDValue Chain = getRoot();
6636 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6637 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6638 Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6639 DAG.getVTList(VT, MVT::Other), Chain, RegName);
6640 setValue(&I, Res);
6641 DAG.setRoot(Res.getValue(1));
6642 return;
6643 }
6644 case Intrinsic::write_register: {
6645 Value *Reg = I.getArgOperand(0);
6646 Value *RegValue = I.getArgOperand(1);
6647 SDValue Chain = getRoot();
6649 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6650 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6651 RegName, getValue(RegValue)));
6652 return;
6653 }
6654 case Intrinsic::memcpy:
6655 case Intrinsic::memcpy_inline: {
6656 const auto &MCI = cast<MemCpyInst>(I);
6657 SDValue Dst = getValue(I.getArgOperand(0));
6658 SDValue Src = getValue(I.getArgOperand(1));
6659 SDValue Size = getValue(I.getArgOperand(2));
6660 assert((!MCI.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6661 "memcpy_inline needs constant size");
6662 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6663 Align DstAlign = MCI.getDestAlign().valueOrOne();
6664 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6665 Align Alignment = std::min(DstAlign, SrcAlign);
6666 bool isVol = MCI.isVolatile();
6667 // FIXME: Support passing different dest/src alignments to the memcpy DAG
6668 // node.
6669 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6670 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol,
6671 MCI.isForceInlined(), &I, std::nullopt,
6672 MachinePointerInfo(I.getArgOperand(0)),
6673 MachinePointerInfo(I.getArgOperand(1)),
6674 I.getAAMetadata(), BatchAA);
6675 updateDAGForMaybeTailCall(MC);
6676 return;
6677 }
6678 case Intrinsic::memset:
6679 case Intrinsic::memset_inline: {
6680 const auto &MSII = cast<MemSetInst>(I);
6681 SDValue Dst = getValue(I.getArgOperand(0));
6682 SDValue Value = getValue(I.getArgOperand(1));
6683 SDValue Size = getValue(I.getArgOperand(2));
6684 assert((!MSII.isForceInlined() || isa<ConstantSDNode>(Size)) &&
6685 "memset_inline needs constant size");
6686 // @llvm.memset defines 0 and 1 to both mean no alignment.
6687 Align DstAlign = MSII.getDestAlign().valueOrOne();
6688 bool isVol = MSII.isVolatile();
6689 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6690 SDValue MC = DAG.getMemset(
6691 Root, sdl, Dst, Value, Size, DstAlign, isVol, MSII.isForceInlined(),
6692 &I, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6693 updateDAGForMaybeTailCall(MC);
6694 return;
6695 }
6696 case Intrinsic::memmove: {
6697 const auto &MMI = cast<MemMoveInst>(I);
6698 SDValue Op1 = getValue(I.getArgOperand(0));
6699 SDValue Op2 = getValue(I.getArgOperand(1));
6700 SDValue Op3 = getValue(I.getArgOperand(2));
6701 // @llvm.memmove defines 0 and 1 to both mean no alignment.
6702 Align DstAlign = MMI.getDestAlign().valueOrOne();
6703 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6704 Align Alignment = std::min(DstAlign, SrcAlign);
6705 bool isVol = MMI.isVolatile();
6706 // FIXME: Support passing different dest/src alignments to the memmove DAG
6707 // node.
6708 SDValue Root = isVol ? getRoot() : getMemoryRoot();
6709 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, &I,
6710 /* OverrideTailCall */ std::nullopt,
6711 MachinePointerInfo(I.getArgOperand(0)),
6712 MachinePointerInfo(I.getArgOperand(1)),
6713 I.getAAMetadata(), BatchAA);
6714 updateDAGForMaybeTailCall(MM);
6715 return;
6716 }
6717 case Intrinsic::memcpy_element_unordered_atomic: {
6718 auto &MI = cast<AnyMemCpyInst>(I);
6719 SDValue Dst = getValue(MI.getRawDest());
6720 SDValue Src = getValue(MI.getRawSource());
6721 SDValue Length = getValue(MI.getLength());
6722
6723 Type *LengthTy = MI.getLength()->getType();
6724 unsigned ElemSz = MI.getElementSizeInBytes();
6725 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6726 SDValue MC =
6727 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6728 isTC, MachinePointerInfo(MI.getRawDest()),
6729 MachinePointerInfo(MI.getRawSource()));
6730 updateDAGForMaybeTailCall(MC);
6731 return;
6732 }
6733 case Intrinsic::memmove_element_unordered_atomic: {
6734 auto &MI = cast<AnyMemMoveInst>(I);
6735 SDValue Dst = getValue(MI.getRawDest());
6736 SDValue Src = getValue(MI.getRawSource());
6737 SDValue Length = getValue(MI.getLength());
6738
6739 Type *LengthTy = MI.getLength()->getType();
6740 unsigned ElemSz = MI.getElementSizeInBytes();
6741 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6742 SDValue MC =
6743 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6744 isTC, MachinePointerInfo(MI.getRawDest()),
6745 MachinePointerInfo(MI.getRawSource()));
6746 updateDAGForMaybeTailCall(MC);
6747 return;
6748 }
6749 case Intrinsic::memset_element_unordered_atomic: {
6750 auto &MI = cast<AnyMemSetInst>(I);
6751 SDValue Dst = getValue(MI.getRawDest());
6752 SDValue Val = getValue(MI.getValue());
6753 SDValue Length = getValue(MI.getLength());
6754
6755 Type *LengthTy = MI.getLength()->getType();
6756 unsigned ElemSz = MI.getElementSizeInBytes();
6757 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6758 SDValue MC =
6759 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6760 isTC, MachinePointerInfo(MI.getRawDest()));
6761 updateDAGForMaybeTailCall(MC);
6762 return;
6763 }
6764 case Intrinsic::call_preallocated_setup: {
6765 const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6766 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6767 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6768 getRoot(), SrcValue);
6769 setValue(&I, Res);
6770 DAG.setRoot(Res);
6771 return;
6772 }
6773 case Intrinsic::call_preallocated_arg: {
6774 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6775 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6776 SDValue Ops[3];
6777 Ops[0] = getRoot();
6778 Ops[1] = SrcValue;
6779 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6780 MVT::i32); // arg index
6781 SDValue Res = DAG.getNode(
6783 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6784 setValue(&I, Res);
6785 DAG.setRoot(Res.getValue(1));
6786 return;
6787 }
6788
6789 case Intrinsic::eh_typeid_for: {
6790 // Find the type id for the given typeinfo.
6791 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6792 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6793 Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6794 setValue(&I, Res);
6795 return;
6796 }
6797
6798 case Intrinsic::eh_return_i32:
6799 case Intrinsic::eh_return_i64:
6800 DAG.getMachineFunction().setCallsEHReturn(true);
6801 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6802 MVT::Other,
6804 getValue(I.getArgOperand(0)),
6805 getValue(I.getArgOperand(1))));
6806 return;
6807 case Intrinsic::eh_unwind_init:
6808 DAG.getMachineFunction().setCallsUnwindInit(true);
6809 return;
6810 case Intrinsic::eh_dwarf_cfa:
6811 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6812 TLI.getPointerTy(DAG.getDataLayout()),
6813 getValue(I.getArgOperand(0))));
6814 return;
6815 case Intrinsic::eh_sjlj_callsite: {
6816 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6817 assert(FuncInfo.getCurrentCallSite() == 0 && "Overlapping call sites!");
6818
6819 FuncInfo.setCurrentCallSite(CI->getZExtValue());
6820 return;
6821 }
6822 case Intrinsic::eh_sjlj_functioncontext: {
6823 // Get and store the index of the function context.
6824 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6825 AllocaInst *FnCtx =
6826 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6827 int FI = FuncInfo.StaticAllocaMap[FnCtx];
6829 return;
6830 }
6831 case Intrinsic::eh_sjlj_setjmp: {
6832 SDValue Ops[2];
6833 Ops[0] = getRoot();
6834 Ops[1] = getValue(I.getArgOperand(0));
6835 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6836 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6837 setValue(&I, Op.getValue(0));
6838 DAG.setRoot(Op.getValue(1));
6839 return;
6840 }
6841 case Intrinsic::eh_sjlj_longjmp:
6842 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6843 getRoot(), getValue(I.getArgOperand(0))));
6844 return;
6845 case Intrinsic::eh_sjlj_setup_dispatch:
6846 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6847 getRoot()));
6848 return;
6849 case Intrinsic::masked_gather:
6850 visitMaskedGather(I);
6851 return;
6852 case Intrinsic::masked_load:
6853 visitMaskedLoad(I);
6854 return;
6855 case Intrinsic::masked_scatter:
6856 visitMaskedScatter(I);
6857 return;
6858 case Intrinsic::masked_store:
6859 visitMaskedStore(I);
6860 return;
6861 case Intrinsic::masked_expandload:
6862 visitMaskedLoad(I, true /* IsExpanding */);
6863 return;
6864 case Intrinsic::masked_compressstore:
6865 visitMaskedStore(I, true /* IsCompressing */);
6866 return;
6867 case Intrinsic::powi:
6868 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6869 getValue(I.getArgOperand(1)), DAG));
6870 return;
6871 case Intrinsic::log:
6872 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6873 return;
6874 case Intrinsic::log2:
6875 setValue(&I,
6876 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6877 return;
6878 case Intrinsic::log10:
6879 setValue(&I,
6880 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6881 return;
6882 case Intrinsic::exp:
6883 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6884 return;
6885 case Intrinsic::exp2:
6886 setValue(&I,
6887 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6888 return;
6889 case Intrinsic::pow:
6890 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6891 getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6892 return;
6893 case Intrinsic::sqrt:
6894 case Intrinsic::fabs:
6895 case Intrinsic::sin:
6896 case Intrinsic::cos:
6897 case Intrinsic::tan:
6898 case Intrinsic::asin:
6899 case Intrinsic::acos:
6900 case Intrinsic::atan:
6901 case Intrinsic::sinh:
6902 case Intrinsic::cosh:
6903 case Intrinsic::tanh:
6904 case Intrinsic::exp10:
6905 case Intrinsic::floor:
6906 case Intrinsic::ceil:
6907 case Intrinsic::trunc:
6908 case Intrinsic::rint:
6909 case Intrinsic::nearbyint:
6910 case Intrinsic::round:
6911 case Intrinsic::roundeven:
6912 case Intrinsic::canonicalize: {
6913 unsigned Opcode;
6914 // clang-format off
6915 switch (Intrinsic) {
6916 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6917 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
6918 case Intrinsic::fabs: Opcode = ISD::FABS; break;
6919 case Intrinsic::sin: Opcode = ISD::FSIN; break;
6920 case Intrinsic::cos: Opcode = ISD::FCOS; break;
6921 case Intrinsic::tan: Opcode = ISD::FTAN; break;
6922 case Intrinsic::asin: Opcode = ISD::FASIN; break;
6923 case Intrinsic::acos: Opcode = ISD::FACOS; break;
6924 case Intrinsic::atan: Opcode = ISD::FATAN; break;
6925 case Intrinsic::sinh: Opcode = ISD::FSINH; break;
6926 case Intrinsic::cosh: Opcode = ISD::FCOSH; break;
6927 case Intrinsic::tanh: Opcode = ISD::FTANH; break;
6928 case Intrinsic::exp10: Opcode = ISD::FEXP10; break;
6929 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
6930 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
6931 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
6932 case Intrinsic::rint: Opcode = ISD::FRINT; break;
6933 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6934 case Intrinsic::round: Opcode = ISD::FROUND; break;
6935 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6936 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6937 }
6938 // clang-format on
6939
6940 setValue(&I, DAG.getNode(Opcode, sdl,
6941 getValue(I.getArgOperand(0)).getValueType(),
6942 getValue(I.getArgOperand(0)), Flags));
6943 return;
6944 }
6945 case Intrinsic::atan2:
6946 setValue(&I, DAG.getNode(ISD::FATAN2, sdl,
6947 getValue(I.getArgOperand(0)).getValueType(),
6948 getValue(I.getArgOperand(0)),
6949 getValue(I.getArgOperand(1)), Flags));
6950 return;
6951 case Intrinsic::lround:
6952 case Intrinsic::llround:
6953 case Intrinsic::lrint:
6954 case Intrinsic::llrint: {
6955 unsigned Opcode;
6956 // clang-format off
6957 switch (Intrinsic) {
6958 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
6959 case Intrinsic::lround: Opcode = ISD::LROUND; break;
6960 case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6961 case Intrinsic::lrint: Opcode = ISD::LRINT; break;
6962 case Intrinsic::llrint: Opcode = ISD::LLRINT; break;
6963 }
6964 // clang-format on
6965
6966 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6967 setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6968 getValue(I.getArgOperand(0))));
6969 return;
6970 }
6971 case Intrinsic::minnum:
6972 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6973 getValue(I.getArgOperand(0)).getValueType(),
6974 getValue(I.getArgOperand(0)),
6975 getValue(I.getArgOperand(1)), Flags));
6976 return;
6977 case Intrinsic::maxnum:
6978 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6979 getValue(I.getArgOperand(0)).getValueType(),
6980 getValue(I.getArgOperand(0)),
6981 getValue(I.getArgOperand(1)), Flags));
6982 return;
6983 case Intrinsic::minimum:
6984 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
6985 getValue(I.getArgOperand(0)).getValueType(),
6986 getValue(I.getArgOperand(0)),
6987 getValue(I.getArgOperand(1)), Flags));
6988 return;
6989 case Intrinsic::maximum:
6990 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
6991 getValue(I.getArgOperand(0)).getValueType(),
6992 getValue(I.getArgOperand(0)),
6993 getValue(I.getArgOperand(1)), Flags));
6994 return;
6995 case Intrinsic::minimumnum:
6996 setValue(&I, DAG.getNode(ISD::FMINIMUMNUM, sdl,
6997 getValue(I.getArgOperand(0)).getValueType(),
6998 getValue(I.getArgOperand(0)),
6999 getValue(I.getArgOperand(1)), Flags));
7000 return;
7001 case Intrinsic::maximumnum:
7002 setValue(&I, DAG.getNode(ISD::FMAXIMUMNUM, sdl,
7003 getValue(I.getArgOperand(0)).getValueType(),
7004 getValue(I.getArgOperand(0)),
7005 getValue(I.getArgOperand(1)), Flags));
7006 return;
7007 case Intrinsic::copysign:
7008 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
7009 getValue(I.getArgOperand(0)).getValueType(),
7010 getValue(I.getArgOperand(0)),
7011 getValue(I.getArgOperand(1)), Flags));
7012 return;
7013 case Intrinsic::ldexp:
7014 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
7015 getValue(I.getArgOperand(0)).getValueType(),
7016 getValue(I.getArgOperand(0)),
7017 getValue(I.getArgOperand(1)), Flags));
7018 return;
7019 case Intrinsic::modf:
7020 case Intrinsic::sincos:
7021 case Intrinsic::sincospi:
7022 case Intrinsic::frexp: {
7023 unsigned Opcode;
7024 switch (Intrinsic) {
7025 default:
7026 llvm_unreachable("unexpected intrinsic");
7027 case Intrinsic::sincos:
7028 Opcode = ISD::FSINCOS;
7029 break;
7030 case Intrinsic::sincospi:
7031 Opcode = ISD::FSINCOSPI;
7032 break;
7033 case Intrinsic::modf:
7034 Opcode = ISD::FMODF;
7035 break;
7036 case Intrinsic::frexp:
7037 Opcode = ISD::FFREXP;
7038 break;
7039 }
7040 SmallVector<EVT, 2> ValueVTs;
7041 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
7042 SDVTList VTs = DAG.getVTList(ValueVTs);
7043 setValue(
7044 &I, DAG.getNode(Opcode, sdl, VTs, getValue(I.getArgOperand(0)), Flags));
7045 return;
7046 }
7047 case Intrinsic::arithmetic_fence: {
7048 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
7049 getValue(I.getArgOperand(0)).getValueType(),
7050 getValue(I.getArgOperand(0)), Flags));
7051 return;
7052 }
7053 case Intrinsic::fma:
7054 setValue(&I, DAG.getNode(
7055 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
7056 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
7057 getValue(I.getArgOperand(2)), Flags));
7058 return;
7059#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
7060 case Intrinsic::INTRINSIC:
7061#include "llvm/IR/ConstrainedOps.def"
7062 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
7063 return;
7064#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
7065#include "llvm/IR/VPIntrinsics.def"
7066 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
7067 return;
7068 case Intrinsic::fptrunc_round: {
7069 // Get the last argument, the metadata and convert it to an integer in the
7070 // call
7071 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
7072 std::optional<RoundingMode> RoundMode =
7073 convertStrToRoundingMode(cast<MDString>(MD)->getString());
7074
7075 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7076
7077 // Propagate fast-math-flags from IR to node(s).
7078 SDNodeFlags Flags;
7079 Flags.copyFMF(*cast<FPMathOperator>(&I));
7080 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
7081
7083 Result = DAG.getNode(
7084 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
7085 DAG.getTargetConstant((int)*RoundMode, sdl, MVT::i32));
7086 setValue(&I, Result);
7087
7088 return;
7089 }
7090 case Intrinsic::fmuladd: {
7091 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7092 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
7093 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
7094 setValue(&I, DAG.getNode(ISD::FMA, sdl,
7095 getValue(I.getArgOperand(0)).getValueType(),
7096 getValue(I.getArgOperand(0)),
7097 getValue(I.getArgOperand(1)),
7098 getValue(I.getArgOperand(2)), Flags));
7099 } else if (TLI.isOperationLegalOrCustom(ISD::FMULADD, VT)) {
7100 // TODO: Support splitting the vector.
7101 setValue(&I, DAG.getNode(ISD::FMULADD, sdl,
7102 getValue(I.getArgOperand(0)).getValueType(),
7103 getValue(I.getArgOperand(0)),
7104 getValue(I.getArgOperand(1)),
7105 getValue(I.getArgOperand(2)), Flags));
7106 } else {
7107 // TODO: Intrinsic calls should have fast-math-flags.
7108 SDValue Mul = DAG.getNode(
7109 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
7110 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
7111 SDValue Add = DAG.getNode(ISD::FADD, sdl,
7112 getValue(I.getArgOperand(0)).getValueType(),
7113 Mul, getValue(I.getArgOperand(2)), Flags);
7114 setValue(&I, Add);
7115 }
7116 return;
7117 }
7118 case Intrinsic::convert_to_fp16:
7119 setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16,
7120 DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16,
7121 getValue(I.getArgOperand(0)),
7122 DAG.getTargetConstant(0, sdl,
7123 MVT::i32))));
7124 return;
7125 case Intrinsic::convert_from_fp16:
7126 setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl,
7127 TLI.getValueType(DAG.getDataLayout(), I.getType()),
7128 DAG.getNode(ISD::BITCAST, sdl, MVT::f16,
7129 getValue(I.getArgOperand(0)))));
7130 return;
7131 case Intrinsic::fptosi_sat: {
7132 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7133 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
7134 getValue(I.getArgOperand(0)),
7135 DAG.getValueType(VT.getScalarType())));
7136 return;
7137 }
7138 case Intrinsic::fptoui_sat: {
7139 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7140 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
7141 getValue(I.getArgOperand(0)),
7142 DAG.getValueType(VT.getScalarType())));
7143 return;
7144 }
7145 case Intrinsic::set_rounding:
7146 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
7147 {getRoot(), getValue(I.getArgOperand(0))});
7148 setValue(&I, Res);
7149 DAG.setRoot(Res.getValue(0));
7150 return;
7151 case Intrinsic::is_fpclass: {
7152 const DataLayout DLayout = DAG.getDataLayout();
7153 EVT DestVT = TLI.getValueType(DLayout, I.getType());
7154 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
7155 FPClassTest Test = static_cast<FPClassTest>(
7156 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
7157 MachineFunction &MF = DAG.getMachineFunction();
7158 const Function &F = MF.getFunction();
7159 SDValue Op = getValue(I.getArgOperand(0));
7160 SDNodeFlags Flags;
7161 Flags.setNoFPExcept(
7162 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7163 // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7164 // expansion can use illegal types. Making expansion early allows
7165 // legalizing these types prior to selection.
7166 if (!TLI.isOperationLegal(ISD::IS_FPCLASS, ArgVT) &&
7167 !TLI.isOperationCustom(ISD::IS_FPCLASS, ArgVT)) {
7168 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7169 setValue(&I, Result);
7170 return;
7171 }
7172
7173 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7174 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7175 setValue(&I, V);
7176 return;
7177 }
7178 case Intrinsic::get_fpenv: {
7179 const DataLayout DLayout = DAG.getDataLayout();
7180 EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7181 Align TempAlign = DAG.getEVTAlign(EnvVT);
7182 SDValue Chain = getRoot();
7183 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7184 // and temporary storage in stack.
7185 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7186 Res = DAG.getNode(
7187 ISD::GET_FPENV, sdl,
7188 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7189 MVT::Other),
7190 Chain);
7191 } else {
7192 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7193 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7194 auto MPI =
7195 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7196 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7198 TempAlign);
7199 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7200 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7201 }
7202 setValue(&I, Res);
7203 DAG.setRoot(Res.getValue(1));
7204 return;
7205 }
7206 case Intrinsic::set_fpenv: {
7207 const DataLayout DLayout = DAG.getDataLayout();
7208 SDValue Env = getValue(I.getArgOperand(0));
7209 EVT EnvVT = Env.getValueType();
7210 Align TempAlign = DAG.getEVTAlign(EnvVT);
7211 SDValue Chain = getRoot();
7212 // If SET_FPENV is custom or legal, use it. Otherwise use loading
7213 // environment from memory.
7214 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7215 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7216 } else {
7217 // Allocate space in stack, copy environment bits into it and use this
7218 // memory in SET_FPENV_MEM.
7219 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7220 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7221 auto MPI =
7222 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7223 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7225 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7227 TempAlign);
7228 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7229 }
7230 DAG.setRoot(Chain);
7231 return;
7232 }
7233 case Intrinsic::reset_fpenv:
7234 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7235 return;
7236 case Intrinsic::get_fpmode:
7237 Res = DAG.getNode(
7238 ISD::GET_FPMODE, sdl,
7239 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7240 MVT::Other),
7241 DAG.getRoot());
7242 setValue(&I, Res);
7243 DAG.setRoot(Res.getValue(1));
7244 return;
7245 case Intrinsic::set_fpmode:
7246 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7247 getValue(I.getArgOperand(0)));
7248 DAG.setRoot(Res);
7249 return;
7250 case Intrinsic::reset_fpmode: {
7251 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7252 DAG.setRoot(Res);
7253 return;
7254 }
7255 case Intrinsic::pcmarker: {
7256 SDValue Tmp = getValue(I.getArgOperand(0));
7257 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7258 return;
7259 }
7260 case Intrinsic::readcyclecounter: {
7261 SDValue Op = getRoot();
7262 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7263 DAG.getVTList(MVT::i64, MVT::Other), Op);
7264 setValue(&I, Res);
7265 DAG.setRoot(Res.getValue(1));
7266 return;
7267 }
7268 case Intrinsic::readsteadycounter: {
7269 SDValue Op = getRoot();
7270 Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7271 DAG.getVTList(MVT::i64, MVT::Other), Op);
7272 setValue(&I, Res);
7273 DAG.setRoot(Res.getValue(1));
7274 return;
7275 }
7276 case Intrinsic::bitreverse:
7277 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7278 getValue(I.getArgOperand(0)).getValueType(),
7279 getValue(I.getArgOperand(0))));
7280 return;
7281 case Intrinsic::bswap:
7282 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7283 getValue(I.getArgOperand(0)).getValueType(),
7284 getValue(I.getArgOperand(0))));
7285 return;
7286 case Intrinsic::cttz: {
7287 SDValue Arg = getValue(I.getArgOperand(0));
7288 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7289 EVT Ty = Arg.getValueType();
7290 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
7291 sdl, Ty, Arg));
7292 return;
7293 }
7294 case Intrinsic::ctlz: {
7295 SDValue Arg = getValue(I.getArgOperand(0));
7296 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7297 EVT Ty = Arg.getValueType();
7298 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
7299 sdl, Ty, Arg));
7300 return;
7301 }
7302 case Intrinsic::ctpop: {
7303 SDValue Arg = getValue(I.getArgOperand(0));
7304 EVT Ty = Arg.getValueType();
7305 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7306 return;
7307 }
7308 case Intrinsic::fshl:
7309 case Intrinsic::fshr: {
7310 bool IsFSHL = Intrinsic == Intrinsic::fshl;
7311 SDValue X = getValue(I.getArgOperand(0));
7312 SDValue Y = getValue(I.getArgOperand(1));
7313 SDValue Z = getValue(I.getArgOperand(2));
7314 EVT VT = X.getValueType();
7315
7316 if (X == Y) {
7317 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7318 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7319 } else {
7320 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7321 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7322 }
7323 return;
7324 }
7325 case Intrinsic::clmul: {
7326 SDValue X = getValue(I.getArgOperand(0));
7327 SDValue Y = getValue(I.getArgOperand(1));
7328 setValue(&I, DAG.getNode(ISD::CLMUL, sdl, X.getValueType(), X, Y));
7329 return;
7330 }
7331 case Intrinsic::sadd_sat: {
7332 SDValue Op1 = getValue(I.getArgOperand(0));
7333 SDValue Op2 = getValue(I.getArgOperand(1));
7334 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7335 return;
7336 }
7337 case Intrinsic::uadd_sat: {
7338 SDValue Op1 = getValue(I.getArgOperand(0));
7339 SDValue Op2 = getValue(I.getArgOperand(1));
7340 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7341 return;
7342 }
7343 case Intrinsic::ssub_sat: {
7344 SDValue Op1 = getValue(I.getArgOperand(0));
7345 SDValue Op2 = getValue(I.getArgOperand(1));
7346 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7347 return;
7348 }
7349 case Intrinsic::usub_sat: {
7350 SDValue Op1 = getValue(I.getArgOperand(0));
7351 SDValue Op2 = getValue(I.getArgOperand(1));
7352 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7353 return;
7354 }
7355 case Intrinsic::sshl_sat:
7356 case Intrinsic::ushl_sat: {
7357 SDValue Op1 = getValue(I.getArgOperand(0));
7358 SDValue Op2 = getValue(I.getArgOperand(1));
7359
7360 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
7361 Op1.getValueType(), DAG.getDataLayout());
7362
7363 // Coerce the shift amount to the right type if we can. This exposes the
7364 // truncate or zext to optimization early.
7365 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
7366 assert(ShiftTy.getSizeInBits() >=
7368 "Unexpected shift type");
7369 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
7370 }
7371
7372 unsigned Opc =
7373 Intrinsic == Intrinsic::sshl_sat ? ISD::SSHLSAT : ISD::USHLSAT;
7374 setValue(&I, DAG.getNode(Opc, sdl, Op1.getValueType(), Op1, Op2));
7375 return;
7376 }
7377 case Intrinsic::smul_fix:
7378 case Intrinsic::umul_fix:
7379 case Intrinsic::smul_fix_sat:
7380 case Intrinsic::umul_fix_sat: {
7381 SDValue Op1 = getValue(I.getArgOperand(0));
7382 SDValue Op2 = getValue(I.getArgOperand(1));
7383 SDValue Op3 = getValue(I.getArgOperand(2));
7384 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7385 Op1.getValueType(), Op1, Op2, Op3));
7386 return;
7387 }
7388 case Intrinsic::sdiv_fix:
7389 case Intrinsic::udiv_fix:
7390 case Intrinsic::sdiv_fix_sat:
7391 case Intrinsic::udiv_fix_sat: {
7392 SDValue Op1 = getValue(I.getArgOperand(0));
7393 SDValue Op2 = getValue(I.getArgOperand(1));
7394 SDValue Op3 = getValue(I.getArgOperand(2));
7396 Op1, Op2, Op3, DAG, TLI));
7397 return;
7398 }
7399 case Intrinsic::smax: {
7400 SDValue Op1 = getValue(I.getArgOperand(0));
7401 SDValue Op2 = getValue(I.getArgOperand(1));
7402 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7403 return;
7404 }
7405 case Intrinsic::smin: {
7406 SDValue Op1 = getValue(I.getArgOperand(0));
7407 SDValue Op2 = getValue(I.getArgOperand(1));
7408 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7409 return;
7410 }
7411 case Intrinsic::umax: {
7412 SDValue Op1 = getValue(I.getArgOperand(0));
7413 SDValue Op2 = getValue(I.getArgOperand(1));
7414 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7415 return;
7416 }
7417 case Intrinsic::umin: {
7418 SDValue Op1 = getValue(I.getArgOperand(0));
7419 SDValue Op2 = getValue(I.getArgOperand(1));
7420 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7421 return;
7422 }
7423 case Intrinsic::abs: {
7424 // TODO: Preserve "int min is poison" arg in SDAG?
7425 SDValue Op1 = getValue(I.getArgOperand(0));
7426 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7427 return;
7428 }
7429 case Intrinsic::scmp: {
7430 SDValue Op1 = getValue(I.getArgOperand(0));
7431 SDValue Op2 = getValue(I.getArgOperand(1));
7432 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7433 setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7434 break;
7435 }
7436 case Intrinsic::ucmp: {
7437 SDValue Op1 = getValue(I.getArgOperand(0));
7438 SDValue Op2 = getValue(I.getArgOperand(1));
7439 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7440 setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7441 break;
7442 }
7443 case Intrinsic::stackaddress:
7444 case Intrinsic::stacksave: {
7445 unsigned SDOpcode = Intrinsic == Intrinsic::stackaddress ? ISD::STACKADDRESS
7447 SDValue Op = getRoot();
7448 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7449 Res = DAG.getNode(SDOpcode, sdl, DAG.getVTList(VT, MVT::Other), Op);
7450 setValue(&I, Res);
7451 DAG.setRoot(Res.getValue(1));
7452 return;
7453 }
7454 case Intrinsic::stackrestore:
7455 Res = getValue(I.getArgOperand(0));
7456 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7457 return;
7458 case Intrinsic::get_dynamic_area_offset: {
7459 SDValue Op = getRoot();
7460 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7461 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7462 Op);
7463 DAG.setRoot(Op);
7464 setValue(&I, Res);
7465 return;
7466 }
7467 case Intrinsic::stackguard: {
7468 MachineFunction &MF = DAG.getMachineFunction();
7469 const Module &M = *MF.getFunction().getParent();
7470 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7471 SDValue Chain = getRoot();
7472 if (TLI.useLoadStackGuardNode(M)) {
7473 Res = getLoadStackGuard(DAG, sdl, Chain);
7474 Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7475 } else {
7476 const Value *Global = TLI.getSDagStackGuard(M, DAG.getLibcalls());
7477 if (!Global) {
7478 LLVMContext &Ctx = *DAG.getContext();
7479 Ctx.diagnose(DiagnosticInfoGeneric("unable to lower stackguard"));
7480 setValue(&I, DAG.getPOISON(PtrTy));
7481 return;
7482 }
7483
7484 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7485 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7486 MachinePointerInfo(Global, 0), Align,
7488 }
7489 if (TLI.useStackGuardXorFP())
7490 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7491 DAG.setRoot(Chain);
7492 setValue(&I, Res);
7493 return;
7494 }
7495 case Intrinsic::stackprotector: {
7496 // Emit code into the DAG to store the stack guard onto the stack.
7497 MachineFunction &MF = DAG.getMachineFunction();
7498 MachineFrameInfo &MFI = MF.getFrameInfo();
7499 const Module &M = *MF.getFunction().getParent();
7500 SDValue Src, Chain = getRoot();
7501
7502 if (TLI.useLoadStackGuardNode(M))
7503 Src = getLoadStackGuard(DAG, sdl, Chain);
7504 else
7505 Src = getValue(I.getArgOperand(0)); // The guard's value.
7506
7507 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7508
7509 int FI = FuncInfo.StaticAllocaMap[Slot];
7510 MFI.setStackProtectorIndex(FI);
7511 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7512
7513 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7514
7515 // Store the stack protector onto the stack.
7516 Res = DAG.getStore(
7517 Chain, sdl, Src, FIN,
7518 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7519 MaybeAlign(), MachineMemOperand::MOVolatile);
7520 setValue(&I, Res);
7521 DAG.setRoot(Res);
7522 return;
7523 }
7524 case Intrinsic::objectsize:
7525 llvm_unreachable("llvm.objectsize.* should have been lowered already");
7526
7527 case Intrinsic::is_constant:
7528 llvm_unreachable("llvm.is.constant.* should have been lowered already");
7529
7530 case Intrinsic::annotation:
7531 case Intrinsic::ptr_annotation:
7532 case Intrinsic::launder_invariant_group:
7533 case Intrinsic::strip_invariant_group:
7534 // Drop the intrinsic, but forward the value
7535 setValue(&I, getValue(I.getOperand(0)));
7536 return;
7537
7538 case Intrinsic::type_test:
7539 case Intrinsic::public_type_test:
7540 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7541 return;
7542
7543 case Intrinsic::assume:
7544 case Intrinsic::experimental_noalias_scope_decl:
7545 case Intrinsic::var_annotation:
7546 case Intrinsic::sideeffect:
7547 // Discard annotate attributes, noalias scope declarations, assumptions, and
7548 // artificial side-effects.
7549 return;
7550
7551 case Intrinsic::codeview_annotation: {
7552 // Emit a label associated with this metadata.
7553 MachineFunction &MF = DAG.getMachineFunction();
7554 MCSymbol *Label = MF.getContext().createTempSymbol("annotation", true);
7555 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7556 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7557 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7558 DAG.setRoot(Res);
7559 return;
7560 }
7561
7562 case Intrinsic::init_trampoline: {
7563 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7564
7565 SDValue Ops[6];
7566 Ops[0] = getRoot();
7567 Ops[1] = getValue(I.getArgOperand(0));
7568 Ops[2] = getValue(I.getArgOperand(1));
7569 Ops[3] = getValue(I.getArgOperand(2));
7570 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7571 Ops[5] = DAG.getSrcValue(F);
7572
7573 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7574
7575 DAG.setRoot(Res);
7576 return;
7577 }
7578 case Intrinsic::adjust_trampoline:
7579 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7580 TLI.getPointerTy(DAG.getDataLayout()),
7581 getValue(I.getArgOperand(0))));
7582 return;
7583 case Intrinsic::gcroot: {
7584 assert(DAG.getMachineFunction().getFunction().hasGC() &&
7585 "only valid in functions with gc specified, enforced by Verifier");
7586 assert(GFI && "implied by previous");
7587 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7588 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7589
7590 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7591 GFI->addStackRoot(FI->getIndex(), TypeMap);
7592 return;
7593 }
7594 case Intrinsic::gcread:
7595 case Intrinsic::gcwrite:
7596 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7597 case Intrinsic::get_rounding:
7598 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7599 setValue(&I, Res);
7600 DAG.setRoot(Res.getValue(1));
7601 return;
7602
7603 case Intrinsic::expect:
7604 case Intrinsic::expect_with_probability:
7605 // Just replace __builtin_expect(exp, c) and
7606 // __builtin_expect_with_probability(exp, c, p) with EXP.
7607 setValue(&I, getValue(I.getArgOperand(0)));
7608 return;
7609
7610 case Intrinsic::ubsantrap:
7611 case Intrinsic::debugtrap:
7612 case Intrinsic::trap: {
7613 StringRef TrapFuncName =
7614 I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7615 if (TrapFuncName.empty()) {
7616 switch (Intrinsic) {
7617 case Intrinsic::trap:
7618 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7619 break;
7620 case Intrinsic::debugtrap:
7621 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7622 break;
7623 case Intrinsic::ubsantrap:
7624 DAG.setRoot(DAG.getNode(
7625 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7626 DAG.getTargetConstant(
7627 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7628 MVT::i32)));
7629 break;
7630 default: llvm_unreachable("unknown trap intrinsic");
7631 }
7632 DAG.addNoMergeSiteInfo(DAG.getRoot().getNode(),
7633 I.hasFnAttr(Attribute::NoMerge));
7634 return;
7635 }
7637 if (Intrinsic == Intrinsic::ubsantrap) {
7638 Value *Arg = I.getArgOperand(0);
7639 Args.emplace_back(Arg, getValue(Arg));
7640 }
7641
7642 TargetLowering::CallLoweringInfo CLI(DAG);
7643 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7644 CallingConv::C, I.getType(),
7645 DAG.getExternalSymbol(TrapFuncName.data(),
7646 TLI.getPointerTy(DAG.getDataLayout())),
7647 std::move(Args));
7648 CLI.NoMerge = I.hasFnAttr(Attribute::NoMerge);
7649 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7650 DAG.setRoot(Result.second);
7651 return;
7652 }
7653
7654 case Intrinsic::allow_runtime_check:
7655 case Intrinsic::allow_ubsan_check:
7656 setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7657 return;
7658
7659 case Intrinsic::uadd_with_overflow:
7660 case Intrinsic::sadd_with_overflow:
7661 case Intrinsic::usub_with_overflow:
7662 case Intrinsic::ssub_with_overflow:
7663 case Intrinsic::umul_with_overflow:
7664 case Intrinsic::smul_with_overflow: {
7666 switch (Intrinsic) {
7667 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
7668 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7669 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7670 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7671 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7672 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7673 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7674 }
7675 SDValue Op1 = getValue(I.getArgOperand(0));
7676 SDValue Op2 = getValue(I.getArgOperand(1));
7677
7678 EVT ResultVT = Op1.getValueType();
7679 EVT OverflowVT = ResultVT.changeElementType(*Context, MVT::i1);
7680
7681 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7682 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7683 return;
7684 }
7685 case Intrinsic::prefetch: {
7686 SDValue Ops[5];
7687 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7689 Ops[0] = DAG.getRoot();
7690 Ops[1] = getValue(I.getArgOperand(0));
7691 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7692 MVT::i32);
7693 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7694 MVT::i32);
7695 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7696 MVT::i32);
7697 SDValue Result = DAG.getMemIntrinsicNode(
7698 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7699 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7700 /* align */ std::nullopt, Flags);
7701
7702 // Chain the prefetch in parallel with any pending loads, to stay out of
7703 // the way of later optimizations.
7704 PendingLoads.push_back(Result);
7705 Result = getRoot();
7706 DAG.setRoot(Result);
7707 return;
7708 }
7709 case Intrinsic::lifetime_start:
7710 case Intrinsic::lifetime_end: {
7711 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7712 // Stack coloring is not enabled in O0, discard region information.
7713 if (TM.getOptLevel() == CodeGenOptLevel::None)
7714 return;
7715
7716 const AllocaInst *LifetimeObject = dyn_cast<AllocaInst>(I.getArgOperand(0));
7717 if (!LifetimeObject)
7718 return;
7719
7720 // First check that the Alloca is static, otherwise it won't have a
7721 // valid frame index.
7722 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7723 if (SI == FuncInfo.StaticAllocaMap.end())
7724 return;
7725
7726 const int FrameIndex = SI->second;
7727 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex);
7728 DAG.setRoot(Res);
7729 return;
7730 }
7731 case Intrinsic::pseudoprobe: {
7732 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7733 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7734 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7735 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7736 DAG.setRoot(Res);
7737 return;
7738 }
7739 case Intrinsic::invariant_start:
7740 // Discard region information.
7741 setValue(&I,
7742 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7743 return;
7744 case Intrinsic::invariant_end:
7745 // Discard region information.
7746 return;
7747 case Intrinsic::clear_cache: {
7748 SDValue InputChain = DAG.getRoot();
7749 SDValue StartVal = getValue(I.getArgOperand(0));
7750 SDValue EndVal = getValue(I.getArgOperand(1));
7751 Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7752 {InputChain, StartVal, EndVal});
7753 setValue(&I, Res);
7754 DAG.setRoot(Res);
7755 return;
7756 }
7757 case Intrinsic::donothing:
7758 case Intrinsic::seh_try_begin:
7759 case Intrinsic::seh_scope_begin:
7760 case Intrinsic::seh_try_end:
7761 case Intrinsic::seh_scope_end:
7762 // ignore
7763 return;
7764 case Intrinsic::experimental_stackmap:
7765 visitStackmap(I);
7766 return;
7767 case Intrinsic::experimental_patchpoint_void:
7768 case Intrinsic::experimental_patchpoint:
7769 visitPatchpoint(I);
7770 return;
7771 case Intrinsic::experimental_gc_statepoint:
7773 return;
7774 case Intrinsic::experimental_gc_result:
7775 visitGCResult(cast<GCResultInst>(I));
7776 return;
7777 case Intrinsic::experimental_gc_relocate:
7778 visitGCRelocate(cast<GCRelocateInst>(I));
7779 return;
7780 case Intrinsic::instrprof_cover:
7781 llvm_unreachable("instrprof failed to lower a cover");
7782 case Intrinsic::instrprof_increment:
7783 llvm_unreachable("instrprof failed to lower an increment");
7784 case Intrinsic::instrprof_timestamp:
7785 llvm_unreachable("instrprof failed to lower a timestamp");
7786 case Intrinsic::instrprof_value_profile:
7787 llvm_unreachable("instrprof failed to lower a value profiling call");
7788 case Intrinsic::instrprof_mcdc_parameters:
7789 llvm_unreachable("instrprof failed to lower mcdc parameters");
7790 case Intrinsic::instrprof_mcdc_tvbitmap_update:
7791 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7792 case Intrinsic::localescape: {
7793 MachineFunction &MF = DAG.getMachineFunction();
7794 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7795
7796 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7797 // is the same on all targets.
7798 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7799 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7800 if (isa<ConstantPointerNull>(Arg))
7801 continue; // Skip null pointers. They represent a hole in index space.
7802 AllocaInst *Slot = cast<AllocaInst>(Arg);
7803 assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7804 "can only escape static allocas");
7805 int FI = FuncInfo.StaticAllocaMap[Slot];
7806 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7808 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7809 TII->get(TargetOpcode::LOCAL_ESCAPE))
7810 .addSym(FrameAllocSym)
7811 .addFrameIndex(FI);
7812 }
7813
7814 return;
7815 }
7816
7817 case Intrinsic::localrecover: {
7818 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7819 MachineFunction &MF = DAG.getMachineFunction();
7820
7821 // Get the symbol that defines the frame offset.
7822 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7823 auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7824 unsigned IdxVal =
7825 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7826 MCSymbol *FrameAllocSym = MF.getContext().getOrCreateFrameAllocSymbol(
7828
7829 Value *FP = I.getArgOperand(1);
7830 SDValue FPVal = getValue(FP);
7831 EVT PtrVT = FPVal.getValueType();
7832
7833 // Create a MCSymbol for the label to avoid any target lowering
7834 // that would make this PC relative.
7835 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7836 SDValue OffsetVal =
7837 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7838
7839 // Add the offset to the FP.
7840 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7841 setValue(&I, Add);
7842
7843 return;
7844 }
7845
7846 case Intrinsic::fake_use: {
7847 Value *V = I.getArgOperand(0);
7848 SDValue Ops[2];
7849 // For Values not declared or previously used in this basic block, the
7850 // NodeMap will not have an entry, and `getValue` will assert if V has no
7851 // valid register value.
7852 auto FakeUseValue = [&]() -> SDValue {
7853 SDValue &N = NodeMap[V];
7854 if (N.getNode())
7855 return N;
7856
7857 // If there's a virtual register allocated and initialized for this
7858 // value, use it.
7859 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
7860 return copyFromReg;
7861 // FIXME: Do we want to preserve constants? It seems pointless.
7862 if (isa<Constant>(V))
7863 return getValue(V);
7864 return SDValue();
7865 }();
7866 if (!FakeUseValue || FakeUseValue.isUndef())
7867 return;
7868 Ops[0] = getRoot();
7869 Ops[1] = FakeUseValue;
7870 // Also, do not translate a fake use with an undef operand, or any other
7871 // empty SDValues.
7872 if (!Ops[1] || Ops[1].isUndef())
7873 return;
7874 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other, Ops));
7875 return;
7876 }
7877
7878 case Intrinsic::reloc_none: {
7879 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7880 StringRef SymbolName = cast<MDString>(MD)->getString();
7881 SDValue Ops[2] = {
7882 getRoot(),
7883 DAG.getTargetExternalSymbol(
7884 SymbolName.data(), TLI.getProgramPointerTy(DAG.getDataLayout()))};
7885 DAG.setRoot(DAG.getNode(ISD::RELOC_NONE, sdl, MVT::Other, Ops));
7886 return;
7887 }
7888
7889 case Intrinsic::eh_exceptionpointer:
7890 case Intrinsic::eh_exceptioncode: {
7891 // Get the exception pointer vreg, copy from it, and resize it to fit.
7892 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7893 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7894 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7895 Register VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7896 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7897 if (Intrinsic == Intrinsic::eh_exceptioncode)
7898 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7899 setValue(&I, N);
7900 return;
7901 }
7902 case Intrinsic::xray_customevent: {
7903 // Here we want to make sure that the intrinsic behaves as if it has a
7904 // specific calling convention.
7905 const auto &Triple = DAG.getTarget().getTargetTriple();
7906 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7907 return;
7908
7910
7911 // We want to say that we always want the arguments in registers.
7912 SDValue LogEntryVal = getValue(I.getArgOperand(0));
7913 SDValue StrSizeVal = getValue(I.getArgOperand(1));
7914 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7915 SDValue Chain = getRoot();
7916 Ops.push_back(LogEntryVal);
7917 Ops.push_back(StrSizeVal);
7918 Ops.push_back(Chain);
7919
7920 // We need to enforce the calling convention for the callsite, so that
7921 // argument ordering is enforced correctly, and that register allocation can
7922 // see that some registers may be assumed clobbered and have to preserve
7923 // them across calls to the intrinsic.
7924 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7925 sdl, NodeTys, Ops);
7926 SDValue patchableNode = SDValue(MN, 0);
7927 DAG.setRoot(patchableNode);
7928 setValue(&I, patchableNode);
7929 return;
7930 }
7931 case Intrinsic::xray_typedevent: {
7932 // Here we want to make sure that the intrinsic behaves as if it has a
7933 // specific calling convention.
7934 const auto &Triple = DAG.getTarget().getTargetTriple();
7935 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7936 return;
7937
7939
7940 // We want to say that we always want the arguments in registers.
7941 // It's unclear to me how manipulating the selection DAG here forces callers
7942 // to provide arguments in registers instead of on the stack.
7943 SDValue LogTypeId = getValue(I.getArgOperand(0));
7944 SDValue LogEntryVal = getValue(I.getArgOperand(1));
7945 SDValue StrSizeVal = getValue(I.getArgOperand(2));
7946 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7947 SDValue Chain = getRoot();
7948 Ops.push_back(LogTypeId);
7949 Ops.push_back(LogEntryVal);
7950 Ops.push_back(StrSizeVal);
7951 Ops.push_back(Chain);
7952
7953 // We need to enforce the calling convention for the callsite, so that
7954 // argument ordering is enforced correctly, and that register allocation can
7955 // see that some registers may be assumed clobbered and have to preserve
7956 // them across calls to the intrinsic.
7957 MachineSDNode *MN = DAG.getMachineNode(
7958 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
7959 SDValue patchableNode = SDValue(MN, 0);
7960 DAG.setRoot(patchableNode);
7961 setValue(&I, patchableNode);
7962 return;
7963 }
7964 case Intrinsic::experimental_deoptimize:
7966 return;
7967 case Intrinsic::stepvector:
7968 visitStepVector(I);
7969 return;
7970 case Intrinsic::vector_reduce_fadd:
7971 case Intrinsic::vector_reduce_fmul:
7972 case Intrinsic::vector_reduce_add:
7973 case Intrinsic::vector_reduce_mul:
7974 case Intrinsic::vector_reduce_and:
7975 case Intrinsic::vector_reduce_or:
7976 case Intrinsic::vector_reduce_xor:
7977 case Intrinsic::vector_reduce_smax:
7978 case Intrinsic::vector_reduce_smin:
7979 case Intrinsic::vector_reduce_umax:
7980 case Intrinsic::vector_reduce_umin:
7981 case Intrinsic::vector_reduce_fmax:
7982 case Intrinsic::vector_reduce_fmin:
7983 case Intrinsic::vector_reduce_fmaximum:
7984 case Intrinsic::vector_reduce_fminimum:
7985 visitVectorReduce(I, Intrinsic);
7986 return;
7987
7988 case Intrinsic::icall_branch_funnel: {
7990 Ops.push_back(getValue(I.getArgOperand(0)));
7991
7992 int64_t Offset;
7994 I.getArgOperand(1), Offset, DAG.getDataLayout()));
7995 if (!Base)
7997 "llvm.icall.branch.funnel operand must be a GlobalValue");
7998 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
7999
8000 struct BranchFunnelTarget {
8001 int64_t Offset;
8003 };
8005
8006 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
8008 I.getArgOperand(Op), Offset, DAG.getDataLayout()));
8009 if (ElemBase != Base)
8010 report_fatal_error("all llvm.icall.branch.funnel operands must refer "
8011 "to the same GlobalValue");
8012
8013 SDValue Val = getValue(I.getArgOperand(Op + 1));
8014 auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
8015 if (!GA)
8017 "llvm.icall.branch.funnel operand must be a GlobalValue");
8018 Targets.push_back({Offset, DAG.getTargetGlobalAddress(
8019 GA->getGlobal(), sdl, Val.getValueType(),
8020 GA->getOffset())});
8021 }
8022 llvm::sort(Targets,
8023 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
8024 return T1.Offset < T2.Offset;
8025 });
8026
8027 for (auto &T : Targets) {
8028 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
8029 Ops.push_back(T.Target);
8030 }
8031
8032 Ops.push_back(DAG.getRoot()); // Chain
8033 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
8034 MVT::Other, Ops),
8035 0);
8036 DAG.setRoot(N);
8037 setValue(&I, N);
8038 HasTailCall = true;
8039 return;
8040 }
8041
8042 case Intrinsic::wasm_landingpad_index:
8043 // Information this intrinsic contained has been transferred to
8044 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
8045 // delete it now.
8046 return;
8047
8048 case Intrinsic::aarch64_settag:
8049 case Intrinsic::aarch64_settag_zero: {
8050 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8051 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
8053 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
8054 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
8055 ZeroMemory);
8056 DAG.setRoot(Val);
8057 setValue(&I, Val);
8058 return;
8059 }
8060 case Intrinsic::amdgcn_cs_chain: {
8061 // At this point we don't care if it's amdgpu_cs_chain or
8062 // amdgpu_cs_chain_preserve.
8064
8065 Type *RetTy = I.getType();
8066 assert(RetTy->isVoidTy() && "Should not return");
8067
8068 SDValue Callee = getValue(I.getOperand(0));
8069
8070 // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
8071 // We'll also tack the value of the EXEC mask at the end.
8073 Args.reserve(3);
8074
8075 for (unsigned Idx : {2, 3, 1}) {
8076 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8077 I.getOperand(Idx)->getType());
8078 Arg.setAttributes(&I, Idx);
8079 Args.push_back(Arg);
8080 }
8081
8082 assert(Args[0].IsInReg && "SGPR args should be marked inreg");
8083 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
8084 Args[2].IsInReg = true; // EXEC should be inreg
8085
8086 // Forward the flags and any additional arguments.
8087 for (unsigned Idx = 4; Idx < I.arg_size(); ++Idx) {
8088 TargetLowering::ArgListEntry Arg(getValue(I.getOperand(Idx)),
8089 I.getOperand(Idx)->getType());
8090 Arg.setAttributes(&I, Idx);
8091 Args.push_back(Arg);
8092 }
8093
8094 TargetLowering::CallLoweringInfo CLI(DAG);
8095 CLI.setDebugLoc(getCurSDLoc())
8096 .setChain(getRoot())
8097 .setCallee(CC, RetTy, Callee, std::move(Args))
8098 .setNoReturn(true)
8099 .setTailCall(true)
8100 .setConvergent(I.isConvergent());
8101 CLI.CB = &I;
8102 std::pair<SDValue, SDValue> Result =
8103 lowerInvokable(CLI, /*EHPadBB*/ nullptr);
8104 (void)Result;
8105 assert(!Result.first.getNode() && !Result.second.getNode() &&
8106 "Should've lowered as tail call");
8107
8108 HasTailCall = true;
8109 return;
8110 }
8111 case Intrinsic::amdgcn_call_whole_wave: {
8113 bool isTailCall = I.isTailCall();
8114
8115 // The first argument is the callee. Skip it when assembling the call args.
8116 for (unsigned Idx = 1; Idx < I.arg_size(); ++Idx) {
8117 TargetLowering::ArgListEntry Arg(getValue(I.getArgOperand(Idx)),
8118 I.getArgOperand(Idx)->getType());
8119 Arg.setAttributes(&I, Idx);
8120
8121 // If we have an explicit sret argument that is an Instruction, (i.e., it
8122 // might point to function-local memory), we can't meaningfully tail-call.
8123 if (Arg.IsSRet && isa<Instruction>(I.getArgOperand(Idx)))
8124 isTailCall = false;
8125
8126 Args.push_back(Arg);
8127 }
8128
8129 SDValue ConvControlToken;
8130 if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8131 auto *Token = Bundle->Inputs[0].get();
8132 ConvControlToken = getValue(Token);
8133 }
8134
8135 TargetLowering::CallLoweringInfo CLI(DAG);
8136 CLI.setDebugLoc(getCurSDLoc())
8137 .setChain(getRoot())
8138 .setCallee(CallingConv::AMDGPU_Gfx_WholeWave, I.getType(),
8139 getValue(I.getArgOperand(0)), std::move(Args))
8140 .setTailCall(isTailCall && canTailCall(I))
8141 .setIsPreallocated(
8142 I.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8143 .setConvergent(I.isConvergent())
8144 .setConvergenceControlToken(ConvControlToken);
8145 CLI.CB = &I;
8146
8147 std::pair<SDValue, SDValue> Result =
8148 lowerInvokable(CLI, /*EHPadBB=*/nullptr);
8149
8150 if (Result.first.getNode())
8151 setValue(&I, Result.first);
8152 return;
8153 }
8154 case Intrinsic::ptrmask: {
8155 SDValue Ptr = getValue(I.getOperand(0));
8156 SDValue Mask = getValue(I.getOperand(1));
8157
8158 // On arm64_32, pointers are 32 bits when stored in memory, but
8159 // zero-extended to 64 bits when in registers. Thus the mask is 32 bits to
8160 // match the index type, but the pointer is 64 bits, so the mask must be
8161 // zero-extended up to 64 bits to match the pointer.
8162 EVT PtrVT =
8163 TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8164 EVT MemVT =
8165 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
8166 assert(PtrVT == Ptr.getValueType());
8167 if (Mask.getValueType().getFixedSizeInBits() < MemVT.getFixedSizeInBits()) {
8168 // For AMDGPU buffer descriptors the mask is 48 bits, but the pointer is
8169 // 128-bit, so we have to pad the mask with ones for unused bits.
8170 auto HighOnes = DAG.getNode(
8171 ISD::SHL, sdl, PtrVT, DAG.getAllOnesConstant(sdl, PtrVT),
8172 DAG.getShiftAmountConstant(Mask.getValueType().getFixedSizeInBits(),
8173 PtrVT, sdl));
8174 Mask = DAG.getNode(ISD::OR, sdl, PtrVT,
8175 DAG.getZExtOrTrunc(Mask, sdl, PtrVT), HighOnes);
8176 } else if (Mask.getValueType() != PtrVT)
8177 Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
8178
8179 assert(Mask.getValueType() == PtrVT);
8180 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
8181 return;
8182 }
8183 case Intrinsic::threadlocal_address: {
8184 setValue(&I, getValue(I.getOperand(0)));
8185 return;
8186 }
8187 case Intrinsic::get_active_lane_mask: {
8188 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8189 SDValue Index = getValue(I.getOperand(0));
8190 SDValue TripCount = getValue(I.getOperand(1));
8191 EVT ElementVT = Index.getValueType();
8192
8193 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
8194 setValue(&I, DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, sdl, CCVT, Index,
8195 TripCount));
8196 return;
8197 }
8198
8199 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
8200 CCVT.getVectorElementCount());
8201
8202 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
8203 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
8204 SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
8205 SDValue VectorInduction = DAG.getNode(
8206 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
8207 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
8208 VectorTripCount, ISD::CondCode::SETULT);
8209 setValue(&I, SetCC);
8210 return;
8211 }
8212 case Intrinsic::experimental_get_vector_length: {
8213 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
8214 "Expected positive VF");
8215 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
8216 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
8217
8218 SDValue Count = getValue(I.getOperand(0));
8219 EVT CountVT = Count.getValueType();
8220
8221 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
8222 visitTargetIntrinsic(I, Intrinsic);
8223 return;
8224 }
8225
8226 // Expand to a umin between the trip count and the maximum elements the type
8227 // can hold.
8228 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8229
8230 // Extend the trip count to at least the result VT.
8231 if (CountVT.bitsLT(VT)) {
8232 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
8233 CountVT = VT;
8234 }
8235
8236 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
8237 ElementCount::get(VF, IsScalable));
8238
8239 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
8240 // Clip to the result type if needed.
8241 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
8242
8243 setValue(&I, Trunc);
8244 return;
8245 }
8246 case Intrinsic::vector_partial_reduce_add: {
8247 SDValue Acc = getValue(I.getOperand(0));
8248 SDValue Input = getValue(I.getOperand(1));
8249 setValue(&I,
8250 DAG.getNode(ISD::PARTIAL_REDUCE_UMLA, sdl, Acc.getValueType(), Acc,
8251 Input, DAG.getConstant(1, sdl, Input.getValueType())));
8252 return;
8253 }
8254 case Intrinsic::vector_partial_reduce_fadd: {
8255 SDValue Acc = getValue(I.getOperand(0));
8256 SDValue Input = getValue(I.getOperand(1));
8257 setValue(&I, DAG.getNode(
8258 ISD::PARTIAL_REDUCE_FMLA, sdl, Acc.getValueType(), Acc,
8259 Input, DAG.getConstantFP(1.0, sdl, Input.getValueType())));
8260 return;
8261 }
8262 case Intrinsic::experimental_cttz_elts: {
8263 auto DL = getCurSDLoc();
8264 SDValue Op = getValue(I.getOperand(0));
8265 EVT OpVT = Op.getValueType();
8266
8267 if (!TLI.shouldExpandCttzElements(OpVT)) {
8268 visitTargetIntrinsic(I, Intrinsic);
8269 return;
8270 }
8271
8272 if (OpVT.getScalarType() != MVT::i1) {
8273 // Compare the input vector elements to zero & use to count trailing zeros
8274 SDValue AllZero = DAG.getConstant(0, DL, OpVT);
8275 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
8276 OpVT.getVectorElementCount());
8277 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
8278 }
8279
8280 // If the zero-is-poison flag is set, we can assume the upper limit
8281 // of the result is VF-1.
8282 bool ZeroIsPoison =
8283 !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
8284 ConstantRange VScaleRange(1, true); // Dummy value.
8285 if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8286 VScaleRange = getVScaleRange(I.getCaller(), 64);
8287 unsigned EltWidth = TLI.getBitWidthForCttzElements(
8288 I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8289
8290 MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8291
8292 // Create the new vector type & get the vector length
8293 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8294 OpVT.getVectorElementCount());
8295
8296 SDValue VL =
8297 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8298
8299 SDValue StepVec = DAG.getStepVector(DL, NewVT);
8300 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8301 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8302 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8303 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8304 SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8305 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8306
8307 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8308 SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
8309
8310 setValue(&I, Ret);
8311 return;
8312 }
8313 case Intrinsic::vector_insert: {
8314 SDValue Vec = getValue(I.getOperand(0));
8315 SDValue SubVec = getValue(I.getOperand(1));
8316 SDValue Index = getValue(I.getOperand(2));
8317
8318 // The intrinsic's index type is i64, but the SDNode requires an index type
8319 // suitable for the target. Convert the index as required.
8320 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8321 if (Index.getValueType() != VectorIdxTy)
8322 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8323
8324 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8325 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8326 Index));
8327 return;
8328 }
8329 case Intrinsic::vector_extract: {
8330 SDValue Vec = getValue(I.getOperand(0));
8331 SDValue Index = getValue(I.getOperand(1));
8332 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8333
8334 // The intrinsic's index type is i64, but the SDNode requires an index type
8335 // suitable for the target. Convert the index as required.
8336 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8337 if (Index.getValueType() != VectorIdxTy)
8338 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8339
8340 setValue(&I,
8341 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8342 return;
8343 }
8344 case Intrinsic::experimental_vector_match: {
8345 SDValue Op1 = getValue(I.getOperand(0));
8346 SDValue Op2 = getValue(I.getOperand(1));
8347 SDValue Mask = getValue(I.getOperand(2));
8348 EVT Op1VT = Op1.getValueType();
8349 EVT Op2VT = Op2.getValueType();
8350 EVT ResVT = Mask.getValueType();
8351 unsigned SearchSize = Op2VT.getVectorNumElements();
8352
8353 // If the target has native support for this vector match operation, lower
8354 // the intrinsic untouched; otherwise, expand it below.
8355 if (!TLI.shouldExpandVectorMatch(Op1VT, SearchSize)) {
8356 visitTargetIntrinsic(I, Intrinsic);
8357 return;
8358 }
8359
8360 SDValue Ret = DAG.getConstant(0, sdl, ResVT);
8361
8362 for (unsigned i = 0; i < SearchSize; ++i) {
8363 SDValue Op2Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, sdl,
8364 Op2VT.getVectorElementType(), Op2,
8365 DAG.getVectorIdxConstant(i, sdl));
8366 SDValue Splat = DAG.getNode(ISD::SPLAT_VECTOR, sdl, Op1VT, Op2Elem);
8367 SDValue Cmp = DAG.getSetCC(sdl, ResVT, Op1, Splat, ISD::SETEQ);
8368 Ret = DAG.getNode(ISD::OR, sdl, ResVT, Ret, Cmp);
8369 }
8370
8371 setValue(&I, DAG.getNode(ISD::AND, sdl, ResVT, Ret, Mask));
8372 return;
8373 }
8374 case Intrinsic::vector_reverse:
8375 visitVectorReverse(I);
8376 return;
8377 case Intrinsic::vector_splice_left:
8378 case Intrinsic::vector_splice_right:
8379 visitVectorSplice(I);
8380 return;
8381 case Intrinsic::callbr_landingpad:
8382 visitCallBrLandingPad(I);
8383 return;
8384 case Intrinsic::vector_interleave2:
8385 visitVectorInterleave(I, 2);
8386 return;
8387 case Intrinsic::vector_interleave3:
8388 visitVectorInterleave(I, 3);
8389 return;
8390 case Intrinsic::vector_interleave4:
8391 visitVectorInterleave(I, 4);
8392 return;
8393 case Intrinsic::vector_interleave5:
8394 visitVectorInterleave(I, 5);
8395 return;
8396 case Intrinsic::vector_interleave6:
8397 visitVectorInterleave(I, 6);
8398 return;
8399 case Intrinsic::vector_interleave7:
8400 visitVectorInterleave(I, 7);
8401 return;
8402 case Intrinsic::vector_interleave8:
8403 visitVectorInterleave(I, 8);
8404 return;
8405 case Intrinsic::vector_deinterleave2:
8406 visitVectorDeinterleave(I, 2);
8407 return;
8408 case Intrinsic::vector_deinterleave3:
8409 visitVectorDeinterleave(I, 3);
8410 return;
8411 case Intrinsic::vector_deinterleave4:
8412 visitVectorDeinterleave(I, 4);
8413 return;
8414 case Intrinsic::vector_deinterleave5:
8415 visitVectorDeinterleave(I, 5);
8416 return;
8417 case Intrinsic::vector_deinterleave6:
8418 visitVectorDeinterleave(I, 6);
8419 return;
8420 case Intrinsic::vector_deinterleave7:
8421 visitVectorDeinterleave(I, 7);
8422 return;
8423 case Intrinsic::vector_deinterleave8:
8424 visitVectorDeinterleave(I, 8);
8425 return;
8426 case Intrinsic::experimental_vector_compress:
8427 setValue(&I, DAG.getNode(ISD::VECTOR_COMPRESS, sdl,
8428 getValue(I.getArgOperand(0)).getValueType(),
8429 getValue(I.getArgOperand(0)),
8430 getValue(I.getArgOperand(1)),
8431 getValue(I.getArgOperand(2)), Flags));
8432 return;
8433 case Intrinsic::experimental_convergence_anchor:
8434 case Intrinsic::experimental_convergence_entry:
8435 case Intrinsic::experimental_convergence_loop:
8436 visitConvergenceControl(I, Intrinsic);
8437 return;
8438 case Intrinsic::experimental_vector_histogram_add: {
8439 visitVectorHistogram(I, Intrinsic);
8440 return;
8441 }
8442 case Intrinsic::experimental_vector_extract_last_active: {
8443 visitVectorExtractLastActive(I, Intrinsic);
8444 return;
8445 }
8446 case Intrinsic::loop_dependence_war_mask:
8447 setValue(&I,
8449 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8450 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8451 DAG.getConstant(0, sdl, MVT::i64)));
8452 return;
8453 case Intrinsic::loop_dependence_raw_mask:
8454 setValue(&I,
8456 EVT::getEVT(I.getType()), getValue(I.getOperand(0)),
8457 getValue(I.getOperand(1)), getValue(I.getOperand(2)),
8458 DAG.getConstant(0, sdl, MVT::i64)));
8459 return;
8460 }
8461}
8462
8463void SelectionDAGBuilder::pushFPOpOutChain(SDValue Result,
8465 assert(Result.getNode()->getNumValues() == 2);
8466 SDValue OutChain = Result.getValue(1);
8467 assert(OutChain.getValueType() == MVT::Other);
8468
8469 // Instead of updating the root immediately, push the produced chain to the
8470 // appropriate list, deferring the update until the root is requested. In this
8471 // case, the nodes from the lists are chained using TokenFactor, indicating
8472 // that the operations are independent.
8473 //
8474 // In particular, the root is updated before any call that might access the
8475 // floating-point environment, except for constrained intrinsics.
8476 switch (EB) {
8479 PendingConstrainedFP.push_back(OutChain);
8480 break;
8482 PendingConstrainedFPStrict.push_back(OutChain);
8483 break;
8484 }
8485}
8486
8487void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8488 const ConstrainedFPIntrinsic &FPI) {
8489 SDLoc sdl = getCurSDLoc();
8490
8491 // We do not need to serialize constrained FP intrinsics against
8492 // each other or against (nonvolatile) loads, so they can be
8493 // chained like loads.
8495 SDValue Chain = getFPOperationRoot(EB);
8497 Opers.push_back(Chain);
8498 for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8499 Opers.push_back(getValue(FPI.getArgOperand(I)));
8500
8501 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8502 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8503 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8504
8505 SDNodeFlags Flags;
8507 Flags.setNoFPExcept(true);
8508
8509 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8510 Flags.copyFMF(*FPOp);
8511
8512 unsigned Opcode;
8513 switch (FPI.getIntrinsicID()) {
8514 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
8515#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
8516 case Intrinsic::INTRINSIC: \
8517 Opcode = ISD::STRICT_##DAGN; \
8518 break;
8519#include "llvm/IR/ConstrainedOps.def"
8520 case Intrinsic::experimental_constrained_fmuladd: {
8521 Opcode = ISD::STRICT_FMA;
8522 // Break fmuladd into fmul and fadd.
8523 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8524 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8525 Opers.pop_back();
8526 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8527 pushFPOpOutChain(Mul, EB);
8528 Opcode = ISD::STRICT_FADD;
8529 Opers.clear();
8530 Opers.push_back(Mul.getValue(1));
8531 Opers.push_back(Mul.getValue(0));
8532 Opers.push_back(getValue(FPI.getArgOperand(2)));
8533 }
8534 break;
8535 }
8536 }
8537
8538 // A few strict DAG nodes carry additional operands that are not
8539 // set up by the default code above.
8540 switch (Opcode) {
8541 default: break;
8543 Opers.push_back(
8544 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8545 break;
8546 case ISD::STRICT_FSETCC:
8547 case ISD::STRICT_FSETCCS: {
8548 auto *FPCmp = dyn_cast<ConstrainedFPCmpIntrinsic>(&FPI);
8549 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8550 if (TM.Options.NoNaNsFPMath)
8551 Condition = getFCmpCodeWithoutNaN(Condition);
8552 Opers.push_back(DAG.getCondCode(Condition));
8553 break;
8554 }
8555 }
8556
8557 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8558 pushFPOpOutChain(Result, EB);
8559
8560 SDValue FPResult = Result.getValue(0);
8561 setValue(&FPI, FPResult);
8562}
8563
8564static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8565 std::optional<unsigned> ResOPC;
8566 switch (VPIntrin.getIntrinsicID()) {
8567 case Intrinsic::vp_ctlz: {
8568 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8569 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8570 break;
8571 }
8572 case Intrinsic::vp_cttz: {
8573 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8574 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8575 break;
8576 }
8577 case Intrinsic::vp_cttz_elts: {
8578 bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8579 ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8580 break;
8581 }
8582#define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \
8583 case Intrinsic::VPID: \
8584 ResOPC = ISD::VPSD; \
8585 break;
8586#include "llvm/IR/VPIntrinsics.def"
8587 }
8588
8589 if (!ResOPC)
8591 "Inconsistency: no SDNode available for this VPIntrinsic!");
8592
8593 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8594 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8595 if (VPIntrin.getFastMathFlags().allowReassoc())
8596 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8597 : ISD::VP_REDUCE_FMUL;
8598 }
8599
8600 return *ResOPC;
8601}
8602
8603void SelectionDAGBuilder::visitVPLoad(
8604 const VPIntrinsic &VPIntrin, EVT VT,
8605 const SmallVectorImpl<SDValue> &OpValues) {
8606 SDLoc DL = getCurSDLoc();
8607 Value *PtrOperand = VPIntrin.getArgOperand(0);
8608 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8609 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8610 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8611 SDValue LD;
8612 // Do not serialize variable-length loads of constant memory with
8613 // anything.
8614 if (!Alignment)
8615 Alignment = DAG.getEVTAlign(VT);
8616 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8617 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8618 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8619 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8620 MachineMemOperand::Flags MMOFlags =
8621 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8622 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8623 MachinePointerInfo(PtrOperand), MMOFlags,
8624 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8625 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8626 MMO, false /*IsExpanding */);
8627 if (AddToChain)
8628 PendingLoads.push_back(LD.getValue(1));
8629 setValue(&VPIntrin, LD);
8630}
8631
8632void SelectionDAGBuilder::visitVPLoadFF(
8633 const VPIntrinsic &VPIntrin, EVT VT, EVT EVLVT,
8634 const SmallVectorImpl<SDValue> &OpValues) {
8635 assert(OpValues.size() == 3 && "Unexpected number of operands");
8636 SDLoc DL = getCurSDLoc();
8637 Value *PtrOperand = VPIntrin.getArgOperand(0);
8638 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8639 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8640 const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
8641 SDValue LD;
8642 // Do not serialize variable-length loads of constant memory with
8643 // anything.
8644 if (!Alignment)
8645 Alignment = DAG.getEVTAlign(VT);
8646 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8647 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8648 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8649 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8650 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8651 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8652 LD = DAG.getLoadFFVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8653 MMO);
8654 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, EVLVT, LD.getValue(1));
8655 if (AddToChain)
8656 PendingLoads.push_back(LD.getValue(2));
8657 setValue(&VPIntrin, DAG.getMergeValues({LD.getValue(0), Trunc}, DL));
8658}
8659
8660void SelectionDAGBuilder::visitVPGather(
8661 const VPIntrinsic &VPIntrin, EVT VT,
8662 const SmallVectorImpl<SDValue> &OpValues) {
8663 SDLoc DL = getCurSDLoc();
8664 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8665 Value *PtrOperand = VPIntrin.getArgOperand(0);
8666 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8667 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8668 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8669 SDValue LD;
8670 if (!Alignment)
8671 Alignment = DAG.getEVTAlign(VT.getScalarType());
8672 unsigned AS =
8673 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8674 MachineMemOperand::Flags MMOFlags =
8675 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8676 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8677 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8678 *Alignment, AAInfo, Ranges);
8679 SDValue Base, Index, Scale;
8680 bool UniformBase =
8681 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8682 VT.getScalarStoreSize());
8683 if (!UniformBase) {
8684 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8685 Index = getValue(PtrOperand);
8686 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8687 }
8688 EVT IdxVT = Index.getValueType();
8689 EVT EltTy = IdxVT.getVectorElementType();
8690 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8691 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8692 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8693 }
8694 LD = DAG.getGatherVP(
8695 DAG.getVTList(VT, MVT::Other), VT, DL,
8696 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8698 PendingLoads.push_back(LD.getValue(1));
8699 setValue(&VPIntrin, LD);
8700}
8701
8702void SelectionDAGBuilder::visitVPStore(
8703 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8704 SDLoc DL = getCurSDLoc();
8705 Value *PtrOperand = VPIntrin.getArgOperand(1);
8706 EVT VT = OpValues[0].getValueType();
8707 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8708 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8709 SDValue ST;
8710 if (!Alignment)
8711 Alignment = DAG.getEVTAlign(VT);
8712 SDValue Ptr = OpValues[1];
8713 SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8714 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8715 MachineMemOperand::Flags MMOFlags =
8716 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8717 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8718 MachinePointerInfo(PtrOperand), MMOFlags,
8719 LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8720 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8721 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8722 /* IsTruncating */ false, /*IsCompressing*/ false);
8723 DAG.setRoot(ST);
8724 setValue(&VPIntrin, ST);
8725}
8726
8727void SelectionDAGBuilder::visitVPScatter(
8728 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8729 SDLoc DL = getCurSDLoc();
8730 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8731 Value *PtrOperand = VPIntrin.getArgOperand(1);
8732 EVT VT = OpValues[0].getValueType();
8733 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8734 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8735 SDValue ST;
8736 if (!Alignment)
8737 Alignment = DAG.getEVTAlign(VT.getScalarType());
8738 unsigned AS =
8739 PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8740 MachineMemOperand::Flags MMOFlags =
8741 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8742 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8743 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8744 *Alignment, AAInfo);
8745 SDValue Base, Index, Scale;
8746 bool UniformBase =
8747 getUniformBase(PtrOperand, Base, Index, Scale, this, VPIntrin.getParent(),
8748 VT.getScalarStoreSize());
8749 if (!UniformBase) {
8750 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8751 Index = getValue(PtrOperand);
8752 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8753 }
8754 EVT IdxVT = Index.getValueType();
8755 EVT EltTy = IdxVT.getVectorElementType();
8756 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8757 EVT NewIdxVT = IdxVT.changeVectorElementType(*DAG.getContext(), EltTy);
8758 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8759 }
8760 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8761 {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8762 OpValues[2], OpValues[3]},
8763 MMO, ISD::SIGNED_SCALED);
8764 DAG.setRoot(ST);
8765 setValue(&VPIntrin, ST);
8766}
8767
8768void SelectionDAGBuilder::visitVPStridedLoad(
8769 const VPIntrinsic &VPIntrin, EVT VT,
8770 const SmallVectorImpl<SDValue> &OpValues) {
8771 SDLoc DL = getCurSDLoc();
8772 Value *PtrOperand = VPIntrin.getArgOperand(0);
8773 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8774 if (!Alignment)
8775 Alignment = DAG.getEVTAlign(VT.getScalarType());
8776 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8777 const MDNode *Ranges = getRangeMetadata(VPIntrin);
8778 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8779 bool AddToChain = !BatchAA || !BatchAA->pointsToConstantMemory(ML);
8780 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8781 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8782 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8783 MachineMemOperand::Flags MMOFlags =
8784 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8785 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8786 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8787 *Alignment, AAInfo, Ranges);
8788
8789 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8790 OpValues[2], OpValues[3], MMO,
8791 false /*IsExpanding*/);
8792
8793 if (AddToChain)
8794 PendingLoads.push_back(LD.getValue(1));
8795 setValue(&VPIntrin, LD);
8796}
8797
8798void SelectionDAGBuilder::visitVPStridedStore(
8799 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8800 SDLoc DL = getCurSDLoc();
8801 Value *PtrOperand = VPIntrin.getArgOperand(1);
8802 EVT VT = OpValues[0].getValueType();
8803 MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8804 if (!Alignment)
8805 Alignment = DAG.getEVTAlign(VT.getScalarType());
8806 AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8807 unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8808 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8809 MachineMemOperand::Flags MMOFlags =
8810 TLI.getVPIntrinsicMemOperandFlags(VPIntrin);
8811 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8812 MachinePointerInfo(AS), MMOFlags, LocationSize::beforeOrAfterPointer(),
8813 *Alignment, AAInfo);
8814
8815 SDValue ST = DAG.getStridedStoreVP(
8816 getMemoryRoot(), DL, OpValues[0], OpValues[1],
8817 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8818 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8819 /*IsCompressing*/ false);
8820
8821 DAG.setRoot(ST);
8822 setValue(&VPIntrin, ST);
8823}
8824
8825void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8826 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8827 SDLoc DL = getCurSDLoc();
8828
8829 ISD::CondCode Condition;
8831 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8832 if (IsFP) {
8833 // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
8834 // flags, but calls that don't return floating-point types can't be
8835 // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
8836 Condition = getFCmpCondCode(CondCode);
8837 if (TM.Options.NoNaNsFPMath)
8838 Condition = getFCmpCodeWithoutNaN(Condition);
8839 } else {
8840 Condition = getICmpCondCode(CondCode);
8841 }
8842
8843 SDValue Op1 = getValue(VPIntrin.getOperand(0));
8844 SDValue Op2 = getValue(VPIntrin.getOperand(1));
8845 // #2 is the condition code
8846 SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8847 SDValue EVL = getValue(VPIntrin.getOperand(4));
8848 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8849 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8850 "Unexpected target EVL type");
8851 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8852
8853 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8854 VPIntrin.getType());
8855 setValue(&VPIntrin,
8856 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8857}
8858
8859void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8860 const VPIntrinsic &VPIntrin) {
8861 SDLoc DL = getCurSDLoc();
8862 unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8863
8864 auto IID = VPIntrin.getIntrinsicID();
8865
8866 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8867 return visitVPCmp(*CmpI);
8868
8869 SmallVector<EVT, 4> ValueVTs;
8870 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8871 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8872 SDVTList VTs = DAG.getVTList(ValueVTs);
8873
8874 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8875
8876 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8877 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8878 "Unexpected target EVL type");
8879
8880 // Request operands.
8881 SmallVector<SDValue, 7> OpValues;
8882 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8883 auto Op = getValue(VPIntrin.getArgOperand(I));
8884 if (I == EVLParamPos)
8885 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8886 OpValues.push_back(Op);
8887 }
8888
8889 switch (Opcode) {
8890 default: {
8891 SDNodeFlags SDFlags;
8892 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8893 SDFlags.copyFMF(*FPMO);
8894 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8895 setValue(&VPIntrin, Result);
8896 break;
8897 }
8898 case ISD::VP_LOAD:
8899 visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8900 break;
8901 case ISD::VP_LOAD_FF:
8902 visitVPLoadFF(VPIntrin, ValueVTs[0], ValueVTs[1], OpValues);
8903 break;
8904 case ISD::VP_GATHER:
8905 visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8906 break;
8907 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8908 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8909 break;
8910 case ISD::VP_STORE:
8911 visitVPStore(VPIntrin, OpValues);
8912 break;
8913 case ISD::VP_SCATTER:
8914 visitVPScatter(VPIntrin, OpValues);
8915 break;
8916 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8917 visitVPStridedStore(VPIntrin, OpValues);
8918 break;
8919 case ISD::VP_FMULADD: {
8920 assert(OpValues.size() == 5 && "Unexpected number of operands");
8921 SDNodeFlags SDFlags;
8922 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8923 SDFlags.copyFMF(*FPMO);
8924 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8925 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8926 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8927 } else {
8928 SDValue Mul = DAG.getNode(
8929 ISD::VP_FMUL, DL, VTs,
8930 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8931 SDValue Add =
8932 DAG.getNode(ISD::VP_FADD, DL, VTs,
8933 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8934 setValue(&VPIntrin, Add);
8935 }
8936 break;
8937 }
8938 case ISD::VP_IS_FPCLASS: {
8939 const DataLayout DLayout = DAG.getDataLayout();
8940 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8941 auto Constant = OpValues[1]->getAsZExtVal();
8942 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8943 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8944 {OpValues[0], Check, OpValues[2], OpValues[3]});
8945 setValue(&VPIntrin, V);
8946 return;
8947 }
8948 case ISD::VP_INTTOPTR: {
8949 SDValue N = OpValues[0];
8950 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8951 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8952 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8953 OpValues[2]);
8954 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8955 OpValues[2]);
8956 setValue(&VPIntrin, N);
8957 break;
8958 }
8959 case ISD::VP_PTRTOINT: {
8960 SDValue N = OpValues[0];
8961 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8962 VPIntrin.getType());
8963 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
8964 VPIntrin.getOperand(0)->getType());
8965 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8966 OpValues[2]);
8967 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8968 OpValues[2]);
8969 setValue(&VPIntrin, N);
8970 break;
8971 }
8972 case ISD::VP_ABS:
8973 case ISD::VP_CTLZ:
8974 case ISD::VP_CTLZ_ZERO_UNDEF:
8975 case ISD::VP_CTTZ:
8976 case ISD::VP_CTTZ_ZERO_UNDEF:
8977 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
8978 case ISD::VP_CTTZ_ELTS: {
8979 SDValue Result =
8980 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
8981 setValue(&VPIntrin, Result);
8982 break;
8983 }
8984 }
8985}
8986
8987SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
8988 const BasicBlock *EHPadBB,
8989 MCSymbol *&BeginLabel) {
8990 MachineFunction &MF = DAG.getMachineFunction();
8991
8992 // Insert a label before the invoke call to mark the try range. This can be
8993 // used to detect deletion of the invoke via the MachineModuleInfo.
8994 BeginLabel = MF.getContext().createTempSymbol();
8995
8996 // For SjLj, keep track of which landing pads go with which invokes
8997 // so as to maintain the ordering of pads in the LSDA.
8998 unsigned CallSiteIndex = FuncInfo.getCurrentCallSite();
8999 if (CallSiteIndex) {
9000 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
9001 LPadToCallSiteMap[FuncInfo.getMBB(EHPadBB)].push_back(CallSiteIndex);
9002
9003 // Now that the call site is handled, stop tracking it.
9004 FuncInfo.setCurrentCallSite(0);
9005 }
9006
9007 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
9008}
9009
9010SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
9011 const BasicBlock *EHPadBB,
9012 MCSymbol *BeginLabel) {
9013 assert(BeginLabel && "BeginLabel should've been set");
9014
9015 MachineFunction &MF = DAG.getMachineFunction();
9016
9017 // Insert a label at the end of the invoke call to mark the try range. This
9018 // can be used to detect deletion of the invoke via the MachineModuleInfo.
9019 MCSymbol *EndLabel = MF.getContext().createTempSymbol();
9020 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
9021
9022 // Inform MachineModuleInfo of range.
9023 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
9024 // There is a platform (e.g. wasm) that uses funclet style IR but does not
9025 // actually use outlined funclets and their LSDA info style.
9026 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
9027 assert(II && "II should've been set");
9028 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
9029 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
9030 } else if (!isScopedEHPersonality(Pers)) {
9031 assert(EHPadBB);
9032 MF.addInvoke(FuncInfo.getMBB(EHPadBB), BeginLabel, EndLabel);
9033 }
9034
9035 return Chain;
9036}
9037
9038std::pair<SDValue, SDValue>
9040 const BasicBlock *EHPadBB) {
9041 MCSymbol *BeginLabel = nullptr;
9042
9043 if (EHPadBB) {
9044 // Both PendingLoads and PendingExports must be flushed here;
9045 // this call might not return.
9046 (void)getRoot();
9047 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
9048 CLI.setChain(getRoot());
9049 }
9050
9051 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9052 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
9053
9054 assert((CLI.IsTailCall || Result.second.getNode()) &&
9055 "Non-null chain expected with non-tail call!");
9056 assert((Result.second.getNode() || !Result.first.getNode()) &&
9057 "Null value expected with tail call!");
9058
9059 if (!Result.second.getNode()) {
9060 // As a special case, a null chain means that a tail call has been emitted
9061 // and the DAG root is already updated.
9062 HasTailCall = true;
9063
9064 // Since there's no actual continuation from this block, nothing can be
9065 // relying on us setting vregs for them.
9066 PendingExports.clear();
9067 } else {
9068 DAG.setRoot(Result.second);
9069 }
9070
9071 if (EHPadBB) {
9072 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
9073 BeginLabel));
9074 Result.second = getRoot();
9075 }
9076
9077 return Result;
9078}
9079
9081 bool isMustTailCall = CB.isMustTailCall();
9082
9083 // Avoid emitting tail calls in functions with the disable-tail-calls
9084 // attribute.
9085 const Function *Caller = CB.getParent()->getParent();
9086 if (!isMustTailCall &&
9087 Caller->getFnAttribute("disable-tail-calls").getValueAsBool())
9088 return false;
9089
9090 // We can't tail call inside a function with a swifterror argument. Lowering
9091 // does not support this yet. It would have to move into the swifterror
9092 // register before the call.
9093 if (DAG.getTargetLoweringInfo().supportSwiftError() &&
9094 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
9095 return false;
9096
9097 // Check if target-independent constraints permit a tail call here.
9098 // Target-dependent constraints are checked within TLI->LowerCallTo.
9099 return isInTailCallPosition(CB, DAG.getTarget());
9100}
9101
9103 bool isTailCall, bool isMustTailCall,
9104 const BasicBlock *EHPadBB,
9105 const TargetLowering::PtrAuthInfo *PAI) {
9106 auto &DL = DAG.getDataLayout();
9107 FunctionType *FTy = CB.getFunctionType();
9108 Type *RetTy = CB.getType();
9109
9111 Args.reserve(CB.arg_size());
9112
9113 const Value *SwiftErrorVal = nullptr;
9114 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9115
9116 if (isTailCall)
9117 isTailCall = canTailCall(CB);
9118
9119 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
9120 const Value *V = *I;
9121
9122 // Skip empty types
9123 if (V->getType()->isEmptyTy())
9124 continue;
9125
9126 SDValue ArgNode = getValue(V);
9127 TargetLowering::ArgListEntry Entry(ArgNode, V->getType());
9128 Entry.setAttributes(&CB, I - CB.arg_begin());
9129
9130 // Use swifterror virtual register as input to the call.
9131 if (Entry.IsSwiftError && TLI.supportSwiftError()) {
9132 SwiftErrorVal = V;
9133 // We find the virtual register for the actual swifterror argument.
9134 // Instead of using the Value, we use the virtual register instead.
9135 Entry.Node =
9136 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
9137 EVT(TLI.getPointerTy(DL)));
9138 }
9139
9140 Args.push_back(Entry);
9141
9142 // If we have an explicit sret argument that is an Instruction, (i.e., it
9143 // might point to function-local memory), we can't meaningfully tail-call.
9144 if (Entry.IsSRet && isa<Instruction>(V))
9145 isTailCall = false;
9146 }
9147
9148 // If call site has a cfguardtarget operand bundle, create and add an
9149 // additional ArgListEntry.
9150 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
9151 Value *V = Bundle->Inputs[0];
9153 Entry.IsCFGuardTarget = true;
9154 Args.push_back(Entry);
9155 }
9156
9157 // Disable tail calls if there is an swifterror argument. Targets have not
9158 // been updated to support tail calls.
9159 if (TLI.supportSwiftError() && SwiftErrorVal)
9160 isTailCall = false;
9161
9162 ConstantInt *CFIType = nullptr;
9163 if (CB.isIndirectCall()) {
9164 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
9165 if (!TLI.supportKCFIBundles())
9167 "Target doesn't support calls with kcfi operand bundles.");
9168 CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
9169 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
9170 }
9171 }
9172
9173 SDValue ConvControlToken;
9174 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
9175 auto *Token = Bundle->Inputs[0].get();
9176 ConvControlToken = getValue(Token);
9177 }
9178
9179 GlobalValue *DeactivationSymbol = nullptr;
9181 DeactivationSymbol = cast<GlobalValue>(Bundle->Inputs[0].get());
9182 }
9183
9186 .setChain(getRoot())
9187 .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
9188 .setTailCall(isTailCall)
9192 .setCFIType(CFIType)
9193 .setConvergenceControlToken(ConvControlToken)
9194 .setDeactivationSymbol(DeactivationSymbol);
9195
9196 // Set the pointer authentication info if we have it.
9197 if (PAI) {
9198 if (!TLI.supportPtrAuthBundles())
9200 "This target doesn't support calls with ptrauth operand bundles.");
9201 CLI.setPtrAuth(*PAI);
9202 }
9203
9204 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9205
9206 if (Result.first.getNode()) {
9207 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
9208 Result.first = lowerNoFPClassToAssertNoFPClass(DAG, CB, Result.first);
9209 setValue(&CB, Result.first);
9210 }
9211
9212 // The last element of CLI.InVals has the SDValue for swifterror return.
9213 // Here we copy it to a virtual register and update SwiftErrorMap for
9214 // book-keeping.
9215 if (SwiftErrorVal && TLI.supportSwiftError()) {
9216 // Get the last element of InVals.
9217 SDValue Src = CLI.InVals.back();
9218 Register VReg =
9219 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
9220 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
9221 DAG.setRoot(CopyNode);
9222 }
9223}
9224
9225static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
9226 SelectionDAGBuilder &Builder) {
9227 // Check to see if this load can be trivially constant folded, e.g. if the
9228 // input is from a string literal.
9229 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
9230 // Cast pointer to the type we really want to load.
9231 Type *LoadTy =
9232 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
9233 if (LoadVT.isVector())
9234 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
9235 if (const Constant *LoadCst =
9236 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
9237 LoadTy, Builder.DAG.getDataLayout()))
9238 return Builder.getValue(LoadCst);
9239 }
9240
9241 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
9242 // still constant memory, the input chain can be the entry node.
9243 SDValue Root;
9244 bool ConstantMemory = false;
9245
9246 // Do not serialize (non-volatile) loads of constant memory with anything.
9247 if (Builder.BatchAA && Builder.BatchAA->pointsToConstantMemory(PtrVal)) {
9248 Root = Builder.DAG.getEntryNode();
9249 ConstantMemory = true;
9250 } else {
9251 // Do not serialize non-volatile loads against each other.
9252 Root = Builder.DAG.getRoot();
9253 }
9254
9255 SDValue Ptr = Builder.getValue(PtrVal);
9256 SDValue LoadVal =
9257 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
9258 MachinePointerInfo(PtrVal), Align(1));
9259
9260 if (!ConstantMemory)
9261 Builder.PendingLoads.push_back(LoadVal.getValue(1));
9262 return LoadVal;
9263}
9264
9265/// Record the value for an instruction that produces an integer result,
9266/// converting the type where necessary.
9267void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
9268 SDValue Value,
9269 bool IsSigned) {
9270 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9271 I.getType(), true);
9272 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
9273 setValue(&I, Value);
9274}
9275
9276/// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
9277/// true and lower it. Otherwise return false, and it will be lowered like a
9278/// normal call.
9279/// The caller already checked that \p I calls the appropriate LibFunc with a
9280/// correct prototype.
9281bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
9282 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
9283 const Value *Size = I.getArgOperand(2);
9284 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
9285 if (CSize && CSize->getZExtValue() == 0) {
9286 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
9287 I.getType(), true);
9288 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
9289 return true;
9290 }
9291
9292 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9293 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
9294 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
9295 getValue(Size), &I);
9296 if (Res.first.getNode()) {
9297 processIntegerCallValue(I, Res.first, true);
9298 PendingLoads.push_back(Res.second);
9299 return true;
9300 }
9301
9302 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
9303 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
9304 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
9305 return false;
9306
9307 // If the target has a fast compare for the given size, it will return a
9308 // preferred load type for that size. Require that the load VT is legal and
9309 // that the target supports unaligned loads of that type. Otherwise, return
9310 // INVALID.
9311 auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
9312 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9313 MVT LVT = TLI.hasFastEqualityCompare(NumBits);
9314 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
9315 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
9316 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
9317 // TODO: Check alignment of src and dest ptrs.
9318 unsigned DstAS = LHS->getType()->getPointerAddressSpace();
9319 unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
9320 if (!TLI.isTypeLegal(LVT) ||
9321 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
9322 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
9324 }
9325
9326 return LVT;
9327 };
9328
9329 // This turns into unaligned loads. We only do this if the target natively
9330 // supports the MVT we'll be loading or if it is small enough (<= 4) that
9331 // we'll only produce a small number of byte loads.
9332 MVT LoadVT;
9333 unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
9334 switch (NumBitsToCompare) {
9335 default:
9336 return false;
9337 case 16:
9338 LoadVT = MVT::i16;
9339 break;
9340 case 32:
9341 LoadVT = MVT::i32;
9342 break;
9343 case 64:
9344 case 128:
9345 case 256:
9346 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
9347 break;
9348 }
9349
9350 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
9351 return false;
9352
9353 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
9354 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
9355
9356 // Bitcast to a wide integer type if the loads are vectors.
9357 if (LoadVT.isVector()) {
9358 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
9359 LoadL = DAG.getBitcast(CmpVT, LoadL);
9360 LoadR = DAG.getBitcast(CmpVT, LoadR);
9361 }
9362
9363 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
9364 processIntegerCallValue(I, Cmp, false);
9365 return true;
9366}
9367
9368/// See if we can lower a memchr call into an optimized form. If so, return
9369/// true and lower it. Otherwise return false, and it will be lowered like a
9370/// normal call.
9371/// The caller already checked that \p I calls the appropriate LibFunc with a
9372/// correct prototype.
9373bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
9374 const Value *Src = I.getArgOperand(0);
9375 const Value *Char = I.getArgOperand(1);
9376 const Value *Length = I.getArgOperand(2);
9377
9378 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9379 std::pair<SDValue, SDValue> Res =
9380 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
9381 getValue(Src), getValue(Char), getValue(Length),
9382 MachinePointerInfo(Src));
9383 if (Res.first.getNode()) {
9384 setValue(&I, Res.first);
9385 PendingLoads.push_back(Res.second);
9386 return true;
9387 }
9388
9389 return false;
9390}
9391
9392/// See if we can lower a mempcpy call into an optimized form. If so, return
9393/// true and lower it. Otherwise return false, and it will be lowered like a
9394/// normal call.
9395/// The caller already checked that \p I calls the appropriate LibFunc with a
9396/// correct prototype.
9397bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
9398 SDValue Dst = getValue(I.getArgOperand(0));
9399 SDValue Src = getValue(I.getArgOperand(1));
9400 SDValue Size = getValue(I.getArgOperand(2));
9401
9402 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
9403 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
9404 // DAG::getMemcpy needs Alignment to be defined.
9405 Align Alignment = std::min(DstAlign, SrcAlign);
9406
9407 SDLoc sdl = getCurSDLoc();
9408
9409 // In the mempcpy context we need to pass in a false value for isTailCall
9410 // because the return pointer needs to be adjusted by the size of
9411 // the copied memory.
9412 SDValue Root = getMemoryRoot();
9413 SDValue MC = DAG.getMemcpy(
9414 Root, sdl, Dst, Src, Size, Alignment, false, false, /*CI=*/nullptr,
9415 std::nullopt, MachinePointerInfo(I.getArgOperand(0)),
9416 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata());
9417 assert(MC.getNode() != nullptr &&
9418 "** memcpy should not be lowered as TailCall in mempcpy context **");
9419 DAG.setRoot(MC);
9420
9421 // Check if Size needs to be truncated or extended.
9422 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9423
9424 // Adjust return pointer to point just past the last dst byte.
9425 SDValue DstPlusSize = DAG.getMemBasePlusOffset(Dst, Size, sdl);
9426 setValue(&I, DstPlusSize);
9427 return true;
9428}
9429
9430/// See if we can lower a strcpy call into an optimized form. If so, return
9431/// true and lower it, otherwise return false and it will be lowered like a
9432/// normal call.
9433/// The caller already checked that \p I calls the appropriate LibFunc with a
9434/// correct prototype.
9435bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9436 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9437
9438 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9439 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrcpy(
9440 DAG, getCurSDLoc(), getRoot(), getValue(Arg0), getValue(Arg1),
9441 MachinePointerInfo(Arg0), MachinePointerInfo(Arg1), isStpcpy, &I);
9442 if (Res.first.getNode()) {
9443 setValue(&I, Res.first);
9444 DAG.setRoot(Res.second);
9445 return true;
9446 }
9447
9448 return false;
9449}
9450
9451/// See if we can lower a strcmp call into an optimized form. If so, return
9452/// true and lower it, otherwise return false and it will be lowered like a
9453/// normal call.
9454/// The caller already checked that \p I calls the appropriate LibFunc with a
9455/// correct prototype.
9456bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9457 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9458
9459 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9460 std::pair<SDValue, SDValue> Res =
9461 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
9462 getValue(Arg0), getValue(Arg1),
9463 MachinePointerInfo(Arg0),
9464 MachinePointerInfo(Arg1));
9465 if (Res.first.getNode()) {
9466 processIntegerCallValue(I, Res.first, true);
9467 PendingLoads.push_back(Res.second);
9468 return true;
9469 }
9470
9471 return false;
9472}
9473
9474/// See if we can lower a strlen call into an optimized form. If so, return
9475/// true and lower it, otherwise return false and it will be lowered like a
9476/// normal call.
9477/// The caller already checked that \p I calls the appropriate LibFunc with a
9478/// correct prototype.
9479bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9480 const Value *Arg0 = I.getArgOperand(0);
9481
9482 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9483 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrlen(
9484 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), &I);
9485 if (Res.first.getNode()) {
9486 processIntegerCallValue(I, Res.first, false);
9487 PendingLoads.push_back(Res.second);
9488 return true;
9489 }
9490
9491 return false;
9492}
9493
9494/// See if we can lower a strnlen call into an optimized form. If so, return
9495/// true and lower it, otherwise return false and it will be lowered like a
9496/// normal call.
9497/// The caller already checked that \p I calls the appropriate LibFunc with a
9498/// correct prototype.
9499bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9500 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9501
9502 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9503 std::pair<SDValue, SDValue> Res =
9504 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9505 getValue(Arg0), getValue(Arg1),
9506 MachinePointerInfo(Arg0));
9507 if (Res.first.getNode()) {
9508 processIntegerCallValue(I, Res.first, false);
9509 PendingLoads.push_back(Res.second);
9510 return true;
9511 }
9512
9513 return false;
9514}
9515
9516/// See if we can lower a Strstr call into an optimized form. If so, return
9517/// true and lower it, otherwise return false and it will be lowered like a
9518/// normal call.
9519/// The caller already checked that \p I calls the appropriate LibFunc with a
9520/// correct prototype.
9521bool SelectionDAGBuilder::visitStrstrCall(const CallInst &I) {
9522 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9523 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9524 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForStrstr(
9525 DAG, getCurSDLoc(), DAG.getRoot(), getValue(Arg0), getValue(Arg1), &I);
9526 if (Res.first) {
9527 processIntegerCallValue(I, Res.first, false);
9528 PendingLoads.push_back(Res.second);
9529 return true;
9530 }
9531 return false;
9532}
9533
9534/// See if we can lower a unary floating-point operation into an SDNode with
9535/// the specified Opcode. If so, return true and lower it, otherwise return
9536/// false and it will be lowered like a normal call.
9537/// The caller already checked that \p I calls the appropriate LibFunc with a
9538/// correct prototype.
9539bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9540 unsigned Opcode) {
9541 // We already checked this call's prototype; verify it doesn't modify errno.
9542 // Do not perform optimizations for call sites that require strict
9543 // floating-point semantics.
9544 if (!I.onlyReadsMemory() || I.isStrictFP())
9545 return false;
9546
9547 SDNodeFlags Flags;
9548 Flags.copyFMF(cast<FPMathOperator>(I));
9549
9550 SDValue Tmp = getValue(I.getArgOperand(0));
9551 setValue(&I,
9552 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9553 return true;
9554}
9555
9556/// See if we can lower a binary floating-point operation into an SDNode with
9557/// the specified Opcode. If so, return true and lower it. Otherwise return
9558/// false, and it will be lowered like a normal call.
9559/// The caller already checked that \p I calls the appropriate LibFunc with a
9560/// correct prototype.
9561bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9562 unsigned Opcode) {
9563 // We already checked this call's prototype; verify it doesn't modify errno.
9564 // Do not perform optimizations for call sites that require strict
9565 // floating-point semantics.
9566 if (!I.onlyReadsMemory() || I.isStrictFP())
9567 return false;
9568
9569 SDNodeFlags Flags;
9570 Flags.copyFMF(cast<FPMathOperator>(I));
9571
9572 SDValue Tmp0 = getValue(I.getArgOperand(0));
9573 SDValue Tmp1 = getValue(I.getArgOperand(1));
9574 EVT VT = Tmp0.getValueType();
9575 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9576 return true;
9577}
9578
9579void SelectionDAGBuilder::visitCall(const CallInst &I) {
9580 // Handle inline assembly differently.
9581 if (I.isInlineAsm()) {
9582 visitInlineAsm(I);
9583 return;
9584 }
9585
9587
9588 if (Function *F = I.getCalledFunction()) {
9589 if (F->isDeclaration()) {
9590 // Is this an LLVM intrinsic?
9591 if (unsigned IID = F->getIntrinsicID()) {
9592 visitIntrinsicCall(I, IID);
9593 return;
9594 }
9595 }
9596
9597 // Check for well-known libc/libm calls. If the function is internal, it
9598 // can't be a library call. Don't do the check if marked as nobuiltin for
9599 // some reason.
9600 // This code should not handle libcalls that are already canonicalized to
9601 // intrinsics by the middle-end.
9602 LibFunc Func;
9603 if (!I.isNoBuiltin() && !F->hasLocalLinkage() && F->hasName() &&
9604 LibInfo->getLibFunc(*F, Func) && LibInfo->hasOptimizedCodeGen(Func)) {
9605 switch (Func) {
9606 default: break;
9607 case LibFunc_bcmp:
9608 if (visitMemCmpBCmpCall(I))
9609 return;
9610 break;
9611 case LibFunc_copysign:
9612 case LibFunc_copysignf:
9613 case LibFunc_copysignl:
9614 // We already checked this call's prototype; verify it doesn't modify
9615 // errno.
9616 if (I.onlyReadsMemory()) {
9617 SDValue LHS = getValue(I.getArgOperand(0));
9618 SDValue RHS = getValue(I.getArgOperand(1));
9620 LHS.getValueType(), LHS, RHS));
9621 return;
9622 }
9623 break;
9624 case LibFunc_fabs:
9625 case LibFunc_fabsf:
9626 case LibFunc_fabsl:
9627 // TODO: Remove this, already canonicalized by the middle-end.
9628 if (visitUnaryFloatCall(I, ISD::FABS))
9629 return;
9630 break;
9631 case LibFunc_sin:
9632 case LibFunc_sinf:
9633 case LibFunc_sinl:
9634 if (visitUnaryFloatCall(I, ISD::FSIN))
9635 return;
9636 break;
9637 case LibFunc_cos:
9638 case LibFunc_cosf:
9639 case LibFunc_cosl:
9640 if (visitUnaryFloatCall(I, ISD::FCOS))
9641 return;
9642 break;
9643 case LibFunc_tan:
9644 case LibFunc_tanf:
9645 case LibFunc_tanl:
9646 if (visitUnaryFloatCall(I, ISD::FTAN))
9647 return;
9648 break;
9649 case LibFunc_asin:
9650 case LibFunc_asinf:
9651 case LibFunc_asinl:
9652 if (visitUnaryFloatCall(I, ISD::FASIN))
9653 return;
9654 break;
9655 case LibFunc_acos:
9656 case LibFunc_acosf:
9657 case LibFunc_acosl:
9658 if (visitUnaryFloatCall(I, ISD::FACOS))
9659 return;
9660 break;
9661 case LibFunc_atan:
9662 case LibFunc_atanf:
9663 case LibFunc_atanl:
9664 if (visitUnaryFloatCall(I, ISD::FATAN))
9665 return;
9666 break;
9667 case LibFunc_atan2:
9668 case LibFunc_atan2f:
9669 case LibFunc_atan2l:
9670 if (visitBinaryFloatCall(I, ISD::FATAN2))
9671 return;
9672 break;
9673 case LibFunc_sinh:
9674 case LibFunc_sinhf:
9675 case LibFunc_sinhl:
9676 if (visitUnaryFloatCall(I, ISD::FSINH))
9677 return;
9678 break;
9679 case LibFunc_cosh:
9680 case LibFunc_coshf:
9681 case LibFunc_coshl:
9682 if (visitUnaryFloatCall(I, ISD::FCOSH))
9683 return;
9684 break;
9685 case LibFunc_tanh:
9686 case LibFunc_tanhf:
9687 case LibFunc_tanhl:
9688 if (visitUnaryFloatCall(I, ISD::FTANH))
9689 return;
9690 break;
9691 case LibFunc_sqrt:
9692 case LibFunc_sqrtf:
9693 case LibFunc_sqrtl:
9694 case LibFunc_sqrt_finite:
9695 case LibFunc_sqrtf_finite:
9696 case LibFunc_sqrtl_finite:
9697 if (visitUnaryFloatCall(I, ISD::FSQRT))
9698 return;
9699 break;
9700 case LibFunc_log2:
9701 case LibFunc_log2f:
9702 case LibFunc_log2l:
9703 if (visitUnaryFloatCall(I, ISD::FLOG2))
9704 return;
9705 break;
9706 case LibFunc_exp2:
9707 case LibFunc_exp2f:
9708 case LibFunc_exp2l:
9709 if (visitUnaryFloatCall(I, ISD::FEXP2))
9710 return;
9711 break;
9712 case LibFunc_exp10:
9713 case LibFunc_exp10f:
9714 case LibFunc_exp10l:
9715 if (visitUnaryFloatCall(I, ISD::FEXP10))
9716 return;
9717 break;
9718 case LibFunc_ldexp:
9719 case LibFunc_ldexpf:
9720 case LibFunc_ldexpl:
9721 if (visitBinaryFloatCall(I, ISD::FLDEXP))
9722 return;
9723 break;
9724 case LibFunc_strstr:
9725 if (visitStrstrCall(I))
9726 return;
9727 break;
9728 case LibFunc_memcmp:
9729 if (visitMemCmpBCmpCall(I))
9730 return;
9731 break;
9732 case LibFunc_mempcpy:
9733 if (visitMemPCpyCall(I))
9734 return;
9735 break;
9736 case LibFunc_memchr:
9737 if (visitMemChrCall(I))
9738 return;
9739 break;
9740 case LibFunc_strcpy:
9741 if (visitStrCpyCall(I, false))
9742 return;
9743 break;
9744 case LibFunc_stpcpy:
9745 if (visitStrCpyCall(I, true))
9746 return;
9747 break;
9748 case LibFunc_strcmp:
9749 if (visitStrCmpCall(I))
9750 return;
9751 break;
9752 case LibFunc_strlen:
9753 if (visitStrLenCall(I))
9754 return;
9755 break;
9756 case LibFunc_strnlen:
9757 if (visitStrNLenCall(I))
9758 return;
9759 break;
9760 }
9761 }
9762 }
9763
9764 if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9765 LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9766 return;
9767 }
9768
9769 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9770 // have to do anything here to lower funclet bundles.
9771 // CFGuardTarget bundles are lowered in LowerCallTo.
9773 I, "calls",
9778
9779 SDValue Callee = getValue(I.getCalledOperand());
9780
9781 if (I.hasDeoptState())
9782 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9783 else
9784 // Check if we can potentially perform a tail call. More detailed checking
9785 // is be done within LowerCallTo, after more information about the call is
9786 // known.
9787 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9788}
9789
9791 const CallBase &CB, const BasicBlock *EHPadBB) {
9792 auto PAB = CB.getOperandBundle("ptrauth");
9793 const Value *CalleeV = CB.getCalledOperand();
9794
9795 // Gather the call ptrauth data from the operand bundle:
9796 // [ i32 <key>, i64 <discriminator> ]
9797 const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9798 const Value *Discriminator = PAB->Inputs[1];
9799
9800 assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9801 assert(Discriminator->getType()->isIntegerTy(64) &&
9802 "Invalid ptrauth discriminator");
9803
9804 // Look through ptrauth constants to find the raw callee.
9805 // Do a direct unauthenticated call if we found it and everything matches.
9806 if (const auto *CalleeCPA = dyn_cast<ConstantPtrAuth>(CalleeV))
9807 if (CalleeCPA->isKnownCompatibleWith(Key, Discriminator,
9808 DAG.getDataLayout()))
9809 return LowerCallTo(CB, getValue(CalleeCPA->getPointer()), CB.isTailCall(),
9810 CB.isMustTailCall(), EHPadBB);
9811
9812 // Functions should never be ptrauth-called directly.
9813 assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9814
9815 // Otherwise, do an authenticated indirect call.
9816 TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9817 getValue(Discriminator)};
9818
9819 LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9820 EHPadBB, &PAI);
9821}
9822
9823namespace {
9824
9825/// AsmOperandInfo - This contains information for each constraint that we are
9826/// lowering.
9827class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9828public:
9829 /// CallOperand - If this is the result output operand or a clobber
9830 /// this is null, otherwise it is the incoming operand to the CallInst.
9831 /// This gets modified as the asm is processed.
9832 SDValue CallOperand;
9833
9834 /// AssignedRegs - If this is a register or register class operand, this
9835 /// contains the set of register corresponding to the operand.
9836 RegsForValue AssignedRegs;
9837
9838 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9839 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9840 }
9841
9842 /// Whether or not this operand accesses memory
9843 bool hasMemory(const TargetLowering &TLI) const {
9844 // Indirect operand accesses access memory.
9845 if (isIndirect)
9846 return true;
9847
9848 for (const auto &Code : Codes)
9850 return true;
9851
9852 return false;
9853 }
9854};
9855
9856
9857} // end anonymous namespace
9858
9859/// Make sure that the output operand \p OpInfo and its corresponding input
9860/// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9861/// out).
9862static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9863 SDISelAsmOperandInfo &MatchingOpInfo,
9864 SelectionDAG &DAG) {
9865 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9866 return;
9867
9869 const auto &TLI = DAG.getTargetLoweringInfo();
9870
9871 std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9872 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9873 OpInfo.ConstraintVT);
9874 std::pair<unsigned, const TargetRegisterClass *> InputRC =
9875 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9876 MatchingOpInfo.ConstraintVT);
9877 const bool OutOpIsIntOrFP =
9878 OpInfo.ConstraintVT.isInteger() || OpInfo.ConstraintVT.isFloatingPoint();
9879 const bool InOpIsIntOrFP = MatchingOpInfo.ConstraintVT.isInteger() ||
9880 MatchingOpInfo.ConstraintVT.isFloatingPoint();
9881 if ((OutOpIsIntOrFP != InOpIsIntOrFP) || (MatchRC.second != InputRC.second)) {
9882 // FIXME: error out in a more elegant fashion
9883 report_fatal_error("Unsupported asm: input constraint"
9884 " with a matching output constraint of"
9885 " incompatible type!");
9886 }
9887 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9888}
9889
9890/// Get a direct memory input to behave well as an indirect operand.
9891/// This may introduce stores, hence the need for a \p Chain.
9892/// \return The (possibly updated) chain.
9893static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9894 SDISelAsmOperandInfo &OpInfo,
9895 SelectionDAG &DAG) {
9896 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9897
9898 // If we don't have an indirect input, put it in the constpool if we can,
9899 // otherwise spill it to a stack slot.
9900 // TODO: This isn't quite right. We need to handle these according to
9901 // the addressing mode that the constraint wants. Also, this may take
9902 // an additional register for the computation and we don't want that
9903 // either.
9904
9905 // If the operand is a float, integer, or vector constant, spill to a
9906 // constant pool entry to get its address.
9907 const Value *OpVal = OpInfo.CallOperandVal;
9908 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9910 OpInfo.CallOperand = DAG.getConstantPool(
9911 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9912 return Chain;
9913 }
9914
9915 // Otherwise, create a stack slot and emit a store to it before the asm.
9916 Type *Ty = OpVal->getType();
9917 auto &DL = DAG.getDataLayout();
9918 TypeSize TySize = DL.getTypeAllocSize(Ty);
9921 int StackID = 0;
9922 if (TySize.isScalable())
9923 StackID = TFI->getStackIDForScalableVectors();
9924 int SSFI = MF.getFrameInfo().CreateStackObject(TySize.getKnownMinValue(),
9925 DL.getPrefTypeAlign(Ty), false,
9926 nullptr, StackID);
9927 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9928 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9930 TLI.getMemValueType(DL, Ty));
9931 OpInfo.CallOperand = StackSlot;
9932
9933 return Chain;
9934}
9935
9936/// GetRegistersForValue - Assign registers (virtual or physical) for the
9937/// specified operand. We prefer to assign virtual registers, to allow the
9938/// register allocator to handle the assignment process. However, if the asm
9939/// uses features that we can't model on machineinstrs, we have SDISel do the
9940/// allocation. This produces generally horrible, but correct, code.
9941///
9942/// OpInfo describes the operand
9943/// RefOpInfo describes the matching operand if any, the operand otherwise
9944static std::optional<unsigned>
9946 SDISelAsmOperandInfo &OpInfo,
9947 SDISelAsmOperandInfo &RefOpInfo) {
9948 LLVMContext &Context = *DAG.getContext();
9949 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9950
9954
9955 // No work to do for memory/address operands.
9956 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9957 OpInfo.ConstraintType == TargetLowering::C_Address)
9958 return std::nullopt;
9959
9960 // If this is a constraint for a single physreg, or a constraint for a
9961 // register class, find it.
9962 unsigned AssignedReg;
9963 const TargetRegisterClass *RC;
9964 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
9965 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
9966 // RC is unset only on failure. Return immediately.
9967 if (!RC)
9968 return std::nullopt;
9969
9970 // Get the actual register value type. This is important, because the user
9971 // may have asked for (e.g.) the AX register in i32 type. We need to
9972 // remember that AX is actually i16 to get the right extension.
9973 const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
9974
9975 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
9976 // If this is an FP operand in an integer register (or visa versa), or more
9977 // generally if the operand value disagrees with the register class we plan
9978 // to stick it in, fix the operand type.
9979 //
9980 // If this is an input value, the bitcast to the new type is done now.
9981 // Bitcast for output value is done at the end of visitInlineAsm().
9982 if ((OpInfo.Type == InlineAsm::isOutput ||
9983 OpInfo.Type == InlineAsm::isInput) &&
9984 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
9985 // Try to convert to the first EVT that the reg class contains. If the
9986 // types are identical size, use a bitcast to convert (e.g. two differing
9987 // vector types). Note: output bitcast is done at the end of
9988 // visitInlineAsm().
9989 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
9990 // Exclude indirect inputs while they are unsupported because the code
9991 // to perform the load is missing and thus OpInfo.CallOperand still
9992 // refers to the input address rather than the pointed-to value.
9993 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
9994 OpInfo.CallOperand =
9995 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
9996 OpInfo.ConstraintVT = RegVT;
9997 // If the operand is an FP value and we want it in integer registers,
9998 // use the corresponding integer type. This turns an f64 value into
9999 // i64, which can be passed with two i32 values on a 32-bit machine.
10000 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
10001 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
10002 if (OpInfo.Type == InlineAsm::isInput)
10003 OpInfo.CallOperand =
10004 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
10005 OpInfo.ConstraintVT = VT;
10006 }
10007 }
10008 }
10009
10010 // No need to allocate a matching input constraint since the constraint it's
10011 // matching to has already been allocated.
10012 if (OpInfo.isMatchingInputConstraint())
10013 return std::nullopt;
10014
10015 EVT ValueVT = OpInfo.ConstraintVT;
10016 if (OpInfo.ConstraintVT == MVT::Other)
10017 ValueVT = RegVT;
10018
10019 // Initialize NumRegs.
10020 unsigned NumRegs = 1;
10021 if (OpInfo.ConstraintVT != MVT::Other)
10022 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
10023
10024 // If this is a constraint for a specific physical register, like {r17},
10025 // assign it now.
10026
10027 // If this associated to a specific register, initialize iterator to correct
10028 // place. If virtual, make sure we have enough registers
10029
10030 // Initialize iterator if necessary
10033
10034 // Do not check for single registers.
10035 if (AssignedReg) {
10036 I = std::find(I, RC->end(), AssignedReg);
10037 if (I == RC->end()) {
10038 // RC does not contain the selected register, which indicates a
10039 // mismatch between the register and the required type/bitwidth.
10040 return {AssignedReg};
10041 }
10042 }
10043
10044 for (; NumRegs; --NumRegs, ++I) {
10045 assert(I != RC->end() && "Ran out of registers to allocate!");
10046 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
10047 Regs.push_back(R);
10048 }
10049
10050 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
10051 return std::nullopt;
10052}
10053
10054static unsigned
10056 const std::vector<SDValue> &AsmNodeOperands) {
10057 // Scan until we find the definition we already emitted of this operand.
10058 unsigned CurOp = InlineAsm::Op_FirstOperand;
10059 for (; OperandNo; --OperandNo) {
10060 // Advance to the next operand.
10061 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
10062 const InlineAsm::Flag F(OpFlag);
10063 assert(
10064 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
10065 "Skipped past definitions?");
10066 CurOp += F.getNumOperandRegisters() + 1;
10067 }
10068 return CurOp;
10069}
10070
10071namespace {
10072
10073class ExtraFlags {
10074 unsigned Flags = 0;
10075
10076public:
10077 explicit ExtraFlags(const CallBase &Call) {
10078 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10079 if (IA->hasSideEffects())
10081 if (IA->isAlignStack())
10083 if (IA->canThrow())
10085 if (Call.isConvergent())
10087 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
10088 }
10089
10090 void update(const TargetLowering::AsmOperandInfo &OpInfo) {
10091 // Ideally, we would only check against memory constraints. However, the
10092 // meaning of an Other constraint can be target-specific and we can't easily
10093 // reason about it. Therefore, be conservative and set MayLoad/MayStore
10094 // for Other constraints as well.
10097 if (OpInfo.Type == InlineAsm::isInput)
10099 else if (OpInfo.Type == InlineAsm::isOutput)
10101 else if (OpInfo.Type == InlineAsm::isClobber)
10103 }
10104 }
10105
10106 unsigned get() const { return Flags; }
10107};
10108
10109} // end anonymous namespace
10110
10111static bool isFunction(SDValue Op) {
10112 if (Op && Op.getOpcode() == ISD::GlobalAddress) {
10113 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
10114 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
10115
10116 // In normal "call dllimport func" instruction (non-inlineasm) it force
10117 // indirect access by specifing call opcode. And usually specially print
10118 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
10119 // not do in this way now. (In fact, this is similar with "Data Access"
10120 // action). So here we ignore dllimport function.
10121 if (Fn && !Fn->hasDLLImportStorageClass())
10122 return true;
10123 }
10124 }
10125 return false;
10126}
10127
10128/// visitInlineAsm - Handle a call to an InlineAsm object.
10129void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
10130 const BasicBlock *EHPadBB) {
10131 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
10132
10133 /// ConstraintOperands - Information about all of the constraints.
10134 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
10135
10136 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10138 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
10139
10140 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
10141 // AsmDialect, MayLoad, MayStore).
10142 bool HasSideEffect = IA->hasSideEffects();
10143 ExtraFlags ExtraInfo(Call);
10144
10145 for (auto &T : TargetConstraints) {
10146 ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
10147 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
10148
10149 if (OpInfo.CallOperandVal)
10150 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
10151
10152 if (!HasSideEffect)
10153 HasSideEffect = OpInfo.hasMemory(TLI);
10154
10155 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
10156 // FIXME: Could we compute this on OpInfo rather than T?
10157
10158 // Compute the constraint code and ConstraintType to use.
10160
10161 if (T.ConstraintType == TargetLowering::C_Immediate &&
10162 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
10163 // We've delayed emitting a diagnostic like the "n" constraint because
10164 // inlining could cause an integer showing up.
10165 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
10166 "' expects an integer constant "
10167 "expression");
10168
10169 ExtraInfo.update(T);
10170 }
10171
10172 // We won't need to flush pending loads if this asm doesn't touch
10173 // memory and is nonvolatile.
10174 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
10175
10176 bool EmitEHLabels = isa<InvokeInst>(Call);
10177 if (EmitEHLabels) {
10178 assert(EHPadBB && "InvokeInst must have an EHPadBB");
10179 }
10180 bool IsCallBr = isa<CallBrInst>(Call);
10181
10182 if (IsCallBr || EmitEHLabels) {
10183 // If this is a callbr or invoke we need to flush pending exports since
10184 // inlineasm_br and invoke are terminators.
10185 // We need to do this before nodes are glued to the inlineasm_br node.
10186 Chain = getControlRoot();
10187 }
10188
10189 MCSymbol *BeginLabel = nullptr;
10190 if (EmitEHLabels) {
10191 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
10192 }
10193
10194 int OpNo = -1;
10195 SmallVector<StringRef> AsmStrs;
10196 IA->collectAsmStrs(AsmStrs);
10197
10198 // Second pass over the constraints: compute which constraint option to use.
10199 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10200 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
10201 OpNo++;
10202
10203 // If this is an output operand with a matching input operand, look up the
10204 // matching input. If their types mismatch, e.g. one is an integer, the
10205 // other is floating point, or their sizes are different, flag it as an
10206 // error.
10207 if (OpInfo.hasMatchingInput()) {
10208 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
10209 patchMatchingInput(OpInfo, Input, DAG);
10210 }
10211
10212 // Compute the constraint code and ConstraintType to use.
10213 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
10214
10215 if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
10216 OpInfo.Type == InlineAsm::isClobber) ||
10217 OpInfo.ConstraintType == TargetLowering::C_Address)
10218 continue;
10219
10220 // In Linux PIC model, there are 4 cases about value/label addressing:
10221 //
10222 // 1: Function call or Label jmp inside the module.
10223 // 2: Data access (such as global variable, static variable) inside module.
10224 // 3: Function call or Label jmp outside the module.
10225 // 4: Data access (such as global variable) outside the module.
10226 //
10227 // Due to current llvm inline asm architecture designed to not "recognize"
10228 // the asm code, there are quite troubles for us to treat mem addressing
10229 // differently for same value/adress used in different instuctions.
10230 // For example, in pic model, call a func may in plt way or direclty
10231 // pc-related, but lea/mov a function adress may use got.
10232 //
10233 // Here we try to "recognize" function call for the case 1 and case 3 in
10234 // inline asm. And try to adjust the constraint for them.
10235 //
10236 // TODO: Due to current inline asm didn't encourage to jmp to the outsider
10237 // label, so here we don't handle jmp function label now, but we need to
10238 // enhance it (especilly in PIC model) if we meet meaningful requirements.
10239 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
10240 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
10241 TM.getCodeModel() != CodeModel::Large) {
10242 OpInfo.isIndirect = false;
10243 OpInfo.ConstraintType = TargetLowering::C_Address;
10244 }
10245
10246 // If this is a memory input, and if the operand is not indirect, do what we
10247 // need to provide an address for the memory input.
10248 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
10249 !OpInfo.isIndirect) {
10250 assert((OpInfo.isMultipleAlternative ||
10251 (OpInfo.Type == InlineAsm::isInput)) &&
10252 "Can only indirectify direct input operands!");
10253
10254 // Memory operands really want the address of the value.
10255 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
10256
10257 // There is no longer a Value* corresponding to this operand.
10258 OpInfo.CallOperandVal = nullptr;
10259
10260 // It is now an indirect operand.
10261 OpInfo.isIndirect = true;
10262 }
10263
10264 }
10265
10266 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
10267 std::vector<SDValue> AsmNodeOperands;
10268 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
10269 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
10270 IA->getAsmString().data(), TLI.getProgramPointerTy(DAG.getDataLayout())));
10271
10272 // If we have a !srcloc metadata node associated with it, we want to attach
10273 // this to the ultimately generated inline asm machineinstr. To do this, we
10274 // pass in the third operand as this (potentially null) inline asm MDNode.
10275 const MDNode *SrcLoc = Call.getMetadata("srcloc");
10276 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
10277
10278 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
10279 // bits as operand 3.
10280 AsmNodeOperands.push_back(DAG.getTargetConstant(
10281 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10282
10283 // Third pass: Loop over operands to prepare DAG-level operands.. As part of
10284 // this, assign virtual and physical registers for inputs and otput.
10285 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10286 // Assign Registers.
10287 SDISelAsmOperandInfo &RefOpInfo =
10288 OpInfo.isMatchingInputConstraint()
10289 ? ConstraintOperands[OpInfo.getMatchedOperand()]
10290 : OpInfo;
10291 const auto RegError =
10292 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
10293 if (RegError) {
10294 const MachineFunction &MF = DAG.getMachineFunction();
10295 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10296 const char *RegName = TRI.getName(*RegError);
10297 emitInlineAsmError(Call, "register '" + Twine(RegName) +
10298 "' allocated for constraint '" +
10299 Twine(OpInfo.ConstraintCode) +
10300 "' does not match required type");
10301 return;
10302 }
10303
10304 auto DetectWriteToReservedRegister = [&]() {
10305 const MachineFunction &MF = DAG.getMachineFunction();
10306 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10307 for (Register Reg : OpInfo.AssignedRegs.Regs) {
10308 if (Reg.isPhysical() && TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
10309 const char *RegName = TRI.getName(Reg);
10310 emitInlineAsmError(Call, "write to reserved register '" +
10311 Twine(RegName) + "'");
10312 return true;
10313 }
10314 }
10315 return false;
10316 };
10317 assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
10318 (OpInfo.Type == InlineAsm::isInput &&
10319 !OpInfo.isMatchingInputConstraint())) &&
10320 "Only address as input operand is allowed.");
10321
10322 switch (OpInfo.Type) {
10324 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10325 const InlineAsm::ConstraintCode ConstraintID =
10326 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10328 "Failed to convert memory constraint code to constraint id.");
10329
10330 // Add information to the INLINEASM node to know about this output.
10331 InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1);
10332 OpFlags.setMemConstraint(ConstraintID);
10333 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
10334 MVT::i32));
10335 AsmNodeOperands.push_back(OpInfo.CallOperand);
10336 } else {
10337 // Otherwise, this outputs to a register (directly for C_Register /
10338 // C_RegisterClass, and a target-defined fashion for
10339 // C_Immediate/C_Other). Find a register that we can use.
10340 if (OpInfo.AssignedRegs.Regs.empty()) {
10341 emitInlineAsmError(
10342 Call, "couldn't allocate output register for constraint '" +
10343 Twine(OpInfo.ConstraintCode) + "'");
10344 return;
10345 }
10346
10347 if (DetectWriteToReservedRegister())
10348 return;
10349
10350 // Add information to the INLINEASM node to know that this register is
10351 // set.
10352 OpInfo.AssignedRegs.AddInlineAsmOperands(
10353 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
10355 false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
10356 }
10357 break;
10358
10359 case InlineAsm::isInput:
10360 case InlineAsm::isLabel: {
10361 SDValue InOperandVal = OpInfo.CallOperand;
10362
10363 if (OpInfo.isMatchingInputConstraint()) {
10364 // If this is required to match an output register we have already set,
10365 // just use its register.
10366 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
10367 AsmNodeOperands);
10368 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
10369 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
10370 if (OpInfo.isIndirect) {
10371 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
10372 emitInlineAsmError(Call, "inline asm not supported yet: "
10373 "don't know how to handle tied "
10374 "indirect register inputs");
10375 return;
10376 }
10377
10379 MachineFunction &MF = DAG.getMachineFunction();
10380 MachineRegisterInfo &MRI = MF.getRegInfo();
10381 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
10382 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
10383 Register TiedReg = R->getReg();
10384 MVT RegVT = R->getSimpleValueType(0);
10385 const TargetRegisterClass *RC =
10386 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg)
10387 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
10388 : TRI.getMinimalPhysRegClass(TiedReg);
10389 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
10390 Regs.push_back(MRI.createVirtualRegister(RC));
10391
10392 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
10393
10394 SDLoc dl = getCurSDLoc();
10395 // Use the produced MatchedRegs object to
10396 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
10397 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
10398 OpInfo.getMatchedOperand(), dl, DAG,
10399 AsmNodeOperands);
10400 break;
10401 }
10402
10403 assert(Flag.isMemKind() && "Unknown matching constraint!");
10404 assert(Flag.getNumOperandRegisters() == 1 &&
10405 "Unexpected number of operands");
10406 // Add information to the INLINEASM node to know about this input.
10407 // See InlineAsm.h isUseOperandTiedToDef.
10408 Flag.clearMemConstraint();
10409 Flag.setMatchingOp(OpInfo.getMatchedOperand());
10410 AsmNodeOperands.push_back(DAG.getTargetConstant(
10411 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10412 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
10413 break;
10414 }
10415
10416 // Treat indirect 'X' constraint as memory.
10417 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
10418 OpInfo.isIndirect)
10419 OpInfo.ConstraintType = TargetLowering::C_Memory;
10420
10421 if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
10422 OpInfo.ConstraintType == TargetLowering::C_Other) {
10423 std::vector<SDValue> Ops;
10424 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
10425 Ops, DAG);
10426 if (Ops.empty()) {
10427 if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
10428 if (isa<ConstantSDNode>(InOperandVal)) {
10429 emitInlineAsmError(Call, "value out of range for constraint '" +
10430 Twine(OpInfo.ConstraintCode) + "'");
10431 return;
10432 }
10433
10434 emitInlineAsmError(Call,
10435 "invalid operand for inline asm constraint '" +
10436 Twine(OpInfo.ConstraintCode) + "'");
10437 return;
10438 }
10439
10440 // Add information to the INLINEASM node to know about this input.
10441 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
10442 AsmNodeOperands.push_back(DAG.getTargetConstant(
10443 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
10444 llvm::append_range(AsmNodeOperands, Ops);
10445 break;
10446 }
10447
10448 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10449 assert((OpInfo.isIndirect ||
10450 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10451 "Operand must be indirect to be a mem!");
10452 assert(InOperandVal.getValueType() ==
10453 TLI.getPointerTy(DAG.getDataLayout()) &&
10454 "Memory operands expect pointer values");
10455
10456 const InlineAsm::ConstraintCode ConstraintID =
10457 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10459 "Failed to convert memory constraint code to constraint id.");
10460
10461 // Add information to the INLINEASM node to know about this input.
10462 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10463 ResOpType.setMemConstraint(ConstraintID);
10464 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10465 getCurSDLoc(),
10466 MVT::i32));
10467 AsmNodeOperands.push_back(InOperandVal);
10468 break;
10469 }
10470
10471 if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10472 const InlineAsm::ConstraintCode ConstraintID =
10473 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10475 "Failed to convert memory constraint code to constraint id.");
10476
10477 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10478
10479 SDValue AsmOp = InOperandVal;
10480 if (isFunction(InOperandVal)) {
10481 auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10482 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10483 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10484 InOperandVal.getValueType(),
10485 GA->getOffset());
10486 }
10487
10488 // Add information to the INLINEASM node to know about this input.
10489 ResOpType.setMemConstraint(ConstraintID);
10490
10491 AsmNodeOperands.push_back(
10492 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10493
10494 AsmNodeOperands.push_back(AsmOp);
10495 break;
10496 }
10497
10498 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10499 OpInfo.ConstraintType != TargetLowering::C_Register) {
10500 emitInlineAsmError(Call, "unknown asm constraint '" +
10501 Twine(OpInfo.ConstraintCode) + "'");
10502 return;
10503 }
10504
10505 // TODO: Support this.
10506 if (OpInfo.isIndirect) {
10507 emitInlineAsmError(
10508 Call, "Don't know how to handle indirect register inputs yet "
10509 "for constraint '" +
10510 Twine(OpInfo.ConstraintCode) + "'");
10511 return;
10512 }
10513
10514 // Copy the input into the appropriate registers.
10515 if (OpInfo.AssignedRegs.Regs.empty()) {
10516 emitInlineAsmError(Call,
10517 "couldn't allocate input reg for constraint '" +
10518 Twine(OpInfo.ConstraintCode) + "'");
10519 return;
10520 }
10521
10522 if (DetectWriteToReservedRegister())
10523 return;
10524
10525 SDLoc dl = getCurSDLoc();
10526
10527 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10528 &Call);
10529
10530 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10531 0, dl, DAG, AsmNodeOperands);
10532 break;
10533 }
10535 // Add the clobbered value to the operand list, so that the register
10536 // allocator is aware that the physreg got clobbered.
10537 if (!OpInfo.AssignedRegs.Regs.empty())
10539 false, 0, getCurSDLoc(), DAG,
10540 AsmNodeOperands);
10541 break;
10542 }
10543 }
10544
10545 // Finish up input operands. Set the input chain and add the flag last.
10546 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10547 if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10548
10549 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10550 Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10551 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10552 Glue = Chain.getValue(1);
10553
10554 // Do additional work to generate outputs.
10555
10556 SmallVector<EVT, 1> ResultVTs;
10557 SmallVector<SDValue, 1> ResultValues;
10558 SmallVector<SDValue, 8> OutChains;
10559
10560 llvm::Type *CallResultType = Call.getType();
10561 ArrayRef<Type *> ResultTypes;
10562 if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10563 ResultTypes = StructResult->elements();
10564 else if (!CallResultType->isVoidTy())
10565 ResultTypes = ArrayRef(CallResultType);
10566
10567 auto CurResultType = ResultTypes.begin();
10568 auto handleRegAssign = [&](SDValue V) {
10569 assert(CurResultType != ResultTypes.end() && "Unexpected value");
10570 assert((*CurResultType)->isSized() && "Unexpected unsized type");
10571 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10572 ++CurResultType;
10573 // If the type of the inline asm call site return value is different but has
10574 // same size as the type of the asm output bitcast it. One example of this
10575 // is for vectors with different width / number of elements. This can
10576 // happen for register classes that can contain multiple different value
10577 // types. The preg or vreg allocated may not have the same VT as was
10578 // expected.
10579 //
10580 // This can also happen for a return value that disagrees with the register
10581 // class it is put in, eg. a double in a general-purpose register on a
10582 // 32-bit machine.
10583 if (ResultVT != V.getValueType() &&
10584 ResultVT.getSizeInBits() == V.getValueSizeInBits())
10585 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10586 else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10587 V.getValueType().isInteger()) {
10588 // If a result value was tied to an input value, the computed result
10589 // may have a wider width than the expected result. Extract the
10590 // relevant portion.
10591 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10592 }
10593 assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10594 ResultVTs.push_back(ResultVT);
10595 ResultValues.push_back(V);
10596 };
10597
10598 // Deal with output operands.
10599 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10600 if (OpInfo.Type == InlineAsm::isOutput) {
10601 SDValue Val;
10602 // Skip trivial output operands.
10603 if (OpInfo.AssignedRegs.Regs.empty())
10604 continue;
10605
10606 switch (OpInfo.ConstraintType) {
10609 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10610 Chain, &Glue, &Call);
10611 break;
10614 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10615 OpInfo, DAG);
10616 break;
10618 break; // Already handled.
10620 break; // Silence warning.
10622 assert(false && "Unexpected unknown constraint");
10623 }
10624
10625 // Indirect output manifest as stores. Record output chains.
10626 if (OpInfo.isIndirect) {
10627 const Value *Ptr = OpInfo.CallOperandVal;
10628 assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10629 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10630 MachinePointerInfo(Ptr));
10631 OutChains.push_back(Store);
10632 } else {
10633 // generate CopyFromRegs to associated registers.
10634 assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10635 if (Val.getOpcode() == ISD::MERGE_VALUES) {
10636 for (const SDValue &V : Val->op_values())
10637 handleRegAssign(V);
10638 } else
10639 handleRegAssign(Val);
10640 }
10641 }
10642 }
10643
10644 // Set results.
10645 if (!ResultValues.empty()) {
10646 assert(CurResultType == ResultTypes.end() &&
10647 "Mismatch in number of ResultTypes");
10648 assert(ResultValues.size() == ResultTypes.size() &&
10649 "Mismatch in number of output operands in asm result");
10650
10652 DAG.getVTList(ResultVTs), ResultValues);
10653 setValue(&Call, V);
10654 }
10655
10656 // Collect store chains.
10657 if (!OutChains.empty())
10658 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10659
10660 if (EmitEHLabels) {
10661 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10662 }
10663
10664 // Only Update Root if inline assembly has a memory effect.
10665 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10666 EmitEHLabels)
10667 DAG.setRoot(Chain);
10668}
10669
10670void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10671 const Twine &Message) {
10672 LLVMContext &Ctx = *DAG.getContext();
10673 Ctx.diagnose(DiagnosticInfoInlineAsm(Call, Message));
10674
10675 // Make sure we leave the DAG in a valid state
10676 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10677 SmallVector<EVT, 1> ValueVTs;
10678 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10679
10680 if (ValueVTs.empty())
10681 return;
10682
10684 for (const EVT &VT : ValueVTs)
10685 Ops.push_back(DAG.getUNDEF(VT));
10686
10687 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10688}
10689
10690void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10691 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10692 MVT::Other, getRoot(),
10693 getValue(I.getArgOperand(0)),
10694 DAG.getSrcValue(I.getArgOperand(0))));
10695}
10696
10697void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10698 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10699 const DataLayout &DL = DAG.getDataLayout();
10700 SDValue V = DAG.getVAArg(
10701 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10702 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10703 DL.getABITypeAlign(I.getType()).value());
10704 DAG.setRoot(V.getValue(1));
10705
10706 if (I.getType()->isPointerTy())
10707 V = DAG.getPtrExtOrTrunc(
10708 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10709 setValue(&I, V);
10710}
10711
10712void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10713 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10714 MVT::Other, getRoot(),
10715 getValue(I.getArgOperand(0)),
10716 DAG.getSrcValue(I.getArgOperand(0))));
10717}
10718
10719void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10720 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10721 MVT::Other, getRoot(),
10722 getValue(I.getArgOperand(0)),
10723 getValue(I.getArgOperand(1)),
10724 DAG.getSrcValue(I.getArgOperand(0)),
10725 DAG.getSrcValue(I.getArgOperand(1))));
10726}
10727
10729 const Instruction &I,
10730 SDValue Op) {
10731 std::optional<ConstantRange> CR = getRange(I);
10732
10733 if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10734 return Op;
10735
10736 APInt Lo = CR->getUnsignedMin();
10737 if (!Lo.isMinValue())
10738 return Op;
10739
10740 APInt Hi = CR->getUnsignedMax();
10741 unsigned Bits = std::max(Hi.getActiveBits(),
10742 static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10743
10744 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10745
10746 SDLoc SL = getCurSDLoc();
10747
10748 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10749 DAG.getValueType(SmallVT));
10750 unsigned NumVals = Op.getNode()->getNumValues();
10751 if (NumVals == 1)
10752 return ZExt;
10753
10755
10756 Ops.push_back(ZExt);
10757 for (unsigned I = 1; I != NumVals; ++I)
10758 Ops.push_back(Op.getValue(I));
10759
10760 return DAG.getMergeValues(Ops, SL);
10761}
10762
10764 SelectionDAG &DAG, const Instruction &I, SDValue Op) {
10765 FPClassTest Classes = getNoFPClass(I);
10766 if (Classes == fcNone)
10767 return Op;
10768
10769 SDLoc SL = getCurSDLoc();
10770 SDValue TestConst = DAG.getTargetConstant(Classes, SDLoc(), MVT::i32);
10771
10772 if (Op.getOpcode() != ISD::MERGE_VALUES) {
10773 return DAG.getNode(ISD::AssertNoFPClass, SL, Op.getValueType(), Op,
10774 TestConst);
10775 }
10776
10777 SmallVector<SDValue, 8> Ops(Op.getNumOperands());
10778 for (unsigned I = 0, E = Ops.size(); I != E; ++I) {
10779 SDValue MergeOp = Op.getOperand(I);
10780 Ops[I] = DAG.getNode(ISD::AssertNoFPClass, SL, MergeOp.getValueType(),
10781 MergeOp, TestConst);
10782 }
10783
10784 return DAG.getMergeValues(Ops, SL);
10785}
10786
10787/// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10788/// the call being lowered.
10789///
10790/// This is a helper for lowering intrinsics that follow a target calling
10791/// convention or require stack pointer adjustment. Only a subset of the
10792/// intrinsic's operands need to participate in the calling convention.
10795 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10796 AttributeSet RetAttrs, bool IsPatchPoint) {
10798 Args.reserve(NumArgs);
10799
10800 // Populate the argument list.
10801 // Attributes for args start at offset 1, after the return attribute.
10802 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10803 ArgI != ArgE; ++ArgI) {
10804 const Value *V = Call->getOperand(ArgI);
10805
10806 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10807
10808 TargetLowering::ArgListEntry Entry(getValue(V), V->getType());
10809 Entry.setAttributes(Call, ArgI);
10810 Args.push_back(Entry);
10811 }
10812
10814 .setChain(getRoot())
10815 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10816 RetAttrs)
10817 .setDiscardResult(Call->use_empty())
10818 .setIsPatchPoint(IsPatchPoint)
10820 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10821}
10822
10823/// Add a stack map intrinsic call's live variable operands to a stackmap
10824/// or patchpoint target node's operand list.
10825///
10826/// Constants are converted to TargetConstants purely as an optimization to
10827/// avoid constant materialization and register allocation.
10828///
10829/// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10830/// generate addess computation nodes, and so FinalizeISel can convert the
10831/// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10832/// address materialization and register allocation, but may also be required
10833/// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10834/// alloca in the entry block, then the runtime may assume that the alloca's
10835/// StackMap location can be read immediately after compilation and that the
10836/// location is valid at any point during execution (this is similar to the
10837/// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10838/// only available in a register, then the runtime would need to trap when
10839/// execution reaches the StackMap in order to read the alloca's location.
10840static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10842 SelectionDAGBuilder &Builder) {
10843 SelectionDAG &DAG = Builder.DAG;
10844 for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10845 SDValue Op = Builder.getValue(Call.getArgOperand(I));
10846
10847 // Things on the stack are pointer-typed, meaning that they are already
10848 // legal and can be emitted directly to target nodes.
10850 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10851 } else {
10852 // Otherwise emit a target independent node to be legalised.
10853 Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10854 }
10855 }
10856}
10857
10858/// Lower llvm.experimental.stackmap.
10859void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10860 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10861 // [live variables...])
10862
10863 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10864
10865 SDValue Chain, InGlue, Callee;
10867
10868 SDLoc DL = getCurSDLoc();
10870
10871 // The stackmap intrinsic only records the live variables (the arguments
10872 // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10873 // intrinsic, this won't be lowered to a function call. This means we don't
10874 // have to worry about calling conventions and target specific lowering code.
10875 // Instead we perform the call lowering right here.
10876 //
10877 // chain, flag = CALLSEQ_START(chain, 0, 0)
10878 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10879 // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10880 //
10881 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10882 InGlue = Chain.getValue(1);
10883
10884 // Add the STACKMAP operands, starting with DAG house-keeping.
10885 Ops.push_back(Chain);
10886 Ops.push_back(InGlue);
10887
10888 // Add the <id>, <numShadowBytes> operands.
10889 //
10890 // These do not require legalisation, and can be emitted directly to target
10891 // constant nodes.
10893 assert(ID.getValueType() == MVT::i64);
10894 SDValue IDConst =
10895 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10896 Ops.push_back(IDConst);
10897
10898 SDValue Shad = getValue(CI.getArgOperand(1));
10899 assert(Shad.getValueType() == MVT::i32);
10900 SDValue ShadConst =
10901 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
10902 Ops.push_back(ShadConst);
10903
10904 // Add the live variables.
10905 addStackMapLiveVars(CI, 2, DL, Ops, *this);
10906
10907 // Create the STACKMAP node.
10908 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10909 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10910 InGlue = Chain.getValue(1);
10911
10912 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10913
10914 // Stackmaps don't generate values, so nothing goes into the NodeMap.
10915
10916 // Set the root to the target-lowered call chain.
10917 DAG.setRoot(Chain);
10918
10919 // Inform the Frame Information that we have a stackmap in this function.
10920 FuncInfo.MF->getFrameInfo().setHasStackMap();
10921}
10922
10923/// Lower llvm.experimental.patchpoint directly to its target opcode.
10924void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10925 const BasicBlock *EHPadBB) {
10926 // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10927 // i32 <numBytes>,
10928 // i8* <target>,
10929 // i32 <numArgs>,
10930 // [Args...],
10931 // [live variables...])
10932
10934 bool IsAnyRegCC = CC == CallingConv::AnyReg;
10935 bool HasDef = !CB.getType()->isVoidTy();
10936 SDLoc dl = getCurSDLoc();
10938
10939 // Handle immediate and symbolic callees.
10940 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10941 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10942 /*isTarget=*/true);
10943 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
10944 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
10945 SDLoc(SymbolicCallee),
10946 SymbolicCallee->getValueType(0));
10947
10948 // Get the real number of arguments participating in the call <numArgs>
10950 unsigned NumArgs = NArgVal->getAsZExtVal();
10951
10952 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
10953 // Intrinsics include all meta-operands up to but not including CC.
10954 unsigned NumMetaOpers = PatchPointOpers::CCPos;
10955 assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
10956 "Not enough arguments provided to the patchpoint intrinsic");
10957
10958 // For AnyRegCC the arguments are lowered later on manually.
10959 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
10960 Type *ReturnTy =
10961 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
10962
10963 TargetLowering::CallLoweringInfo CLI(DAG);
10964 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
10965 ReturnTy, CB.getAttributes().getRetAttrs(), true);
10966 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
10967
10968 SDNode *CallEnd = Result.second.getNode();
10969 if (CallEnd->getOpcode() == ISD::EH_LABEL)
10970 CallEnd = CallEnd->getOperand(0).getNode();
10971 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
10972 CallEnd = CallEnd->getOperand(0).getNode();
10973
10974 /// Get a call instruction from the call sequence chain.
10975 /// Tail calls are not allowed.
10976 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
10977 "Expected a callseq node.");
10978 SDNode *Call = CallEnd->getOperand(0).getNode();
10979 bool HasGlue = Call->getGluedNode();
10980
10981 // Replace the target specific call node with the patchable intrinsic.
10983
10984 // Push the chain.
10985 Ops.push_back(*(Call->op_begin()));
10986
10987 // Optionally, push the glue (if any).
10988 if (HasGlue)
10989 Ops.push_back(*(Call->op_end() - 1));
10990
10991 // Push the register mask info.
10992 if (HasGlue)
10993 Ops.push_back(*(Call->op_end() - 2));
10994 else
10995 Ops.push_back(*(Call->op_end() - 1));
10996
10997 // Add the <id> and <numBytes> constants.
10999 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
11001 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
11002
11003 // Add the callee.
11004 Ops.push_back(Callee);
11005
11006 // Adjust <numArgs> to account for any arguments that have been passed on the
11007 // stack instead.
11008 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
11009 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
11010 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
11011 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
11012
11013 // Add the calling convention
11014 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
11015
11016 // Add the arguments we omitted previously. The register allocator should
11017 // place these in any free register.
11018 if (IsAnyRegCC)
11019 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
11020 Ops.push_back(getValue(CB.getArgOperand(i)));
11021
11022 // Push the arguments from the call instruction.
11023 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
11024 Ops.append(Call->op_begin() + 2, e);
11025
11026 // Push live variables for the stack map.
11027 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
11028
11029 SDVTList NodeTys;
11030 if (IsAnyRegCC && HasDef) {
11031 // Create the return types based on the intrinsic definition
11032 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11033 SmallVector<EVT, 3> ValueVTs;
11034 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
11035 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
11036
11037 // There is always a chain and a glue type at the end
11038 ValueVTs.push_back(MVT::Other);
11039 ValueVTs.push_back(MVT::Glue);
11040 NodeTys = DAG.getVTList(ValueVTs);
11041 } else
11042 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
11043
11044 // Replace the target specific call node with a PATCHPOINT node.
11045 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
11046
11047 // Update the NodeMap.
11048 if (HasDef) {
11049 if (IsAnyRegCC)
11050 setValue(&CB, SDValue(PPV.getNode(), 0));
11051 else
11052 setValue(&CB, Result.first);
11053 }
11054
11055 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
11056 // call sequence. Furthermore the location of the chain and glue can change
11057 // when the AnyReg calling convention is used and the intrinsic returns a
11058 // value.
11059 if (IsAnyRegCC && HasDef) {
11060 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
11061 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
11062 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
11063 } else
11064 DAG.ReplaceAllUsesWith(Call, PPV.getNode());
11065 DAG.DeleteNode(Call);
11066
11067 // Inform the Frame Information that we have a patchpoint in this function.
11068 FuncInfo.MF->getFrameInfo().setHasPatchPoint();
11069}
11070
11071void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
11072 unsigned Intrinsic) {
11073 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11074 SDValue Op1 = getValue(I.getArgOperand(0));
11075 SDValue Op2;
11076 if (I.arg_size() > 1)
11077 Op2 = getValue(I.getArgOperand(1));
11078 SDLoc dl = getCurSDLoc();
11079 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11080 SDValue Res;
11081 SDNodeFlags SDFlags;
11082 if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
11083 SDFlags.copyFMF(*FPMO);
11084
11085 switch (Intrinsic) {
11086 case Intrinsic::vector_reduce_fadd:
11087 if (SDFlags.hasAllowReassociation())
11088 Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
11089 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
11090 SDFlags);
11091 else
11092 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
11093 break;
11094 case Intrinsic::vector_reduce_fmul:
11095 if (SDFlags.hasAllowReassociation())
11096 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
11097 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
11098 SDFlags);
11099 else
11100 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
11101 break;
11102 case Intrinsic::vector_reduce_add:
11103 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
11104 break;
11105 case Intrinsic::vector_reduce_mul:
11106 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
11107 break;
11108 case Intrinsic::vector_reduce_and:
11109 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
11110 break;
11111 case Intrinsic::vector_reduce_or:
11112 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
11113 break;
11114 case Intrinsic::vector_reduce_xor:
11115 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
11116 break;
11117 case Intrinsic::vector_reduce_smax:
11118 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
11119 break;
11120 case Intrinsic::vector_reduce_smin:
11121 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
11122 break;
11123 case Intrinsic::vector_reduce_umax:
11124 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
11125 break;
11126 case Intrinsic::vector_reduce_umin:
11127 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
11128 break;
11129 case Intrinsic::vector_reduce_fmax:
11130 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
11131 break;
11132 case Intrinsic::vector_reduce_fmin:
11133 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
11134 break;
11135 case Intrinsic::vector_reduce_fmaximum:
11136 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
11137 break;
11138 case Intrinsic::vector_reduce_fminimum:
11139 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
11140 break;
11141 default:
11142 llvm_unreachable("Unhandled vector reduce intrinsic");
11143 }
11144 setValue(&I, Res);
11145}
11146
11147/// Returns an AttributeList representing the attributes applied to the return
11148/// value of the given call.
11151 if (CLI.RetSExt)
11152 Attrs.push_back(Attribute::SExt);
11153 if (CLI.RetZExt)
11154 Attrs.push_back(Attribute::ZExt);
11155 if (CLI.IsInReg)
11156 Attrs.push_back(Attribute::InReg);
11157
11158 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
11159 Attrs);
11160}
11161
11162/// TargetLowering::LowerCallTo - This is the default LowerCallTo
11163/// implementation, which just calls LowerCall.
11164/// FIXME: When all targets are
11165/// migrated to using LowerCall, this hook should be integrated into SDISel.
11166std::pair<SDValue, SDValue>
11168 LLVMContext &Context = CLI.RetTy->getContext();
11169
11170 // Handle the incoming return values from the call.
11171 CLI.Ins.clear();
11172 SmallVector<Type *, 4> RetOrigTys;
11174 auto &DL = CLI.DAG.getDataLayout();
11175 ComputeValueTypes(DL, CLI.OrigRetTy, RetOrigTys, &Offsets);
11176
11177 SmallVector<EVT, 4> RetVTs;
11178 if (CLI.RetTy != CLI.OrigRetTy) {
11179 assert(RetOrigTys.size() == 1 &&
11180 "Only supported for non-aggregate returns");
11181 RetVTs.push_back(getValueType(DL, CLI.RetTy));
11182 } else {
11183 for (Type *Ty : RetOrigTys)
11184 RetVTs.push_back(getValueType(DL, Ty));
11185 }
11186
11187 if (CLI.IsPostTypeLegalization) {
11188 // If we are lowering a libcall after legalization, split the return type.
11189 SmallVector<Type *, 4> OldRetOrigTys;
11190 SmallVector<EVT, 4> OldRetVTs;
11191 SmallVector<TypeSize, 4> OldOffsets;
11192 RetOrigTys.swap(OldRetOrigTys);
11193 RetVTs.swap(OldRetVTs);
11194 Offsets.swap(OldOffsets);
11195
11196 for (size_t i = 0, e = OldRetVTs.size(); i != e; ++i) {
11197 EVT RetVT = OldRetVTs[i];
11198 uint64_t Offset = OldOffsets[i];
11199 MVT RegisterVT = getRegisterType(Context, RetVT);
11200 unsigned NumRegs = getNumRegisters(Context, RetVT);
11201 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
11202 RetOrigTys.append(NumRegs, OldRetOrigTys[i]);
11203 RetVTs.append(NumRegs, RegisterVT);
11204 for (unsigned j = 0; j != NumRegs; ++j)
11205 Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
11206 }
11207 }
11208
11210 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
11211
11212 bool CanLowerReturn =
11214 CLI.IsVarArg, Outs, Context, CLI.RetTy);
11215
11216 SDValue DemoteStackSlot;
11217 int DemoteStackIdx = -100;
11218 if (!CanLowerReturn) {
11219 // FIXME: equivalent assert?
11220 // assert(!CS.hasInAllocaArgument() &&
11221 // "sret demotion is incompatible with inalloca");
11222 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
11223 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
11225 DemoteStackIdx =
11226 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
11227 Type *StackSlotPtrType = PointerType::get(Context, DL.getAllocaAddrSpace());
11228
11229 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
11230 ArgListEntry Entry(DemoteStackSlot, StackSlotPtrType);
11231 Entry.IsSRet = true;
11232 Entry.Alignment = Alignment;
11233 CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
11234 CLI.NumFixedArgs += 1;
11235 CLI.getArgs()[0].IndirectType = CLI.RetTy;
11236 CLI.RetTy = CLI.OrigRetTy = Type::getVoidTy(Context);
11237
11238 // sret demotion isn't compatible with tail-calls, since the sret argument
11239 // points into the callers stack frame.
11240 CLI.IsTailCall = false;
11241 } else {
11242 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11243 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
11244 for (unsigned I = 0, E = RetVTs.size(); I != E; ++I) {
11245 ISD::ArgFlagsTy Flags;
11246 if (NeedsRegBlock) {
11247 Flags.setInConsecutiveRegs();
11248 if (I == RetVTs.size() - 1)
11249 Flags.setInConsecutiveRegsLast();
11250 }
11251 EVT VT = RetVTs[I];
11252 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11253 unsigned NumRegs =
11254 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11255 for (unsigned i = 0; i != NumRegs; ++i) {
11256 ISD::InputArg Ret(Flags, RegisterVT, VT, RetOrigTys[I],
11258 if (CLI.RetTy->isPointerTy()) {
11259 Ret.Flags.setPointer();
11261 cast<PointerType>(CLI.RetTy)->getAddressSpace());
11262 }
11263 if (CLI.RetSExt)
11264 Ret.Flags.setSExt();
11265 if (CLI.RetZExt)
11266 Ret.Flags.setZExt();
11267 if (CLI.IsInReg)
11268 Ret.Flags.setInReg();
11269 CLI.Ins.push_back(Ret);
11270 }
11271 }
11272 }
11273
11274 // We push in swifterror return as the last element of CLI.Ins.
11275 ArgListTy &Args = CLI.getArgs();
11276 if (supportSwiftError()) {
11277 for (const ArgListEntry &Arg : Args) {
11278 if (Arg.IsSwiftError) {
11279 ISD::ArgFlagsTy Flags;
11280 Flags.setSwiftError();
11282 PointerType::getUnqual(Context),
11283 /*Used=*/true, ISD::InputArg::NoArgIndex, 0);
11284 CLI.Ins.push_back(Ret);
11285 }
11286 }
11287 }
11288
11289 // Handle all of the outgoing arguments.
11290 CLI.Outs.clear();
11291 CLI.OutVals.clear();
11292 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
11293 SmallVector<Type *, 4> OrigArgTys;
11294 ComputeValueTypes(DL, Args[i].OrigTy, OrigArgTys);
11295 // FIXME: Split arguments if CLI.IsPostTypeLegalization
11296 Type *FinalType = Args[i].Ty;
11297 if (Args[i].IsByVal)
11298 FinalType = Args[i].IndirectType;
11299 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
11300 FinalType, CLI.CallConv, CLI.IsVarArg, DL);
11301 for (unsigned Value = 0, NumValues = OrigArgTys.size(); Value != NumValues;
11302 ++Value) {
11303 Type *OrigArgTy = OrigArgTys[Value];
11304 Type *ArgTy = OrigArgTy;
11305 if (Args[i].Ty != Args[i].OrigTy) {
11306 assert(Value == 0 && "Only supported for non-aggregate arguments");
11307 ArgTy = Args[i].Ty;
11308 }
11309
11310 EVT VT = getValueType(DL, ArgTy);
11311 SDValue Op = SDValue(Args[i].Node.getNode(),
11312 Args[i].Node.getResNo() + Value);
11313 ISD::ArgFlagsTy Flags;
11314
11315 // Certain targets (such as MIPS), may have a different ABI alignment
11316 // for a type depending on the context. Give the target a chance to
11317 // specify the alignment it wants.
11318 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
11319 Flags.setOrigAlign(OriginalAlignment);
11320
11321 if (i >= CLI.NumFixedArgs)
11322 Flags.setVarArg();
11323 if (ArgTy->isPointerTy()) {
11324 Flags.setPointer();
11325 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11326 }
11327 if (Args[i].IsZExt)
11328 Flags.setZExt();
11329 if (Args[i].IsSExt)
11330 Flags.setSExt();
11331 if (Args[i].IsNoExt)
11332 Flags.setNoExt();
11333 if (Args[i].IsInReg) {
11334 // If we are using vectorcall calling convention, a structure that is
11335 // passed InReg - is surely an HVA
11337 isa<StructType>(FinalType)) {
11338 // The first value of a structure is marked
11339 if (0 == Value)
11340 Flags.setHvaStart();
11341 Flags.setHva();
11342 }
11343 // Set InReg Flag
11344 Flags.setInReg();
11345 }
11346 if (Args[i].IsSRet)
11347 Flags.setSRet();
11348 if (Args[i].IsSwiftSelf)
11349 Flags.setSwiftSelf();
11350 if (Args[i].IsSwiftAsync)
11351 Flags.setSwiftAsync();
11352 if (Args[i].IsSwiftError)
11353 Flags.setSwiftError();
11354 if (Args[i].IsCFGuardTarget)
11355 Flags.setCFGuardTarget();
11356 if (Args[i].IsByVal)
11357 Flags.setByVal();
11358 if (Args[i].IsByRef)
11359 Flags.setByRef();
11360 if (Args[i].IsPreallocated) {
11361 Flags.setPreallocated();
11362 // Set the byval flag for CCAssignFn callbacks that don't know about
11363 // preallocated. This way we can know how many bytes we should've
11364 // allocated and how many bytes a callee cleanup function will pop. If
11365 // we port preallocated to more targets, we'll have to add custom
11366 // preallocated handling in the various CC lowering callbacks.
11367 Flags.setByVal();
11368 }
11369 if (Args[i].IsInAlloca) {
11370 Flags.setInAlloca();
11371 // Set the byval flag for CCAssignFn callbacks that don't know about
11372 // inalloca. This way we can know how many bytes we should've allocated
11373 // and how many bytes a callee cleanup function will pop. If we port
11374 // inalloca to more targets, we'll have to add custom inalloca handling
11375 // in the various CC lowering callbacks.
11376 Flags.setByVal();
11377 }
11378 Align MemAlign;
11379 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
11380 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
11381 Flags.setByValSize(FrameSize);
11382
11383 // info is not there but there are cases it cannot get right.
11384 if (auto MA = Args[i].Alignment)
11385 MemAlign = *MA;
11386 else
11387 MemAlign = getByValTypeAlignment(Args[i].IndirectType, DL);
11388 } else if (auto MA = Args[i].Alignment) {
11389 MemAlign = *MA;
11390 } else {
11391 MemAlign = OriginalAlignment;
11392 }
11393 Flags.setMemAlign(MemAlign);
11394 if (Args[i].IsNest)
11395 Flags.setNest();
11396 if (NeedsRegBlock)
11397 Flags.setInConsecutiveRegs();
11398
11399 MVT PartVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11400 unsigned NumParts =
11401 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11402 SmallVector<SDValue, 4> Parts(NumParts);
11403 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
11404
11405 if (Args[i].IsSExt)
11406 ExtendKind = ISD::SIGN_EXTEND;
11407 else if (Args[i].IsZExt)
11408 ExtendKind = ISD::ZERO_EXTEND;
11409
11410 // Conservatively only handle 'returned' on non-vectors that can be lowered,
11411 // for now.
11412 if (Args[i].IsReturned && !Op.getValueType().isVector() &&
11414 assert((CLI.RetTy == Args[i].Ty ||
11415 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
11417 Args[i].Ty->getPointerAddressSpace())) &&
11418 RetVTs.size() == NumValues && "unexpected use of 'returned'");
11419 // Before passing 'returned' to the target lowering code, ensure that
11420 // either the register MVT and the actual EVT are the same size or that
11421 // the return value and argument are extended in the same way; in these
11422 // cases it's safe to pass the argument register value unchanged as the
11423 // return register value (although it's at the target's option whether
11424 // to do so)
11425 // TODO: allow code generation to take advantage of partially preserved
11426 // registers rather than clobbering the entire register when the
11427 // parameter extension method is not compatible with the return
11428 // extension method
11429 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
11430 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
11431 CLI.RetZExt == Args[i].IsZExt))
11432 Flags.setReturned();
11433 }
11434
11435 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
11436 CLI.CallConv, ExtendKind);
11437
11438 for (unsigned j = 0; j != NumParts; ++j) {
11439 // if it isn't first piece, alignment must be 1
11440 // For scalable vectors the scalable part is currently handled
11441 // by individual targets, so we just use the known minimum size here.
11442 ISD::OutputArg MyFlags(
11443 Flags, Parts[j].getValueType().getSimpleVT(), VT, OrigArgTy, i,
11444 j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
11445 if (NumParts > 1 && j == 0)
11446 MyFlags.Flags.setSplit();
11447 else if (j != 0) {
11448 MyFlags.Flags.setOrigAlign(Align(1));
11449 if (j == NumParts - 1)
11450 MyFlags.Flags.setSplitEnd();
11451 }
11452
11453 CLI.Outs.push_back(MyFlags);
11454 CLI.OutVals.push_back(Parts[j]);
11455 }
11456
11457 if (NeedsRegBlock && Value == NumValues - 1)
11458 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
11459 }
11460 }
11461
11463 CLI.Chain = LowerCall(CLI, InVals);
11464
11465 // Update CLI.InVals to use outside of this function.
11466 CLI.InVals = InVals;
11467
11468 // Verify that the target's LowerCall behaved as expected.
11469 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
11470 "LowerCall didn't return a valid chain!");
11471 assert((!CLI.IsTailCall || InVals.empty()) &&
11472 "LowerCall emitted a return value for a tail call!");
11473 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11474 "LowerCall didn't emit the correct number of values!");
11475
11476 // For a tail call, the return value is merely live-out and there aren't
11477 // any nodes in the DAG representing it. Return a special value to
11478 // indicate that a tail call has been emitted and no more Instructions
11479 // should be processed in the current block.
11480 if (CLI.IsTailCall) {
11481 CLI.DAG.setRoot(CLI.Chain);
11482 return std::make_pair(SDValue(), SDValue());
11483 }
11484
11485#ifndef NDEBUG
11486 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11487 assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11488 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11489 "LowerCall emitted a value with the wrong type!");
11490 }
11491#endif
11492
11493 SmallVector<SDValue, 4> ReturnValues;
11494 if (!CanLowerReturn) {
11495 // The instruction result is the result of loading from the
11496 // hidden sret parameter.
11497 MVT PtrVT = getPointerTy(DL, DL.getAllocaAddrSpace());
11498
11499 unsigned NumValues = RetVTs.size();
11500 ReturnValues.resize(NumValues);
11501 SmallVector<SDValue, 4> Chains(NumValues);
11502
11503 // An aggregate return value cannot wrap around the address space, so
11504 // offsets to its parts don't wrap either.
11506 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11507 for (unsigned i = 0; i < NumValues; ++i) {
11509 DemoteStackSlot, CLI.DAG.getConstant(Offsets[i], CLI.DL, PtrVT),
11511 SDValue L = CLI.DAG.getLoad(
11512 RetVTs[i], CLI.DL, CLI.Chain, Add,
11514 DemoteStackIdx, Offsets[i]),
11515 HiddenSRetAlign);
11516 ReturnValues[i] = L;
11517 Chains[i] = L.getValue(1);
11518 }
11519
11520 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11521 } else {
11522 // Collect the legal value parts into potentially illegal values
11523 // that correspond to the original function's return values.
11524 std::optional<ISD::NodeType> AssertOp;
11525 if (CLI.RetSExt)
11526 AssertOp = ISD::AssertSext;
11527 else if (CLI.RetZExt)
11528 AssertOp = ISD::AssertZext;
11529 unsigned CurReg = 0;
11530 for (EVT VT : RetVTs) {
11531 MVT RegisterVT = getRegisterTypeForCallingConv(Context, CLI.CallConv, VT);
11532 unsigned NumRegs =
11533 getNumRegistersForCallingConv(Context, CLI.CallConv, VT);
11534
11535 ReturnValues.push_back(getCopyFromParts(
11536 CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11537 CLI.Chain, CLI.CallConv, AssertOp));
11538 CurReg += NumRegs;
11539 }
11540
11541 // For a function returning void, there is no return value. We can't create
11542 // such a node, so we just return a null return value in that case. In
11543 // that case, nothing will actually look at the value.
11544 if (ReturnValues.empty())
11545 return std::make_pair(SDValue(), CLI.Chain);
11546 }
11547
11548 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11549 CLI.DAG.getVTList(RetVTs), ReturnValues);
11550 return std::make_pair(Res, CLI.Chain);
11551}
11552
11553/// Places new result values for the node in Results (their number
11554/// and types must exactly match those of the original return values of
11555/// the node), or leaves Results empty, which indicates that the node is not
11556/// to be custom lowered after all.
11559 SelectionDAG &DAG) const {
11560 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11561
11562 if (!Res.getNode())
11563 return;
11564
11565 // If the original node has one result, take the return value from
11566 // LowerOperation as is. It might not be result number 0.
11567 if (N->getNumValues() == 1) {
11568 Results.push_back(Res);
11569 return;
11570 }
11571
11572 // If the original node has multiple results, then the return node should
11573 // have the same number of results.
11574 assert((N->getNumValues() == Res->getNumValues()) &&
11575 "Lowering returned the wrong number of results!");
11576
11577 // Places new result values base on N result number.
11578 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11579 Results.push_back(Res.getValue(I));
11580}
11581
11583 llvm_unreachable("LowerOperation not implemented for this target!");
11584}
11585
11587 Register Reg,
11588 ISD::NodeType ExtendType) {
11590 assert((Op.getOpcode() != ISD::CopyFromReg ||
11591 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11592 "Copy from a reg to the same reg!");
11593 assert(!Reg.isPhysical() && "Is a physreg");
11594
11595 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11596 // If this is an InlineAsm we have to match the registers required, not the
11597 // notional registers required by the type.
11598
11599 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11600 std::nullopt); // This is not an ABI copy.
11601 SDValue Chain = DAG.getEntryNode();
11602
11603 if (ExtendType == ISD::ANY_EXTEND) {
11604 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11605 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11606 ExtendType = PreferredExtendIt->second;
11607 }
11608 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11609 PendingExports.push_back(Chain);
11610}
11611
11613
11614/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11615/// entry block, return true. This includes arguments used by switches, since
11616/// the switch may expand into multiple basic blocks.
11617static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11618 // With FastISel active, we may be splitting blocks, so force creation
11619 // of virtual registers for all non-dead arguments.
11620 if (FastISel)
11621 return A->use_empty();
11622
11623 const BasicBlock &Entry = A->getParent()->front();
11624 for (const User *U : A->users())
11625 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11626 return false; // Use not in entry block.
11627
11628 return true;
11629}
11630
11632 DenseMap<const Argument *,
11633 std::pair<const AllocaInst *, const StoreInst *>>;
11634
11635/// Scan the entry block of the function in FuncInfo for arguments that look
11636/// like copies into a local alloca. Record any copied arguments in
11637/// ArgCopyElisionCandidates.
11638static void
11640 FunctionLoweringInfo *FuncInfo,
11641 ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11642 // Record the state of every static alloca used in the entry block. Argument
11643 // allocas are all used in the entry block, so we need approximately as many
11644 // entries as we have arguments.
11645 enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11647 unsigned NumArgs = FuncInfo->Fn->arg_size();
11648 StaticAllocas.reserve(NumArgs * 2);
11649
11650 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11651 if (!V)
11652 return nullptr;
11653 V = V->stripPointerCasts();
11654 const auto *AI = dyn_cast<AllocaInst>(V);
11655 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11656 return nullptr;
11657 auto Iter = StaticAllocas.insert({AI, Unknown});
11658 return &Iter.first->second;
11659 };
11660
11661 // Look for stores of arguments to static allocas. Look through bitcasts and
11662 // GEPs to handle type coercions, as long as the alloca is fully initialized
11663 // by the store. Any non-store use of an alloca escapes it and any subsequent
11664 // unanalyzed store might write it.
11665 // FIXME: Handle structs initialized with multiple stores.
11666 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11667 // Look for stores, and handle non-store uses conservatively.
11668 const auto *SI = dyn_cast<StoreInst>(&I);
11669 if (!SI) {
11670 // We will look through cast uses, so ignore them completely.
11671 if (I.isCast())
11672 continue;
11673 // Ignore debug info and pseudo op intrinsics, they don't escape or store
11674 // to allocas.
11675 if (I.isDebugOrPseudoInst())
11676 continue;
11677 // This is an unknown instruction. Assume it escapes or writes to all
11678 // static alloca operands.
11679 for (const Use &U : I.operands()) {
11680 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11681 *Info = StaticAllocaInfo::Clobbered;
11682 }
11683 continue;
11684 }
11685
11686 // If the stored value is a static alloca, mark it as escaped.
11687 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11688 *Info = StaticAllocaInfo::Clobbered;
11689
11690 // Check if the destination is a static alloca.
11691 const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11692 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11693 if (!Info)
11694 continue;
11695 const AllocaInst *AI = cast<AllocaInst>(Dst);
11696
11697 // Skip allocas that have been initialized or clobbered.
11698 if (*Info != StaticAllocaInfo::Unknown)
11699 continue;
11700
11701 // Check if the stored value is an argument, and that this store fully
11702 // initializes the alloca.
11703 // If the argument type has padding bits we can't directly forward a pointer
11704 // as the upper bits may contain garbage.
11705 // Don't elide copies from the same argument twice.
11706 const Value *Val = SI->getValueOperand()->stripPointerCasts();
11707 const auto *Arg = dyn_cast<Argument>(Val);
11708 std::optional<TypeSize> AllocaSize = AI->getAllocationSize(DL);
11709 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11710 Arg->getType()->isEmptyTy() || !AllocaSize ||
11711 DL.getTypeStoreSize(Arg->getType()) != *AllocaSize ||
11712 !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11713 ArgCopyElisionCandidates.count(Arg)) {
11714 *Info = StaticAllocaInfo::Clobbered;
11715 continue;
11716 }
11717
11718 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11719 << '\n');
11720
11721 // Mark this alloca and store for argument copy elision.
11722 *Info = StaticAllocaInfo::Elidable;
11723 ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11724
11725 // Stop scanning if we've seen all arguments. This will happen early in -O0
11726 // builds, which is useful, because -O0 builds have large entry blocks and
11727 // many allocas.
11728 if (ArgCopyElisionCandidates.size() == NumArgs)
11729 break;
11730 }
11731}
11732
11733/// Try to elide argument copies from memory into a local alloca. Succeeds if
11734/// ArgVal is a load from a suitable fixed stack object.
11737 DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11738 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11739 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11740 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11741 // Check if this is a load from a fixed stack object.
11742 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11743 if (!LNode)
11744 return;
11745 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11746 if (!FINode)
11747 return;
11748
11749 // Check that the fixed stack object is the right size and alignment.
11750 // Look at the alignment that the user wrote on the alloca instead of looking
11751 // at the stack object.
11752 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11753 assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11754 const AllocaInst *AI = ArgCopyIter->second.first;
11755 int FixedIndex = FINode->getIndex();
11756 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11757 int OldIndex = AllocaIndex;
11758 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11759 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11760 LLVM_DEBUG(
11761 dbgs() << " argument copy elision failed due to bad fixed stack "
11762 "object size\n");
11763 return;
11764 }
11765 Align RequiredAlignment = AI->getAlign();
11766 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11767 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca "
11768 "greater than stack argument alignment ("
11769 << DebugStr(RequiredAlignment) << " vs "
11770 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11771 return;
11772 }
11773
11774 // Perform the elision. Delete the old stack object and replace its only use
11775 // in the variable info map. Mark the stack object as mutable and aliased.
11776 LLVM_DEBUG({
11777 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11778 << " Replacing frame index " << OldIndex << " with " << FixedIndex
11779 << '\n';
11780 });
11781 MFI.RemoveStackObject(OldIndex);
11782 MFI.setIsImmutableObjectIndex(FixedIndex, false);
11783 MFI.setIsAliasedObjectIndex(FixedIndex, true);
11784 AllocaIndex = FixedIndex;
11785 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11786 for (SDValue ArgVal : ArgVals)
11787 Chains.push_back(ArgVal.getValue(1));
11788
11789 // Avoid emitting code for the store implementing the copy.
11790 const StoreInst *SI = ArgCopyIter->second.second;
11791 ElidedArgCopyInstrs.insert(SI);
11792
11793 // Check for uses of the argument again so that we can avoid exporting ArgVal
11794 // if it is't used by anything other than the store.
11795 for (const Value *U : Arg.users()) {
11796 if (U != SI) {
11797 ArgHasUses = true;
11798 break;
11799 }
11800 }
11801}
11802
11803void SelectionDAGISel::LowerArguments(const Function &F) {
11804 SelectionDAG &DAG = SDB->DAG;
11805 SDLoc dl = SDB->getCurSDLoc();
11806 const DataLayout &DL = DAG.getDataLayout();
11808
11809 // In Naked functions we aren't going to save any registers.
11810 if (F.hasFnAttribute(Attribute::Naked))
11811 return;
11812
11813 if (!FuncInfo->CanLowerReturn) {
11814 // Put in an sret pointer parameter before all the other parameters.
11815 MVT ValueVT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
11816
11817 ISD::ArgFlagsTy Flags;
11818 Flags.setSRet();
11819 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVT);
11820 ISD::InputArg RetArg(Flags, RegisterVT, ValueVT, F.getReturnType(), true,
11822 Ins.push_back(RetArg);
11823 }
11824
11825 // Look for stores of arguments to static allocas. Mark such arguments with a
11826 // flag to ask the target to give us the memory location of that argument if
11827 // available.
11828 ArgCopyElisionMapTy ArgCopyElisionCandidates;
11830 ArgCopyElisionCandidates);
11831
11832 // Set up the incoming argument description vector.
11833 for (const Argument &Arg : F.args()) {
11834 unsigned ArgNo = Arg.getArgNo();
11836 ComputeValueTypes(DAG.getDataLayout(), Arg.getType(), Types);
11837 bool isArgValueUsed = !Arg.use_empty();
11838 Type *FinalType = Arg.getType();
11839 if (Arg.hasAttribute(Attribute::ByVal))
11840 FinalType = Arg.getParamByValType();
11841 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11842 FinalType, F.getCallingConv(), F.isVarArg(), DL);
11843 for (unsigned Value = 0, NumValues = Types.size(); Value != NumValues;
11844 ++Value) {
11845 Type *ArgTy = Types[Value];
11846 EVT VT = TLI->getValueType(DL, ArgTy);
11847 ISD::ArgFlagsTy Flags;
11848
11849 if (ArgTy->isPointerTy()) {
11850 Flags.setPointer();
11851 Flags.setPointerAddrSpace(cast<PointerType>(ArgTy)->getAddressSpace());
11852 }
11853 if (Arg.hasAttribute(Attribute::ZExt))
11854 Flags.setZExt();
11855 if (Arg.hasAttribute(Attribute::SExt))
11856 Flags.setSExt();
11857 if (Arg.hasAttribute(Attribute::InReg)) {
11858 // If we are using vectorcall calling convention, a structure that is
11859 // passed InReg - is surely an HVA
11860 if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11861 isa<StructType>(Arg.getType())) {
11862 // The first value of a structure is marked
11863 if (0 == Value)
11864 Flags.setHvaStart();
11865 Flags.setHva();
11866 }
11867 // Set InReg Flag
11868 Flags.setInReg();
11869 }
11870 if (Arg.hasAttribute(Attribute::StructRet))
11871 Flags.setSRet();
11872 if (Arg.hasAttribute(Attribute::SwiftSelf))
11873 Flags.setSwiftSelf();
11874 if (Arg.hasAttribute(Attribute::SwiftAsync))
11875 Flags.setSwiftAsync();
11876 if (Arg.hasAttribute(Attribute::SwiftError))
11877 Flags.setSwiftError();
11878 if (Arg.hasAttribute(Attribute::ByVal))
11879 Flags.setByVal();
11880 if (Arg.hasAttribute(Attribute::ByRef))
11881 Flags.setByRef();
11882 if (Arg.hasAttribute(Attribute::InAlloca)) {
11883 Flags.setInAlloca();
11884 // Set the byval flag for CCAssignFn callbacks that don't know about
11885 // inalloca. This way we can know how many bytes we should've allocated
11886 // and how many bytes a callee cleanup function will pop. If we port
11887 // inalloca to more targets, we'll have to add custom inalloca handling
11888 // in the various CC lowering callbacks.
11889 Flags.setByVal();
11890 }
11891 if (Arg.hasAttribute(Attribute::Preallocated)) {
11892 Flags.setPreallocated();
11893 // Set the byval flag for CCAssignFn callbacks that don't know about
11894 // preallocated. This way we can know how many bytes we should've
11895 // allocated and how many bytes a callee cleanup function will pop. If
11896 // we port preallocated to more targets, we'll have to add custom
11897 // preallocated handling in the various CC lowering callbacks.
11898 Flags.setByVal();
11899 }
11900
11901 // Certain targets (such as MIPS), may have a different ABI alignment
11902 // for a type depending on the context. Give the target a chance to
11903 // specify the alignment it wants.
11904 const Align OriginalAlignment(
11905 TLI->getABIAlignmentForCallingConv(ArgTy, DL));
11906 Flags.setOrigAlign(OriginalAlignment);
11907
11908 Align MemAlign;
11909 Type *ArgMemTy = nullptr;
11910 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11911 Flags.isByRef()) {
11912 if (!ArgMemTy)
11913 ArgMemTy = Arg.getPointeeInMemoryValueType();
11914
11915 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11916
11917 // For in-memory arguments, size and alignment should be passed from FE.
11918 // BE will guess if this info is not there but there are cases it cannot
11919 // get right.
11920 if (auto ParamAlign = Arg.getParamStackAlign())
11921 MemAlign = *ParamAlign;
11922 else if ((ParamAlign = Arg.getParamAlign()))
11923 MemAlign = *ParamAlign;
11924 else
11925 MemAlign = TLI->getByValTypeAlignment(ArgMemTy, DL);
11926 if (Flags.isByRef())
11927 Flags.setByRefSize(MemSize);
11928 else
11929 Flags.setByValSize(MemSize);
11930 } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11931 MemAlign = *ParamAlign;
11932 } else {
11933 MemAlign = OriginalAlignment;
11934 }
11935 Flags.setMemAlign(MemAlign);
11936
11937 if (Arg.hasAttribute(Attribute::Nest))
11938 Flags.setNest();
11939 if (NeedsRegBlock)
11940 Flags.setInConsecutiveRegs();
11941 if (ArgCopyElisionCandidates.count(&Arg))
11942 Flags.setCopyElisionCandidate();
11943 if (Arg.hasAttribute(Attribute::Returned))
11944 Flags.setReturned();
11945
11946 MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
11947 *CurDAG->getContext(), F.getCallingConv(), VT);
11948 unsigned NumRegs = TLI->getNumRegistersForCallingConv(
11949 *CurDAG->getContext(), F.getCallingConv(), VT);
11950 for (unsigned i = 0; i != NumRegs; ++i) {
11951 // For scalable vectors, use the minimum size; individual targets
11952 // are responsible for handling scalable vector arguments and
11953 // return values.
11954 ISD::InputArg MyFlags(
11955 Flags, RegisterVT, VT, ArgTy, isArgValueUsed, ArgNo,
11956 i * RegisterVT.getStoreSize().getKnownMinValue());
11957 if (NumRegs > 1 && i == 0)
11958 MyFlags.Flags.setSplit();
11959 // if it isn't first piece, alignment must be 1
11960 else if (i > 0) {
11961 MyFlags.Flags.setOrigAlign(Align(1));
11962 if (i == NumRegs - 1)
11963 MyFlags.Flags.setSplitEnd();
11964 }
11965 Ins.push_back(MyFlags);
11966 }
11967 if (NeedsRegBlock && Value == NumValues - 1)
11968 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
11969 }
11970 }
11971
11972 // Call the target to set up the argument values.
11974 SDValue NewRoot = TLI->LowerFormalArguments(
11975 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
11976
11977 // Verify that the target's LowerFormalArguments behaved as expected.
11978 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
11979 "LowerFormalArguments didn't return a valid chain!");
11980 assert(InVals.size() == Ins.size() &&
11981 "LowerFormalArguments didn't emit the correct number of values!");
11982 LLVM_DEBUG({
11983 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
11984 assert(InVals[i].getNode() &&
11985 "LowerFormalArguments emitted a null value!");
11986 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
11987 "LowerFormalArguments emitted a value with the wrong type!");
11988 }
11989 });
11990
11991 // Update the DAG with the new chain value resulting from argument lowering.
11992 DAG.setRoot(NewRoot);
11993
11994 // Set up the argument values.
11995 unsigned i = 0;
11996 if (!FuncInfo->CanLowerReturn) {
11997 // Create a virtual register for the sret pointer, and put in a copy
11998 // from the sret argument into it.
11999 MVT VT = TLI->getPointerTy(DL, DL.getAllocaAddrSpace());
12000 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
12001 std::optional<ISD::NodeType> AssertOp;
12002 SDValue ArgValue =
12003 getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
12004 F.getCallingConv(), AssertOp);
12005
12006 MachineFunction& MF = SDB->DAG.getMachineFunction();
12007 MachineRegisterInfo& RegInfo = MF.getRegInfo();
12008 Register SRetReg =
12009 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
12010 FuncInfo->DemoteRegister = SRetReg;
12011 NewRoot =
12012 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
12013 DAG.setRoot(NewRoot);
12014
12015 // i indexes lowered arguments. Bump it past the hidden sret argument.
12016 ++i;
12017 }
12018
12020 DenseMap<int, int> ArgCopyElisionFrameIndexMap;
12021 for (const Argument &Arg : F.args()) {
12022 SmallVector<SDValue, 4> ArgValues;
12023 SmallVector<EVT, 4> ValueVTs;
12024 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
12025 unsigned NumValues = ValueVTs.size();
12026 if (NumValues == 0)
12027 continue;
12028
12029 bool ArgHasUses = !Arg.use_empty();
12030
12031 // Elide the copying store if the target loaded this argument from a
12032 // suitable fixed stack object.
12033 if (Ins[i].Flags.isCopyElisionCandidate()) {
12034 unsigned NumParts = 0;
12035 for (EVT VT : ValueVTs)
12036 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
12037 F.getCallingConv(), VT);
12038
12039 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
12040 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
12041 ArrayRef(&InVals[i], NumParts), ArgHasUses);
12042 }
12043
12044 // If this argument is unused then remember its value. It is used to generate
12045 // debugging information.
12046 bool isSwiftErrorArg =
12047 TLI->supportSwiftError() &&
12048 Arg.hasAttribute(Attribute::SwiftError);
12049 if (!ArgHasUses && !isSwiftErrorArg) {
12050 SDB->setUnusedArgValue(&Arg, InVals[i]);
12051
12052 // Also remember any frame index for use in FastISel.
12053 if (FrameIndexSDNode *FI =
12055 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12056 }
12057
12058 for (unsigned Val = 0; Val != NumValues; ++Val) {
12059 EVT VT = ValueVTs[Val];
12060 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
12061 F.getCallingConv(), VT);
12062 unsigned NumParts = TLI->getNumRegistersForCallingConv(
12063 *CurDAG->getContext(), F.getCallingConv(), VT);
12064
12065 // Even an apparent 'unused' swifterror argument needs to be returned. So
12066 // we do generate a copy for it that can be used on return from the
12067 // function.
12068 if (ArgHasUses || isSwiftErrorArg) {
12069 std::optional<ISD::NodeType> AssertOp;
12070 if (Arg.hasAttribute(Attribute::SExt))
12071 AssertOp = ISD::AssertSext;
12072 else if (Arg.hasAttribute(Attribute::ZExt))
12073 AssertOp = ISD::AssertZext;
12074
12075 SDValue OutVal =
12076 getCopyFromParts(DAG, dl, &InVals[i], NumParts, PartVT, VT, nullptr,
12077 NewRoot, F.getCallingConv(), AssertOp);
12078
12079 FPClassTest NoFPClass = Arg.getNoFPClass();
12080 if (NoFPClass != fcNone) {
12081 SDValue SDNoFPClass = DAG.getTargetConstant(
12082 static_cast<uint64_t>(NoFPClass), dl, MVT::i32);
12083 OutVal = DAG.getNode(ISD::AssertNoFPClass, dl, OutVal.getValueType(),
12084 OutVal, SDNoFPClass);
12085 }
12086 ArgValues.push_back(OutVal);
12087 }
12088
12089 i += NumParts;
12090 }
12091
12092 // We don't need to do anything else for unused arguments.
12093 if (ArgValues.empty())
12094 continue;
12095
12096 // Note down frame index.
12097 if (FrameIndexSDNode *FI =
12098 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
12099 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12100
12101 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
12102 SDB->getCurSDLoc());
12103
12104 SDB->setValue(&Arg, Res);
12105 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
12106 // We want to associate the argument with the frame index, among
12107 // involved operands, that correspond to the lowest address. The
12108 // getCopyFromParts function, called earlier, is swapping the order of
12109 // the operands to BUILD_PAIR depending on endianness. The result of
12110 // that swapping is that the least significant bits of the argument will
12111 // be in the first operand of the BUILD_PAIR node, and the most
12112 // significant bits will be in the second operand.
12113 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
12114 if (LoadSDNode *LNode =
12115 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
12116 if (FrameIndexSDNode *FI =
12117 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
12118 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
12119 }
12120
12121 // Analyses past this point are naive and don't expect an assertion.
12122 if (Res.getOpcode() == ISD::AssertZext)
12123 Res = Res.getOperand(0);
12124
12125 // Update the SwiftErrorVRegDefMap.
12126 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
12127 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12128 if (Reg.isVirtual())
12129 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
12130 Reg);
12131 }
12132
12133 // If this argument is live outside of the entry block, insert a copy from
12134 // wherever we got it to the vreg that other BB's will reference it as.
12135 if (Res.getOpcode() == ISD::CopyFromReg) {
12136 // If we can, though, try to skip creating an unnecessary vreg.
12137 // FIXME: This isn't very clean... it would be nice to make this more
12138 // general.
12139 Register Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
12140 if (Reg.isVirtual()) {
12141 FuncInfo->ValueMap[&Arg] = Reg;
12142 continue;
12143 }
12144 }
12145 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
12146 FuncInfo->InitializeRegForValue(&Arg);
12147 SDB->CopyToExportRegsIfNeeded(&Arg);
12148 }
12149 }
12150
12151 if (!Chains.empty()) {
12152 Chains.push_back(NewRoot);
12153 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
12154 }
12155
12156 DAG.setRoot(NewRoot);
12157
12158 assert(i == InVals.size() && "Argument register count mismatch!");
12159
12160 // If any argument copy elisions occurred and we have debug info, update the
12161 // stale frame indices used in the dbg.declare variable info table.
12162 if (!ArgCopyElisionFrameIndexMap.empty()) {
12163 for (MachineFunction::VariableDbgInfo &VI :
12164 MF->getInStackSlotVariableDbgInfo()) {
12165 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
12166 if (I != ArgCopyElisionFrameIndexMap.end())
12167 VI.updateStackSlot(I->second);
12168 }
12169 }
12170
12171 // Finally, if the target has anything special to do, allow it to do so.
12173}
12174
12175/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
12176/// ensure constants are generated when needed. Remember the virtual registers
12177/// that need to be added to the Machine PHI nodes as input. We cannot just
12178/// directly add them, because expansion might result in multiple MBB's for one
12179/// BB. As such, the start of the BB might correspond to a different MBB than
12180/// the end.
12181void
12182SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
12183 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12184
12185 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
12186
12187 // Check PHI nodes in successors that expect a value to be available from this
12188 // block.
12189 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
12190 if (!isa<PHINode>(SuccBB->begin())) continue;
12191 MachineBasicBlock *SuccMBB = FuncInfo.getMBB(SuccBB);
12192
12193 // If this terminator has multiple identical successors (common for
12194 // switches), only handle each succ once.
12195 if (!SuccsHandled.insert(SuccMBB).second)
12196 continue;
12197
12199
12200 // At this point we know that there is a 1-1 correspondence between LLVM PHI
12201 // nodes and Machine PHI nodes, but the incoming operands have not been
12202 // emitted yet.
12203 for (const PHINode &PN : SuccBB->phis()) {
12204 // Ignore dead phi's.
12205 if (PN.use_empty())
12206 continue;
12207
12208 // Skip empty types
12209 if (PN.getType()->isEmptyTy())
12210 continue;
12211
12212 Register Reg;
12213 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
12214
12215 if (const auto *C = dyn_cast<Constant>(PHIOp)) {
12216 Register &RegOut = ConstantsOut[C];
12217 if (!RegOut) {
12218 RegOut = FuncInfo.CreateRegs(&PN);
12219 // We need to zero/sign extend ConstantInt phi operands to match
12220 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
12221 ISD::NodeType ExtendType = ISD::ANY_EXTEND;
12222 if (auto *CI = dyn_cast<ConstantInt>(C))
12223 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
12225 CopyValueToVirtualRegister(C, RegOut, ExtendType);
12226 }
12227 Reg = RegOut;
12228 } else {
12230 FuncInfo.ValueMap.find(PHIOp);
12231 if (I != FuncInfo.ValueMap.end())
12232 Reg = I->second;
12233 else {
12234 assert(isa<AllocaInst>(PHIOp) &&
12235 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
12236 "Didn't codegen value into a register!??");
12237 Reg = FuncInfo.CreateRegs(&PN);
12239 }
12240 }
12241
12242 // Remember that this register needs to added to the machine PHI node as
12243 // the input for this MBB.
12244 SmallVector<EVT, 4> ValueVTs;
12245 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
12246 for (EVT VT : ValueVTs) {
12247 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
12248 for (unsigned i = 0; i != NumRegisters; ++i)
12249 FuncInfo.PHINodesToUpdate.emplace_back(&*MBBI++, Reg + i);
12250 Reg += NumRegisters;
12251 }
12252 }
12253 }
12254
12255 ConstantsOut.clear();
12256}
12257
12258MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
12260 if (++I == FuncInfo.MF->end())
12261 return nullptr;
12262 return &*I;
12263}
12264
12265/// During lowering new call nodes can be created (such as memset, etc.).
12266/// Those will become new roots of the current DAG, but complications arise
12267/// when they are tail calls. In such cases, the call lowering will update
12268/// the root, but the builder still needs to know that a tail call has been
12269/// lowered in order to avoid generating an additional return.
12270void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
12271 // If the node is null, we do have a tail call.
12272 if (MaybeTC.getNode() != nullptr)
12273 DAG.setRoot(MaybeTC);
12274 else
12275 HasTailCall = true;
12276}
12277
12278void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
12279 MachineBasicBlock *SwitchMBB,
12280 MachineBasicBlock *DefaultMBB) {
12281 MachineFunction *CurMF = FuncInfo.MF;
12282 MachineBasicBlock *NextMBB = nullptr;
12284 if (++BBI != FuncInfo.MF->end())
12285 NextMBB = &*BBI;
12286
12287 unsigned Size = W.LastCluster - W.FirstCluster + 1;
12288
12289 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12290
12291 if (Size == 2 && W.MBB == SwitchMBB) {
12292 // If any two of the cases has the same destination, and if one value
12293 // is the same as the other, but has one bit unset that the other has set,
12294 // use bit manipulation to do two compares at once. For example:
12295 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
12296 // TODO: This could be extended to merge any 2 cases in switches with 3
12297 // cases.
12298 // TODO: Handle cases where W.CaseBB != SwitchBB.
12299 CaseCluster &Small = *W.FirstCluster;
12300 CaseCluster &Big = *W.LastCluster;
12301
12302 if (Small.Low == Small.High && Big.Low == Big.High &&
12303 Small.MBB == Big.MBB) {
12304 const APInt &SmallValue = Small.Low->getValue();
12305 const APInt &BigValue = Big.Low->getValue();
12306
12307 // Check that there is only one bit different.
12308 APInt CommonBit = BigValue ^ SmallValue;
12309 if (CommonBit.isPowerOf2()) {
12310 SDValue CondLHS = getValue(Cond);
12311 EVT VT = CondLHS.getValueType();
12312 SDLoc DL = getCurSDLoc();
12313
12314 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
12315 DAG.getConstant(CommonBit, DL, VT));
12316 SDValue Cond = DAG.getSetCC(
12317 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
12318 ISD::SETEQ);
12319
12320 // Update successor info.
12321 // Both Small and Big will jump to Small.BB, so we sum up the
12322 // probabilities.
12323 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
12324 if (BPI)
12325 addSuccessorWithProb(
12326 SwitchMBB, DefaultMBB,
12327 // The default destination is the first successor in IR.
12328 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
12329 else
12330 addSuccessorWithProb(SwitchMBB, DefaultMBB);
12331
12332 // Insert the true branch.
12333 SDValue BrCond =
12334 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
12335 DAG.getBasicBlock(Small.MBB));
12336 // Insert the false branch.
12337 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
12338 DAG.getBasicBlock(DefaultMBB));
12339
12340 DAG.setRoot(BrCond);
12341 return;
12342 }
12343 }
12344 }
12345
12346 if (TM.getOptLevel() != CodeGenOptLevel::None) {
12347 // Here, we order cases by probability so the most likely case will be
12348 // checked first. However, two clusters can have the same probability in
12349 // which case their relative ordering is non-deterministic. So we use Low
12350 // as a tie-breaker as clusters are guaranteed to never overlap.
12351 llvm::sort(W.FirstCluster, W.LastCluster + 1,
12352 [](const CaseCluster &a, const CaseCluster &b) {
12353 return a.Prob != b.Prob ?
12354 a.Prob > b.Prob :
12355 a.Low->getValue().slt(b.Low->getValue());
12356 });
12357
12358 // Rearrange the case blocks so that the last one falls through if possible
12359 // without changing the order of probabilities.
12360 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
12361 --I;
12362 if (I->Prob > W.LastCluster->Prob)
12363 break;
12364 if (I->Kind == CC_Range && I->MBB == NextMBB) {
12365 std::swap(*I, *W.LastCluster);
12366 break;
12367 }
12368 }
12369 }
12370
12371 // Compute total probability.
12372 BranchProbability DefaultProb = W.DefaultProb;
12373 BranchProbability UnhandledProbs = DefaultProb;
12374 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
12375 UnhandledProbs += I->Prob;
12376
12377 MachineBasicBlock *CurMBB = W.MBB;
12378 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
12379 bool FallthroughUnreachable = false;
12380 MachineBasicBlock *Fallthrough;
12381 if (I == W.LastCluster) {
12382 // For the last cluster, fall through to the default destination.
12383 Fallthrough = DefaultMBB;
12384 FallthroughUnreachable = isa<UnreachableInst>(
12385 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
12386 } else {
12387 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
12388 CurMF->insert(BBI, Fallthrough);
12389 // Put Cond in a virtual register to make it available from the new blocks.
12391 }
12392 UnhandledProbs -= I->Prob;
12393
12394 switch (I->Kind) {
12395 case CC_JumpTable: {
12396 // FIXME: Optimize away range check based on pivot comparisons.
12397 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
12398 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
12399
12400 // The jump block hasn't been inserted yet; insert it here.
12401 MachineBasicBlock *JumpMBB = JT->MBB;
12402 CurMF->insert(BBI, JumpMBB);
12403
12404 auto JumpProb = I->Prob;
12405 auto FallthroughProb = UnhandledProbs;
12406
12407 // If the default statement is a target of the jump table, we evenly
12408 // distribute the default probability to successors of CurMBB. Also
12409 // update the probability on the edge from JumpMBB to Fallthrough.
12410 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
12411 SE = JumpMBB->succ_end();
12412 SI != SE; ++SI) {
12413 if (*SI == DefaultMBB) {
12414 JumpProb += DefaultProb / 2;
12415 FallthroughProb -= DefaultProb / 2;
12416 JumpMBB->setSuccProbability(SI, DefaultProb / 2);
12417 JumpMBB->normalizeSuccProbs();
12418 break;
12419 }
12420 }
12421
12422 // If the default clause is unreachable, propagate that knowledge into
12423 // JTH->FallthroughUnreachable which will use it to suppress the range
12424 // check.
12425 //
12426 // However, don't do this if we're doing branch target enforcement,
12427 // because a table branch _without_ a range check can be a tempting JOP
12428 // gadget - out-of-bounds inputs that are impossible in correct
12429 // execution become possible again if an attacker can influence the
12430 // control flow. So if an attacker doesn't already have a BTI bypass
12431 // available, we don't want them to be able to get one out of this
12432 // table branch.
12433 if (FallthroughUnreachable) {
12434 Function &CurFunc = CurMF->getFunction();
12435 if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
12436 JTH->FallthroughUnreachable = true;
12437 }
12438
12439 if (!JTH->FallthroughUnreachable)
12440 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
12441 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
12442 CurMBB->normalizeSuccProbs();
12443
12444 // The jump table header will be inserted in our current block, do the
12445 // range check, and fall through to our fallthrough block.
12446 JTH->HeaderBB = CurMBB;
12447 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12448
12449 // If we're in the right place, emit the jump table header right now.
12450 if (CurMBB == SwitchMBB) {
12451 visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12452 JTH->Emitted = true;
12453 }
12454 break;
12455 }
12456 case CC_BitTests: {
12457 // FIXME: Optimize away range check based on pivot comparisons.
12458 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12459
12460 // The bit test blocks haven't been inserted yet; insert them here.
12461 for (BitTestCase &BTC : BTB->Cases)
12462 CurMF->insert(BBI, BTC.ThisBB);
12463
12464 // Fill in fields of the BitTestBlock.
12465 BTB->Parent = CurMBB;
12466 BTB->Default = Fallthrough;
12467
12468 BTB->DefaultProb = UnhandledProbs;
12469 // If the cases in bit test don't form a contiguous range, we evenly
12470 // distribute the probability on the edge to Fallthrough to two
12471 // successors of CurMBB.
12472 if (!BTB->ContiguousRange) {
12473 BTB->Prob += DefaultProb / 2;
12474 BTB->DefaultProb -= DefaultProb / 2;
12475 }
12476
12477 if (FallthroughUnreachable)
12478 BTB->FallthroughUnreachable = true;
12479
12480 // If we're in the right place, emit the bit test header right now.
12481 if (CurMBB == SwitchMBB) {
12482 visitBitTestHeader(*BTB, SwitchMBB);
12483 BTB->Emitted = true;
12484 }
12485 break;
12486 }
12487 case CC_Range: {
12488 const Value *RHS, *LHS, *MHS;
12489 ISD::CondCode CC;
12490 if (I->Low == I->High) {
12491 // Check Cond == I->Low.
12492 CC = ISD::SETEQ;
12493 LHS = Cond;
12494 RHS=I->Low;
12495 MHS = nullptr;
12496 } else {
12497 // Check I->Low <= Cond <= I->High.
12498 CC = ISD::SETLE;
12499 LHS = I->Low;
12500 MHS = Cond;
12501 RHS = I->High;
12502 }
12503
12504 // If Fallthrough is unreachable, fold away the comparison.
12505 if (FallthroughUnreachable)
12506 CC = ISD::SETTRUE;
12507
12508 // The false probability is the sum of all unhandled cases.
12509 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12510 getCurSDLoc(), I->Prob, UnhandledProbs);
12511
12512 if (CurMBB == SwitchMBB)
12513 visitSwitchCase(CB, SwitchMBB);
12514 else
12515 SL->SwitchCases.push_back(CB);
12516
12517 break;
12518 }
12519 }
12520 CurMBB = Fallthrough;
12521 }
12522}
12523
12524void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12525 const SwitchWorkListItem &W,
12526 Value *Cond,
12527 MachineBasicBlock *SwitchMBB) {
12528 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12529 "Clusters not sorted?");
12530 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12531
12532 auto [LastLeft, FirstRight, LeftProb, RightProb] =
12533 SL->computeSplitWorkItemInfo(W);
12534
12535 // Use the first element on the right as pivot since we will make less-than
12536 // comparisons against it.
12537 CaseClusterIt PivotCluster = FirstRight;
12538 assert(PivotCluster > W.FirstCluster);
12539 assert(PivotCluster <= W.LastCluster);
12540
12541 CaseClusterIt FirstLeft = W.FirstCluster;
12542 CaseClusterIt LastRight = W.LastCluster;
12543
12544 const ConstantInt *Pivot = PivotCluster->Low;
12545
12546 // New blocks will be inserted immediately after the current one.
12548 ++BBI;
12549
12550 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12551 // we can branch to its destination directly if it's squeezed exactly in
12552 // between the known lower bound and Pivot - 1.
12553 MachineBasicBlock *LeftMBB;
12554 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12555 FirstLeft->Low == W.GE &&
12556 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12557 LeftMBB = FirstLeft->MBB;
12558 } else {
12559 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12560 FuncInfo.MF->insert(BBI, LeftMBB);
12561 WorkList.push_back(
12562 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12563 // Put Cond in a virtual register to make it available from the new blocks.
12565 }
12566
12567 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12568 // single cluster, RHS.Low == Pivot, and we can branch to its destination
12569 // directly if RHS.High equals the current upper bound.
12570 MachineBasicBlock *RightMBB;
12571 if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12572 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12573 RightMBB = FirstRight->MBB;
12574 } else {
12575 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12576 FuncInfo.MF->insert(BBI, RightMBB);
12577 WorkList.push_back(
12578 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12579 // Put Cond in a virtual register to make it available from the new blocks.
12581 }
12582
12583 // Create the CaseBlock record that will be used to lower the branch.
12584 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12585 getCurSDLoc(), LeftProb, RightProb);
12586
12587 if (W.MBB == SwitchMBB)
12588 visitSwitchCase(CB, SwitchMBB);
12589 else
12590 SL->SwitchCases.push_back(CB);
12591}
12592
12593// Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12594// from the swith statement.
12596 BranchProbability PeeledCaseProb) {
12597 if (PeeledCaseProb == BranchProbability::getOne())
12599 BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12600
12601 uint32_t Numerator = CaseProb.getNumerator();
12602 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12603 return BranchProbability(Numerator, std::max(Numerator, Denominator));
12604}
12605
12606// Try to peel the top probability case if it exceeds the threshold.
12607// Return current MachineBasicBlock for the switch statement if the peeling
12608// does not occur.
12609// If the peeling is performed, return the newly created MachineBasicBlock
12610// for the peeled switch statement. Also update Clusters to remove the peeled
12611// case. PeeledCaseProb is the BranchProbability for the peeled case.
12612MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12613 const SwitchInst &SI, CaseClusterVector &Clusters,
12614 BranchProbability &PeeledCaseProb) {
12615 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12616 // Don't perform if there is only one cluster or optimizing for size.
12617 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12618 TM.getOptLevel() == CodeGenOptLevel::None ||
12619 SwitchMBB->getParent()->getFunction().hasMinSize())
12620 return SwitchMBB;
12621
12622 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12623 unsigned PeeledCaseIndex = 0;
12624 bool SwitchPeeled = false;
12625 for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12626 CaseCluster &CC = Clusters[Index];
12627 if (CC.Prob < TopCaseProb)
12628 continue;
12629 TopCaseProb = CC.Prob;
12630 PeeledCaseIndex = Index;
12631 SwitchPeeled = true;
12632 }
12633 if (!SwitchPeeled)
12634 return SwitchMBB;
12635
12636 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12637 << TopCaseProb << "\n");
12638
12639 // Record the MBB for the peeled switch statement.
12640 MachineFunction::iterator BBI(SwitchMBB);
12641 ++BBI;
12642 MachineBasicBlock *PeeledSwitchMBB =
12643 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12644 FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12645
12646 ExportFromCurrentBlock(SI.getCondition());
12647 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12648 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12649 nullptr, nullptr, TopCaseProb.getCompl()};
12650 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12651
12652 Clusters.erase(PeeledCaseIt);
12653 for (CaseCluster &CC : Clusters) {
12654 LLVM_DEBUG(
12655 dbgs() << "Scale the probablity for one cluster, before scaling: "
12656 << CC.Prob << "\n");
12657 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12658 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12659 }
12660 PeeledCaseProb = TopCaseProb;
12661 return PeeledSwitchMBB;
12662}
12663
12664void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12665 // Extract cases from the switch.
12666 BranchProbabilityInfo *BPI = FuncInfo.BPI;
12667 CaseClusterVector Clusters;
12668 Clusters.reserve(SI.getNumCases());
12669 for (auto I : SI.cases()) {
12670 MachineBasicBlock *Succ = FuncInfo.getMBB(I.getCaseSuccessor());
12671 const ConstantInt *CaseVal = I.getCaseValue();
12672 BranchProbability Prob =
12673 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12674 : BranchProbability(1, SI.getNumCases() + 1);
12675 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12676 }
12677
12678 MachineBasicBlock *DefaultMBB = FuncInfo.getMBB(SI.getDefaultDest());
12679
12680 // Cluster adjacent cases with the same destination. We do this at all
12681 // optimization levels because it's cheap to do and will make codegen faster
12682 // if there are many clusters.
12683 sortAndRangeify(Clusters);
12684
12685 // The branch probablity of the peeled case.
12686 BranchProbability PeeledCaseProb = BranchProbability::getZero();
12687 MachineBasicBlock *PeeledSwitchMBB =
12688 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12689
12690 // If there is only the default destination, jump there directly.
12691 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12692 if (Clusters.empty()) {
12693 assert(PeeledSwitchMBB == SwitchMBB);
12694 SwitchMBB->addSuccessor(DefaultMBB);
12695 if (DefaultMBB != NextBlock(SwitchMBB)) {
12696 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12697 getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12698 }
12699 return;
12700 }
12701
12702 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12703 DAG.getBFI());
12704 SL->findBitTestClusters(Clusters, &SI);
12705
12706 LLVM_DEBUG({
12707 dbgs() << "Case clusters: ";
12708 for (const CaseCluster &C : Clusters) {
12709 if (C.Kind == CC_JumpTable)
12710 dbgs() << "JT:";
12711 if (C.Kind == CC_BitTests)
12712 dbgs() << "BT:";
12713
12714 C.Low->getValue().print(dbgs(), true);
12715 if (C.Low != C.High) {
12716 dbgs() << '-';
12717 C.High->getValue().print(dbgs(), true);
12718 }
12719 dbgs() << ' ';
12720 }
12721 dbgs() << '\n';
12722 });
12723
12724 assert(!Clusters.empty());
12725 SwitchWorkList WorkList;
12726 CaseClusterIt First = Clusters.begin();
12727 CaseClusterIt Last = Clusters.end() - 1;
12728 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12729 // Scale the branchprobability for DefaultMBB if the peel occurs and
12730 // DefaultMBB is not replaced.
12731 if (PeeledCaseProb != BranchProbability::getZero() &&
12732 DefaultMBB == FuncInfo.getMBB(SI.getDefaultDest()))
12733 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12734 WorkList.push_back(
12735 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12736
12737 while (!WorkList.empty()) {
12738 SwitchWorkListItem W = WorkList.pop_back_val();
12739 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12740
12741 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12742 !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12743 // For optimized builds, lower large range as a balanced binary tree.
12744 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12745 continue;
12746 }
12747
12748 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12749 }
12750}
12751
12752void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12753 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12754 auto DL = getCurSDLoc();
12755 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12756 setValue(&I, DAG.getStepVector(DL, ResultVT));
12757}
12758
12759void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12760 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12761 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12762
12763 SDLoc DL = getCurSDLoc();
12764 SDValue V = getValue(I.getOperand(0));
12765 assert(VT == V.getValueType() && "Malformed vector.reverse!");
12766
12767 if (VT.isScalableVector()) {
12768 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12769 return;
12770 }
12771
12772 // Use VECTOR_SHUFFLE for the fixed-length vector
12773 // to maintain existing behavior.
12774 SmallVector<int, 8> Mask;
12775 unsigned NumElts = VT.getVectorMinNumElements();
12776 for (unsigned i = 0; i != NumElts; ++i)
12777 Mask.push_back(NumElts - 1 - i);
12778
12779 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12780}
12781
12782void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I,
12783 unsigned Factor) {
12784 auto DL = getCurSDLoc();
12785 SDValue InVec = getValue(I.getOperand(0));
12786
12787 SmallVector<EVT, 4> ValueVTs;
12788 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12789 ValueVTs);
12790
12791 EVT OutVT = ValueVTs[0];
12792 unsigned OutNumElts = OutVT.getVectorMinNumElements();
12793
12794 SmallVector<SDValue, 4> SubVecs(Factor);
12795 for (unsigned i = 0; i != Factor; ++i) {
12796 assert(ValueVTs[i] == OutVT && "Expected VTs to be the same");
12797 SubVecs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12798 DAG.getVectorIdxConstant(OutNumElts * i, DL));
12799 }
12800
12801 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12802 // from existing legalisation and combines.
12803 if (OutVT.isFixedLengthVector() && Factor == 2) {
12804 SDValue Even = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12805 createStrideMask(0, 2, OutNumElts));
12806 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, SubVecs[0], SubVecs[1],
12807 createStrideMask(1, 2, OutNumElts));
12808 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12809 setValue(&I, Res);
12810 return;
12811 }
12812
12813 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12814 DAG.getVTList(ValueVTs), SubVecs);
12815 setValue(&I, Res);
12816}
12817
12818void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I,
12819 unsigned Factor) {
12820 auto DL = getCurSDLoc();
12821 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12822 EVT InVT = getValue(I.getOperand(0)).getValueType();
12823 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12824
12825 SmallVector<SDValue, 8> InVecs(Factor);
12826 for (unsigned i = 0; i < Factor; ++i) {
12827 InVecs[i] = getValue(I.getOperand(i));
12828 assert(InVecs[i].getValueType() == InVecs[0].getValueType() &&
12829 "Expected VTs to be the same");
12830 }
12831
12832 // Use VECTOR_SHUFFLE for fixed-length vectors with factor of 2 to benefit
12833 // from existing legalisation and combines.
12834 if (OutVT.isFixedLengthVector() && Factor == 2) {
12835 unsigned NumElts = InVT.getVectorMinNumElements();
12836 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVecs);
12837 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12838 createInterleaveMask(NumElts, 2)));
12839 return;
12840 }
12841
12842 SmallVector<EVT, 8> ValueVTs(Factor, InVT);
12843 SDValue Res =
12844 DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, DAG.getVTList(ValueVTs), InVecs);
12845
12847 for (unsigned i = 0; i < Factor; ++i)
12848 Results[i] = Res.getValue(i);
12849
12850 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Results);
12851 setValue(&I, Res);
12852}
12853
12854void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12855 SmallVector<EVT, 4> ValueVTs;
12856 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12857 ValueVTs);
12858 unsigned NumValues = ValueVTs.size();
12859 if (NumValues == 0) return;
12860
12861 SmallVector<SDValue, 4> Values(NumValues);
12862 SDValue Op = getValue(I.getOperand(0));
12863
12864 for (unsigned i = 0; i != NumValues; ++i)
12865 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12866 SDValue(Op.getNode(), Op.getResNo() + i));
12867
12869 DAG.getVTList(ValueVTs), Values));
12870}
12871
12872void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12873 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12874 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12875
12876 SDLoc DL = getCurSDLoc();
12877 SDValue V1 = getValue(I.getOperand(0));
12878 SDValue V2 = getValue(I.getOperand(1));
12879 uint64_t Imm = cast<ConstantInt>(I.getOperand(2))->getZExtValue();
12880 const bool IsLeft = I.getIntrinsicID() == Intrinsic::vector_splice_left;
12881
12882 // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
12883 if (VT.isScalableVector()) {
12884 setValue(
12885 &I,
12886 DAG.getNode(
12888 V1, V2,
12889 DAG.getConstant(Imm, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
12890 return;
12891 }
12892
12893 unsigned NumElts = VT.getVectorNumElements();
12894
12895 uint64_t Idx = IsLeft ? Imm : NumElts - Imm;
12896
12897 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12898 SmallVector<int, 8> Mask;
12899 for (unsigned i = 0; i < NumElts; ++i)
12900 Mask.push_back(Idx + i);
12901 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12902}
12903
12904// Consider the following MIR after SelectionDAG, which produces output in
12905// phyregs in the first case or virtregs in the second case.
12906//
12907// INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12908// %5:gr32 = COPY $ebx
12909// %6:gr32 = COPY $edx
12910// %1:gr32 = COPY %6:gr32
12911// %0:gr32 = COPY %5:gr32
12912//
12913// INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12914// %1:gr32 = COPY %6:gr32
12915// %0:gr32 = COPY %5:gr32
12916//
12917// Given %0, we'd like to return $ebx in the first case and %5 in the second.
12918// Given %1, we'd like to return $edx in the first case and %6 in the second.
12919//
12920// If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12921// to a single virtreg (such as %0). The remaining outputs monotonically
12922// increase in virtreg number from there. If a callbr has no outputs, then it
12923// should not have a corresponding callbr landingpad; in fact, the callbr
12924// landingpad would not even be able to refer to such a callbr.
12926 MachineInstr *MI = MRI.def_begin(Reg)->getParent();
12927 // There is definitely at least one copy.
12928 assert(MI->getOpcode() == TargetOpcode::COPY &&
12929 "start of copy chain MUST be COPY");
12930 Reg = MI->getOperand(1).getReg();
12931
12932 // If the copied register in the first copy must be virtual.
12933 assert(Reg.isVirtual() && "expected COPY of virtual register");
12934 MI = MRI.def_begin(Reg)->getParent();
12935
12936 // There may be an optional second copy.
12937 if (MI->getOpcode() == TargetOpcode::COPY) {
12938 assert(Reg.isVirtual() && "expected COPY of virtual register");
12939 Reg = MI->getOperand(1).getReg();
12940 assert(Reg.isPhysical() && "expected COPY of physical register");
12941 } else {
12942 // The start of the chain must be an INLINEASM_BR.
12943 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12944 "end of copy chain MUST be INLINEASM_BR");
12945 }
12946
12947 return Reg;
12948}
12949
12950// We must do this walk rather than the simpler
12951// setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
12952// otherwise we will end up with copies of virtregs only valid along direct
12953// edges.
12954void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
12955 SmallVector<EVT, 8> ResultVTs;
12956 SmallVector<SDValue, 8> ResultValues;
12957 const auto *CBR =
12958 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
12959
12960 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12961 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
12962 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
12963
12964 Register InitialDef = FuncInfo.ValueMap[CBR];
12965 SDValue Chain = DAG.getRoot();
12966
12967 // Re-parse the asm constraints string.
12968 TargetLowering::AsmOperandInfoVector TargetConstraints =
12969 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
12970 for (auto &T : TargetConstraints) {
12971 SDISelAsmOperandInfo OpInfo(T);
12972 if (OpInfo.Type != InlineAsm::isOutput)
12973 continue;
12974
12975 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
12976 // individual constraint.
12977 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
12978
12979 switch (OpInfo.ConstraintType) {
12982 // Fill in OpInfo.AssignedRegs.Regs.
12983 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
12984
12985 // getRegistersForValue may produce 1 to many registers based on whether
12986 // the OpInfo.ConstraintVT is legal on the target or not.
12987 for (Register &Reg : OpInfo.AssignedRegs.Regs) {
12988 Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
12989 if (OriginalDef.isPhysical())
12990 FuncInfo.MBB->addLiveIn(OriginalDef);
12991 // Update the assigned registers to use the original defs.
12992 Reg = OriginalDef;
12993 }
12994
12995 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
12996 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
12997 ResultValues.push_back(V);
12998 ResultVTs.push_back(OpInfo.ConstraintVT);
12999 break;
13000 }
13002 SDValue Flag;
13003 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
13004 OpInfo, DAG);
13005 ++InitialDef;
13006 ResultValues.push_back(V);
13007 ResultVTs.push_back(OpInfo.ConstraintVT);
13008 break;
13009 }
13010 default:
13011 break;
13012 }
13013 }
13015 DAG.getVTList(ResultVTs), ResultValues);
13016 setValue(&I, V);
13017}
unsigned const MachineRegisterInfo * MRI
return SDValue()
static unsigned getIntrinsicID(const SDNode *N)
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
Function Alias Analysis Results
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
static AttributeList getReturnAttrs(FastISel::CallLoweringInfo &CLI)
Returns an AttributeList representing the attributes applied to the return value of the given call.
Definition FastISel.cpp:942
#define Check(C,...)
static Value * getCondition(Instruction *I)
Hexagon Common GEP
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
static void getRegistersForValue(MachineFunction &MF, MachineIRBuilder &MIRBuilder, GISelAsmOperandInfo &OpInfo, GISelAsmOperandInfo &RefOpInfo)
Assign virtual/physical registers for the specified register operand.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define RegName(no)
lazy value info
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
static bool isUndef(const MachineInstr &MI)
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static const Function * getCalledFunction(const Value *V)
This file provides utility analysis objects describing memory locations.
This file provides utility for Memory Model Relaxation Annotations (MMRAs).
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
#define T
#define T1
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static unsigned getAddressSpace(const Value *V, unsigned MaxLookup)
MachineInstr unsigned OpIdx
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t High
uint64_t IntrinsicInst * II
OptimizedStructLayoutField Field
#define P(N)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static bool hasOnlySelectUsers(const Value *Cond)
static SDValue getLoadStackGuard(SelectionDAG &DAG, const SDLoc &DL, SDValue &Chain)
Create a LOAD_STACK_GUARD node, and let it carry the target specific global variable if there exists ...
static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index, SDValue &Scale, SelectionDAGBuilder *SDB, const BasicBlock *CurBB, uint64_t ElemSize)
static void failForInvalidBundles(const CallBase &I, StringRef Name, ArrayRef< uint32_t > AllowedBundles)
static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx, const SDLoc &DL, SmallVectorImpl< SDValue > &Ops, SelectionDAGBuilder &Builder)
Add a stack map intrinsic call's live variable operands to a stackmap or patchpoint target node's ope...
static const unsigned MaxParallelChains
static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
visitPow - Lower a pow intrinsic.
static const CallBase * FindPreallocatedCall(const Value *PreallocatedSetup)
Given a @llvm.call.preallocated.setup, return the corresponding preallocated call.
static cl::opt< unsigned > SwitchPeelThreshold("switch-peel-threshold", cl::Hidden, cl::init(66), cl::desc("Set the case probability threshold for peeling the case from a " "switch statement. A value greater than 100 will void this " "optimization"))
static cl::opt< bool > InsertAssertAlign("insert-assert-align", cl::init(true), cl::desc("Insert the experimental `assertalign` node."), cl::ReallyHidden)
static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin)
static bool handleDanglingVariadicDebugInfo(SelectionDAG &DAG, DILocalVariable *Variable, DebugLoc DL, unsigned Order, SmallVectorImpl< Value * > &Values, DIExpression *Expression)
static unsigned findMatchingInlineAsmOperand(unsigned OperandNo, const std::vector< SDValue > &AsmNodeOperands)
static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo, SDISelAsmOperandInfo &MatchingOpInfo, SelectionDAG &DAG)
Make sure that the output operand OpInfo and its corresponding input operand MatchingOpInfo have comp...
static void findUnwindDestinations(FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB, BranchProbability Prob, SmallVectorImpl< std::pair< MachineBasicBlock *, BranchProbability > > &UnwindDests)
When an invoke or a cleanupret unwinds to the next EH pad, there are many places it could ultimately ...
static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic)
static BranchProbability scaleCaseProbality(BranchProbability CaseProb, BranchProbability PeeledCaseProb)
static SDValue expandExp2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp2 - Lower an exp2 intrinsic.
static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue Scale, SelectionDAG &DAG, const TargetLowering &TLI)
static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt, const SDLoc &dl)
getF32Constant - Get 32-bit floating point constant.
static SDValue widenVectorToPartType(SelectionDAG &DAG, SDValue Val, const SDLoc &DL, EVT PartVT)
static SDValue expandLog10(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog10 - Lower a log10 intrinsic.
DenseMap< const Argument *, std::pair< const AllocaInst *, const StoreInst * > > ArgCopyElisionMapTy
static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv)
getCopyToPartsVector - Create a series of nodes that contain the specified value split into legal par...
static void getUnderlyingArgRegs(SmallVectorImpl< std::pair< Register, TypeSize > > &Regs, const SDValue &N)
static void getCopyToParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, const Value *V, std::optional< CallingConv::ID > CallConv=std::nullopt, ISD::NodeType ExtendKind=ISD::ANY_EXTEND)
getCopyToParts - Create a series of nodes that contain the specified value split into legal parts.
static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, SelectionDAGBuilder &Builder)
static SDValue expandLog2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog2 - Lower a log2 intrinsic.
static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location, SDISelAsmOperandInfo &OpInfo, SelectionDAG &DAG)
Get a direct memory input to behave well as an indirect operand.
static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel)
isOnlyUsedInEntryBlock - If the specified argument is only used in the entry block,...
static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V, const Twine &ErrMsg)
static bool collectInstructionDeps(SmallMapVector< const Instruction *, bool, 8 > *Deps, const Value *V, SmallMapVector< const Instruction *, bool, 8 > *Necessary=nullptr, unsigned Depth=0)
static void findArgumentCopyElisionCandidates(const DataLayout &DL, FunctionLoweringInfo *FuncInfo, ArgCopyElisionMapTy &ArgCopyElisionCandidates)
Scan the entry block of the function in FuncInfo for arguments that look like copies into a local all...
static bool isFunction(SDValue Op)
static SDValue GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI, const SDLoc &dl)
GetExponent - Get the exponent:
static Register FollowCopyChain(MachineRegisterInfo &MRI, Register Reg)
static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS, SelectionDAG &DAG)
ExpandPowI - Expand a llvm.powi intrinsic.
static SDValue expandLog(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandLog - Lower a log intrinsic.
static SDValue getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC=std::nullopt, std::optional< ISD::NodeType > AssertOp=std::nullopt)
getCopyFromParts - Create a value that contains the specified legal parts combined into the value the...
static SDValue getLimitedPrecisionExp2(SDValue t0, const SDLoc &dl, SelectionDAG &DAG)
static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl)
GetSignificand - Get the significand and build it into a floating-point number with exponent of 1:
static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, const TargetLowering &TLI, SDNodeFlags Flags)
expandExp - Lower an exp intrinsic.
static const MDNode * getRangeMetadata(const Instruction &I)
static cl::opt< unsigned, true > LimitFPPrecision("limit-float-precision", cl::desc("Generate low-precision inline sequences " "for some float libcalls"), cl::location(LimitFloatPrecision), cl::Hidden, cl::init(0))
static void tryToElideArgumentCopy(FunctionLoweringInfo &FuncInfo, SmallVectorImpl< SDValue > &Chains, DenseMap< int, int > &ArgCopyElisionFrameIndexMap, SmallPtrSetImpl< const Instruction * > &ElidedArgCopyInstrs, ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg, ArrayRef< SDValue > ArgVals, bool &ArgHasUses)
Try to elide argument copies from memory into a local alloca.
static unsigned LimitFloatPrecision
LimitFloatPrecision - Generate low-precision inline sequences for some float libcalls (6,...
static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, SDValue InChain, std::optional< CallingConv::ID > CC)
getCopyFromPartsVector - Create a value that contains the specified legal parts combined into the val...
static bool InBlock(const Value *V, const BasicBlock *BB)
static FPClassTest getNoFPClass(const Instruction &I)
static LLVM_ATTRIBUTE_ALWAYS_INLINE MVT::SimpleValueType getSimpleVT(const uint8_t *MatcherTable, unsigned &MatcherIndex)
getSimpleVT - Decode a value in MatcherTable, if it's a VBR encoded value, use GetVBR to decode it.
This file defines the SmallPtrSet class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
uint16_t RegSizeInBits(const MCRegisterInfo &MRI, MCRegister RegNo)
Value * RHS
Value * LHS
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
Class for arbitrary precision integers.
Definition APInt.h:78
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
an instruction to allocate memory on the stack
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
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:339
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
Definition Argument.h:50
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
A cache of @llvm.assume calls within a function.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ Nand
*p = ~(old & v)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:361
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
This class represents a no-op cast from one type to another.
The address of a basic block.
Definition Constants.h:904
Conditional or Unconditional Branch instruction.
Analysis providing branch probability information.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static uint32_t getDenominator()
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
LLVM_ABI uint64_t scale(uint64_t Num) const
Scale a large integer.
BranchProbability getCompl() const
static BranchProbability getZero()
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
CallingConv::ID getCallingConv() const
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
LLVM_ABI bool isMustTailCall() const
Tests if this call site must be tail call optimized.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
unsigned countOperandBundlesOfType(StringRef Name) const
Return the number of operand bundles with the tag Name attached to this instruction.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
bool isConvergent() const
Determine if the invoke is convergent.
FunctionType * getFunctionType() const
unsigned arg_size() const
AttributeList getAttributes() const
Return the attributes for this call.
LLVM_ABI bool isTailCall() const
Tests if this call site is marked as a tail call.
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
This class represents a function call, abstracting a target machine's calling convention.
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:598
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1130
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
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:1037
uint64_t getZExtValue() const
Constant Vector Declarations.
Definition Constants.h:522
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:215
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:123
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:67
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
bool empty() const
Definition DenseMap.h:109
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:75
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:114
Diagnostic information for inline asm reporting.
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:315
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Class representing an expression and its matching format.
This instruction extracts a struct member or array element value from an aggregate value.
This instruction compares its operands according to the predicate given to the constructor.
This is a fast-path instruction selection class that generates poor code and doesn't support illegal ...
Definition FastISel.h:66
bool allowReassoc() const
Flag queries.
Definition FMF.h:64
An instruction for ordering other memory operations.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:802
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
BranchProbabilityInfo * BPI
MachineBasicBlock * getMBB(const BasicBlock *BB) const
DenseMap< const AllocaInst *, int > StaticAllocaMap
StaticAllocaMap - Keep track of frame indices for fixed sized allocas in the entry block.
const LiveOutInfo * GetLiveOutRegInfo(Register Reg)
GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the register is a PHI destinat...
MachineBasicBlock * MBB
MBB - The current block.
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Type * getParamType(unsigned i) const
Parameter type accessors.
Type * getReturnType() const
Data structure describing the variable locations in a function.
const BasicBlock & getEntryBlock() const
Definition Function.h:807
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:244
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition Function.cpp:742
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:270
Constant * getPersonalityFn() const
Get the personality function associated with this function.
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:249
size_t arg_size() const
Definition Function.h:899
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:730
Garbage collection metadata for a single function.
Definition GCMetadata.h:80
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
bool isInBounds() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
bool hasDLLImportStorageClass() const
Module * getParent()
Get the module that this global value is contained inside of...
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
This instruction inserts a struct field of array element value into an aggregate value.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
@ MIN_INT_BITS
Minimum number of bits that can be specified.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
A helper class to return the specified delimiter string after the first invocation of operator String...
An instruction for reading from memory.
static LocationSize precise(uint64_t Value)
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI MCSymbol * getOrCreateFrameAllocSymbol(const Twine &FuncName, unsigned Idx)
Gets a symbol that will be defined to the final stack offset of a local variable after codegen.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1078
Machine Value Type.
@ INVALID_SIMPLE_VALUE_TYPE
uint64_t getScalarSizeInBits() const
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
ElementCount getVectorElementCount() const
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
bool bitsGE(MVT VT) const
Return true if this has no less bits than VT.
bool isScalarInteger() const
Return true if this is an integer, not including vectors.
static MVT getVectorVT(MVT VT, unsigned NumElements)
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
static MVT getIntegerVT(unsigned BitWidth)
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void setIsEHContTarget(bool V=true)
Indicates if this is a target of Windows EH Continuation Guard.
void setIsEHFuncletEntry(bool V=true)
Indicates if this is the entry block of an EH funclet.
MachineInstrBundleIterator< MachineInstr > iterator
void setIsEHScopeEntry(bool V=true)
Indicates if this is the entry block of an EH scope, i.e., the block that that used to have a catchpa...
void setMachineBlockAddressTaken()
Set this block to indicate that its address is used as something other than the target of a terminato...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
int getStackProtectorIndex() const
Return the index for the stack protector object.
void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased)
Set "maybe pointed to by an LLVM IR value" for an object.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
void setFunctionContextIndex(int I)
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
bool useDebugInstrRef() const
Returns true if the function's variable locations are tracked with instruction referencing.
void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site)
Map the begin label for a call site.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void addCodeViewAnnotation(MCSymbol *Label, MDNode *MD)
Record annotations associated with a particular label.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
void setHasEHContTarget(bool V)
void addInvoke(MachineBasicBlock *LandingPad, MCSymbol *BeginLabel, MCSymbol *EndLabel)
Provide the begin and end labels of an invoke style call and associate it with a try landing pad bloc...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
Representation of each machine instruction.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateFI(int Idx)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI MCRegister getLiveInPhysReg(Register VReg) const
getLiveInPhysReg - If VReg is a live-in virtual register, return the corresponding live-in physical r...
An SDNode that represents everything that will be needed to construct a MachineInstr.
bool contains(const KeyT &Key) const
Definition MapVector.h:146
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
Definition MapVector.h:116
static MemoryLocation getAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location after Ptr, while remaining within the underlying objec...
Metadata wrapper in the Value hierarchy.
Definition Metadata.h:183
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
Resume the propagation of an exception.
Return a value (possibly void), from a function.
Holds the information from a dbg_label node through SDISel.
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getOpcode() const
SelectionDAGBuilder - This is the common target-independent lowering implementation that is parameter...
SDValue getValue(const Value *V)
getValue - Return an SDValue for the given Value.
DenseMap< const Constant *, Register > ConstantsOut
void addDanglingDebugInfo(SmallVectorImpl< Value * > &Values, DILocalVariable *Var, DIExpression *Expr, bool IsVariadic, DebugLoc DL, unsigned Order)
Register a dbg_value which relies on a Value which we have not yet seen.
void visitDbgInfo(const Instruction &I)
void clearDanglingDebugInfo()
Clear the dangling debug information map.
void LowerCallTo(const CallBase &CB, SDValue Callee, bool IsTailCall, bool IsMustTailCall, const BasicBlock *EHPadBB=nullptr, const TargetLowering::PtrAuthInfo *PAI=nullptr)
void clear()
Clear out the current SelectionDAG and the associated state and prepare this SelectionDAGBuilder obje...
void visitBitTestHeader(SwitchCG::BitTestBlock &B, MachineBasicBlock *SwitchBB)
visitBitTestHeader - This function emits necessary code to produce value suitable for "bit tests"
void LowerStatepoint(const GCStatepointInst &I, const BasicBlock *EHPadBB=nullptr)
std::unique_ptr< SDAGSwitchLowering > SL
SDValue lowerRangeToAssertZExt(SelectionDAG &DAG, const Instruction &I, SDValue Op)
bool HasTailCall
This is set to true if a call in the current block has been translated as a tail call.
bool ShouldEmitAsBranches(const std::vector< SwitchCG::CaseBlock > &Cases)
If the set of cases should be emitted as a series of branches, return true.
void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
EmitBranchForMergedCondition - Helper method for FindMergedConditions.
void LowerDeoptimizeCall(const CallInst *CI)
void LowerCallSiteWithDeoptBundle(const CallBase *Call, SDValue Callee, const BasicBlock *EHPadBB)
SwiftErrorValueTracking & SwiftError
Information about the swifterror values used throughout the function.
SDValue getNonRegisterValue(const Value *V)
getNonRegisterValue - Return an SDValue for the given Value, but don't look in FuncInfo....
const TargetTransformInfo * TTI
DenseMap< MachineBasicBlock *, SmallVector< unsigned, 4 > > LPadToCallSiteMap
Map a landing pad to the call site indexes.
SDValue lowerNoFPClassToAssertNoFPClass(SelectionDAG &DAG, const Instruction &I, SDValue Op)
void handleDebugDeclare(Value *Address, DILocalVariable *Variable, DIExpression *Expression, DebugLoc DL)
bool shouldKeepJumpConditionsTogether(const FunctionLoweringInfo &FuncInfo, const BranchInst &I, Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs, TargetLoweringBase::CondMergingParams Params) const
StatepointLoweringState StatepointLowering
State used while lowering a statepoint sequence (gc_statepoint, gc_relocate, and gc_result).
void visitBitTestCase(SwitchCG::BitTestBlock &BB, MachineBasicBlock *NextMBB, BranchProbability BranchProbToNext, Register Reg, SwitchCG::BitTestCase &B, MachineBasicBlock *SwitchBB)
visitBitTestCase - this function produces one "bit test"
bool canTailCall(const CallBase &CB) const
void populateCallLoweringInfo(TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, AttributeSet RetAttrs, bool IsPatchPoint)
Populate a CallLowerinInfo (into CLI) based on the properties of the call being lowered.
void CopyValueToVirtualRegister(const Value *V, Register Reg, ISD::NodeType ExtendType=ISD::ANY_EXTEND)
void salvageUnresolvedDbgValue(const Value *V, DanglingDebugInfo &DDI)
For the given dangling debuginfo record, perform last-ditch efforts to resolve the debuginfo to somet...
SmallVector< SDValue, 8 > PendingLoads
Loads are not emitted to the program immediately.
GCFunctionInfo * GFI
Garbage collection metadata for the function.
void init(GCFunctionInfo *gfi, BatchAAResults *BatchAA, AssumptionCache *AC, const TargetLibraryInfo *li, const TargetTransformInfo &TTI)
SDValue getRoot()
Similar to getMemoryRoot, but also flushes PendingConstrainedFP(Strict) items.
void ExportFromCurrentBlock(const Value *V)
ExportFromCurrentBlock - If this condition isn't known to be exported from the current basic block,...
void resolveOrClearDbgInfo()
Evict any dangling debug information, attempting to salvage it first.
std::pair< SDValue, SDValue > lowerInvokable(TargetLowering::CallLoweringInfo &CLI, const BasicBlock *EHPadBB=nullptr)
SDValue getMemoryRoot()
Return the current virtual root of the Selection DAG, flushing any PendingLoad items.
void resolveDanglingDebugInfo(const Value *V, SDValue Val)
If we saw an earlier dbg_value referring to V, generate the debug data structures now that we've seen...
void visit(const Instruction &I)
void dropDanglingDebugInfo(const DILocalVariable *Variable, const DIExpression *Expr)
If we have dangling debug info that describes Variable, or an overlapping part of variable considerin...
SDValue getCopyFromRegs(const Value *V, Type *Ty)
If there was virtual register allocated for the value V emit CopyFromReg of the specified type Ty.
void CopyToExportRegsIfNeeded(const Value *V)
CopyToExportRegsIfNeeded - If the given value has virtual registers created for it,...
void handleKillDebugValue(DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order)
Create a record for a kill location debug intrinsic.
void visitJumpTable(SwitchCG::JumpTable &JT)
visitJumpTable - Emit JumpTable node in the current MBB
SDValue getFPOperationRoot(fp::ExceptionBehavior EB)
Return the current virtual root of the Selection DAG, flushing PendingConstrainedFP or PendingConstra...
void visitJumpTableHeader(SwitchCG::JumpTable &JT, SwitchCG::JumpTableHeader &JTH, MachineBasicBlock *SwitchBB)
visitJumpTableHeader - This function emits necessary code to produce index in the JumpTable from swit...
void LowerCallSiteWithPtrAuthBundle(const CallBase &CB, const BasicBlock *EHPadBB)
static const unsigned LowestSDNodeOrder
Lowest valid SDNodeOrder.
FunctionLoweringInfo & FuncInfo
Information about the function as a whole.
void setValue(const Value *V, SDValue NewN)
void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB, MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB, Instruction::BinaryOps Opc, BranchProbability TProb, BranchProbability FProb, bool InvertCond)
const TargetLibraryInfo * LibInfo
bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB)
void visitSPDescriptorParent(StackProtectorDescriptor &SPD, MachineBasicBlock *ParentBB)
Codegen a new tail for a stack protector check ParentMBB which has had its tail spliced into a stack ...
bool handleDebugValue(ArrayRef< const Value * > Values, DILocalVariable *Var, DIExpression *Expr, DebugLoc DbgLoc, unsigned Order, bool IsVariadic)
For a given list of Values, attempt to create and record a SDDbgValue in the SelectionDAG.
SDValue getControlRoot()
Similar to getRoot, but instead of flushing all the PendingLoad items, flush all the PendingExports (...
void UpdateSplitBlock(MachineBasicBlock *First, MachineBasicBlock *Last)
When an MBB was split during scheduling, update the references that need to refer to the last resulti...
SDValue getValueImpl(const Value *V)
getValueImpl - Helper function for getValue and getNonRegisterValue.
void visitSwitchCase(SwitchCG::CaseBlock &CB, MachineBasicBlock *SwitchBB)
visitSwitchCase - Emits the necessary code to represent a single node in the binary search tree resul...
void visitSPDescriptorFailure(StackProtectorDescriptor &SPD)
Codegen the failure basic block for a stack protector check.
std::unique_ptr< FunctionLoweringInfo > FuncInfo
SmallPtrSet< const Instruction *, 4 > ElidedArgCopyInstrs
const TargetLowering * TLI
MachineRegisterInfo * RegInfo
std::unique_ptr< SwiftErrorValueTracking > SwiftError
virtual void emitFunctionEntryCode()
std::unique_ptr< SelectionDAGBuilder > SDB
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrnlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrlen(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, const CallInst *CI) const
virtual std::pair< SDValue, SDValue > 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 > EmitTargetCodeForMemcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, const CallInst *CI) const
Emit target-specific code that performs a memcmp/bcmp, in cases where that is faster than a libcall.
virtual std::pair< SDValue, SDValue > EmitTargetCodeForStrcmp(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, MachinePointerInfo Op1PtrInfo, MachinePointerInfo Op2PtrInfo) const
Emit target-specific code that performs a strcmp, in cases where that is faster than a libcall.
virtual SDValue EmitTargetCodeForSetTag(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Addr, SDValue Size, MachinePointerInfo DstPtrInfo, bool ZeroData) const
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 getExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT, unsigned Opcode)
Convert Op, which must be of integer type, to the integer type VT, by either any/sign/zero-extending ...
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
const TargetSubtargetInfo & getSubtarget() const
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
const TargetLowering & getTargetLoweringInfo() const
static constexpr unsigned MaxRecursionDepth
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
const DataLayout & getDataLayout() const
SDValue getTargetFrameIndex(int FI, EVT VT)
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
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.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void swap(SmallVectorImpl &RHS)
void resize(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Encapsulates all of the information needed to generate a stack protector check, and signals to isel w...
MachineBasicBlock * getSuccessMBB()
MachineBasicBlock * getFailureMBB()
MachineBasicBlock * getParentMBB()
bool shouldEmitFunctionBasedCheckStackProtector() const
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Multiway switch.
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Provides information about what library functions are available for the current target.
virtual Align getByValTypeAlignment(Type *Ty, const DataLayout &DL) const
Returns the desired alignment for ByVal or InAlloca aggregate function arguments in the caller parame...
virtual bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, EVT) const
Return true if an FMA operation is faster than a pair of fmul and fadd instructions.
EVT getMemValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
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 bool useStackGuardXorFP() const
If this function returns true, stack protection checks should XOR the frame pointer (or whichever poi...
virtual const TargetRegisterClass * getRegClassFor(MVT VT, bool isDivergent=false) const
Return the register class that should be used for the specified value type.
virtual bool isLegalScaleForGatherScatter(uint64_t Scale, uint64_t ElemSize) const
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
virtual unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain targets require unusual breakdowns of certain types.
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
virtual unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const
Return the number of registers that this ValueType will eventually require.
unsigned getBitWidthForCttzElements(Type *RetTy, ElementCount EC, bool ZeroIsPoison, const ConstantRange *VScaleRange) const
Return the minimum number of bits required to hold the maximum possible number of trailing zero vecto...
virtual bool shouldExtendGSIndex(EVT VT, EVT &EltTy) const
Returns true if the index type for a masked gather/scatter requires extending.
virtual unsigned getVectorTypeBreakdownForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Certain targets such as MIPS require that some types such as vectors are always broken down into scal...
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 bool getTgtMemIntrinsic(IntrinsicInfo &, const CallBase &, MachineFunction &, unsigned) const
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
MachineMemOperand::Flags getLoadMemOperandFlags(const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC=nullptr, const TargetLibraryInfo *LibInfo=nullptr) const
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
MVT getProgramPointerTy(const DataLayout &DL) const
Return the type for code pointers, which is determined by the program address space specified through...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
virtual bool shouldExpandVectorMatch(EVT VT, unsigned SearchSize) const
Return true if the @llvm.experimental.vector.match intrinsic should be expanded for vector type ‘VT’ ...
virtual MVT getFenceOperandTy(const DataLayout &DL) const
Return the type for operands of fence.
virtual bool shouldExpandGetVectorLength(EVT CountVT, unsigned VF, bool IsScalable) const
bool isOperationLegalOrCustom(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
virtual MVT hasFastEqualityCompare(unsigned NumBits) const
Return the preferred operand type if the target has a quick way to compare integer values of the give...
MachineMemOperand::Flags getStoreMemOperandFlags(const StoreInst &SI, const DataLayout &DL) const
virtual bool shouldExpandCttzElements(EVT VT) const
Return true if the @llvm.experimental.cttz.elts intrinsic should be expanded using generic code in Se...
virtual bool signExtendConstant(const ConstantInt *C) const
Return true if this constant should be sign extended when promoting to a larger type.
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 SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, const SDLoc &DL) const
virtual EVT getTypeForExtReturn(LLVMContext &Context, EVT VT, ISD::NodeType) const
Return the type that should be used to zero or sign extend a zeroext/signext integer return value.
virtual InlineAsm::ConstraintCode getInlineAsmMemConstraint(StringRef ConstraintCode) const
std::vector< AsmOperandInfo > AsmOperandInfoVector
SDValue expandIS_FPCLASS(EVT ResultVT, SDValue Op, FPClassTest Test, SDNodeFlags Flags, const SDLoc &DL, SelectionDAG &DAG) const
Expand check for floating point class.
virtual SDValue prepareVolatileOrAtomicLoad(SDValue Chain, const SDLoc &DL, SelectionDAG &DAG) const
This callback is used to prepare for a volatile or atomic load.
virtual ConstraintType getConstraintType(StringRef Constraint) const
Given a constraint, return the type of constraint it is for this target.
virtual bool splitValueIntoRegisterParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, unsigned NumParts, MVT PartVT, std::optional< CallingConv::ID > CC) const
Target-specific splitting of values into parts that fit a register storing a legal type.
virtual SDValue joinRegisterPartsIntoValue(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts, MVT PartVT, EVT ValueVT, std::optional< CallingConv::ID > CC) const
Target-specific combining of register parts into its original value.
virtual SDValue LowerCall(CallLoweringInfo &, SmallVectorImpl< SDValue > &) const
This hook must be implemented to lower calls into the specified DAG.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual SDValue LowerAsmOutputForConstraint(SDValue &Chain, SDValue &Glue, const SDLoc &DL, const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const
virtual std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const
Given a physical register constraint (e.g.
virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, const CallBase &Call) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const
This callback is invoked for operations that are unsupported by the target, which are registered to u...
virtual bool functionArgumentNeedsConsecutiveRegisters(Type *Ty, CallingConv::ID CallConv, bool isVarArg, const DataLayout &DL) const
For some targets, an LLVM struct type must be broken down into multiple simple types,...
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo,...
virtual void CollectTargetIntrinsicOperands(const CallInst &I, SmallVectorImpl< SDValue > &Ops, SelectionDAG &DAG) const
virtual SDValue visitMaskedStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, MachineMemOperand *MMO, SDValue Ptr, SDValue Val, SDValue Mask) const
virtual bool useLoadStackGuardNode(const Module &M) const
If this function returns true, SelectionDAGBuilder emits a LOAD_STACK_GUARD node when it is lowering ...
virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, std::vector< SDValue > &Ops, SelectionDAG &DAG) const
Lower the specified operand into the Ops vector.
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...
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
unsigned getID() const
Return the register class ID number.
iterator begin() const
begin/end - Return all of the registers in this class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
@ TCK_Latency
The latency of instruction.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI bool isEmptyTy() const
Return true if this type is empty, that is, it has no elements or all of its elements are empty.
Definition Type.cpp:180
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:293
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:234
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:300
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_iterator op_begin()
Definition User.h: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:256
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:259
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:708
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:322
Base class of all SIMD vector types.
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
const ParentTy * getParent() const
Definition ilist_node.h:34
A raw_ostream that writes to an std::string.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h: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.
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:787
@ 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.
@ LOOP_DEPENDENCE_RAW_MASK
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ 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:778
@ 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:852
@ 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:879
@ 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:746
@ 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...
Definition ISDOpcodes.h:992
@ 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:773
@ 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
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ATOMIC_LOAD_USUB_SAT
@ 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:974
@ CONVERGENCECTRL_GLUE
This does not correspond to any convergence control intrinsic.
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:843
@ 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.
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:786
@ 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:795
@ 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:671
@ 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:969
@ 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:764
@ 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:849
@ 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, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by IMM elements and retu...
Definition ISDOpcodes.h:653
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:726
@ 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:977
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:804
@ PCMARKER
PCMARKER - This corresponds to the pcmarker intrinsic.
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ 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
@ 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:925
@ 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:738
@ 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:734
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) right by IMM elements and re...
Definition ISDOpcodes.h:656
@ 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,...
@ 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:958
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:698
@ 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:944
@ 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:855
@ BRCOND
BRCOND - Conditional branch.
@ VECREDUCE_SEQ_FMUL
@ 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.
@ 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
@ 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_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Offsets
Offsets in bytes from the start of the input buffer.
std::pair< JumpTableHeader, JumpTable > JumpTableBlock
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::vector< CaseCluster > CaseClusterVector
@ CC_Range
A cluster of adjacent case labels with the same destination, or just one case.
@ CC_JumpTable
A cluster of cases suitable for jump table lowering.
@ CC_BitTests
A cluster of cases suitable for bit test lowering.
SmallVector< SwitchWorkListItem, 4 > SwitchWorkList
CaseClusterVector::iterator CaseClusterIt
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
ExceptionBehavior
Exception behavior used for floating point operations.
Definition FPEnv.h:39
@ ebStrict
This corresponds to "fpexcept.strict".
Definition FPEnv.h:42
@ ebMayTrap
This corresponds to "fpexcept.maytrap".
Definition FPEnv.h:41
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition FPEnv.h:40
constexpr float log2ef
Definition MathExtras.h:51
constexpr double e
constexpr float ln2f
Definition MathExtras.h:49
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp: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:1737
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:1667
LLVM_ABI void GetReturnInfo(CallingConv::ID CC, Type *ReturnType, AttributeList attr, SmallVectorImpl< ISD::OutputArg > &Outs, const TargetLowering &TLI, const DataLayout &DL)
Given an LLVM IR type and return type attributes, compute the return value EVTs and flags,...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
void ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL, Type *Ty, SmallVectorImpl< EVT > &ValueVTs, SmallVectorImpl< EVT > *MemVTs=nullptr, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
ComputeValueVTs - Given an LLVM IR type, compute a sequence of EVTs that represent all the individual...
Definition Analysis.cpp:119
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
LLVM_ABI void diagnoseDontCall(const CallInst &CI)
auto successors(const MachineBasicBlock *BB)
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
static ConstantRange getRange(Value *Op, SCCPSolver &Solver, const SmallPtrSetImpl< Value * > &InsertedValues)
Helper for getting ranges from Solver.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
auto cast_or_null(const Y &Val)
Definition Casting.h:714
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
Definition MathExtras.h:546
gep_type_iterator gep_type_end(const User *GEP)
constexpr 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:2163
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:154
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
Definition STLExtras.h:1150
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
void ComputeValueTypes(const DataLayout &DL, Type *Ty, SmallVectorImpl< Type * > &Types, SmallVectorImpl< TypeSize > *Offsets=nullptr, TypeSize StartingOffset=TypeSize::getZero())
Given an LLVM IR type, compute non-aggregate subtypes.
Definition Analysis.cpp:72
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI llvm::SmallVector< int, 16 > createStrideMask(unsigned Start, unsigned Stride, unsigned VF)
Create a stride shuffle mask.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_FMINNUM
Unsigned maximum.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
detail::zippy< detail::zip_first, T, U, Args... > zip_first(T &&t, U &&u, Args &&...args)
zip iterator that, for the sake of efficiency, assumes the first iteratee to be the shortest.
Definition STLExtras.h:852
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
generic_gep_type_iterator<> gep_type_iterator
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
auto succ_size(const MachineBasicBlock *BB)
bool hasSingleElement(ContainerTy &&C)
Returns true if the given container only contains a single element.
Definition STLExtras.h:300
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred)
getFCmpCondCode - Return the ISD condition code corresponding to the given LLVM IR floating-point con...
Definition Analysis.cpp: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:2274
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Global
Append to llvm.global_dtors.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
bool isFuncletEHPersonality(EHPersonality Pers)
Returns true if this is a personality function that invokes handler funclets (which must return to it...
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
LLVM_ABI llvm::SmallVector< int, 16 > createInterleaveMask(unsigned VF, unsigned NumVecs)
Create an interleave shuffle mask.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ And
Bitwise or logical AND of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:539
DWARFExpression::Operation Op
ISD::CondCode getFCmpCodeWithoutNaN(ISD::CondCode CC)
getFCmpCodeWithoutNaN - Given an ISD condition code comparing floats, return the equivalent code if w...
Definition Analysis.cpp: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 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:2182
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:1945
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:2156
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
unsigned ComputeLinearIndex(Type *Ty, const unsigned *Indices, const unsigned *IndicesEnd, unsigned CurIndex=0)
Compute the linearized index of a member in a nested aggregate/struct/array.
Definition Analysis.cpp:33
T bit_floor(T Value)
Returns the largest integral power of two no greater than Value if Value is nonzero.
Definition bit.h:330
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
#define NC
Definition regutils.h:42
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:395
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
uint64_t getScalarStoreSize() const
Definition ValueTypes.h:402
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
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:102
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isRISCVVectorTuple() const
Return true if this is a vector value type.
Definition ValueTypes.h:179
uint64_t getFixedSizeInBits() const
Return the size of the specified fixed width value type in bits.
Definition ValueTypes.h:381
bool isFixedLengthVector() const
Definition ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
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:113
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:157
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
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:261
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:276
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.
void addIPToStateRange(const InvokeInst *II, MCSymbol *InvokeBegin, MCSymbol *InvokeEnd)