LLVM 22.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
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 the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <set>
80#include <string>
81#include <utility>
82#include <vector>
83
84using namespace llvm;
85using namespace llvm::SDPatternMatch;
86
87/// makeVTList - Return an instance of the SDVTList struct initialized with the
88/// specified members.
89static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
90 SDVTList Res = {VTs, NumVTs};
91 return Res;
92}
93
94// Default null implementations of the callbacks.
98
99void SelectionDAG::DAGNodeDeletedListener::anchor() {}
100void SelectionDAG::DAGNodeInsertedListener::anchor() {}
101
102#define DEBUG_TYPE "selectiondag"
103
104static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
105 cl::Hidden, cl::init(true),
106 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
107
108static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
109 cl::desc("Number limit for gluing ld/st of memcpy."),
110 cl::Hidden, cl::init(0));
111
113 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
114 cl::desc("DAG combiner limit number of steps when searching DAG "
115 "for predecessor nodes"));
116
118 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
119}
120
122
123//===----------------------------------------------------------------------===//
124// ConstantFPSDNode Class
125//===----------------------------------------------------------------------===//
126
127/// isExactlyValue - We don't rely on operator== working on double values, as
128/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
129/// As such, this method can be used to do an exact bit-for-bit comparison of
130/// two floating point values.
132 return getValueAPF().bitwiseIsEqual(V);
133}
134
136 const APFloat& Val) {
137 assert(VT.isFloatingPoint() && "Can only convert between FP types");
138
139 // convert modifies in place, so make a copy.
140 APFloat Val2 = APFloat(Val);
141 bool losesInfo;
143 &losesInfo);
144 return !losesInfo;
145}
146
147//===----------------------------------------------------------------------===//
148// ISD Namespace
149//===----------------------------------------------------------------------===//
150
151bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
152 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
153 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
154 unsigned EltSize =
155 N->getValueType(0).getVectorElementType().getSizeInBits();
156 SplatVal = OptAPInt->trunc(EltSize);
157 return true;
158 }
159 }
160
161 auto *BV = dyn_cast<BuildVectorSDNode>(N);
162 if (!BV)
163 return false;
164
165 APInt SplatUndef;
166 unsigned SplatBitSize;
167 bool HasUndefs;
168 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
169 // Endianness does not matter here. We are checking for a splat given the
170 // element size of the vector, and if we find such a splat for little endian
171 // layout, then that should be valid also for big endian (as the full vector
172 // size is known to be a multiple of the element size).
173 const bool IsBigEndian = false;
174 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
175 EltSize, IsBigEndian) &&
176 EltSize == SplatBitSize;
177}
178
179// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
180// specializations of the more general isConstantSplatVector()?
181
182bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
183 // Look through a bit convert.
184 while (N->getOpcode() == ISD::BITCAST)
185 N = N->getOperand(0).getNode();
186
187 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
188 APInt SplatVal;
189 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
190 }
191
192 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
193
194 unsigned i = 0, e = N->getNumOperands();
195
196 // Skip over all of the undef values.
197 while (i != e && N->getOperand(i).isUndef())
198 ++i;
199
200 // Do not accept an all-undef vector.
201 if (i == e) return false;
202
203 // Do not accept build_vectors that aren't all constants or which have non-~0
204 // elements. We have to be a bit careful here, as the type of the constant
205 // may not be the same as the type of the vector elements due to type
206 // legalization (the elements are promoted to a legal type for the target and
207 // a vector of a type may be legal when the base element type is not).
208 // We only want to check enough bits to cover the vector elements, because
209 // we care if the resultant vector is all ones, not whether the individual
210 // constants are.
211 SDValue NotZero = N->getOperand(i);
212 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
213 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
214 if (OptAPInt->countr_one() < EltSize)
215 return false;
216 } else
217 return false;
218
219 // Okay, we have at least one ~0 value, check to see if the rest match or are
220 // undefs. Even with the above element type twiddling, this should be OK, as
221 // the same type legalization should have applied to all the elements.
222 for (++i; i != e; ++i)
223 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
224 return false;
225 return true;
226}
227
228bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
229 // Look through a bit convert.
230 while (N->getOpcode() == ISD::BITCAST)
231 N = N->getOperand(0).getNode();
232
233 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
234 APInt SplatVal;
235 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
236 }
237
238 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
239
240 bool IsAllUndef = true;
241 for (const SDValue &Op : N->op_values()) {
242 if (Op.isUndef())
243 continue;
244 IsAllUndef = false;
245 // Do not accept build_vectors that aren't all constants or which have non-0
246 // elements. We have to be a bit careful here, as the type of the constant
247 // may not be the same as the type of the vector elements due to type
248 // legalization (the elements are promoted to a legal type for the target
249 // and a vector of a type may be legal when the base element type is not).
250 // We only want to check enough bits to cover the vector elements, because
251 // we care if the resultant vector is all zeros, not whether the individual
252 // constants are.
253 if (auto OptAPInt = Op->bitcastToAPInt()) {
254 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
255 if (OptAPInt->countr_zero() < EltSize)
256 return false;
257 } else
258 return false;
259 }
260
261 // Do not accept an all-undef vector.
262 if (IsAllUndef)
263 return false;
264 return true;
265}
266
268 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
269}
270
272 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
273}
274
276 if (N->getOpcode() != ISD::BUILD_VECTOR)
277 return false;
278
279 for (const SDValue &Op : N->op_values()) {
280 if (Op.isUndef())
281 continue;
283 return false;
284 }
285 return true;
286}
287
289 if (N->getOpcode() != ISD::BUILD_VECTOR)
290 return false;
291
292 for (const SDValue &Op : N->op_values()) {
293 if (Op.isUndef())
294 continue;
296 return false;
297 }
298 return true;
299}
300
301bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
302 bool Signed) {
303 assert(N->getValueType(0).isVector() && "Expected a vector!");
304
305 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
306 if (EltSize <= NewEltSize)
307 return false;
308
309 if (N->getOpcode() == ISD::ZERO_EXTEND) {
310 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
311 NewEltSize) &&
312 !Signed;
313 }
314 if (N->getOpcode() == ISD::SIGN_EXTEND) {
315 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
316 NewEltSize) &&
317 Signed;
318 }
319 if (N->getOpcode() != ISD::BUILD_VECTOR)
320 return false;
321
322 for (const SDValue &Op : N->op_values()) {
323 if (Op.isUndef())
324 continue;
326 return false;
327
328 APInt C = Op->getAsAPIntVal().trunc(EltSize);
329 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
330 return false;
331 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
332 return false;
333 }
334
335 return true;
336}
337
339 // Return false if the node has no operands.
340 // This is "logically inconsistent" with the definition of "all" but
341 // is probably the desired behavior.
342 if (N->getNumOperands() == 0)
343 return false;
344 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
345}
346
348 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
349}
350
351template <typename ConstNodeType>
353 std::function<bool(ConstNodeType *)> Match,
354 bool AllowUndefs, bool AllowTruncation) {
355 // FIXME: Add support for scalar UNDEF cases?
356 if (auto *C = dyn_cast<ConstNodeType>(Op))
357 return Match(C);
358
359 // FIXME: Add support for vector UNDEF cases?
360 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
361 ISD::SPLAT_VECTOR != Op.getOpcode())
362 return false;
363
364 EVT SVT = Op.getValueType().getScalarType();
365 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
366 if (AllowUndefs && Op.getOperand(i).isUndef()) {
367 if (!Match(nullptr))
368 return false;
369 continue;
370 }
371
372 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
373 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
374 !Match(Cst))
375 return false;
376 }
377 return true;
378}
379// Build used template types.
381 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
383 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
384
386 SDValue LHS, SDValue RHS,
387 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
388 bool AllowUndefs, bool AllowTypeMismatch) {
389 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
390 return false;
391
392 // TODO: Add support for scalar UNDEF cases?
393 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
394 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
395 return Match(LHSCst, RHSCst);
396
397 // TODO: Add support for vector UNDEF cases?
398 if (LHS.getOpcode() != RHS.getOpcode() ||
399 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
400 LHS.getOpcode() != ISD::SPLAT_VECTOR))
401 return false;
402
403 EVT SVT = LHS.getValueType().getScalarType();
404 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
405 SDValue LHSOp = LHS.getOperand(i);
406 SDValue RHSOp = RHS.getOperand(i);
407 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
408 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
409 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
410 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
411 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
412 return false;
413 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
414 LHSOp.getValueType() != RHSOp.getValueType()))
415 return false;
416 if (!Match(LHSCst, RHSCst))
417 return false;
418 }
419 return true;
420}
421
423 switch (MinMaxOpc) {
424 default:
425 llvm_unreachable("unrecognized opcode");
426 case ISD::UMIN:
427 return ISD::UMAX;
428 case ISD::UMAX:
429 return ISD::UMIN;
430 case ISD::SMIN:
431 return ISD::SMAX;
432 case ISD::SMAX:
433 return ISD::SMIN;
434 }
435}
436
438 switch (VecReduceOpcode) {
439 default:
440 llvm_unreachable("Expected VECREDUCE opcode");
441 case ISD::VECREDUCE_FADD:
442 case ISD::VECREDUCE_SEQ_FADD:
443 case ISD::VP_REDUCE_FADD:
444 case ISD::VP_REDUCE_SEQ_FADD:
445 return ISD::FADD;
446 case ISD::VECREDUCE_FMUL:
447 case ISD::VECREDUCE_SEQ_FMUL:
448 case ISD::VP_REDUCE_FMUL:
449 case ISD::VP_REDUCE_SEQ_FMUL:
450 return ISD::FMUL;
451 case ISD::VECREDUCE_ADD:
452 case ISD::VP_REDUCE_ADD:
453 return ISD::ADD;
454 case ISD::VECREDUCE_MUL:
455 case ISD::VP_REDUCE_MUL:
456 return ISD::MUL;
457 case ISD::VECREDUCE_AND:
458 case ISD::VP_REDUCE_AND:
459 return ISD::AND;
460 case ISD::VECREDUCE_OR:
461 case ISD::VP_REDUCE_OR:
462 return ISD::OR;
463 case ISD::VECREDUCE_XOR:
464 case ISD::VP_REDUCE_XOR:
465 return ISD::XOR;
466 case ISD::VECREDUCE_SMAX:
467 case ISD::VP_REDUCE_SMAX:
468 return ISD::SMAX;
469 case ISD::VECREDUCE_SMIN:
470 case ISD::VP_REDUCE_SMIN:
471 return ISD::SMIN;
472 case ISD::VECREDUCE_UMAX:
473 case ISD::VP_REDUCE_UMAX:
474 return ISD::UMAX;
475 case ISD::VECREDUCE_UMIN:
476 case ISD::VP_REDUCE_UMIN:
477 return ISD::UMIN;
478 case ISD::VECREDUCE_FMAX:
479 case ISD::VP_REDUCE_FMAX:
480 return ISD::FMAXNUM;
481 case ISD::VECREDUCE_FMIN:
482 case ISD::VP_REDUCE_FMIN:
483 return ISD::FMINNUM;
484 case ISD::VECREDUCE_FMAXIMUM:
485 case ISD::VP_REDUCE_FMAXIMUM:
486 return ISD::FMAXIMUM;
487 case ISD::VECREDUCE_FMINIMUM:
488 case ISD::VP_REDUCE_FMINIMUM:
489 return ISD::FMINIMUM;
490 }
491}
492
493bool ISD::isVPOpcode(unsigned Opcode) {
494 switch (Opcode) {
495 default:
496 return false;
497#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
498 case ISD::VPSD: \
499 return true;
500#include "llvm/IR/VPIntrinsics.def"
501 }
502}
503
504bool ISD::isVPBinaryOp(unsigned Opcode) {
505 switch (Opcode) {
506 default:
507 break;
508#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
509#define VP_PROPERTY_BINARYOP return true;
510#define END_REGISTER_VP_SDNODE(VPSD) break;
511#include "llvm/IR/VPIntrinsics.def"
512 }
513 return false;
514}
515
516bool ISD::isVPReduction(unsigned Opcode) {
517 switch (Opcode) {
518 default:
519 return false;
520 case ISD::VP_REDUCE_ADD:
521 case ISD::VP_REDUCE_MUL:
522 case ISD::VP_REDUCE_AND:
523 case ISD::VP_REDUCE_OR:
524 case ISD::VP_REDUCE_XOR:
525 case ISD::VP_REDUCE_SMAX:
526 case ISD::VP_REDUCE_SMIN:
527 case ISD::VP_REDUCE_UMAX:
528 case ISD::VP_REDUCE_UMIN:
529 case ISD::VP_REDUCE_FMAX:
530 case ISD::VP_REDUCE_FMIN:
531 case ISD::VP_REDUCE_FMAXIMUM:
532 case ISD::VP_REDUCE_FMINIMUM:
533 case ISD::VP_REDUCE_FADD:
534 case ISD::VP_REDUCE_FMUL:
535 case ISD::VP_REDUCE_SEQ_FADD:
536 case ISD::VP_REDUCE_SEQ_FMUL:
537 return true;
538 }
539}
540
541/// The operand position of the vector mask.
542std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
543 switch (Opcode) {
544 default:
545 return std::nullopt;
546#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
547 case ISD::VPSD: \
548 return MASKPOS;
549#include "llvm/IR/VPIntrinsics.def"
550 }
551}
552
553/// The operand position of the explicit vector length parameter.
554std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
555 switch (Opcode) {
556 default:
557 return std::nullopt;
558#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
559 case ISD::VPSD: \
560 return EVLPOS;
561#include "llvm/IR/VPIntrinsics.def"
562 }
563}
564
565std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
566 bool hasFPExcept) {
567 // FIXME: Return strict opcodes in case of fp exceptions.
568 switch (VPOpcode) {
569 default:
570 return std::nullopt;
571#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
572#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
573#define END_REGISTER_VP_SDNODE(VPOPC) break;
574#include "llvm/IR/VPIntrinsics.def"
575 }
576 return std::nullopt;
577}
578
579std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
580 switch (Opcode) {
581 default:
582 return std::nullopt;
583#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
584#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
585#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
586#include "llvm/IR/VPIntrinsics.def"
587 }
588}
589
591 switch (ExtType) {
592 case ISD::EXTLOAD:
593 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
594 case ISD::SEXTLOAD:
595 return ISD::SIGN_EXTEND;
596 case ISD::ZEXTLOAD:
597 return ISD::ZERO_EXTEND;
598 default:
599 break;
600 }
601
602 llvm_unreachable("Invalid LoadExtType");
603}
604
606 // To perform this operation, we just need to swap the L and G bits of the
607 // operation.
608 unsigned OldL = (Operation >> 2) & 1;
609 unsigned OldG = (Operation >> 1) & 1;
610 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
611 (OldL << 1) | // New G bit
612 (OldG << 2)); // New L bit.
613}
614
616 unsigned Operation = Op;
617 if (isIntegerLike)
618 Operation ^= 7; // Flip L, G, E bits, but not U.
619 else
620 Operation ^= 15; // Flip all of the condition bits.
621
623 Operation &= ~8; // Don't let N and U bits get set.
624
625 return ISD::CondCode(Operation);
626}
627
631
633 bool isIntegerLike) {
634 return getSetCCInverseImpl(Op, isIntegerLike);
635}
636
637/// For an integer comparison, return 1 if the comparison is a signed operation
638/// and 2 if the result is an unsigned comparison. Return zero if the operation
639/// does not depend on the sign of the input (setne and seteq).
640static int isSignedOp(ISD::CondCode Opcode) {
641 switch (Opcode) {
642 default: llvm_unreachable("Illegal integer setcc operation!");
643 case ISD::SETEQ:
644 case ISD::SETNE: return 0;
645 case ISD::SETLT:
646 case ISD::SETLE:
647 case ISD::SETGT:
648 case ISD::SETGE: return 1;
649 case ISD::SETULT:
650 case ISD::SETULE:
651 case ISD::SETUGT:
652 case ISD::SETUGE: return 2;
653 }
654}
655
657 EVT Type) {
658 bool IsInteger = Type.isInteger();
659 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
660 // Cannot fold a signed integer setcc with an unsigned integer setcc.
661 return ISD::SETCC_INVALID;
662
663 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
664
665 // If the N and U bits get set, then the resultant comparison DOES suddenly
666 // care about orderedness, and it is true when ordered.
667 if (Op > ISD::SETTRUE2)
668 Op &= ~16; // Clear the U bit if the N bit is set.
669
670 // Canonicalize illegal integer setcc's.
671 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
672 Op = ISD::SETNE;
673
674 return ISD::CondCode(Op);
675}
676
678 EVT Type) {
679 bool IsInteger = Type.isInteger();
680 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
681 // Cannot fold a signed setcc with an unsigned setcc.
682 return ISD::SETCC_INVALID;
683
684 // Combine all of the condition bits.
685 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
686
687 // Canonicalize illegal integer setcc's.
688 if (IsInteger) {
689 switch (Result) {
690 default: break;
691 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
692 case ISD::SETOEQ: // SETEQ & SETU[LG]E
693 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
694 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
695 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
696 }
697 }
698
699 return Result;
700}
701
702//===----------------------------------------------------------------------===//
703// SDNode Profile Support
704//===----------------------------------------------------------------------===//
705
706/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
707static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
708 ID.AddInteger(OpC);
709}
710
711/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
712/// solely with their pointer.
714 ID.AddPointer(VTList.VTs);
715}
716
717/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
720 for (const auto &Op : Ops) {
721 ID.AddPointer(Op.getNode());
722 ID.AddInteger(Op.getResNo());
723 }
724}
725
726/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
729 for (const auto &Op : Ops) {
730 ID.AddPointer(Op.getNode());
731 ID.AddInteger(Op.getResNo());
732 }
733}
734
735static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
736 SDVTList VTList, ArrayRef<SDValue> OpList) {
737 AddNodeIDOpcode(ID, OpC);
738 AddNodeIDValueTypes(ID, VTList);
739 AddNodeIDOperands(ID, OpList);
740}
741
742/// If this is an SDNode with special info, add this info to the NodeID data.
744 switch (N->getOpcode()) {
747 case ISD::MCSymbol:
748 llvm_unreachable("Should only be used on nodes with operands");
749 default: break; // Normal nodes don't need extra info.
751 case ISD::Constant: {
753 ID.AddPointer(C->getConstantIntValue());
754 ID.AddBoolean(C->isOpaque());
755 break;
756 }
758 case ISD::ConstantFP:
759 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
760 break;
766 ID.AddPointer(GA->getGlobal());
767 ID.AddInteger(GA->getOffset());
768 ID.AddInteger(GA->getTargetFlags());
769 break;
770 }
771 case ISD::BasicBlock:
773 break;
774 case ISD::Register:
775 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
776 break;
778 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
779 break;
780 case ISD::SRCVALUE:
781 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
782 break;
783 case ISD::FrameIndex:
785 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
786 break;
787 case ISD::PSEUDO_PROBE:
788 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
789 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
790 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
791 break;
792 case ISD::JumpTable:
794 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
795 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
796 break;
800 ID.AddInteger(CP->getAlign().value());
801 ID.AddInteger(CP->getOffset());
802 if (CP->isMachineConstantPoolEntry())
803 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
804 else
805 ID.AddPointer(CP->getConstVal());
806 ID.AddInteger(CP->getTargetFlags());
807 break;
808 }
809 case ISD::TargetIndex: {
811 ID.AddInteger(TI->getIndex());
812 ID.AddInteger(TI->getOffset());
813 ID.AddInteger(TI->getTargetFlags());
814 break;
815 }
816 case ISD::LOAD: {
817 const LoadSDNode *LD = cast<LoadSDNode>(N);
818 ID.AddInteger(LD->getMemoryVT().getRawBits());
819 ID.AddInteger(LD->getRawSubclassData());
820 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
821 ID.AddInteger(LD->getMemOperand()->getFlags());
822 break;
823 }
824 case ISD::STORE: {
825 const StoreSDNode *ST = cast<StoreSDNode>(N);
826 ID.AddInteger(ST->getMemoryVT().getRawBits());
827 ID.AddInteger(ST->getRawSubclassData());
828 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
829 ID.AddInteger(ST->getMemOperand()->getFlags());
830 break;
831 }
832 case ISD::VP_LOAD: {
833 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
834 ID.AddInteger(ELD->getMemoryVT().getRawBits());
835 ID.AddInteger(ELD->getRawSubclassData());
836 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
837 ID.AddInteger(ELD->getMemOperand()->getFlags());
838 break;
839 }
840 case ISD::VP_LOAD_FF: {
841 const auto *LD = cast<VPLoadFFSDNode>(N);
842 ID.AddInteger(LD->getMemoryVT().getRawBits());
843 ID.AddInteger(LD->getRawSubclassData());
844 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
845 ID.AddInteger(LD->getMemOperand()->getFlags());
846 break;
847 }
848 case ISD::VP_STORE: {
849 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
850 ID.AddInteger(EST->getMemoryVT().getRawBits());
851 ID.AddInteger(EST->getRawSubclassData());
852 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
853 ID.AddInteger(EST->getMemOperand()->getFlags());
854 break;
855 }
856 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
858 ID.AddInteger(SLD->getMemoryVT().getRawBits());
859 ID.AddInteger(SLD->getRawSubclassData());
860 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
861 break;
862 }
863 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
865 ID.AddInteger(SST->getMemoryVT().getRawBits());
866 ID.AddInteger(SST->getRawSubclassData());
867 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
868 break;
869 }
870 case ISD::VP_GATHER: {
872 ID.AddInteger(EG->getMemoryVT().getRawBits());
873 ID.AddInteger(EG->getRawSubclassData());
874 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
875 ID.AddInteger(EG->getMemOperand()->getFlags());
876 break;
877 }
878 case ISD::VP_SCATTER: {
880 ID.AddInteger(ES->getMemoryVT().getRawBits());
881 ID.AddInteger(ES->getRawSubclassData());
882 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
883 ID.AddInteger(ES->getMemOperand()->getFlags());
884 break;
885 }
886 case ISD::MLOAD: {
888 ID.AddInteger(MLD->getMemoryVT().getRawBits());
889 ID.AddInteger(MLD->getRawSubclassData());
890 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
891 ID.AddInteger(MLD->getMemOperand()->getFlags());
892 break;
893 }
894 case ISD::MSTORE: {
896 ID.AddInteger(MST->getMemoryVT().getRawBits());
897 ID.AddInteger(MST->getRawSubclassData());
898 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
899 ID.AddInteger(MST->getMemOperand()->getFlags());
900 break;
901 }
902 case ISD::MGATHER: {
904 ID.AddInteger(MG->getMemoryVT().getRawBits());
905 ID.AddInteger(MG->getRawSubclassData());
906 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
907 ID.AddInteger(MG->getMemOperand()->getFlags());
908 break;
909 }
910 case ISD::MSCATTER: {
912 ID.AddInteger(MS->getMemoryVT().getRawBits());
913 ID.AddInteger(MS->getRawSubclassData());
914 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
915 ID.AddInteger(MS->getMemOperand()->getFlags());
916 break;
917 }
918 case ISD::ATOMIC_CMP_SWAP:
919 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
920 case ISD::ATOMIC_SWAP:
921 case ISD::ATOMIC_LOAD_ADD:
922 case ISD::ATOMIC_LOAD_SUB:
923 case ISD::ATOMIC_LOAD_AND:
924 case ISD::ATOMIC_LOAD_CLR:
925 case ISD::ATOMIC_LOAD_OR:
926 case ISD::ATOMIC_LOAD_XOR:
927 case ISD::ATOMIC_LOAD_NAND:
928 case ISD::ATOMIC_LOAD_MIN:
929 case ISD::ATOMIC_LOAD_MAX:
930 case ISD::ATOMIC_LOAD_UMIN:
931 case ISD::ATOMIC_LOAD_UMAX:
932 case ISD::ATOMIC_LOAD:
933 case ISD::ATOMIC_STORE: {
934 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
935 ID.AddInteger(AT->getMemoryVT().getRawBits());
936 ID.AddInteger(AT->getRawSubclassData());
937 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
938 ID.AddInteger(AT->getMemOperand()->getFlags());
939 break;
940 }
941 case ISD::VECTOR_SHUFFLE: {
942 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
943 for (int M : Mask)
944 ID.AddInteger(M);
945 break;
946 }
947 case ISD::ADDRSPACECAST: {
949 ID.AddInteger(ASC->getSrcAddressSpace());
950 ID.AddInteger(ASC->getDestAddressSpace());
951 break;
952 }
954 case ISD::BlockAddress: {
956 ID.AddPointer(BA->getBlockAddress());
957 ID.AddInteger(BA->getOffset());
958 ID.AddInteger(BA->getTargetFlags());
959 break;
960 }
961 case ISD::AssertAlign:
962 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
963 break;
964 case ISD::PREFETCH:
967 // Handled by MemIntrinsicSDNode check after the switch.
968 break;
969 case ISD::MDNODE_SDNODE:
970 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
971 break;
972 } // end switch (N->getOpcode())
973
974 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
975 // to check.
976 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
977 ID.AddInteger(MN->getRawSubclassData());
978 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
979 ID.AddInteger(MN->getMemOperand()->getFlags());
980 ID.AddInteger(MN->getMemoryVT().getRawBits());
981 }
982}
983
984/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
985/// data.
986static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
987 AddNodeIDOpcode(ID, N->getOpcode());
988 // Add the return value info.
989 AddNodeIDValueTypes(ID, N->getVTList());
990 // Add the operand info.
991 AddNodeIDOperands(ID, N->ops());
992
993 // Handle SDNode leafs with special info.
995}
996
997//===----------------------------------------------------------------------===//
998// SelectionDAG Class
999//===----------------------------------------------------------------------===//
1000
1001/// doNotCSE - Return true if CSE should not be performed for this node.
1002static bool doNotCSE(SDNode *N) {
1003 if (N->getValueType(0) == MVT::Glue)
1004 return true; // Never CSE anything that produces a glue result.
1005
1006 switch (N->getOpcode()) {
1007 default: break;
1008 case ISD::HANDLENODE:
1009 case ISD::EH_LABEL:
1010 return true; // Never CSE these nodes.
1011 }
1012
1013 // Check that remaining values produced are not flags.
1014 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1015 if (N->getValueType(i) == MVT::Glue)
1016 return true; // Never CSE anything that produces a glue result.
1017
1018 return false;
1019}
1020
1021/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1022/// SelectionDAG.
1024 // Create a dummy node (which is not added to allnodes), that adds a reference
1025 // to the root node, preventing it from being deleted.
1026 HandleSDNode Dummy(getRoot());
1027
1028 SmallVector<SDNode*, 128> DeadNodes;
1029
1030 // Add all obviously-dead nodes to the DeadNodes worklist.
1031 for (SDNode &Node : allnodes())
1032 if (Node.use_empty())
1033 DeadNodes.push_back(&Node);
1034
1035 RemoveDeadNodes(DeadNodes);
1036
1037 // If the root changed (e.g. it was a dead load, update the root).
1038 setRoot(Dummy.getValue());
1039}
1040
1041/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1042/// given list, and any nodes that become unreachable as a result.
1044
1045 // Process the worklist, deleting the nodes and adding their uses to the
1046 // worklist.
1047 while (!DeadNodes.empty()) {
1048 SDNode *N = DeadNodes.pop_back_val();
1049 // Skip to next node if we've already managed to delete the node. This could
1050 // happen if replacing a node causes a node previously added to the node to
1051 // be deleted.
1052 if (N->getOpcode() == ISD::DELETED_NODE)
1053 continue;
1054
1055 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1056 DUL->NodeDeleted(N, nullptr);
1057
1058 // Take the node out of the appropriate CSE map.
1059 RemoveNodeFromCSEMaps(N);
1060
1061 // Next, brutally remove the operand list. This is safe to do, as there are
1062 // no cycles in the graph.
1063 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1064 SDUse &Use = *I++;
1065 SDNode *Operand = Use.getNode();
1066 Use.set(SDValue());
1067
1068 // Now that we removed this operand, see if there are no uses of it left.
1069 if (Operand->use_empty())
1070 DeadNodes.push_back(Operand);
1071 }
1072
1073 DeallocateNode(N);
1074 }
1075}
1076
1078 SmallVector<SDNode*, 16> DeadNodes(1, N);
1079
1080 // Create a dummy node that adds a reference to the root node, preventing
1081 // it from being deleted. (This matters if the root is an operand of the
1082 // dead node.)
1083 HandleSDNode Dummy(getRoot());
1084
1085 RemoveDeadNodes(DeadNodes);
1086}
1087
1089 // First take this out of the appropriate CSE map.
1090 RemoveNodeFromCSEMaps(N);
1091
1092 // Finally, remove uses due to operands of this node, remove from the
1093 // AllNodes list, and delete the node.
1094 DeleteNodeNotInCSEMaps(N);
1095}
1096
1097void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1098 assert(N->getIterator() != AllNodes.begin() &&
1099 "Cannot delete the entry node!");
1100 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1101
1102 // Drop all of the operands and decrement used node's use counts.
1103 N->DropOperands();
1104
1105 DeallocateNode(N);
1106}
1107
1108void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1109 assert(!(V->isVariadic() && isParameter));
1110 if (isParameter)
1111 ByvalParmDbgValues.push_back(V);
1112 else
1113 DbgValues.push_back(V);
1114 for (const SDNode *Node : V->getSDNodes())
1115 if (Node)
1116 DbgValMap[Node].push_back(V);
1117}
1118
1120 DbgValMapType::iterator I = DbgValMap.find(Node);
1121 if (I == DbgValMap.end())
1122 return;
1123 for (auto &Val: I->second)
1124 Val->setIsInvalidated();
1125 DbgValMap.erase(I);
1126}
1127
1128void SelectionDAG::DeallocateNode(SDNode *N) {
1129 // If we have operands, deallocate them.
1131
1132 NodeAllocator.Deallocate(AllNodes.remove(N));
1133
1134 // Set the opcode to DELETED_NODE to help catch bugs when node
1135 // memory is reallocated.
1136 // FIXME: There are places in SDag that have grown a dependency on the opcode
1137 // value in the released node.
1138 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1139 N->NodeType = ISD::DELETED_NODE;
1140
1141 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1142 // them and forget about that node.
1143 DbgInfo->erase(N);
1144
1145 // Invalidate extra info.
1146 SDEI.erase(N);
1147}
1148
1149#ifndef NDEBUG
1150/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1151void SelectionDAG::verifyNode(SDNode *N) const {
1152 switch (N->getOpcode()) {
1153 default:
1154 if (N->isTargetOpcode())
1156 break;
1157 case ISD::BUILD_PAIR: {
1158 EVT VT = N->getValueType(0);
1159 assert(N->getNumValues() == 1 && "Too many results!");
1160 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1161 "Wrong return type!");
1162 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1163 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1164 "Mismatched operand types!");
1165 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1166 "Wrong operand type!");
1167 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1168 "Wrong return type size");
1169 break;
1170 }
1171 case ISD::BUILD_VECTOR: {
1172 assert(N->getNumValues() == 1 && "Too many results!");
1173 assert(N->getValueType(0).isVector() && "Wrong return type!");
1174 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1175 "Wrong number of operands!");
1176 EVT EltVT = N->getValueType(0).getVectorElementType();
1177 for (const SDUse &Op : N->ops()) {
1178 assert((Op.getValueType() == EltVT ||
1179 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1180 EltVT.bitsLE(Op.getValueType()))) &&
1181 "Wrong operand type!");
1182 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1183 "Operands must all have the same type");
1184 }
1185 break;
1186 }
1187 }
1188}
1189#endif // NDEBUG
1190
1191/// Insert a newly allocated node into the DAG.
1192///
1193/// Handles insertion into the all nodes list and CSE map, as well as
1194/// verification and other common operations when a new node is allocated.
1195void SelectionDAG::InsertNode(SDNode *N) {
1196 AllNodes.push_back(N);
1197#ifndef NDEBUG
1198 N->PersistentId = NextPersistentId++;
1199 verifyNode(N);
1200#endif
1201 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1202 DUL->NodeInserted(N);
1203}
1204
1205/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1206/// correspond to it. This is useful when we're about to delete or repurpose
1207/// the node. We don't want future request for structurally identical nodes
1208/// to return N anymore.
1209bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1210 bool Erased = false;
1211 switch (N->getOpcode()) {
1212 case ISD::HANDLENODE: return false; // noop.
1213 case ISD::CONDCODE:
1214 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1215 "Cond code doesn't exist!");
1216 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1217 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1218 break;
1220 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1221 break;
1223 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1224 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1225 ESN->getSymbol(), ESN->getTargetFlags()));
1226 break;
1227 }
1228 case ISD::MCSymbol: {
1229 auto *MCSN = cast<MCSymbolSDNode>(N);
1230 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1231 break;
1232 }
1233 case ISD::VALUETYPE: {
1234 EVT VT = cast<VTSDNode>(N)->getVT();
1235 if (VT.isExtended()) {
1236 Erased = ExtendedValueTypeNodes.erase(VT);
1237 } else {
1238 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1239 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1240 }
1241 break;
1242 }
1243 default:
1244 // Remove it from the CSE Map.
1245 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1246 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1247 Erased = CSEMap.RemoveNode(N);
1248 break;
1249 }
1250#ifndef NDEBUG
1251 // Verify that the node was actually in one of the CSE maps, unless it has a
1252 // glue result (which cannot be CSE'd) or is one of the special cases that are
1253 // not subject to CSE.
1254 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1255 !N->isMachineOpcode() && !doNotCSE(N)) {
1256 N->dump(this);
1257 dbgs() << "\n";
1258 llvm_unreachable("Node is not in map!");
1259 }
1260#endif
1261 return Erased;
1262}
1263
1264/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1265/// maps and modified in place. Add it back to the CSE maps, unless an identical
1266/// node already exists, in which case transfer all its users to the existing
1267/// node. This transfer can potentially trigger recursive merging.
1268void
1269SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1270 // For node types that aren't CSE'd, just act as if no identical node
1271 // already exists.
1272 if (!doNotCSE(N)) {
1273 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1274 if (Existing != N) {
1275 // If there was already an existing matching node, use ReplaceAllUsesWith
1276 // to replace the dead one with the existing one. This can cause
1277 // recursive merging of other unrelated nodes down the line.
1278 Existing->intersectFlagsWith(N->getFlags());
1279 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1280 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1281 ReplaceAllUsesWith(N, Existing);
1282
1283 // N is now dead. Inform the listeners and delete it.
1284 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1285 DUL->NodeDeleted(N, Existing);
1286 DeleteNodeNotInCSEMaps(N);
1287 return;
1288 }
1289 }
1290
1291 // If the node doesn't already exist, we updated it. Inform listeners.
1292 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1293 DUL->NodeUpdated(N);
1294}
1295
1296/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1297/// were replaced with those specified. If this node is never memoized,
1298/// return null, otherwise return a pointer to the slot it would take. If a
1299/// node already exists with these operands, the slot will be non-null.
1300SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1301 void *&InsertPos) {
1302 if (doNotCSE(N))
1303 return nullptr;
1304
1305 SDValue Ops[] = { Op };
1306 FoldingSetNodeID ID;
1307 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1309 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1310 if (Node)
1311 Node->intersectFlagsWith(N->getFlags());
1312 return Node;
1313}
1314
1315/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1316/// were replaced with those specified. If this node is never memoized,
1317/// return null, otherwise return a pointer to the slot it would take. If a
1318/// node already exists with these operands, the slot will be non-null.
1319SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1320 SDValue Op1, SDValue Op2,
1321 void *&InsertPos) {
1322 if (doNotCSE(N))
1323 return nullptr;
1324
1325 SDValue Ops[] = { Op1, Op2 };
1326 FoldingSetNodeID ID;
1327 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1329 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1330 if (Node)
1331 Node->intersectFlagsWith(N->getFlags());
1332 return Node;
1333}
1334
1335/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1336/// were replaced with those specified. If this node is never memoized,
1337/// return null, otherwise return a pointer to the slot it would take. If a
1338/// node already exists with these operands, the slot will be non-null.
1339SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1340 void *&InsertPos) {
1341 if (doNotCSE(N))
1342 return nullptr;
1343
1344 FoldingSetNodeID ID;
1345 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1347 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1348 if (Node)
1349 Node->intersectFlagsWith(N->getFlags());
1350 return Node;
1351}
1352
1354 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1355 : VT.getTypeForEVT(*getContext());
1356
1357 return getDataLayout().getABITypeAlign(Ty);
1358}
1359
1360// EntryNode could meaningfully have debug info if we can find it...
1362 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1363 getVTList(MVT::Other, MVT::Glue)),
1364 Root(getEntryNode()) {
1365 InsertNode(&EntryNode);
1366 DbgInfo = new SDDbgInfo();
1367}
1368
1370 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1371 const TargetLibraryInfo *LibraryInfo,
1372 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1374 FunctionVarLocs const *VarLocs) {
1375 MF = &NewMF;
1376 SDAGISelPass = PassPtr;
1377 ORE = &NewORE;
1380 LibInfo = LibraryInfo;
1381 Context = &MF->getFunction().getContext();
1382 UA = NewUA;
1383 PSI = PSIin;
1384 BFI = BFIin;
1385 MMI = &MMIin;
1386 FnVarLocs = VarLocs;
1387}
1388
1390 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1391 allnodes_clear();
1392 OperandRecycler.clear(OperandAllocator);
1393 delete DbgInfo;
1394}
1395
1397 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1398}
1399
1400void SelectionDAG::allnodes_clear() {
1401 assert(&*AllNodes.begin() == &EntryNode);
1402 AllNodes.remove(AllNodes.begin());
1403 while (!AllNodes.empty())
1404 DeallocateNode(&AllNodes.front());
1405#ifndef NDEBUG
1406 NextPersistentId = 0;
1407#endif
1408}
1409
1410SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1411 void *&InsertPos) {
1412 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1413 if (N) {
1414 switch (N->getOpcode()) {
1415 default: break;
1416 case ISD::Constant:
1417 case ISD::ConstantFP:
1418 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1419 "debug location. Use another overload.");
1420 }
1421 }
1422 return N;
1423}
1424
1425SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1426 const SDLoc &DL, void *&InsertPos) {
1427 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1428 if (N) {
1429 switch (N->getOpcode()) {
1430 case ISD::Constant:
1431 case ISD::ConstantFP:
1432 // Erase debug location from the node if the node is used at several
1433 // different places. Do not propagate one location to all uses as it
1434 // will cause a worse single stepping debugging experience.
1435 if (N->getDebugLoc() != DL.getDebugLoc())
1436 N->setDebugLoc(DebugLoc());
1437 break;
1438 default:
1439 // When the node's point of use is located earlier in the instruction
1440 // sequence than its prior point of use, update its debug info to the
1441 // earlier location.
1442 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1443 N->setDebugLoc(DL.getDebugLoc());
1444 break;
1445 }
1446 }
1447 return N;
1448}
1449
1451 allnodes_clear();
1452 OperandRecycler.clear(OperandAllocator);
1453 OperandAllocator.Reset();
1454 CSEMap.clear();
1455
1456 ExtendedValueTypeNodes.clear();
1457 ExternalSymbols.clear();
1458 TargetExternalSymbols.clear();
1459 MCSymbols.clear();
1460 SDEI.clear();
1461 llvm::fill(CondCodeNodes, nullptr);
1462 llvm::fill(ValueTypeNodes, nullptr);
1463
1464 EntryNode.UseList = nullptr;
1465 InsertNode(&EntryNode);
1466 Root = getEntryNode();
1467 DbgInfo->clear();
1468}
1469
1471 return VT.bitsGT(Op.getValueType())
1472 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1473 : getNode(ISD::FP_ROUND, DL, VT, Op,
1474 getIntPtrConstant(0, DL, /*isTarget=*/true));
1475}
1476
1477std::pair<SDValue, SDValue>
1479 const SDLoc &DL, EVT VT) {
1480 assert(!VT.bitsEq(Op.getValueType()) &&
1481 "Strict no-op FP extend/round not allowed.");
1482 SDValue Res =
1483 VT.bitsGT(Op.getValueType())
1484 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1485 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1486 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1487
1488 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1489}
1490
1492 return VT.bitsGT(Op.getValueType()) ?
1493 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1494 getNode(ISD::TRUNCATE, DL, VT, Op);
1495}
1496
1498 return VT.bitsGT(Op.getValueType()) ?
1499 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1500 getNode(ISD::TRUNCATE, DL, VT, Op);
1501}
1502
1504 return VT.bitsGT(Op.getValueType()) ?
1505 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1506 getNode(ISD::TRUNCATE, DL, VT, Op);
1507}
1508
1510 EVT VT) {
1511 assert(!VT.isVector());
1512 auto Type = Op.getValueType();
1513 SDValue DestOp;
1514 if (Type == VT)
1515 return Op;
1516 auto Size = Op.getValueSizeInBits();
1517 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1518 if (DestOp.getValueType() == VT)
1519 return DestOp;
1520
1521 return getAnyExtOrTrunc(DestOp, DL, VT);
1522}
1523
1525 EVT VT) {
1526 assert(!VT.isVector());
1527 auto Type = Op.getValueType();
1528 SDValue DestOp;
1529 if (Type == VT)
1530 return Op;
1531 auto Size = Op.getValueSizeInBits();
1532 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1533 if (DestOp.getValueType() == VT)
1534 return DestOp;
1535
1536 return getSExtOrTrunc(DestOp, DL, VT);
1537}
1538
1540 EVT VT) {
1541 assert(!VT.isVector());
1542 auto Type = Op.getValueType();
1543 SDValue DestOp;
1544 if (Type == VT)
1545 return Op;
1546 auto Size = Op.getValueSizeInBits();
1547 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1548 if (DestOp.getValueType() == VT)
1549 return DestOp;
1550
1551 return getZExtOrTrunc(DestOp, DL, VT);
1552}
1553
1555 EVT OpVT) {
1556 if (VT.bitsLE(Op.getValueType()))
1557 return getNode(ISD::TRUNCATE, SL, VT, Op);
1558
1559 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1560 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1561}
1562
1564 EVT OpVT = Op.getValueType();
1565 assert(VT.isInteger() && OpVT.isInteger() &&
1566 "Cannot getZeroExtendInReg FP types");
1567 assert(VT.isVector() == OpVT.isVector() &&
1568 "getZeroExtendInReg type should be vector iff the operand "
1569 "type is vector!");
1570 assert((!VT.isVector() ||
1572 "Vector element counts must match in getZeroExtendInReg");
1573 assert(VT.bitsLE(OpVT) && "Not extending!");
1574 if (OpVT == VT)
1575 return Op;
1577 VT.getScalarSizeInBits());
1578 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1579}
1580
1582 SDValue EVL, const SDLoc &DL,
1583 EVT VT) {
1584 EVT OpVT = Op.getValueType();
1585 assert(VT.isInteger() && OpVT.isInteger() &&
1586 "Cannot getVPZeroExtendInReg FP types");
1587 assert(VT.isVector() && OpVT.isVector() &&
1588 "getVPZeroExtendInReg type and operand type should be vector!");
1590 "Vector element counts must match in getZeroExtendInReg");
1591 assert(VT.bitsLE(OpVT) && "Not extending!");
1592 if (OpVT == VT)
1593 return Op;
1595 VT.getScalarSizeInBits());
1596 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1597 EVL);
1598}
1599
1601 // Only unsigned pointer semantics are supported right now. In the future this
1602 // might delegate to TLI to check pointer signedness.
1603 return getZExtOrTrunc(Op, DL, VT);
1604}
1605
1607 // Only unsigned pointer semantics are supported right now. In the future this
1608 // might delegate to TLI to check pointer signedness.
1609 return getZeroExtendInReg(Op, DL, VT);
1610}
1611
1613 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1614}
1615
1616/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1618 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1619}
1620
1622 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1623 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1624}
1625
1627 SDValue Mask, SDValue EVL, EVT VT) {
1628 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1629 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1630}
1631
1633 SDValue Mask, SDValue EVL) {
1634 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1635}
1636
1638 SDValue Mask, SDValue EVL) {
1639 if (VT.bitsGT(Op.getValueType()))
1640 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1641 if (VT.bitsLT(Op.getValueType()))
1642 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1643 return Op;
1644}
1645
1647 EVT OpVT) {
1648 if (!V)
1649 return getConstant(0, DL, VT);
1650
1651 switch (TLI->getBooleanContents(OpVT)) {
1654 return getConstant(1, DL, VT);
1656 return getAllOnesConstant(DL, VT);
1657 }
1658 llvm_unreachable("Unexpected boolean content enum!");
1659}
1660
1662 bool isT, bool isO) {
1663 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1664 DL, VT, isT, isO);
1665}
1666
1668 bool isT, bool isO) {
1669 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1670}
1671
1673 EVT VT, bool isT, bool isO) {
1674 assert(VT.isInteger() && "Cannot create FP integer constant!");
1675
1676 EVT EltVT = VT.getScalarType();
1677 const ConstantInt *Elt = &Val;
1678
1679 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1680 // to-be-splatted scalar ConstantInt.
1681 if (isa<VectorType>(Elt->getType()))
1682 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1683
1684 // In some cases the vector type is legal but the element type is illegal and
1685 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1686 // inserted value (the type does not need to match the vector element type).
1687 // Any extra bits introduced will be truncated away.
1688 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1690 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1691 APInt NewVal;
1692 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1693 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1694 else
1695 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1696 Elt = ConstantInt::get(*getContext(), NewVal);
1697 }
1698 // In other cases the element type is illegal and needs to be expanded, for
1699 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1700 // the value into n parts and use a vector type with n-times the elements.
1701 // Then bitcast to the type requested.
1702 // Legalizing constants too early makes the DAGCombiner's job harder so we
1703 // only legalize if the DAG tells us we must produce legal types.
1704 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1705 TLI->getTypeAction(*getContext(), EltVT) ==
1707 const APInt &NewVal = Elt->getValue();
1708 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1709 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1710
1711 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1712 if (VT.isScalableVector() ||
1713 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1714 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1715 "Can only handle an even split!");
1716 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1717
1718 SmallVector<SDValue, 2> ScalarParts;
1719 for (unsigned i = 0; i != Parts; ++i)
1720 ScalarParts.push_back(getConstant(
1721 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1722 ViaEltVT, isT, isO));
1723
1724 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1725 }
1726
1727 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1728 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1729
1730 // Check the temporary vector is the correct size. If this fails then
1731 // getTypeToTransformTo() probably returned a type whose size (in bits)
1732 // isn't a power-of-2 factor of the requested type size.
1733 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1734
1735 SmallVector<SDValue, 2> EltParts;
1736 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1737 EltParts.push_back(getConstant(
1738 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1739 ViaEltVT, isT, isO));
1740
1741 // EltParts is currently in little endian order. If we actually want
1742 // big-endian order then reverse it now.
1743 if (getDataLayout().isBigEndian())
1744 std::reverse(EltParts.begin(), EltParts.end());
1745
1746 // The elements must be reversed when the element order is different
1747 // to the endianness of the elements (because the BITCAST is itself a
1748 // vector shuffle in this situation). However, we do not need any code to
1749 // perform this reversal because getConstant() is producing a vector
1750 // splat.
1751 // This situation occurs in MIPS MSA.
1752
1754 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1755 llvm::append_range(Ops, EltParts);
1756
1757 SDValue V =
1758 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1759 return V;
1760 }
1761
1762 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1763 "APInt size does not match type size!");
1764 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1765 SDVTList VTs = getVTList(EltVT);
1767 AddNodeIDNode(ID, Opc, VTs, {});
1768 ID.AddPointer(Elt);
1769 ID.AddBoolean(isO);
1770 void *IP = nullptr;
1771 SDNode *N = nullptr;
1772 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1773 if (!VT.isVector())
1774 return SDValue(N, 0);
1775
1776 if (!N) {
1777 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1778 CSEMap.InsertNode(N, IP);
1779 InsertNode(N);
1780 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1781 }
1782
1783 SDValue Result(N, 0);
1784 if (VT.isVector())
1785 Result = getSplat(VT, DL, Result);
1786 return Result;
1787}
1788
1790 bool isT, bool isO) {
1791 unsigned Size = VT.getScalarSizeInBits();
1792 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1793}
1794
1796 bool IsOpaque) {
1798 IsTarget, IsOpaque);
1799}
1800
1802 bool isTarget) {
1803 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1804}
1805
1807 const SDLoc &DL) {
1808 assert(VT.isInteger() && "Shift amount is not an integer type!");
1809 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1810 return getConstant(Val, DL, ShiftVT);
1811}
1812
1814 const SDLoc &DL) {
1815 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1816 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1817}
1818
1820 bool isTarget) {
1821 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1822}
1823
1825 bool isTarget) {
1826 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1827}
1828
1830 EVT VT, bool isTarget) {
1831 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1832
1833 EVT EltVT = VT.getScalarType();
1834 const ConstantFP *Elt = &V;
1835
1836 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1837 // the to-be-splatted scalar ConstantFP.
1838 if (isa<VectorType>(Elt->getType()))
1839 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1840
1841 // Do the map lookup using the actual bit pattern for the floating point
1842 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1843 // we don't have issues with SNANs.
1844 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1845 SDVTList VTs = getVTList(EltVT);
1847 AddNodeIDNode(ID, Opc, VTs, {});
1848 ID.AddPointer(Elt);
1849 void *IP = nullptr;
1850 SDNode *N = nullptr;
1851 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1852 if (!VT.isVector())
1853 return SDValue(N, 0);
1854
1855 if (!N) {
1856 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1857 CSEMap.InsertNode(N, IP);
1858 InsertNode(N);
1859 }
1860
1861 SDValue Result(N, 0);
1862 if (VT.isVector())
1863 Result = getSplat(VT, DL, Result);
1864 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1865 return Result;
1866}
1867
1869 bool isTarget) {
1870 EVT EltVT = VT.getScalarType();
1871 if (EltVT == MVT::f32)
1872 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1873 if (EltVT == MVT::f64)
1874 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1875 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1876 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1877 bool Ignored;
1878 APFloat APF = APFloat(Val);
1880 &Ignored);
1881 return getConstantFP(APF, DL, VT, isTarget);
1882 }
1883 llvm_unreachable("Unsupported type in getConstantFP");
1884}
1885
1887 EVT VT, int64_t Offset, bool isTargetGA,
1888 unsigned TargetFlags) {
1889 assert((TargetFlags == 0 || isTargetGA) &&
1890 "Cannot set target flags on target-independent globals");
1891
1892 // Truncate (with sign-extension) the offset value to the pointer size.
1894 if (BitWidth < 64)
1896
1897 unsigned Opc;
1898 if (GV->isThreadLocal())
1900 else
1902
1903 SDVTList VTs = getVTList(VT);
1905 AddNodeIDNode(ID, Opc, VTs, {});
1906 ID.AddPointer(GV);
1907 ID.AddInteger(Offset);
1908 ID.AddInteger(TargetFlags);
1909 void *IP = nullptr;
1910 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1911 return SDValue(E, 0);
1912
1913 auto *N = newSDNode<GlobalAddressSDNode>(
1914 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1915 CSEMap.InsertNode(N, IP);
1916 InsertNode(N);
1917 return SDValue(N, 0);
1918}
1919
1920SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1921 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1922 SDVTList VTs = getVTList(VT);
1924 AddNodeIDNode(ID, Opc, VTs, {});
1925 ID.AddInteger(FI);
1926 void *IP = nullptr;
1927 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1928 return SDValue(E, 0);
1929
1930 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1931 CSEMap.InsertNode(N, IP);
1932 InsertNode(N);
1933 return SDValue(N, 0);
1934}
1935
1936SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1937 unsigned TargetFlags) {
1938 assert((TargetFlags == 0 || isTarget) &&
1939 "Cannot set target flags on target-independent jump tables");
1940 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1941 SDVTList VTs = getVTList(VT);
1943 AddNodeIDNode(ID, Opc, VTs, {});
1944 ID.AddInteger(JTI);
1945 ID.AddInteger(TargetFlags);
1946 void *IP = nullptr;
1947 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1948 return SDValue(E, 0);
1949
1950 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1951 CSEMap.InsertNode(N, IP);
1952 InsertNode(N);
1953 return SDValue(N, 0);
1954}
1955
1957 const SDLoc &DL) {
1959 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1960 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1961}
1962
1964 MaybeAlign Alignment, int Offset,
1965 bool isTarget, unsigned TargetFlags) {
1966 assert((TargetFlags == 0 || isTarget) &&
1967 "Cannot set target flags on target-independent globals");
1968 if (!Alignment)
1969 Alignment = shouldOptForSize()
1970 ? getDataLayout().getABITypeAlign(C->getType())
1971 : getDataLayout().getPrefTypeAlign(C->getType());
1972 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1973 SDVTList VTs = getVTList(VT);
1975 AddNodeIDNode(ID, Opc, VTs, {});
1976 ID.AddInteger(Alignment->value());
1977 ID.AddInteger(Offset);
1978 ID.AddPointer(C);
1979 ID.AddInteger(TargetFlags);
1980 void *IP = nullptr;
1981 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1982 return SDValue(E, 0);
1983
1984 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1985 TargetFlags);
1986 CSEMap.InsertNode(N, IP);
1987 InsertNode(N);
1988 SDValue V = SDValue(N, 0);
1989 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1990 return V;
1991}
1992
1994 MaybeAlign Alignment, int Offset,
1995 bool isTarget, unsigned TargetFlags) {
1996 assert((TargetFlags == 0 || isTarget) &&
1997 "Cannot set target flags on target-independent globals");
1998 if (!Alignment)
1999 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2000 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2001 SDVTList VTs = getVTList(VT);
2003 AddNodeIDNode(ID, Opc, VTs, {});
2004 ID.AddInteger(Alignment->value());
2005 ID.AddInteger(Offset);
2006 C->addSelectionDAGCSEId(ID);
2007 ID.AddInteger(TargetFlags);
2008 void *IP = nullptr;
2009 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2010 return SDValue(E, 0);
2011
2012 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2013 TargetFlags);
2014 CSEMap.InsertNode(N, IP);
2015 InsertNode(N);
2016 return SDValue(N, 0);
2017}
2018
2021 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2022 ID.AddPointer(MBB);
2023 void *IP = nullptr;
2024 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2025 return SDValue(E, 0);
2026
2027 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2028 CSEMap.InsertNode(N, IP);
2029 InsertNode(N);
2030 return SDValue(N, 0);
2031}
2032
2034 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2035 ValueTypeNodes.size())
2036 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2037
2038 SDNode *&N = VT.isExtended() ?
2039 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2040
2041 if (N) return SDValue(N, 0);
2042 N = newSDNode<VTSDNode>(VT);
2043 InsertNode(N);
2044 return SDValue(N, 0);
2045}
2046
2048 SDNode *&N = ExternalSymbols[Sym];
2049 if (N) return SDValue(N, 0);
2050 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2051 InsertNode(N);
2052 return SDValue(N, 0);
2053}
2054
2056 SDNode *&N = MCSymbols[Sym];
2057 if (N)
2058 return SDValue(N, 0);
2059 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2060 InsertNode(N);
2061 return SDValue(N, 0);
2062}
2063
2065 unsigned TargetFlags) {
2066 SDNode *&N =
2067 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2068 if (N) return SDValue(N, 0);
2069 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2070 InsertNode(N);
2071 return SDValue(N, 0);
2072}
2073
2075 if ((unsigned)Cond >= CondCodeNodes.size())
2076 CondCodeNodes.resize(Cond+1);
2077
2078 if (!CondCodeNodes[Cond]) {
2079 auto *N = newSDNode<CondCodeSDNode>(Cond);
2080 CondCodeNodes[Cond] = N;
2081 InsertNode(N);
2082 }
2083
2084 return SDValue(CondCodeNodes[Cond], 0);
2085}
2086
2088 bool ConstantFold) {
2089 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2090 "APInt size does not match type size!");
2091
2092 if (MulImm == 0)
2093 return getConstant(0, DL, VT);
2094
2095 if (ConstantFold) {
2096 const MachineFunction &MF = getMachineFunction();
2097 const Function &F = MF.getFunction();
2098 ConstantRange CR = getVScaleRange(&F, 64);
2099 if (const APInt *C = CR.getSingleElement())
2100 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2101 }
2102
2103 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2104}
2105
2107 bool ConstantFold) {
2108 if (EC.isScalable())
2109 return getVScale(DL, VT,
2110 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2111
2112 return getConstant(EC.getKnownMinValue(), DL, VT);
2113}
2114
2116 APInt One(ResVT.getScalarSizeInBits(), 1);
2117 return getStepVector(DL, ResVT, One);
2118}
2119
2121 const APInt &StepVal) {
2122 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2123 if (ResVT.isScalableVector())
2124 return getNode(
2125 ISD::STEP_VECTOR, DL, ResVT,
2126 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2127
2128 SmallVector<SDValue, 16> OpsStepConstants;
2129 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2130 OpsStepConstants.push_back(
2131 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2132 return getBuildVector(ResVT, DL, OpsStepConstants);
2133}
2134
2135/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2136/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2141
2143 SDValue N2, ArrayRef<int> Mask) {
2144 assert(VT.getVectorNumElements() == Mask.size() &&
2145 "Must have the same number of vector elements as mask elements!");
2146 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2147 "Invalid VECTOR_SHUFFLE");
2148
2149 // Canonicalize shuffle undef, undef -> undef
2150 if (N1.isUndef() && N2.isUndef())
2151 return getUNDEF(VT);
2152
2153 // Validate that all indices in Mask are within the range of the elements
2154 // input to the shuffle.
2155 int NElts = Mask.size();
2156 assert(llvm::all_of(Mask,
2157 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2158 "Index out of range");
2159
2160 // Copy the mask so we can do any needed cleanup.
2161 SmallVector<int, 8> MaskVec(Mask);
2162
2163 // Canonicalize shuffle v, v -> v, undef
2164 if (N1 == N2) {
2165 N2 = getUNDEF(VT);
2166 for (int i = 0; i != NElts; ++i)
2167 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2168 }
2169
2170 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2171 if (N1.isUndef())
2172 commuteShuffle(N1, N2, MaskVec);
2173
2174 if (TLI->hasVectorBlend()) {
2175 // If shuffling a splat, try to blend the splat instead. We do this here so
2176 // that even when this arises during lowering we don't have to re-handle it.
2177 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2178 BitVector UndefElements;
2179 SDValue Splat = BV->getSplatValue(&UndefElements);
2180 if (!Splat)
2181 return;
2182
2183 for (int i = 0; i < NElts; ++i) {
2184 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2185 continue;
2186
2187 // If this input comes from undef, mark it as such.
2188 if (UndefElements[MaskVec[i] - Offset]) {
2189 MaskVec[i] = -1;
2190 continue;
2191 }
2192
2193 // If we can blend a non-undef lane, use that instead.
2194 if (!UndefElements[i])
2195 MaskVec[i] = i + Offset;
2196 }
2197 };
2198 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2199 BlendSplat(N1BV, 0);
2200 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2201 BlendSplat(N2BV, NElts);
2202 }
2203
2204 // Canonicalize all index into lhs, -> shuffle lhs, undef
2205 // Canonicalize all index into rhs, -> shuffle rhs, undef
2206 bool AllLHS = true, AllRHS = true;
2207 bool N2Undef = N2.isUndef();
2208 for (int i = 0; i != NElts; ++i) {
2209 if (MaskVec[i] >= NElts) {
2210 if (N2Undef)
2211 MaskVec[i] = -1;
2212 else
2213 AllLHS = false;
2214 } else if (MaskVec[i] >= 0) {
2215 AllRHS = false;
2216 }
2217 }
2218 if (AllLHS && AllRHS)
2219 return getUNDEF(VT);
2220 if (AllLHS && !N2Undef)
2221 N2 = getUNDEF(VT);
2222 if (AllRHS) {
2223 N1 = getUNDEF(VT);
2224 commuteShuffle(N1, N2, MaskVec);
2225 }
2226 // Reset our undef status after accounting for the mask.
2227 N2Undef = N2.isUndef();
2228 // Re-check whether both sides ended up undef.
2229 if (N1.isUndef() && N2Undef)
2230 return getUNDEF(VT);
2231
2232 // If Identity shuffle return that node.
2233 bool Identity = true, AllSame = true;
2234 for (int i = 0; i != NElts; ++i) {
2235 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2236 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2237 }
2238 if (Identity && NElts)
2239 return N1;
2240
2241 // Shuffling a constant splat doesn't change the result.
2242 if (N2Undef) {
2243 SDValue V = N1;
2244
2245 // Look through any bitcasts. We check that these don't change the number
2246 // (and size) of elements and just changes their types.
2247 while (V.getOpcode() == ISD::BITCAST)
2248 V = V->getOperand(0);
2249
2250 // A splat should always show up as a build vector node.
2251 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2252 BitVector UndefElements;
2253 SDValue Splat = BV->getSplatValue(&UndefElements);
2254 // If this is a splat of an undef, shuffling it is also undef.
2255 if (Splat && Splat.isUndef())
2256 return getUNDEF(VT);
2257
2258 bool SameNumElts =
2259 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2260
2261 // We only have a splat which can skip shuffles if there is a splatted
2262 // value and no undef lanes rearranged by the shuffle.
2263 if (Splat && UndefElements.none()) {
2264 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2265 // number of elements match or the value splatted is a zero constant.
2266 if (SameNumElts || isNullConstant(Splat))
2267 return N1;
2268 }
2269
2270 // If the shuffle itself creates a splat, build the vector directly.
2271 if (AllSame && SameNumElts) {
2272 EVT BuildVT = BV->getValueType(0);
2273 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2274 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2275
2276 // We may have jumped through bitcasts, so the type of the
2277 // BUILD_VECTOR may not match the type of the shuffle.
2278 if (BuildVT != VT)
2279 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2280 return NewBV;
2281 }
2282 }
2283 }
2284
2285 SDVTList VTs = getVTList(VT);
2287 SDValue Ops[2] = { N1, N2 };
2289 for (int i = 0; i != NElts; ++i)
2290 ID.AddInteger(MaskVec[i]);
2291
2292 void* IP = nullptr;
2293 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2294 return SDValue(E, 0);
2295
2296 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2297 // SDNode doesn't have access to it. This memory will be "leaked" when
2298 // the node is deallocated, but recovered when the NodeAllocator is released.
2299 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2300 llvm::copy(MaskVec, MaskAlloc);
2301
2302 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2303 dl.getDebugLoc(), MaskAlloc);
2304 createOperands(N, Ops);
2305
2306 CSEMap.InsertNode(N, IP);
2307 InsertNode(N);
2308 SDValue V = SDValue(N, 0);
2309 NewSDValueDbgMsg(V, "Creating new node: ", this);
2310 return V;
2311}
2312
2314 EVT VT = SV.getValueType(0);
2315 SmallVector<int, 8> MaskVec(SV.getMask());
2317
2318 SDValue Op0 = SV.getOperand(0);
2319 SDValue Op1 = SV.getOperand(1);
2320 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2321}
2322
2324 SDVTList VTs = getVTList(VT);
2326 AddNodeIDNode(ID, ISD::Register, VTs, {});
2327 ID.AddInteger(Reg.id());
2328 void *IP = nullptr;
2329 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2330 return SDValue(E, 0);
2331
2332 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2333 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2334 CSEMap.InsertNode(N, IP);
2335 InsertNode(N);
2336 return SDValue(N, 0);
2337}
2338
2341 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2342 ID.AddPointer(RegMask);
2343 void *IP = nullptr;
2344 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2345 return SDValue(E, 0);
2346
2347 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2348 CSEMap.InsertNode(N, IP);
2349 InsertNode(N);
2350 return SDValue(N, 0);
2351}
2352
2354 MCSymbol *Label) {
2355 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2356}
2357
2358SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2359 SDValue Root, MCSymbol *Label) {
2361 SDValue Ops[] = { Root };
2362 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2363 ID.AddPointer(Label);
2364 void *IP = nullptr;
2365 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2366 return SDValue(E, 0);
2367
2368 auto *N =
2369 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2370 createOperands(N, Ops);
2371
2372 CSEMap.InsertNode(N, IP);
2373 InsertNode(N);
2374 return SDValue(N, 0);
2375}
2376
2378 int64_t Offset, bool isTarget,
2379 unsigned TargetFlags) {
2380 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2381 SDVTList VTs = getVTList(VT);
2382
2384 AddNodeIDNode(ID, Opc, VTs, {});
2385 ID.AddPointer(BA);
2386 ID.AddInteger(Offset);
2387 ID.AddInteger(TargetFlags);
2388 void *IP = nullptr;
2389 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2390 return SDValue(E, 0);
2391
2392 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2393 CSEMap.InsertNode(N, IP);
2394 InsertNode(N);
2395 return SDValue(N, 0);
2396}
2397
2400 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2401 ID.AddPointer(V);
2402
2403 void *IP = nullptr;
2404 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2405 return SDValue(E, 0);
2406
2407 auto *N = newSDNode<SrcValueSDNode>(V);
2408 CSEMap.InsertNode(N, IP);
2409 InsertNode(N);
2410 return SDValue(N, 0);
2411}
2412
2415 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2416 ID.AddPointer(MD);
2417
2418 void *IP = nullptr;
2419 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2420 return SDValue(E, 0);
2421
2422 auto *N = newSDNode<MDNodeSDNode>(MD);
2423 CSEMap.InsertNode(N, IP);
2424 InsertNode(N);
2425 return SDValue(N, 0);
2426}
2427
2429 if (VT == V.getValueType())
2430 return V;
2431
2432 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2433}
2434
2436 unsigned SrcAS, unsigned DestAS) {
2437 SDVTList VTs = getVTList(VT);
2438 SDValue Ops[] = {Ptr};
2440 AddNodeIDNode(ID, ISD::ADDRSPACECAST, VTs, Ops);
2441 ID.AddInteger(SrcAS);
2442 ID.AddInteger(DestAS);
2443
2444 void *IP = nullptr;
2445 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2446 return SDValue(E, 0);
2447
2448 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2449 VTs, SrcAS, DestAS);
2450 createOperands(N, Ops);
2451
2452 CSEMap.InsertNode(N, IP);
2453 InsertNode(N);
2454 return SDValue(N, 0);
2455}
2456
2458 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2459}
2460
2461/// getShiftAmountOperand - Return the specified value casted to
2462/// the target's desired shift amount type.
2464 EVT OpTy = Op.getValueType();
2465 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2466 if (OpTy == ShTy || OpTy.isVector()) return Op;
2467
2468 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2469}
2470
2471/// Given a store node \p StoreNode, return true if it is safe to fold that node
2472/// into \p FPNode, which expands to a library call with output pointers.
2474 SDNode *FPNode) {
2476 SmallVector<const SDNode *, 8> DeferredNodes;
2478
2479 // Skip FPNode use by StoreNode (that's the use we want to fold into FPNode).
2480 for (SDValue Op : StoreNode->ops())
2481 if (Op.getNode() != FPNode)
2482 Worklist.push_back(Op.getNode());
2483
2485 while (!Worklist.empty()) {
2486 const SDNode *Node = Worklist.pop_back_val();
2487 auto [_, Inserted] = Visited.insert(Node);
2488 if (!Inserted)
2489 continue;
2490
2491 if (MaxSteps > 0 && Visited.size() >= MaxSteps)
2492 return false;
2493
2494 // Reached the FPNode (would result in a cycle).
2495 // OR Reached CALLSEQ_START (would result in nested call sequences).
2496 if (Node == FPNode || Node->getOpcode() == ISD::CALLSEQ_START)
2497 return false;
2498
2499 if (Node->getOpcode() == ISD::CALLSEQ_END) {
2500 // Defer looking into call sequences (so we can check we're outside one).
2501 // We still need to look through these for the predecessor check.
2502 DeferredNodes.push_back(Node);
2503 continue;
2504 }
2505
2506 for (SDValue Op : Node->ops())
2507 Worklist.push_back(Op.getNode());
2508 }
2509
2510 // True if we're outside a call sequence and don't have the FPNode as a
2511 // predecessor. No cycles or nested call sequences possible.
2512 return !SDNode::hasPredecessorHelper(FPNode, Visited, DeferredNodes,
2513 MaxSteps);
2514}
2515
2517 RTLIB::Libcall LC, SDNode *Node, SmallVectorImpl<SDValue> &Results,
2518 std::optional<unsigned> CallRetResNo) {
2519 LLVMContext &Ctx = *getContext();
2520 EVT VT = Node->getValueType(0);
2521 unsigned NumResults = Node->getNumValues();
2522
2523 if (LC == RTLIB::UNKNOWN_LIBCALL)
2524 return false;
2525
2526 const char *LCName = TLI->getLibcallName(LC);
2527 if (!LCName)
2528 return false;
2529
2530 auto getVecDesc = [&]() -> VecDesc const * {
2531 for (bool Masked : {false, true}) {
2532 if (VecDesc const *VD = getLibInfo().getVectorMappingInfo(
2533 LCName, VT.getVectorElementCount(), Masked)) {
2534 return VD;
2535 }
2536 }
2537 return nullptr;
2538 };
2539
2540 // For vector types, we must find a vector mapping for the libcall.
2541 VecDesc const *VD = nullptr;
2542 if (VT.isVector() && !(VD = getVecDesc()))
2543 return false;
2544
2545 // Find users of the node that store the results (and share input chains). The
2546 // destination pointers can be used instead of creating stack allocations.
2547 SDValue StoresInChain;
2548 SmallVector<StoreSDNode *, 2> ResultStores(NumResults);
2549 for (SDNode *User : Node->users()) {
2551 continue;
2552 auto *ST = cast<StoreSDNode>(User);
2553 SDValue StoreValue = ST->getValue();
2554 unsigned ResNo = StoreValue.getResNo();
2555 // Ensure the store corresponds to an output pointer.
2556 if (CallRetResNo == ResNo)
2557 continue;
2558 // Ensure the store to the default address space and not atomic or volatile.
2559 if (!ST->isSimple() || ST->getAddressSpace() != 0)
2560 continue;
2561 // Ensure all store chains are the same (so they don't alias).
2562 if (StoresInChain && ST->getChain() != StoresInChain)
2563 continue;
2564 // Ensure the store is properly aligned.
2565 Type *StoreType = StoreValue.getValueType().getTypeForEVT(Ctx);
2566 if (ST->getAlign() <
2567 getDataLayout().getABITypeAlign(StoreType->getScalarType()))
2568 continue;
2569 // Avoid:
2570 // 1. Creating cyclic dependencies.
2571 // 2. Expanding the node to a call within a call sequence.
2573 continue;
2574 ResultStores[ResNo] = ST;
2575 StoresInChain = ST->getChain();
2576 }
2577
2579
2580 // Pass the arguments.
2581 for (const SDValue &Op : Node->op_values()) {
2582 EVT ArgVT = Op.getValueType();
2583 Type *ArgTy = ArgVT.getTypeForEVT(Ctx);
2584 Args.emplace_back(Op, ArgTy);
2585 }
2586
2587 // Pass the output pointers.
2588 SmallVector<SDValue, 2> ResultPtrs(NumResults);
2590 for (auto [ResNo, ST] : llvm::enumerate(ResultStores)) {
2591 if (ResNo == CallRetResNo)
2592 continue;
2593 EVT ResVT = Node->getValueType(ResNo);
2594 SDValue ResultPtr = ST ? ST->getBasePtr() : CreateStackTemporary(ResVT);
2595 ResultPtrs[ResNo] = ResultPtr;
2596 Args.emplace_back(ResultPtr, PointerTy);
2597 }
2598
2599 SDLoc DL(Node);
2600
2601 // Pass the vector mask (if required).
2602 if (VD && VD->isMasked()) {
2603 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), Ctx, VT);
2604 SDValue Mask = getBoolConstant(true, DL, MaskVT, VT);
2605 Args.emplace_back(Mask, MaskVT.getTypeForEVT(Ctx));
2606 }
2607
2608 Type *RetType = CallRetResNo.has_value()
2609 ? Node->getValueType(*CallRetResNo).getTypeForEVT(Ctx)
2610 : Type::getVoidTy(Ctx);
2611 SDValue InChain = StoresInChain ? StoresInChain : getEntryNode();
2612 SDValue Callee = getExternalSymbol(VD ? VD->getVectorFnName().data() : LCName,
2613 TLI->getPointerTy(getDataLayout()));
2615 CLI.setDebugLoc(DL).setChain(InChain).setLibCallee(
2616 TLI->getLibcallCallingConv(LC), RetType, Callee, std::move(Args));
2617
2618 auto [Call, CallChain] = TLI->LowerCallTo(CLI);
2619
2620 for (auto [ResNo, ResultPtr] : llvm::enumerate(ResultPtrs)) {
2621 if (ResNo == CallRetResNo) {
2622 Results.push_back(Call);
2623 continue;
2624 }
2625 MachinePointerInfo PtrInfo;
2626 SDValue LoadResult =
2627 getLoad(Node->getValueType(ResNo), DL, CallChain, ResultPtr, PtrInfo);
2628 SDValue OutChain = LoadResult.getValue(1);
2629
2630 if (StoreSDNode *ST = ResultStores[ResNo]) {
2631 // Replace store with the library call.
2632 ReplaceAllUsesOfValueWith(SDValue(ST, 0), OutChain);
2633 PtrInfo = ST->getPointerInfo();
2634 } else {
2636 getMachineFunction(), cast<FrameIndexSDNode>(ResultPtr)->getIndex());
2637 }
2638
2639 Results.push_back(LoadResult);
2640 }
2641
2642 return true;
2643}
2644
2646 SDLoc dl(Node);
2648 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2649 EVT VT = Node->getValueType(0);
2650 SDValue Tmp1 = Node->getOperand(0);
2651 SDValue Tmp2 = Node->getOperand(1);
2652 const MaybeAlign MA(Node->getConstantOperandVal(3));
2653
2654 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2655 Tmp2, MachinePointerInfo(V));
2656 SDValue VAList = VAListLoad;
2657
2658 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2659 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2660 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2661
2662 VAList = getNode(
2663 ISD::AND, dl, VAList.getValueType(), VAList,
2664 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2665 }
2666
2667 // Increment the pointer, VAList, to the next vaarg
2668 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2669 getConstant(getDataLayout().getTypeAllocSize(
2670 VT.getTypeForEVT(*getContext())),
2671 dl, VAList.getValueType()));
2672 // Store the incremented VAList to the legalized pointer
2673 Tmp1 =
2674 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2675 // Load the actual argument out of the pointer VAList
2676 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2677}
2678
2680 SDLoc dl(Node);
2682 // This defaults to loading a pointer from the input and storing it to the
2683 // output, returning the chain.
2684 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2685 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2686 SDValue Tmp1 =
2687 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2688 Node->getOperand(2), MachinePointerInfo(VS));
2689 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2690 MachinePointerInfo(VD));
2691}
2692
2694 const DataLayout &DL = getDataLayout();
2695 Type *Ty = VT.getTypeForEVT(*getContext());
2696 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2697
2698 if (TLI->isTypeLegal(VT) || !VT.isVector())
2699 return RedAlign;
2700
2701 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2702 const Align StackAlign = TFI->getStackAlign();
2703
2704 // See if we can choose a smaller ABI alignment in cases where it's an
2705 // illegal vector type that will get broken down.
2706 if (RedAlign > StackAlign) {
2707 EVT IntermediateVT;
2708 MVT RegisterVT;
2709 unsigned NumIntermediates;
2710 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2711 NumIntermediates, RegisterVT);
2712 Ty = IntermediateVT.getTypeForEVT(*getContext());
2713 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2714 if (RedAlign2 < RedAlign)
2715 RedAlign = RedAlign2;
2716
2717 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2718 // If the stack is not realignable, the alignment should be limited to the
2719 // StackAlignment
2720 RedAlign = std::min(RedAlign, StackAlign);
2721 }
2722
2723 return RedAlign;
2724}
2725
2727 MachineFrameInfo &MFI = MF->getFrameInfo();
2728 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2729 int StackID = 0;
2730 if (Bytes.isScalable())
2731 StackID = TFI->getStackIDForScalableVectors();
2732 // The stack id gives an indication of whether the object is scalable or
2733 // not, so it's safe to pass in the minimum size here.
2734 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2735 false, nullptr, StackID);
2736 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2737}
2738
2740 Type *Ty = VT.getTypeForEVT(*getContext());
2741 Align StackAlign =
2742 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2743 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2744}
2745
2747 TypeSize VT1Size = VT1.getStoreSize();
2748 TypeSize VT2Size = VT2.getStoreSize();
2749 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2750 "Don't know how to choose the maximum size when creating a stack "
2751 "temporary");
2752 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2753 ? VT1Size
2754 : VT2Size;
2755
2756 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2757 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2758 const DataLayout &DL = getDataLayout();
2759 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2760 return CreateStackTemporary(Bytes, Align);
2761}
2762
2764 ISD::CondCode Cond, const SDLoc &dl) {
2765 EVT OpVT = N1.getValueType();
2766
2767 auto GetUndefBooleanConstant = [&]() {
2768 if (VT.getScalarType() == MVT::i1 ||
2769 TLI->getBooleanContents(OpVT) ==
2771 return getUNDEF(VT);
2772 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2773 // so we cannot use getUNDEF(). Return zero instead.
2774 return getConstant(0, dl, VT);
2775 };
2776
2777 // These setcc operations always fold.
2778 switch (Cond) {
2779 default: break;
2780 case ISD::SETFALSE:
2781 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2782 case ISD::SETTRUE:
2783 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2784
2785 case ISD::SETOEQ:
2786 case ISD::SETOGT:
2787 case ISD::SETOGE:
2788 case ISD::SETOLT:
2789 case ISD::SETOLE:
2790 case ISD::SETONE:
2791 case ISD::SETO:
2792 case ISD::SETUO:
2793 case ISD::SETUEQ:
2794 case ISD::SETUNE:
2795 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2796 break;
2797 }
2798
2799 if (OpVT.isInteger()) {
2800 // For EQ and NE, we can always pick a value for the undef to make the
2801 // predicate pass or fail, so we can return undef.
2802 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2803 // icmp eq/ne X, undef -> undef.
2804 if ((N1.isUndef() || N2.isUndef()) &&
2805 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2806 return GetUndefBooleanConstant();
2807
2808 // If both operands are undef, we can return undef for int comparison.
2809 // icmp undef, undef -> undef.
2810 if (N1.isUndef() && N2.isUndef())
2811 return GetUndefBooleanConstant();
2812
2813 // icmp X, X -> true/false
2814 // icmp X, undef -> true/false because undef could be X.
2815 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2816 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2817 }
2818
2820 const APInt &C2 = N2C->getAPIntValue();
2822 const APInt &C1 = N1C->getAPIntValue();
2823
2825 dl, VT, OpVT);
2826 }
2827 }
2828
2829 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2830 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2831
2832 if (N1CFP && N2CFP) {
2833 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2834 switch (Cond) {
2835 default: break;
2836 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2837 return GetUndefBooleanConstant();
2838 [[fallthrough]];
2839 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2840 OpVT);
2841 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2842 return GetUndefBooleanConstant();
2843 [[fallthrough]];
2845 R==APFloat::cmpLessThan, dl, VT,
2846 OpVT);
2847 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2848 return GetUndefBooleanConstant();
2849 [[fallthrough]];
2850 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2851 OpVT);
2852 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2853 return GetUndefBooleanConstant();
2854 [[fallthrough]];
2856 VT, OpVT);
2857 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2858 return GetUndefBooleanConstant();
2859 [[fallthrough]];
2861 R==APFloat::cmpEqual, dl, VT,
2862 OpVT);
2863 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2864 return GetUndefBooleanConstant();
2865 [[fallthrough]];
2867 R==APFloat::cmpEqual, dl, VT, OpVT);
2868 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2869 OpVT);
2870 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2871 OpVT);
2873 R==APFloat::cmpEqual, dl, VT,
2874 OpVT);
2875 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2876 OpVT);
2878 R==APFloat::cmpLessThan, dl, VT,
2879 OpVT);
2881 R==APFloat::cmpUnordered, dl, VT,
2882 OpVT);
2884 VT, OpVT);
2885 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2886 OpVT);
2887 }
2888 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2889 // Ensure that the constant occurs on the RHS.
2891 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2892 return SDValue();
2893 return getSetCC(dl, VT, N2, N1, SwappedCond);
2894 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2895 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2896 // If an operand is known to be a nan (or undef that could be a nan), we can
2897 // fold it.
2898 // Choosing NaN for the undef will always make unordered comparison succeed
2899 // and ordered comparison fails.
2900 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2901 switch (ISD::getUnorderedFlavor(Cond)) {
2902 default:
2903 llvm_unreachable("Unknown flavor!");
2904 case 0: // Known false.
2905 return getBoolConstant(false, dl, VT, OpVT);
2906 case 1: // Known true.
2907 return getBoolConstant(true, dl, VT, OpVT);
2908 case 2: // Undefined.
2909 return GetUndefBooleanConstant();
2910 }
2911 }
2912
2913 // Could not fold it.
2914 return SDValue();
2915}
2916
2917/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2918/// use this predicate to simplify operations downstream.
2920 unsigned BitWidth = Op.getScalarValueSizeInBits();
2922}
2923
2924/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2925/// this predicate to simplify operations downstream. Mask is known to be zero
2926/// for bits that V cannot have.
2928 unsigned Depth) const {
2929 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2930}
2931
2932/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2933/// DemandedElts. We use this predicate to simplify operations downstream.
2934/// Mask is known to be zero for bits that V cannot have.
2936 const APInt &DemandedElts,
2937 unsigned Depth) const {
2938 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2939}
2940
2941/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2942/// DemandedElts. We use this predicate to simplify operations downstream.
2944 unsigned Depth /* = 0 */) const {
2945 return computeKnownBits(V, DemandedElts, Depth).isZero();
2946}
2947
2948/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2950 unsigned Depth) const {
2951 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2952}
2953
2955 const APInt &DemandedElts,
2956 unsigned Depth) const {
2957 EVT VT = Op.getValueType();
2958 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2959
2960 unsigned NumElts = VT.getVectorNumElements();
2961 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2962
2963 APInt KnownZeroElements = APInt::getZero(NumElts);
2964 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2965 if (!DemandedElts[EltIdx])
2966 continue; // Don't query elements that are not demanded.
2967 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2968 if (MaskedVectorIsZero(Op, Mask, Depth))
2969 KnownZeroElements.setBit(EltIdx);
2970 }
2971 return KnownZeroElements;
2972}
2973
2974/// isSplatValue - Return true if the vector V has the same value
2975/// across all DemandedElts. For scalable vectors, we don't know the
2976/// number of lanes at compile time. Instead, we use a 1 bit APInt
2977/// to represent a conservative value for all lanes; that is, that
2978/// one bit value is implicitly splatted across all lanes.
2979bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2980 APInt &UndefElts, unsigned Depth) const {
2981 unsigned Opcode = V.getOpcode();
2982 EVT VT = V.getValueType();
2983 assert(VT.isVector() && "Vector type expected");
2984 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2985 "scalable demanded bits are ignored");
2986
2987 if (!DemandedElts)
2988 return false; // No demanded elts, better to assume we don't know anything.
2989
2990 if (Depth >= MaxRecursionDepth)
2991 return false; // Limit search depth.
2992
2993 // Deal with some common cases here that work for both fixed and scalable
2994 // vector types.
2995 switch (Opcode) {
2996 case ISD::SPLAT_VECTOR:
2997 UndefElts = V.getOperand(0).isUndef()
2998 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2999 : APInt(DemandedElts.getBitWidth(), 0);
3000 return true;
3001 case ISD::ADD:
3002 case ISD::SUB:
3003 case ISD::AND:
3004 case ISD::XOR:
3005 case ISD::OR: {
3006 APInt UndefLHS, UndefRHS;
3007 SDValue LHS = V.getOperand(0);
3008 SDValue RHS = V.getOperand(1);
3009 // Only recognize splats with the same demanded undef elements for both
3010 // operands, otherwise we might fail to handle binop-specific undef
3011 // handling.
3012 // e.g. (and undef, 0) -> 0 etc.
3013 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
3014 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
3015 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
3016 UndefElts = UndefLHS | UndefRHS;
3017 return true;
3018 }
3019 return false;
3020 }
3021 case ISD::ABS:
3022 case ISD::TRUNCATE:
3023 case ISD::SIGN_EXTEND:
3024 case ISD::ZERO_EXTEND:
3025 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
3026 default:
3027 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
3028 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
3029 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
3030 Depth);
3031 break;
3032 }
3033
3034 // We don't support other cases than those above for scalable vectors at
3035 // the moment.
3036 if (VT.isScalableVector())
3037 return false;
3038
3039 unsigned NumElts = VT.getVectorNumElements();
3040 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3041 UndefElts = APInt::getZero(NumElts);
3042
3043 switch (Opcode) {
3044 case ISD::BUILD_VECTOR: {
3045 SDValue Scl;
3046 for (unsigned i = 0; i != NumElts; ++i) {
3047 SDValue Op = V.getOperand(i);
3048 if (Op.isUndef()) {
3049 UndefElts.setBit(i);
3050 continue;
3051 }
3052 if (!DemandedElts[i])
3053 continue;
3054 if (Scl && Scl != Op)
3055 return false;
3056 Scl = Op;
3057 }
3058 return true;
3059 }
3060 case ISD::VECTOR_SHUFFLE: {
3061 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3062 APInt DemandedLHS = APInt::getZero(NumElts);
3063 APInt DemandedRHS = APInt::getZero(NumElts);
3064 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3065 for (int i = 0; i != (int)NumElts; ++i) {
3066 int M = Mask[i];
3067 if (M < 0) {
3068 UndefElts.setBit(i);
3069 continue;
3070 }
3071 if (!DemandedElts[i])
3072 continue;
3073 if (M < (int)NumElts)
3074 DemandedLHS.setBit(M);
3075 else
3076 DemandedRHS.setBit(M - NumElts);
3077 }
3078
3079 // If we aren't demanding either op, assume there's no splat.
3080 // If we are demanding both ops, assume there's no splat.
3081 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3082 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3083 return false;
3084
3085 // See if the demanded elts of the source op is a splat or we only demand
3086 // one element, which should always be a splat.
3087 // TODO: Handle source ops splats with undefs.
3088 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3089 APInt SrcUndefs;
3090 return (SrcElts.popcount() == 1) ||
3091 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3092 (SrcElts & SrcUndefs).isZero());
3093 };
3094 if (!DemandedLHS.isZero())
3095 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3096 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3097 }
3099 // Offset the demanded elts by the subvector index.
3100 SDValue Src = V.getOperand(0);
3101 // We don't support scalable vectors at the moment.
3102 if (Src.getValueType().isScalableVector())
3103 return false;
3104 uint64_t Idx = V.getConstantOperandVal(1);
3105 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3106 APInt UndefSrcElts;
3107 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3108 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3109 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3110 return true;
3111 }
3112 break;
3113 }
3117 // Widen the demanded elts by the src element count.
3118 SDValue Src = V.getOperand(0);
3119 // We don't support scalable vectors at the moment.
3120 if (Src.getValueType().isScalableVector())
3121 return false;
3122 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3123 APInt UndefSrcElts;
3124 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3125 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3126 UndefElts = UndefSrcElts.trunc(NumElts);
3127 return true;
3128 }
3129 break;
3130 }
3131 case ISD::BITCAST: {
3132 SDValue Src = V.getOperand(0);
3133 EVT SrcVT = Src.getValueType();
3134 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3135 unsigned BitWidth = VT.getScalarSizeInBits();
3136
3137 // Ignore bitcasts from unsupported types.
3138 // TODO: Add fp support?
3139 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3140 break;
3141
3142 // Bitcast 'small element' vector to 'large element' vector.
3143 if ((BitWidth % SrcBitWidth) == 0) {
3144 // See if each sub element is a splat.
3145 unsigned Scale = BitWidth / SrcBitWidth;
3146 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3147 APInt ScaledDemandedElts =
3148 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3149 for (unsigned I = 0; I != Scale; ++I) {
3150 APInt SubUndefElts;
3151 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3152 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3153 SubDemandedElts &= ScaledDemandedElts;
3154 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3155 return false;
3156 // TODO: Add support for merging sub undef elements.
3157 if (!SubUndefElts.isZero())
3158 return false;
3159 }
3160 return true;
3161 }
3162 break;
3163 }
3164 }
3165
3166 return false;
3167}
3168
3169/// Helper wrapper to main isSplatValue function.
3170bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3171 EVT VT = V.getValueType();
3172 assert(VT.isVector() && "Vector type expected");
3173
3174 APInt UndefElts;
3175 // Since the number of lanes in a scalable vector is unknown at compile time,
3176 // we track one bit which is implicitly broadcast to all lanes. This means
3177 // that all lanes in a scalable vector are considered demanded.
3178 APInt DemandedElts
3180 return isSplatValue(V, DemandedElts, UndefElts) &&
3181 (AllowUndefs || !UndefElts);
3182}
3183
3186
3187 EVT VT = V.getValueType();
3188 unsigned Opcode = V.getOpcode();
3189 switch (Opcode) {
3190 default: {
3191 APInt UndefElts;
3192 // Since the number of lanes in a scalable vector is unknown at compile time,
3193 // we track one bit which is implicitly broadcast to all lanes. This means
3194 // that all lanes in a scalable vector are considered demanded.
3195 APInt DemandedElts
3197
3198 if (isSplatValue(V, DemandedElts, UndefElts)) {
3199 if (VT.isScalableVector()) {
3200 // DemandedElts and UndefElts are ignored for scalable vectors, since
3201 // the only supported cases are SPLAT_VECTOR nodes.
3202 SplatIdx = 0;
3203 } else {
3204 // Handle case where all demanded elements are UNDEF.
3205 if (DemandedElts.isSubsetOf(UndefElts)) {
3206 SplatIdx = 0;
3207 return getUNDEF(VT);
3208 }
3209 SplatIdx = (UndefElts & DemandedElts).countr_one();
3210 }
3211 return V;
3212 }
3213 break;
3214 }
3215 case ISD::SPLAT_VECTOR:
3216 SplatIdx = 0;
3217 return V;
3218 case ISD::VECTOR_SHUFFLE: {
3219 assert(!VT.isScalableVector());
3220 // Check if this is a shuffle node doing a splat.
3221 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3222 // getTargetVShiftNode currently struggles without the splat source.
3223 auto *SVN = cast<ShuffleVectorSDNode>(V);
3224 if (!SVN->isSplat())
3225 break;
3226 int Idx = SVN->getSplatIndex();
3227 int NumElts = V.getValueType().getVectorNumElements();
3228 SplatIdx = Idx % NumElts;
3229 return V.getOperand(Idx / NumElts);
3230 }
3231 }
3232
3233 return SDValue();
3234}
3235
3237 int SplatIdx;
3238 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3239 EVT SVT = SrcVector.getValueType().getScalarType();
3240 EVT LegalSVT = SVT;
3241 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3242 if (!SVT.isInteger())
3243 return SDValue();
3244 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3245 if (LegalSVT.bitsLT(SVT))
3246 return SDValue();
3247 }
3248 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3249 }
3250 return SDValue();
3251}
3252
3253std::optional<ConstantRange>
3255 unsigned Depth) const {
3256 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3257 V.getOpcode() == ISD::SRA) &&
3258 "Unknown shift node");
3259 // Shifting more than the bitwidth is not valid.
3260 unsigned BitWidth = V.getScalarValueSizeInBits();
3261
3262 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3263 const APInt &ShAmt = Cst->getAPIntValue();
3264 if (ShAmt.uge(BitWidth))
3265 return std::nullopt;
3266 return ConstantRange(ShAmt);
3267 }
3268
3269 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3270 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3271 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3272 if (!DemandedElts[i])
3273 continue;
3274 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3275 if (!SA) {
3276 MinAmt = MaxAmt = nullptr;
3277 break;
3278 }
3279 const APInt &ShAmt = SA->getAPIntValue();
3280 if (ShAmt.uge(BitWidth))
3281 return std::nullopt;
3282 if (!MinAmt || MinAmt->ugt(ShAmt))
3283 MinAmt = &ShAmt;
3284 if (!MaxAmt || MaxAmt->ult(ShAmt))
3285 MaxAmt = &ShAmt;
3286 }
3287 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3288 "Failed to find matching min/max shift amounts");
3289 if (MinAmt && MaxAmt)
3290 return ConstantRange(*MinAmt, *MaxAmt + 1);
3291 }
3292
3293 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3294 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3295 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3296 if (KnownAmt.getMaxValue().ult(BitWidth))
3297 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3298
3299 return std::nullopt;
3300}
3301
3302std::optional<unsigned>
3304 unsigned Depth) const {
3305 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3306 V.getOpcode() == ISD::SRA) &&
3307 "Unknown shift node");
3308 if (std::optional<ConstantRange> AmtRange =
3309 getValidShiftAmountRange(V, DemandedElts, Depth))
3310 if (const APInt *ShAmt = AmtRange->getSingleElement())
3311 return ShAmt->getZExtValue();
3312 return std::nullopt;
3313}
3314
3315std::optional<unsigned>
3317 EVT VT = V.getValueType();
3318 APInt DemandedElts = VT.isFixedLengthVector()
3320 : APInt(1, 1);
3321 return getValidShiftAmount(V, DemandedElts, Depth);
3322}
3323
3324std::optional<unsigned>
3326 unsigned Depth) const {
3327 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3328 V.getOpcode() == ISD::SRA) &&
3329 "Unknown shift node");
3330 if (std::optional<ConstantRange> AmtRange =
3331 getValidShiftAmountRange(V, DemandedElts, Depth))
3332 return AmtRange->getUnsignedMin().getZExtValue();
3333 return std::nullopt;
3334}
3335
3336std::optional<unsigned>
3338 EVT VT = V.getValueType();
3339 APInt DemandedElts = VT.isFixedLengthVector()
3341 : APInt(1, 1);
3342 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3343}
3344
3345std::optional<unsigned>
3347 unsigned Depth) const {
3348 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3349 V.getOpcode() == ISD::SRA) &&
3350 "Unknown shift node");
3351 if (std::optional<ConstantRange> AmtRange =
3352 getValidShiftAmountRange(V, DemandedElts, Depth))
3353 return AmtRange->getUnsignedMax().getZExtValue();
3354 return std::nullopt;
3355}
3356
3357std::optional<unsigned>
3359 EVT VT = V.getValueType();
3360 APInt DemandedElts = VT.isFixedLengthVector()
3362 : APInt(1, 1);
3363 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3364}
3365
3366/// Determine which bits of Op are known to be either zero or one and return
3367/// them in Known. For vectors, the known bits are those that are shared by
3368/// every vector element.
3370 EVT VT = Op.getValueType();
3371
3372 // Since the number of lanes in a scalable vector is unknown at compile time,
3373 // we track one bit which is implicitly broadcast to all lanes. This means
3374 // that all lanes in a scalable vector are considered demanded.
3375 APInt DemandedElts = VT.isFixedLengthVector()
3377 : APInt(1, 1);
3378 return computeKnownBits(Op, DemandedElts, Depth);
3379}
3380
3381/// Determine which bits of Op are known to be either zero or one and return
3382/// them in Known. The DemandedElts argument allows us to only collect the known
3383/// bits that are shared by the requested vector elements.
3385 unsigned Depth) const {
3386 unsigned BitWidth = Op.getScalarValueSizeInBits();
3387
3388 KnownBits Known(BitWidth); // Don't know anything.
3389
3390 if (auto OptAPInt = Op->bitcastToAPInt()) {
3391 // We know all of the bits for a constant!
3392 return KnownBits::makeConstant(*std::move(OptAPInt));
3393 }
3394
3395 if (Depth >= MaxRecursionDepth)
3396 return Known; // Limit search depth.
3397
3398 KnownBits Known2;
3399 unsigned NumElts = DemandedElts.getBitWidth();
3400 assert((!Op.getValueType().isFixedLengthVector() ||
3401 NumElts == Op.getValueType().getVectorNumElements()) &&
3402 "Unexpected vector size");
3403
3404 if (!DemandedElts)
3405 return Known; // No demanded elts, better to assume we don't know anything.
3406
3407 unsigned Opcode = Op.getOpcode();
3408 switch (Opcode) {
3409 case ISD::MERGE_VALUES:
3410 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3411 Depth + 1);
3412 case ISD::SPLAT_VECTOR: {
3413 SDValue SrcOp = Op.getOperand(0);
3414 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3415 "Expected SPLAT_VECTOR implicit truncation");
3416 // Implicitly truncate the bits to match the official semantics of
3417 // SPLAT_VECTOR.
3418 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3419 break;
3420 }
3422 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3423 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3424 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3425 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3426 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3427 }
3428 break;
3429 }
3430 case ISD::STEP_VECTOR: {
3431 const APInt &Step = Op.getConstantOperandAPInt(0);
3432
3433 if (Step.isPowerOf2())
3434 Known.Zero.setLowBits(Step.logBase2());
3435
3437
3438 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3439 break;
3440 const APInt MinNumElts =
3441 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3442
3443 bool Overflow;
3444 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3446 .umul_ov(MinNumElts, Overflow);
3447 if (Overflow)
3448 break;
3449
3450 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3451 if (Overflow)
3452 break;
3453
3454 Known.Zero.setHighBits(MaxValue.countl_zero());
3455 break;
3456 }
3457 case ISD::BUILD_VECTOR:
3458 assert(!Op.getValueType().isScalableVector());
3459 // Collect the known bits that are shared by every demanded vector element.
3460 Known.setAllConflict();
3461 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3462 if (!DemandedElts[i])
3463 continue;
3464
3465 SDValue SrcOp = Op.getOperand(i);
3466 Known2 = computeKnownBits(SrcOp, Depth + 1);
3467
3468 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3469 if (SrcOp.getValueSizeInBits() != BitWidth) {
3470 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3471 "Expected BUILD_VECTOR implicit truncation");
3472 Known2 = Known2.trunc(BitWidth);
3473 }
3474
3475 // Known bits are the values that are shared by every demanded element.
3476 Known = Known.intersectWith(Known2);
3477
3478 // If we don't know any bits, early out.
3479 if (Known.isUnknown())
3480 break;
3481 }
3482 break;
3483 case ISD::VECTOR_SHUFFLE: {
3484 assert(!Op.getValueType().isScalableVector());
3485 // Collect the known bits that are shared by every vector element referenced
3486 // by the shuffle.
3487 APInt DemandedLHS, DemandedRHS;
3489 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3490 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3491 DemandedLHS, DemandedRHS))
3492 break;
3493
3494 // Known bits are the values that are shared by every demanded element.
3495 Known.setAllConflict();
3496 if (!!DemandedLHS) {
3497 SDValue LHS = Op.getOperand(0);
3498 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3499 Known = Known.intersectWith(Known2);
3500 }
3501 // If we don't know any bits, early out.
3502 if (Known.isUnknown())
3503 break;
3504 if (!!DemandedRHS) {
3505 SDValue RHS = Op.getOperand(1);
3506 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3507 Known = Known.intersectWith(Known2);
3508 }
3509 break;
3510 }
3511 case ISD::VSCALE: {
3513 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3514 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3515 break;
3516 }
3517 case ISD::CONCAT_VECTORS: {
3518 if (Op.getValueType().isScalableVector())
3519 break;
3520 // Split DemandedElts and test each of the demanded subvectors.
3521 Known.setAllConflict();
3522 EVT SubVectorVT = Op.getOperand(0).getValueType();
3523 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3524 unsigned NumSubVectors = Op.getNumOperands();
3525 for (unsigned i = 0; i != NumSubVectors; ++i) {
3526 APInt DemandedSub =
3527 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3528 if (!!DemandedSub) {
3529 SDValue Sub = Op.getOperand(i);
3530 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3531 Known = Known.intersectWith(Known2);
3532 }
3533 // If we don't know any bits, early out.
3534 if (Known.isUnknown())
3535 break;
3536 }
3537 break;
3538 }
3539 case ISD::INSERT_SUBVECTOR: {
3540 if (Op.getValueType().isScalableVector())
3541 break;
3542 // Demand any elements from the subvector and the remainder from the src its
3543 // inserted into.
3544 SDValue Src = Op.getOperand(0);
3545 SDValue Sub = Op.getOperand(1);
3546 uint64_t Idx = Op.getConstantOperandVal(2);
3547 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3548 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3549 APInt DemandedSrcElts = DemandedElts;
3550 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3551
3552 Known.setAllConflict();
3553 if (!!DemandedSubElts) {
3554 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3555 if (Known.isUnknown())
3556 break; // early-out.
3557 }
3558 if (!!DemandedSrcElts) {
3559 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3560 Known = Known.intersectWith(Known2);
3561 }
3562 break;
3563 }
3565 // Offset the demanded elts by the subvector index.
3566 SDValue Src = Op.getOperand(0);
3567 // Bail until we can represent demanded elements for scalable vectors.
3568 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3569 break;
3570 uint64_t Idx = Op.getConstantOperandVal(1);
3571 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3572 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3573 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3574 break;
3575 }
3576 case ISD::SCALAR_TO_VECTOR: {
3577 if (Op.getValueType().isScalableVector())
3578 break;
3579 // We know about scalar_to_vector as much as we know about it source,
3580 // which becomes the first element of otherwise unknown vector.
3581 if (DemandedElts != 1)
3582 break;
3583
3584 SDValue N0 = Op.getOperand(0);
3585 Known = computeKnownBits(N0, Depth + 1);
3586 if (N0.getValueSizeInBits() != BitWidth)
3587 Known = Known.trunc(BitWidth);
3588
3589 break;
3590 }
3591 case ISD::BITCAST: {
3592 if (Op.getValueType().isScalableVector())
3593 break;
3594
3595 SDValue N0 = Op.getOperand(0);
3596 EVT SubVT = N0.getValueType();
3597 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3598
3599 // Ignore bitcasts from unsupported types.
3600 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3601 break;
3602
3603 // Fast handling of 'identity' bitcasts.
3604 if (BitWidth == SubBitWidth) {
3605 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3606 break;
3607 }
3608
3609 bool IsLE = getDataLayout().isLittleEndian();
3610
3611 // Bitcast 'small element' vector to 'large element' scalar/vector.
3612 if ((BitWidth % SubBitWidth) == 0) {
3613 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3614
3615 // Collect known bits for the (larger) output by collecting the known
3616 // bits from each set of sub elements and shift these into place.
3617 // We need to separately call computeKnownBits for each set of
3618 // sub elements as the knownbits for each is likely to be different.
3619 unsigned SubScale = BitWidth / SubBitWidth;
3620 APInt SubDemandedElts(NumElts * SubScale, 0);
3621 for (unsigned i = 0; i != NumElts; ++i)
3622 if (DemandedElts[i])
3623 SubDemandedElts.setBit(i * SubScale);
3624
3625 for (unsigned i = 0; i != SubScale; ++i) {
3626 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3627 Depth + 1);
3628 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3629 Known.insertBits(Known2, SubBitWidth * Shifts);
3630 }
3631 }
3632
3633 // Bitcast 'large element' scalar/vector to 'small element' vector.
3634 if ((SubBitWidth % BitWidth) == 0) {
3635 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3636
3637 // Collect known bits for the (smaller) output by collecting the known
3638 // bits from the overlapping larger input elements and extracting the
3639 // sub sections we actually care about.
3640 unsigned SubScale = SubBitWidth / BitWidth;
3641 APInt SubDemandedElts =
3642 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3643 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3644
3645 Known.setAllConflict();
3646 for (unsigned i = 0; i != NumElts; ++i)
3647 if (DemandedElts[i]) {
3648 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3649 unsigned Offset = (Shifts % SubScale) * BitWidth;
3650 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3651 // If we don't know any bits, early out.
3652 if (Known.isUnknown())
3653 break;
3654 }
3655 }
3656 break;
3657 }
3658 case ISD::AND:
3659 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3660 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3661
3662 Known &= Known2;
3663 break;
3664 case ISD::OR:
3665 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3666 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3667
3668 Known |= Known2;
3669 break;
3670 case ISD::XOR:
3671 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3672 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3673
3674 Known ^= Known2;
3675 break;
3676 case ISD::MUL: {
3677 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3678 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3679 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3680 // TODO: SelfMultiply can be poison, but not undef.
3681 if (SelfMultiply)
3682 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3683 Op.getOperand(0), DemandedElts, false, Depth + 1);
3684 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3685
3686 // If the multiplication is known not to overflow, the product of a number
3687 // with itself is non-negative. Only do this if we didn't already computed
3688 // the opposite value for the sign bit.
3689 if (Op->getFlags().hasNoSignedWrap() &&
3690 Op.getOperand(0) == Op.getOperand(1) &&
3691 !Known.isNegative())
3692 Known.makeNonNegative();
3693 break;
3694 }
3695 case ISD::MULHU: {
3696 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3697 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3698 Known = KnownBits::mulhu(Known, Known2);
3699 break;
3700 }
3701 case ISD::MULHS: {
3702 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3703 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3704 Known = KnownBits::mulhs(Known, Known2);
3705 break;
3706 }
3707 case ISD::ABDU: {
3708 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3709 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3710 Known = KnownBits::abdu(Known, Known2);
3711 break;
3712 }
3713 case ISD::ABDS: {
3714 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3715 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3716 Known = KnownBits::abds(Known, Known2);
3717 unsigned SignBits1 =
3718 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3719 if (SignBits1 == 1)
3720 break;
3721 unsigned SignBits0 =
3722 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3723 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3724 break;
3725 }
3726 case ISD::UMUL_LOHI: {
3727 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3728 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3729 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3730 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3731 if (Op.getResNo() == 0)
3732 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3733 else
3734 Known = KnownBits::mulhu(Known, Known2);
3735 break;
3736 }
3737 case ISD::SMUL_LOHI: {
3738 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3739 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3740 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3741 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3742 if (Op.getResNo() == 0)
3743 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3744 else
3745 Known = KnownBits::mulhs(Known, Known2);
3746 break;
3747 }
3748 case ISD::AVGFLOORU: {
3749 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3750 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3751 Known = KnownBits::avgFloorU(Known, Known2);
3752 break;
3753 }
3754 case ISD::AVGCEILU: {
3755 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3756 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3757 Known = KnownBits::avgCeilU(Known, Known2);
3758 break;
3759 }
3760 case ISD::AVGFLOORS: {
3761 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3762 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3763 Known = KnownBits::avgFloorS(Known, Known2);
3764 break;
3765 }
3766 case ISD::AVGCEILS: {
3767 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3768 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3769 Known = KnownBits::avgCeilS(Known, Known2);
3770 break;
3771 }
3772 case ISD::SELECT:
3773 case ISD::VSELECT:
3774 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3775 // If we don't know any bits, early out.
3776 if (Known.isUnknown())
3777 break;
3778 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3779
3780 // Only known if known in both the LHS and RHS.
3781 Known = Known.intersectWith(Known2);
3782 break;
3783 case ISD::SELECT_CC:
3784 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3785 // If we don't know any bits, early out.
3786 if (Known.isUnknown())
3787 break;
3788 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3789
3790 // Only known if known in both the LHS and RHS.
3791 Known = Known.intersectWith(Known2);
3792 break;
3793 case ISD::SMULO:
3794 case ISD::UMULO:
3795 if (Op.getResNo() != 1)
3796 break;
3797 // The boolean result conforms to getBooleanContents.
3798 // If we know the result of a setcc has the top bits zero, use this info.
3799 // We know that we have an integer-based boolean since these operations
3800 // are only available for integer.
3801 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3803 BitWidth > 1)
3804 Known.Zero.setBitsFrom(1);
3805 break;
3806 case ISD::SETCC:
3807 case ISD::SETCCCARRY:
3808 case ISD::STRICT_FSETCC:
3809 case ISD::STRICT_FSETCCS: {
3810 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3811 // If we know the result of a setcc has the top bits zero, use this info.
3812 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3814 BitWidth > 1)
3815 Known.Zero.setBitsFrom(1);
3816 break;
3817 }
3818 case ISD::SHL: {
3819 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3820 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3821
3822 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3823 bool NSW = Op->getFlags().hasNoSignedWrap();
3824
3825 bool ShAmtNonZero = Known2.isNonZero();
3826
3827 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3828
3829 // Minimum shift low bits are known zero.
3830 if (std::optional<unsigned> ShMinAmt =
3831 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3832 Known.Zero.setLowBits(*ShMinAmt);
3833 break;
3834 }
3835 case ISD::SRL:
3836 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3837 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3838 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3839 Op->getFlags().hasExact());
3840
3841 // Minimum shift high bits are known zero.
3842 if (std::optional<unsigned> ShMinAmt =
3843 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3844 Known.Zero.setHighBits(*ShMinAmt);
3845 break;
3846 case ISD::SRA:
3847 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3848 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3849 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3850 Op->getFlags().hasExact());
3851 break;
3852 case ISD::ROTL:
3853 case ISD::ROTR:
3854 if (ConstantSDNode *C =
3855 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3856 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3857
3858 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3859
3860 // Canonicalize to ROTR.
3861 if (Opcode == ISD::ROTL && Amt != 0)
3862 Amt = BitWidth - Amt;
3863
3864 Known.Zero = Known.Zero.rotr(Amt);
3865 Known.One = Known.One.rotr(Amt);
3866 }
3867 break;
3868 case ISD::FSHL:
3869 case ISD::FSHR:
3870 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3871 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3872
3873 // For fshl, 0-shift returns the 1st arg.
3874 // For fshr, 0-shift returns the 2nd arg.
3875 if (Amt == 0) {
3876 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3877 DemandedElts, Depth + 1);
3878 break;
3879 }
3880
3881 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3882 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3883 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3884 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3885 if (Opcode == ISD::FSHL) {
3886 Known <<= Amt;
3887 Known2 >>= BitWidth - Amt;
3888 } else {
3889 Known <<= BitWidth - Amt;
3890 Known2 >>= Amt;
3891 }
3892 Known = Known.unionWith(Known2);
3893 }
3894 break;
3895 case ISD::SHL_PARTS:
3896 case ISD::SRA_PARTS:
3897 case ISD::SRL_PARTS: {
3898 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3899
3900 // Collect lo/hi source values and concatenate.
3901 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3902 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3903 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3904 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3905 Known = Known2.concat(Known);
3906
3907 // Collect shift amount.
3908 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3909
3910 if (Opcode == ISD::SHL_PARTS)
3911 Known = KnownBits::shl(Known, Known2);
3912 else if (Opcode == ISD::SRA_PARTS)
3913 Known = KnownBits::ashr(Known, Known2);
3914 else // if (Opcode == ISD::SRL_PARTS)
3915 Known = KnownBits::lshr(Known, Known2);
3916
3917 // TODO: Minimum shift low/high bits are known zero.
3918
3919 if (Op.getResNo() == 0)
3920 Known = Known.extractBits(LoBits, 0);
3921 else
3922 Known = Known.extractBits(HiBits, LoBits);
3923 break;
3924 }
3926 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3927 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3928 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3929 break;
3930 }
3931 case ISD::CTTZ:
3932 case ISD::CTTZ_ZERO_UNDEF: {
3933 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3934 // If we have a known 1, its position is our upper bound.
3935 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3936 unsigned LowBits = llvm::bit_width(PossibleTZ);
3937 Known.Zero.setBitsFrom(LowBits);
3938 break;
3939 }
3940 case ISD::CTLZ:
3941 case ISD::CTLZ_ZERO_UNDEF: {
3942 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3943 // If we have a known 1, its position is our upper bound.
3944 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3945 unsigned LowBits = llvm::bit_width(PossibleLZ);
3946 Known.Zero.setBitsFrom(LowBits);
3947 break;
3948 }
3949 case ISD::CTPOP: {
3950 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3951 // If we know some of the bits are zero, they can't be one.
3952 unsigned PossibleOnes = Known2.countMaxPopulation();
3953 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3954 break;
3955 }
3956 case ISD::PARITY: {
3957 // Parity returns 0 everywhere but the LSB.
3958 Known.Zero.setBitsFrom(1);
3959 break;
3960 }
3961 case ISD::MGATHER:
3962 case ISD::MLOAD: {
3963 ISD::LoadExtType ETy =
3964 (Opcode == ISD::MGATHER)
3965 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3966 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3967 if (ETy == ISD::ZEXTLOAD) {
3968 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3969 KnownBits Known0(MemVT.getScalarSizeInBits());
3970 return Known0.zext(BitWidth);
3971 }
3972 break;
3973 }
3974 case ISD::LOAD: {
3976 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3977 if (ISD::isNON_EXTLoad(LD) && Cst) {
3978 // Determine any common known bits from the loaded constant pool value.
3979 Type *CstTy = Cst->getType();
3980 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3981 !Op.getValueType().isScalableVector()) {
3982 // If its a vector splat, then we can (quickly) reuse the scalar path.
3983 // NOTE: We assume all elements match and none are UNDEF.
3984 if (CstTy->isVectorTy()) {
3985 if (const Constant *Splat = Cst->getSplatValue()) {
3986 Cst = Splat;
3987 CstTy = Cst->getType();
3988 }
3989 }
3990 // TODO - do we need to handle different bitwidths?
3991 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3992 // Iterate across all vector elements finding common known bits.
3993 Known.setAllConflict();
3994 for (unsigned i = 0; i != NumElts; ++i) {
3995 if (!DemandedElts[i])
3996 continue;
3997 if (Constant *Elt = Cst->getAggregateElement(i)) {
3998 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3999 const APInt &Value = CInt->getValue();
4000 Known.One &= Value;
4001 Known.Zero &= ~Value;
4002 continue;
4003 }
4004 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4005 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4006 Known.One &= Value;
4007 Known.Zero &= ~Value;
4008 continue;
4009 }
4010 }
4011 Known.One.clearAllBits();
4012 Known.Zero.clearAllBits();
4013 break;
4014 }
4015 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4016 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4017 Known = KnownBits::makeConstant(CInt->getValue());
4018 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4019 Known =
4020 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4021 }
4022 }
4023 }
4024 } else if (Op.getResNo() == 0) {
4025 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4026 KnownBits KnownScalarMemory(ScalarMemorySize);
4027 if (const MDNode *MD = LD->getRanges())
4028 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4029
4030 // Extend the Known bits from memory to the size of the scalar result.
4031 if (ISD::isZEXTLoad(Op.getNode()))
4032 Known = KnownScalarMemory.zext(BitWidth);
4033 else if (ISD::isSEXTLoad(Op.getNode()))
4034 Known = KnownScalarMemory.sext(BitWidth);
4035 else if (ISD::isEXTLoad(Op.getNode()))
4036 Known = KnownScalarMemory.anyext(BitWidth);
4037 else
4038 Known = KnownScalarMemory;
4039 assert(Known.getBitWidth() == BitWidth);
4040 return Known;
4041 }
4042 break;
4043 }
4045 if (Op.getValueType().isScalableVector())
4046 break;
4047 EVT InVT = Op.getOperand(0).getValueType();
4048 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4049 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4050 Known = Known.zext(BitWidth);
4051 break;
4052 }
4053 case ISD::ZERO_EXTEND: {
4054 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4055 Known = Known.zext(BitWidth);
4056 break;
4057 }
4059 if (Op.getValueType().isScalableVector())
4060 break;
4061 EVT InVT = Op.getOperand(0).getValueType();
4062 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4063 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4064 // If the sign bit is known to be zero or one, then sext will extend
4065 // it to the top bits, else it will just zext.
4066 Known = Known.sext(BitWidth);
4067 break;
4068 }
4069 case ISD::SIGN_EXTEND: {
4070 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4071 // If the sign bit is known to be zero or one, then sext will extend
4072 // it to the top bits, else it will just zext.
4073 Known = Known.sext(BitWidth);
4074 break;
4075 }
4077 if (Op.getValueType().isScalableVector())
4078 break;
4079 EVT InVT = Op.getOperand(0).getValueType();
4080 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4081 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4082 Known = Known.anyext(BitWidth);
4083 break;
4084 }
4085 case ISD::ANY_EXTEND: {
4086 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4087 Known = Known.anyext(BitWidth);
4088 break;
4089 }
4090 case ISD::TRUNCATE: {
4091 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4092 Known = Known.trunc(BitWidth);
4093 break;
4094 }
4095 case ISD::AssertZext: {
4096 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4098 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4099 Known.Zero |= (~InMask);
4100 Known.One &= (~Known.Zero);
4101 break;
4102 }
4103 case ISD::AssertAlign: {
4104 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4105 assert(LogOfAlign != 0);
4106
4107 // TODO: Should use maximum with source
4108 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4109 // well as clearing one bits.
4110 Known.Zero.setLowBits(LogOfAlign);
4111 Known.One.clearLowBits(LogOfAlign);
4112 break;
4113 }
4114 case ISD::FGETSIGN:
4115 // All bits are zero except the low bit.
4116 Known.Zero.setBitsFrom(1);
4117 break;
4118 case ISD::ADD:
4119 case ISD::SUB: {
4120 SDNodeFlags Flags = Op.getNode()->getFlags();
4121 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4122 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4124 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4125 Flags.hasNoUnsignedWrap(), Known, Known2);
4126 break;
4127 }
4128 case ISD::USUBO:
4129 case ISD::SSUBO:
4130 case ISD::USUBO_CARRY:
4131 case ISD::SSUBO_CARRY:
4132 if (Op.getResNo() == 1) {
4133 // If we know the result of a setcc has the top bits zero, use this info.
4134 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4136 BitWidth > 1)
4137 Known.Zero.setBitsFrom(1);
4138 break;
4139 }
4140 [[fallthrough]];
4141 case ISD::SUBC: {
4142 assert(Op.getResNo() == 0 &&
4143 "We only compute knownbits for the difference here.");
4144
4145 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4146 KnownBits Borrow(1);
4147 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4148 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4149 // Borrow has bit width 1
4150 Borrow = Borrow.trunc(1);
4151 } else {
4152 Borrow.setAllZero();
4153 }
4154
4155 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4156 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4157 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4158 break;
4159 }
4160 case ISD::UADDO:
4161 case ISD::SADDO:
4162 case ISD::UADDO_CARRY:
4163 case ISD::SADDO_CARRY:
4164 if (Op.getResNo() == 1) {
4165 // If we know the result of a setcc has the top bits zero, use this info.
4166 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4168 BitWidth > 1)
4169 Known.Zero.setBitsFrom(1);
4170 break;
4171 }
4172 [[fallthrough]];
4173 case ISD::ADDC:
4174 case ISD::ADDE: {
4175 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4176
4177 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4178 KnownBits Carry(1);
4179 if (Opcode == ISD::ADDE)
4180 // Can't track carry from glue, set carry to unknown.
4181 Carry.resetAll();
4182 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4183 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4184 // Carry has bit width 1
4185 Carry = Carry.trunc(1);
4186 } else {
4187 Carry.setAllZero();
4188 }
4189
4190 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4191 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4192 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4193 break;
4194 }
4195 case ISD::UDIV: {
4196 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4197 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4198 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4199 break;
4200 }
4201 case ISD::SDIV: {
4202 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4203 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4204 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4205 break;
4206 }
4207 case ISD::SREM: {
4208 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4209 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4210 Known = KnownBits::srem(Known, Known2);
4211 break;
4212 }
4213 case ISD::UREM: {
4214 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4215 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4216 Known = KnownBits::urem(Known, Known2);
4217 break;
4218 }
4219 case ISD::EXTRACT_ELEMENT: {
4220 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4221 const unsigned Index = Op.getConstantOperandVal(1);
4222 const unsigned EltBitWidth = Op.getValueSizeInBits();
4223
4224 // Remove low part of known bits mask
4225 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4226 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4227
4228 // Remove high part of known bit mask
4229 Known = Known.trunc(EltBitWidth);
4230 break;
4231 }
4233 SDValue InVec = Op.getOperand(0);
4234 SDValue EltNo = Op.getOperand(1);
4235 EVT VecVT = InVec.getValueType();
4236 // computeKnownBits not yet implemented for scalable vectors.
4237 if (VecVT.isScalableVector())
4238 break;
4239 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4240 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4241
4242 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4243 // anything about the extended bits.
4244 if (BitWidth > EltBitWidth)
4245 Known = Known.trunc(EltBitWidth);
4246
4247 // If we know the element index, just demand that vector element, else for
4248 // an unknown element index, ignore DemandedElts and demand them all.
4249 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4250 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4251 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4252 DemandedSrcElts =
4253 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4254
4255 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4256 if (BitWidth > EltBitWidth)
4257 Known = Known.anyext(BitWidth);
4258 break;
4259 }
4261 if (Op.getValueType().isScalableVector())
4262 break;
4263
4264 // If we know the element index, split the demand between the
4265 // source vector and the inserted element, otherwise assume we need
4266 // the original demanded vector elements and the value.
4267 SDValue InVec = Op.getOperand(0);
4268 SDValue InVal = Op.getOperand(1);
4269 SDValue EltNo = Op.getOperand(2);
4270 bool DemandedVal = true;
4271 APInt DemandedVecElts = DemandedElts;
4272 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4273 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4274 unsigned EltIdx = CEltNo->getZExtValue();
4275 DemandedVal = !!DemandedElts[EltIdx];
4276 DemandedVecElts.clearBit(EltIdx);
4277 }
4278 Known.setAllConflict();
4279 if (DemandedVal) {
4280 Known2 = computeKnownBits(InVal, Depth + 1);
4281 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4282 }
4283 if (!!DemandedVecElts) {
4284 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4285 Known = Known.intersectWith(Known2);
4286 }
4287 break;
4288 }
4289 case ISD::BITREVERSE: {
4290 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4291 Known = Known2.reverseBits();
4292 break;
4293 }
4294 case ISD::BSWAP: {
4295 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4296 Known = Known2.byteSwap();
4297 break;
4298 }
4299 case ISD::ABS: {
4300 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4301 Known = Known2.abs();
4302 Known.Zero.setHighBits(
4303 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4304 break;
4305 }
4306 case ISD::USUBSAT: {
4307 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4308 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4309 Known = KnownBits::usub_sat(Known, Known2);
4310 break;
4311 }
4312 case ISD::UMIN: {
4313 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4314 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4315 Known = KnownBits::umin(Known, Known2);
4316 break;
4317 }
4318 case ISD::UMAX: {
4319 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4320 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4321 Known = KnownBits::umax(Known, Known2);
4322 break;
4323 }
4324 case ISD::SMIN:
4325 case ISD::SMAX: {
4326 // If we have a clamp pattern, we know that the number of sign bits will be
4327 // the minimum of the clamp min/max range.
4328 bool IsMax = (Opcode == ISD::SMAX);
4329 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4330 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4331 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4332 CstHigh =
4333 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4334 if (CstLow && CstHigh) {
4335 if (!IsMax)
4336 std::swap(CstLow, CstHigh);
4337
4338 const APInt &ValueLow = CstLow->getAPIntValue();
4339 const APInt &ValueHigh = CstHigh->getAPIntValue();
4340 if (ValueLow.sle(ValueHigh)) {
4341 unsigned LowSignBits = ValueLow.getNumSignBits();
4342 unsigned HighSignBits = ValueHigh.getNumSignBits();
4343 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4344 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4345 Known.One.setHighBits(MinSignBits);
4346 break;
4347 }
4348 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4349 Known.Zero.setHighBits(MinSignBits);
4350 break;
4351 }
4352 }
4353 }
4354
4355 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4356 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4357 if (IsMax)
4358 Known = KnownBits::smax(Known, Known2);
4359 else
4360 Known = KnownBits::smin(Known, Known2);
4361
4362 // For SMAX, if CstLow is non-negative we know the result will be
4363 // non-negative and thus all sign bits are 0.
4364 // TODO: There's an equivalent of this for smin with negative constant for
4365 // known ones.
4366 if (IsMax && CstLow) {
4367 const APInt &ValueLow = CstLow->getAPIntValue();
4368 if (ValueLow.isNonNegative()) {
4369 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4370 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4371 }
4372 }
4373
4374 break;
4375 }
4376 case ISD::UINT_TO_FP: {
4377 Known.makeNonNegative();
4378 break;
4379 }
4380 case ISD::SINT_TO_FP: {
4381 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4382 if (Known2.isNonNegative())
4383 Known.makeNonNegative();
4384 else if (Known2.isNegative())
4385 Known.makeNegative();
4386 break;
4387 }
4388 case ISD::FP_TO_UINT_SAT: {
4389 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4390 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4392 break;
4393 }
4394 case ISD::ATOMIC_LOAD: {
4395 // If we are looking at the loaded value.
4396 if (Op.getResNo() == 0) {
4397 auto *AT = cast<AtomicSDNode>(Op);
4398 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4399 KnownBits KnownScalarMemory(ScalarMemorySize);
4400 if (const MDNode *MD = AT->getRanges())
4401 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4402
4403 switch (AT->getExtensionType()) {
4404 case ISD::ZEXTLOAD:
4405 Known = KnownScalarMemory.zext(BitWidth);
4406 break;
4407 case ISD::SEXTLOAD:
4408 Known = KnownScalarMemory.sext(BitWidth);
4409 break;
4410 case ISD::EXTLOAD:
4411 switch (TLI->getExtendForAtomicOps()) {
4412 case ISD::ZERO_EXTEND:
4413 Known = KnownScalarMemory.zext(BitWidth);
4414 break;
4415 case ISD::SIGN_EXTEND:
4416 Known = KnownScalarMemory.sext(BitWidth);
4417 break;
4418 default:
4419 Known = KnownScalarMemory.anyext(BitWidth);
4420 break;
4421 }
4422 break;
4423 case ISD::NON_EXTLOAD:
4424 Known = KnownScalarMemory;
4425 break;
4426 }
4427 assert(Known.getBitWidth() == BitWidth);
4428 }
4429 break;
4430 }
4431 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
4432 if (Op.getResNo() == 1) {
4433 // The boolean result conforms to getBooleanContents.
4434 // If we know the result of a setcc has the top bits zero, use this info.
4435 // We know that we have an integer-based boolean since these operations
4436 // are only available for integer.
4437 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4439 BitWidth > 1)
4440 Known.Zero.setBitsFrom(1);
4441 break;
4442 }
4443 [[fallthrough]];
4444 case ISD::ATOMIC_CMP_SWAP:
4445 case ISD::ATOMIC_SWAP:
4446 case ISD::ATOMIC_LOAD_ADD:
4447 case ISD::ATOMIC_LOAD_SUB:
4448 case ISD::ATOMIC_LOAD_AND:
4449 case ISD::ATOMIC_LOAD_CLR:
4450 case ISD::ATOMIC_LOAD_OR:
4451 case ISD::ATOMIC_LOAD_XOR:
4452 case ISD::ATOMIC_LOAD_NAND:
4453 case ISD::ATOMIC_LOAD_MIN:
4454 case ISD::ATOMIC_LOAD_MAX:
4455 case ISD::ATOMIC_LOAD_UMIN:
4456 case ISD::ATOMIC_LOAD_UMAX: {
4457 // If we are looking at the loaded value.
4458 if (Op.getResNo() == 0) {
4459 auto *AT = cast<AtomicSDNode>(Op);
4460 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4461
4462 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4463 Known.Zero.setBitsFrom(MemBits);
4464 }
4465 break;
4466 }
4467 case ISD::FrameIndex:
4469 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4470 Known, getMachineFunction());
4471 break;
4472
4473 default:
4474 if (Opcode < ISD::BUILTIN_OP_END)
4475 break;
4476 [[fallthrough]];
4480 // TODO: Probably okay to remove after audit; here to reduce change size
4481 // in initial enablement patch for scalable vectors
4482 if (Op.getValueType().isScalableVector())
4483 break;
4484
4485 // Allow the target to implement this method for its nodes.
4486 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4487 break;
4488 }
4489
4490 return Known;
4491}
4492
4493/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4506
4509 // X + 0 never overflow
4510 if (isNullConstant(N1))
4511 return OFK_Never;
4512
4513 // If both operands each have at least two sign bits, the addition
4514 // cannot overflow.
4515 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4516 return OFK_Never;
4517
4518 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4519 return OFK_Sometime;
4520}
4521
4524 // X + 0 never overflow
4525 if (isNullConstant(N1))
4526 return OFK_Never;
4527
4528 // mulhi + 1 never overflow
4529 KnownBits N1Known = computeKnownBits(N1);
4530 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4531 N1Known.getMaxValue().ult(2))
4532 return OFK_Never;
4533
4534 KnownBits N0Known = computeKnownBits(N0);
4535 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4536 N0Known.getMaxValue().ult(2))
4537 return OFK_Never;
4538
4539 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4540 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4541 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4542 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4543}
4544
4547 // X - 0 never overflow
4548 if (isNullConstant(N1))
4549 return OFK_Never;
4550
4551 // If both operands each have at least two sign bits, the subtraction
4552 // cannot overflow.
4553 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4554 return OFK_Never;
4555
4556 KnownBits N0Known = computeKnownBits(N0);
4557 KnownBits N1Known = computeKnownBits(N1);
4558 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4559 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4560 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4561}
4562
4565 // X - 0 never overflow
4566 if (isNullConstant(N1))
4567 return OFK_Never;
4568
4569 KnownBits N0Known = computeKnownBits(N0);
4570 KnownBits N1Known = computeKnownBits(N1);
4571 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4572 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4573 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4574}
4575
4578 // X * 0 and X * 1 never overflow.
4579 if (isNullConstant(N1) || isOneConstant(N1))
4580 return OFK_Never;
4581
4582 KnownBits N0Known = computeKnownBits(N0);
4583 KnownBits N1Known = computeKnownBits(N1);
4584 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4585 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4586 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4587}
4588
4591 // X * 0 and X * 1 never overflow.
4592 if (isNullConstant(N1) || isOneConstant(N1))
4593 return OFK_Never;
4594
4595 // Get the size of the result.
4596 unsigned BitWidth = N0.getScalarValueSizeInBits();
4597
4598 // Sum of the sign bits.
4599 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4600
4601 // If we have enough sign bits, then there's no overflow.
4602 if (SignBits > BitWidth + 1)
4603 return OFK_Never;
4604
4605 if (SignBits == BitWidth + 1) {
4606 // The overflow occurs when the true multiplication of the
4607 // the operands is the minimum negative number.
4608 KnownBits N0Known = computeKnownBits(N0);
4609 KnownBits N1Known = computeKnownBits(N1);
4610 // If one of the operands is non-negative, then there's no
4611 // overflow.
4612 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4613 return OFK_Never;
4614 }
4615
4616 return OFK_Sometime;
4617}
4618
4620 if (Depth >= MaxRecursionDepth)
4621 return false; // Limit search depth.
4622
4623 EVT OpVT = Val.getValueType();
4624 unsigned BitWidth = OpVT.getScalarSizeInBits();
4625
4626 // Is the constant a known power of 2?
4628 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4629 }))
4630 return true;
4631
4632 // A left-shift of a constant one will have exactly one bit set because
4633 // shifting the bit off the end is undefined.
4634 if (Val.getOpcode() == ISD::SHL) {
4635 auto *C = isConstOrConstSplat(Val.getOperand(0));
4636 if (C && C->getAPIntValue() == 1)
4637 return true;
4638 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4639 isKnownNeverZero(Val, Depth);
4640 }
4641
4642 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4643 // one bit set.
4644 if (Val.getOpcode() == ISD::SRL) {
4645 auto *C = isConstOrConstSplat(Val.getOperand(0));
4646 if (C && C->getAPIntValue().isSignMask())
4647 return true;
4648 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4649 isKnownNeverZero(Val, Depth);
4650 }
4651
4652 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4653 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4654
4655 // Are all operands of a build vector constant powers of two?
4656 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4657 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4658 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4659 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4660 return false;
4661 }))
4662 return true;
4663
4664 // Is the operand of a splat vector a constant power of two?
4665 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4667 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4668 return true;
4669
4670 // vscale(power-of-two) is a power-of-two for some targets
4671 if (Val.getOpcode() == ISD::VSCALE &&
4672 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4674 return true;
4675
4676 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4677 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4678 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4680
4681 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4682 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4684
4685 // Looking for `x & -x` pattern:
4686 // If x == 0:
4687 // x & -x -> 0
4688 // If x != 0:
4689 // x & -x -> non-zero pow2
4690 // so if we find the pattern return whether we know `x` is non-zero.
4691 SDValue X;
4692 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4693 return isKnownNeverZero(X, Depth);
4694
4695 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4696 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4697
4698 // More could be done here, though the above checks are enough
4699 // to handle some common cases.
4700 return false;
4701}
4702
4704 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4705 return C1->getValueAPF().getExactLog2Abs() >= 0;
4706
4707 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4708 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4709
4710 return false;
4711}
4712
4714 EVT VT = Op.getValueType();
4715
4716 // Since the number of lanes in a scalable vector is unknown at compile time,
4717 // we track one bit which is implicitly broadcast to all lanes. This means
4718 // that all lanes in a scalable vector are considered demanded.
4719 APInt DemandedElts = VT.isFixedLengthVector()
4721 : APInt(1, 1);
4722 return ComputeNumSignBits(Op, DemandedElts, Depth);
4723}
4724
4725unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4726 unsigned Depth) const {
4727 EVT VT = Op.getValueType();
4728 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4729 unsigned VTBits = VT.getScalarSizeInBits();
4730 unsigned NumElts = DemandedElts.getBitWidth();
4731 unsigned Tmp, Tmp2;
4732 unsigned FirstAnswer = 1;
4733
4734 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4735 const APInt &Val = C->getAPIntValue();
4736 return Val.getNumSignBits();
4737 }
4738
4739 if (Depth >= MaxRecursionDepth)
4740 return 1; // Limit search depth.
4741
4742 if (!DemandedElts)
4743 return 1; // No demanded elts, better to assume we don't know anything.
4744
4745 unsigned Opcode = Op.getOpcode();
4746 switch (Opcode) {
4747 default: break;
4748 case ISD::AssertSext:
4749 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4750 return VTBits-Tmp+1;
4751 case ISD::AssertZext:
4752 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4753 return VTBits-Tmp;
4754 case ISD::MERGE_VALUES:
4755 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4756 Depth + 1);
4757 case ISD::SPLAT_VECTOR: {
4758 // Check if the sign bits of source go down as far as the truncated value.
4759 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4760 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4761 if (NumSrcSignBits > (NumSrcBits - VTBits))
4762 return NumSrcSignBits - (NumSrcBits - VTBits);
4763 break;
4764 }
4765 case ISD::BUILD_VECTOR:
4766 assert(!VT.isScalableVector());
4767 Tmp = VTBits;
4768 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4769 if (!DemandedElts[i])
4770 continue;
4771
4772 SDValue SrcOp = Op.getOperand(i);
4773 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4774 // for constant nodes to ensure we only look at the sign bits.
4776 APInt T = C->getAPIntValue().trunc(VTBits);
4777 Tmp2 = T.getNumSignBits();
4778 } else {
4779 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4780
4781 if (SrcOp.getValueSizeInBits() != VTBits) {
4782 assert(SrcOp.getValueSizeInBits() > VTBits &&
4783 "Expected BUILD_VECTOR implicit truncation");
4784 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4785 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4786 }
4787 }
4788 Tmp = std::min(Tmp, Tmp2);
4789 }
4790 return Tmp;
4791
4792 case ISD::VECTOR_SHUFFLE: {
4793 // Collect the minimum number of sign bits that are shared by every vector
4794 // element referenced by the shuffle.
4795 APInt DemandedLHS, DemandedRHS;
4797 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4798 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4799 DemandedLHS, DemandedRHS))
4800 return 1;
4801
4802 Tmp = std::numeric_limits<unsigned>::max();
4803 if (!!DemandedLHS)
4804 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4805 if (!!DemandedRHS) {
4806 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4807 Tmp = std::min(Tmp, Tmp2);
4808 }
4809 // If we don't know anything, early out and try computeKnownBits fall-back.
4810 if (Tmp == 1)
4811 break;
4812 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4813 return Tmp;
4814 }
4815
4816 case ISD::BITCAST: {
4817 if (VT.isScalableVector())
4818 break;
4819 SDValue N0 = Op.getOperand(0);
4820 EVT SrcVT = N0.getValueType();
4821 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4822
4823 // Ignore bitcasts from unsupported types..
4824 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4825 break;
4826
4827 // Fast handling of 'identity' bitcasts.
4828 if (VTBits == SrcBits)
4829 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4830
4831 bool IsLE = getDataLayout().isLittleEndian();
4832
4833 // Bitcast 'large element' scalar/vector to 'small element' vector.
4834 if ((SrcBits % VTBits) == 0) {
4835 assert(VT.isVector() && "Expected bitcast to vector");
4836
4837 unsigned Scale = SrcBits / VTBits;
4838 APInt SrcDemandedElts =
4839 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4840
4841 // Fast case - sign splat can be simply split across the small elements.
4842 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4843 if (Tmp == SrcBits)
4844 return VTBits;
4845
4846 // Slow case - determine how far the sign extends into each sub-element.
4847 Tmp2 = VTBits;
4848 for (unsigned i = 0; i != NumElts; ++i)
4849 if (DemandedElts[i]) {
4850 unsigned SubOffset = i % Scale;
4851 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4852 SubOffset = SubOffset * VTBits;
4853 if (Tmp <= SubOffset)
4854 return 1;
4855 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4856 }
4857 return Tmp2;
4858 }
4859 break;
4860 }
4861
4863 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4864 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4865 return VTBits - Tmp + 1;
4866 case ISD::SIGN_EXTEND:
4867 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4868 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4870 // Max of the input and what this extends.
4871 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4872 Tmp = VTBits-Tmp+1;
4873 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4874 return std::max(Tmp, Tmp2);
4876 if (VT.isScalableVector())
4877 break;
4878 SDValue Src = Op.getOperand(0);
4879 EVT SrcVT = Src.getValueType();
4880 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4881 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4882 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4883 }
4884 case ISD::SRA:
4885 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4886 // SRA X, C -> adds C sign bits.
4887 if (std::optional<unsigned> ShAmt =
4888 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4889 Tmp = std::min(Tmp + *ShAmt, VTBits);
4890 return Tmp;
4891 case ISD::SHL:
4892 if (std::optional<ConstantRange> ShAmtRange =
4893 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4894 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4895 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4896 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4897 // shifted out, then we can compute the number of sign bits for the
4898 // operand being extended. A future improvement could be to pass along the
4899 // "shifted left by" information in the recursive calls to
4900 // ComputeKnownSignBits. Allowing us to handle this more generically.
4901 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4902 SDValue Ext = Op.getOperand(0);
4903 EVT ExtVT = Ext.getValueType();
4904 SDValue Extendee = Ext.getOperand(0);
4905 EVT ExtendeeVT = Extendee.getValueType();
4906 unsigned SizeDifference =
4907 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4908 if (SizeDifference <= MinShAmt) {
4909 Tmp = SizeDifference +
4910 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4911 if (MaxShAmt < Tmp)
4912 return Tmp - MaxShAmt;
4913 }
4914 }
4915 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4916 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4917 if (MaxShAmt < Tmp)
4918 return Tmp - MaxShAmt;
4919 }
4920 break;
4921 case ISD::AND:
4922 case ISD::OR:
4923 case ISD::XOR: // NOT is handled here.
4924 // Logical binary ops preserve the number of sign bits at the worst.
4925 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4926 if (Tmp != 1) {
4927 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4928 FirstAnswer = std::min(Tmp, Tmp2);
4929 // We computed what we know about the sign bits as our first
4930 // answer. Now proceed to the generic code that uses
4931 // computeKnownBits, and pick whichever answer is better.
4932 }
4933 break;
4934
4935 case ISD::SELECT:
4936 case ISD::VSELECT:
4937 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4938 if (Tmp == 1) return 1; // Early out.
4939 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4940 return std::min(Tmp, Tmp2);
4941 case ISD::SELECT_CC:
4942 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4943 if (Tmp == 1) return 1; // Early out.
4944 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4945 return std::min(Tmp, Tmp2);
4946
4947 case ISD::SMIN:
4948 case ISD::SMAX: {
4949 // If we have a clamp pattern, we know that the number of sign bits will be
4950 // the minimum of the clamp min/max range.
4951 bool IsMax = (Opcode == ISD::SMAX);
4952 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4953 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4954 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4955 CstHigh =
4956 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4957 if (CstLow && CstHigh) {
4958 if (!IsMax)
4959 std::swap(CstLow, CstHigh);
4960 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4961 Tmp = CstLow->getAPIntValue().getNumSignBits();
4962 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4963 return std::min(Tmp, Tmp2);
4964 }
4965 }
4966
4967 // Fallback - just get the minimum number of sign bits of the operands.
4968 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4969 if (Tmp == 1)
4970 return 1; // Early out.
4971 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4972 return std::min(Tmp, Tmp2);
4973 }
4974 case ISD::UMIN:
4975 case ISD::UMAX:
4976 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4977 if (Tmp == 1)
4978 return 1; // Early out.
4979 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4980 return std::min(Tmp, Tmp2);
4981 case ISD::SSUBO_CARRY:
4982 case ISD::USUBO_CARRY:
4983 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4984 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4985 return VTBits;
4986 [[fallthrough]];
4987 case ISD::SADDO:
4988 case ISD::UADDO:
4989 case ISD::SADDO_CARRY:
4990 case ISD::UADDO_CARRY:
4991 case ISD::SSUBO:
4992 case ISD::USUBO:
4993 case ISD::SMULO:
4994 case ISD::UMULO:
4995 if (Op.getResNo() != 1)
4996 break;
4997 // The boolean result conforms to getBooleanContents. Fall through.
4998 // If setcc returns 0/-1, all bits are sign bits.
4999 // We know that we have an integer-based boolean since these operations
5000 // are only available for integer.
5001 if (TLI->getBooleanContents(VT.isVector(), false) ==
5003 return VTBits;
5004 break;
5005 case ISD::SETCC:
5006 case ISD::SETCCCARRY:
5007 case ISD::STRICT_FSETCC:
5008 case ISD::STRICT_FSETCCS: {
5009 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5010 // If setcc returns 0/-1, all bits are sign bits.
5011 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5013 return VTBits;
5014 break;
5015 }
5016 case ISD::ROTL:
5017 case ISD::ROTR:
5018 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5019
5020 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5021 if (Tmp == VTBits)
5022 return VTBits;
5023
5024 if (ConstantSDNode *C =
5025 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5026 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5027
5028 // Handle rotate right by N like a rotate left by 32-N.
5029 if (Opcode == ISD::ROTR)
5030 RotAmt = (VTBits - RotAmt) % VTBits;
5031
5032 // If we aren't rotating out all of the known-in sign bits, return the
5033 // number that are left. This handles rotl(sext(x), 1) for example.
5034 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5035 }
5036 break;
5037 case ISD::ADD:
5038 case ISD::ADDC:
5039 // Add can have at most one carry bit. Thus we know that the output
5040 // is, at worst, one more bit than the inputs.
5041 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5042 if (Tmp == 1) return 1; // Early out.
5043
5044 // Special case decrementing a value (ADD X, -1):
5045 if (ConstantSDNode *CRHS =
5046 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5047 if (CRHS->isAllOnes()) {
5048 KnownBits Known =
5049 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5050
5051 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5052 // sign bits set.
5053 if ((Known.Zero | 1).isAllOnes())
5054 return VTBits;
5055
5056 // If we are subtracting one from a positive number, there is no carry
5057 // out of the result.
5058 if (Known.isNonNegative())
5059 return Tmp;
5060 }
5061
5062 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5063 if (Tmp2 == 1) return 1; // Early out.
5064 return std::min(Tmp, Tmp2) - 1;
5065 case ISD::SUB:
5066 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5067 if (Tmp2 == 1) return 1; // Early out.
5068
5069 // Handle NEG.
5070 if (ConstantSDNode *CLHS =
5071 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5072 if (CLHS->isZero()) {
5073 KnownBits Known =
5074 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5075 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5076 // sign bits set.
5077 if ((Known.Zero | 1).isAllOnes())
5078 return VTBits;
5079
5080 // If the input is known to be positive (the sign bit is known clear),
5081 // the output of the NEG has the same number of sign bits as the input.
5082 if (Known.isNonNegative())
5083 return Tmp2;
5084
5085 // Otherwise, we treat this like a SUB.
5086 }
5087
5088 // Sub can have at most one carry bit. Thus we know that the output
5089 // is, at worst, one more bit than the inputs.
5090 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5091 if (Tmp == 1) return 1; // Early out.
5092 return std::min(Tmp, Tmp2) - 1;
5093 case ISD::MUL: {
5094 // The output of the Mul can be at most twice the valid bits in the inputs.
5095 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5096 if (SignBitsOp0 == 1)
5097 break;
5098 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5099 if (SignBitsOp1 == 1)
5100 break;
5101 unsigned OutValidBits =
5102 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5103 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5104 }
5105 case ISD::AVGCEILS:
5106 case ISD::AVGFLOORS:
5107 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5108 if (Tmp == 1)
5109 return 1; // Early out.
5110 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5111 return std::min(Tmp, Tmp2);
5112 case ISD::SREM:
5113 // The sign bit is the LHS's sign bit, except when the result of the
5114 // remainder is zero. The magnitude of the result should be less than or
5115 // equal to the magnitude of the LHS. Therefore, the result should have
5116 // at least as many sign bits as the left hand side.
5117 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5118 case ISD::TRUNCATE: {
5119 // Check if the sign bits of source go down as far as the truncated value.
5120 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5121 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5122 if (NumSrcSignBits > (NumSrcBits - VTBits))
5123 return NumSrcSignBits - (NumSrcBits - VTBits);
5124 break;
5125 }
5126 case ISD::EXTRACT_ELEMENT: {
5127 if (VT.isScalableVector())
5128 break;
5129 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5130 const int BitWidth = Op.getValueSizeInBits();
5131 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5132
5133 // Get reverse index (starting from 1), Op1 value indexes elements from
5134 // little end. Sign starts at big end.
5135 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5136
5137 // If the sign portion ends in our element the subtraction gives correct
5138 // result. Otherwise it gives either negative or > bitwidth result
5139 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5140 }
5142 if (VT.isScalableVector())
5143 break;
5144 // If we know the element index, split the demand between the
5145 // source vector and the inserted element, otherwise assume we need
5146 // the original demanded vector elements and the value.
5147 SDValue InVec = Op.getOperand(0);
5148 SDValue InVal = Op.getOperand(1);
5149 SDValue EltNo = Op.getOperand(2);
5150 bool DemandedVal = true;
5151 APInt DemandedVecElts = DemandedElts;
5152 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5153 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5154 unsigned EltIdx = CEltNo->getZExtValue();
5155 DemandedVal = !!DemandedElts[EltIdx];
5156 DemandedVecElts.clearBit(EltIdx);
5157 }
5158 Tmp = std::numeric_limits<unsigned>::max();
5159 if (DemandedVal) {
5160 // TODO - handle implicit truncation of inserted elements.
5161 if (InVal.getScalarValueSizeInBits() != VTBits)
5162 break;
5163 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5164 Tmp = std::min(Tmp, Tmp2);
5165 }
5166 if (!!DemandedVecElts) {
5167 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5168 Tmp = std::min(Tmp, Tmp2);
5169 }
5170 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5171 return Tmp;
5172 }
5174 assert(!VT.isScalableVector());
5175 SDValue InVec = Op.getOperand(0);
5176 SDValue EltNo = Op.getOperand(1);
5177 EVT VecVT = InVec.getValueType();
5178 // ComputeNumSignBits not yet implemented for scalable vectors.
5179 if (VecVT.isScalableVector())
5180 break;
5181 const unsigned BitWidth = Op.getValueSizeInBits();
5182 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5183 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5184
5185 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5186 // anything about sign bits. But if the sizes match we can derive knowledge
5187 // about sign bits from the vector operand.
5188 if (BitWidth != EltBitWidth)
5189 break;
5190
5191 // If we know the element index, just demand that vector element, else for
5192 // an unknown element index, ignore DemandedElts and demand them all.
5193 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5194 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5195 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5196 DemandedSrcElts =
5197 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5198
5199 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5200 }
5202 // Offset the demanded elts by the subvector index.
5203 SDValue Src = Op.getOperand(0);
5204 // Bail until we can represent demanded elements for scalable vectors.
5205 if (Src.getValueType().isScalableVector())
5206 break;
5207 uint64_t Idx = Op.getConstantOperandVal(1);
5208 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5209 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5210 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5211 }
5212 case ISD::CONCAT_VECTORS: {
5213 if (VT.isScalableVector())
5214 break;
5215 // Determine the minimum number of sign bits across all demanded
5216 // elts of the input vectors. Early out if the result is already 1.
5217 Tmp = std::numeric_limits<unsigned>::max();
5218 EVT SubVectorVT = Op.getOperand(0).getValueType();
5219 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5220 unsigned NumSubVectors = Op.getNumOperands();
5221 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5222 APInt DemandedSub =
5223 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5224 if (!DemandedSub)
5225 continue;
5226 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5227 Tmp = std::min(Tmp, Tmp2);
5228 }
5229 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5230 return Tmp;
5231 }
5232 case ISD::INSERT_SUBVECTOR: {
5233 if (VT.isScalableVector())
5234 break;
5235 // Demand any elements from the subvector and the remainder from the src its
5236 // inserted into.
5237 SDValue Src = Op.getOperand(0);
5238 SDValue Sub = Op.getOperand(1);
5239 uint64_t Idx = Op.getConstantOperandVal(2);
5240 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5241 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5242 APInt DemandedSrcElts = DemandedElts;
5243 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5244
5245 Tmp = std::numeric_limits<unsigned>::max();
5246 if (!!DemandedSubElts) {
5247 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5248 if (Tmp == 1)
5249 return 1; // early-out
5250 }
5251 if (!!DemandedSrcElts) {
5252 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5253 Tmp = std::min(Tmp, Tmp2);
5254 }
5255 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5256 return Tmp;
5257 }
5258 case ISD::LOAD: {
5260 if (const MDNode *Ranges = LD->getRanges()) {
5261 if (DemandedElts != 1)
5262 break;
5263
5265 if (VTBits > CR.getBitWidth()) {
5266 switch (LD->getExtensionType()) {
5267 case ISD::SEXTLOAD:
5268 CR = CR.signExtend(VTBits);
5269 break;
5270 case ISD::ZEXTLOAD:
5271 CR = CR.zeroExtend(VTBits);
5272 break;
5273 default:
5274 break;
5275 }
5276 }
5277
5278 if (VTBits != CR.getBitWidth())
5279 break;
5280 return std::min(CR.getSignedMin().getNumSignBits(),
5282 }
5283
5284 break;
5285 }
5286 case ISD::ATOMIC_CMP_SWAP:
5287 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
5288 case ISD::ATOMIC_SWAP:
5289 case ISD::ATOMIC_LOAD_ADD:
5290 case ISD::ATOMIC_LOAD_SUB:
5291 case ISD::ATOMIC_LOAD_AND:
5292 case ISD::ATOMIC_LOAD_CLR:
5293 case ISD::ATOMIC_LOAD_OR:
5294 case ISD::ATOMIC_LOAD_XOR:
5295 case ISD::ATOMIC_LOAD_NAND:
5296 case ISD::ATOMIC_LOAD_MIN:
5297 case ISD::ATOMIC_LOAD_MAX:
5298 case ISD::ATOMIC_LOAD_UMIN:
5299 case ISD::ATOMIC_LOAD_UMAX:
5300 case ISD::ATOMIC_LOAD: {
5301 auto *AT = cast<AtomicSDNode>(Op);
5302 // If we are looking at the loaded value.
5303 if (Op.getResNo() == 0) {
5304 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5305 if (Tmp == VTBits)
5306 return 1; // early-out
5307
5308 // For atomic_load, prefer to use the extension type.
5309 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5310 switch (AT->getExtensionType()) {
5311 default:
5312 break;
5313 case ISD::SEXTLOAD:
5314 return VTBits - Tmp + 1;
5315 case ISD::ZEXTLOAD:
5316 return VTBits - Tmp;
5317 }
5318 }
5319
5320 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5321 return VTBits - Tmp + 1;
5322 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5323 return VTBits - Tmp;
5324 }
5325 break;
5326 }
5327 }
5328
5329 // If we are looking at the loaded value of the SDNode.
5330 if (Op.getResNo() == 0) {
5331 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5332 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5333 unsigned ExtType = LD->getExtensionType();
5334 switch (ExtType) {
5335 default: break;
5336 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5337 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5338 return VTBits - Tmp + 1;
5339 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5340 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5341 return VTBits - Tmp;
5342 case ISD::NON_EXTLOAD:
5343 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5344 // We only need to handle vectors - computeKnownBits should handle
5345 // scalar cases.
5346 Type *CstTy = Cst->getType();
5347 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5348 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5349 VTBits == CstTy->getScalarSizeInBits()) {
5350 Tmp = VTBits;
5351 for (unsigned i = 0; i != NumElts; ++i) {
5352 if (!DemandedElts[i])
5353 continue;
5354 if (Constant *Elt = Cst->getAggregateElement(i)) {
5355 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5356 const APInt &Value = CInt->getValue();
5357 Tmp = std::min(Tmp, Value.getNumSignBits());
5358 continue;
5359 }
5360 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5361 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5362 Tmp = std::min(Tmp, Value.getNumSignBits());
5363 continue;
5364 }
5365 }
5366 // Unknown type. Conservatively assume no bits match sign bit.
5367 return 1;
5368 }
5369 return Tmp;
5370 }
5371 }
5372 break;
5373 }
5374 }
5375 }
5376
5377 // Allow the target to implement this method for its nodes.
5378 if (Opcode >= ISD::BUILTIN_OP_END ||
5379 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5380 Opcode == ISD::INTRINSIC_W_CHAIN ||
5381 Opcode == ISD::INTRINSIC_VOID) {
5382 // TODO: This can probably be removed once target code is audited. This
5383 // is here purely to reduce patch size and review complexity.
5384 if (!VT.isScalableVector()) {
5385 unsigned NumBits =
5386 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5387 if (NumBits > 1)
5388 FirstAnswer = std::max(FirstAnswer, NumBits);
5389 }
5390 }
5391
5392 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5393 // use this information.
5394 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5395 return std::max(FirstAnswer, Known.countMinSignBits());
5396}
5397
5399 unsigned Depth) const {
5400 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5401 return Op.getScalarValueSizeInBits() - SignBits + 1;
5402}
5403
5405 const APInt &DemandedElts,
5406 unsigned Depth) const {
5407 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5408 return Op.getScalarValueSizeInBits() - SignBits + 1;
5409}
5410
5412 unsigned Depth) const {
5413 // Early out for FREEZE.
5414 if (Op.getOpcode() == ISD::FREEZE)
5415 return true;
5416
5417 EVT VT = Op.getValueType();
5418 APInt DemandedElts = VT.isFixedLengthVector()
5420 : APInt(1, 1);
5421 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5422}
5423
5425 const APInt &DemandedElts,
5426 bool PoisonOnly,
5427 unsigned Depth) const {
5428 unsigned Opcode = Op.getOpcode();
5429
5430 // Early out for FREEZE.
5431 if (Opcode == ISD::FREEZE)
5432 return true;
5433
5434 if (Depth >= MaxRecursionDepth)
5435 return false; // Limit search depth.
5436
5437 if (isIntOrFPConstant(Op))
5438 return true;
5439
5440 switch (Opcode) {
5441 case ISD::CONDCODE:
5442 case ISD::VALUETYPE:
5443 case ISD::FrameIndex:
5445 case ISD::CopyFromReg:
5446 return true;
5447
5448 case ISD::POISON:
5449 return false;
5450
5451 case ISD::UNDEF:
5452 return PoisonOnly;
5453
5454 case ISD::BUILD_VECTOR:
5455 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5456 // this shouldn't affect the result.
5457 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5458 if (!DemandedElts[i])
5459 continue;
5461 Depth + 1))
5462 return false;
5463 }
5464 return true;
5465
5467 SDValue Src = Op.getOperand(0);
5468 if (Src.getValueType().isScalableVector())
5469 break;
5470 uint64_t Idx = Op.getConstantOperandVal(1);
5471 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5472 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5473 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5474 Depth + 1);
5475 }
5476
5477 case ISD::INSERT_SUBVECTOR: {
5478 if (Op.getValueType().isScalableVector())
5479 break;
5480 SDValue Src = Op.getOperand(0);
5481 SDValue Sub = Op.getOperand(1);
5482 uint64_t Idx = Op.getConstantOperandVal(2);
5483 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5484 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5485 APInt DemandedSrcElts = DemandedElts;
5486 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5487
5488 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5489 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5490 return false;
5491 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5492 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5493 return false;
5494 return true;
5495 }
5496
5498 SDValue Src = Op.getOperand(0);
5499 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5500 EVT SrcVT = Src.getValueType();
5501 if (SrcVT.isFixedLengthVector() && IndexC &&
5502 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5503 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5504 IndexC->getZExtValue());
5505 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5506 Depth + 1);
5507 }
5508 break;
5509 }
5510
5512 SDValue InVec = Op.getOperand(0);
5513 SDValue InVal = Op.getOperand(1);
5514 SDValue EltNo = Op.getOperand(2);
5515 EVT VT = InVec.getValueType();
5516 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5517 if (IndexC && VT.isFixedLengthVector() &&
5518 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5519 if (DemandedElts[IndexC->getZExtValue()] &&
5521 return false;
5522 APInt InVecDemandedElts = DemandedElts;
5523 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5524 if (!!InVecDemandedElts &&
5526 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5527 InVecDemandedElts, PoisonOnly, Depth + 1))
5528 return false;
5529 return true;
5530 }
5531 break;
5532 }
5533
5535 // Check upper (known undef) elements.
5536 if (DemandedElts.ugt(1) && !PoisonOnly)
5537 return false;
5538 // Check element zero.
5539 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5540 Op.getOperand(0), PoisonOnly, Depth + 1))
5541 return false;
5542 return true;
5543
5544 case ISD::SPLAT_VECTOR:
5545 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5546 Depth + 1);
5547
5548 case ISD::VECTOR_SHUFFLE: {
5549 APInt DemandedLHS, DemandedRHS;
5550 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5551 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5552 DemandedElts, DemandedLHS, DemandedRHS,
5553 /*AllowUndefElts=*/false))
5554 return false;
5555 if (!DemandedLHS.isZero() &&
5556 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5557 PoisonOnly, Depth + 1))
5558 return false;
5559 if (!DemandedRHS.isZero() &&
5560 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5561 PoisonOnly, Depth + 1))
5562 return false;
5563 return true;
5564 }
5565
5566 case ISD::SHL:
5567 case ISD::SRL:
5568 case ISD::SRA:
5569 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5570 // enough to check operand 0 if Op can't create undef/poison.
5571 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5572 /*ConsiderFlags*/ true, Depth) &&
5573 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5574 PoisonOnly, Depth + 1);
5575
5576 case ISD::BSWAP:
5577 case ISD::CTPOP:
5578 case ISD::BITREVERSE:
5579 case ISD::AND:
5580 case ISD::OR:
5581 case ISD::XOR:
5582 case ISD::ADD:
5583 case ISD::SUB:
5584 case ISD::MUL:
5585 case ISD::SADDSAT:
5586 case ISD::UADDSAT:
5587 case ISD::SSUBSAT:
5588 case ISD::USUBSAT:
5589 case ISD::SSHLSAT:
5590 case ISD::USHLSAT:
5591 case ISD::SMIN:
5592 case ISD::SMAX:
5593 case ISD::UMIN:
5594 case ISD::UMAX:
5595 case ISD::ZERO_EXTEND:
5596 case ISD::SIGN_EXTEND:
5597 case ISD::ANY_EXTEND:
5598 case ISD::TRUNCATE:
5599 case ISD::VSELECT: {
5600 // If Op can't create undef/poison and none of its operands are undef/poison
5601 // then Op is never undef/poison. A difference from the more common check
5602 // below, outside the switch, is that we handle elementwise operations for
5603 // which the DemandedElts mask is valid for all operands here.
5604 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5605 /*ConsiderFlags*/ true, Depth) &&
5606 all_of(Op->ops(), [&](SDValue V) {
5607 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5608 PoisonOnly, Depth + 1);
5609 });
5610 }
5611
5612 // TODO: Search for noundef attributes from library functions.
5613
5614 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5615
5616 default:
5617 // Allow the target to implement this method for its nodes.
5618 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5619 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5620 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5621 Op, DemandedElts, *this, PoisonOnly, Depth);
5622 break;
5623 }
5624
5625 // If Op can't create undef/poison and none of its operands are undef/poison
5626 // then Op is never undef/poison.
5627 // NOTE: TargetNodes can handle this in themselves in
5628 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5629 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5630 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5631 Depth) &&
5632 all_of(Op->ops(), [&](SDValue V) {
5633 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5634 });
5635}
5636
5638 bool ConsiderFlags,
5639 unsigned Depth) const {
5640 EVT VT = Op.getValueType();
5641 APInt DemandedElts = VT.isFixedLengthVector()
5643 : APInt(1, 1);
5644 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5645 Depth);
5646}
5647
5649 bool PoisonOnly, bool ConsiderFlags,
5650 unsigned Depth) const {
5651 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5652 return true;
5653
5654 unsigned Opcode = Op.getOpcode();
5655 switch (Opcode) {
5656 case ISD::AssertSext:
5657 case ISD::AssertZext:
5658 case ISD::AssertAlign:
5660 // Assertion nodes can create poison if the assertion fails.
5661 return true;
5662
5663 case ISD::FREEZE:
5667 case ISD::SADDSAT:
5668 case ISD::UADDSAT:
5669 case ISD::SSUBSAT:
5670 case ISD::USUBSAT:
5671 case ISD::MULHU:
5672 case ISD::MULHS:
5673 case ISD::AVGFLOORS:
5674 case ISD::AVGFLOORU:
5675 case ISD::AVGCEILS:
5676 case ISD::AVGCEILU:
5677 case ISD::ABDU:
5678 case ISD::ABDS:
5679 case ISD::SMIN:
5680 case ISD::SMAX:
5681 case ISD::SCMP:
5682 case ISD::UMIN:
5683 case ISD::UMAX:
5684 case ISD::UCMP:
5685 case ISD::AND:
5686 case ISD::XOR:
5687 case ISD::ROTL:
5688 case ISD::ROTR:
5689 case ISD::FSHL:
5690 case ISD::FSHR:
5691 case ISD::BSWAP:
5692 case ISD::CTTZ:
5693 case ISD::CTLZ:
5694 case ISD::CTPOP:
5695 case ISD::BITREVERSE:
5696 case ISD::PARITY:
5697 case ISD::SIGN_EXTEND:
5698 case ISD::TRUNCATE:
5702 case ISD::BITCAST:
5703 case ISD::BUILD_VECTOR:
5704 case ISD::BUILD_PAIR:
5705 case ISD::SPLAT_VECTOR:
5706 case ISD::FABS:
5707 return false;
5708
5709 case ISD::ABS:
5710 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5711 // Different to Intrinsic::abs.
5712 return false;
5713
5714 case ISD::ADDC:
5715 case ISD::SUBC:
5716 case ISD::ADDE:
5717 case ISD::SUBE:
5718 case ISD::SADDO:
5719 case ISD::SSUBO:
5720 case ISD::SMULO:
5721 case ISD::SADDO_CARRY:
5722 case ISD::SSUBO_CARRY:
5723 case ISD::UADDO:
5724 case ISD::USUBO:
5725 case ISD::UMULO:
5726 case ISD::UADDO_CARRY:
5727 case ISD::USUBO_CARRY:
5728 // No poison on result or overflow flags.
5729 return false;
5730
5731 case ISD::SELECT_CC:
5732 case ISD::SETCC: {
5733 // Integer setcc cannot create undef or poison.
5734 if (Op.getOperand(0).getValueType().isInteger())
5735 return false;
5736
5737 // FP compares are more complicated. They can create poison for nan/infinity
5738 // based on options and flags. The options and flags also cause special
5739 // nonan condition codes to be used. Those condition codes may be preserved
5740 // even if the nonan flag is dropped somewhere.
5741 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5742 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5743 if (((unsigned)CCCode & 0x10U))
5744 return true;
5745
5747 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5748 }
5749
5750 case ISD::OR:
5751 case ISD::ZERO_EXTEND:
5752 case ISD::SELECT:
5753 case ISD::VSELECT:
5754 case ISD::ADD:
5755 case ISD::SUB:
5756 case ISD::MUL:
5757 case ISD::FNEG:
5758 case ISD::FADD:
5759 case ISD::FSUB:
5760 case ISD::FMUL:
5761 case ISD::FDIV:
5762 case ISD::FREM:
5763 case ISD::FCOPYSIGN:
5764 case ISD::FMA:
5765 case ISD::FMAD:
5766 case ISD::FP_EXTEND:
5769 // No poison except from flags (which is handled above)
5770 return false;
5771
5772 case ISD::SHL:
5773 case ISD::SRL:
5774 case ISD::SRA:
5775 // If the max shift amount isn't in range, then the shift can
5776 // create poison.
5777 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5778
5781 // If the amount is zero then the result will be poison.
5782 // TODO: Add isKnownNeverZero DemandedElts handling.
5783 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5784
5786 // Check if we demand any upper (undef) elements.
5787 return !PoisonOnly && DemandedElts.ugt(1);
5788
5791 // Ensure that the element index is in bounds.
5792 EVT VecVT = Op.getOperand(0).getValueType();
5793 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5794 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5795 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5796 }
5797
5798 case ISD::VECTOR_SHUFFLE: {
5799 // Check for any demanded shuffle element that is undef.
5800 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5801 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5802 if (Elt < 0 && DemandedElts[Idx])
5803 return true;
5804 return false;
5805 }
5806
5807 default:
5808 // Allow the target to implement this method for its nodes.
5809 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5810 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5811 return TLI->canCreateUndefOrPoisonForTargetNode(
5812 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5813 break;
5814 }
5815
5816 // Be conservative and return true.
5817 return true;
5818}
5819
5820bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5821 unsigned Opcode = Op.getOpcode();
5822 if (Opcode == ISD::OR)
5823 return Op->getFlags().hasDisjoint() ||
5824 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5825 if (Opcode == ISD::XOR)
5826 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5827 return false;
5828}
5829
5831 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5832 (Op.isAnyAdd() || isADDLike(Op));
5833}
5834
5836 unsigned Depth) const {
5837 EVT VT = Op.getValueType();
5838
5839 // Since the number of lanes in a scalable vector is unknown at compile time,
5840 // we track one bit which is implicitly broadcast to all lanes. This means
5841 // that all lanes in a scalable vector are considered demanded.
5842 APInt DemandedElts = VT.isFixedLengthVector()
5844 : APInt(1, 1);
5845
5846 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5847}
5848
5850 bool SNaN, unsigned Depth) const {
5851 assert(!DemandedElts.isZero() && "No demanded elements");
5852
5853 // If we're told that NaNs won't happen, assume they won't.
5854 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5855 return true;
5856
5857 if (Depth >= MaxRecursionDepth)
5858 return false; // Limit search depth.
5859
5860 // If the value is a constant, we can obviously see if it is a NaN or not.
5862 return !C->getValueAPF().isNaN() ||
5863 (SNaN && !C->getValueAPF().isSignaling());
5864 }
5865
5866 unsigned Opcode = Op.getOpcode();
5867 switch (Opcode) {
5868 case ISD::FADD:
5869 case ISD::FSUB:
5870 case ISD::FMUL:
5871 case ISD::FDIV:
5872 case ISD::FREM:
5873 case ISD::FSIN:
5874 case ISD::FCOS:
5875 case ISD::FTAN:
5876 case ISD::FASIN:
5877 case ISD::FACOS:
5878 case ISD::FATAN:
5879 case ISD::FATAN2:
5880 case ISD::FSINH:
5881 case ISD::FCOSH:
5882 case ISD::FTANH:
5883 case ISD::FMA:
5884 case ISD::FMAD: {
5885 if (SNaN)
5886 return true;
5887 // TODO: Need isKnownNeverInfinity
5888 return false;
5889 }
5890 case ISD::FCANONICALIZE:
5891 case ISD::FEXP:
5892 case ISD::FEXP2:
5893 case ISD::FEXP10:
5894 case ISD::FTRUNC:
5895 case ISD::FFLOOR:
5896 case ISD::FCEIL:
5897 case ISD::FROUND:
5898 case ISD::FROUNDEVEN:
5899 case ISD::LROUND:
5900 case ISD::LLROUND:
5901 case ISD::FRINT:
5902 case ISD::LRINT:
5903 case ISD::LLRINT:
5904 case ISD::FNEARBYINT:
5905 case ISD::FLDEXP: {
5906 if (SNaN)
5907 return true;
5908 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5909 }
5910 case ISD::FABS:
5911 case ISD::FNEG:
5912 case ISD::FCOPYSIGN: {
5913 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5914 }
5915 case ISD::SELECT:
5916 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5917 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5918 case ISD::FP_EXTEND:
5919 case ISD::FP_ROUND: {
5920 if (SNaN)
5921 return true;
5922 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5923 }
5924 case ISD::SINT_TO_FP:
5925 case ISD::UINT_TO_FP:
5926 return true;
5927 case ISD::FSQRT: // Need is known positive
5928 case ISD::FLOG:
5929 case ISD::FLOG2:
5930 case ISD::FLOG10:
5931 case ISD::FPOWI:
5932 case ISD::FPOW: {
5933 if (SNaN)
5934 return true;
5935 // TODO: Refine on operand
5936 return false;
5937 }
5938 case ISD::FMINNUM:
5939 case ISD::FMAXNUM:
5940 case ISD::FMINIMUMNUM:
5941 case ISD::FMAXIMUMNUM: {
5942 // Only one needs to be known not-nan, since it will be returned if the
5943 // other ends up being one.
5944 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5945 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5946 }
5947 case ISD::FMINNUM_IEEE:
5948 case ISD::FMAXNUM_IEEE: {
5949 if (SNaN)
5950 return true;
5951 // This can return a NaN if either operand is an sNaN, or if both operands
5952 // are NaN.
5953 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5954 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5955 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5956 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5957 }
5958 case ISD::FMINIMUM:
5959 case ISD::FMAXIMUM: {
5960 // TODO: Does this quiet or return the origina NaN as-is?
5961 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5962 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5963 }
5965 SDValue Src = Op.getOperand(0);
5966 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5967 EVT SrcVT = Src.getValueType();
5968 if (SrcVT.isFixedLengthVector() && Idx &&
5969 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5970 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5971 Idx->getZExtValue());
5972 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5973 }
5974 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5975 }
5977 SDValue Src = Op.getOperand(0);
5978 if (Src.getValueType().isFixedLengthVector()) {
5979 unsigned Idx = Op.getConstantOperandVal(1);
5980 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5981 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5982 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5983 }
5984 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5985 }
5986 case ISD::INSERT_SUBVECTOR: {
5987 SDValue BaseVector = Op.getOperand(0);
5988 SDValue SubVector = Op.getOperand(1);
5989 EVT BaseVectorVT = BaseVector.getValueType();
5990 if (BaseVectorVT.isFixedLengthVector()) {
5991 unsigned Idx = Op.getConstantOperandVal(2);
5992 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5993 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5994
5995 // Clear/Extract the bits at the position where the subvector will be
5996 // inserted.
5997 APInt DemandedMask =
5998 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5999 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6000 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6001
6002 bool NeverNaN = true;
6003 if (!DemandedSrcElts.isZero())
6004 NeverNaN &=
6005 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6006 if (NeverNaN && !DemandedSubElts.isZero())
6007 NeverNaN &=
6008 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6009 return NeverNaN;
6010 }
6011 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6012 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6013 }
6014 case ISD::BUILD_VECTOR: {
6015 unsigned NumElts = Op.getNumOperands();
6016 for (unsigned I = 0; I != NumElts; ++I)
6017 if (DemandedElts[I] &&
6018 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6019 return false;
6020 return true;
6021 }
6022 case ISD::AssertNoFPClass: {
6023 FPClassTest NoFPClass =
6024 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6025 if ((NoFPClass & fcNan) == fcNan)
6026 return true;
6027 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6028 return true;
6029 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6030 }
6031 default:
6032 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6033 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6034 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6035 Depth);
6036 }
6037
6038 return false;
6039 }
6040}
6041
6043 assert(Op.getValueType().isFloatingPoint() &&
6044 "Floating point type expected");
6045
6046 // If the value is a constant, we can obviously see if it is a zero or not.
6048 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6049}
6050
6052 if (Depth >= MaxRecursionDepth)
6053 return false; // Limit search depth.
6054
6055 assert(!Op.getValueType().isFloatingPoint() &&
6056 "Floating point types unsupported - use isKnownNeverZeroFloat");
6057
6058 // If the value is a constant, we can obviously see if it is a zero or not.
6060 [](ConstantSDNode *C) { return !C->isZero(); }))
6061 return true;
6062
6063 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6064 // some degree.
6065 switch (Op.getOpcode()) {
6066 default:
6067 break;
6068
6069 case ISD::OR:
6070 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6071 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6072
6073 case ISD::VSELECT:
6074 case ISD::SELECT:
6075 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6076 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6077
6078 case ISD::SHL: {
6079 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6080 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6081 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6082 // 1 << X is never zero.
6083 if (ValKnown.One[0])
6084 return true;
6085 // If max shift cnt of known ones is non-zero, result is non-zero.
6086 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6087 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6088 !ValKnown.One.shl(MaxCnt).isZero())
6089 return true;
6090 break;
6091 }
6092 case ISD::UADDSAT:
6093 case ISD::UMAX:
6094 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6095 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6096
6097 // For smin/smax: If either operand is known negative/positive
6098 // respectively we don't need the other to be known at all.
6099 case ISD::SMAX: {
6100 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6101 if (Op1.isStrictlyPositive())
6102 return true;
6103
6104 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6105 if (Op0.isStrictlyPositive())
6106 return true;
6107
6108 if (Op1.isNonZero() && Op0.isNonZero())
6109 return true;
6110
6111 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6112 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6113 }
6114 case ISD::SMIN: {
6115 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6116 if (Op1.isNegative())
6117 return true;
6118
6119 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6120 if (Op0.isNegative())
6121 return true;
6122
6123 if (Op1.isNonZero() && Op0.isNonZero())
6124 return true;
6125
6126 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6127 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6128 }
6129 case ISD::UMIN:
6130 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6131 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6132
6133 case ISD::ROTL:
6134 case ISD::ROTR:
6135 case ISD::BITREVERSE:
6136 case ISD::BSWAP:
6137 case ISD::CTPOP:
6138 case ISD::ABS:
6139 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6140
6141 case ISD::SRA:
6142 case ISD::SRL: {
6143 if (Op->getFlags().hasExact())
6144 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6145 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6146 if (ValKnown.isNegative())
6147 return true;
6148 // If max shift cnt of known ones is non-zero, result is non-zero.
6149 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6150 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6151 !ValKnown.One.lshr(MaxCnt).isZero())
6152 return true;
6153 break;
6154 }
6155 case ISD::UDIV:
6156 case ISD::SDIV:
6157 // div exact can only produce a zero if the dividend is zero.
6158 // TODO: For udiv this is also true if Op1 u<= Op0
6159 if (Op->getFlags().hasExact())
6160 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6161 break;
6162
6163 case ISD::ADD:
6164 if (Op->getFlags().hasNoUnsignedWrap())
6165 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6166 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6167 return true;
6168 // TODO: There are a lot more cases we can prove for add.
6169 break;
6170
6171 case ISD::SUB: {
6172 if (isNullConstant(Op.getOperand(0)))
6173 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6174
6175 std::optional<bool> ne =
6176 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6177 computeKnownBits(Op.getOperand(1), Depth + 1));
6178 return ne && *ne;
6179 }
6180
6181 case ISD::MUL:
6182 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6183 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6184 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6185 return true;
6186 break;
6187
6188 case ISD::ZERO_EXTEND:
6189 case ISD::SIGN_EXTEND:
6190 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6191 case ISD::VSCALE: {
6193 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6194 ConstantRange CR =
6195 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6196 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6197 return true;
6198 break;
6199 }
6200 }
6201
6203}
6204
6206 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6207 return !C1->isNegative();
6208
6209 return Op.getOpcode() == ISD::FABS;
6210}
6211
6213 // Check the obvious case.
6214 if (A == B) return true;
6215
6216 // For negative and positive zero.
6219 if (CA->isZero() && CB->isZero()) return true;
6220
6221 // Otherwise they may not be equal.
6222 return false;
6223}
6224
6225// Only bits set in Mask must be negated, other bits may be arbitrary.
6227 if (isBitwiseNot(V, AllowUndefs))
6228 return V.getOperand(0);
6229
6230 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6231 // bits in the non-extended part.
6232 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6233 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6234 return SDValue();
6235 SDValue ExtArg = V.getOperand(0);
6236 if (ExtArg.getScalarValueSizeInBits() >=
6237 MaskC->getAPIntValue().getActiveBits() &&
6238 isBitwiseNot(ExtArg, AllowUndefs) &&
6239 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6240 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6241 return ExtArg.getOperand(0).getOperand(0);
6242 return SDValue();
6243}
6244
6246 // Match masked merge pattern (X & ~M) op (Y & M)
6247 // Including degenerate case (X & ~M) op M
6248 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6249 SDValue Other) {
6250 if (SDValue NotOperand =
6251 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6252 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6253 NotOperand->getOpcode() == ISD::TRUNCATE)
6254 NotOperand = NotOperand->getOperand(0);
6255
6256 if (Other == NotOperand)
6257 return true;
6258 if (Other->getOpcode() == ISD::AND)
6259 return NotOperand == Other->getOperand(0) ||
6260 NotOperand == Other->getOperand(1);
6261 }
6262 return false;
6263 };
6264
6265 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6266 A = A->getOperand(0);
6267
6268 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6269 B = B->getOperand(0);
6270
6271 if (A->getOpcode() == ISD::AND)
6272 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6273 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6274 return false;
6275}
6276
6277// FIXME: unify with llvm::haveNoCommonBitsSet.
6279 assert(A.getValueType() == B.getValueType() &&
6280 "Values must have the same type");
6283 return true;
6286}
6287
6288static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6289 SelectionDAG &DAG) {
6290 if (cast<ConstantSDNode>(Step)->isZero())
6291 return DAG.getConstant(0, DL, VT);
6292
6293 return SDValue();
6294}
6295
6298 SelectionDAG &DAG) {
6299 int NumOps = Ops.size();
6300 assert(NumOps != 0 && "Can't build an empty vector!");
6301 assert(!VT.isScalableVector() &&
6302 "BUILD_VECTOR cannot be used with scalable types");
6303 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6304 "Incorrect element count in BUILD_VECTOR!");
6305
6306 // BUILD_VECTOR of UNDEFs is UNDEF.
6307 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6308 return DAG.getUNDEF(VT);
6309
6310 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6311 SDValue IdentitySrc;
6312 bool IsIdentity = true;
6313 for (int i = 0; i != NumOps; ++i) {
6315 Ops[i].getOperand(0).getValueType() != VT ||
6316 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6317 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6318 Ops[i].getConstantOperandAPInt(1) != i) {
6319 IsIdentity = false;
6320 break;
6321 }
6322 IdentitySrc = Ops[i].getOperand(0);
6323 }
6324 if (IsIdentity)
6325 return IdentitySrc;
6326
6327 return SDValue();
6328}
6329
6330/// Try to simplify vector concatenation to an input value, undef, or build
6331/// vector.
6334 SelectionDAG &DAG) {
6335 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6337 [Ops](SDValue Op) {
6338 return Ops[0].getValueType() == Op.getValueType();
6339 }) &&
6340 "Concatenation of vectors with inconsistent value types!");
6341 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6342 VT.getVectorElementCount() &&
6343 "Incorrect element count in vector concatenation!");
6344
6345 if (Ops.size() == 1)
6346 return Ops[0];
6347
6348 // Concat of UNDEFs is UNDEF.
6349 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6350 return DAG.getUNDEF(VT);
6351
6352 // Scan the operands and look for extract operations from a single source
6353 // that correspond to insertion at the same location via this concatenation:
6354 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6355 SDValue IdentitySrc;
6356 bool IsIdentity = true;
6357 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6358 SDValue Op = Ops[i];
6359 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6360 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6361 Op.getOperand(0).getValueType() != VT ||
6362 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6363 Op.getConstantOperandVal(1) != IdentityIndex) {
6364 IsIdentity = false;
6365 break;
6366 }
6367 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6368 "Unexpected identity source vector for concat of extracts");
6369 IdentitySrc = Op.getOperand(0);
6370 }
6371 if (IsIdentity) {
6372 assert(IdentitySrc && "Failed to set source vector of extracts");
6373 return IdentitySrc;
6374 }
6375
6376 // The code below this point is only designed to work for fixed width
6377 // vectors, so we bail out for now.
6378 if (VT.isScalableVector())
6379 return SDValue();
6380
6381 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
6382 // simplified to one big BUILD_VECTOR.
6383 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6384 EVT SVT = VT.getScalarType();
6386 for (SDValue Op : Ops) {
6387 EVT OpVT = Op.getValueType();
6388 if (Op.isUndef())
6389 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6390 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6391 Elts.append(Op->op_begin(), Op->op_end());
6392 else
6393 return SDValue();
6394 }
6395
6396 // BUILD_VECTOR requires all inputs to be of the same type, find the
6397 // maximum type and extend them all.
6398 for (SDValue Op : Elts)
6399 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6400
6401 if (SVT.bitsGT(VT.getScalarType())) {
6402 for (SDValue &Op : Elts) {
6403 if (Op.isUndef())
6404 Op = DAG.getUNDEF(SVT);
6405 else
6406 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6407 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6408 : DAG.getSExtOrTrunc(Op, DL, SVT);
6409 }
6410 }
6411
6412 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6413 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6414 return V;
6415}
6416
6417/// Gets or creates the specified node.
6418SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6419 SDVTList VTs = getVTList(VT);
6421 AddNodeIDNode(ID, Opcode, VTs, {});
6422 void *IP = nullptr;
6423 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6424 return SDValue(E, 0);
6425
6426 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6427 CSEMap.InsertNode(N, IP);
6428
6429 InsertNode(N);
6430 SDValue V = SDValue(N, 0);
6431 NewSDValueDbgMsg(V, "Creating new node: ", this);
6432 return V;
6433}
6434
6435SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6436 SDValue N1) {
6437 SDNodeFlags Flags;
6438 if (Inserter)
6439 Flags = Inserter->getFlags();
6440 return getNode(Opcode, DL, VT, N1, Flags);
6441}
6442
6443SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6444 SDValue N1, const SDNodeFlags Flags) {
6445 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6446
6447 // Constant fold unary operations with a vector integer or float operand.
6448 switch (Opcode) {
6449 default:
6450 // FIXME: Entirely reasonable to perform folding of other unary
6451 // operations here as the need arises.
6452 break;
6453 case ISD::FNEG:
6454 case ISD::FABS:
6455 case ISD::FCEIL:
6456 case ISD::FTRUNC:
6457 case ISD::FFLOOR:
6458 case ISD::FP_EXTEND:
6459 case ISD::FP_TO_SINT:
6460 case ISD::FP_TO_UINT:
6461 case ISD::FP_TO_FP16:
6462 case ISD::FP_TO_BF16:
6463 case ISD::TRUNCATE:
6464 case ISD::ANY_EXTEND:
6465 case ISD::ZERO_EXTEND:
6466 case ISD::SIGN_EXTEND:
6467 case ISD::UINT_TO_FP:
6468 case ISD::SINT_TO_FP:
6469 case ISD::FP16_TO_FP:
6470 case ISD::BF16_TO_FP:
6471 case ISD::BITCAST:
6472 case ISD::ABS:
6473 case ISD::BITREVERSE:
6474 case ISD::BSWAP:
6475 case ISD::CTLZ:
6477 case ISD::CTTZ:
6479 case ISD::CTPOP:
6480 case ISD::STEP_VECTOR: {
6481 SDValue Ops = {N1};
6482 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6483 return Fold;
6484 }
6485 }
6486
6487 unsigned OpOpcode = N1.getNode()->getOpcode();
6488 switch (Opcode) {
6489 case ISD::STEP_VECTOR:
6490 assert(VT.isScalableVector() &&
6491 "STEP_VECTOR can only be used with scalable types");
6492 assert(OpOpcode == ISD::TargetConstant &&
6493 VT.getVectorElementType() == N1.getValueType() &&
6494 "Unexpected step operand");
6495 break;
6496 case ISD::FREEZE:
6497 assert(VT == N1.getValueType() && "Unexpected VT!");
6498 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6499 return N1;
6500 break;
6501 case ISD::TokenFactor:
6502 case ISD::MERGE_VALUES:
6504 return N1; // Factor, merge or concat of one node? No need.
6505 case ISD::BUILD_VECTOR: {
6506 // Attempt to simplify BUILD_VECTOR.
6507 SDValue Ops[] = {N1};
6508 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6509 return V;
6510 break;
6511 }
6512 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6513 case ISD::FP_EXTEND:
6515 "Invalid FP cast!");
6516 if (N1.getValueType() == VT) return N1; // noop conversion.
6517 assert((!VT.isVector() || VT.getVectorElementCount() ==
6519 "Vector element count mismatch!");
6520 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6521 if (N1.isUndef())
6522 return getUNDEF(VT);
6523 break;
6524 case ISD::FP_TO_SINT:
6525 case ISD::FP_TO_UINT:
6526 if (N1.isUndef())
6527 return getUNDEF(VT);
6528 break;
6529 case ISD::SINT_TO_FP:
6530 case ISD::UINT_TO_FP:
6531 // [us]itofp(undef) = 0, because the result value is bounded.
6532 if (N1.isUndef())
6533 return getConstantFP(0.0, DL, VT);
6534 break;
6535 case ISD::SIGN_EXTEND:
6536 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6537 "Invalid SIGN_EXTEND!");
6538 assert(VT.isVector() == N1.getValueType().isVector() &&
6539 "SIGN_EXTEND result type type should be vector iff the operand "
6540 "type is vector!");
6541 if (N1.getValueType() == VT) return N1; // noop extension
6542 assert((!VT.isVector() || VT.getVectorElementCount() ==
6544 "Vector element count mismatch!");
6545 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6546 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6547 SDNodeFlags Flags;
6548 if (OpOpcode == ISD::ZERO_EXTEND)
6549 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6550 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6551 transferDbgValues(N1, NewVal);
6552 return NewVal;
6553 }
6554
6555 if (OpOpcode == ISD::POISON)
6556 return getPOISON(VT);
6557
6558 if (N1.isUndef())
6559 // sext(undef) = 0, because the top bits will all be the same.
6560 return getConstant(0, DL, VT);
6561
6562 // Skip unnecessary sext_inreg pattern:
6563 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6564 if (OpOpcode == ISD::TRUNCATE) {
6565 SDValue OpOp = N1.getOperand(0);
6566 if (OpOp.getValueType() == VT) {
6567 unsigned NumSignExtBits =
6569 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6570 transferDbgValues(N1, OpOp);
6571 return OpOp;
6572 }
6573 }
6574 }
6575 break;
6576 case ISD::ZERO_EXTEND:
6577 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6578 "Invalid ZERO_EXTEND!");
6579 assert(VT.isVector() == N1.getValueType().isVector() &&
6580 "ZERO_EXTEND result type type should be vector iff the operand "
6581 "type is vector!");
6582 if (N1.getValueType() == VT) return N1; // noop extension
6583 assert((!VT.isVector() || VT.getVectorElementCount() ==
6585 "Vector element count mismatch!");
6586 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6587 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6588 SDNodeFlags Flags;
6589 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6590 SDValue NewVal =
6591 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6592 transferDbgValues(N1, NewVal);
6593 return NewVal;
6594 }
6595
6596 if (OpOpcode == ISD::POISON)
6597 return getPOISON(VT);
6598
6599 if (N1.isUndef())
6600 // zext(undef) = 0, because the top bits will be zero.
6601 return getConstant(0, DL, VT);
6602
6603 // Skip unnecessary zext_inreg pattern:
6604 // (zext (trunc x)) -> x iff the upper bits are known zero.
6605 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6606 // use to recognise zext_inreg patterns.
6607 if (OpOpcode == ISD::TRUNCATE) {
6608 SDValue OpOp = N1.getOperand(0);
6609 if (OpOp.getValueType() == VT) {
6610 if (OpOp.getOpcode() != ISD::AND) {
6613 if (MaskedValueIsZero(OpOp, HiBits)) {
6614 transferDbgValues(N1, OpOp);
6615 return OpOp;
6616 }
6617 }
6618 }
6619 }
6620 break;
6621 case ISD::ANY_EXTEND:
6622 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6623 "Invalid ANY_EXTEND!");
6624 assert(VT.isVector() == N1.getValueType().isVector() &&
6625 "ANY_EXTEND result type type should be vector iff the operand "
6626 "type is vector!");
6627 if (N1.getValueType() == VT) return N1; // noop extension
6628 assert((!VT.isVector() || VT.getVectorElementCount() ==
6630 "Vector element count mismatch!");
6631 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6632
6633 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6634 OpOpcode == ISD::ANY_EXTEND) {
6635 SDNodeFlags Flags;
6636 if (OpOpcode == ISD::ZERO_EXTEND)
6637 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6638 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6639 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6640 }
6641 if (N1.isUndef())
6642 return getUNDEF(VT);
6643
6644 // (ext (trunc x)) -> x
6645 if (OpOpcode == ISD::TRUNCATE) {
6646 SDValue OpOp = N1.getOperand(0);
6647 if (OpOp.getValueType() == VT) {
6648 transferDbgValues(N1, OpOp);
6649 return OpOp;
6650 }
6651 }
6652 break;
6653 case ISD::TRUNCATE:
6654 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6655 "Invalid TRUNCATE!");
6656 assert(VT.isVector() == N1.getValueType().isVector() &&
6657 "TRUNCATE result type type should be vector iff the operand "
6658 "type is vector!");
6659 if (N1.getValueType() == VT) return N1; // noop truncate
6660 assert((!VT.isVector() || VT.getVectorElementCount() ==
6662 "Vector element count mismatch!");
6663 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6664 if (OpOpcode == ISD::TRUNCATE)
6665 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6666 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6667 OpOpcode == ISD::ANY_EXTEND) {
6668 // If the source is smaller than the dest, we still need an extend.
6670 VT.getScalarType())) {
6671 SDNodeFlags Flags;
6672 if (OpOpcode == ISD::ZERO_EXTEND)
6673 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6674 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6675 }
6676 if (N1.getOperand(0).getValueType().bitsGT(VT))
6677 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6678 return N1.getOperand(0);
6679 }
6680 if (N1.isUndef())
6681 return getUNDEF(VT);
6682 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6683 return getVScale(DL, VT,
6685 break;
6689 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6690 assert(N1.getValueType().bitsLE(VT) &&
6691 "The input must be the same size or smaller than the result.");
6694 "The destination vector type must have fewer lanes than the input.");
6695 break;
6696 case ISD::ABS:
6697 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6698 if (N1.isUndef())
6699 return getConstant(0, DL, VT);
6700 break;
6701 case ISD::BSWAP:
6702 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6703 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6704 "BSWAP types must be a multiple of 16 bits!");
6705 if (N1.isUndef())
6706 return getUNDEF(VT);
6707 // bswap(bswap(X)) -> X.
6708 if (OpOpcode == ISD::BSWAP)
6709 return N1.getOperand(0);
6710 break;
6711 case ISD::BITREVERSE:
6712 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6713 if (N1.isUndef())
6714 return getUNDEF(VT);
6715 break;
6716 case ISD::BITCAST:
6718 "Cannot BITCAST between types of different sizes!");
6719 if (VT == N1.getValueType()) return N1; // noop conversion.
6720 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6721 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6722 if (N1.isUndef())
6723 return getUNDEF(VT);
6724 break;
6726 assert(VT.isVector() && !N1.getValueType().isVector() &&
6727 (VT.getVectorElementType() == N1.getValueType() ||
6729 N1.getValueType().isInteger() &&
6731 "Illegal SCALAR_TO_VECTOR node!");
6732 if (N1.isUndef())
6733 return getUNDEF(VT);
6734 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6735 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6737 N1.getConstantOperandVal(1) == 0 &&
6738 N1.getOperand(0).getValueType() == VT)
6739 return N1.getOperand(0);
6740 break;
6741 case ISD::FNEG:
6742 // Negation of an unknown bag of bits is still completely undefined.
6743 if (N1.isUndef())
6744 return getUNDEF(VT);
6745
6746 if (OpOpcode == ISD::FNEG) // --X -> X
6747 return N1.getOperand(0);
6748 break;
6749 case ISD::FABS:
6750 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6751 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6752 break;
6753 case ISD::VSCALE:
6754 assert(VT == N1.getValueType() && "Unexpected VT!");
6755 break;
6756 case ISD::CTPOP:
6757 if (N1.getValueType().getScalarType() == MVT::i1)
6758 return N1;
6759 break;
6760 case ISD::CTLZ:
6761 case ISD::CTTZ:
6762 if (N1.getValueType().getScalarType() == MVT::i1)
6763 return getNOT(DL, N1, N1.getValueType());
6764 break;
6765 case ISD::VECREDUCE_ADD:
6766 if (N1.getValueType().getScalarType() == MVT::i1)
6767 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6768 break;
6769 case ISD::VECREDUCE_SMIN:
6770 case ISD::VECREDUCE_UMAX:
6771 if (N1.getValueType().getScalarType() == MVT::i1)
6772 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6773 break;
6774 case ISD::VECREDUCE_SMAX:
6775 case ISD::VECREDUCE_UMIN:
6776 if (N1.getValueType().getScalarType() == MVT::i1)
6777 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6778 break;
6779 case ISD::SPLAT_VECTOR:
6780 assert(VT.isVector() && "Wrong return type!");
6781 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6782 // that for now.
6784 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6786 N1.getValueType().isInteger() &&
6788 "Wrong operand type!");
6789 break;
6790 }
6791
6792 SDNode *N;
6793 SDVTList VTs = getVTList(VT);
6794 SDValue Ops[] = {N1};
6795 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6797 AddNodeIDNode(ID, Opcode, VTs, Ops);
6798 void *IP = nullptr;
6799 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6800 E->intersectFlagsWith(Flags);
6801 return SDValue(E, 0);
6802 }
6803
6804 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6805 N->setFlags(Flags);
6806 createOperands(N, Ops);
6807 CSEMap.InsertNode(N, IP);
6808 } else {
6809 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6810 createOperands(N, Ops);
6811 }
6812
6813 InsertNode(N);
6814 SDValue V = SDValue(N, 0);
6815 NewSDValueDbgMsg(V, "Creating new node: ", this);
6816 return V;
6817}
6818
6819static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6820 const APInt &C2) {
6821 switch (Opcode) {
6822 case ISD::ADD: return C1 + C2;
6823 case ISD::SUB: return C1 - C2;
6824 case ISD::MUL: return C1 * C2;
6825 case ISD::AND: return C1 & C2;
6826 case ISD::OR: return C1 | C2;
6827 case ISD::XOR: return C1 ^ C2;
6828 case ISD::SHL: return C1 << C2;
6829 case ISD::SRL: return C1.lshr(C2);
6830 case ISD::SRA: return C1.ashr(C2);
6831 case ISD::ROTL: return C1.rotl(C2);
6832 case ISD::ROTR: return C1.rotr(C2);
6833 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6834 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6835 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6836 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6837 case ISD::SADDSAT: return C1.sadd_sat(C2);
6838 case ISD::UADDSAT: return C1.uadd_sat(C2);
6839 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6840 case ISD::USUBSAT: return C1.usub_sat(C2);
6841 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6842 case ISD::USHLSAT: return C1.ushl_sat(C2);
6843 case ISD::UDIV:
6844 if (!C2.getBoolValue())
6845 break;
6846 return C1.udiv(C2);
6847 case ISD::UREM:
6848 if (!C2.getBoolValue())
6849 break;
6850 return C1.urem(C2);
6851 case ISD::SDIV:
6852 if (!C2.getBoolValue())
6853 break;
6854 return C1.sdiv(C2);
6855 case ISD::SREM:
6856 if (!C2.getBoolValue())
6857 break;
6858 return C1.srem(C2);
6859 case ISD::AVGFLOORS:
6860 return APIntOps::avgFloorS(C1, C2);
6861 case ISD::AVGFLOORU:
6862 return APIntOps::avgFloorU(C1, C2);
6863 case ISD::AVGCEILS:
6864 return APIntOps::avgCeilS(C1, C2);
6865 case ISD::AVGCEILU:
6866 return APIntOps::avgCeilU(C1, C2);
6867 case ISD::ABDS:
6868 return APIntOps::abds(C1, C2);
6869 case ISD::ABDU:
6870 return APIntOps::abdu(C1, C2);
6871 case ISD::MULHS:
6872 return APIntOps::mulhs(C1, C2);
6873 case ISD::MULHU:
6874 return APIntOps::mulhu(C1, C2);
6875 }
6876 return std::nullopt;
6877}
6878// Handle constant folding with UNDEF.
6879// TODO: Handle more cases.
6880static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6881 bool IsUndef1, const APInt &C2,
6882 bool IsUndef2) {
6883 if (!(IsUndef1 || IsUndef2))
6884 return FoldValue(Opcode, C1, C2);
6885
6886 // Fold and(x, undef) -> 0
6887 // Fold mul(x, undef) -> 0
6888 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6889 return APInt::getZero(C1.getBitWidth());
6890
6891 return std::nullopt;
6892}
6893
6895 const GlobalAddressSDNode *GA,
6896 const SDNode *N2) {
6897 if (GA->getOpcode() != ISD::GlobalAddress)
6898 return SDValue();
6899 if (!TLI->isOffsetFoldingLegal(GA))
6900 return SDValue();
6901 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6902 if (!C2)
6903 return SDValue();
6904 int64_t Offset = C2->getSExtValue();
6905 switch (Opcode) {
6906 case ISD::ADD:
6907 case ISD::PTRADD:
6908 break;
6909 case ISD::SUB: Offset = -uint64_t(Offset); break;
6910 default: return SDValue();
6911 }
6912 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6913 GA->getOffset() + uint64_t(Offset));
6914}
6915
6917 switch (Opcode) {
6918 case ISD::SDIV:
6919 case ISD::UDIV:
6920 case ISD::SREM:
6921 case ISD::UREM: {
6922 // If a divisor is zero/undef or any element of a divisor vector is
6923 // zero/undef, the whole op is undef.
6924 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6925 SDValue Divisor = Ops[1];
6926 if (Divisor.isUndef() || isNullConstant(Divisor))
6927 return true;
6928
6929 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6930 llvm::any_of(Divisor->op_values(),
6931 [](SDValue V) { return V.isUndef() ||
6932 isNullConstant(V); });
6933 // TODO: Handle signed overflow.
6934 }
6935 // TODO: Handle oversized shifts.
6936 default:
6937 return false;
6938 }
6939}
6940
6943 SDNodeFlags Flags) {
6944 // If the opcode is a target-specific ISD node, there's nothing we can
6945 // do here and the operand rules may not line up with the below, so
6946 // bail early.
6947 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6948 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6949 // foldCONCAT_VECTORS in getNode before this is called.
6950 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6951 return SDValue();
6952
6953 unsigned NumOps = Ops.size();
6954 if (NumOps == 0)
6955 return SDValue();
6956
6957 if (isUndef(Opcode, Ops))
6958 return getUNDEF(VT);
6959
6960 // Handle unary special cases.
6961 if (NumOps == 1) {
6962 SDValue N1 = Ops[0];
6963
6964 // Constant fold unary operations with an integer constant operand. Even
6965 // opaque constant will be folded, because the folding of unary operations
6966 // doesn't create new constants with different values. Nevertheless, the
6967 // opaque flag is preserved during folding to prevent future folding with
6968 // other constants.
6969 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6970 const APInt &Val = C->getAPIntValue();
6971 switch (Opcode) {
6972 case ISD::SIGN_EXTEND:
6973 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6974 C->isTargetOpcode(), C->isOpaque());
6975 case ISD::TRUNCATE:
6976 if (C->isOpaque())
6977 break;
6978 [[fallthrough]];
6979 case ISD::ZERO_EXTEND:
6980 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6981 C->isTargetOpcode(), C->isOpaque());
6982 case ISD::ANY_EXTEND:
6983 // Some targets like RISCV prefer to sign extend some types.
6984 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6985 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6986 C->isTargetOpcode(), C->isOpaque());
6987 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6988 C->isTargetOpcode(), C->isOpaque());
6989 case ISD::ABS:
6990 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6991 C->isOpaque());
6992 case ISD::BITREVERSE:
6993 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6994 C->isOpaque());
6995 case ISD::BSWAP:
6996 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6997 C->isOpaque());
6998 case ISD::CTPOP:
6999 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7000 C->isOpaque());
7001 case ISD::CTLZ:
7003 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7004 C->isOpaque());
7005 case ISD::CTTZ:
7007 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7008 C->isOpaque());
7009 case ISD::UINT_TO_FP:
7010 case ISD::SINT_TO_FP: {
7012 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7014 return getConstantFP(FPV, DL, VT);
7015 }
7016 case ISD::FP16_TO_FP:
7017 case ISD::BF16_TO_FP: {
7018 bool Ignored;
7019 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7020 : APFloat::BFloat(),
7021 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7022
7023 // This can return overflow, underflow, or inexact; we don't care.
7024 // FIXME need to be more flexible about rounding mode.
7026 &Ignored);
7027 return getConstantFP(FPV, DL, VT);
7028 }
7029 case ISD::STEP_VECTOR:
7030 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7031 return V;
7032 break;
7033 case ISD::BITCAST:
7034 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7035 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7036 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7037 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7038 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7039 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7040 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7041 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7042 break;
7043 }
7044 }
7045
7046 // Constant fold unary operations with a floating point constant operand.
7047 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7048 APFloat V = C->getValueAPF(); // make copy
7049 switch (Opcode) {
7050 case ISD::FNEG:
7051 V.changeSign();
7052 return getConstantFP(V, DL, VT);
7053 case ISD::FABS:
7054 V.clearSign();
7055 return getConstantFP(V, DL, VT);
7056 case ISD::FCEIL: {
7057 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7059 return getConstantFP(V, DL, VT);
7060 return SDValue();
7061 }
7062 case ISD::FTRUNC: {
7063 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7065 return getConstantFP(V, DL, VT);
7066 return SDValue();
7067 }
7068 case ISD::FFLOOR: {
7069 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7071 return getConstantFP(V, DL, VT);
7072 return SDValue();
7073 }
7074 case ISD::FP_EXTEND: {
7075 bool ignored;
7076 // This can return overflow, underflow, or inexact; we don't care.
7077 // FIXME need to be more flexible about rounding mode.
7078 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7079 &ignored);
7080 return getConstantFP(V, DL, VT);
7081 }
7082 case ISD::FP_TO_SINT:
7083 case ISD::FP_TO_UINT: {
7084 bool ignored;
7085 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7086 // FIXME need to be more flexible about rounding mode.
7088 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7089 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7090 break;
7091 return getConstant(IntVal, DL, VT);
7092 }
7093 case ISD::FP_TO_FP16:
7094 case ISD::FP_TO_BF16: {
7095 bool Ignored;
7096 // This can return overflow, underflow, or inexact; we don't care.
7097 // FIXME need to be more flexible about rounding mode.
7098 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7099 : APFloat::BFloat(),
7101 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7102 }
7103 case ISD::BITCAST:
7104 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7105 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7106 VT);
7107 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7108 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7109 VT);
7110 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7111 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7112 VT);
7113 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7114 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7115 break;
7116 }
7117 }
7118
7119 // Early-out if we failed to constant fold a bitcast.
7120 if (Opcode == ISD::BITCAST)
7121 return SDValue();
7122 }
7123
7124 // Handle binops special cases.
7125 if (NumOps == 2) {
7126 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7127 return CFP;
7128
7129 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7130 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7131 if (C1->isOpaque() || C2->isOpaque())
7132 return SDValue();
7133
7134 std::optional<APInt> FoldAttempt =
7135 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7136 if (!FoldAttempt)
7137 return SDValue();
7138
7139 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7140 assert((!Folded || !VT.isVector()) &&
7141 "Can't fold vectors ops with scalar operands");
7142 return Folded;
7143 }
7144 }
7145
7146 // fold (add Sym, c) -> Sym+c
7148 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7149 if (TLI->isCommutativeBinOp(Opcode))
7151 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7152
7153 // fold (sext_in_reg c1) -> c2
7154 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7155 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7156
7157 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7158 unsigned FromBits = EVT.getScalarSizeInBits();
7159 Val <<= Val.getBitWidth() - FromBits;
7160 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7161 return getConstant(Val, DL, ConstantVT);
7162 };
7163
7164 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7165 const APInt &Val = C1->getAPIntValue();
7166 return SignExtendInReg(Val, VT);
7167 }
7168
7170 SmallVector<SDValue, 8> ScalarOps;
7171 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7172 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7173 SDValue Op = Ops[0].getOperand(I);
7174 if (Op.isUndef()) {
7175 ScalarOps.push_back(getUNDEF(OpVT));
7176 continue;
7177 }
7178 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7179 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7180 }
7181 return getBuildVector(VT, DL, ScalarOps);
7182 }
7183
7184 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7185 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7186 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7187 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7188 Ops[0].getOperand(0).getValueType()));
7189 }
7190 }
7191
7192 // Handle fshl/fshr special cases.
7193 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7194 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7195 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7196 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7197
7198 if (C1 && C2 && C3) {
7199 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7200 return SDValue();
7201 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7202 &V3 = C3->getAPIntValue();
7203
7204 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7205 : APIntOps::fshr(V1, V2, V3);
7206 return getConstant(FoldedVal, DL, VT);
7207 }
7208 }
7209
7210 // Handle fma/fmad special cases.
7211 if (Opcode == ISD::FMA || Opcode == ISD::FMAD) {
7212 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7213 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7214 Ops[2].getValueType() == VT && "FMA types must match!");
7218 if (C1 && C2 && C3) {
7219 APFloat V1 = C1->getValueAPF();
7220 const APFloat &V2 = C2->getValueAPF();
7221 const APFloat &V3 = C3->getValueAPF();
7222 if (Opcode == ISD::FMAD) {
7225 } else
7227 return getConstantFP(V1, DL, VT);
7228 }
7229 }
7230
7231 // This is for vector folding only from here on.
7232 if (!VT.isVector())
7233 return SDValue();
7234
7235 ElementCount NumElts = VT.getVectorElementCount();
7236
7237 // See if we can fold through any bitcasted integer ops.
7238 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7239 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7240 (Ops[0].getOpcode() == ISD::BITCAST ||
7241 Ops[1].getOpcode() == ISD::BITCAST)) {
7244 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7245 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7246 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7247 N2.getValueType().isInteger()) {
7248 bool IsLE = getDataLayout().isLittleEndian();
7249 unsigned EltBits = VT.getScalarSizeInBits();
7250 SmallVector<APInt> RawBits1, RawBits2;
7251 BitVector UndefElts1, UndefElts2;
7252 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7253 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7254 SmallVector<APInt> RawBits;
7255 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7256 std::optional<APInt> Fold = FoldValueWithUndef(
7257 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7258 if (!Fold)
7259 break;
7260 RawBits.push_back(*Fold);
7261 }
7262 if (RawBits.size() == NumElts.getFixedValue()) {
7263 // We have constant folded, but we might need to cast this again back
7264 // to the original (possibly legalized) type.
7265 EVT BVVT, BVEltVT;
7266 if (N1.getValueType() == VT) {
7267 BVVT = N1.getValueType();
7268 BVEltVT = BV1->getOperand(0).getValueType();
7269 } else {
7270 BVVT = N2.getValueType();
7271 BVEltVT = BV2->getOperand(0).getValueType();
7272 }
7273 unsigned BVEltBits = BVEltVT.getSizeInBits();
7274 SmallVector<APInt> DstBits;
7275 BitVector DstUndefs;
7277 DstBits, RawBits, DstUndefs,
7278 BitVector(RawBits.size(), false));
7279 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7280 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7281 if (DstUndefs[I])
7282 continue;
7283 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7284 }
7285 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7286 }
7287 }
7288 }
7289 }
7290
7291 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7292 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7293 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7294 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7295 APInt RHSVal;
7296 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7297 APInt NewStep = Opcode == ISD::MUL
7298 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7299 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7300 return getStepVector(DL, VT, NewStep);
7301 }
7302 }
7303
7304 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7305 return !Op.getValueType().isVector() ||
7306 Op.getValueType().getVectorElementCount() == NumElts;
7307 };
7308
7309 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7310 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7311 Op.getOpcode() == ISD::BUILD_VECTOR ||
7312 Op.getOpcode() == ISD::SPLAT_VECTOR;
7313 };
7314
7315 // All operands must be vector types with the same number of elements as
7316 // the result type and must be either UNDEF or a build/splat vector
7317 // or UNDEF scalars.
7318 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7319 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7320 return SDValue();
7321
7322 // If we are comparing vectors, then the result needs to be a i1 boolean that
7323 // is then extended back to the legal result type depending on how booleans
7324 // are represented.
7325 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7326 ISD::NodeType ExtendCode =
7327 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7328 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7330
7331 // Find legal integer scalar type for constant promotion and
7332 // ensure that its scalar size is at least as large as source.
7333 EVT LegalSVT = VT.getScalarType();
7334 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7335 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7336 if (LegalSVT.bitsLT(VT.getScalarType()))
7337 return SDValue();
7338 }
7339
7340 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7341 // only have one operand to check. For fixed-length vector types we may have
7342 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7343 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7344
7345 // Constant fold each scalar lane separately.
7346 SmallVector<SDValue, 4> ScalarResults;
7347 for (unsigned I = 0; I != NumVectorElts; I++) {
7348 SmallVector<SDValue, 4> ScalarOps;
7349 for (SDValue Op : Ops) {
7350 EVT InSVT = Op.getValueType().getScalarType();
7351 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7352 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7353 if (Op.isUndef())
7354 ScalarOps.push_back(getUNDEF(InSVT));
7355 else
7356 ScalarOps.push_back(Op);
7357 continue;
7358 }
7359
7360 SDValue ScalarOp =
7361 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7362 EVT ScalarVT = ScalarOp.getValueType();
7363
7364 // Build vector (integer) scalar operands may need implicit
7365 // truncation - do this before constant folding.
7366 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7367 // Don't create illegally-typed nodes unless they're constants or undef
7368 // - if we fail to constant fold we can't guarantee the (dead) nodes
7369 // we're creating will be cleaned up before being visited for
7370 // legalization.
7371 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7372 !isa<ConstantSDNode>(ScalarOp) &&
7373 TLI->getTypeAction(*getContext(), InSVT) !=
7375 return SDValue();
7376 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7377 }
7378
7379 ScalarOps.push_back(ScalarOp);
7380 }
7381
7382 // Constant fold the scalar operands.
7383 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7384
7385 // Scalar folding only succeeded if the result is a constant or UNDEF.
7386 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7387 ScalarResult.getOpcode() != ISD::ConstantFP)
7388 return SDValue();
7389
7390 // Legalize the (integer) scalar constant if necessary. We only do
7391 // this once we know the folding succeeded, since otherwise we would
7392 // get a node with illegal type which has a user.
7393 if (LegalSVT != SVT)
7394 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7395
7396 ScalarResults.push_back(ScalarResult);
7397 }
7398
7399 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7400 : getBuildVector(VT, DL, ScalarResults);
7401 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7402 return V;
7403}
7404
7407 // TODO: Add support for unary/ternary fp opcodes.
7408 if (Ops.size() != 2)
7409 return SDValue();
7410
7411 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7412 // should. That will require dealing with a potentially non-default
7413 // rounding mode, checking the "opStatus" return value from the APFloat
7414 // math calculations, and possibly other variations.
7415 SDValue N1 = Ops[0];
7416 SDValue N2 = Ops[1];
7417 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7418 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7419 if (N1CFP && N2CFP) {
7420 APFloat C1 = N1CFP->getValueAPF(); // make copy
7421 const APFloat &C2 = N2CFP->getValueAPF();
7422 switch (Opcode) {
7423 case ISD::FADD:
7425 return getConstantFP(C1, DL, VT);
7426 case ISD::FSUB:
7428 return getConstantFP(C1, DL, VT);
7429 case ISD::FMUL:
7431 return getConstantFP(C1, DL, VT);
7432 case ISD::FDIV:
7434 return getConstantFP(C1, DL, VT);
7435 case ISD::FREM:
7436 C1.mod(C2);
7437 return getConstantFP(C1, DL, VT);
7438 case ISD::FCOPYSIGN:
7439 C1.copySign(C2);
7440 return getConstantFP(C1, DL, VT);
7441 case ISD::FMINNUM:
7442 return getConstantFP(minnum(C1, C2), DL, VT);
7443 case ISD::FMAXNUM:
7444 return getConstantFP(maxnum(C1, C2), DL, VT);
7445 case ISD::FMINIMUM:
7446 return getConstantFP(minimum(C1, C2), DL, VT);
7447 case ISD::FMAXIMUM:
7448 return getConstantFP(maximum(C1, C2), DL, VT);
7449 case ISD::FMINIMUMNUM:
7450 return getConstantFP(minimumnum(C1, C2), DL, VT);
7451 case ISD::FMAXIMUMNUM:
7452 return getConstantFP(maximumnum(C1, C2), DL, VT);
7453 default: break;
7454 }
7455 }
7456 if (N1CFP && Opcode == ISD::FP_ROUND) {
7457 APFloat C1 = N1CFP->getValueAPF(); // make copy
7458 bool Unused;
7459 // This can return overflow, underflow, or inexact; we don't care.
7460 // FIXME need to be more flexible about rounding mode.
7462 &Unused);
7463 return getConstantFP(C1, DL, VT);
7464 }
7465
7466 switch (Opcode) {
7467 case ISD::FSUB:
7468 // -0.0 - undef --> undef (consistent with "fneg undef")
7469 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7470 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7471 return getUNDEF(VT);
7472 [[fallthrough]];
7473
7474 case ISD::FADD:
7475 case ISD::FMUL:
7476 case ISD::FDIV:
7477 case ISD::FREM:
7478 // If both operands are undef, the result is undef. If 1 operand is undef,
7479 // the result is NaN. This should match the behavior of the IR optimizer.
7480 if (N1.isUndef() && N2.isUndef())
7481 return getUNDEF(VT);
7482 if (N1.isUndef() || N2.isUndef())
7484 }
7485 return SDValue();
7486}
7487
7489 const SDLoc &DL, EVT DstEltVT) {
7490 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7491
7492 // If this is already the right type, we're done.
7493 if (SrcEltVT == DstEltVT)
7494 return SDValue(BV, 0);
7495
7496 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7497 unsigned DstBitSize = DstEltVT.getSizeInBits();
7498
7499 // If this is a conversion of N elements of one type to N elements of another
7500 // type, convert each element. This handles FP<->INT cases.
7501 if (SrcBitSize == DstBitSize) {
7503 for (SDValue Op : BV->op_values()) {
7504 // If the vector element type is not legal, the BUILD_VECTOR operands
7505 // are promoted and implicitly truncated. Make that explicit here.
7506 if (Op.getValueType() != SrcEltVT)
7507 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7508 Ops.push_back(getBitcast(DstEltVT, Op));
7509 }
7510 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7512 return getBuildVector(VT, DL, Ops);
7513 }
7514
7515 // Otherwise, we're growing or shrinking the elements. To avoid having to
7516 // handle annoying details of growing/shrinking FP values, we convert them to
7517 // int first.
7518 if (SrcEltVT.isFloatingPoint()) {
7519 // Convert the input float vector to a int vector where the elements are the
7520 // same sizes.
7521 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7522 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7524 DstEltVT);
7525 return SDValue();
7526 }
7527
7528 // Now we know the input is an integer vector. If the output is a FP type,
7529 // convert to integer first, then to FP of the right size.
7530 if (DstEltVT.isFloatingPoint()) {
7531 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7532 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7534 DstEltVT);
7535 return SDValue();
7536 }
7537
7538 // Okay, we know the src/dst types are both integers of differing types.
7539 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7540
7541 // Extract the constant raw bit data.
7542 BitVector UndefElements;
7543 SmallVector<APInt> RawBits;
7544 bool IsLE = getDataLayout().isLittleEndian();
7545 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7546 return SDValue();
7547
7549 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7550 if (UndefElements[I])
7551 Ops.push_back(getUNDEF(DstEltVT));
7552 else
7553 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7554 }
7555
7556 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7557 return getBuildVector(VT, DL, Ops);
7558}
7559
7561 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7562
7563 // There's no need to assert on a byte-aligned pointer. All pointers are at
7564 // least byte aligned.
7565 if (A == Align(1))
7566 return Val;
7567
7568 SDVTList VTs = getVTList(Val.getValueType());
7570 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7571 ID.AddInteger(A.value());
7572
7573 void *IP = nullptr;
7574 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7575 return SDValue(E, 0);
7576
7577 auto *N =
7578 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7579 createOperands(N, {Val});
7580
7581 CSEMap.InsertNode(N, IP);
7582 InsertNode(N);
7583
7584 SDValue V(N, 0);
7585 NewSDValueDbgMsg(V, "Creating new node: ", this);
7586 return V;
7587}
7588
7589SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7590 SDValue N1, SDValue N2) {
7591 SDNodeFlags Flags;
7592 if (Inserter)
7593 Flags = Inserter->getFlags();
7594 return getNode(Opcode, DL, VT, N1, N2, Flags);
7595}
7596
7598 SDValue &N2) const {
7599 if (!TLI->isCommutativeBinOp(Opcode))
7600 return;
7601
7602 // Canonicalize:
7603 // binop(const, nonconst) -> binop(nonconst, const)
7606 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7607 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7608 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7609 std::swap(N1, N2);
7610
7611 // Canonicalize:
7612 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7613 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7615 std::swap(N1, N2);
7616}
7617
7618SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7619 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7621 N2.getOpcode() != ISD::DELETED_NODE &&
7622 "Operand is DELETED_NODE!");
7623
7624 canonicalizeCommutativeBinop(Opcode, N1, N2);
7625
7626 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7627 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7628
7629 // Don't allow undefs in vector splats - we might be returning N2 when folding
7630 // to zero etc.
7631 ConstantSDNode *N2CV =
7632 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7633
7634 switch (Opcode) {
7635 default: break;
7636 case ISD::TokenFactor:
7637 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7638 N2.getValueType() == MVT::Other && "Invalid token factor!");
7639 // Fold trivial token factors.
7640 if (N1.getOpcode() == ISD::EntryToken) return N2;
7641 if (N2.getOpcode() == ISD::EntryToken) return N1;
7642 if (N1 == N2) return N1;
7643 break;
7644 case ISD::BUILD_VECTOR: {
7645 // Attempt to simplify BUILD_VECTOR.
7646 SDValue Ops[] = {N1, N2};
7647 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7648 return V;
7649 break;
7650 }
7651 case ISD::CONCAT_VECTORS: {
7652 SDValue Ops[] = {N1, N2};
7653 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7654 return V;
7655 break;
7656 }
7657 case ISD::AND:
7658 assert(VT.isInteger() && "This operator does not apply to FP types!");
7659 assert(N1.getValueType() == N2.getValueType() &&
7660 N1.getValueType() == VT && "Binary operator types must match!");
7661 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7662 // worth handling here.
7663 if (N2CV && N2CV->isZero())
7664 return N2;
7665 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7666 return N1;
7667 break;
7668 case ISD::OR:
7669 case ISD::XOR:
7670 case ISD::ADD:
7671 case ISD::PTRADD:
7672 case ISD::SUB:
7673 assert(VT.isInteger() && "This operator does not apply to FP types!");
7674 assert(N1.getValueType() == N2.getValueType() &&
7675 N1.getValueType() == VT && "Binary operator types must match!");
7676 // The equal operand types requirement is unnecessarily strong for PTRADD.
7677 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7678 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7679 // logic everywhere where PTRADDs may be folded or combined to properly
7680 // support them. If/when we introduce pointer types to the SDAG, we will
7681 // need to relax this constraint.
7682
7683 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7684 // it's worth handling here.
7685 if (N2CV && N2CV->isZero())
7686 return N1;
7687 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7688 VT.getScalarType() == MVT::i1)
7689 return getNode(ISD::XOR, DL, VT, N1, N2);
7690 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7691 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7692 N2.getOpcode() == ISD::VSCALE) {
7693 const APInt &C1 = N1->getConstantOperandAPInt(0);
7694 const APInt &C2 = N2->getConstantOperandAPInt(0);
7695 return getVScale(DL, VT, C1 + C2);
7696 }
7697 break;
7698 case ISD::MUL:
7699 assert(VT.isInteger() && "This operator does not apply to FP types!");
7700 assert(N1.getValueType() == N2.getValueType() &&
7701 N1.getValueType() == VT && "Binary operator types must match!");
7702 if (VT.getScalarType() == MVT::i1)
7703 return getNode(ISD::AND, DL, VT, N1, N2);
7704 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7705 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7706 const APInt &N2CImm = N2C->getAPIntValue();
7707 return getVScale(DL, VT, MulImm * N2CImm);
7708 }
7709 break;
7710 case ISD::UDIV:
7711 case ISD::UREM:
7712 case ISD::MULHU:
7713 case ISD::MULHS:
7714 case ISD::SDIV:
7715 case ISD::SREM:
7716 case ISD::SADDSAT:
7717 case ISD::SSUBSAT:
7718 case ISD::UADDSAT:
7719 case ISD::USUBSAT:
7720 assert(VT.isInteger() && "This operator does not apply to FP types!");
7721 assert(N1.getValueType() == N2.getValueType() &&
7722 N1.getValueType() == VT && "Binary operator types must match!");
7723 if (VT.getScalarType() == MVT::i1) {
7724 // fold (add_sat x, y) -> (or x, y) for bool types.
7725 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7726 return getNode(ISD::OR, DL, VT, N1, N2);
7727 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7728 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7729 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7730 }
7731 break;
7732 case ISD::SCMP:
7733 case ISD::UCMP:
7734 assert(N1.getValueType() == N2.getValueType() &&
7735 "Types of operands of UCMP/SCMP must match");
7736 assert(N1.getValueType().isVector() == VT.isVector() &&
7737 "Operands and return type of must both be scalars or vectors");
7738 if (VT.isVector())
7741 "Result and operands must have the same number of elements");
7742 break;
7743 case ISD::AVGFLOORS:
7744 case ISD::AVGFLOORU:
7745 case ISD::AVGCEILS:
7746 case ISD::AVGCEILU:
7747 assert(VT.isInteger() && "This operator does not apply to FP types!");
7748 assert(N1.getValueType() == N2.getValueType() &&
7749 N1.getValueType() == VT && "Binary operator types must match!");
7750 break;
7751 case ISD::ABDS:
7752 case ISD::ABDU:
7753 assert(VT.isInteger() && "This operator does not apply to FP types!");
7754 assert(N1.getValueType() == N2.getValueType() &&
7755 N1.getValueType() == VT && "Binary operator types must match!");
7756 if (VT.getScalarType() == MVT::i1)
7757 return getNode(ISD::XOR, DL, VT, N1, N2);
7758 break;
7759 case ISD::SMIN:
7760 case ISD::UMAX:
7761 assert(VT.isInteger() && "This operator does not apply to FP types!");
7762 assert(N1.getValueType() == N2.getValueType() &&
7763 N1.getValueType() == VT && "Binary operator types must match!");
7764 if (VT.getScalarType() == MVT::i1)
7765 return getNode(ISD::OR, DL, VT, N1, N2);
7766 break;
7767 case ISD::SMAX:
7768 case ISD::UMIN:
7769 assert(VT.isInteger() && "This operator does not apply to FP types!");
7770 assert(N1.getValueType() == N2.getValueType() &&
7771 N1.getValueType() == VT && "Binary operator types must match!");
7772 if (VT.getScalarType() == MVT::i1)
7773 return getNode(ISD::AND, DL, VT, N1, N2);
7774 break;
7775 case ISD::FADD:
7776 case ISD::FSUB:
7777 case ISD::FMUL:
7778 case ISD::FDIV:
7779 case ISD::FREM:
7780 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7781 assert(N1.getValueType() == N2.getValueType() &&
7782 N1.getValueType() == VT && "Binary operator types must match!");
7783 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7784 return V;
7785 break;
7786 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7787 assert(N1.getValueType() == VT &&
7790 "Invalid FCOPYSIGN!");
7791 break;
7792 case ISD::SHL:
7793 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7794 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7795 const APInt &ShiftImm = N2C->getAPIntValue();
7796 return getVScale(DL, VT, MulImm << ShiftImm);
7797 }
7798 [[fallthrough]];
7799 case ISD::SRA:
7800 case ISD::SRL:
7801 if (SDValue V = simplifyShift(N1, N2))
7802 return V;
7803 [[fallthrough]];
7804 case ISD::ROTL:
7805 case ISD::ROTR:
7806 assert(VT == N1.getValueType() &&
7807 "Shift operators return type must be the same as their first arg");
7808 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7809 "Shifts only work on integers");
7810 assert((!VT.isVector() || VT == N2.getValueType()) &&
7811 "Vector shift amounts must be in the same as their first arg");
7812 // Verify that the shift amount VT is big enough to hold valid shift
7813 // amounts. This catches things like trying to shift an i1024 value by an
7814 // i8, which is easy to fall into in generic code that uses
7815 // TLI.getShiftAmount().
7818 "Invalid use of small shift amount with oversized value!");
7819
7820 // Always fold shifts of i1 values so the code generator doesn't need to
7821 // handle them. Since we know the size of the shift has to be less than the
7822 // size of the value, the shift/rotate count is guaranteed to be zero.
7823 if (VT == MVT::i1)
7824 return N1;
7825 if (N2CV && N2CV->isZero())
7826 return N1;
7827 break;
7828 case ISD::FP_ROUND:
7830 VT.bitsLE(N1.getValueType()) && N2C &&
7831 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7832 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7833 if (N1.getValueType() == VT) return N1; // noop conversion.
7834 break;
7835 case ISD::AssertNoFPClass: {
7837 "AssertNoFPClass is used for a non-floating type");
7838 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7839 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7840 assert(llvm::to_underlying(NoFPClass) <=
7842 "FPClassTest value too large");
7843 (void)NoFPClass;
7844 break;
7845 }
7846 case ISD::AssertSext:
7847 case ISD::AssertZext: {
7848 EVT EVT = cast<VTSDNode>(N2)->getVT();
7849 assert(VT == N1.getValueType() && "Not an inreg extend!");
7850 assert(VT.isInteger() && EVT.isInteger() &&
7851 "Cannot *_EXTEND_INREG FP types");
7852 assert(!EVT.isVector() &&
7853 "AssertSExt/AssertZExt type should be the vector element type "
7854 "rather than the vector type!");
7855 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7856 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7857 break;
7858 }
7860 EVT EVT = cast<VTSDNode>(N2)->getVT();
7861 assert(VT == N1.getValueType() && "Not an inreg extend!");
7862 assert(VT.isInteger() && EVT.isInteger() &&
7863 "Cannot *_EXTEND_INREG FP types");
7864 assert(EVT.isVector() == VT.isVector() &&
7865 "SIGN_EXTEND_INREG type should be vector iff the operand "
7866 "type is vector!");
7867 assert((!EVT.isVector() ||
7869 "Vector element counts must match in SIGN_EXTEND_INREG");
7870 assert(EVT.bitsLE(VT) && "Not extending!");
7871 if (EVT == VT) return N1; // Not actually extending
7872 break;
7873 }
7875 case ISD::FP_TO_UINT_SAT: {
7876 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7877 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7878 assert(N1.getValueType().isVector() == VT.isVector() &&
7879 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7880 "vector!");
7881 assert((!VT.isVector() || VT.getVectorElementCount() ==
7883 "Vector element counts must match in FP_TO_*INT_SAT");
7884 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7885 "Type to saturate to must be a scalar.");
7886 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7887 "Not extending!");
7888 break;
7889 }
7892 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7893 element type of the vector.");
7894
7895 // Extract from an undefined value or using an undefined index is undefined.
7896 if (N1.isUndef() || N2.isUndef())
7897 return getUNDEF(VT);
7898
7899 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7900 // vectors. For scalable vectors we will provide appropriate support for
7901 // dealing with arbitrary indices.
7902 if (N2C && N1.getValueType().isFixedLengthVector() &&
7903 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7904 return getUNDEF(VT);
7905
7906 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7907 // expanding copies of large vectors from registers. This only works for
7908 // fixed length vectors, since we need to know the exact number of
7909 // elements.
7910 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7912 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7913 return getExtractVectorElt(DL, VT,
7914 N1.getOperand(N2C->getZExtValue() / Factor),
7915 N2C->getZExtValue() % Factor);
7916 }
7917
7918 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7919 // lowering is expanding large vector constants.
7920 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7921 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7924 "BUILD_VECTOR used for scalable vectors");
7925 unsigned Index =
7926 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7927 SDValue Elt = N1.getOperand(Index);
7928
7929 if (VT != Elt.getValueType())
7930 // If the vector element type is not legal, the BUILD_VECTOR operands
7931 // are promoted and implicitly truncated, and the result implicitly
7932 // extended. Make that explicit here.
7933 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7934
7935 return Elt;
7936 }
7937
7938 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7939 // operations are lowered to scalars.
7940 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7941 // If the indices are the same, return the inserted element else
7942 // if the indices are known different, extract the element from
7943 // the original vector.
7944 SDValue N1Op2 = N1.getOperand(2);
7946
7947 if (N1Op2C && N2C) {
7948 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7949 if (VT == N1.getOperand(1).getValueType())
7950 return N1.getOperand(1);
7951 if (VT.isFloatingPoint()) {
7953 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7954 }
7955 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7956 }
7957 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7958 }
7959 }
7960
7961 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7962 // when vector types are scalarized and v1iX is legal.
7963 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7964 // Here we are completely ignoring the extract element index (N2),
7965 // which is fine for fixed width vectors, since any index other than 0
7966 // is undefined anyway. However, this cannot be ignored for scalable
7967 // vectors - in theory we could support this, but we don't want to do this
7968 // without a profitability check.
7969 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7971 N1.getValueType().getVectorNumElements() == 1) {
7972 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7973 N1.getOperand(1));
7974 }
7975 break;
7977 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7978 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7979 (N1.getValueType().isInteger() == VT.isInteger()) &&
7980 N1.getValueType() != VT &&
7981 "Wrong types for EXTRACT_ELEMENT!");
7982
7983 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7984 // 64-bit integers into 32-bit parts. Instead of building the extract of
7985 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7986 if (N1.getOpcode() == ISD::BUILD_PAIR)
7987 return N1.getOperand(N2C->getZExtValue());
7988
7989 // EXTRACT_ELEMENT of a constant int is also very common.
7990 if (N1C) {
7991 unsigned ElementSize = VT.getSizeInBits();
7992 unsigned Shift = ElementSize * N2C->getZExtValue();
7993 const APInt &Val = N1C->getAPIntValue();
7994 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7995 }
7996 break;
7998 EVT N1VT = N1.getValueType();
7999 assert(VT.isVector() && N1VT.isVector() &&
8000 "Extract subvector VTs must be vectors!");
8002 "Extract subvector VTs must have the same element type!");
8003 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8004 "Cannot extract a scalable vector from a fixed length vector!");
8005 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8007 "Extract subvector must be from larger vector to smaller vector!");
8008 assert(N2C && "Extract subvector index must be a constant");
8009 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8010 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8011 N1VT.getVectorMinNumElements()) &&
8012 "Extract subvector overflow!");
8013 assert(N2C->getAPIntValue().getBitWidth() ==
8014 TLI->getVectorIdxWidth(getDataLayout()) &&
8015 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8016 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8017 "Extract index is not a multiple of the output vector length");
8018
8019 // Trivial extraction.
8020 if (VT == N1VT)
8021 return N1;
8022
8023 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8024 if (N1.isUndef())
8025 return getUNDEF(VT);
8026
8027 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8028 // the concat have the same type as the extract.
8029 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8030 VT == N1.getOperand(0).getValueType()) {
8031 unsigned Factor = VT.getVectorMinNumElements();
8032 return N1.getOperand(N2C->getZExtValue() / Factor);
8033 }
8034
8035 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8036 // during shuffle legalization.
8037 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8038 VT == N1.getOperand(1).getValueType())
8039 return N1.getOperand(1);
8040 break;
8041 }
8042 }
8043
8044 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8045 switch (Opcode) {
8046 case ISD::XOR:
8047 case ISD::ADD:
8048 case ISD::PTRADD:
8049 case ISD::SUB:
8051 case ISD::UDIV:
8052 case ISD::SDIV:
8053 case ISD::UREM:
8054 case ISD::SREM:
8055 case ISD::MUL:
8056 case ISD::AND:
8057 case ISD::SSUBSAT:
8058 case ISD::USUBSAT:
8059 case ISD::UMIN:
8060 case ISD::OR:
8061 case ISD::SADDSAT:
8062 case ISD::UADDSAT:
8063 case ISD::UMAX:
8064 case ISD::SMAX:
8065 case ISD::SMIN:
8066 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8067 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8068 }
8069 }
8070
8071 // Canonicalize an UNDEF to the RHS, even over a constant.
8072 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8073 if (TLI->isCommutativeBinOp(Opcode)) {
8074 std::swap(N1, N2);
8075 } else {
8076 switch (Opcode) {
8077 case ISD::PTRADD:
8078 case ISD::SUB:
8079 // fold op(undef, non_undef_arg2) -> undef.
8080 return N1;
8082 case ISD::UDIV:
8083 case ISD::SDIV:
8084 case ISD::UREM:
8085 case ISD::SREM:
8086 case ISD::SSUBSAT:
8087 case ISD::USUBSAT:
8088 // fold op(undef, non_undef_arg2) -> 0.
8089 return getConstant(0, DL, VT);
8090 }
8091 }
8092 }
8093
8094 // Fold a bunch of operators when the RHS is undef.
8095 if (N2.getOpcode() == ISD::UNDEF) {
8096 switch (Opcode) {
8097 case ISD::XOR:
8098 if (N1.getOpcode() == ISD::UNDEF)
8099 // Handle undef ^ undef -> 0 special case. This is a common
8100 // idiom (misuse).
8101 return getConstant(0, DL, VT);
8102 [[fallthrough]];
8103 case ISD::ADD:
8104 case ISD::PTRADD:
8105 case ISD::SUB:
8106 // fold op(arg1, undef) -> undef.
8107 return N2;
8108 case ISD::UDIV:
8109 case ISD::SDIV:
8110 case ISD::UREM:
8111 case ISD::SREM:
8112 // fold op(arg1, undef) -> poison.
8113 return getPOISON(VT);
8114 case ISD::MUL:
8115 case ISD::AND:
8116 case ISD::SSUBSAT:
8117 case ISD::USUBSAT:
8118 case ISD::UMIN:
8119 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8120 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8121 case ISD::OR:
8122 case ISD::SADDSAT:
8123 case ISD::UADDSAT:
8124 case ISD::UMAX:
8125 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8126 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8127 case ISD::SMAX:
8128 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8129 return N1.getOpcode() == ISD::UNDEF
8130 ? N2
8131 : getConstant(
8133 VT);
8134 case ISD::SMIN:
8135 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8136 return N1.getOpcode() == ISD::UNDEF
8137 ? N2
8138 : getConstant(
8140 VT);
8141 }
8142 }
8143
8144 // Perform trivial constant folding.
8145 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8146 return SV;
8147
8148 // Memoize this node if possible.
8149 SDNode *N;
8150 SDVTList VTs = getVTList(VT);
8151 SDValue Ops[] = {N1, N2};
8152 if (VT != MVT::Glue) {
8154 AddNodeIDNode(ID, Opcode, VTs, Ops);
8155 void *IP = nullptr;
8156 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8157 E->intersectFlagsWith(Flags);
8158 return SDValue(E, 0);
8159 }
8160
8161 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8162 N->setFlags(Flags);
8163 createOperands(N, Ops);
8164 CSEMap.InsertNode(N, IP);
8165 } else {
8166 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8167 createOperands(N, Ops);
8168 }
8169
8170 InsertNode(N);
8171 SDValue V = SDValue(N, 0);
8172 NewSDValueDbgMsg(V, "Creating new node: ", this);
8173 return V;
8174}
8175
8176SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8177 SDValue N1, SDValue N2, SDValue N3) {
8178 SDNodeFlags Flags;
8179 if (Inserter)
8180 Flags = Inserter->getFlags();
8181 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8182}
8183
8184SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8185 SDValue N1, SDValue N2, SDValue N3,
8186 const SDNodeFlags Flags) {
8188 N2.getOpcode() != ISD::DELETED_NODE &&
8189 N3.getOpcode() != ISD::DELETED_NODE &&
8190 "Operand is DELETED_NODE!");
8191 // Perform various simplifications.
8192 switch (Opcode) {
8193 case ISD::BUILD_VECTOR: {
8194 // Attempt to simplify BUILD_VECTOR.
8195 SDValue Ops[] = {N1, N2, N3};
8196 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8197 return V;
8198 break;
8199 }
8200 case ISD::CONCAT_VECTORS: {
8201 SDValue Ops[] = {N1, N2, N3};
8202 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8203 return V;
8204 break;
8205 }
8206 case ISD::SETCC: {
8207 assert(VT.isInteger() && "SETCC result type must be an integer!");
8208 assert(N1.getValueType() == N2.getValueType() &&
8209 "SETCC operands must have the same type!");
8210 assert(VT.isVector() == N1.getValueType().isVector() &&
8211 "SETCC type should be vector iff the operand type is vector!");
8212 assert((!VT.isVector() || VT.getVectorElementCount() ==
8214 "SETCC vector element counts must match!");
8215 // Use FoldSetCC to simplify SETCC's.
8216 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8217 return V;
8218 break;
8219 }
8220 case ISD::SELECT:
8221 case ISD::VSELECT:
8222 if (SDValue V = simplifySelect(N1, N2, N3))
8223 return V;
8224 break;
8226 llvm_unreachable("should use getVectorShuffle constructor!");
8227 case ISD::VECTOR_SPLICE: {
8228 if (cast<ConstantSDNode>(N3)->isZero())
8229 return N1;
8230 break;
8231 }
8233 assert(VT.isVector() && VT == N1.getValueType() &&
8234 "INSERT_VECTOR_ELT vector type mismatch");
8236 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8237 assert((!VT.isFloatingPoint() ||
8238 VT.getVectorElementType() == N2.getValueType()) &&
8239 "INSERT_VECTOR_ELT fp scalar type mismatch");
8240 assert((!VT.isInteger() ||
8242 "INSERT_VECTOR_ELT int scalar size mismatch");
8243
8244 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8245 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8246 // for scalable vectors where we will generate appropriate code to
8247 // deal with out-of-bounds cases correctly.
8248 if (N3C && VT.isFixedLengthVector() &&
8249 N3C->getZExtValue() >= VT.getVectorNumElements())
8250 return getUNDEF(VT);
8251
8252 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8253 if (N3.isUndef())
8254 return getUNDEF(VT);
8255
8256 // If inserting poison, just use the input vector.
8257 if (N2.getOpcode() == ISD::POISON)
8258 return N1;
8259
8260 // Inserting undef into undef/poison is still undef.
8261 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8262 return getUNDEF(VT);
8263
8264 // If the inserted element is an UNDEF, just use the input vector.
8265 // But not if skipping the insert could make the result more poisonous.
8266 if (N2.isUndef()) {
8267 if (N3C && VT.isFixedLengthVector()) {
8268 APInt EltMask =
8269 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8270 if (isGuaranteedNotToBePoison(N1, EltMask))
8271 return N1;
8272 } else if (isGuaranteedNotToBePoison(N1))
8273 return N1;
8274 }
8275 break;
8276 }
8277 case ISD::INSERT_SUBVECTOR: {
8278 // If inserting poison, just use the input vector,
8279 if (N2.getOpcode() == ISD::POISON)
8280 return N1;
8281
8282 // Inserting undef into undef/poison is still undef.
8283 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8284 return getUNDEF(VT);
8285
8286 EVT N2VT = N2.getValueType();
8287 assert(VT == N1.getValueType() &&
8288 "Dest and insert subvector source types must match!");
8289 assert(VT.isVector() && N2VT.isVector() &&
8290 "Insert subvector VTs must be vectors!");
8292 "Insert subvector VTs must have the same element type!");
8293 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8294 "Cannot insert a scalable vector into a fixed length vector!");
8295 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8297 "Insert subvector must be from smaller vector to larger vector!");
8299 "Insert subvector index must be constant");
8300 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8301 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8303 "Insert subvector overflow!");
8305 TLI->getVectorIdxWidth(getDataLayout()) &&
8306 "Constant index for INSERT_SUBVECTOR has an invalid size");
8307
8308 // Trivial insertion.
8309 if (VT == N2VT)
8310 return N2;
8311
8312 // If this is an insert of an extracted vector into an undef/poison vector,
8313 // we can just use the input to the extract. But not if skipping the
8314 // extract+insert could make the result more poisonous.
8315 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8316 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8317 if (N1.getOpcode() == ISD::POISON)
8318 return N2.getOperand(0);
8319 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8320 unsigned LoBit = N3->getAsZExtVal();
8321 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8322 APInt EltMask =
8323 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8324 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8325 return N2.getOperand(0);
8326 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8327 return N2.getOperand(0);
8328 }
8329
8330 // If the inserted subvector is UNDEF, just use the input vector.
8331 // But not if skipping the insert could make the result more poisonous.
8332 if (N2.isUndef()) {
8333 if (VT.isFixedLengthVector()) {
8334 unsigned LoBit = N3->getAsZExtVal();
8335 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8336 APInt EltMask =
8337 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8338 if (isGuaranteedNotToBePoison(N1, EltMask))
8339 return N1;
8340 } else if (isGuaranteedNotToBePoison(N1))
8341 return N1;
8342 }
8343 break;
8344 }
8345 case ISD::BITCAST:
8346 // Fold bit_convert nodes from a type to themselves.
8347 if (N1.getValueType() == VT)
8348 return N1;
8349 break;
8350 case ISD::VP_TRUNCATE:
8351 case ISD::VP_SIGN_EXTEND:
8352 case ISD::VP_ZERO_EXTEND:
8353 // Don't create noop casts.
8354 if (N1.getValueType() == VT)
8355 return N1;
8356 break;
8357 case ISD::VECTOR_COMPRESS: {
8358 [[maybe_unused]] EVT VecVT = N1.getValueType();
8359 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8360 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8361 assert(VT == VecVT && "Vector and result type don't match.");
8362 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8363 "All inputs must be vectors.");
8364 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8366 "Vector and mask must have same number of elements.");
8367
8368 if (N1.isUndef() || N2.isUndef())
8369 return N3;
8370
8371 break;
8372 }
8373 case ISD::PARTIAL_REDUCE_UMLA:
8374 case ISD::PARTIAL_REDUCE_SMLA:
8375 case ISD::PARTIAL_REDUCE_SUMLA: {
8376 [[maybe_unused]] EVT AccVT = N1.getValueType();
8377 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8378 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8379 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8380 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8381 "node to have the same type!");
8382 assert(VT.isVector() && VT == AccVT &&
8383 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8384 "the same type as its result!");
8386 AccVT.getVectorElementCount()) &&
8387 "Expected the element count of the second and third operands of the "
8388 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8389 "element count of the first operand and the result!");
8391 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8392 "node to have an element type which is the same as or smaller than "
8393 "the element type of the first operand and result!");
8394 break;
8395 }
8396 }
8397
8398 // Perform trivial constant folding for arithmetic operators.
8399 switch (Opcode) {
8400 case ISD::FMA:
8401 case ISD::FMAD:
8402 case ISD::SETCC:
8403 case ISD::FSHL:
8404 case ISD::FSHR:
8405 if (SDValue SV =
8406 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8407 return SV;
8408 break;
8409 }
8410
8411 // Memoize node if it doesn't produce a glue result.
8412 SDNode *N;
8413 SDVTList VTs = getVTList(VT);
8414 SDValue Ops[] = {N1, N2, N3};
8415 if (VT != MVT::Glue) {
8417 AddNodeIDNode(ID, Opcode, VTs, Ops);
8418 void *IP = nullptr;
8419 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8420 E->intersectFlagsWith(Flags);
8421 return SDValue(E, 0);
8422 }
8423
8424 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8425 N->setFlags(Flags);
8426 createOperands(N, Ops);
8427 CSEMap.InsertNode(N, IP);
8428 } else {
8429 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8430 createOperands(N, Ops);
8431 }
8432
8433 InsertNode(N);
8434 SDValue V = SDValue(N, 0);
8435 NewSDValueDbgMsg(V, "Creating new node: ", this);
8436 return V;
8437}
8438
8439SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8440 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8441 const SDNodeFlags Flags) {
8442 SDValue Ops[] = { N1, N2, N3, N4 };
8443 return getNode(Opcode, DL, VT, Ops, Flags);
8444}
8445
8446SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8447 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8448 SDNodeFlags Flags;
8449 if (Inserter)
8450 Flags = Inserter->getFlags();
8451 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8452}
8453
8454SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8455 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8456 SDValue N5, const SDNodeFlags Flags) {
8457 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8458 return getNode(Opcode, DL, VT, Ops, Flags);
8459}
8460
8461SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8462 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8463 SDValue N5) {
8464 SDNodeFlags Flags;
8465 if (Inserter)
8466 Flags = Inserter->getFlags();
8467 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8468}
8469
8470/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8471/// the incoming stack arguments to be loaded from the stack.
8473 SmallVector<SDValue, 8> ArgChains;
8474
8475 // Include the original chain at the beginning of the list. When this is
8476 // used by target LowerCall hooks, this helps legalize find the
8477 // CALLSEQ_BEGIN node.
8478 ArgChains.push_back(Chain);
8479
8480 // Add a chain value for each stack argument.
8481 for (SDNode *U : getEntryNode().getNode()->users())
8482 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8483 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8484 if (FI->getIndex() < 0)
8485 ArgChains.push_back(SDValue(L, 1));
8486
8487 // Build a tokenfactor for all the chains.
8488 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8489}
8490
8491/// getMemsetValue - Vectorized representation of the memset value
8492/// operand.
8494 const SDLoc &dl) {
8495 assert(!Value.isUndef());
8496
8497 unsigned NumBits = VT.getScalarSizeInBits();
8499 assert(C->getAPIntValue().getBitWidth() == 8);
8500 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8501 if (VT.isInteger()) {
8502 bool IsOpaque = VT.getSizeInBits() > 64 ||
8503 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8504 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8505 }
8506 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8507 }
8508
8509 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8510 EVT IntVT = VT.getScalarType();
8511 if (!IntVT.isInteger())
8512 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8513
8514 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8515 if (NumBits > 8) {
8516 // Use a multiplication with 0x010101... to extend the input to the
8517 // required length.
8518 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8519 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8520 DAG.getConstant(Magic, dl, IntVT));
8521 }
8522
8523 if (VT != Value.getValueType() && !VT.isInteger())
8524 Value = DAG.getBitcast(VT.getScalarType(), Value);
8525 if (VT != Value.getValueType())
8526 Value = DAG.getSplatBuildVector(VT, dl, Value);
8527
8528 return Value;
8529}
8530
8531/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8532/// used when a memcpy is turned into a memset when the source is a constant
8533/// string ptr.
8535 const TargetLowering &TLI,
8536 const ConstantDataArraySlice &Slice) {
8537 // Handle vector with all elements zero.
8538 if (Slice.Array == nullptr) {
8539 if (VT.isInteger())
8540 return DAG.getConstant(0, dl, VT);
8541 return DAG.getNode(ISD::BITCAST, dl, VT,
8542 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8543 }
8544
8545 assert(!VT.isVector() && "Can't handle vector type here!");
8546 unsigned NumVTBits = VT.getSizeInBits();
8547 unsigned NumVTBytes = NumVTBits / 8;
8548 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8549
8550 APInt Val(NumVTBits, 0);
8551 if (DAG.getDataLayout().isLittleEndian()) {
8552 for (unsigned i = 0; i != NumBytes; ++i)
8553 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8554 } else {
8555 for (unsigned i = 0; i != NumBytes; ++i)
8556 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8557 }
8558
8559 // If the "cost" of materializing the integer immediate is less than the cost
8560 // of a load, then it is cost effective to turn the load into the immediate.
8561 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8562 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8563 return DAG.getConstant(Val, dl, VT);
8564 return SDValue();
8565}
8566
8568 const SDLoc &DL,
8569 const SDNodeFlags Flags) {
8570 EVT VT = Base.getValueType();
8571 SDValue Index;
8572
8573 if (Offset.isScalable())
8574 Index = getVScale(DL, Base.getValueType(),
8575 APInt(Base.getValueSizeInBits().getFixedValue(),
8576 Offset.getKnownMinValue()));
8577 else
8578 Index = getConstant(Offset.getFixedValue(), DL, VT);
8579
8580 return getMemBasePlusOffset(Base, Index, DL, Flags);
8581}
8582
8584 const SDLoc &DL,
8585 const SDNodeFlags Flags) {
8586 assert(Offset.getValueType().isInteger());
8587 EVT BasePtrVT = Ptr.getValueType();
8588 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8589 BasePtrVT))
8590 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8591 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
8592}
8593
8594/// Returns true if memcpy source is constant data.
8596 uint64_t SrcDelta = 0;
8597 GlobalAddressSDNode *G = nullptr;
8598 if (Src.getOpcode() == ISD::GlobalAddress)
8600 else if (Src->isAnyAdd() &&
8601 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8602 Src.getOperand(1).getOpcode() == ISD::Constant) {
8603 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8604 SrcDelta = Src.getConstantOperandVal(1);
8605 }
8606 if (!G)
8607 return false;
8608
8609 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8610 SrcDelta + G->getOffset());
8611}
8612
8614 SelectionDAG &DAG) {
8615 // On Darwin, -Os means optimize for size without hurting performance, so
8616 // only really optimize for size when -Oz (MinSize) is used.
8618 return MF.getFunction().hasMinSize();
8619 return DAG.shouldOptForSize();
8620}
8621
8623 SmallVector<SDValue, 32> &OutChains, unsigned From,
8624 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8625 SmallVector<SDValue, 16> &OutStoreChains) {
8626 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8627 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8628 SmallVector<SDValue, 16> GluedLoadChains;
8629 for (unsigned i = From; i < To; ++i) {
8630 OutChains.push_back(OutLoadChains[i]);
8631 GluedLoadChains.push_back(OutLoadChains[i]);
8632 }
8633
8634 // Chain for all loads.
8635 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8636 GluedLoadChains);
8637
8638 for (unsigned i = From; i < To; ++i) {
8639 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8640 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8641 ST->getBasePtr(), ST->getMemoryVT(),
8642 ST->getMemOperand());
8643 OutChains.push_back(NewStore);
8644 }
8645}
8646
8648 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8649 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8650 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8651 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8652 // Turn a memcpy of undef to nop.
8653 // FIXME: We need to honor volatile even is Src is undef.
8654 if (Src.isUndef())
8655 return Chain;
8656
8657 // Expand memcpy to a series of load and store ops if the size operand falls
8658 // below a certain threshold.
8659 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8660 // rather than maybe a humongous number of loads and stores.
8661 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8662 const DataLayout &DL = DAG.getDataLayout();
8663 LLVMContext &C = *DAG.getContext();
8664 std::vector<EVT> MemOps;
8665 bool DstAlignCanChange = false;
8667 MachineFrameInfo &MFI = MF.getFrameInfo();
8668 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8670 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8671 DstAlignCanChange = true;
8672 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8673 if (!SrcAlign || Alignment > *SrcAlign)
8674 SrcAlign = Alignment;
8675 assert(SrcAlign && "SrcAlign must be set");
8677 // If marked as volatile, perform a copy even when marked as constant.
8678 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8679 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8680 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8681 const MemOp Op = isZeroConstant
8682 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8683 /*IsZeroMemset*/ true, isVol)
8684 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8685 *SrcAlign, isVol, CopyFromConstant);
8686 if (!TLI.findOptimalMemOpLowering(
8687 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8688 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8689 return SDValue();
8690
8691 if (DstAlignCanChange) {
8692 Type *Ty = MemOps[0].getTypeForEVT(C);
8693 Align NewAlign = DL.getABITypeAlign(Ty);
8694
8695 // Don't promote to an alignment that would require dynamic stack
8696 // realignment which may conflict with optimizations such as tail call
8697 // optimization.
8699 if (!TRI->hasStackRealignment(MF))
8700 if (MaybeAlign StackAlign = DL.getStackAlignment())
8701 NewAlign = std::min(NewAlign, *StackAlign);
8702
8703 if (NewAlign > Alignment) {
8704 // Give the stack frame object a larger alignment if needed.
8705 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8706 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8707 Alignment = NewAlign;
8708 }
8709 }
8710
8711 // Prepare AAInfo for loads/stores after lowering this memcpy.
8712 AAMDNodes NewAAInfo = AAInfo;
8713 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8714
8715 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8716 bool isConstant =
8717 BatchAA && SrcVal &&
8718 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8719
8720 MachineMemOperand::Flags MMOFlags =
8722 SmallVector<SDValue, 16> OutLoadChains;
8723 SmallVector<SDValue, 16> OutStoreChains;
8724 SmallVector<SDValue, 32> OutChains;
8725 unsigned NumMemOps = MemOps.size();
8726 uint64_t SrcOff = 0, DstOff = 0;
8727 for (unsigned i = 0; i != NumMemOps; ++i) {
8728 EVT VT = MemOps[i];
8729 unsigned VTSize = VT.getSizeInBits() / 8;
8730 SDValue Value, Store;
8731
8732 if (VTSize > Size) {
8733 // Issuing an unaligned load / store pair that overlaps with the previous
8734 // pair. Adjust the offset accordingly.
8735 assert(i == NumMemOps-1 && i != 0);
8736 SrcOff -= VTSize - Size;
8737 DstOff -= VTSize - Size;
8738 }
8739
8740 if (CopyFromConstant &&
8741 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8742 // It's unlikely a store of a vector immediate can be done in a single
8743 // instruction. It would require a load from a constantpool first.
8744 // We only handle zero vectors here.
8745 // FIXME: Handle other cases where store of vector immediate is done in
8746 // a single instruction.
8747 ConstantDataArraySlice SubSlice;
8748 if (SrcOff < Slice.Length) {
8749 SubSlice = Slice;
8750 SubSlice.move(SrcOff);
8751 } else {
8752 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8753 SubSlice.Array = nullptr;
8754 SubSlice.Offset = 0;
8755 SubSlice.Length = VTSize;
8756 }
8757 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8758 if (Value.getNode()) {
8759 Store = DAG.getStore(
8760 Chain, dl, Value,
8761 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8762 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8763 OutChains.push_back(Store);
8764 }
8765 }
8766
8767 if (!Store.getNode()) {
8768 // The type might not be legal for the target. This should only happen
8769 // if the type is smaller than a legal type, as on PPC, so the right
8770 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8771 // to Load/Store if NVT==VT.
8772 // FIXME does the case above also need this?
8773 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8774 assert(NVT.bitsGE(VT));
8775
8776 bool isDereferenceable =
8777 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8778 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8779 if (isDereferenceable)
8781 if (isConstant)
8782 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8783
8784 Value = DAG.getExtLoad(
8785 ISD::EXTLOAD, dl, NVT, Chain,
8786 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8787 SrcPtrInfo.getWithOffset(SrcOff), VT,
8788 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8789 OutLoadChains.push_back(Value.getValue(1));
8790
8791 Store = DAG.getTruncStore(
8792 Chain, dl, Value,
8793 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8794 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8795 OutStoreChains.push_back(Store);
8796 }
8797 SrcOff += VTSize;
8798 DstOff += VTSize;
8799 Size -= VTSize;
8800 }
8801
8802 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8804 unsigned NumLdStInMemcpy = OutStoreChains.size();
8805
8806 if (NumLdStInMemcpy) {
8807 // It may be that memcpy might be converted to memset if it's memcpy
8808 // of constants. In such a case, we won't have loads and stores, but
8809 // just stores. In the absence of loads, there is nothing to gang up.
8810 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8811 // If target does not care, just leave as it.
8812 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8813 OutChains.push_back(OutLoadChains[i]);
8814 OutChains.push_back(OutStoreChains[i]);
8815 }
8816 } else {
8817 // Ld/St less than/equal limit set by target.
8818 if (NumLdStInMemcpy <= GluedLdStLimit) {
8819 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8820 NumLdStInMemcpy, OutLoadChains,
8821 OutStoreChains);
8822 } else {
8823 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8824 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8825 unsigned GlueIter = 0;
8826
8827 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8828 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8829 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8830
8831 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8832 OutLoadChains, OutStoreChains);
8833 GlueIter += GluedLdStLimit;
8834 }
8835
8836 // Residual ld/st.
8837 if (RemainingLdStInMemcpy) {
8838 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8839 RemainingLdStInMemcpy, OutLoadChains,
8840 OutStoreChains);
8841 }
8842 }
8843 }
8844 }
8845 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8846}
8847
8849 SDValue Chain, SDValue Dst, SDValue Src,
8850 uint64_t Size, Align Alignment,
8851 bool isVol, bool AlwaysInline,
8852 MachinePointerInfo DstPtrInfo,
8853 MachinePointerInfo SrcPtrInfo,
8854 const AAMDNodes &AAInfo) {
8855 // Turn a memmove of undef to nop.
8856 // FIXME: We need to honor volatile even is Src is undef.
8857 if (Src.isUndef())
8858 return Chain;
8859
8860 // Expand memmove to a series of load and store ops if the size operand falls
8861 // below a certain threshold.
8862 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8863 const DataLayout &DL = DAG.getDataLayout();
8864 LLVMContext &C = *DAG.getContext();
8865 std::vector<EVT> MemOps;
8866 bool DstAlignCanChange = false;
8868 MachineFrameInfo &MFI = MF.getFrameInfo();
8869 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8871 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8872 DstAlignCanChange = true;
8873 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8874 if (!SrcAlign || Alignment > *SrcAlign)
8875 SrcAlign = Alignment;
8876 assert(SrcAlign && "SrcAlign must be set");
8877 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8878 if (!TLI.findOptimalMemOpLowering(
8879 C, MemOps, Limit,
8880 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8881 /*IsVolatile*/ true),
8882 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8883 MF.getFunction().getAttributes()))
8884 return SDValue();
8885
8886 if (DstAlignCanChange) {
8887 Type *Ty = MemOps[0].getTypeForEVT(C);
8888 Align NewAlign = DL.getABITypeAlign(Ty);
8889
8890 // Don't promote to an alignment that would require dynamic stack
8891 // realignment which may conflict with optimizations such as tail call
8892 // optimization.
8894 if (!TRI->hasStackRealignment(MF))
8895 if (MaybeAlign StackAlign = DL.getStackAlignment())
8896 NewAlign = std::min(NewAlign, *StackAlign);
8897
8898 if (NewAlign > Alignment) {
8899 // Give the stack frame object a larger alignment if needed.
8900 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8901 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8902 Alignment = NewAlign;
8903 }
8904 }
8905
8906 // Prepare AAInfo for loads/stores after lowering this memmove.
8907 AAMDNodes NewAAInfo = AAInfo;
8908 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8909
8910 MachineMemOperand::Flags MMOFlags =
8912 uint64_t SrcOff = 0, DstOff = 0;
8913 SmallVector<SDValue, 8> LoadValues;
8914 SmallVector<SDValue, 8> LoadChains;
8915 SmallVector<SDValue, 8> OutChains;
8916 unsigned NumMemOps = MemOps.size();
8917 for (unsigned i = 0; i < NumMemOps; i++) {
8918 EVT VT = MemOps[i];
8919 unsigned VTSize = VT.getSizeInBits() / 8;
8920 SDValue Value;
8921
8922 bool isDereferenceable =
8923 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8924 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8925 if (isDereferenceable)
8927
8928 Value = DAG.getLoad(
8929 VT, dl, Chain,
8930 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8931 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8932 LoadValues.push_back(Value);
8933 LoadChains.push_back(Value.getValue(1));
8934 SrcOff += VTSize;
8935 }
8936 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8937 OutChains.clear();
8938 for (unsigned i = 0; i < NumMemOps; i++) {
8939 EVT VT = MemOps[i];
8940 unsigned VTSize = VT.getSizeInBits() / 8;
8941 SDValue Store;
8942
8943 Store = DAG.getStore(
8944 Chain, dl, LoadValues[i],
8945 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8946 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8947 OutChains.push_back(Store);
8948 DstOff += VTSize;
8949 }
8950
8951 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8952}
8953
8954/// Lower the call to 'memset' intrinsic function into a series of store
8955/// operations.
8956///
8957/// \param DAG Selection DAG where lowered code is placed.
8958/// \param dl Link to corresponding IR location.
8959/// \param Chain Control flow dependency.
8960/// \param Dst Pointer to destination memory location.
8961/// \param Src Value of byte to write into the memory.
8962/// \param Size Number of bytes to write.
8963/// \param Alignment Alignment of the destination in bytes.
8964/// \param isVol True if destination is volatile.
8965/// \param AlwaysInline Makes sure no function call is generated.
8966/// \param DstPtrInfo IR information on the memory pointer.
8967/// \returns New head in the control flow, if lowering was successful, empty
8968/// SDValue otherwise.
8969///
8970/// The function tries to replace 'llvm.memset' intrinsic with several store
8971/// operations and value calculation code. This is usually profitable for small
8972/// memory size or when the semantic requires inlining.
8974 SDValue Chain, SDValue Dst, SDValue Src,
8975 uint64_t Size, Align Alignment, bool isVol,
8976 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8977 const AAMDNodes &AAInfo) {
8978 // Turn a memset of undef to nop.
8979 // FIXME: We need to honor volatile even is Src is undef.
8980 if (Src.isUndef())
8981 return Chain;
8982
8983 // Expand memset to a series of load/store ops if the size operand
8984 // falls below a certain threshold.
8985 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8986 std::vector<EVT> MemOps;
8987 bool DstAlignCanChange = false;
8988 LLVMContext &C = *DAG.getContext();
8990 MachineFrameInfo &MFI = MF.getFrameInfo();
8991 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8993 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8994 DstAlignCanChange = true;
8995 bool IsZeroVal = isNullConstant(Src);
8996 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8997
8998 if (!TLI.findOptimalMemOpLowering(
8999 C, MemOps, Limit,
9000 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9001 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
9002 return SDValue();
9003
9004 if (DstAlignCanChange) {
9005 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9006 const DataLayout &DL = DAG.getDataLayout();
9007 Align NewAlign = DL.getABITypeAlign(Ty);
9008
9009 // Don't promote to an alignment that would require dynamic stack
9010 // realignment which may conflict with optimizations such as tail call
9011 // optimization.
9013 if (!TRI->hasStackRealignment(MF))
9014 if (MaybeAlign StackAlign = DL.getStackAlignment())
9015 NewAlign = std::min(NewAlign, *StackAlign);
9016
9017 if (NewAlign > Alignment) {
9018 // Give the stack frame object a larger alignment if needed.
9019 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9020 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9021 Alignment = NewAlign;
9022 }
9023 }
9024
9025 SmallVector<SDValue, 8> OutChains;
9026 uint64_t DstOff = 0;
9027 unsigned NumMemOps = MemOps.size();
9028
9029 // Find the largest store and generate the bit pattern for it.
9030 EVT LargestVT = MemOps[0];
9031 for (unsigned i = 1; i < NumMemOps; i++)
9032 if (MemOps[i].bitsGT(LargestVT))
9033 LargestVT = MemOps[i];
9034 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9035
9036 // Prepare AAInfo for loads/stores after lowering this memset.
9037 AAMDNodes NewAAInfo = AAInfo;
9038 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9039
9040 for (unsigned i = 0; i < NumMemOps; i++) {
9041 EVT VT = MemOps[i];
9042 unsigned VTSize = VT.getSizeInBits() / 8;
9043 if (VTSize > Size) {
9044 // Issuing an unaligned load / store pair that overlaps with the previous
9045 // pair. Adjust the offset accordingly.
9046 assert(i == NumMemOps-1 && i != 0);
9047 DstOff -= VTSize - Size;
9048 }
9049
9050 // If this store is smaller than the largest store see whether we can get
9051 // the smaller value for free with a truncate or extract vector element and
9052 // then store.
9053 SDValue Value = MemSetValue;
9054 if (VT.bitsLT(LargestVT)) {
9055 unsigned Index;
9056 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9057 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9058 if (!LargestVT.isVector() && !VT.isVector() &&
9059 TLI.isTruncateFree(LargestVT, VT))
9060 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9061 else if (LargestVT.isVector() && !VT.isVector() &&
9063 LargestVT.getTypeForEVT(*DAG.getContext()),
9064 VT.getSizeInBits(), Index) &&
9065 TLI.isTypeLegal(SVT) &&
9066 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9067 // Target which can combine store(extractelement VectorTy, Idx) can get
9068 // the smaller value for free.
9069 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9070 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9071 } else
9072 Value = getMemsetValue(Src, VT, DAG, dl);
9073 }
9074 assert(Value.getValueType() == VT && "Value with wrong type.");
9075 SDValue Store = DAG.getStore(
9076 Chain, dl, Value,
9077 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
9078 DstPtrInfo.getWithOffset(DstOff), Alignment,
9080 NewAAInfo);
9081 OutChains.push_back(Store);
9082 DstOff += VT.getSizeInBits() / 8;
9083 Size -= VTSize;
9084 }
9085
9086 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9087}
9088
9090 unsigned AS) {
9091 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9092 // pointer operands can be losslessly bitcasted to pointers of address space 0
9093 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9094 report_fatal_error("cannot lower memory intrinsic in address space " +
9095 Twine(AS));
9096 }
9097}
9098
9100 const SelectionDAG *SelDAG,
9101 bool AllowReturnsFirstArg) {
9102 if (!CI || !CI->isTailCall())
9103 return false;
9104 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9105 // helper symbol we lower to.
9106 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9107 AllowReturnsFirstArg &&
9109}
9110
9111std::pair<SDValue, SDValue>
9113 SDValue Mem1, SDValue Size, const CallInst *CI) {
9114 const char *LibCallName = TLI->getLibcallName(RTLIB::MEMCMP);
9115 if (!LibCallName)
9116 return {};
9117
9120 {Mem0, PT},
9121 {Mem1, PT},
9123
9125 bool IsTailCall =
9126 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9127
9128 CLI.setDebugLoc(dl)
9129 .setChain(Chain)
9130 .setLibCallee(
9131 TLI->getLibcallCallingConv(RTLIB::MEMCMP),
9133 getExternalSymbol(LibCallName, TLI->getPointerTy(getDataLayout())),
9134 std::move(Args))
9135 .setTailCall(IsTailCall);
9136
9137 return TLI->LowerCallTo(CLI);
9138}
9139
9140std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9141 const SDLoc &dl,
9142 SDValue Src,
9143 const CallInst *CI) {
9144 const char *LibCallName = TLI->getLibcallName(RTLIB::STRLEN);
9145 if (!LibCallName)
9146 return {};
9147
9148 // Emit a library call.
9151
9153 bool IsTailCall =
9154 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9155
9156 CLI.setDebugLoc(dl)
9157 .setChain(Chain)
9158 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::STRLEN), CI->getType(),
9160 LibCallName, TLI->getProgramPointerTy(getDataLayout())),
9161 std::move(Args))
9162 .setTailCall(IsTailCall);
9163
9164 return TLI->LowerCallTo(CLI);
9165}
9166
9168 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9169 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9170 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9171 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9172 BatchAAResults *BatchAA) {
9173 // Check to see if we should lower the memcpy to loads and stores first.
9174 // For cases within the target-specified limits, this is the best choice.
9176 if (ConstantSize) {
9177 // Memcpy with size zero? Just return the original chain.
9178 if (ConstantSize->isZero())
9179 return Chain;
9180
9182 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9183 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9184 if (Result.getNode())
9185 return Result;
9186 }
9187
9188 // Then check to see if we should lower the memcpy with target-specific
9189 // code. If the target chooses to do this, this is the next best.
9190 if (TSI) {
9191 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9192 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9193 DstPtrInfo, SrcPtrInfo);
9194 if (Result.getNode())
9195 return Result;
9196 }
9197
9198 // If we really need inline code and the target declined to provide it,
9199 // use a (potentially long) sequence of loads and stores.
9200 if (AlwaysInline) {
9201 assert(ConstantSize && "AlwaysInline requires a constant size!");
9203 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9204 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9205 }
9206
9209
9210 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9211 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9212 // respect volatile, so they may do things like read or write memory
9213 // beyond the given memory regions. But fixing this isn't easy, and most
9214 // people don't care.
9215
9216 // Emit a library call.
9219 Args.emplace_back(Dst, PtrTy);
9220 Args.emplace_back(Src, PtrTy);
9221 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9222 // FIXME: pass in SDLoc
9224 bool IsTailCall = false;
9225 const char *MemCpyName = TLI->getMemcpyName();
9226
9227 if (OverrideTailCall.has_value()) {
9228 IsTailCall = *OverrideTailCall;
9229 } else {
9230 bool LowersToMemcpy = StringRef(MemCpyName) == StringRef("memcpy");
9231 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9232 }
9233
9234 CLI.setDebugLoc(dl)
9235 .setChain(Chain)
9236 .setLibCallee(
9237 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
9238 Dst.getValueType().getTypeForEVT(*getContext()),
9239 getExternalSymbol(MemCpyName, TLI->getPointerTy(getDataLayout())),
9240 std::move(Args))
9242 .setTailCall(IsTailCall);
9243
9244 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9245 return CallResult.second;
9246}
9247
9249 SDValue Dst, SDValue Src, SDValue Size,
9250 Type *SizeTy, unsigned ElemSz,
9251 bool isTailCall,
9252 MachinePointerInfo DstPtrInfo,
9253 MachinePointerInfo SrcPtrInfo) {
9254 // Emit a library call.
9257 Args.emplace_back(Dst, ArgTy);
9258 Args.emplace_back(Src, ArgTy);
9259 Args.emplace_back(Size, SizeTy);
9260
9261 RTLIB::Libcall LibraryCall =
9263 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9264 report_fatal_error("Unsupported element size");
9265
9267 CLI.setDebugLoc(dl)
9268 .setChain(Chain)
9269 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9271 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9272 TLI->getPointerTy(getDataLayout())),
9273 std::move(Args))
9275 .setTailCall(isTailCall);
9276
9277 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9278 return CallResult.second;
9279}
9280
9282 SDValue Src, SDValue Size, Align Alignment,
9283 bool isVol, const CallInst *CI,
9284 std::optional<bool> OverrideTailCall,
9285 MachinePointerInfo DstPtrInfo,
9286 MachinePointerInfo SrcPtrInfo,
9287 const AAMDNodes &AAInfo,
9288 BatchAAResults *BatchAA) {
9289 // Check to see if we should lower the memmove to loads and stores first.
9290 // For cases within the target-specified limits, this is the best choice.
9292 if (ConstantSize) {
9293 // Memmove with size zero? Just return the original chain.
9294 if (ConstantSize->isZero())
9295 return Chain;
9296
9298 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9299 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9300 if (Result.getNode())
9301 return Result;
9302 }
9303
9304 // Then check to see if we should lower the memmove with target-specific
9305 // code. If the target chooses to do this, this is the next best.
9306 if (TSI) {
9307 SDValue Result =
9308 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9309 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9310 if (Result.getNode())
9311 return Result;
9312 }
9313
9316
9317 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9318 // not be safe. See memcpy above for more details.
9319
9320 // Emit a library call.
9323 Args.emplace_back(Dst, PtrTy);
9324 Args.emplace_back(Src, PtrTy);
9325 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9326 // FIXME: pass in SDLoc
9328
9329 bool IsTailCall = false;
9330 if (OverrideTailCall.has_value()) {
9331 IsTailCall = *OverrideTailCall;
9332 } else {
9333 bool LowersToMemmove =
9334 TLI->getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove");
9335 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9336 }
9337
9338 CLI.setDebugLoc(dl)
9339 .setChain(Chain)
9340 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
9341 Dst.getValueType().getTypeForEVT(*getContext()),
9342 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
9343 TLI->getPointerTy(getDataLayout())),
9344 std::move(Args))
9346 .setTailCall(IsTailCall);
9347
9348 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9349 return CallResult.second;
9350}
9351
9353 SDValue Dst, SDValue Src, SDValue Size,
9354 Type *SizeTy, unsigned ElemSz,
9355 bool isTailCall,
9356 MachinePointerInfo DstPtrInfo,
9357 MachinePointerInfo SrcPtrInfo) {
9358 // Emit a library call.
9360 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9361 Args.emplace_back(Dst, IntPtrTy);
9362 Args.emplace_back(Src, IntPtrTy);
9363 Args.emplace_back(Size, SizeTy);
9364
9365 RTLIB::Libcall LibraryCall =
9367 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9368 report_fatal_error("Unsupported element size");
9369
9371 CLI.setDebugLoc(dl)
9372 .setChain(Chain)
9373 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9375 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9376 TLI->getPointerTy(getDataLayout())),
9377 std::move(Args))
9379 .setTailCall(isTailCall);
9380
9381 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9382 return CallResult.second;
9383}
9384
9386 SDValue Src, SDValue Size, Align Alignment,
9387 bool isVol, bool AlwaysInline,
9388 const CallInst *CI,
9389 MachinePointerInfo DstPtrInfo,
9390 const AAMDNodes &AAInfo) {
9391 // Check to see if we should lower the memset to stores first.
9392 // For cases within the target-specified limits, this is the best choice.
9394 if (ConstantSize) {
9395 // Memset with size zero? Just return the original chain.
9396 if (ConstantSize->isZero())
9397 return Chain;
9398
9399 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9400 ConstantSize->getZExtValue(), Alignment,
9401 isVol, false, DstPtrInfo, AAInfo);
9402
9403 if (Result.getNode())
9404 return Result;
9405 }
9406
9407 // Then check to see if we should lower the memset with target-specific
9408 // code. If the target chooses to do this, this is the next best.
9409 if (TSI) {
9410 SDValue Result = TSI->EmitTargetCodeForMemset(
9411 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9412 if (Result.getNode())
9413 return Result;
9414 }
9415
9416 // If we really need inline code and the target declined to provide it,
9417 // use a (potentially long) sequence of loads and stores.
9418 if (AlwaysInline) {
9419 assert(ConstantSize && "AlwaysInline requires a constant size!");
9420 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9421 ConstantSize->getZExtValue(), Alignment,
9422 isVol, true, DstPtrInfo, AAInfo);
9423 assert(Result &&
9424 "getMemsetStores must return a valid sequence when AlwaysInline");
9425 return Result;
9426 }
9427
9429
9430 // Emit a library call.
9431 auto &Ctx = *getContext();
9432 const auto& DL = getDataLayout();
9433
9435 // FIXME: pass in SDLoc
9436 CLI.setDebugLoc(dl).setChain(Chain);
9437
9438 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
9439
9440 bool UseBZero = isNullConstant(Src) && BzeroName;
9441 // If zeroing out and bzero is present, use it.
9442 if (UseBZero) {
9444 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9445 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9446 CLI.setLibCallee(
9447 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
9448 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
9449 } else {
9451 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9452 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9453 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9454 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
9455 Dst.getValueType().getTypeForEVT(Ctx),
9456 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
9457 TLI->getPointerTy(DL)),
9458 std::move(Args));
9459 }
9460 bool LowersToMemset =
9461 TLI->getLibcallName(RTLIB::MEMSET) == StringRef("memset");
9462 // If we're going to use bzero, make sure not to tail call unless the
9463 // subsequent return doesn't need a value, as bzero doesn't return the first
9464 // arg unlike memset.
9465 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9466 bool IsTailCall =
9467 CI && CI->isTailCall() &&
9468 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9469 CLI.setDiscardResult().setTailCall(IsTailCall);
9470
9471 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9472 return CallResult.second;
9473}
9474
9477 Type *SizeTy, unsigned ElemSz,
9478 bool isTailCall,
9479 MachinePointerInfo DstPtrInfo) {
9480 // Emit a library call.
9482 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9483 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9484 Args.emplace_back(Size, SizeTy);
9485
9486 RTLIB::Libcall LibraryCall =
9488 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9489 report_fatal_error("Unsupported element size");
9490
9492 CLI.setDebugLoc(dl)
9493 .setChain(Chain)
9494 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9496 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9497 TLI->getPointerTy(getDataLayout())),
9498 std::move(Args))
9500 .setTailCall(isTailCall);
9501
9502 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9503 return CallResult.second;
9504}
9505
9506SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9508 MachineMemOperand *MMO,
9509 ISD::LoadExtType ExtType) {
9511 AddNodeIDNode(ID, Opcode, VTList, Ops);
9512 ID.AddInteger(MemVT.getRawBits());
9513 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9514 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9515 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9516 ID.AddInteger(MMO->getFlags());
9517 void* IP = nullptr;
9518 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9519 E->refineAlignment(MMO);
9520 E->refineRanges(MMO);
9521 return SDValue(E, 0);
9522 }
9523
9524 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9525 VTList, MemVT, MMO, ExtType);
9526 createOperands(N, Ops);
9527
9528 CSEMap.InsertNode(N, IP);
9529 InsertNode(N);
9530 SDValue V(N, 0);
9531 NewSDValueDbgMsg(V, "Creating new node: ", this);
9532 return V;
9533}
9534
9536 EVT MemVT, SDVTList VTs, SDValue Chain,
9537 SDValue Ptr, SDValue Cmp, SDValue Swp,
9538 MachineMemOperand *MMO) {
9539 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9540 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
9541 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9542
9543 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9544 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9545}
9546
9547SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9548 SDValue Chain, SDValue Ptr, SDValue Val,
9549 MachineMemOperand *MMO) {
9550 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9551 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9552 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9553 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9554 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9555 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9556 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9557 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9558 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9559 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9560 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9561 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9562 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9563 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9564 Opcode == ISD::ATOMIC_STORE) &&
9565 "Invalid Atomic Op");
9566
9567 EVT VT = Val.getValueType();
9568
9569 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9570 getVTList(VT, MVT::Other);
9571 SDValue Ops[] = {Chain, Ptr, Val};
9572 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9573}
9574
9576 EVT MemVT, EVT VT, SDValue Chain,
9578 SDVTList VTs = getVTList(VT, MVT::Other);
9579 SDValue Ops[] = {Chain, Ptr};
9580 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9581}
9582
9583/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9585 if (Ops.size() == 1)
9586 return Ops[0];
9587
9589 VTs.reserve(Ops.size());
9590 for (const SDValue &Op : Ops)
9591 VTs.push_back(Op.getValueType());
9592 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9593}
9594
9596 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9597 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9599 const AAMDNodes &AAInfo) {
9600 if (Size.hasValue() && !Size.getValue())
9602
9604 MachineMemOperand *MMO =
9605 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9606
9607 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9608}
9609
9611 SDVTList VTList,
9612 ArrayRef<SDValue> Ops, EVT MemVT,
9613 MachineMemOperand *MMO) {
9614 assert(
9615 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9616 Opcode == ISD::PREFETCH ||
9617 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9618 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9619 "Opcode is not a memory-accessing opcode!");
9620
9621 // Memoize the node unless it returns a glue result.
9623 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9625 AddNodeIDNode(ID, Opcode, VTList, Ops);
9626 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9627 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9628 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9629 ID.AddInteger(MMO->getFlags());
9630 ID.AddInteger(MemVT.getRawBits());
9631 void *IP = nullptr;
9632 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9633 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9634 return SDValue(E, 0);
9635 }
9636
9637 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9638 VTList, MemVT, MMO);
9639 createOperands(N, Ops);
9640
9641 CSEMap.InsertNode(N, IP);
9642 } else {
9643 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9644 VTList, MemVT, MMO);
9645 createOperands(N, Ops);
9646 }
9647 InsertNode(N);
9648 SDValue V(N, 0);
9649 NewSDValueDbgMsg(V, "Creating new node: ", this);
9650 return V;
9651}
9652
9654 SDValue Chain, int FrameIndex) {
9655 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9656 const auto VTs = getVTList(MVT::Other);
9657 SDValue Ops[2] = {
9658 Chain,
9659 getFrameIndex(FrameIndex,
9660 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9661 true)};
9662
9664 AddNodeIDNode(ID, Opcode, VTs, Ops);
9665 ID.AddInteger(FrameIndex);
9666 void *IP = nullptr;
9667 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9668 return SDValue(E, 0);
9669
9670 LifetimeSDNode *N =
9671 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9672 createOperands(N, Ops);
9673 CSEMap.InsertNode(N, IP);
9674 InsertNode(N);
9675 SDValue V(N, 0);
9676 NewSDValueDbgMsg(V, "Creating new node: ", this);
9677 return V;
9678}
9679
9681 uint64_t Guid, uint64_t Index,
9682 uint32_t Attr) {
9683 const unsigned Opcode = ISD::PSEUDO_PROBE;
9684 const auto VTs = getVTList(MVT::Other);
9685 SDValue Ops[] = {Chain};
9687 AddNodeIDNode(ID, Opcode, VTs, Ops);
9688 ID.AddInteger(Guid);
9689 ID.AddInteger(Index);
9690 void *IP = nullptr;
9691 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9692 return SDValue(E, 0);
9693
9694 auto *N = newSDNode<PseudoProbeSDNode>(
9695 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9696 createOperands(N, Ops);
9697 CSEMap.InsertNode(N, IP);
9698 InsertNode(N);
9699 SDValue V(N, 0);
9700 NewSDValueDbgMsg(V, "Creating new node: ", this);
9701 return V;
9702}
9703
9704/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9705/// MachinePointerInfo record from it. This is particularly useful because the
9706/// code generator has many cases where it doesn't bother passing in a
9707/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9709 SelectionDAG &DAG, SDValue Ptr,
9710 int64_t Offset = 0) {
9711 // If this is FI+Offset, we can model it.
9714 FI->getIndex(), Offset);
9715
9716 // If this is (FI+Offset1)+Offset2, we can model it.
9717 if (Ptr.getOpcode() != ISD::ADD ||
9718 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
9719 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
9720 return Info;
9721
9722 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9724 DAG.getMachineFunction(), FI,
9725 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9726}
9727
9728/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9729/// MachinePointerInfo record from it. This is particularly useful because the
9730/// code generator has many cases where it doesn't bother passing in a
9731/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9733 SelectionDAG &DAG, SDValue Ptr,
9734 SDValue OffsetOp) {
9735 // If the 'Offset' value isn't a constant, we can't handle this.
9737 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9738 if (OffsetOp.isUndef())
9739 return InferPointerInfo(Info, DAG, Ptr);
9740 return Info;
9741}
9742
9744 EVT VT, const SDLoc &dl, SDValue Chain,
9746 MachinePointerInfo PtrInfo, EVT MemVT,
9747 Align Alignment,
9748 MachineMemOperand::Flags MMOFlags,
9749 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9750 assert(Chain.getValueType() == MVT::Other &&
9751 "Invalid chain type");
9752
9753 MMOFlags |= MachineMemOperand::MOLoad;
9754 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9755 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9756 // clients.
9757 if (PtrInfo.V.isNull())
9758 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9759
9760 TypeSize Size = MemVT.getStoreSize();
9762 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9763 Alignment, AAInfo, Ranges);
9764 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9765}
9766
9768 EVT VT, const SDLoc &dl, SDValue Chain,
9769 SDValue Ptr, SDValue Offset, EVT MemVT,
9770 MachineMemOperand *MMO) {
9771 if (VT == MemVT) {
9772 ExtType = ISD::NON_EXTLOAD;
9773 } else if (ExtType == ISD::NON_EXTLOAD) {
9774 assert(VT == MemVT && "Non-extending load from different memory type!");
9775 } else {
9776 // Extending load.
9777 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9778 "Should only be an extending load, not truncating!");
9779 assert(VT.isInteger() == MemVT.isInteger() &&
9780 "Cannot convert from FP to Int or Int -> FP!");
9781 assert(VT.isVector() == MemVT.isVector() &&
9782 "Cannot use an ext load to convert to or from a vector!");
9783 assert((!VT.isVector() ||
9785 "Cannot use an ext load to change the number of vector elements!");
9786 }
9787
9788 assert((!MMO->getRanges() ||
9790 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9791 MemVT.isInteger())) &&
9792 "Range metadata and load type must match!");
9793
9794 bool Indexed = AM != ISD::UNINDEXED;
9795 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9796
9797 SDVTList VTs = Indexed ?
9798 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9799 SDValue Ops[] = { Chain, Ptr, Offset };
9801 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9802 ID.AddInteger(MemVT.getRawBits());
9803 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9804 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9805 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9806 ID.AddInteger(MMO->getFlags());
9807 void *IP = nullptr;
9808 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9809 E->refineAlignment(MMO);
9810 E->refineRanges(MMO);
9811 return SDValue(E, 0);
9812 }
9813 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9814 ExtType, MemVT, MMO);
9815 createOperands(N, Ops);
9816
9817 CSEMap.InsertNode(N, IP);
9818 InsertNode(N);
9819 SDValue V(N, 0);
9820 NewSDValueDbgMsg(V, "Creating new node: ", this);
9821 return V;
9822}
9823
9826 MaybeAlign Alignment,
9827 MachineMemOperand::Flags MMOFlags,
9828 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9829 SDValue Undef = getUNDEF(Ptr.getValueType());
9830 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9831 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9832}
9833
9836 SDValue Undef = getUNDEF(Ptr.getValueType());
9837 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9838 VT, MMO);
9839}
9840
9842 EVT VT, SDValue Chain, SDValue Ptr,
9843 MachinePointerInfo PtrInfo, EVT MemVT,
9844 MaybeAlign Alignment,
9845 MachineMemOperand::Flags MMOFlags,
9846 const AAMDNodes &AAInfo) {
9847 SDValue Undef = getUNDEF(Ptr.getValueType());
9848 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9849 MemVT, Alignment, MMOFlags, AAInfo);
9850}
9851
9853 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9854 MachineMemOperand *MMO) {
9855 SDValue Undef = getUNDEF(Ptr.getValueType());
9856 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9857 MemVT, MMO);
9858}
9859
9863 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9864 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9865 // Don't propagate the invariant or dereferenceable flags.
9866 auto MMOFlags =
9867 LD->getMemOperand()->getFlags() &
9869 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9870 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9871 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9872}
9873
9876 Align Alignment,
9877 MachineMemOperand::Flags MMOFlags,
9878 const AAMDNodes &AAInfo) {
9879 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9880
9881 MMOFlags |= MachineMemOperand::MOStore;
9882 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9883
9884 if (PtrInfo.V.isNull())
9885 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9886
9889 MachineMemOperand *MMO =
9890 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9891 return getStore(Chain, dl, Val, Ptr, MMO);
9892}
9893
9896 SDValue Undef = getUNDEF(Ptr.getValueType());
9897 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9899}
9900
9904 bool IsTruncating) {
9905 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9906 EVT VT = Val.getValueType();
9907 if (VT == SVT) {
9908 IsTruncating = false;
9909 } else if (!IsTruncating) {
9910 assert(VT == SVT && "No-truncating store from different memory type!");
9911 } else {
9913 "Should only be a truncating store, not extending!");
9914 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9915 assert(VT.isVector() == SVT.isVector() &&
9916 "Cannot use trunc store to convert to or from a vector!");
9917 assert((!VT.isVector() ||
9919 "Cannot use trunc store to change the number of vector elements!");
9920 }
9921
9922 bool Indexed = AM != ISD::UNINDEXED;
9923 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9924 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9925 : getVTList(MVT::Other);
9926 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9928 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9929 ID.AddInteger(SVT.getRawBits());
9930 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9931 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9932 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9933 ID.AddInteger(MMO->getFlags());
9934 void *IP = nullptr;
9935 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9936 cast<StoreSDNode>(E)->refineAlignment(MMO);
9937 return SDValue(E, 0);
9938 }
9939 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9940 IsTruncating, SVT, MMO);
9941 createOperands(N, Ops);
9942
9943 CSEMap.InsertNode(N, IP);
9944 InsertNode(N);
9945 SDValue V(N, 0);
9946 NewSDValueDbgMsg(V, "Creating new node: ", this);
9947 return V;
9948}
9949
9952 EVT SVT, Align Alignment,
9953 MachineMemOperand::Flags MMOFlags,
9954 const AAMDNodes &AAInfo) {
9955 assert(Chain.getValueType() == MVT::Other &&
9956 "Invalid chain type");
9957
9958 MMOFlags |= MachineMemOperand::MOStore;
9959 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9960
9961 if (PtrInfo.V.isNull())
9962 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9963
9965 MachineMemOperand *MMO = MF.getMachineMemOperand(
9966 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
9967 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9968}
9969
9971 SDValue Ptr, EVT SVT,
9972 MachineMemOperand *MMO) {
9973 SDValue Undef = getUNDEF(Ptr.getValueType());
9974 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
9975}
9976
9980 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9981 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9982 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9983 ST->getMemoryVT(), ST->getMemOperand(), AM,
9984 ST->isTruncatingStore());
9985}
9986
9988 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9989 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9990 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9991 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9992 const MDNode *Ranges, bool IsExpanding) {
9993 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9994
9995 MMOFlags |= MachineMemOperand::MOLoad;
9996 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9997 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9998 // clients.
9999 if (PtrInfo.V.isNull())
10000 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10001
10002 TypeSize Size = MemVT.getStoreSize();
10004 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10005 Alignment, AAInfo, Ranges);
10006 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10007 MMO, IsExpanding);
10008}
10009
10011 ISD::LoadExtType ExtType, EVT VT,
10012 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10013 SDValue Offset, SDValue Mask, SDValue EVL,
10014 EVT MemVT, MachineMemOperand *MMO,
10015 bool IsExpanding) {
10016 bool Indexed = AM != ISD::UNINDEXED;
10017 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10018
10019 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10020 : getVTList(VT, MVT::Other);
10021 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10023 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10024 ID.AddInteger(MemVT.getRawBits());
10025 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10026 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10027 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10028 ID.AddInteger(MMO->getFlags());
10029 void *IP = nullptr;
10030 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10031 E->refineAlignment(MMO);
10032 E->refineRanges(MMO);
10033 return SDValue(E, 0);
10034 }
10035 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10036 ExtType, IsExpanding, MemVT, MMO);
10037 createOperands(N, Ops);
10038
10039 CSEMap.InsertNode(N, IP);
10040 InsertNode(N);
10041 SDValue V(N, 0);
10042 NewSDValueDbgMsg(V, "Creating new node: ", this);
10043 return V;
10044}
10045
10047 SDValue Ptr, SDValue Mask, SDValue EVL,
10048 MachinePointerInfo PtrInfo,
10049 MaybeAlign Alignment,
10050 MachineMemOperand::Flags MMOFlags,
10051 const AAMDNodes &AAInfo, const MDNode *Ranges,
10052 bool IsExpanding) {
10053 SDValue Undef = getUNDEF(Ptr.getValueType());
10054 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10055 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10056 IsExpanding);
10057}
10058
10060 SDValue Ptr, SDValue Mask, SDValue EVL,
10061 MachineMemOperand *MMO, bool IsExpanding) {
10062 SDValue Undef = getUNDEF(Ptr.getValueType());
10063 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10064 Mask, EVL, VT, MMO, IsExpanding);
10065}
10066
10068 EVT VT, SDValue Chain, SDValue Ptr,
10069 SDValue Mask, SDValue EVL,
10070 MachinePointerInfo PtrInfo, EVT MemVT,
10071 MaybeAlign Alignment,
10072 MachineMemOperand::Flags MMOFlags,
10073 const AAMDNodes &AAInfo, bool IsExpanding) {
10074 SDValue Undef = getUNDEF(Ptr.getValueType());
10075 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10076 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10077 IsExpanding);
10078}
10079
10081 EVT VT, SDValue Chain, SDValue Ptr,
10082 SDValue Mask, SDValue EVL, EVT MemVT,
10083 MachineMemOperand *MMO, bool IsExpanding) {
10084 SDValue Undef = getUNDEF(Ptr.getValueType());
10085 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10086 EVL, MemVT, MMO, IsExpanding);
10087}
10088
10092 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10093 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10094 // Don't propagate the invariant or dereferenceable flags.
10095 auto MMOFlags =
10096 LD->getMemOperand()->getFlags() &
10098 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10099 LD->getChain(), Base, Offset, LD->getMask(),
10100 LD->getVectorLength(), LD->getPointerInfo(),
10101 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10102 nullptr, LD->isExpandingLoad());
10103}
10104
10107 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10108 ISD::MemIndexedMode AM, bool IsTruncating,
10109 bool IsCompressing) {
10110 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10111 bool Indexed = AM != ISD::UNINDEXED;
10112 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10113 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10114 : getVTList(MVT::Other);
10115 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10117 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10118 ID.AddInteger(MemVT.getRawBits());
10119 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10120 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10121 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10122 ID.AddInteger(MMO->getFlags());
10123 void *IP = nullptr;
10124 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10125 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10126 return SDValue(E, 0);
10127 }
10128 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10129 IsTruncating, IsCompressing, MemVT, MMO);
10130 createOperands(N, Ops);
10131
10132 CSEMap.InsertNode(N, IP);
10133 InsertNode(N);
10134 SDValue V(N, 0);
10135 NewSDValueDbgMsg(V, "Creating new node: ", this);
10136 return V;
10137}
10138
10140 SDValue Val, SDValue Ptr, SDValue Mask,
10141 SDValue EVL, MachinePointerInfo PtrInfo,
10142 EVT SVT, Align Alignment,
10143 MachineMemOperand::Flags MMOFlags,
10144 const AAMDNodes &AAInfo,
10145 bool IsCompressing) {
10146 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10147
10148 MMOFlags |= MachineMemOperand::MOStore;
10149 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10150
10151 if (PtrInfo.V.isNull())
10152 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10153
10155 MachineMemOperand *MMO = MF.getMachineMemOperand(
10156 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10157 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10158 IsCompressing);
10159}
10160
10162 SDValue Val, SDValue Ptr, SDValue Mask,
10163 SDValue EVL, EVT SVT,
10164 MachineMemOperand *MMO,
10165 bool IsCompressing) {
10166 EVT VT = Val.getValueType();
10167
10168 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10169 if (VT == SVT)
10170 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10171 EVL, VT, MMO, ISD::UNINDEXED,
10172 /*IsTruncating*/ false, IsCompressing);
10173
10175 "Should only be a truncating store, not extending!");
10176 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10177 assert(VT.isVector() == SVT.isVector() &&
10178 "Cannot use trunc store to convert to or from a vector!");
10179 assert((!VT.isVector() ||
10181 "Cannot use trunc store to change the number of vector elements!");
10182
10183 SDVTList VTs = getVTList(MVT::Other);
10184 SDValue Undef = getUNDEF(Ptr.getValueType());
10185 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10187 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10188 ID.AddInteger(SVT.getRawBits());
10189 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10190 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10191 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10192 ID.AddInteger(MMO->getFlags());
10193 void *IP = nullptr;
10194 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10195 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10196 return SDValue(E, 0);
10197 }
10198 auto *N =
10199 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10200 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10201 createOperands(N, Ops);
10202
10203 CSEMap.InsertNode(N, IP);
10204 InsertNode(N);
10205 SDValue V(N, 0);
10206 NewSDValueDbgMsg(V, "Creating new node: ", this);
10207 return V;
10208}
10209
10213 auto *ST = cast<VPStoreSDNode>(OrigStore);
10214 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10215 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10216 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10217 Offset, ST->getMask(), ST->getVectorLength()};
10219 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10220 ID.AddInteger(ST->getMemoryVT().getRawBits());
10221 ID.AddInteger(ST->getRawSubclassData());
10222 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10223 ID.AddInteger(ST->getMemOperand()->getFlags());
10224 void *IP = nullptr;
10225 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10226 return SDValue(E, 0);
10227
10228 auto *N = newSDNode<VPStoreSDNode>(
10229 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10230 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10231 createOperands(N, Ops);
10232
10233 CSEMap.InsertNode(N, IP);
10234 InsertNode(N);
10235 SDValue V(N, 0);
10236 NewSDValueDbgMsg(V, "Creating new node: ", this);
10237 return V;
10238}
10239
10241 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10242 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10243 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10244 bool Indexed = AM != ISD::UNINDEXED;
10245 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10246
10247 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10248 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10249 : getVTList(VT, MVT::Other);
10251 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10252 ID.AddInteger(VT.getRawBits());
10253 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10254 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10255 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10256
10257 void *IP = nullptr;
10258 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10259 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10260 return SDValue(E, 0);
10261 }
10262
10263 auto *N =
10264 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10265 ExtType, IsExpanding, MemVT, MMO);
10266 createOperands(N, Ops);
10267 CSEMap.InsertNode(N, IP);
10268 InsertNode(N);
10269 SDValue V(N, 0);
10270 NewSDValueDbgMsg(V, "Creating new node: ", this);
10271 return V;
10272}
10273
10275 SDValue Ptr, SDValue Stride,
10276 SDValue Mask, SDValue EVL,
10277 MachineMemOperand *MMO,
10278 bool IsExpanding) {
10279 SDValue Undef = getUNDEF(Ptr.getValueType());
10281 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10282}
10283
10285 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10286 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10287 MachineMemOperand *MMO, bool IsExpanding) {
10288 SDValue Undef = getUNDEF(Ptr.getValueType());
10289 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10290 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10291}
10292
10294 SDValue Val, SDValue Ptr,
10295 SDValue Offset, SDValue Stride,
10296 SDValue Mask, SDValue EVL, EVT MemVT,
10297 MachineMemOperand *MMO,
10299 bool IsTruncating, bool IsCompressing) {
10300 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10301 bool Indexed = AM != ISD::UNINDEXED;
10302 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10303 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10304 : getVTList(MVT::Other);
10305 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10307 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10308 ID.AddInteger(MemVT.getRawBits());
10309 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10310 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10311 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10312 void *IP = nullptr;
10313 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10314 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10315 return SDValue(E, 0);
10316 }
10317 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10318 VTs, AM, IsTruncating,
10319 IsCompressing, MemVT, MMO);
10320 createOperands(N, Ops);
10321
10322 CSEMap.InsertNode(N, IP);
10323 InsertNode(N);
10324 SDValue V(N, 0);
10325 NewSDValueDbgMsg(V, "Creating new node: ", this);
10326 return V;
10327}
10328
10330 SDValue Val, SDValue Ptr,
10331 SDValue Stride, SDValue Mask,
10332 SDValue EVL, EVT SVT,
10333 MachineMemOperand *MMO,
10334 bool IsCompressing) {
10335 EVT VT = Val.getValueType();
10336
10337 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10338 if (VT == SVT)
10339 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10340 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10341 /*IsTruncating*/ false, IsCompressing);
10342
10344 "Should only be a truncating store, not extending!");
10345 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10346 assert(VT.isVector() == SVT.isVector() &&
10347 "Cannot use trunc store to convert to or from a vector!");
10348 assert((!VT.isVector() ||
10350 "Cannot use trunc store to change the number of vector elements!");
10351
10352 SDVTList VTs = getVTList(MVT::Other);
10353 SDValue Undef = getUNDEF(Ptr.getValueType());
10354 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10356 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10357 ID.AddInteger(SVT.getRawBits());
10358 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10359 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10360 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10361 void *IP = nullptr;
10362 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10363 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10364 return SDValue(E, 0);
10365 }
10366 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10367 VTs, ISD::UNINDEXED, true,
10368 IsCompressing, SVT, MMO);
10369 createOperands(N, Ops);
10370
10371 CSEMap.InsertNode(N, IP);
10372 InsertNode(N);
10373 SDValue V(N, 0);
10374 NewSDValueDbgMsg(V, "Creating new node: ", this);
10375 return V;
10376}
10377
10380 ISD::MemIndexType IndexType) {
10381 assert(Ops.size() == 6 && "Incompatible number of operands");
10382
10384 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10385 ID.AddInteger(VT.getRawBits());
10386 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10387 dl.getIROrder(), VTs, VT, MMO, IndexType));
10388 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10389 ID.AddInteger(MMO->getFlags());
10390 void *IP = nullptr;
10391 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10392 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10393 return SDValue(E, 0);
10394 }
10395
10396 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10397 VT, MMO, IndexType);
10398 createOperands(N, Ops);
10399
10400 assert(N->getMask().getValueType().getVectorElementCount() ==
10401 N->getValueType(0).getVectorElementCount() &&
10402 "Vector width mismatch between mask and data");
10403 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10404 N->getValueType(0).getVectorElementCount().isScalable() &&
10405 "Scalable flags of index and data do not match");
10407 N->getIndex().getValueType().getVectorElementCount(),
10408 N->getValueType(0).getVectorElementCount()) &&
10409 "Vector width mismatch between index and data");
10410 assert(isa<ConstantSDNode>(N->getScale()) &&
10411 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10412 "Scale should be a constant power of 2");
10413
10414 CSEMap.InsertNode(N, IP);
10415 InsertNode(N);
10416 SDValue V(N, 0);
10417 NewSDValueDbgMsg(V, "Creating new node: ", this);
10418 return V;
10419}
10420
10423 MachineMemOperand *MMO,
10424 ISD::MemIndexType IndexType) {
10425 assert(Ops.size() == 7 && "Incompatible number of operands");
10426
10428 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10429 ID.AddInteger(VT.getRawBits());
10430 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10431 dl.getIROrder(), VTs, VT, MMO, IndexType));
10432 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10433 ID.AddInteger(MMO->getFlags());
10434 void *IP = nullptr;
10435 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10436 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10437 return SDValue(E, 0);
10438 }
10439 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10440 VT, MMO, IndexType);
10441 createOperands(N, Ops);
10442
10443 assert(N->getMask().getValueType().getVectorElementCount() ==
10444 N->getValue().getValueType().getVectorElementCount() &&
10445 "Vector width mismatch between mask and data");
10446 assert(
10447 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10448 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10449 "Scalable flags of index and data do not match");
10451 N->getIndex().getValueType().getVectorElementCount(),
10452 N->getValue().getValueType().getVectorElementCount()) &&
10453 "Vector width mismatch between index and data");
10454 assert(isa<ConstantSDNode>(N->getScale()) &&
10455 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10456 "Scale should be a constant power of 2");
10457
10458 CSEMap.InsertNode(N, IP);
10459 InsertNode(N);
10460 SDValue V(N, 0);
10461 NewSDValueDbgMsg(V, "Creating new node: ", this);
10462 return V;
10463}
10464
10467 SDValue PassThru, EVT MemVT,
10468 MachineMemOperand *MMO,
10470 ISD::LoadExtType ExtTy, bool isExpanding) {
10471 bool Indexed = AM != ISD::UNINDEXED;
10472 assert((Indexed || Offset.isUndef()) &&
10473 "Unindexed masked load with an offset!");
10474 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10475 : getVTList(VT, MVT::Other);
10476 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10478 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
10479 ID.AddInteger(MemVT.getRawBits());
10480 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10481 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10482 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10483 ID.AddInteger(MMO->getFlags());
10484 void *IP = nullptr;
10485 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10486 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10487 return SDValue(E, 0);
10488 }
10489 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10490 AM, ExtTy, isExpanding, MemVT, MMO);
10491 createOperands(N, Ops);
10492
10493 CSEMap.InsertNode(N, IP);
10494 InsertNode(N);
10495 SDValue V(N, 0);
10496 NewSDValueDbgMsg(V, "Creating new node: ", this);
10497 return V;
10498}
10499
10504 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10505 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10506 Offset, LD->getMask(), LD->getPassThru(),
10507 LD->getMemoryVT(), LD->getMemOperand(), AM,
10508 LD->getExtensionType(), LD->isExpandingLoad());
10509}
10510
10513 SDValue Mask, EVT MemVT,
10514 MachineMemOperand *MMO,
10515 ISD::MemIndexedMode AM, bool IsTruncating,
10516 bool IsCompressing) {
10517 assert(Chain.getValueType() == MVT::Other &&
10518 "Invalid chain type");
10519 bool Indexed = AM != ISD::UNINDEXED;
10520 assert((Indexed || Offset.isUndef()) &&
10521 "Unindexed masked store with an offset!");
10522 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10523 : getVTList(MVT::Other);
10524 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10526 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
10527 ID.AddInteger(MemVT.getRawBits());
10528 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10529 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10530 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10531 ID.AddInteger(MMO->getFlags());
10532 void *IP = nullptr;
10533 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10534 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10535 return SDValue(E, 0);
10536 }
10537 auto *N =
10538 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10539 IsTruncating, IsCompressing, MemVT, MMO);
10540 createOperands(N, Ops);
10541
10542 CSEMap.InsertNode(N, IP);
10543 InsertNode(N);
10544 SDValue V(N, 0);
10545 NewSDValueDbgMsg(V, "Creating new node: ", this);
10546 return V;
10547}
10548
10553 assert(ST->getOffset().isUndef() &&
10554 "Masked store is already a indexed store!");
10555 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10556 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10557 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10558}
10559
10562 MachineMemOperand *MMO,
10563 ISD::MemIndexType IndexType,
10564 ISD::LoadExtType ExtTy) {
10565 assert(Ops.size() == 6 && "Incompatible number of operands");
10566
10568 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10569 ID.AddInteger(MemVT.getRawBits());
10570 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10571 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10572 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10573 ID.AddInteger(MMO->getFlags());
10574 void *IP = nullptr;
10575 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10576 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10577 return SDValue(E, 0);
10578 }
10579
10580 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10581 VTs, MemVT, MMO, IndexType, ExtTy);
10582 createOperands(N, Ops);
10583
10584 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10585 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10586 assert(N->getMask().getValueType().getVectorElementCount() ==
10587 N->getValueType(0).getVectorElementCount() &&
10588 "Vector width mismatch between mask and data");
10589 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10590 N->getValueType(0).getVectorElementCount().isScalable() &&
10591 "Scalable flags of index and data do not match");
10593 N->getIndex().getValueType().getVectorElementCount(),
10594 N->getValueType(0).getVectorElementCount()) &&
10595 "Vector width mismatch between index and data");
10596 assert(isa<ConstantSDNode>(N->getScale()) &&
10597 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10598 "Scale should be a constant power of 2");
10599
10600 CSEMap.InsertNode(N, IP);
10601 InsertNode(N);
10602 SDValue V(N, 0);
10603 NewSDValueDbgMsg(V, "Creating new node: ", this);
10604 return V;
10605}
10606
10609 MachineMemOperand *MMO,
10610 ISD::MemIndexType IndexType,
10611 bool IsTrunc) {
10612 assert(Ops.size() == 6 && "Incompatible number of operands");
10613
10615 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10616 ID.AddInteger(MemVT.getRawBits());
10617 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10618 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10619 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10620 ID.AddInteger(MMO->getFlags());
10621 void *IP = nullptr;
10622 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10623 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10624 return SDValue(E, 0);
10625 }
10626
10627 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10628 VTs, MemVT, MMO, IndexType, IsTrunc);
10629 createOperands(N, Ops);
10630
10631 assert(N->getMask().getValueType().getVectorElementCount() ==
10632 N->getValue().getValueType().getVectorElementCount() &&
10633 "Vector width mismatch between mask and data");
10634 assert(
10635 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10636 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10637 "Scalable flags of index and data do not match");
10639 N->getIndex().getValueType().getVectorElementCount(),
10640 N->getValue().getValueType().getVectorElementCount()) &&
10641 "Vector width mismatch between index and data");
10642 assert(isa<ConstantSDNode>(N->getScale()) &&
10643 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10644 "Scale should be a constant power of 2");
10645
10646 CSEMap.InsertNode(N, IP);
10647 InsertNode(N);
10648 SDValue V(N, 0);
10649 NewSDValueDbgMsg(V, "Creating new node: ", this);
10650 return V;
10651}
10652
10654 const SDLoc &dl, ArrayRef<SDValue> Ops,
10655 MachineMemOperand *MMO,
10656 ISD::MemIndexType IndexType) {
10657 assert(Ops.size() == 7 && "Incompatible number of operands");
10658
10660 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VECTOR_HISTOGRAM, VTs, Ops);
10661 ID.AddInteger(MemVT.getRawBits());
10662 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10663 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10664 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10665 ID.AddInteger(MMO->getFlags());
10666 void *IP = nullptr;
10667 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10668 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10669 return SDValue(E, 0);
10670 }
10671
10672 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10673 VTs, MemVT, MMO, IndexType);
10674 createOperands(N, Ops);
10675
10676 assert(N->getMask().getValueType().getVectorElementCount() ==
10677 N->getIndex().getValueType().getVectorElementCount() &&
10678 "Vector width mismatch between mask and data");
10679 assert(isa<ConstantSDNode>(N->getScale()) &&
10680 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10681 "Scale should be a constant power of 2");
10682 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10683
10684 CSEMap.InsertNode(N, IP);
10685 InsertNode(N);
10686 SDValue V(N, 0);
10687 NewSDValueDbgMsg(V, "Creating new node: ", this);
10688 return V;
10689}
10690
10692 SDValue Ptr, SDValue Mask, SDValue EVL,
10693 MachineMemOperand *MMO) {
10694 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10695 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10697 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10698 ID.AddInteger(VT.getRawBits());
10699 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10700 VTs, VT, MMO));
10701 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10702 ID.AddInteger(MMO->getFlags());
10703 void *IP = nullptr;
10704 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10705 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10706 return SDValue(E, 0);
10707 }
10708 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10709 VT, MMO);
10710 createOperands(N, Ops);
10711
10712 CSEMap.InsertNode(N, IP);
10713 InsertNode(N);
10714 SDValue V(N, 0);
10715 NewSDValueDbgMsg(V, "Creating new node: ", this);
10716 return V;
10717}
10718
10720 EVT MemVT, MachineMemOperand *MMO) {
10721 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10722 SDVTList VTs = getVTList(MVT::Other);
10723 SDValue Ops[] = {Chain, Ptr};
10725 AddNodeIDNode(ID, ISD::GET_FPENV_MEM, VTs, Ops);
10726 ID.AddInteger(MemVT.getRawBits());
10727 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10728 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10729 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10730 ID.AddInteger(MMO->getFlags());
10731 void *IP = nullptr;
10732 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10733 return SDValue(E, 0);
10734
10735 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10736 dl.getDebugLoc(), VTs, MemVT, MMO);
10737 createOperands(N, Ops);
10738
10739 CSEMap.InsertNode(N, IP);
10740 InsertNode(N);
10741 SDValue V(N, 0);
10742 NewSDValueDbgMsg(V, "Creating new node: ", this);
10743 return V;
10744}
10745
10747 EVT MemVT, MachineMemOperand *MMO) {
10748 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10749 SDVTList VTs = getVTList(MVT::Other);
10750 SDValue Ops[] = {Chain, Ptr};
10752 AddNodeIDNode(ID, ISD::SET_FPENV_MEM, VTs, Ops);
10753 ID.AddInteger(MemVT.getRawBits());
10754 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10755 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10756 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10757 ID.AddInteger(MMO->getFlags());
10758 void *IP = nullptr;
10759 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10760 return SDValue(E, 0);
10761
10762 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10763 dl.getDebugLoc(), VTs, MemVT, MMO);
10764 createOperands(N, Ops);
10765
10766 CSEMap.InsertNode(N, IP);
10767 InsertNode(N);
10768 SDValue V(N, 0);
10769 NewSDValueDbgMsg(V, "Creating new node: ", this);
10770 return V;
10771}
10772
10774 // select undef, T, F --> T (if T is a constant), otherwise F
10775 // select, ?, undef, F --> F
10776 // select, ?, T, undef --> T
10777 if (Cond.isUndef())
10778 return isConstantValueOfAnyType(T) ? T : F;
10779 if (T.isUndef())
10780 return F;
10781 if (F.isUndef())
10782 return T;
10783
10784 // select true, T, F --> T
10785 // select false, T, F --> F
10786 if (auto C = isBoolConstant(Cond))
10787 return *C ? T : F;
10788
10789 // select ?, T, T --> T
10790 if (T == F)
10791 return T;
10792
10793 return SDValue();
10794}
10795
10797 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10798 if (X.isUndef())
10799 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10800 // shift X, undef --> undef (because it may shift by the bitwidth)
10801 if (Y.isUndef())
10802 return getUNDEF(X.getValueType());
10803
10804 // shift 0, Y --> 0
10805 // shift X, 0 --> X
10807 return X;
10808
10809 // shift X, C >= bitwidth(X) --> undef
10810 // All vector elements must be too big (or undef) to avoid partial undefs.
10811 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10812 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10813 };
10814 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10815 return getUNDEF(X.getValueType());
10816
10817 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10818 if (X.getValueType().getScalarType() == MVT::i1)
10819 return X;
10820
10821 return SDValue();
10822}
10823
10825 SDNodeFlags Flags) {
10826 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10827 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10828 // operation is poison. That result can be relaxed to undef.
10829 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10830 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10831 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10832 (YC && YC->getValueAPF().isNaN());
10833 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10834 (YC && YC->getValueAPF().isInfinity());
10835
10836 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10837 return getUNDEF(X.getValueType());
10838
10839 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10840 return getUNDEF(X.getValueType());
10841
10842 if (!YC)
10843 return SDValue();
10844
10845 // X + -0.0 --> X
10846 if (Opcode == ISD::FADD)
10847 if (YC->getValueAPF().isNegZero())
10848 return X;
10849
10850 // X - +0.0 --> X
10851 if (Opcode == ISD::FSUB)
10852 if (YC->getValueAPF().isPosZero())
10853 return X;
10854
10855 // X * 1.0 --> X
10856 // X / 1.0 --> X
10857 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10858 if (YC->getValueAPF().isExactlyValue(1.0))
10859 return X;
10860
10861 // X * 0.0 --> 0.0
10862 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10863 if (YC->getValueAPF().isZero())
10864 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10865
10866 return SDValue();
10867}
10868
10870 SDValue Ptr, SDValue SV, unsigned Align) {
10871 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10872 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10873}
10874
10875SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10877 switch (Ops.size()) {
10878 case 0: return getNode(Opcode, DL, VT);
10879 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10880 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10881 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10882 default: break;
10883 }
10884
10885 // Copy from an SDUse array into an SDValue array for use with
10886 // the regular getNode logic.
10888 return getNode(Opcode, DL, VT, NewOps);
10889}
10890
10891SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10893 SDNodeFlags Flags;
10894 if (Inserter)
10895 Flags = Inserter->getFlags();
10896 return getNode(Opcode, DL, VT, Ops, Flags);
10897}
10898
10899SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10900 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10901 unsigned NumOps = Ops.size();
10902 switch (NumOps) {
10903 case 0: return getNode(Opcode, DL, VT);
10904 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10905 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10906 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10907 default: break;
10908 }
10909
10910#ifndef NDEBUG
10911 for (const auto &Op : Ops)
10912 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10913 "Operand is DELETED_NODE!");
10914#endif
10915
10916 switch (Opcode) {
10917 default: break;
10918 case ISD::BUILD_VECTOR:
10919 // Attempt to simplify BUILD_VECTOR.
10920 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10921 return V;
10922 break;
10924 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10925 return V;
10926 break;
10927 case ISD::SELECT_CC:
10928 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10929 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10930 "LHS and RHS of condition must have same type!");
10931 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10932 "True and False arms of SelectCC must have same type!");
10933 assert(Ops[2].getValueType() == VT &&
10934 "select_cc node must be of same type as true and false value!");
10935 assert((!Ops[0].getValueType().isVector() ||
10936 Ops[0].getValueType().getVectorElementCount() ==
10937 VT.getVectorElementCount()) &&
10938 "Expected select_cc with vector result to have the same sized "
10939 "comparison type!");
10940 break;
10941 case ISD::BR_CC:
10942 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10943 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10944 "LHS/RHS of comparison should match types!");
10945 break;
10946 case ISD::VP_ADD:
10947 case ISD::VP_SUB:
10948 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10949 if (VT.getScalarType() == MVT::i1)
10950 Opcode = ISD::VP_XOR;
10951 break;
10952 case ISD::VP_MUL:
10953 // If it is VP_MUL mask operation then turn it to VP_AND
10954 if (VT.getScalarType() == MVT::i1)
10955 Opcode = ISD::VP_AND;
10956 break;
10957 case ISD::VP_REDUCE_MUL:
10958 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10959 if (VT == MVT::i1)
10960 Opcode = ISD::VP_REDUCE_AND;
10961 break;
10962 case ISD::VP_REDUCE_ADD:
10963 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10964 if (VT == MVT::i1)
10965 Opcode = ISD::VP_REDUCE_XOR;
10966 break;
10967 case ISD::VP_REDUCE_SMAX:
10968 case ISD::VP_REDUCE_UMIN:
10969 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10970 // VP_REDUCE_AND.
10971 if (VT == MVT::i1)
10972 Opcode = ISD::VP_REDUCE_AND;
10973 break;
10974 case ISD::VP_REDUCE_SMIN:
10975 case ISD::VP_REDUCE_UMAX:
10976 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10977 // VP_REDUCE_OR.
10978 if (VT == MVT::i1)
10979 Opcode = ISD::VP_REDUCE_OR;
10980 break;
10981 }
10982
10983 // Memoize nodes.
10984 SDNode *N;
10985 SDVTList VTs = getVTList(VT);
10986
10987 if (VT != MVT::Glue) {
10989 AddNodeIDNode(ID, Opcode, VTs, Ops);
10990 void *IP = nullptr;
10991
10992 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10993 E->intersectFlagsWith(Flags);
10994 return SDValue(E, 0);
10995 }
10996
10997 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10998 createOperands(N, Ops);
10999
11000 CSEMap.InsertNode(N, IP);
11001 } else {
11002 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11003 createOperands(N, Ops);
11004 }
11005
11006 N->setFlags(Flags);
11007 InsertNode(N);
11008 SDValue V(N, 0);
11009 NewSDValueDbgMsg(V, "Creating new node: ", this);
11010 return V;
11011}
11012
11013SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11014 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11015 SDNodeFlags Flags;
11016 if (Inserter)
11017 Flags = Inserter->getFlags();
11018 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11019}
11020
11021SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11023 const SDNodeFlags Flags) {
11024 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11025}
11026
11027SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11029 SDNodeFlags Flags;
11030 if (Inserter)
11031 Flags = Inserter->getFlags();
11032 return getNode(Opcode, DL, VTList, Ops, Flags);
11033}
11034
11035SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11036 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11037 if (VTList.NumVTs == 1)
11038 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11039
11040#ifndef NDEBUG
11041 for (const auto &Op : Ops)
11042 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11043 "Operand is DELETED_NODE!");
11044#endif
11045
11046 switch (Opcode) {
11047 case ISD::SADDO:
11048 case ISD::UADDO:
11049 case ISD::SSUBO:
11050 case ISD::USUBO: {
11051 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11052 "Invalid add/sub overflow op!");
11053 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11054 Ops[0].getValueType() == Ops[1].getValueType() &&
11055 Ops[0].getValueType() == VTList.VTs[0] &&
11056 "Binary operator types must match!");
11057 SDValue N1 = Ops[0], N2 = Ops[1];
11058 canonicalizeCommutativeBinop(Opcode, N1, N2);
11059
11060 // (X +- 0) -> X with zero-overflow.
11061 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11062 /*AllowTruncation*/ true);
11063 if (N2CV && N2CV->isZero()) {
11064 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11065 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11066 }
11067
11068 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11069 VTList.VTs[1].getScalarType() == MVT::i1) {
11070 SDValue F1 = getFreeze(N1);
11071 SDValue F2 = getFreeze(N2);
11072 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11073 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11074 return getNode(ISD::MERGE_VALUES, DL, VTList,
11075 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11076 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11077 Flags);
11078 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11079 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11080 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11081 return getNode(ISD::MERGE_VALUES, DL, VTList,
11082 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11083 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11084 Flags);
11085 }
11086 }
11087 break;
11088 }
11089 case ISD::SADDO_CARRY:
11090 case ISD::UADDO_CARRY:
11091 case ISD::SSUBO_CARRY:
11092 case ISD::USUBO_CARRY:
11093 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11094 "Invalid add/sub overflow op!");
11095 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11096 Ops[0].getValueType() == Ops[1].getValueType() &&
11097 Ops[0].getValueType() == VTList.VTs[0] &&
11098 Ops[2].getValueType() == VTList.VTs[1] &&
11099 "Binary operator types must match!");
11100 break;
11101 case ISD::SMUL_LOHI:
11102 case ISD::UMUL_LOHI: {
11103 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11104 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11105 VTList.VTs[0] == Ops[0].getValueType() &&
11106 VTList.VTs[0] == Ops[1].getValueType() &&
11107 "Binary operator types must match!");
11108 // Constant fold.
11111 if (LHS && RHS) {
11112 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11113 unsigned OutWidth = Width * 2;
11114 APInt Val = LHS->getAPIntValue();
11115 APInt Mul = RHS->getAPIntValue();
11116 if (Opcode == ISD::SMUL_LOHI) {
11117 Val = Val.sext(OutWidth);
11118 Mul = Mul.sext(OutWidth);
11119 } else {
11120 Val = Val.zext(OutWidth);
11121 Mul = Mul.zext(OutWidth);
11122 }
11123 Val *= Mul;
11124
11125 SDValue Hi =
11126 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11127 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11128 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11129 }
11130 break;
11131 }
11132 case ISD::FFREXP: {
11133 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11134 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11135 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11136
11138 int FrexpExp;
11139 APFloat FrexpMant =
11140 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11141 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11142 SDValue Result1 =
11143 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
11144 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11145 }
11146
11147 break;
11148 }
11150 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11151 "Invalid STRICT_FP_EXTEND!");
11152 assert(VTList.VTs[0].isFloatingPoint() &&
11153 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11154 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11155 "STRICT_FP_EXTEND result type should be vector iff the operand "
11156 "type is vector!");
11157 assert((!VTList.VTs[0].isVector() ||
11158 VTList.VTs[0].getVectorElementCount() ==
11159 Ops[1].getValueType().getVectorElementCount()) &&
11160 "Vector element count mismatch!");
11161 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11162 "Invalid fpext node, dst <= src!");
11163 break;
11165 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11166 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11167 "STRICT_FP_ROUND result type should be vector iff the operand "
11168 "type is vector!");
11169 assert((!VTList.VTs[0].isVector() ||
11170 VTList.VTs[0].getVectorElementCount() ==
11171 Ops[1].getValueType().getVectorElementCount()) &&
11172 "Vector element count mismatch!");
11173 assert(VTList.VTs[0].isFloatingPoint() &&
11174 Ops[1].getValueType().isFloatingPoint() &&
11175 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11176 Ops[2].getOpcode() == ISD::TargetConstant &&
11177 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11178 "Invalid STRICT_FP_ROUND!");
11179 break;
11180 }
11181
11182 // Memoize the node unless it returns a glue result.
11183 SDNode *N;
11184 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11186 AddNodeIDNode(ID, Opcode, VTList, Ops);
11187 void *IP = nullptr;
11188 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11189 E->intersectFlagsWith(Flags);
11190 return SDValue(E, 0);
11191 }
11192
11193 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11194 createOperands(N, Ops);
11195 CSEMap.InsertNode(N, IP);
11196 } else {
11197 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11198 createOperands(N, Ops);
11199 }
11200
11201 N->setFlags(Flags);
11202 InsertNode(N);
11203 SDValue V(N, 0);
11204 NewSDValueDbgMsg(V, "Creating new node: ", this);
11205 return V;
11206}
11207
11208SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11209 SDVTList VTList) {
11210 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11211}
11212
11213SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11214 SDValue N1) {
11215 SDValue Ops[] = { N1 };
11216 return getNode(Opcode, DL, VTList, Ops);
11217}
11218
11219SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11220 SDValue N1, SDValue N2) {
11221 SDValue Ops[] = { N1, N2 };
11222 return getNode(Opcode, DL, VTList, Ops);
11223}
11224
11225SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11226 SDValue N1, SDValue N2, SDValue N3) {
11227 SDValue Ops[] = { N1, N2, N3 };
11228 return getNode(Opcode, DL, VTList, Ops);
11229}
11230
11231SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11232 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11233 SDValue Ops[] = { N1, N2, N3, N4 };
11234 return getNode(Opcode, DL, VTList, Ops);
11235}
11236
11237SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11238 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11239 SDValue N5) {
11240 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11241 return getNode(Opcode, DL, VTList, Ops);
11242}
11243
11245 if (!VT.isExtended())
11246 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11247
11248 return makeVTList(&(*EVTs.insert(VT).first), 1);
11249}
11250
11253 ID.AddInteger(2U);
11254 ID.AddInteger(VT1.getRawBits());
11255 ID.AddInteger(VT2.getRawBits());
11256
11257 void *IP = nullptr;
11258 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11259 if (!Result) {
11260 EVT *Array = Allocator.Allocate<EVT>(2);
11261 Array[0] = VT1;
11262 Array[1] = VT2;
11263 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11264 VTListMap.InsertNode(Result, IP);
11265 }
11266 return Result->getSDVTList();
11267}
11268
11271 ID.AddInteger(3U);
11272 ID.AddInteger(VT1.getRawBits());
11273 ID.AddInteger(VT2.getRawBits());
11274 ID.AddInteger(VT3.getRawBits());
11275
11276 void *IP = nullptr;
11277 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11278 if (!Result) {
11279 EVT *Array = Allocator.Allocate<EVT>(3);
11280 Array[0] = VT1;
11281 Array[1] = VT2;
11282 Array[2] = VT3;
11283 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11284 VTListMap.InsertNode(Result, IP);
11285 }
11286 return Result->getSDVTList();
11287}
11288
11291 ID.AddInteger(4U);
11292 ID.AddInteger(VT1.getRawBits());
11293 ID.AddInteger(VT2.getRawBits());
11294 ID.AddInteger(VT3.getRawBits());
11295 ID.AddInteger(VT4.getRawBits());
11296
11297 void *IP = nullptr;
11298 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11299 if (!Result) {
11300 EVT *Array = Allocator.Allocate<EVT>(4);
11301 Array[0] = VT1;
11302 Array[1] = VT2;
11303 Array[2] = VT3;
11304 Array[3] = VT4;
11305 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11306 VTListMap.InsertNode(Result, IP);
11307 }
11308 return Result->getSDVTList();
11309}
11310
11312 unsigned NumVTs = VTs.size();
11314 ID.AddInteger(NumVTs);
11315 for (unsigned index = 0; index < NumVTs; index++) {
11316 ID.AddInteger(VTs[index].getRawBits());
11317 }
11318
11319 void *IP = nullptr;
11320 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11321 if (!Result) {
11322 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11323 llvm::copy(VTs, Array);
11324 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11325 VTListMap.InsertNode(Result, IP);
11326 }
11327 return Result->getSDVTList();
11328}
11329
11330
11331/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11332/// specified operands. If the resultant node already exists in the DAG,
11333/// this does not modify the specified node, instead it returns the node that
11334/// already exists. If the resultant node does not exist in the DAG, the
11335/// input node is returned. As a degenerate case, if you specify the same
11336/// input operands as the node already has, the input node is returned.
11338 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11339
11340 // Check to see if there is no change.
11341 if (Op == N->getOperand(0)) return N;
11342
11343 // See if the modified node already exists.
11344 void *InsertPos = nullptr;
11345 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11346 return Existing;
11347
11348 // Nope it doesn't. Remove the node from its current place in the maps.
11349 if (InsertPos)
11350 if (!RemoveNodeFromCSEMaps(N))
11351 InsertPos = nullptr;
11352
11353 // Now we update the operands.
11354 N->OperandList[0].set(Op);
11355
11357 // If this gets put into a CSE map, add it.
11358 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11359 return N;
11360}
11361
11363 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11364
11365 // Check to see if there is no change.
11366 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11367 return N; // No operands changed, just return the input node.
11368
11369 // See if the modified node already exists.
11370 void *InsertPos = nullptr;
11371 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11372 return Existing;
11373
11374 // Nope it doesn't. Remove the node from its current place in the maps.
11375 if (InsertPos)
11376 if (!RemoveNodeFromCSEMaps(N))
11377 InsertPos = nullptr;
11378
11379 // Now we update the operands.
11380 if (N->OperandList[0] != Op1)
11381 N->OperandList[0].set(Op1);
11382 if (N->OperandList[1] != Op2)
11383 N->OperandList[1].set(Op2);
11384
11386 // If this gets put into a CSE map, add it.
11387 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11388 return N;
11389}
11390
11393 SDValue Ops[] = { Op1, Op2, Op3 };
11394 return UpdateNodeOperands(N, Ops);
11395}
11396
11399 SDValue Op3, SDValue Op4) {
11400 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11401 return UpdateNodeOperands(N, Ops);
11402}
11403
11406 SDValue Op3, SDValue Op4, SDValue Op5) {
11407 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11408 return UpdateNodeOperands(N, Ops);
11409}
11410
11413 unsigned NumOps = Ops.size();
11414 assert(N->getNumOperands() == NumOps &&
11415 "Update with wrong number of operands");
11416
11417 // If no operands changed just return the input node.
11418 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11419 return N;
11420
11421 // See if the modified node already exists.
11422 void *InsertPos = nullptr;
11423 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11424 return Existing;
11425
11426 // Nope it doesn't. Remove the node from its current place in the maps.
11427 if (InsertPos)
11428 if (!RemoveNodeFromCSEMaps(N))
11429 InsertPos = nullptr;
11430
11431 // Now we update the operands.
11432 for (unsigned i = 0; i != NumOps; ++i)
11433 if (N->OperandList[i] != Ops[i])
11434 N->OperandList[i].set(Ops[i]);
11435
11437 // If this gets put into a CSE map, add it.
11438 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11439 return N;
11440}
11441
11442/// DropOperands - Release the operands and set this node to have
11443/// zero operands.
11445 // Unlike the code in MorphNodeTo that does this, we don't need to
11446 // watch for dead nodes here.
11447 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11448 SDUse &Use = *I++;
11449 Use.set(SDValue());
11450 }
11451}
11452
11454 ArrayRef<MachineMemOperand *> NewMemRefs) {
11455 if (NewMemRefs.empty()) {
11456 N->clearMemRefs();
11457 return;
11458 }
11459
11460 // Check if we can avoid allocating by storing a single reference directly.
11461 if (NewMemRefs.size() == 1) {
11462 N->MemRefs = NewMemRefs[0];
11463 N->NumMemRefs = 1;
11464 return;
11465 }
11466
11467 MachineMemOperand **MemRefsBuffer =
11468 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11469 llvm::copy(NewMemRefs, MemRefsBuffer);
11470 N->MemRefs = MemRefsBuffer;
11471 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11472}
11473
11474/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11475/// machine opcode.
11476///
11478 EVT VT) {
11479 SDVTList VTs = getVTList(VT);
11480 return SelectNodeTo(N, MachineOpc, VTs, {});
11481}
11482
11484 EVT VT, SDValue Op1) {
11485 SDVTList VTs = getVTList(VT);
11486 SDValue Ops[] = { Op1 };
11487 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11488}
11489
11491 EVT VT, SDValue Op1,
11492 SDValue Op2) {
11493 SDVTList VTs = getVTList(VT);
11494 SDValue Ops[] = { Op1, Op2 };
11495 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11496}
11497
11499 EVT VT, SDValue Op1,
11500 SDValue Op2, SDValue Op3) {
11501 SDVTList VTs = getVTList(VT);
11502 SDValue Ops[] = { Op1, Op2, Op3 };
11503 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11504}
11505
11508 SDVTList VTs = getVTList(VT);
11509 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11510}
11511
11513 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11514 SDVTList VTs = getVTList(VT1, VT2);
11515 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11516}
11517
11519 EVT VT1, EVT VT2) {
11520 SDVTList VTs = getVTList(VT1, VT2);
11521 return SelectNodeTo(N, MachineOpc, VTs, {});
11522}
11523
11525 EVT VT1, EVT VT2, EVT VT3,
11527 SDVTList VTs = getVTList(VT1, VT2, VT3);
11528 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11529}
11530
11532 EVT VT1, EVT VT2,
11533 SDValue Op1, SDValue Op2) {
11534 SDVTList VTs = getVTList(VT1, VT2);
11535 SDValue Ops[] = { Op1, Op2 };
11536 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11537}
11538
11541 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11542 // Reset the NodeID to -1.
11543 New->setNodeId(-1);
11544 if (New != N) {
11545 ReplaceAllUsesWith(N, New);
11547 }
11548 return New;
11549}
11550
11551/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11552/// the line number information on the merged node since it is not possible to
11553/// preserve the information that operation is associated with multiple lines.
11554/// This will make the debugger working better at -O0, were there is a higher
11555/// probability having other instructions associated with that line.
11556///
11557/// For IROrder, we keep the smaller of the two
11558SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11559 DebugLoc NLoc = N->getDebugLoc();
11560 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11561 N->setDebugLoc(DebugLoc());
11562 }
11563 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11564 N->setIROrder(Order);
11565 return N;
11566}
11567
11568/// MorphNodeTo - This *mutates* the specified node to have the specified
11569/// return type, opcode, and operands.
11570///
11571/// Note that MorphNodeTo returns the resultant node. If there is already a
11572/// node of the specified opcode and operands, it returns that node instead of
11573/// the current one. Note that the SDLoc need not be the same.
11574///
11575/// Using MorphNodeTo is faster than creating a new node and swapping it in
11576/// with ReplaceAllUsesWith both because it often avoids allocating a new
11577/// node, and because it doesn't require CSE recalculation for any of
11578/// the node's users.
11579///
11580/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11581/// As a consequence it isn't appropriate to use from within the DAG combiner or
11582/// the legalizer which maintain worklists that would need to be updated when
11583/// deleting things.
11586 // If an identical node already exists, use it.
11587 void *IP = nullptr;
11588 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11590 AddNodeIDNode(ID, Opc, VTs, Ops);
11591 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11592 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11593 }
11594
11595 if (!RemoveNodeFromCSEMaps(N))
11596 IP = nullptr;
11597
11598 // Start the morphing.
11599 N->NodeType = Opc;
11600 N->ValueList = VTs.VTs;
11601 N->NumValues = VTs.NumVTs;
11602
11603 // Clear the operands list, updating used nodes to remove this from their
11604 // use list. Keep track of any operands that become dead as a result.
11605 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11606 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11607 SDUse &Use = *I++;
11608 SDNode *Used = Use.getNode();
11609 Use.set(SDValue());
11610 if (Used->use_empty())
11611 DeadNodeSet.insert(Used);
11612 }
11613
11614 // For MachineNode, initialize the memory references information.
11616 MN->clearMemRefs();
11617
11618 // Swap for an appropriately sized array from the recycler.
11619 removeOperands(N);
11620 createOperands(N, Ops);
11621
11622 // Delete any nodes that are still dead after adding the uses for the
11623 // new operands.
11624 if (!DeadNodeSet.empty()) {
11625 SmallVector<SDNode *, 16> DeadNodes;
11626 for (SDNode *N : DeadNodeSet)
11627 if (N->use_empty())
11628 DeadNodes.push_back(N);
11629 RemoveDeadNodes(DeadNodes);
11630 }
11631
11632 if (IP)
11633 CSEMap.InsertNode(N, IP); // Memoize the new node.
11634 return N;
11635}
11636
11638 unsigned OrigOpc = Node->getOpcode();
11639 unsigned NewOpc;
11640 switch (OrigOpc) {
11641 default:
11642 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11643#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11644 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11645#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11646 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11647#include "llvm/IR/ConstrainedOps.def"
11648 }
11649
11650 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11651
11652 // We're taking this node out of the chain, so we need to re-link things.
11653 SDValue InputChain = Node->getOperand(0);
11654 SDValue OutputChain = SDValue(Node, 1);
11655 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11656
11658 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11659 Ops.push_back(Node->getOperand(i));
11660
11661 SDVTList VTs = getVTList(Node->getValueType(0));
11662 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11663
11664 // MorphNodeTo can operate in two ways: if an existing node with the
11665 // specified operands exists, it can just return it. Otherwise, it
11666 // updates the node in place to have the requested operands.
11667 if (Res == Node) {
11668 // If we updated the node in place, reset the node ID. To the isel,
11669 // this should be just like a newly allocated machine node.
11670 Res->setNodeId(-1);
11671 } else {
11674 }
11675
11676 return Res;
11677}
11678
11679/// getMachineNode - These are used for target selectors to create a new node
11680/// with specified return type(s), MachineInstr opcode, and operands.
11681///
11682/// Note that getMachineNode returns the resultant node. If there is already a
11683/// node of the specified opcode and operands, it returns that node instead of
11684/// the current one.
11686 EVT VT) {
11687 SDVTList VTs = getVTList(VT);
11688 return getMachineNode(Opcode, dl, VTs, {});
11689}
11690
11692 EVT VT, SDValue Op1) {
11693 SDVTList VTs = getVTList(VT);
11694 SDValue Ops[] = { Op1 };
11695 return getMachineNode(Opcode, dl, VTs, Ops);
11696}
11697
11699 EVT VT, SDValue Op1, SDValue Op2) {
11700 SDVTList VTs = getVTList(VT);
11701 SDValue Ops[] = { Op1, Op2 };
11702 return getMachineNode(Opcode, dl, VTs, Ops);
11703}
11704
11706 EVT VT, SDValue Op1, SDValue Op2,
11707 SDValue Op3) {
11708 SDVTList VTs = getVTList(VT);
11709 SDValue Ops[] = { Op1, Op2, Op3 };
11710 return getMachineNode(Opcode, dl, VTs, Ops);
11711}
11712
11715 SDVTList VTs = getVTList(VT);
11716 return getMachineNode(Opcode, dl, VTs, Ops);
11717}
11718
11720 EVT VT1, EVT VT2, SDValue Op1,
11721 SDValue Op2) {
11722 SDVTList VTs = getVTList(VT1, VT2);
11723 SDValue Ops[] = { Op1, Op2 };
11724 return getMachineNode(Opcode, dl, VTs, Ops);
11725}
11726
11728 EVT VT1, EVT VT2, SDValue Op1,
11729 SDValue Op2, SDValue Op3) {
11730 SDVTList VTs = getVTList(VT1, VT2);
11731 SDValue Ops[] = { Op1, Op2, Op3 };
11732 return getMachineNode(Opcode, dl, VTs, Ops);
11733}
11734
11736 EVT VT1, EVT VT2,
11738 SDVTList VTs = getVTList(VT1, VT2);
11739 return getMachineNode(Opcode, dl, VTs, Ops);
11740}
11741
11743 EVT VT1, EVT VT2, EVT VT3,
11744 SDValue Op1, SDValue Op2) {
11745 SDVTList VTs = getVTList(VT1, VT2, VT3);
11746 SDValue Ops[] = { Op1, Op2 };
11747 return getMachineNode(Opcode, dl, VTs, Ops);
11748}
11749
11751 EVT VT1, EVT VT2, EVT VT3,
11752 SDValue Op1, SDValue Op2,
11753 SDValue Op3) {
11754 SDVTList VTs = getVTList(VT1, VT2, VT3);
11755 SDValue Ops[] = { Op1, Op2, Op3 };
11756 return getMachineNode(Opcode, dl, VTs, Ops);
11757}
11758
11760 EVT VT1, EVT VT2, EVT VT3,
11762 SDVTList VTs = getVTList(VT1, VT2, VT3);
11763 return getMachineNode(Opcode, dl, VTs, Ops);
11764}
11765
11767 ArrayRef<EVT> ResultTys,
11769 SDVTList VTs = getVTList(ResultTys);
11770 return getMachineNode(Opcode, dl, VTs, Ops);
11771}
11772
11774 SDVTList VTs,
11776 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11778 void *IP = nullptr;
11779
11780 if (DoCSE) {
11782 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11783 IP = nullptr;
11784 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11785 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11786 }
11787 }
11788
11789 // Allocate a new MachineSDNode.
11790 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11791 createOperands(N, Ops);
11792
11793 if (DoCSE)
11794 CSEMap.InsertNode(N, IP);
11795
11796 InsertNode(N);
11797 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11798 return N;
11799}
11800
11801/// getTargetExtractSubreg - A convenience function for creating
11802/// TargetOpcode::EXTRACT_SUBREG nodes.
11804 SDValue Operand) {
11805 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11806 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11807 VT, Operand, SRIdxVal);
11808 return SDValue(Subreg, 0);
11809}
11810
11811/// getTargetInsertSubreg - A convenience function for creating
11812/// TargetOpcode::INSERT_SUBREG nodes.
11814 SDValue Operand, SDValue Subreg) {
11815 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11816 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11817 VT, Operand, Subreg, SRIdxVal);
11818 return SDValue(Result, 0);
11819}
11820
11821/// getNodeIfExists - Get the specified node if it's already available, or
11822/// else return NULL.
11825 SDNodeFlags Flags;
11826 if (Inserter)
11827 Flags = Inserter->getFlags();
11828 return getNodeIfExists(Opcode, VTList, Ops, Flags);
11829}
11830
11833 const SDNodeFlags Flags) {
11834 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11836 AddNodeIDNode(ID, Opcode, VTList, Ops);
11837 void *IP = nullptr;
11838 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
11839 E->intersectFlagsWith(Flags);
11840 return E;
11841 }
11842 }
11843 return nullptr;
11844}
11845
11846/// doesNodeExist - Check if a node exists without modifying its flags.
11847bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11849 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11851 AddNodeIDNode(ID, Opcode, VTList, Ops);
11852 void *IP = nullptr;
11853 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11854 return true;
11855 }
11856 return false;
11857}
11858
11859/// getDbgValue - Creates a SDDbgValue node.
11860///
11861/// SDNode
11863 SDNode *N, unsigned R, bool IsIndirect,
11864 const DebugLoc &DL, unsigned O) {
11865 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11866 "Expected inlined-at fields to agree");
11867 return new (DbgInfo->getAlloc())
11868 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11869 {}, IsIndirect, DL, O,
11870 /*IsVariadic=*/false);
11871}
11872
11873/// Constant
11875 DIExpression *Expr,
11876 const Value *C,
11877 const DebugLoc &DL, unsigned O) {
11878 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11879 "Expected inlined-at fields to agree");
11880 return new (DbgInfo->getAlloc())
11881 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11882 /*IsIndirect=*/false, DL, O,
11883 /*IsVariadic=*/false);
11884}
11885
11886/// FrameIndex
11888 DIExpression *Expr, unsigned FI,
11889 bool IsIndirect,
11890 const DebugLoc &DL,
11891 unsigned O) {
11892 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11893 "Expected inlined-at fields to agree");
11894 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11895}
11896
11897/// FrameIndex with dependencies
11899 DIExpression *Expr, unsigned FI,
11900 ArrayRef<SDNode *> Dependencies,
11901 bool IsIndirect,
11902 const DebugLoc &DL,
11903 unsigned O) {
11904 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11905 "Expected inlined-at fields to agree");
11906 return new (DbgInfo->getAlloc())
11907 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11908 Dependencies, IsIndirect, DL, O,
11909 /*IsVariadic=*/false);
11910}
11911
11912/// VReg
11914 Register VReg, bool IsIndirect,
11915 const DebugLoc &DL, unsigned O) {
11916 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11917 "Expected inlined-at fields to agree");
11918 return new (DbgInfo->getAlloc())
11919 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11920 {}, IsIndirect, DL, O,
11921 /*IsVariadic=*/false);
11922}
11923
11926 ArrayRef<SDNode *> Dependencies,
11927 bool IsIndirect, const DebugLoc &DL,
11928 unsigned O, bool IsVariadic) {
11929 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11930 "Expected inlined-at fields to agree");
11931 return new (DbgInfo->getAlloc())
11932 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11933 DL, O, IsVariadic);
11934}
11935
11937 unsigned OffsetInBits, unsigned SizeInBits,
11938 bool InvalidateDbg) {
11939 SDNode *FromNode = From.getNode();
11940 SDNode *ToNode = To.getNode();
11941 assert(FromNode && ToNode && "Can't modify dbg values");
11942
11943 // PR35338
11944 // TODO: assert(From != To && "Redundant dbg value transfer");
11945 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11946 if (From == To || FromNode == ToNode)
11947 return;
11948
11949 if (!FromNode->getHasDebugValue())
11950 return;
11951
11952 SDDbgOperand FromLocOp =
11953 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11955
11957 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11958 if (Dbg->isInvalidated())
11959 continue;
11960
11961 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11962
11963 // Create a new location ops vector that is equal to the old vector, but
11964 // with each instance of FromLocOp replaced with ToLocOp.
11965 bool Changed = false;
11966 auto NewLocOps = Dbg->copyLocationOps();
11967 std::replace_if(
11968 NewLocOps.begin(), NewLocOps.end(),
11969 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11970 bool Match = Op == FromLocOp;
11971 Changed |= Match;
11972 return Match;
11973 },
11974 ToLocOp);
11975 // Ignore this SDDbgValue if we didn't find a matching location.
11976 if (!Changed)
11977 continue;
11978
11979 DIVariable *Var = Dbg->getVariable();
11980 auto *Expr = Dbg->getExpression();
11981 // If a fragment is requested, update the expression.
11982 if (SizeInBits) {
11983 // When splitting a larger (e.g., sign-extended) value whose
11984 // lower bits are described with an SDDbgValue, do not attempt
11985 // to transfer the SDDbgValue to the upper bits.
11986 if (auto FI = Expr->getFragmentInfo())
11987 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11988 continue;
11989 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11990 SizeInBits);
11991 if (!Fragment)
11992 continue;
11993 Expr = *Fragment;
11994 }
11995
11996 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11997 // Clone the SDDbgValue and move it to To.
11998 SDDbgValue *Clone = getDbgValueList(
11999 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12000 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12001 Dbg->isVariadic());
12002 ClonedDVs.push_back(Clone);
12003
12004 if (InvalidateDbg) {
12005 // Invalidate value and indicate the SDDbgValue should not be emitted.
12006 Dbg->setIsInvalidated();
12007 Dbg->setIsEmitted();
12008 }
12009 }
12010
12011 for (SDDbgValue *Dbg : ClonedDVs) {
12012 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12013 "Transferred DbgValues should depend on the new SDNode");
12014 AddDbgValue(Dbg, false);
12015 }
12016}
12017
12019 if (!N.getHasDebugValue())
12020 return;
12021
12022 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12023 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12024 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12025 return SDDbgOperand::fromNode(Node, ResNo);
12026 };
12027
12029 for (auto *DV : GetDbgValues(&N)) {
12030 if (DV->isInvalidated())
12031 continue;
12032 switch (N.getOpcode()) {
12033 default:
12034 break;
12035 case ISD::ADD: {
12036 SDValue N0 = N.getOperand(0);
12037 SDValue N1 = N.getOperand(1);
12038 if (!isa<ConstantSDNode>(N0)) {
12039 bool RHSConstant = isa<ConstantSDNode>(N1);
12041 if (RHSConstant)
12042 Offset = N.getConstantOperandVal(1);
12043 // We are not allowed to turn indirect debug values variadic, so
12044 // don't salvage those.
12045 if (!RHSConstant && DV->isIndirect())
12046 continue;
12047
12048 // Rewrite an ADD constant node into a DIExpression. Since we are
12049 // performing arithmetic to compute the variable's *value* in the
12050 // DIExpression, we need to mark the expression with a
12051 // DW_OP_stack_value.
12052 auto *DIExpr = DV->getExpression();
12053 auto NewLocOps = DV->copyLocationOps();
12054 bool Changed = false;
12055 size_t OrigLocOpsSize = NewLocOps.size();
12056 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12057 // We're not given a ResNo to compare against because the whole
12058 // node is going away. We know that any ISD::ADD only has one
12059 // result, so we can assume any node match is using the result.
12060 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12061 NewLocOps[i].getSDNode() != &N)
12062 continue;
12063 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12064 if (RHSConstant) {
12067 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12068 } else {
12069 // Convert to a variadic expression (if not already).
12070 // convertToVariadicExpression() returns a const pointer, so we use
12071 // a temporary const variable here.
12072 const auto *TmpDIExpr =
12076 ExprOps.push_back(NewLocOps.size());
12077 ExprOps.push_back(dwarf::DW_OP_plus);
12078 SDDbgOperand RHS =
12080 NewLocOps.push_back(RHS);
12081 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12082 }
12083 Changed = true;
12084 }
12085 (void)Changed;
12086 assert(Changed && "Salvage target doesn't use N");
12087
12088 bool IsVariadic =
12089 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12090
12091 auto AdditionalDependencies = DV->getAdditionalDependencies();
12092 SDDbgValue *Clone = getDbgValueList(
12093 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12094 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12095 ClonedDVs.push_back(Clone);
12096 DV->setIsInvalidated();
12097 DV->setIsEmitted();
12098 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12099 N0.getNode()->dumprFull(this);
12100 dbgs() << " into " << *DIExpr << '\n');
12101 }
12102 break;
12103 }
12104 case ISD::TRUNCATE: {
12105 SDValue N0 = N.getOperand(0);
12106 TypeSize FromSize = N0.getValueSizeInBits();
12107 TypeSize ToSize = N.getValueSizeInBits(0);
12108
12109 DIExpression *DbgExpression = DV->getExpression();
12110 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12111 auto NewLocOps = DV->copyLocationOps();
12112 bool Changed = false;
12113 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12114 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12115 NewLocOps[i].getSDNode() != &N)
12116 continue;
12117
12118 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12119 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12120 Changed = true;
12121 }
12122 assert(Changed && "Salvage target doesn't use N");
12123 (void)Changed;
12124
12125 SDDbgValue *Clone =
12126 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12127 DV->getAdditionalDependencies(), DV->isIndirect(),
12128 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12129
12130 ClonedDVs.push_back(Clone);
12131 DV->setIsInvalidated();
12132 DV->setIsEmitted();
12133 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12134 dbgs() << " into " << *DbgExpression << '\n');
12135 break;
12136 }
12137 }
12138 }
12139
12140 for (SDDbgValue *Dbg : ClonedDVs) {
12141 assert((!Dbg->getSDNodes().empty() ||
12142 llvm::any_of(Dbg->getLocationOps(),
12143 [&](const SDDbgOperand &Op) {
12144 return Op.getKind() == SDDbgOperand::FRAMEIX;
12145 })) &&
12146 "Salvaged DbgValue should depend on a new SDNode");
12147 AddDbgValue(Dbg, false);
12148 }
12149}
12150
12151/// Creates a SDDbgLabel node.
12153 const DebugLoc &DL, unsigned O) {
12154 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12155 "Expected inlined-at fields to agree");
12156 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12157}
12158
12159namespace {
12160
12161/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12162/// pointed to by a use iterator is deleted, increment the use iterator
12163/// so that it doesn't dangle.
12164///
12165class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12168
12169 void NodeDeleted(SDNode *N, SDNode *E) override {
12170 // Increment the iterator as needed.
12171 while (UI != UE && N == UI->getUser())
12172 ++UI;
12173 }
12174
12175public:
12176 RAUWUpdateListener(SelectionDAG &d,
12179 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12180};
12181
12182} // end anonymous namespace
12183
12184/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12185/// This can cause recursive merging of nodes in the DAG.
12186///
12187/// This version assumes From has a single result value.
12188///
12190 SDNode *From = FromN.getNode();
12191 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12192 "Cannot replace with this method!");
12193 assert(From != To.getNode() && "Cannot replace uses of with self");
12194
12195 // Preserve Debug Values
12196 transferDbgValues(FromN, To);
12197 // Preserve extra info.
12198 copyExtraInfo(From, To.getNode());
12199
12200 // Iterate over all the existing uses of From. New uses will be added
12201 // to the beginning of the use list, which we avoid visiting.
12202 // This specifically avoids visiting uses of From that arise while the
12203 // replacement is happening, because any such uses would be the result
12204 // of CSE: If an existing node looks like From after one of its operands
12205 // is replaced by To, we don't want to replace of all its users with To
12206 // too. See PR3018 for more info.
12207 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12208 RAUWUpdateListener Listener(*this, UI, UE);
12209 while (UI != UE) {
12210 SDNode *User = UI->getUser();
12211
12212 // This node is about to morph, remove its old self from the CSE maps.
12213 RemoveNodeFromCSEMaps(User);
12214
12215 // A user can appear in a use list multiple times, and when this
12216 // happens the uses are usually next to each other in the list.
12217 // To help reduce the number of CSE recomputations, process all
12218 // the uses of this user that we can find this way.
12219 do {
12220 SDUse &Use = *UI;
12221 ++UI;
12222 Use.set(To);
12223 if (To->isDivergent() != From->isDivergent())
12225 } while (UI != UE && UI->getUser() == User);
12226 // Now that we have modified User, add it back to the CSE maps. If it
12227 // already exists there, recursively merge the results together.
12228 AddModifiedNodeToCSEMaps(User);
12229 }
12230
12231 // If we just RAUW'd the root, take note.
12232 if (FromN == getRoot())
12233 setRoot(To);
12234}
12235
12236/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12237/// This can cause recursive merging of nodes in the DAG.
12238///
12239/// This version assumes that for each value of From, there is a
12240/// corresponding value in To in the same position with the same type.
12241///
12243#ifndef NDEBUG
12244 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12245 assert((!From->hasAnyUseOfValue(i) ||
12246 From->getValueType(i) == To->getValueType(i)) &&
12247 "Cannot use this version of ReplaceAllUsesWith!");
12248#endif
12249
12250 // Handle the trivial case.
12251 if (From == To)
12252 return;
12253
12254 // Preserve Debug Info. Only do this if there's a use.
12255 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12256 if (From->hasAnyUseOfValue(i)) {
12257 assert((i < To->getNumValues()) && "Invalid To location");
12258 transferDbgValues(SDValue(From, i), SDValue(To, i));
12259 }
12260 // Preserve extra info.
12261 copyExtraInfo(From, To);
12262
12263 // Iterate over just the existing users of From. See the comments in
12264 // the ReplaceAllUsesWith above.
12265 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12266 RAUWUpdateListener Listener(*this, UI, UE);
12267 while (UI != UE) {
12268 SDNode *User = UI->getUser();
12269
12270 // This node is about to morph, remove its old self from the CSE maps.
12271 RemoveNodeFromCSEMaps(User);
12272
12273 // A user can appear in a use list multiple times, and when this
12274 // happens the uses are usually next to each other in the list.
12275 // To help reduce the number of CSE recomputations, process all
12276 // the uses of this user that we can find this way.
12277 do {
12278 SDUse &Use = *UI;
12279 ++UI;
12280 Use.setNode(To);
12281 if (To->isDivergent() != From->isDivergent())
12283 } while (UI != UE && UI->getUser() == User);
12284
12285 // Now that we have modified User, add it back to the CSE maps. If it
12286 // already exists there, recursively merge the results together.
12287 AddModifiedNodeToCSEMaps(User);
12288 }
12289
12290 // If we just RAUW'd the root, take note.
12291 if (From == getRoot().getNode())
12292 setRoot(SDValue(To, getRoot().getResNo()));
12293}
12294
12295/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12296/// This can cause recursive merging of nodes in the DAG.
12297///
12298/// This version can replace From with any result values. To must match the
12299/// number and types of values returned by From.
12301 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12302 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12303
12304 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12305 // Preserve Debug Info.
12306 transferDbgValues(SDValue(From, i), To[i]);
12307 // Preserve extra info.
12308 copyExtraInfo(From, To[i].getNode());
12309 }
12310
12311 // Iterate over just the existing users of From. See the comments in
12312 // the ReplaceAllUsesWith above.
12313 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12314 RAUWUpdateListener Listener(*this, UI, UE);
12315 while (UI != UE) {
12316 SDNode *User = UI->getUser();
12317
12318 // This node is about to morph, remove its old self from the CSE maps.
12319 RemoveNodeFromCSEMaps(User);
12320
12321 // A user can appear in a use list multiple times, and when this happens the
12322 // uses are usually next to each other in the list. To help reduce the
12323 // number of CSE and divergence recomputations, process all the uses of this
12324 // user that we can find this way.
12325 bool To_IsDivergent = false;
12326 do {
12327 SDUse &Use = *UI;
12328 const SDValue &ToOp = To[Use.getResNo()];
12329 ++UI;
12330 Use.set(ToOp);
12331 To_IsDivergent |= ToOp->isDivergent();
12332 } while (UI != UE && UI->getUser() == User);
12333
12334 if (To_IsDivergent != From->isDivergent())
12336
12337 // Now that we have modified User, add it back to the CSE maps. If it
12338 // already exists there, recursively merge the results together.
12339 AddModifiedNodeToCSEMaps(User);
12340 }
12341
12342 // If we just RAUW'd the root, take note.
12343 if (From == getRoot().getNode())
12344 setRoot(SDValue(To[getRoot().getResNo()]));
12345}
12346
12347/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12348/// uses of other values produced by From.getNode() alone. The Deleted
12349/// vector is handled the same way as for ReplaceAllUsesWith.
12351 // Handle the really simple, really trivial case efficiently.
12352 if (From == To) return;
12353
12354 // Handle the simple, trivial, case efficiently.
12355 if (From.getNode()->getNumValues() == 1) {
12356 ReplaceAllUsesWith(From, To);
12357 return;
12358 }
12359
12360 // Preserve Debug Info.
12361 transferDbgValues(From, To);
12362 copyExtraInfo(From.getNode(), To.getNode());
12363
12364 // Iterate over just the existing users of From. See the comments in
12365 // the ReplaceAllUsesWith above.
12366 SDNode::use_iterator UI = From.getNode()->use_begin(),
12367 UE = From.getNode()->use_end();
12368 RAUWUpdateListener Listener(*this, UI, UE);
12369 while (UI != UE) {
12370 SDNode *User = UI->getUser();
12371 bool UserRemovedFromCSEMaps = false;
12372
12373 // A user can appear in a use list multiple times, and when this
12374 // happens the uses are usually next to each other in the list.
12375 // To help reduce the number of CSE recomputations, process all
12376 // the uses of this user that we can find this way.
12377 do {
12378 SDUse &Use = *UI;
12379
12380 // Skip uses of different values from the same node.
12381 if (Use.getResNo() != From.getResNo()) {
12382 ++UI;
12383 continue;
12384 }
12385
12386 // If this node hasn't been modified yet, it's still in the CSE maps,
12387 // so remove its old self from the CSE maps.
12388 if (!UserRemovedFromCSEMaps) {
12389 RemoveNodeFromCSEMaps(User);
12390 UserRemovedFromCSEMaps = true;
12391 }
12392
12393 ++UI;
12394 Use.set(To);
12395 if (To->isDivergent() != From->isDivergent())
12397 } while (UI != UE && UI->getUser() == User);
12398 // We are iterating over all uses of the From node, so if a use
12399 // doesn't use the specific value, no changes are made.
12400 if (!UserRemovedFromCSEMaps)
12401 continue;
12402
12403 // Now that we have modified User, add it back to the CSE maps. If it
12404 // already exists there, recursively merge the results together.
12405 AddModifiedNodeToCSEMaps(User);
12406 }
12407
12408 // If we just RAUW'd the root, take note.
12409 if (From == getRoot())
12410 setRoot(To);
12411}
12412
12413namespace {
12414
12415/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12416/// to record information about a use.
12417struct UseMemo {
12418 SDNode *User;
12419 unsigned Index;
12420 SDUse *Use;
12421};
12422
12423/// operator< - Sort Memos by User.
12424bool operator<(const UseMemo &L, const UseMemo &R) {
12425 return (intptr_t)L.User < (intptr_t)R.User;
12426}
12427
12428/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12429/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12430/// the node already has been taken care of recursively.
12431class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12432 SmallVectorImpl<UseMemo> &Uses;
12433
12434 void NodeDeleted(SDNode *N, SDNode *E) override {
12435 for (UseMemo &Memo : Uses)
12436 if (Memo.User == N)
12437 Memo.User = nullptr;
12438 }
12439
12440public:
12441 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12442 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12443};
12444
12445} // end anonymous namespace
12446
12447/// Return true if a glue output should propagate divergence information.
12449 switch (Node->getOpcode()) {
12450 case ISD::CopyFromReg:
12451 case ISD::CopyToReg:
12452 return false;
12453 default:
12454 return true;
12455 }
12456
12457 llvm_unreachable("covered opcode switch");
12458}
12459
12461 if (TLI->isSDNodeAlwaysUniform(N)) {
12462 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12463 "Conflicting divergence information!");
12464 return false;
12465 }
12466 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12467 return true;
12468 for (const auto &Op : N->ops()) {
12469 EVT VT = Op.getValueType();
12470
12471 // Skip Chain. It does not carry divergence.
12472 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12473 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12474 return true;
12475 }
12476 return false;
12477}
12478
12480 SmallVector<SDNode *, 16> Worklist(1, N);
12481 do {
12482 N = Worklist.pop_back_val();
12483 bool IsDivergent = calculateDivergence(N);
12484 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12485 N->SDNodeBits.IsDivergent = IsDivergent;
12486 llvm::append_range(Worklist, N->users());
12487 }
12488 } while (!Worklist.empty());
12489}
12490
12491void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12493 Order.reserve(AllNodes.size());
12494 for (auto &N : allnodes()) {
12495 unsigned NOps = N.getNumOperands();
12496 Degree[&N] = NOps;
12497 if (0 == NOps)
12498 Order.push_back(&N);
12499 }
12500 for (size_t I = 0; I != Order.size(); ++I) {
12501 SDNode *N = Order[I];
12502 for (auto *U : N->users()) {
12503 unsigned &UnsortedOps = Degree[U];
12504 if (0 == --UnsortedOps)
12505 Order.push_back(U);
12506 }
12507 }
12508}
12509
12510#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12511void SelectionDAG::VerifyDAGDivergence() {
12512 std::vector<SDNode *> TopoOrder;
12513 CreateTopologicalOrder(TopoOrder);
12514 for (auto *N : TopoOrder) {
12515 assert(calculateDivergence(N) == N->isDivergent() &&
12516 "Divergence bit inconsistency detected");
12517 }
12518}
12519#endif
12520
12521/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12522/// uses of other values produced by From.getNode() alone. The same value
12523/// may appear in both the From and To list. The Deleted vector is
12524/// handled the same way as for ReplaceAllUsesWith.
12526 const SDValue *To,
12527 unsigned Num){
12528 // Handle the simple, trivial case efficiently.
12529 if (Num == 1)
12530 return ReplaceAllUsesOfValueWith(*From, *To);
12531
12532 transferDbgValues(*From, *To);
12533 copyExtraInfo(From->getNode(), To->getNode());
12534
12535 // Read up all the uses and make records of them. This helps
12536 // processing new uses that are introduced during the
12537 // replacement process.
12539 for (unsigned i = 0; i != Num; ++i) {
12540 unsigned FromResNo = From[i].getResNo();
12541 SDNode *FromNode = From[i].getNode();
12542 for (SDUse &Use : FromNode->uses()) {
12543 if (Use.getResNo() == FromResNo) {
12544 UseMemo Memo = {Use.getUser(), i, &Use};
12545 Uses.push_back(Memo);
12546 }
12547 }
12548 }
12549
12550 // Sort the uses, so that all the uses from a given User are together.
12552 RAUOVWUpdateListener Listener(*this, Uses);
12553
12554 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12555 UseIndex != UseIndexEnd; ) {
12556 // We know that this user uses some value of From. If it is the right
12557 // value, update it.
12558 SDNode *User = Uses[UseIndex].User;
12559 // If the node has been deleted by recursive CSE updates when updating
12560 // another node, then just skip this entry.
12561 if (User == nullptr) {
12562 ++UseIndex;
12563 continue;
12564 }
12565
12566 // This node is about to morph, remove its old self from the CSE maps.
12567 RemoveNodeFromCSEMaps(User);
12568
12569 // The Uses array is sorted, so all the uses for a given User
12570 // are next to each other in the list.
12571 // To help reduce the number of CSE recomputations, process all
12572 // the uses of this user that we can find this way.
12573 do {
12574 unsigned i = Uses[UseIndex].Index;
12575 SDUse &Use = *Uses[UseIndex].Use;
12576 ++UseIndex;
12577
12578 Use.set(To[i]);
12579 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12580
12581 // Now that we have modified User, add it back to the CSE maps. If it
12582 // already exists there, recursively merge the results together.
12583 AddModifiedNodeToCSEMaps(User);
12584 }
12585}
12586
12587/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12588/// based on their topological order. It returns the maximum id and a vector
12589/// of the SDNodes* in assigned order by reference.
12591 unsigned DAGSize = 0;
12592
12593 // SortedPos tracks the progress of the algorithm. Nodes before it are
12594 // sorted, nodes after it are unsorted. When the algorithm completes
12595 // it is at the end of the list.
12596 allnodes_iterator SortedPos = allnodes_begin();
12597
12598 // Visit all the nodes. Move nodes with no operands to the front of
12599 // the list immediately. Annotate nodes that do have operands with their
12600 // operand count. Before we do this, the Node Id fields of the nodes
12601 // may contain arbitrary values. After, the Node Id fields for nodes
12602 // before SortedPos will contain the topological sort index, and the
12603 // Node Id fields for nodes At SortedPos and after will contain the
12604 // count of outstanding operands.
12606 checkForCycles(&N, this);
12607 unsigned Degree = N.getNumOperands();
12608 if (Degree == 0) {
12609 // A node with no uses, add it to the result array immediately.
12610 N.setNodeId(DAGSize++);
12611 allnodes_iterator Q(&N);
12612 if (Q != SortedPos)
12613 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12614 assert(SortedPos != AllNodes.end() && "Overran node list");
12615 ++SortedPos;
12616 } else {
12617 // Temporarily use the Node Id as scratch space for the degree count.
12618 N.setNodeId(Degree);
12619 }
12620 }
12621
12622 // Visit all the nodes. As we iterate, move nodes into sorted order,
12623 // such that by the time the end is reached all nodes will be sorted.
12624 for (SDNode &Node : allnodes()) {
12625 SDNode *N = &Node;
12626 checkForCycles(N, this);
12627 // N is in sorted position, so all its uses have one less operand
12628 // that needs to be sorted.
12629 for (SDNode *P : N->users()) {
12630 unsigned Degree = P->getNodeId();
12631 assert(Degree != 0 && "Invalid node degree");
12632 --Degree;
12633 if (Degree == 0) {
12634 // All of P's operands are sorted, so P may sorted now.
12635 P->setNodeId(DAGSize++);
12636 if (P->getIterator() != SortedPos)
12637 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12638 assert(SortedPos != AllNodes.end() && "Overran node list");
12639 ++SortedPos;
12640 } else {
12641 // Update P's outstanding operand count.
12642 P->setNodeId(Degree);
12643 }
12644 }
12645 if (Node.getIterator() == SortedPos) {
12646#ifndef NDEBUG
12648 SDNode *S = &*++I;
12649 dbgs() << "Overran sorted position:\n";
12650 S->dumprFull(this); dbgs() << "\n";
12651 dbgs() << "Checking if this is due to cycles\n";
12652 checkForCycles(this, true);
12653#endif
12654 llvm_unreachable(nullptr);
12655 }
12656 }
12657
12658 assert(SortedPos == AllNodes.end() &&
12659 "Topological sort incomplete!");
12660 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12661 "First node in topological sort is not the entry token!");
12662 assert(AllNodes.front().getNodeId() == 0 &&
12663 "First node in topological sort has non-zero id!");
12664 assert(AllNodes.front().getNumOperands() == 0 &&
12665 "First node in topological sort has operands!");
12666 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12667 "Last node in topologic sort has unexpected id!");
12668 assert(AllNodes.back().use_empty() &&
12669 "Last node in topologic sort has users!");
12670 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12671 return DAGSize;
12672}
12673
12674/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12675/// value is produced by SD.
12676void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12677 for (SDNode *SD : DB->getSDNodes()) {
12678 if (!SD)
12679 continue;
12680 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12681 SD->setHasDebugValue(true);
12682 }
12683 DbgInfo->add(DB, isParameter);
12684}
12685
12686void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12687
12689 SDValue NewMemOpChain) {
12690 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12691 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12692 // The new memory operation must have the same position as the old load in
12693 // terms of memory dependency. Create a TokenFactor for the old load and new
12694 // memory operation and update uses of the old load's output chain to use that
12695 // TokenFactor.
12696 if (OldChain == NewMemOpChain || OldChain.use_empty())
12697 return NewMemOpChain;
12698
12699 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12700 OldChain, NewMemOpChain);
12701 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12702 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12703 return TokenFactor;
12704}
12705
12707 SDValue NewMemOp) {
12708 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12709 SDValue OldChain = SDValue(OldLoad, 1);
12710 SDValue NewMemOpChain = NewMemOp.getValue(1);
12711 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12712}
12713
12715 Function **OutFunction) {
12716 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12717
12718 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12719 auto *Module = MF->getFunction().getParent();
12720 auto *Function = Module->getFunction(Symbol);
12721
12722 if (OutFunction != nullptr)
12723 *OutFunction = Function;
12724
12725 if (Function != nullptr) {
12726 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12727 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12728 }
12729
12730 std::string ErrorStr;
12731 raw_string_ostream ErrorFormatter(ErrorStr);
12732 ErrorFormatter << "Undefined external symbol ";
12733 ErrorFormatter << '"' << Symbol << '"';
12734 report_fatal_error(Twine(ErrorStr));
12735}
12736
12737//===----------------------------------------------------------------------===//
12738// SDNode Class
12739//===----------------------------------------------------------------------===//
12740
12743 return Const != nullptr && Const->isZero();
12744}
12745
12747 return V.isUndef() || isNullConstant(V);
12748}
12749
12752 return Const != nullptr && Const->isZero() && !Const->isNegative();
12753}
12754
12757 return Const != nullptr && Const->isAllOnes();
12758}
12759
12762 return Const != nullptr && Const->isOne();
12763}
12764
12767 return Const != nullptr && Const->isMinSignedValue();
12768}
12769
12770bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12771 unsigned OperandNo) {
12772 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12773 // TODO: Target-specific opcodes could be added.
12774 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12775 /*AllowTruncation*/ true)) {
12776 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12777 switch (Opcode) {
12778 case ISD::ADD:
12779 case ISD::OR:
12780 case ISD::XOR:
12781 case ISD::UMAX:
12782 return Const.isZero();
12783 case ISD::MUL:
12784 return Const.isOne();
12785 case ISD::AND:
12786 case ISD::UMIN:
12787 return Const.isAllOnes();
12788 case ISD::SMAX:
12789 return Const.isMinSignedValue();
12790 case ISD::SMIN:
12791 return Const.isMaxSignedValue();
12792 case ISD::SUB:
12793 case ISD::SHL:
12794 case ISD::SRA:
12795 case ISD::SRL:
12796 return OperandNo == 1 && Const.isZero();
12797 case ISD::UDIV:
12798 case ISD::SDIV:
12799 return OperandNo == 1 && Const.isOne();
12800 }
12801 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12802 switch (Opcode) {
12803 case ISD::FADD:
12804 return ConstFP->isZero() &&
12805 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12806 case ISD::FSUB:
12807 return OperandNo == 1 && ConstFP->isZero() &&
12808 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12809 case ISD::FMUL:
12810 return ConstFP->isExactlyValue(1.0);
12811 case ISD::FDIV:
12812 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12813 case ISD::FMINNUM:
12814 case ISD::FMAXNUM: {
12815 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12816 EVT VT = V.getValueType();
12817 const fltSemantics &Semantics = VT.getFltSemantics();
12818 APFloat NeutralAF = !Flags.hasNoNaNs()
12819 ? APFloat::getQNaN(Semantics)
12820 : !Flags.hasNoInfs()
12821 ? APFloat::getInf(Semantics)
12822 : APFloat::getLargest(Semantics);
12823 if (Opcode == ISD::FMAXNUM)
12824 NeutralAF.changeSign();
12825
12826 return ConstFP->isExactlyValue(NeutralAF);
12827 }
12828 }
12829 }
12830 return false;
12831}
12832
12834 while (V.getOpcode() == ISD::BITCAST)
12835 V = V.getOperand(0);
12836 return V;
12837}
12838
12840 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12841 V = V.getOperand(0);
12842 return V;
12843}
12844
12846 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12847 V = V.getOperand(0);
12848 return V;
12849}
12850
12852 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12853 SDValue InVec = V.getOperand(0);
12854 SDValue EltNo = V.getOperand(2);
12855 EVT VT = InVec.getValueType();
12856 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12857 if (IndexC && VT.isFixedLengthVector() &&
12858 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12859 !DemandedElts[IndexC->getZExtValue()]) {
12860 V = InVec;
12861 continue;
12862 }
12863 break;
12864 }
12865 return V;
12866}
12867
12869 while (V.getOpcode() == ISD::TRUNCATE)
12870 V = V.getOperand(0);
12871 return V;
12872}
12873
12874bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12875 if (V.getOpcode() != ISD::XOR)
12876 return false;
12877 V = peekThroughBitcasts(V.getOperand(1));
12878 unsigned NumBits = V.getScalarValueSizeInBits();
12879 ConstantSDNode *C =
12880 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12881 return C && (C->getAPIntValue().countr_one() >= NumBits);
12882}
12883
12885 bool AllowTruncation) {
12886 EVT VT = N.getValueType();
12887 APInt DemandedElts = VT.isFixedLengthVector()
12889 : APInt(1, 1);
12890 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12891}
12892
12894 bool AllowUndefs,
12895 bool AllowTruncation) {
12897 return CN;
12898
12899 // SplatVectors can truncate their operands. Ignore that case here unless
12900 // AllowTruncation is set.
12901 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12902 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12903 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12904 EVT CVT = CN->getValueType(0);
12905 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12906 if (AllowTruncation || CVT == VecEltVT)
12907 return CN;
12908 }
12909 }
12910
12912 BitVector UndefElements;
12913 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12914
12915 // BuildVectors can truncate their operands. Ignore that case here unless
12916 // AllowTruncation is set.
12917 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12918 if (CN && (UndefElements.none() || AllowUndefs)) {
12919 EVT CVT = CN->getValueType(0);
12920 EVT NSVT = N.getValueType().getScalarType();
12921 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12922 if (AllowTruncation || (CVT == NSVT))
12923 return CN;
12924 }
12925 }
12926
12927 return nullptr;
12928}
12929
12931 EVT VT = N.getValueType();
12932 APInt DemandedElts = VT.isFixedLengthVector()
12934 : APInt(1, 1);
12935 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12936}
12937
12939 const APInt &DemandedElts,
12940 bool AllowUndefs) {
12942 return CN;
12943
12945 BitVector UndefElements;
12946 ConstantFPSDNode *CN =
12947 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12948 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12949 if (CN && (UndefElements.none() || AllowUndefs))
12950 return CN;
12951 }
12952
12953 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12954 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12955 return CN;
12956
12957 return nullptr;
12958}
12959
12960bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12961 // TODO: may want to use peekThroughBitcast() here.
12962 ConstantSDNode *C =
12963 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12964 return C && C->isZero();
12965}
12966
12967bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12968 ConstantSDNode *C =
12969 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12970 return C && C->isOne();
12971}
12972
12973bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12975 unsigned BitWidth = N.getScalarValueSizeInBits();
12976 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12977 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12978}
12979
12980bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
12981 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12982 return C && APInt::isSameValue(C->getAPIntValue(),
12983 APInt(C->getAPIntValue().getBitWidth(), 1));
12984}
12985
12986bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
12988 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
12989 return C && C->isZero();
12990}
12991
12995
12996MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12997 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12998 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12999 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13000 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13001 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13002 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13003
13004 // We check here that the size of the memory operand fits within the size of
13005 // the MMO. This is because the MMO might indicate only a possible address
13006 // range instead of specifying the affected memory addresses precisely.
13007 assert(
13008 (!MMO->getType().isValid() ||
13009 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13010 "Size mismatch!");
13011}
13012
13013/// Profile - Gather unique data for the node.
13014///
13016 AddNodeIDNode(ID, this);
13017}
13018
13019namespace {
13020
13021 struct EVTArray {
13022 std::vector<EVT> VTs;
13023
13024 EVTArray() {
13025 VTs.reserve(MVT::VALUETYPE_SIZE);
13026 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13027 VTs.push_back(MVT((MVT::SimpleValueType)i));
13028 }
13029 };
13030
13031} // end anonymous namespace
13032
13033/// getValueTypeList - Return a pointer to the specified value type.
13034///
13035const EVT *SDNode::getValueTypeList(MVT VT) {
13036 static EVTArray SimpleVTArray;
13037
13038 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13039 return &SimpleVTArray.VTs[VT.SimpleTy];
13040}
13041
13042/// hasAnyUseOfValue - Return true if there are any use of the indicated
13043/// value. This method ignores uses of other values defined by this operation.
13044bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13045 assert(Value < getNumValues() && "Bad value!");
13046
13047 for (SDUse &U : uses())
13048 if (U.getResNo() == Value)
13049 return true;
13050
13051 return false;
13052}
13053
13054/// isOnlyUserOf - Return true if this node is the only use of N.
13055bool SDNode::isOnlyUserOf(const SDNode *N) const {
13056 bool Seen = false;
13057 for (const SDNode *User : N->users()) {
13058 if (User == this)
13059 Seen = true;
13060 else
13061 return false;
13062 }
13063
13064 return Seen;
13065}
13066
13067/// Return true if the only users of N are contained in Nodes.
13069 bool Seen = false;
13070 for (const SDNode *User : N->users()) {
13071 if (llvm::is_contained(Nodes, User))
13072 Seen = true;
13073 else
13074 return false;
13075 }
13076
13077 return Seen;
13078}
13079
13080/// Return true if the referenced return value is an operand of N.
13081bool SDValue::isOperandOf(const SDNode *N) const {
13082 return is_contained(N->op_values(), *this);
13083}
13084
13085bool SDNode::isOperandOf(const SDNode *N) const {
13086 return any_of(N->op_values(),
13087 [this](SDValue Op) { return this == Op.getNode(); });
13088}
13089
13090/// reachesChainWithoutSideEffects - Return true if this operand (which must
13091/// be a chain) reaches the specified operand without crossing any
13092/// side-effecting instructions on any chain path. In practice, this looks
13093/// through token factors and non-volatile loads. In order to remain efficient,
13094/// this only looks a couple of nodes in, it does not do an exhaustive search.
13095///
13096/// Note that we only need to examine chains when we're searching for
13097/// side-effects; SelectionDAG requires that all side-effects are represented
13098/// by chains, even if another operand would force a specific ordering. This
13099/// constraint is necessary to allow transformations like splitting loads.
13101 unsigned Depth) const {
13102 if (*this == Dest) return true;
13103
13104 // Don't search too deeply, we just want to be able to see through
13105 // TokenFactor's etc.
13106 if (Depth == 0) return false;
13107
13108 // If this is a token factor, all inputs to the TF happen in parallel.
13109 if (getOpcode() == ISD::TokenFactor) {
13110 // First, try a shallow search.
13111 if (is_contained((*this)->ops(), Dest)) {
13112 // We found the chain we want as an operand of this TokenFactor.
13113 // Essentially, we reach the chain without side-effects if we could
13114 // serialize the TokenFactor into a simple chain of operations with
13115 // Dest as the last operation. This is automatically true if the
13116 // chain has one use: there are no other ordering constraints.
13117 // If the chain has more than one use, we give up: some other
13118 // use of Dest might force a side-effect between Dest and the current
13119 // node.
13120 if (Dest.hasOneUse())
13121 return true;
13122 }
13123 // Next, try a deep search: check whether every operand of the TokenFactor
13124 // reaches Dest.
13125 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13126 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13127 });
13128 }
13129
13130 // Loads don't have side effects, look through them.
13131 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13132 if (Ld->isUnordered())
13133 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13134 }
13135 return false;
13136}
13137
13138bool SDNode::hasPredecessor(const SDNode *N) const {
13141 Worklist.push_back(this);
13142 return hasPredecessorHelper(N, Visited, Worklist);
13143}
13144
13146 this->Flags &= Flags;
13147}
13148
13149SDValue
13151 ArrayRef<ISD::NodeType> CandidateBinOps,
13152 bool AllowPartials) {
13153 // The pattern must end in an extract from index 0.
13154 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13155 !isNullConstant(Extract->getOperand(1)))
13156 return SDValue();
13157
13158 // Match against one of the candidate binary ops.
13159 SDValue Op = Extract->getOperand(0);
13160 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13161 return Op.getOpcode() == unsigned(BinOp);
13162 }))
13163 return SDValue();
13164
13165 // Floating-point reductions may require relaxed constraints on the final step
13166 // of the reduction because they may reorder intermediate operations.
13167 unsigned CandidateBinOp = Op.getOpcode();
13168 if (Op.getValueType().isFloatingPoint()) {
13169 SDNodeFlags Flags = Op->getFlags();
13170 switch (CandidateBinOp) {
13171 case ISD::FADD:
13172 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13173 return SDValue();
13174 break;
13175 default:
13176 llvm_unreachable("Unhandled FP opcode for binop reduction");
13177 }
13178 }
13179
13180 // Matching failed - attempt to see if we did enough stages that a partial
13181 // reduction from a subvector is possible.
13182 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13183 if (!AllowPartials || !Op)
13184 return SDValue();
13185 EVT OpVT = Op.getValueType();
13186 EVT OpSVT = OpVT.getScalarType();
13187 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13188 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13189 return SDValue();
13190 BinOp = (ISD::NodeType)CandidateBinOp;
13191 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13192 };
13193
13194 // At each stage, we're looking for something that looks like:
13195 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13196 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13197 // i32 undef, i32 undef, i32 undef, i32 undef>
13198 // %a = binop <8 x i32> %op, %s
13199 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13200 // we expect something like:
13201 // <4,5,6,7,u,u,u,u>
13202 // <2,3,u,u,u,u,u,u>
13203 // <1,u,u,u,u,u,u,u>
13204 // While a partial reduction match would be:
13205 // <2,3,u,u,u,u,u,u>
13206 // <1,u,u,u,u,u,u,u>
13207 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13208 SDValue PrevOp;
13209 for (unsigned i = 0; i < Stages; ++i) {
13210 unsigned MaskEnd = (1 << i);
13211
13212 if (Op.getOpcode() != CandidateBinOp)
13213 return PartialReduction(PrevOp, MaskEnd);
13214
13215 SDValue Op0 = Op.getOperand(0);
13216 SDValue Op1 = Op.getOperand(1);
13217
13219 if (Shuffle) {
13220 Op = Op1;
13221 } else {
13222 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13223 Op = Op0;
13224 }
13225
13226 // The first operand of the shuffle should be the same as the other operand
13227 // of the binop.
13228 if (!Shuffle || Shuffle->getOperand(0) != Op)
13229 return PartialReduction(PrevOp, MaskEnd);
13230
13231 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13232 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13233 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13234 return PartialReduction(PrevOp, MaskEnd);
13235
13236 PrevOp = Op;
13237 }
13238
13239 // Handle subvector reductions, which tend to appear after the shuffle
13240 // reduction stages.
13241 while (Op.getOpcode() == CandidateBinOp) {
13242 unsigned NumElts = Op.getValueType().getVectorNumElements();
13243 SDValue Op0 = Op.getOperand(0);
13244 SDValue Op1 = Op.getOperand(1);
13245 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13247 Op0.getOperand(0) != Op1.getOperand(0))
13248 break;
13249 SDValue Src = Op0.getOperand(0);
13250 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13251 if (NumSrcElts != (2 * NumElts))
13252 break;
13253 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13254 Op1.getConstantOperandAPInt(1) == NumElts) &&
13255 !(Op1.getConstantOperandAPInt(1) == 0 &&
13256 Op0.getConstantOperandAPInt(1) == NumElts))
13257 break;
13258 Op = Src;
13259 }
13260
13261 BinOp = (ISD::NodeType)CandidateBinOp;
13262 return Op;
13263}
13264
13266 EVT VT = N->getValueType(0);
13267 EVT EltVT = VT.getVectorElementType();
13268 unsigned NE = VT.getVectorNumElements();
13269
13270 SDLoc dl(N);
13271
13272 // If ResNE is 0, fully unroll the vector op.
13273 if (ResNE == 0)
13274 ResNE = NE;
13275 else if (NE > ResNE)
13276 NE = ResNE;
13277
13278 if (N->getNumValues() == 2) {
13279 SmallVector<SDValue, 8> Scalars0, Scalars1;
13280 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13281 EVT VT1 = N->getValueType(1);
13282 EVT EltVT1 = VT1.getVectorElementType();
13283
13284 unsigned i;
13285 for (i = 0; i != NE; ++i) {
13286 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13287 SDValue Operand = N->getOperand(j);
13288 EVT OperandVT = Operand.getValueType();
13289
13290 // A vector operand; extract a single element.
13291 EVT OperandEltVT = OperandVT.getVectorElementType();
13292 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13293 }
13294
13295 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13296 Scalars0.push_back(EltOp);
13297 Scalars1.push_back(EltOp.getValue(1));
13298 }
13299
13300 for (; i < ResNE; ++i) {
13301 Scalars0.push_back(getUNDEF(EltVT));
13302 Scalars1.push_back(getUNDEF(EltVT1));
13303 }
13304
13305 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13306 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13307 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13308 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13309 return getMergeValues({Vec0, Vec1}, dl);
13310 }
13311
13312 assert(N->getNumValues() == 1 &&
13313 "Can't unroll a vector with multiple results!");
13314
13316 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13317
13318 unsigned i;
13319 for (i= 0; i != NE; ++i) {
13320 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13321 SDValue Operand = N->getOperand(j);
13322 EVT OperandVT = Operand.getValueType();
13323 if (OperandVT.isVector()) {
13324 // A vector operand; extract a single element.
13325 EVT OperandEltVT = OperandVT.getVectorElementType();
13326 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13327 } else {
13328 // A scalar operand; just use it as is.
13329 Operands[j] = Operand;
13330 }
13331 }
13332
13333 switch (N->getOpcode()) {
13334 default: {
13335 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13336 N->getFlags()));
13337 break;
13338 }
13339 case ISD::VSELECT:
13340 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13341 break;
13342 case ISD::SHL:
13343 case ISD::SRA:
13344 case ISD::SRL:
13345 case ISD::ROTL:
13346 case ISD::ROTR:
13347 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13349 Operands[1])));
13350 break;
13352 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13353 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13354 Operands[0],
13355 getValueType(ExtVT)));
13356 break;
13357 }
13358 case ISD::ADDRSPACECAST: {
13359 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13360 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13361 ASC->getSrcAddressSpace(),
13362 ASC->getDestAddressSpace()));
13363 break;
13364 }
13365 }
13366 }
13367
13368 for (; i < ResNE; ++i)
13369 Scalars.push_back(getUNDEF(EltVT));
13370
13371 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13372 return getBuildVector(VecVT, dl, Scalars);
13373}
13374
13375std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13376 SDNode *N, unsigned ResNE) {
13377 unsigned Opcode = N->getOpcode();
13378 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13379 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13380 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13381 "Expected an overflow opcode");
13382
13383 EVT ResVT = N->getValueType(0);
13384 EVT OvVT = N->getValueType(1);
13385 EVT ResEltVT = ResVT.getVectorElementType();
13386 EVT OvEltVT = OvVT.getVectorElementType();
13387 SDLoc dl(N);
13388
13389 // If ResNE is 0, fully unroll the vector op.
13390 unsigned NE = ResVT.getVectorNumElements();
13391 if (ResNE == 0)
13392 ResNE = NE;
13393 else if (NE > ResNE)
13394 NE = ResNE;
13395
13396 SmallVector<SDValue, 8> LHSScalars;
13397 SmallVector<SDValue, 8> RHSScalars;
13398 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13399 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13400
13401 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13402 SDVTList VTs = getVTList(ResEltVT, SVT);
13403 SmallVector<SDValue, 8> ResScalars;
13404 SmallVector<SDValue, 8> OvScalars;
13405 for (unsigned i = 0; i < NE; ++i) {
13406 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13407 SDValue Ov =
13408 getSelect(dl, OvEltVT, Res.getValue(1),
13409 getBoolConstant(true, dl, OvEltVT, ResVT),
13410 getConstant(0, dl, OvEltVT));
13411
13412 ResScalars.push_back(Res);
13413 OvScalars.push_back(Ov);
13414 }
13415
13416 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13417 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13418
13419 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13420 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13421 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13422 getBuildVector(NewOvVT, dl, OvScalars));
13423}
13424
13427 unsigned Bytes,
13428 int Dist) const {
13429 if (LD->isVolatile() || Base->isVolatile())
13430 return false;
13431 // TODO: probably too restrictive for atomics, revisit
13432 if (!LD->isSimple())
13433 return false;
13434 if (LD->isIndexed() || Base->isIndexed())
13435 return false;
13436 if (LD->getChain() != Base->getChain())
13437 return false;
13438 EVT VT = LD->getMemoryVT();
13439 if (VT.getSizeInBits() / 8 != Bytes)
13440 return false;
13441
13442 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13443 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13444
13445 int64_t Offset = 0;
13446 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13447 return (Dist * (int64_t)Bytes == Offset);
13448 return false;
13449}
13450
13451/// InferPtrAlignment - Infer alignment of a load / store address. Return
13452/// std::nullopt if it cannot be inferred.
13454 // If this is a GlobalAddress + cst, return the alignment.
13455 const GlobalValue *GV = nullptr;
13456 int64_t GVOffset = 0;
13457 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13458 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13459 KnownBits Known(PtrWidth);
13461 unsigned AlignBits = Known.countMinTrailingZeros();
13462 if (AlignBits)
13463 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13464 }
13465
13466 // If this is a direct reference to a stack slot, use information about the
13467 // stack slot's alignment.
13468 int FrameIdx = INT_MIN;
13469 int64_t FrameOffset = 0;
13471 FrameIdx = FI->getIndex();
13472 } else if (isBaseWithConstantOffset(Ptr) &&
13473 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
13474 // Handle FI+Cst
13475 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13476 FrameOffset = Ptr.getConstantOperandVal(1);
13477 }
13478
13479 if (FrameIdx != INT_MIN) {
13481 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13482 }
13483
13484 return std::nullopt;
13485}
13486
13487/// Split the scalar node with EXTRACT_ELEMENT using the provided
13488/// VTs and return the low/high part.
13489std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13490 const SDLoc &DL,
13491 const EVT &LoVT,
13492 const EVT &HiVT) {
13493 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13494 "Split node must be a scalar type");
13495 SDValue Lo =
13497 SDValue Hi =
13499 return std::make_pair(Lo, Hi);
13500}
13501
13502/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13503/// which is split (or expanded) into two not necessarily identical pieces.
13504std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13505 // Currently all types are split in half.
13506 EVT LoVT, HiVT;
13507 if (!VT.isVector())
13508 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13509 else
13510 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13511
13512 return std::make_pair(LoVT, HiVT);
13513}
13514
13515/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13516/// type, dependent on an enveloping VT that has been split into two identical
13517/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13518std::pair<EVT, EVT>
13520 bool *HiIsEmpty) const {
13521 EVT EltTp = VT.getVectorElementType();
13522 // Examples:
13523 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13524 // custom VL=9 with enveloping VL=8/8 yields 8/1
13525 // custom VL=10 with enveloping VL=8/8 yields 8/2
13526 // etc.
13527 ElementCount VTNumElts = VT.getVectorElementCount();
13528 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13529 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13530 "Mixing fixed width and scalable vectors when enveloping a type");
13531 EVT LoVT, HiVT;
13532 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13533 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13534 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13535 *HiIsEmpty = false;
13536 } else {
13537 // Flag that hi type has zero storage size, but return split envelop type
13538 // (this would be easier if vector types with zero elements were allowed).
13539 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13540 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13541 *HiIsEmpty = true;
13542 }
13543 return std::make_pair(LoVT, HiVT);
13544}
13545
13546/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13547/// low/high part.
13548std::pair<SDValue, SDValue>
13549SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13550 const EVT &HiVT) {
13551 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13552 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13553 "Splitting vector with an invalid mixture of fixed and scalable "
13554 "vector types");
13556 N.getValueType().getVectorMinNumElements() &&
13557 "More vector elements requested than available!");
13558 SDValue Lo, Hi;
13559 Lo = getExtractSubvector(DL, LoVT, N, 0);
13560 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13561 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13562 // IDX with the runtime scaling factor of the result vector type. For
13563 // fixed-width result vectors, that runtime scaling factor is 1.
13566 return std::make_pair(Lo, Hi);
13567}
13568
13569std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13570 const SDLoc &DL) {
13571 // Split the vector length parameter.
13572 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13573 EVT VT = N.getValueType();
13575 "Expecting the mask to be an evenly-sized vector");
13576 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
13577 SDValue HalfNumElts =
13578 VecVT.isFixedLengthVector()
13579 ? getConstant(HalfMinNumElts, DL, VT)
13580 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
13581 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13582 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13583 return std::make_pair(Lo, Hi);
13584}
13585
13586/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13588 EVT VT = N.getValueType();
13591 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13592}
13593
13596 unsigned Start, unsigned Count,
13597 EVT EltVT) {
13598 EVT VT = Op.getValueType();
13599 if (Count == 0)
13601 if (EltVT == EVT())
13602 EltVT = VT.getVectorElementType();
13603 SDLoc SL(Op);
13604 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13605 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13606 }
13607}
13608
13609// getAddressSpace - Return the address space this GlobalAddress belongs to.
13611 return getGlobal()->getType()->getAddressSpace();
13612}
13613
13616 return Val.MachineCPVal->getType();
13617 return Val.ConstVal->getType();
13618}
13619
13620bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13621 unsigned &SplatBitSize,
13622 bool &HasAnyUndefs,
13623 unsigned MinSplatBits,
13624 bool IsBigEndian) const {
13625 EVT VT = getValueType(0);
13626 assert(VT.isVector() && "Expected a vector type");
13627 unsigned VecWidth = VT.getSizeInBits();
13628 if (MinSplatBits > VecWidth)
13629 return false;
13630
13631 // FIXME: The widths are based on this node's type, but build vectors can
13632 // truncate their operands.
13633 SplatValue = APInt(VecWidth, 0);
13634 SplatUndef = APInt(VecWidth, 0);
13635
13636 // Get the bits. Bits with undefined values (when the corresponding element
13637 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13638 // in SplatValue. If any of the values are not constant, give up and return
13639 // false.
13640 unsigned int NumOps = getNumOperands();
13641 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13642 unsigned EltWidth = VT.getScalarSizeInBits();
13643
13644 for (unsigned j = 0; j < NumOps; ++j) {
13645 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13646 SDValue OpVal = getOperand(i);
13647 unsigned BitPos = j * EltWidth;
13648
13649 if (OpVal.isUndef())
13650 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13651 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13652 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13653 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13654 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13655 else
13656 return false;
13657 }
13658
13659 // The build_vector is all constants or undefs. Find the smallest element
13660 // size that splats the vector.
13661 HasAnyUndefs = (SplatUndef != 0);
13662
13663 // FIXME: This does not work for vectors with elements less than 8 bits.
13664 while (VecWidth > 8) {
13665 // If we can't split in half, stop here.
13666 if (VecWidth & 1)
13667 break;
13668
13669 unsigned HalfSize = VecWidth / 2;
13670 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13671 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13672 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13673 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13674
13675 // If the two halves do not match (ignoring undef bits), stop here.
13676 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13677 MinSplatBits > HalfSize)
13678 break;
13679
13680 SplatValue = HighValue | LowValue;
13681 SplatUndef = HighUndef & LowUndef;
13682
13683 VecWidth = HalfSize;
13684 }
13685
13686 // FIXME: The loop above only tries to split in halves. But if the input
13687 // vector for example is <3 x i16> it wouldn't be able to detect a
13688 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13689 // optimizations. I guess that back in the days when this helper was created
13690 // vectors normally was power-of-2 sized.
13691
13692 SplatBitSize = VecWidth;
13693 return true;
13694}
13695
13697 BitVector *UndefElements) const {
13698 unsigned NumOps = getNumOperands();
13699 if (UndefElements) {
13700 UndefElements->clear();
13701 UndefElements->resize(NumOps);
13702 }
13703 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13704 if (!DemandedElts)
13705 return SDValue();
13706 SDValue Splatted;
13707 for (unsigned i = 0; i != NumOps; ++i) {
13708 if (!DemandedElts[i])
13709 continue;
13710 SDValue Op = getOperand(i);
13711 if (Op.isUndef()) {
13712 if (UndefElements)
13713 (*UndefElements)[i] = true;
13714 } else if (!Splatted) {
13715 Splatted = Op;
13716 } else if (Splatted != Op) {
13717 return SDValue();
13718 }
13719 }
13720
13721 if (!Splatted) {
13722 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13723 assert(getOperand(FirstDemandedIdx).isUndef() &&
13724 "Can only have a splat without a constant for all undefs.");
13725 return getOperand(FirstDemandedIdx);
13726 }
13727
13728 return Splatted;
13729}
13730
13732 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13733 return getSplatValue(DemandedElts, UndefElements);
13734}
13735
13737 SmallVectorImpl<SDValue> &Sequence,
13738 BitVector *UndefElements) const {
13739 unsigned NumOps = getNumOperands();
13740 Sequence.clear();
13741 if (UndefElements) {
13742 UndefElements->clear();
13743 UndefElements->resize(NumOps);
13744 }
13745 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13746 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13747 return false;
13748
13749 // Set the undefs even if we don't find a sequence (like getSplatValue).
13750 if (UndefElements)
13751 for (unsigned I = 0; I != NumOps; ++I)
13752 if (DemandedElts[I] && getOperand(I).isUndef())
13753 (*UndefElements)[I] = true;
13754
13755 // Iteratively widen the sequence length looking for repetitions.
13756 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13757 Sequence.append(SeqLen, SDValue());
13758 for (unsigned I = 0; I != NumOps; ++I) {
13759 if (!DemandedElts[I])
13760 continue;
13761 SDValue &SeqOp = Sequence[I % SeqLen];
13763 if (Op.isUndef()) {
13764 if (!SeqOp)
13765 SeqOp = Op;
13766 continue;
13767 }
13768 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13769 Sequence.clear();
13770 break;
13771 }
13772 SeqOp = Op;
13773 }
13774 if (!Sequence.empty())
13775 return true;
13776 }
13777
13778 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13779 return false;
13780}
13781
13783 BitVector *UndefElements) const {
13784 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13785 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13786}
13787
13790 BitVector *UndefElements) const {
13792 getSplatValue(DemandedElts, UndefElements));
13793}
13794
13797 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13798}
13799
13802 BitVector *UndefElements) const {
13804 getSplatValue(DemandedElts, UndefElements));
13805}
13806
13811
13812int32_t
13814 uint32_t BitWidth) const {
13815 if (ConstantFPSDNode *CN =
13817 bool IsExact;
13818 APSInt IntVal(BitWidth);
13819 const APFloat &APF = CN->getValueAPF();
13820 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13821 APFloat::opOK ||
13822 !IsExact)
13823 return -1;
13824
13825 return IntVal.exactLogBase2();
13826 }
13827 return -1;
13828}
13829
13831 bool IsLittleEndian, unsigned DstEltSizeInBits,
13832 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13833 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13834 if (!isConstant())
13835 return false;
13836
13837 unsigned NumSrcOps = getNumOperands();
13838 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13839 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13840 "Invalid bitcast scale");
13841
13842 // Extract raw src bits.
13843 SmallVector<APInt> SrcBitElements(NumSrcOps,
13844 APInt::getZero(SrcEltSizeInBits));
13845 BitVector SrcUndeElements(NumSrcOps, false);
13846
13847 for (unsigned I = 0; I != NumSrcOps; ++I) {
13849 if (Op.isUndef()) {
13850 SrcUndeElements.set(I);
13851 continue;
13852 }
13853 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13854 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13855 assert((CInt || CFP) && "Unknown constant");
13856 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13857 : CFP->getValueAPF().bitcastToAPInt();
13858 }
13859
13860 // Recast to dst width.
13861 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13862 SrcBitElements, UndefElements, SrcUndeElements);
13863 return true;
13864}
13865
13866void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13867 unsigned DstEltSizeInBits,
13868 SmallVectorImpl<APInt> &DstBitElements,
13869 ArrayRef<APInt> SrcBitElements,
13870 BitVector &DstUndefElements,
13871 const BitVector &SrcUndefElements) {
13872 unsigned NumSrcOps = SrcBitElements.size();
13873 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13874 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13875 "Invalid bitcast scale");
13876 assert(NumSrcOps == SrcUndefElements.size() &&
13877 "Vector size mismatch");
13878
13879 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13880 DstUndefElements.clear();
13881 DstUndefElements.resize(NumDstOps, false);
13882 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13883
13884 // Concatenate src elements constant bits together into dst element.
13885 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13886 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13887 for (unsigned I = 0; I != NumDstOps; ++I) {
13888 DstUndefElements.set(I);
13889 APInt &DstBits = DstBitElements[I];
13890 for (unsigned J = 0; J != Scale; ++J) {
13891 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13892 if (SrcUndefElements[Idx])
13893 continue;
13894 DstUndefElements.reset(I);
13895 const APInt &SrcBits = SrcBitElements[Idx];
13896 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13897 "Illegal constant bitwidths");
13898 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13899 }
13900 }
13901 return;
13902 }
13903
13904 // Split src element constant bits into dst elements.
13905 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13906 for (unsigned I = 0; I != NumSrcOps; ++I) {
13907 if (SrcUndefElements[I]) {
13908 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13909 continue;
13910 }
13911 const APInt &SrcBits = SrcBitElements[I];
13912 for (unsigned J = 0; J != Scale; ++J) {
13913 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13914 APInt &DstBits = DstBitElements[Idx];
13915 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13916 }
13917 }
13918}
13919
13921 for (const SDValue &Op : op_values()) {
13922 unsigned Opc = Op.getOpcode();
13923 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13924 return false;
13925 }
13926 return true;
13927}
13928
13929std::optional<std::pair<APInt, APInt>>
13931 unsigned NumOps = getNumOperands();
13932 if (NumOps < 2)
13933 return std::nullopt;
13934
13937 return std::nullopt;
13938
13939 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13940 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13941 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13942
13943 if (Stride.isZero())
13944 return std::nullopt;
13945
13946 for (unsigned i = 2; i < NumOps; ++i) {
13948 return std::nullopt;
13949
13950 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13951 if (Val != (Start + (Stride * i)))
13952 return std::nullopt;
13953 }
13954
13955 return std::make_pair(Start, Stride);
13956}
13957
13959 // Find the first non-undef value in the shuffle mask.
13960 unsigned i, e;
13961 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
13962 /* search */;
13963
13964 // If all elements are undefined, this shuffle can be considered a splat
13965 // (although it should eventually get simplified away completely).
13966 if (i == e)
13967 return true;
13968
13969 // Make sure all remaining elements are either undef or the same as the first
13970 // non-undef value.
13971 for (int Idx = Mask[i]; i != e; ++i)
13972 if (Mask[i] >= 0 && Mask[i] != Idx)
13973 return false;
13974 return true;
13975}
13976
13977// Returns true if it is a constant integer BuildVector or constant integer,
13978// possibly hidden by a bitcast.
13980 SDValue N, bool AllowOpaques) const {
13982
13983 if (auto *C = dyn_cast<ConstantSDNode>(N))
13984 return AllowOpaques || !C->isOpaque();
13985
13987 return true;
13988
13989 // Treat a GlobalAddress supporting constant offset folding as a
13990 // constant integer.
13991 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
13992 if (GA->getOpcode() == ISD::GlobalAddress &&
13993 TLI->isOffsetFoldingLegal(GA))
13994 return true;
13995
13996 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13997 isa<ConstantSDNode>(N.getOperand(0)))
13998 return true;
13999 return false;
14000}
14001
14002// Returns true if it is a constant float BuildVector or constant float.
14005 return true;
14006
14008 return true;
14009
14010 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14011 isa<ConstantFPSDNode>(N.getOperand(0)))
14012 return true;
14013
14014 return false;
14015}
14016
14017std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14018 ConstantSDNode *Const =
14019 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14020 if (!Const)
14021 return std::nullopt;
14022
14023 EVT VT = N->getValueType(0);
14024 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14025 switch (TLI->getBooleanContents(N.getValueType())) {
14027 if (CVal.isOne())
14028 return true;
14029 if (CVal.isZero())
14030 return false;
14031 return std::nullopt;
14033 if (CVal.isAllOnes())
14034 return true;
14035 if (CVal.isZero())
14036 return false;
14037 return std::nullopt;
14039 return CVal[0];
14040 }
14041 llvm_unreachable("Unknown BooleanContent enum");
14042}
14043
14044void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14045 assert(!Node->OperandList && "Node already has operands");
14047 "too many operands to fit into SDNode");
14048 SDUse *Ops = OperandRecycler.allocate(
14049 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14050
14051 bool IsDivergent = false;
14052 for (unsigned I = 0; I != Vals.size(); ++I) {
14053 Ops[I].setUser(Node);
14054 Ops[I].setInitial(Vals[I]);
14055 EVT VT = Ops[I].getValueType();
14056
14057 // Skip Chain. It does not carry divergence.
14058 if (VT != MVT::Other &&
14059 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14060 Ops[I].getNode()->isDivergent()) {
14061 IsDivergent = true;
14062 }
14063 }
14064 Node->NumOperands = Vals.size();
14065 Node->OperandList = Ops;
14066 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14067 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14068 Node->SDNodeBits.IsDivergent = IsDivergent;
14069 }
14070 checkForCycles(Node);
14071}
14072
14075 size_t Limit = SDNode::getMaxNumOperands();
14076 while (Vals.size() > Limit) {
14077 unsigned SliceIdx = Vals.size() - Limit;
14078 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14079 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14080 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14081 Vals.emplace_back(NewTF);
14082 }
14083 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14084}
14085
14087 EVT VT, SDNodeFlags Flags) {
14088 switch (Opcode) {
14089 default:
14090 return SDValue();
14091 case ISD::ADD:
14092 case ISD::OR:
14093 case ISD::XOR:
14094 case ISD::UMAX:
14095 return getConstant(0, DL, VT);
14096 case ISD::MUL:
14097 return getConstant(1, DL, VT);
14098 case ISD::AND:
14099 case ISD::UMIN:
14100 return getAllOnesConstant(DL, VT);
14101 case ISD::SMAX:
14103 case ISD::SMIN:
14105 case ISD::FADD:
14106 // If flags allow, prefer positive zero since it's generally cheaper
14107 // to materialize on most targets.
14108 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14109 case ISD::FMUL:
14110 return getConstantFP(1.0, DL, VT);
14111 case ISD::FMINNUM:
14112 case ISD::FMAXNUM: {
14113 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14114 const fltSemantics &Semantics = VT.getFltSemantics();
14115 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14116 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14117 APFloat::getLargest(Semantics);
14118 if (Opcode == ISD::FMAXNUM)
14119 NeutralAF.changeSign();
14120
14121 return getConstantFP(NeutralAF, DL, VT);
14122 }
14123 case ISD::FMINIMUM:
14124 case ISD::FMAXIMUM: {
14125 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14126 const fltSemantics &Semantics = VT.getFltSemantics();
14127 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14128 : APFloat::getLargest(Semantics);
14129 if (Opcode == ISD::FMAXIMUM)
14130 NeutralAF.changeSign();
14131
14132 return getConstantFP(NeutralAF, DL, VT);
14133 }
14134
14135 }
14136}
14137
14138/// Helper used to make a call to a library function that has one argument of
14139/// pointer type.
14140///
14141/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14142/// used to get or set floating-point state. They have one argument of pointer
14143/// type, which points to the memory region containing bits of the
14144/// floating-point state. The value returned by such function is ignored in the
14145/// created call.
14146///
14147/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14148/// \param Ptr Pointer used to save/load state.
14149/// \param InChain Ingoing token chain.
14150/// \returns Outgoing chain token.
14152 SDValue InChain,
14153 const SDLoc &DLoc) {
14154 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14156 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14157 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
14158 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
14159 TLI->getPointerTy(getDataLayout()));
14161 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14162 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
14163 std::move(Args));
14164 return TLI->LowerCallTo(CLI).second;
14165}
14166
14168 assert(From && To && "Invalid SDNode; empty source SDValue?");
14169 auto I = SDEI.find(From);
14170 if (I == SDEI.end())
14171 return;
14172
14173 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14174 // the iterator, hence the need to make a copy to prevent a use-after-free.
14175 NodeExtraInfo NEI = I->second;
14176 if (LLVM_LIKELY(!NEI.PCSections)) {
14177 // No deep copy required for the types of extra info set.
14178 //
14179 // FIXME: Investigate if other types of extra info also need deep copy. This
14180 // depends on the types of nodes they can be attached to: if some extra info
14181 // is only ever attached to nodes where a replacement To node is always the
14182 // node where later use and propagation of the extra info has the intended
14183 // semantics, no deep copy is required.
14184 SDEI[To] = std::move(NEI);
14185 return;
14186 }
14187
14188 const SDNode *EntrySDN = getEntryNode().getNode();
14189
14190 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14191 // through the replacement of From with To. Otherwise, replacements of a node
14192 // (From) with more complex nodes (To and its operands) may result in lost
14193 // extra info where the root node (To) is insignificant in further propagating
14194 // and using extra info when further lowering to MIR.
14195 //
14196 // In the first step pre-populate the visited set with the nodes reachable
14197 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14198 // DAG that is not new and should be left untouched.
14199 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14200 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14201 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14202 if (MaxDepth == 0) {
14203 // Remember this node in case we need to increase MaxDepth and continue
14204 // populating FromReach from this node.
14205 Leafs.emplace_back(N);
14206 return;
14207 }
14208 if (!FromReach.insert(N).second)
14209 return;
14210 for (const SDValue &Op : N->op_values())
14211 Self(Self, Op.getNode(), MaxDepth - 1);
14212 };
14213
14214 // Copy extra info to To and all its transitive operands (that are new).
14216 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14217 if (FromReach.contains(N))
14218 return true;
14219 if (!Visited.insert(N).second)
14220 return true;
14221 if (EntrySDN == N)
14222 return false;
14223 for (const SDValue &Op : N->op_values()) {
14224 if (N == To && Op.getNode() == EntrySDN) {
14225 // Special case: New node's operand is the entry node; just need to
14226 // copy extra info to new node.
14227 break;
14228 }
14229 if (!Self(Self, Op.getNode()))
14230 return false;
14231 }
14232 // Copy only if entry node was not reached.
14233 SDEI[N] = NEI;
14234 return true;
14235 };
14236
14237 // We first try with a lower MaxDepth, assuming that the path to common
14238 // operands between From and To is relatively short. This significantly
14239 // improves performance in the common case. The initial MaxDepth is big
14240 // enough to avoid retry in the common case; the last MaxDepth is large
14241 // enough to avoid having to use the fallback below (and protects from
14242 // potential stack exhaustion from recursion).
14243 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14244 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14245 // StartFrom is the previous (or initial) set of leafs reachable at the
14246 // previous maximum depth.
14248 std::swap(StartFrom, Leafs);
14249 for (const SDNode *N : StartFrom)
14250 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14251 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14252 return;
14253 // This should happen very rarely (reached the entry node).
14254 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14255 assert(!Leafs.empty());
14256 }
14257
14258 // This should not happen - but if it did, that means the subgraph reachable
14259 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14260 // could not visit all reachable common operands. Consequently, we were able
14261 // to reach the entry node.
14262 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14263 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14264 // Best-effort fallback if assertions disabled.
14265 SDEI[To] = std::move(NEI);
14266}
14267
14268#ifndef NDEBUG
14269static void checkForCyclesHelper(const SDNode *N,
14272 const llvm::SelectionDAG *DAG) {
14273 // If this node has already been checked, don't check it again.
14274 if (Checked.count(N))
14275 return;
14276
14277 // If a node has already been visited on this depth-first walk, reject it as
14278 // a cycle.
14279 if (!Visited.insert(N).second) {
14280 errs() << "Detected cycle in SelectionDAG\n";
14281 dbgs() << "Offending node:\n";
14282 N->dumprFull(DAG); dbgs() << "\n";
14283 abort();
14284 }
14285
14286 for (const SDValue &Op : N->op_values())
14287 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14288
14289 Checked.insert(N);
14290 Visited.erase(N);
14291}
14292#endif
14293
14295 const llvm::SelectionDAG *DAG,
14296 bool force) {
14297#ifndef NDEBUG
14298 bool check = force;
14299#ifdef EXPENSIVE_CHECKS
14300 check = true;
14301#endif // EXPENSIVE_CHECKS
14302 if (check) {
14303 assert(N && "Checking nonexistent SDNode");
14306 checkForCyclesHelper(N, visited, checked, DAG);
14307 }
14308#endif // !NDEBUG
14309}
14310
14311void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14312 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14313}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isConstant(const MachineInstr &MI)
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...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
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
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition Compiler.h:569
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
This file defines a hash set that can be used to remove duplication of nodes in a graph.
#define _
iv users
Definition IVUsers.cpp:48
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static LVOptions Options
Definition LVOptions.cpp:25
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool shouldLowerMemFuncForSize(const MachineFunction &MF)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define G(x, y, z)
Definition MD5.cpp:56
mir Rename Register Operands
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
#define T
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
Contains matchers for matching SelectionDAG nodes and values.
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 uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, BatchAAResults *BatchAA)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static bool canFoldStoreIntoLibCallOutputPointers(StoreSDNode *StoreNode, SDNode *FPNode)
Given a store node StoreNode, return true if it is safe to fold that node into FPNode,...
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static bool isInTailCallPositionWrapper(const CallInst *CI, const SelectionDAG *SelDAG, bool AllowReturnsFirstArg)
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
static cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#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")
This file describes how to lower LLVM code to machine code.
static void removeOperands(MachineInstr &MI, unsigned i)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1120
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1208
void copySign(const APFloat &RHS)
Definition APFloat.h:1302
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6057
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1190
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition APFloat.h:1432
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1181
bool isFinite() const
Definition APFloat.h:1454
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1347
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1199
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1235
bool isZero() const
Definition APFloat.h:1445
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1138
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1332
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1098
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1226
bool isPosZero() const
Definition APFloat.h:1460
bool isNegZero() const
Definition APFloat.h:1461
void changeSign()
Definition APFloat.h:1297
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1109
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1971
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2055
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1406
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1012
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:229
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1540
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1391
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1670
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1385
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1033
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1512
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1330
APInt abs() const
Get the absolute value.
Definition APInt.h:1795
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2026
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1182
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:258
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:380
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:209
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:329
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1396
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1154
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:834
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1166
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1639
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1628
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1598
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:219
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2086
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2100
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1141
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1435
unsigned logBase2() const
Definition APInt.h:1761
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2036
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:827
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1736
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1150
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:985
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1367
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:873
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1257
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:440
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:553
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:306
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1417
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1388
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1237
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:389
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:239
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1221
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2045
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:392
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:341
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:335
BitVector & set()
Definition BitVector.h:351
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:188
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:159
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:899
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
const APFloat & getValue() const
Definition Constants.h:321
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:157
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
LLVM_ABI Type * getType() const
This class represents a range of values.
LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
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 const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
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...
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:198
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
A debug info location.
Definition DebugLoc.h:124
Implements a dense probed hash-table based set.
Definition DenseSet.h:269
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:330
Data structure describing the variable locations in a function.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
LLVM_ABI unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
This class is used to form a handle around another node that is persistent and is updated across invo...
const SDValue & getValue() const
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
This SDNode is used for LIFETIME_START/LIFETIME_END values.
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1077
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1445
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
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.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
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.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:230
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:303
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
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.
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition Register.h:19
Keeps track of dbg_value information through SDISel.
LLVM_ABI void add(SDDbgValue *V, bool isParameter)
LLVM_ABI void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
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...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
LLVM_ABI void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
LLVM_ABI bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
iterator_range< use_iterator > uses()
MemSDNodeBitfields MemSDNodeBits
LLVM_ABI void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
LLVM_ABI void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static LLVM_ABI bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
std::optional< APInt > bitcastToAPInt() const
LLVM_ABI bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
LLVM_ABI bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Returns true if the node type is UNDEF or POISON.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
Create an SDNode.
Represents a use of a SDNode.
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if the referenced return value is an operand of N.
SDValue()=default
LLVM_ABI bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
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
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
LLVM_ABI SDValue getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
LLVM_ABI SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
LLVM_ABI unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
LLVM_ABI std::pair< SDValue, SDValue > getStrlen(SDValue Chain, const SDLoc &dl, SDValue Src, const CallInst *CI)
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
LLVM_ABI std::optional< bool > isBoolConstant(SDValue N) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
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 void updateDivergence(SDNode *N)
LLVM_ABI SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
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 getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
LLVM_ABI SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm, bool ConstantFold=true)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
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 getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC, bool ConstantFold=true)
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
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 SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
LLVM_ABI SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth=0) const
Return true if this function can prove that Op is never poison.
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
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 getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
LLVM_ABI bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
LLVM_ABI void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
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 getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
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 getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
LLVM_ABI std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
LLVM_ABI SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
LLVM_ABI void dump() const
LLVM_ABI bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
LLVM_ABI SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
LLVM_ABI std::optional< unsigned > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
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 getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
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...
iterator_range< allnodes_iterator > allnodes()
LLVM_ABI SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
LLVM_ABI bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
LLVM_ABI SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
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...
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
LLVM_ABI SDValue FoldConstantBuildVector(BuildVectorSDNode *BV, const SDLoc &DL, EVT DstEltVT)
Fold BUILD_VECTOR of constants/undefs to the destination type BUILD_VECTOR of constants/undefs elemen...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
const TargetLibraryInfo & getLibInfo() const
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
LLVM_ABI SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
LLVM_ABI bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
LLVM_ABI void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
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...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
LLVM_ABI bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
LLVM_ABI OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
LLVM_ABI bool expandMultipleResultFPLibCall(RTLIB::Libcall LC, SDNode *Node, SmallVectorImpl< SDValue > &Results, std::optional< unsigned > CallRetResNo={})
Expands a node with multiple results to an FP or vector libcall.
LLVMContext * getContext() const
LLVM_ABI SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags)
Get the specified node if it's already available, or else return NULL.
LLVM_ABI void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
LLVM_ABI std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
LLVM_ABI bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
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...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:148
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
const TargetMachine & getTargetMachine() const
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...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
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.
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...
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes) const
Determines the optimal series of memory ops to replace the memset / memcpy.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetOptions Options
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:612
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
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:295
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:198
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:231
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI void set(Value *Val)
Definition Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
Provides info so a possible vectorization of a function can be computed.
bool isMasked() const
StringRef getVectorFnName() const
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:194
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:169
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition TypeSize.h:177
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:237
A raw_ostream that writes to an std::string.
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3131
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3118
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3108
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3182
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3123
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2268
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3173
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3009
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2273
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3103
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3113
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
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:801
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:774
@ TargetConstantPool
Definition ISDOpcodes.h:184
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:504
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:231
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition ISDOpcodes.h:525
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:270
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:587
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:765
@ TargetBlockAddress
Definition ISDOpcodes.h:186
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:289
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:515
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:835
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ GlobalAddress
Definition ISDOpcodes.h:88
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:862
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:571
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:738
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:892
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:826
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:706
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:656
@ TargetExternalSymbol
Definition ISDOpcodes.h:185
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:773
@ TargetJumpTable
Definition ISDOpcodes.h:183
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition ISDOpcodes.h:193
@ SETCCCARRY
Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but op #2 is a boolean indicating ...
Definition ISDOpcodes.h:809
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:682
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:528
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:369
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:778
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:228
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:242
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:663
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:225
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:180
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:695
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:756
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:636
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:601
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:563
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:219
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:832
@ TargetConstantFP
Definition ISDOpcodes.h:175
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:793
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ TargetFrameIndex
Definition ISDOpcodes.h:182
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:881
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:870
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:718
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:787
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:323
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:493
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:908
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:174
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:498
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:730
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:200
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:726
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:701
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:299
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:672
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:236
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:552
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition ISDOpcodes.h:648
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:941
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:690
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:903
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:927
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:838
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:815
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:521
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:713
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:333
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:208
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:181
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:543
LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
bool isExtOpcode(unsigned Opcode)
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
LLVM_ABI bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
LLVM_ABI bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
LLVM_ABI std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
LLVM_ABI bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
LLVM_ABI bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
LLVM_ABI NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
LLVM_ABI bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
LLVM_ABI bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
LLVM_ABI bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
LLVM_ABI NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
LLVM_ABI Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:666
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:355
@ Offset
Definition DWP.cpp:477
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
LLVM_ABI SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1705
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1607
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2452
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:279
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:738
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:289
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1643
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2116
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:252
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
auto cast_or_null(const Y &Val)
Definition Casting.h:720
void * PointerTy
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1589
LLVM_ABI bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1555
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1712
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1598
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:342
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:288
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1624
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1629
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1719
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
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...
LLVM_ABI SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ABI bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1579
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:543
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1815
constexpr unsigned BitWidth
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition Analysis.cpp:723
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1877
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:212
LLVM_ABI bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:583
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:208
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1616
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1656
LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:384
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:760
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:780
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:777
static LLVM_ABI const fltSemantics & IEEEsingle() LLVM_READNONE
Definition APFloat.cpp:266
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:294
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:307
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:304
static constexpr roundingMode rmTowardZero
Definition APFloat.h:308
static LLVM_ABI const fltSemantics & IEEEquad() LLVM_READNONE
Definition APFloat.cpp:268
static LLVM_ABI const fltSemantics & IEEEdouble() LLVM_READNONE
Definition APFloat.cpp:267
static LLVM_ABI const fltSemantics & IEEEhalf() LLVM_READNONE
Definition APFloat.cpp:264
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:306
static LLVM_ABI const fltSemantics & BFloat() LLVM_READNONE
Definition APFloat.cpp:265
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:320
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
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
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:137
intptr_t getRawBits() const
Definition ValueTypes.h:512
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
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:121
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
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 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 bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:256
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:142
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:308
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:453
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:301
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:255
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:124
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:242
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:274
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:119
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:161
KnownBits byteSwap() const
Definition KnownBits.h:514
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:289
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:518
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:233
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:172
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:321
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:225
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:311
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:196
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:326
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:53
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:280
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:219
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:167
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:117
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
These are IR-level optimization flags that may be propagated to SDNodes.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
virtual void NodeDeleted(SDNode *N, SDNode *E)
The node N that was deleted and, if E is not null, an equivalent node E that replaced it.
virtual void NodeInserted(SDNode *N)
The node N that was inserted.
virtual void NodeUpdated(SDNode *N)
The node N that was updated.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)